communicate_twitter_job_spec.rb 8.6 KB

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