communicate_twitter_job_spec.rb 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. require 'rails_helper'
  2. RSpec.describe CommunicateTwitterJob, type: :job do
  3. let(:article) { create(:twitter_article, **(try(:factory_options) || {})) }
  4. describe 'core behavior', :use_vcr do
  5. context 'for tweets' do
  6. let(:tweet_attributes) do
  7. {
  8. 'mention_ids' => [],
  9. 'geo' => {},
  10. 'retweeted' => false,
  11. 'possibly_sensitive' => false,
  12. 'in_reply_to_user_id' => nil,
  13. 'place' => {},
  14. 'retweet_count' => 0,
  15. 'source' => '<a href="https://zammad.com/" rel="nofollow">zammad</a>',
  16. 'favorited' => false,
  17. 'truncated' => false,
  18. }
  19. end
  20. let(:links_array) do
  21. [{
  22. url: 'https://twitter.com/_/status/1244937367435108360',
  23. target: '_blank',
  24. name: 'on Twitter',
  25. }]
  26. end
  27. it 'increments the "delivery_retry" preference' do
  28. expect { described_class.perform_now(article.id) }
  29. .to change { article.reload.preferences[:delivery_retry] }.to(1)
  30. end
  31. it 'dispatches the tweet' do
  32. described_class.perform_now(article.id)
  33. expect(WebMock)
  34. .to have_requested(:post, 'https://api.twitter.com/1.1/statuses/update.json')
  35. .with(body: "in_reply_to_status_id&status=#{CGI.escape(article.body)}" )
  36. end
  37. it 'updates the article with tweet attributes' do
  38. expect { described_class.perform_now(article.id) }
  39. .to change { article.reload.message_id }.to('1244937367435108360')
  40. .and change { article.reload.preferences[:twitter] }.to(hash_including(tweet_attributes))
  41. .and change { article.reload.preferences[:links] }.to(links_array)
  42. end
  43. it 'sets the appropriate delivery status attributes' do
  44. expect { described_class.perform_now(article.id) }
  45. .to change { article.reload.preferences[:delivery_status] }.to('success')
  46. .and change { article.reload.preferences[:delivery_status_date] }.to(an_instance_of(ActiveSupport::TimeWithZone))
  47. .and not_change { article.reload.preferences[:delivery_status_message] }.from(nil)
  48. end
  49. context 'with a user mention' do
  50. let(:factory_options) { { body: '@twitter @twitterlive Don’t mind me, just testing the API' } }
  51. it 'updates the article with tweet recipients' do
  52. expect { described_class.perform_now(article.id) }
  53. .to change { article.reload.to }.to('@Twitter @TwitterLive')
  54. end
  55. end
  56. end
  57. context 'for DMs' do
  58. let(:article) { create(:twitter_dm_article, :pending_delivery, recipient: recipient, body: 'Please ignore this message.') }
  59. let(:recipient) { create(:twitter_authorization, uid: '2688148651', username: 'AirbnbHelp') }
  60. let(:request_body) do
  61. {
  62. event: {
  63. type: 'message_create',
  64. message_create: {
  65. target: { recipient_id: recipient.uid },
  66. message_data: { text: article.body }
  67. }
  68. }
  69. }.to_json
  70. end
  71. let(:dm_attributes) do
  72. {
  73. 'recipient_id' => recipient.uid,
  74. 'sender_id' => '1205290247124217856',
  75. }
  76. end
  77. let(:links_array) do
  78. [{
  79. url: "https://twitter.com/messages/#{recipient.uid}-1205290247124217856",
  80. target: '_blank',
  81. name: 'on Twitter',
  82. }]
  83. end
  84. it 'increments the "delivery_retry" preference' do
  85. expect { described_class.perform_now(article.id) }
  86. .to change { article.reload.preferences[:delivery_retry] }.to(1)
  87. end
  88. it 'dispatches the DM' do
  89. described_class.perform_now(article.id)
  90. expect(WebMock)
  91. .to have_requested(:post, 'https://api.twitter.com/1.1/direct_messages/events/new.json')
  92. .with(body: request_body)
  93. end
  94. it 'updates the article with DM attributes' do
  95. expect { described_class.perform_now(article.id) }
  96. .to change { article.reload.message_id }.to('1244953398509617156')
  97. .and change { article.reload.preferences[:twitter] }.to(hash_including(dm_attributes))
  98. .and change { article.reload.preferences[:links] }.to(links_array)
  99. end
  100. it 'sets the appropriate delivery status attributes' do
  101. expect { described_class.perform_now(article.id) }
  102. .to change { article.reload.preferences[:delivery_status] }.to('success')
  103. .and change { article.reload.preferences[:delivery_status_date] }.to(an_instance_of(ActiveSupport::TimeWithZone))
  104. .and not_change { article.reload.preferences[:delivery_status_message] }.from(nil)
  105. end
  106. end
  107. describe 'failure cases' do
  108. shared_examples 'for failure cases' do
  109. it 'raises an error and sets the appropriate delivery status messages' do
  110. expect { described_class.perform_now(article.id) }
  111. .to change { article.reload.preferences[:delivery_status] }.to('fail')
  112. .and change { article.reload.preferences[:delivery_status_date] }.to(an_instance_of(ActiveSupport::TimeWithZone))
  113. .and change { article.reload.preferences[:delivery_status_message] }.to(error_message)
  114. end
  115. end
  116. context 'when article.ticket.preferences["channel_id"] is nil' do
  117. before do
  118. article.ticket.preferences.delete(:channel_id)
  119. article.ticket.save
  120. end
  121. let(:error_message) { "Can't find ticket.preferences['channel_id'] for Ticket.find(#{article.ticket_id})" }
  122. include_examples 'for failure cases'
  123. end
  124. context 'if article.ticket.preferences["channel_id"] has been removed' do
  125. before { channel.destroy }
  126. let(:channel) { Channel.find(article.ticket.preferences[:channel_id]) }
  127. let(:error_message) { "No such channel id #{article.ticket.preferences['channel_id']}" }
  128. include_examples 'for failure cases'
  129. context 'and another suitable channel exists (matching on ticket.preferences[:channel_screen_name])' do
  130. let!(:new_channel) { create(:twitter_channel, custom_options: { user: { screen_name: channel.options[:user][:screen_name] } }) }
  131. it 'uses that channel' do
  132. described_class.perform_now(article.id)
  133. expect(WebMock)
  134. .to have_requested(:post, 'https://api.twitter.com/1.1/statuses/update.json')
  135. .with(body: "in_reply_to_status_id&status=#{CGI.escape(article.body)}" )
  136. end
  137. end
  138. end
  139. context 'if article.ticket.preferences["channel_id"] isn’t actually a twitter channel' do
  140. before do
  141. article.ticket.preferences[:channel_id] = create(:email_channel).id
  142. article.ticket.save
  143. end
  144. let(:error_message) { "Channel.find(#{article.ticket.preferences[:channel_id]}) isn't a twitter channel!" }
  145. include_examples 'for failure cases'
  146. end
  147. context 'when tweet dispatch fails (e.g., due to authentication error)' do
  148. before do
  149. article.ticket.preferences[:channel_id] = create(:twitter_channel, :invalid).id
  150. article.ticket.save
  151. end
  152. let(:error_message) { "Can't use Channel::Driver::Twitter: #<Twitter::Error::Unauthorized: Invalid or expired token.>" }
  153. include_examples 'for failure cases'
  154. end
  155. context 'when tweet comes back nil' do
  156. before do
  157. allow(Twitter::REST::Client).to receive(:new).with(any_args).and_return(client_double)
  158. allow(client_double).to receive(:update).with(any_args).and_return(nil)
  159. end
  160. let(:client_double) { double('Twitter::REST::Client') }
  161. let(:error_message) { 'Got no tweet!' }
  162. include_examples 'for failure cases'
  163. end
  164. context 'on the fourth time it fails' do
  165. before { Channel.find(article.ticket.preferences[:channel_id]).destroy }
  166. let(:error_message) { "No such channel id #{article.ticket.preferences['channel_id']}" }
  167. let(:factory_options) { { preferences: { delivery_retry: 3 } } }
  168. it 'adds a delivery failure note (article) to the ticket' do
  169. expect { described_class.perform_now(article.id) }
  170. .to change { article.ticket.reload.articles.count }.by(1)
  171. expect(Ticket::Article.last.attributes).to include(
  172. 'content_type' => 'text/plain',
  173. 'body' => "Unable to send tweet: #{error_message}",
  174. 'internal' => true,
  175. 'sender_id' => Ticket::Article::Sender.find_by(name: 'System').id,
  176. 'type_id' => Ticket::Article::Type.find_by(name: 'note').id,
  177. 'preferences' => {
  178. 'delivery_article_id_related' => article.id,
  179. 'delivery_message' => true,
  180. },
  181. )
  182. end
  183. end
  184. end
  185. end
  186. end