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