twitter_publish_agent_spec.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. require 'rails_helper'
  2. describe Agents::TwitterPublishAgent do
  3. before do
  4. @opts = {
  5. :username => "HuginnBot",
  6. :expected_update_period_in_days => "2",
  7. :consumer_key => "---",
  8. :consumer_secret => "---",
  9. :oauth_token => "---",
  10. :oauth_token_secret => "---",
  11. :message => "{{text}}"
  12. }
  13. @checker = Agents::TwitterPublishAgent.new(:name => "HuginnBot", :options => @opts)
  14. @checker.service = services(:generic)
  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. allow_any_instance_of(Agents::TwitterPublishAgent).to receive(:publish_tweet) { |message|
  23. @sent_messages << message
  24. OpenStruct.new(:id => 454209588376502272)
  25. }
  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::TwitterPublishAgent.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 '#working?' do
  43. it 'checks if events have been received within the expected receive period' do
  44. expect(@checker).not_to be_working # No events received
  45. Agents::TwitterPublishAgent.async_receive(@checker.id, [@event.id])
  46. expect(@checker.reload).to be_working # Just received events
  47. two_days_from_now = 2.days.from_now
  48. allow(Time).to receive(:now) { two_days_from_now }
  49. expect(@checker.reload).not_to be_working # More time has passed than the expected receive period without any new events
  50. end
  51. end
  52. end