twitter_action_agent_spec.rb 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. require 'rails_helper'
  2. describe Agents::TwitterActionAgent do
  3. describe '#receive' do
  4. before do
  5. @event1 = Event.new
  6. @event1.agent = agents(:bob_twitter_user_agent)
  7. @event1.payload = { id: 123, text: 'So awesome.. gotta retweet' }
  8. @event1.save!
  9. @tweet1 = Twitter::Tweet.new(
  10. id: @event1.payload[:id],
  11. text: @event1.payload[:text]
  12. )
  13. @event2 = Event.new
  14. @event2.agent = agents(:bob_twitter_user_agent)
  15. @event2.payload = { id: 456, text: 'Something Justin Bieber said' }
  16. @event2.save!
  17. @tweet2 = Twitter::Tweet.new(
  18. id: @event2.payload[:id],
  19. text: @event2.payload[:text]
  20. )
  21. end
  22. context 'when set up to retweet' do
  23. before do
  24. @agent = build_agent(
  25. 'favorite' => 'false',
  26. 'retweet' => 'true',
  27. 'emit_error_events' => 'true'
  28. )
  29. @agent.save!
  30. end
  31. context 'when the twitter client succeeds retweeting' do
  32. it 'should retweet the tweets from the payload' do
  33. mock(@agent.twitter).retweet([@tweet1, @tweet2])
  34. @agent.receive([@event1, @event2])
  35. end
  36. end
  37. context 'when the twitter client fails retweeting' do
  38. it 'creates an event with tweet info and the error message' do
  39. stub(@agent.twitter).retweet(anything) {
  40. raise Twitter::Error.new('uh oh')
  41. }
  42. @agent.receive([@event1, @event2])
  43. failure_event = @agent.events.last
  44. expect(failure_event.payload[:error]).to eq('uh oh')
  45. expect(failure_event.payload[:tweets]).to eq(
  46. {
  47. @event1.payload[:id].to_s => @event1.payload[:text],
  48. @event2.payload[:id].to_s => @event2.payload[:text]
  49. }
  50. )
  51. expect(failure_event.payload[:agent_ids]).to match_array(
  52. [@event1.agent_id, @event2.agent_id]
  53. )
  54. expect(failure_event.payload[:event_ids]).to match_array(
  55. [@event2.id, @event1.id]
  56. )
  57. end
  58. end
  59. end
  60. context 'when set up to favorite' do
  61. before do
  62. @agent = build_agent(
  63. 'favorite' => 'true',
  64. 'retweet' => 'false',
  65. 'emit_error_events' => 'true'
  66. )
  67. @agent.save!
  68. end
  69. context 'when the twitter client succeeds favoriting' do
  70. it 'should favorite the tweets from the payload' do
  71. mock(@agent.twitter).favorite([@tweet1, @tweet2])
  72. @agent.receive([@event1, @event2])
  73. end
  74. end
  75. context 'when the twitter client fails retweeting' do
  76. it 'creates an event with tweet info and the error message' do
  77. stub(@agent.twitter).favorite(anything) {
  78. raise Twitter::Error.new('uh oh')
  79. }
  80. @agent.receive([@event1, @event2])
  81. failure_event = @agent.events.last
  82. expect(failure_event.payload[:error]).to eq('uh oh')
  83. expect(failure_event.payload[:tweets]).to eq(
  84. {
  85. @event1.payload[:id].to_s => @event1.payload[:text],
  86. @event2.payload[:id].to_s => @event2.payload[:text]
  87. }
  88. )
  89. expect(failure_event.payload[:agent_ids]).to match_array(
  90. [@event1.agent_id, @event2.agent_id]
  91. )
  92. expect(failure_event.payload[:event_ids]).to match_array(
  93. [@event2.id, @event1.id]
  94. )
  95. end
  96. end
  97. end
  98. context 'with emit_error_events set to false' do
  99. it 'does re-raises the exception on failure' do
  100. agent = build_agent
  101. stub(agent.twitter).retweet(anything) {
  102. raise Twitter::Error.new('uh oh')
  103. }
  104. expect { agent.receive([@event1]) }.to raise_error(StandardError, /uh oh/)
  105. end
  106. end
  107. end
  108. describe "#validate_options" do
  109. it 'the default options are valid' do
  110. agent = build_agent(described_class.new.default_options)
  111. expect(agent).to be_valid
  112. end
  113. context 'emit_error_events' do
  114. it 'can be set to true' do
  115. agent = build_agent(described_class.new.default_options.merge('emit_error_events' => 'true'))
  116. expect(agent).to be_valid
  117. end
  118. it 'must be a boolean' do
  119. agent = build_agent(described_class.new.default_options.merge('emit_error_events' => 'notbolean'))
  120. expect(agent).not_to be_valid
  121. end
  122. end
  123. it 'expected_receive_period_in_days must be set' do
  124. agent = build_agent(described_class.new.default_options.merge('expected_receive_period_in_days' => ''))
  125. expect(agent).not_to be_valid
  126. end
  127. context 'when set up to neither favorite or retweet' do
  128. it 'is invalid' do
  129. agent = build_agent(
  130. 'favorite' => 'false',
  131. 'retweet' => 'false',
  132. )
  133. expect(agent).not_to be_valid
  134. end
  135. end
  136. end
  137. describe '#working?' do
  138. before do
  139. stub.any_instance_of(Twitter::REST::Client).retweet(anything)
  140. end
  141. it 'checks if events have been received within the expected time period' do
  142. agent = build_agent
  143. agent.save!
  144. expect(agent).not_to be_working # No events received
  145. described_class.async_receive(agent.id, [events(:bob_website_agent_event)])
  146. expect(agent.reload).to be_working # Just received events
  147. two_days_from_now = 2.days.from_now
  148. stub(Time).now { two_days_from_now }
  149. expect(agent.reload).not_to be_working # Too much time has passed
  150. end
  151. end
  152. def build_agent(options = {})
  153. described_class.new do |agent|
  154. agent.name = 'twitter stuff'
  155. agent.options = agent.default_options.merge(options)
  156. agent.service = services(:generic)
  157. agent.user = users(:bob)
  158. end
  159. end
  160. end