weibo_publish_agent_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # encoding: utf-8
  2. require 'rails_helper'
  3. describe Agents::WeiboPublishAgent do
  4. before do
  5. @opts = {
  6. :uid => "1234567",
  7. :expected_update_period_in_days => "2",
  8. :app_key => "---",
  9. :app_secret => "---",
  10. :access_token => "---",
  11. :message_path => "text",
  12. :pic_path => "pic"
  13. }
  14. @checker = Agents::WeiboPublishAgent.new(:name => "Weibo Publisher", :options => @opts)
  15. @checker.user = users(:bob)
  16. @checker.save!
  17. @event = Event.new
  18. @event.agent = agents(:bob_weather_agent)
  19. @event.payload = { :text => 'Gonna rain..' }
  20. @event.save!
  21. @sent_messages = []
  22. @sent_pictures = []
  23. allow_any_instance_of(Agents::WeiboPublishAgent).to receive(:publish_tweet) { |agent, message| @sent_messages << message}
  24. allow_any_instance_of(Agents::WeiboPublishAgent).to receive(:publish_tweet_with_pic) { |agent, message, picture| @sent_pictures << picture}
  25. allow_any_instance_of(Agents::WeiboPublishAgent).to receive(:sleep)
  26. end
  27. describe '#receive' do
  28. it 'should publish any payload it receives' do
  29. event1 = Event.new
  30. event1.agent = agents(:bob_rain_notifier_agent)
  31. event1.payload = { :text => 'Gonna rain..' }
  32. event1.save!
  33. event2 = Event.new
  34. event2.agent = agents(:bob_weather_agent)
  35. event2.payload = { :text => 'More payload' }
  36. event2.save!
  37. Agents::WeiboPublishAgent.async_receive(@checker.id, [event1.id, event2.id])
  38. expect(@sent_messages.count).to eq(2)
  39. expect(@checker.events.count).to eq(2)
  40. end
  41. end
  42. describe '#receive a tweet' do
  43. it 'should publish a tweet after expanding any t.co urls' do
  44. event = Event.new
  45. event.agent = agents(:bob_twitter_user_agent)
  46. event.payload = JSON.parse(File.read(Rails.root.join("spec/data_fixtures/one_tweet.json")))
  47. event.save!
  48. Agents::WeiboPublishAgent.async_receive(@checker.id, [event.id])
  49. expect(@sent_messages.count).to eq(1)
  50. expect(@sent_pictures.count).to eq(0)
  51. expect(@checker.events.count).to eq(1)
  52. expect(@sent_messages.first.include?("t.co")).not_to be_truthy
  53. end
  54. end
  55. describe '#receive payload with picture url' do
  56. before do
  57. stub_request(:head, 'http://valid.image').to_return(status: 200, headers: {"Content-Type" => "image/jpeg"})
  58. stub_request(:head, 'http://invalid.image').to_return(status: 200, headers: {"Content-Type" => "text/html"})
  59. end
  60. it 'should publish a tweet without a picture if image url is not valid' do
  61. event = Event.new
  62. event.agent = agents(:bob_weather_agent)
  63. event.payload = {:text => 'whatever', :pic => 'http://invalid.image'}
  64. event.save!
  65. Agents::WeiboPublishAgent.async_receive(@checker.id, [event.id])
  66. expect(@sent_messages.count).to eq(1)
  67. expect(@sent_pictures.count).to eq(0)
  68. expect(@checker.events.count).to eq(1)
  69. end
  70. it 'should publish a tweet along with a picture if image url is valid' do
  71. event = Event.new
  72. event.agent = agents(:bob_weather_agent)
  73. event.payload = {:text => 'whatever', :pic => 'http://valid.image'}
  74. event.save!
  75. Agents::WeiboPublishAgent.async_receive(@checker.id, [event.id])
  76. expect(@sent_messages.count).to eq(0)
  77. expect(@sent_pictures.count).to eq(1)
  78. expect(@checker.events.count).to eq(1)
  79. end
  80. end
  81. describe '#working?' do
  82. it 'checks if events have been received within the expected receive period' do
  83. expect(@checker).not_to be_working # No events received
  84. Agents::WeiboPublishAgent.async_receive(@checker.id, [@event.id])
  85. expect(@checker.reload).to be_working # Just received events
  86. two_days_from_now = 2.days.from_now
  87. allow(Time).to receive(:now) { two_days_from_now }
  88. expect(@checker.reload).not_to be_working # More time has passed than the expected receive period without any new events
  89. end
  90. end
  91. end