weibo_publish_agent_spec.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # encoding: utf-8
  2. require 'spec_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. }
  13. @checker = Agents::WeiboPublishAgent.new(:name => "Weibo Publisher", :options => @opts)
  14. @checker.user = users(:bob)
  15. @checker.save!
  16. @event = Event.new
  17. @event.agent = agents(:bob_weather_agent)
  18. @event.payload = { :text => 'Gonna rain..' }
  19. @event.save!
  20. @sent_messages = []
  21. stub.any_instance_of(Agents::WeiboPublishAgent).publish_tweet { |message| @sent_messages << message}
  22. end
  23. describe '#receive' do
  24. it 'should publish any payload it receives' do
  25. event1 = Event.new
  26. event1.agent = agents(:bob_rain_notifier_agent)
  27. event1.payload = { :text => 'Gonna rain..' }
  28. event1.save!
  29. event2 = Event.new
  30. event2.agent = agents(:bob_weather_agent)
  31. event2.payload = { :text => 'More payload' }
  32. event2.save!
  33. Agents::WeiboPublishAgent.async_receive(@checker.id, [event1.id, event2.id])
  34. expect(@sent_messages.count).to eq(2)
  35. expect(@checker.events.count).to eq(2)
  36. end
  37. end
  38. describe '#receive a tweet' do
  39. it 'should publish a tweet after expanding any t.co urls' do
  40. event = Event.new
  41. event.agent = agents(:bob_twitter_user_agent)
  42. event.payload = JSON.parse(File.read(Rails.root.join("spec/data_fixtures/one_tweet.json")))
  43. event.save!
  44. Agents::WeiboPublishAgent.async_receive(@checker.id, [event.id])
  45. expect(@sent_messages.count).to eq(1)
  46. expect(@checker.events.count).to eq(1)
  47. expect(@sent_messages.first.include?("t.co")).not_to be_truthy
  48. end
  49. end
  50. describe '#working?' do
  51. it 'checks if events have been received within the expected receive period' do
  52. expect(@checker).not_to be_working # No events received
  53. Agents::WeiboPublishAgent.async_receive(@checker.id, [@event.id])
  54. expect(@checker.reload).to be_working # Just received events
  55. two_days_from_now = 2.days.from_now
  56. stub(Time).now { two_days_from_now }
  57. expect(@checker.reload).not_to be_working # More time has passed than the expected receive period without any new events
  58. end
  59. end
  60. end