twitter_spec.rb 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Channel::Driver::Twitter, required_envs: %w[TWITTER_CONSUMER_KEY TWITTER_CONSUMER_SECRET TWITTER_OAUTH_TOKEN TWITTER_OAUTH_TOKEN_SECRET TWITTER_DM_REAL_RECIPIENT TWITTER_USER_ID] do
  4. subject(:channel) { create(:twitter_channel) }
  5. describe '#process', current_user_id: 1 do
  6. # Twitter channels must be configured to know whose account they're monitoring.
  7. subject(:channel) do
  8. create(:twitter_channel, custom_options: { user: { id: payload[:for_user_id] } })
  9. end
  10. let(:payload) { YAML.safe_load_file(payload_file, permitted_classes: [ActiveSupport::HashWithIndifferentAccess]) }
  11. # https://git.zammad.com/zammad/zammad/-/issues/305
  12. shared_examples 'for user processing' do
  13. let(:sender_attributes) do
  14. {
  15. 'login' => sender_profile[:screen_name],
  16. 'firstname' => sender_profile[:name].capitalize,
  17. 'web' => sender_profile[:url],
  18. 'note' => sender_profile[:description],
  19. 'address' => sender_profile[:location],
  20. 'image_source' => sender_profile[:profile_image_url],
  21. }
  22. end
  23. let(:avatar_attributes) do
  24. {
  25. 'object_lookup_id' => ObjectLookup.by_name('User'),
  26. 'deletable' => true,
  27. 'source' => 'twitter',
  28. 'source_url' => sender_profile[:profile_image_url],
  29. }
  30. end
  31. let(:authorization_attributes) do
  32. {
  33. 'uid' => sender_profile[:id],
  34. 'username' => sender_profile[:screen_name],
  35. 'provider' => 'twitter',
  36. }
  37. end
  38. context 'from unknown user' do
  39. it 'creates a User record for the sender' do
  40. expect { channel.process(payload) }
  41. .to change(User, :count).by(1)
  42. .and change { User.exists?(sender_attributes) }.to(true)
  43. end
  44. it 'creates an Avatar record for the sender', :use_vcr do
  45. # Why 2, and not 1? Avatar.add auto-generates a default (source: 'init') record
  46. # before actually adding the specified (source: 'twitter') one.
  47. expect { channel.process(payload) }
  48. .to change(Avatar, :count).by_at_least(1)
  49. .and change { Avatar.exists?(avatar_attributes) }.to(true)
  50. expect(User.last.image).to eq(Avatar.last.store_hash)
  51. end
  52. it 'creates an Authorization record for the sender' do
  53. expect { channel.process(payload) }
  54. .to change(Authorization, :count).by(1)
  55. .and change { Authorization.exists?(authorization_attributes) }.to(true)
  56. end
  57. end
  58. context 'from known user' do
  59. let!(:user) { create(:user) }
  60. let!(:avatar) { create(:avatar, o_id: user.id, object_lookup_id: ObjectLookup.by_name('User'), source: 'twitter') }
  61. let!(:authorization) do
  62. Authorization.create(user_id: user.id, uid: sender_profile[:id], provider: 'twitter')
  63. end
  64. it 'updates the sender’s existing User record' do
  65. expect { channel.process(payload) }
  66. .to not_change(User, :count)
  67. .and not_change { user.reload.attributes.slice('login', 'firstname') }
  68. .and change { User.exists?(sender_attributes.except('login', 'firstname')) }.to(true)
  69. end
  70. it 'updates the sender’s existing Avatar record', :use_vcr do
  71. expect { channel.process(payload) }
  72. .to not_change(Avatar, :count)
  73. .and change { Avatar.exists?(avatar_attributes) }.to(true)
  74. expect(user.reload.image).to eq(avatar.reload.store_hash)
  75. end
  76. it 'updates the sender’s existing Authorization record' do
  77. expect { channel.process(payload) }
  78. .to not_change(Authorization, :count)
  79. .and change { Authorization.exists?(authorization_attributes) }.to(true)
  80. end
  81. end
  82. end
  83. context 'for incoming DM' do
  84. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/direct_message-incoming.yml') }
  85. include_examples 'for user processing' do
  86. # Payload sent by Twitter is { ..., users: [{ <uid>: <sender> }, { <uid>: <receiver> }] }
  87. let(:sender_profile) { payload[:users].values.first }
  88. end
  89. describe 'ticket creation' do
  90. let(:ticket_attributes) do
  91. # NOTE: missing "customer_id" (because the value is generated as part of the #process method)
  92. {
  93. 'title' => title,
  94. 'group_id' => channel.options[:sync][:direct_messages][:group_id],
  95. 'state' => Ticket::State.find_by(default_create: true),
  96. 'priority' => Ticket::Priority.find_by(default_create: true),
  97. 'preferences' => {
  98. 'channel_id' => channel.id,
  99. 'channel_screen_name' => channel.options[:user][:screen_name],
  100. },
  101. }
  102. end
  103. let(:title) { payload[:direct_message_events].first[:message_create][:message_data][:text] }
  104. it 'creates a new ticket' do
  105. expect { channel.process(payload) }
  106. .to change(Ticket, :count).by(1)
  107. .and change { Ticket.exists?(ticket_attributes) }.to(true)
  108. end
  109. context 'for duplicate messages' do
  110. before do
  111. channel.process(
  112. YAML.safe_load_file(payload_file, permitted_classes: [ActiveSupport::HashWithIndifferentAccess])
  113. )
  114. end
  115. it 'does not create duplicate ticket' do
  116. expect { channel.process(payload) }
  117. .to not_change(Ticket, :count)
  118. .and not_change(Ticket::Article, :count)
  119. end
  120. end
  121. context 'for message longer than 80 chars' do
  122. before { payload[:direct_message_events].first[:message_create][:message_data][:text] = 'a' * 81 }
  123. let(:title) { "#{'a' * 80}..." }
  124. it 'creates ticket with truncated title' do
  125. expect { channel.process(payload) }
  126. .to change(Ticket, :count).by(1)
  127. .and change { Ticket.exists?(ticket_attributes) }.to(true)
  128. end
  129. end
  130. context 'in reply to existing thread/ticket' do
  131. # import parent DM
  132. before do
  133. channel.process(
  134. YAML.safe_load(
  135. Rails.root.join('test/data/twitter/webhook_events/direct_message-incoming.yml').read,
  136. permitted_classes: [ActiveSupport::HashWithIndifferentAccess]
  137. )
  138. )
  139. end
  140. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/direct_message-incoming_2.yml') }
  141. it 'uses existing ticket' do
  142. expect { channel.process(payload) }
  143. .to not_change(Ticket, :count)
  144. .and not_change { Ticket.last.state }
  145. end
  146. context 'marked "closed" / "merged" / "removed"' do
  147. before { Ticket.last.update(state: Ticket::State.find_by(name: 'closed')) }
  148. it 'creates a new ticket' do
  149. expect { channel.process(payload) }
  150. .to change(Ticket, :count).by(1)
  151. .and change { Ticket.exists?(ticket_attributes) }.to(true)
  152. end
  153. end
  154. context 'marked "pending reminder" / "pending close"' do
  155. before { Ticket.last.update(state: Ticket::State.find_by(name: 'pending reminder')) }
  156. it 'sets existing ticket to "open"' do
  157. expect { channel.process(payload) }
  158. .to not_change(Ticket, :count)
  159. .and change { Ticket.last.state.name }.to('open')
  160. end
  161. end
  162. end
  163. end
  164. describe 'article creation' do
  165. let(:article_attributes) do
  166. # NOTE: missing "ticket_id" (because the value is generated as part of the #process method)
  167. {
  168. 'from' => "@#{payload[:users].values.first[:screen_name]}",
  169. 'to' => "@#{payload[:users].values.second[:screen_name]}",
  170. 'body' => payload[:direct_message_events].first[:message_create][:message_data][:text],
  171. 'message_id' => payload[:direct_message_events].first[:id],
  172. 'in_reply_to' => nil,
  173. 'type_id' => Ticket::Article::Type.find_by(name: 'twitter direct-message').id,
  174. 'sender_id' => Ticket::Article::Sender.find_by(name: 'Customer').id,
  175. 'internal' => false,
  176. 'preferences' => { 'twitter' => twitter_prefs, 'links' => link_array }
  177. }
  178. end
  179. let(:twitter_prefs) do
  180. {
  181. 'created_at' => payload[:direct_message_events].first[:created_timestamp],
  182. 'recipient_id' => payload[:direct_message_events].first[:message_create][:target][:recipient_id],
  183. 'recipient_screen_name' => payload[:users].values.second[:screen_name],
  184. 'sender_id' => payload[:direct_message_events].first[:message_create][:sender_id],
  185. 'sender_screen_name' => payload[:users].values.first[:screen_name],
  186. 'app_id' => payload[:apps]&.values&.first&.dig(:app_id),
  187. 'app_name' => payload[:apps]&.values&.first&.dig(:app_name),
  188. 'geo' => {},
  189. 'place' => {},
  190. }
  191. end
  192. let(:link_array) do
  193. [
  194. {
  195. 'url' => "https://twitter.com/messages/#{user_ids.map(&:to_i).sort.join('-')}",
  196. 'target' => '_blank',
  197. 'name' => 'on Twitter',
  198. },
  199. ]
  200. end
  201. let(:user_ids) { payload[:users].values.pluck(:id) }
  202. it 'creates a new article' do
  203. expect { channel.process(payload) }
  204. .to change(Ticket::Article, :count).by(1)
  205. .and change { Ticket::Article.exists?(article_attributes) }.to(true)
  206. end
  207. context 'for duplicate messages' do
  208. before do
  209. channel.process(
  210. YAML.safe_load_file(payload_file, permitted_classes: [ActiveSupport::HashWithIndifferentAccess])
  211. )
  212. end
  213. it 'does not create duplicate article' do
  214. expect { channel.process(payload) }
  215. .to not_change(Ticket::Article, :count)
  216. end
  217. end
  218. context 'when message contains shortened (t.co) url' do
  219. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/direct_message-incoming_with_url.yml') }
  220. it 'replaces the t.co url for the original' do
  221. expect { channel.process(payload) }
  222. .to change { Ticket::Article.exists?(body: <<~BODY.chomp) }.to(true)
  223. Did you know about this? https://en.wikipedia.org/wiki/Frankenstein#Composition
  224. BODY
  225. end
  226. end
  227. end
  228. end
  229. context 'for outgoing DM' do
  230. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/direct_message-outgoing.yml') }
  231. describe 'ticket creation' do
  232. let(:ticket_attributes) do
  233. # NOTE: missing "customer_id" (because User.last changes before and after the method is called)
  234. {
  235. 'title' => payload[:direct_message_events].first[:message_create][:message_data][:text],
  236. 'group_id' => channel.options[:sync][:direct_messages][:group_id],
  237. 'state' => Ticket::State.find_by(name: 'closed'),
  238. 'priority' => Ticket::Priority.find_by(default_create: true),
  239. 'preferences' => {
  240. 'channel_id' => channel.id,
  241. 'channel_screen_name' => channel.options[:user][:screen_name],
  242. },
  243. }
  244. end
  245. it 'creates a closed ticket' do
  246. expect { channel.process(payload) }
  247. .to change(Ticket, :count).by(1)
  248. .and change { Ticket.exists?(ticket_attributes) }.to(true)
  249. end
  250. end
  251. describe 'article creation' do
  252. let(:article_attributes) do
  253. # NOTE: missing "ticket_id" (because the value is generated as part of the #process method)
  254. {
  255. 'from' => "@#{payload[:users].values.first[:screen_name]}",
  256. 'to' => "@#{payload[:users].values.second[:screen_name]}",
  257. 'body' => payload[:direct_message_events].first[:message_create][:message_data][:text],
  258. 'message_id' => payload[:direct_message_events].first[:id],
  259. 'in_reply_to' => nil,
  260. 'type_id' => Ticket::Article::Type.find_by(name: 'twitter direct-message').id,
  261. 'sender_id' => Ticket::Article::Sender.find_by(name: 'Customer').id,
  262. 'internal' => false,
  263. 'preferences' => { 'twitter' => twitter_prefs, 'links' => link_array }
  264. }
  265. end
  266. let(:twitter_prefs) do
  267. {
  268. 'created_at' => payload[:direct_message_events].first[:created_timestamp],
  269. 'recipient_id' => payload[:direct_message_events].first[:message_create][:target][:recipient_id],
  270. 'recipient_screen_name' => payload[:users].values.second[:screen_name],
  271. 'sender_id' => payload[:direct_message_events].first[:message_create][:sender_id],
  272. 'sender_screen_name' => payload[:users].values.first[:screen_name],
  273. 'app_id' => payload[:apps]&.values&.first&.dig(:app_id),
  274. 'app_name' => payload[:apps]&.values&.first&.dig(:app_name),
  275. 'geo' => {},
  276. 'place' => {},
  277. }
  278. end
  279. let(:link_array) do
  280. [
  281. {
  282. 'url' => "https://twitter.com/messages/#{user_ids.map(&:to_i).sort.join('-')}",
  283. 'target' => '_blank',
  284. 'name' => 'on Twitter',
  285. },
  286. ]
  287. end
  288. let(:user_ids) { payload[:users].values.pluck(:id) }
  289. it 'creates a new article' do
  290. expect { channel.process(payload) }
  291. .to change(Ticket::Article, :count).by(1)
  292. .and change { Ticket::Article.exists?(article_attributes) }.to(true)
  293. end
  294. context 'when message contains shortened (t.co) url' do
  295. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/direct_message-incoming_with_url.yml') }
  296. it 'replaces the t.co url for the original' do
  297. expect { channel.process(payload) }
  298. .to change { Ticket::Article.exists?(body: <<~BODY.chomp) }.to(true)
  299. Did you know about this? https://en.wikipedia.org/wiki/Frankenstein#Composition
  300. BODY
  301. end
  302. end
  303. context 'when message contains a media attachment (e.g., JPG)' do
  304. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/direct_message-incoming_with_media.yml') }
  305. it 'does not store it as an attachment on the article' do
  306. channel.process(payload)
  307. expect(Ticket::Article.last.attachments).to be_empty
  308. end
  309. end
  310. end
  311. end
  312. context 'for incoming tweet' do
  313. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/tweet_create-user_mention.yml') }
  314. include_examples 'for user processing' do
  315. # Payload sent by Twitter is { ..., tweet_create_events: [{ ..., user: <author> }] }
  316. let(:sender_profile) { payload[:tweet_create_events].first[:user] }
  317. end
  318. describe 'ticket creation' do
  319. let(:ticket_attributes) do
  320. # NOTE: missing "customer_id" (because User.last changes before and after the method is called)
  321. {
  322. 'title' => payload[:tweet_create_events].first[:text],
  323. 'group_id' => channel.options[:sync][:direct_messages][:group_id],
  324. 'state' => Ticket::State.find_by(default_create: true),
  325. 'priority' => Ticket::Priority.find_by(default_create: true),
  326. 'preferences' => {
  327. 'channel_id' => channel.id,
  328. 'channel_screen_name' => channel.options[:user][:screen_name],
  329. },
  330. }
  331. end
  332. it 'creates a new ticket' do
  333. expect { channel.process(payload) }
  334. .to change(Ticket, :count).by(1)
  335. end
  336. context 'for duplicate tweets' do
  337. before do
  338. channel.process(
  339. YAML.safe_load_file(payload_file, permitted_classes: [ActiveSupport::HashWithIndifferentAccess])
  340. )
  341. end
  342. it 'does not create duplicate ticket' do
  343. expect { channel.process(payload) }
  344. .to not_change(Ticket, :count)
  345. .and not_change(Ticket::Article, :count)
  346. end
  347. end
  348. context 'in response to existing tweet thread' do
  349. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/tweet_create-response.yml') }
  350. let(:parent_tweet_payload) do
  351. YAML.safe_load(
  352. Rails.root.join('test/data/twitter/webhook_events/tweet_create-user_mention.yml').read,
  353. permitted_classes: [ActiveSupport::HashWithIndifferentAccess]
  354. )
  355. end
  356. context 'that hasn’t been imported yet', :use_vcr do
  357. it 'creates a new ticket' do
  358. expect { channel.process(payload) }
  359. .to change(Ticket, :count).by(1)
  360. end
  361. it 'retrieves the parent tweet via the Twitter API' do
  362. expect { channel.process(payload) }
  363. .to change(Ticket::Article, :count).by(2)
  364. expect(Ticket::Article.second_to_last.body).to eq(parent_tweet_payload[:tweet_create_events].first[:text])
  365. end
  366. context 'after parent tweet has been deleted' do
  367. before do
  368. payload[:tweet_create_events].first[:in_reply_to_status_id] = 1207610954160037890 # rubocop:disable Style/NumericLiterals
  369. payload[:tweet_create_events].first[:in_reply_to_status_id_str] = '1207610954160037890'
  370. end
  371. it 'creates a new ticket' do
  372. expect { channel.process(payload) }
  373. .to change(Ticket, :count).by(1)
  374. end
  375. it 'silently ignores error when retrieving parent tweet' do
  376. expect { channel.process(payload) }.to not_raise_error
  377. end
  378. end
  379. end
  380. context 'that was previously imported' do
  381. # import parent tweet
  382. before { channel.process(parent_tweet_payload) }
  383. it 'uses existing ticket' do
  384. expect { channel.process(payload) }
  385. .to not_change(Ticket, :count)
  386. .and not_change { Ticket.last.state }
  387. end
  388. context 'and marked "closed" / "merged" / "removed" / "pending reminder" / "pending close"' do
  389. before { Ticket.last.update(state: Ticket::State.find_by(name: 'closed')) }
  390. it 'sets existing ticket to "open"' do
  391. expect { channel.process(payload) }
  392. .to not_change(Ticket, :count)
  393. .and change { Ticket.last.state.name }.to('open')
  394. end
  395. end
  396. end
  397. end
  398. end
  399. describe 'article creation' do
  400. let(:article_attributes) do
  401. # NOTE: missing "ticket_id" (because the value is generated as part of the #process method)
  402. {
  403. 'from' => "@#{payload[:tweet_create_events].first[:user][:screen_name]}",
  404. 'to' => "@#{payload[:tweet_create_events].first[:entities][:user_mentions].first[:screen_name]}",
  405. 'body' => payload[:tweet_create_events].first[:text],
  406. 'message_id' => payload[:tweet_create_events].first[:id_str],
  407. 'in_reply_to' => payload[:tweet_create_events].first[:in_reply_to_status_id],
  408. 'type_id' => Ticket::Article::Type.find_by(name: 'twitter status').id,
  409. 'sender_id' => Ticket::Article::Sender.find_by(name: 'Customer').id,
  410. 'internal' => false,
  411. 'preferences' => { 'twitter' => twitter_prefs, 'links' => link_array }
  412. }
  413. end
  414. let(:twitter_prefs) do
  415. {
  416. 'mention_ids' => payload[:tweet_create_events].first[:entities][:user_mentions].pluck(:id),
  417. 'geo' => payload[:tweet_create_events].first[:geo].to_h,
  418. 'retweeted' => payload[:tweet_create_events].first[:retweeted],
  419. 'possibly_sensitive' => payload[:tweet_create_events].first[:possibly_sensitive],
  420. 'in_reply_to_user_id' => payload[:tweet_create_events].first[:in_reply_to_user_id],
  421. 'place' => payload[:tweet_create_events].first[:place].to_h,
  422. 'retweet_count' => payload[:tweet_create_events].first[:retweet_count],
  423. 'source' => payload[:tweet_create_events].first[:source],
  424. 'favorited' => payload[:tweet_create_events].first[:favorited],
  425. 'truncated' => payload[:tweet_create_events].first[:truncated],
  426. }
  427. end
  428. let(:link_array) do
  429. [
  430. {
  431. 'url' => "https://twitter.com/_/status/#{payload[:tweet_create_events].first[:id]}",
  432. 'target' => '_blank',
  433. 'name' => 'on Twitter',
  434. },
  435. ]
  436. end
  437. it 'creates a new article' do
  438. expect { channel.process(payload) }
  439. .to change(Ticket::Article, :count).by(1)
  440. .and change { Ticket::Article.exists?(article_attributes) }.to(true)
  441. end
  442. context 'when message mentions multiple users' do
  443. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/tweet_create-user_mention_multiple.yml') }
  444. let(:mentionees) { "@#{payload[:tweet_create_events].first[:entities][:user_mentions].pluck(:screen_name).join(', @')}" }
  445. it 'records all mentionees in comma-separated "to" attribute' do
  446. expect { channel.process(payload) }
  447. .to change { Ticket::Article.exists?(to: mentionees) }.to(true)
  448. end
  449. end
  450. context 'when message exceeds 140 characters' do
  451. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/tweet_create-user_mention_extended.yml') }
  452. let(:full_body) { payload[:tweet_create_events].first[:extended_tweet][:full_text] }
  453. it 'records the full (extended) tweet body' do
  454. expect { channel.process(payload) }
  455. .to change { Ticket::Article.exists?(body: full_body) }.to(true)
  456. end
  457. end
  458. context 'when message contains shortened (t.co) url' do
  459. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/tweet_create-user_mention_with_url.yml') }
  460. it 'replaces the t.co url for the original' do
  461. expect { channel.process(payload) }
  462. .to change { Ticket::Article.exists?(body: <<~BODY.chomp) }.to(true)
  463. @ScruffyMcG https://zammad.org/
  464. BODY
  465. end
  466. end
  467. context 'when message contains a media attachment (e.g., JPG)' do
  468. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/tweet_create-user_mention_with_media.yml') }
  469. it 'replaces the t.co url for the original' do
  470. expect { channel.process(payload) }
  471. .to change { Ticket::Article.exists?(body: <<~BODY.chomp) }.to(true)
  472. @ScruffyMcG https://twitter.com/pennbrooke1/status/1209101446706122752/photo/1
  473. BODY
  474. end
  475. it 'stores it as an attachment on the article', :use_vcr do
  476. channel.process(payload)
  477. expect(Ticket::Article.last.attachments).to be_one
  478. end
  479. end
  480. context 'when message is a retweet' do
  481. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/tweet_create-retweet.yml') }
  482. context 'and "conversion of retweets" is enabled' do
  483. before do
  484. channel.options['sync']['track_retweets'] = true
  485. channel.save
  486. end
  487. it 'creates a new article' do
  488. expect { channel.process(payload) }
  489. .to change(Ticket::Article, :count).by(1)
  490. .and change { Ticket::Article.exists?(article_attributes) }.to(true)
  491. end
  492. end
  493. context 'and "conversion of retweets" is disabled' do
  494. before do
  495. channel.options['sync']['track_retweets'] = false
  496. channel.save
  497. end
  498. it 'does not create a new article' do
  499. expect { channel.process(payload) }
  500. .not_to change(Ticket::Article, :count)
  501. end
  502. end
  503. end
  504. end
  505. end
  506. context 'for outgoing tweet' do
  507. let(:payload_file) { Rails.root.join('test/data/twitter/webhook_events/tweet_create-user_mention_outgoing.yml') }
  508. describe 'ticket creation' do
  509. let(:ticket_attributes) do
  510. # NOTE: missing "customer_id" (because User.last changes before and after the method is called)
  511. {
  512. 'title' => payload[:tweet_create_events].first[:text],
  513. 'group_id' => channel.options[:sync][:direct_messages][:group_id],
  514. 'state' => Ticket::State.find_by(name: 'closed'),
  515. 'priority' => Ticket::Priority.find_by(default_create: true),
  516. 'preferences' => {
  517. 'channel_id' => channel.id,
  518. 'channel_screen_name' => channel.options[:user][:screen_name],
  519. },
  520. }
  521. end
  522. it 'creates a closed ticket' do
  523. expect { channel.process(payload) }
  524. .to change(Ticket, :count).by(1)
  525. end
  526. end
  527. describe 'article creation' do
  528. let(:article_attributes) do
  529. # NOTE: missing "ticket_id" (because the value is generated as part of the #process method)
  530. {
  531. 'from' => "@#{payload[:tweet_create_events].first[:user][:screen_name]}",
  532. 'to' => "@#{payload[:tweet_create_events].first[:entities][:user_mentions].first[:screen_name]}",
  533. 'body' => payload[:tweet_create_events].first[:text],
  534. 'message_id' => payload[:tweet_create_events].first[:id_str],
  535. 'in_reply_to' => payload[:tweet_create_events].first[:in_reply_to_status_id],
  536. 'type_id' => Ticket::Article::Type.find_by(name: 'twitter status').id,
  537. 'sender_id' => Ticket::Article::Sender.find_by(name: 'Customer').id,
  538. 'internal' => false,
  539. 'preferences' => { 'twitter' => twitter_prefs, 'links' => link_array }
  540. }
  541. end
  542. let(:twitter_prefs) do
  543. {
  544. 'mention_ids' => payload[:tweet_create_events].first[:entities][:user_mentions].pluck(:id),
  545. 'geo' => payload[:tweet_create_events].first[:geo].to_h,
  546. 'retweeted' => payload[:tweet_create_events].first[:retweeted],
  547. 'possibly_sensitive' => payload[:tweet_create_events].first[:possibly_sensitive],
  548. 'in_reply_to_user_id' => payload[:tweet_create_events].first[:in_reply_to_user_id],
  549. 'place' => payload[:tweet_create_events].first[:place].to_h,
  550. 'retweet_count' => payload[:tweet_create_events].first[:retweet_count],
  551. 'source' => payload[:tweet_create_events].first[:source],
  552. 'favorited' => payload[:tweet_create_events].first[:favorited],
  553. 'truncated' => payload[:tweet_create_events].first[:truncated],
  554. }
  555. end
  556. let(:link_array) do
  557. [
  558. {
  559. 'url' => "https://twitter.com/_/status/#{payload[:tweet_create_events].first[:id]}",
  560. 'target' => '_blank',
  561. 'name' => 'on Twitter',
  562. },
  563. ]
  564. end
  565. it 'creates a new article' do
  566. expect { channel.process(payload) }
  567. .to change(Ticket::Article, :count).by(1)
  568. .and change { Ticket::Article.exists?(article_attributes) }.to(true)
  569. end
  570. end
  571. end
  572. end
  573. describe '#send', :use_vcr do
  574. shared_examples 'for #send' do
  575. # Channel#deliver takes a hash in the following format
  576. # (see CommunicateTwitterJob#perform)
  577. #
  578. # Why not just accept the whole article?
  579. # Presumably so all channels have a consistent interface...
  580. # but it might be a good idea to let it accept both one day
  581. # (the "robustness principle")
  582. let(:delivery_payload) do
  583. {
  584. type: outgoing_tweet.type.name,
  585. to: outgoing_tweet.to,
  586. body: outgoing_tweet.body,
  587. in_reply_to: outgoing_tweet.in_reply_to
  588. }
  589. end
  590. describe 'Import Mode behavior' do
  591. before { Setting.set('import_mode', true) }
  592. it 'is a no-op' do
  593. expect(Twitter::REST::Client).not_to receive(:new)
  594. channel.deliver(delivery_payload)
  595. end
  596. end
  597. describe 'Twitter API activity' do
  598. it 'returns the created tweet/DM' do
  599. expect(channel.deliver(delivery_payload)).to be_a(return_value)
  600. end
  601. end
  602. end
  603. context 'for tweets' do
  604. let!(:outgoing_tweet) { create(:twitter_article) }
  605. let(:return_value) { Twitter::Tweet }
  606. include_examples 'for #send'
  607. context 'in a thread' do
  608. let!(:outgoing_tweet) { create(:twitter_article, :reply) }
  609. it 'creates a tweet via the API' do
  610. expect { channel.deliver(delivery_payload) }.to not_raise_error
  611. end
  612. end
  613. context 'containing an asterisk (workaround for sferik/twitter #677)' do
  614. let!(:outgoing_tweet) { create(:twitter_article, body: 'foo * bar') }
  615. it 'converts it to a full-width asterisk (U+FF0A)' do
  616. expect { channel.deliver(delivery_payload) }.to not_raise_error
  617. end
  618. end
  619. end
  620. context 'for DMs' do
  621. let(:recipient) { create(:twitter_authorization, uid: ENV.fetch('TWITTER_DM_REAL_RECIPIENT', '1577555254278766596')) }
  622. let!(:outgoing_tweet) { create(:twitter_dm_article, :pending_delivery, recipient: recipient) }
  623. let(:return_value) { Twitter::DirectMessage }
  624. include_examples 'for #send'
  625. end
  626. end
  627. describe '#fetch', use_vcr: :time_sensitive do
  628. context 'when ApplicationHandleInfo context' do
  629. it 'gets switched to "twitter"' do
  630. allow(ApplicationHandleInfo).to receive('context=')
  631. channel.fetch
  632. expect(ApplicationHandleInfo).to have_received('context=').with('twitter').at_least(1)
  633. end
  634. it 'reverts back to default' do
  635. allow(ApplicationHandleInfo).to receive('context=')
  636. channel.fetch
  637. expect(ApplicationHandleInfo.context).not_to eq 'twitter'
  638. end
  639. end
  640. describe 'rate limiting' do
  641. before do
  642. allow(Rails.env).to receive(:test?).and_return(false)
  643. channel.fetch
  644. end
  645. context 'within 20 minutes of last run' do
  646. before { travel(19.minutes) }
  647. it 'aborts' do
  648. expect { channel.fetch }
  649. .not_to change { channel.reload.preferences[:last_fetch] }
  650. end
  651. end
  652. context '20+ minutes since last run' do
  653. before { travel(21.minutes) }
  654. it 'runs again' do
  655. expect { channel.fetch }
  656. .to change { channel.reload.preferences[:last_fetch] }
  657. end
  658. end
  659. end
  660. end
  661. end