-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathfcm_spec.rb
More file actions
449 lines (376 loc) · 12.6 KB
/
Copy pathfcm_spec.rb
File metadata and controls
449 lines (376 loc) · 12.6 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
require "spec_helper"
describe FCM do
let(:project_name) { 'test-project' }
let(:json_key_path) { 'path/to/json/key.json' }
let(:client) { FCM.new(json_key_path) }
let(:mock_token) { "access_token" }
let(:mock_headers) do
{
"Content-Type" => "application/json",
"Authorization" => "Bearer #{mock_token}",
}
end
before do
allow(client).to receive(:json_key)
# Mock the Google::Auth::ServiceAccountCredentials
allow(Google::Auth::ServiceAccountCredentials).to receive(:make_creds).
and_return(double(fetch_access_token!: { 'access_token' => mock_token }))
end
it "should initialize" do
expect { client }.not_to raise_error
end
describe "credentials path" do
it "can be a path to a file" do
fcm = FCM.new("README.md")
expect(fcm.__send__(:json_key).class).to eq(File)
end
it "can be an IO object" do
fcm = FCM.new(StringIO.new("hey"))
expect(fcm.__send__(:json_key).class).to eq(StringIO)
end
end
describe 'faraday configurer' do
let(:configurer) { ->(faraday) { } }
let(:client) { FCM.new(json_key_path, '', {}, configurer) }
it 'should be called when initializing faraday client' do
expect(configurer).to receive(:call).with(Faraday::Connection)
client.__send__(:for_uri, '') {}
end
end
describe "#send_v1 or #send_notification_v1" do
let(:client) { FCM.new(json_key_path, project_name) }
let(:uri) { "#{FCM::BASE_URI_V1}#{project_name}/messages:send" }
let(:status_code) { 200 }
let(:stub_fcm_send_v1_request) do
stub_request(:post, uri).with(
body: { 'message' => send_v1_params }.to_json,
headers: mock_headers
).to_return(
# ref: https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream
body: "{}",
headers: {},
status: status_code,
)
end
before do
stub_fcm_send_v1_request
end
shared_examples 'succesfuly send notification' do
it 'should send notification of HTTP V1 using POST to FCM server' do
client.send_v1(send_v1_params).should eq(
response: 'success', body: '{}', headers: {}, status_code: 200
)
stub_fcm_send_v1_request.should have_been_made.times(1)
end
end
describe 'send to token' do
let(:token) { '4sdsx' }
let(:send_v1_params) do
{
'token' => token,
'notification' => {
'title' => 'Breaking News',
'body' => 'New news story available.'
},
'data' => {
'story_id' => 'story_12345'
},
'android' => {
'notification' => {
'click_action' => 'TOP_STORY_ACTIVITY',
'body' => 'Check out the Top Story'
}
},
'apns' => {
'payload' => {
'aps' => {
'category' => 'NEW_MESSAGE_CATEGORY'
}
}
}
}
end
include_examples 'succesfuly send notification'
it 'includes all the response' do
response = client.send_v1(send_v1_params)
expect(response[:status_code]).to eq(status_code)
expect(response[:response]).to eq('success')
expect(response[:body]).to eq('{}')
expect(response[:headers]).to eq({})
expect(response[:canonical_ids]).to be_nil
expect(response[:not_registered_ids]).to be_nil
end
end
describe 'send to multiple tokens' do
let(:tokens) { ['4sdsx', '4sdsy'] }
let(:send_v1_params) do
{
'token' => tokens,
'notification' => {
'title' => 'Breaking News',
'body' => 'New news story available.'
}
}
end
include_examples 'succesfuly send notification'
end
describe 'send to topic' do
let(:topic) { 'news' }
let(:send_v1_params) do
{
'topic' => topic,
'notification' => {
'title' => 'Breaking News',
'body' => 'New news story available.'
}
}
end
include_examples 'succesfuly send notification'
context 'when topic is invalid' do
let(:topic) { '/topics/news$' }
it 'should raise error' do
stub_fcm_send_v1_request.should_not have_been_requested
end
end
end
describe 'send to condition' do
let(:condition) { "'foo' in topics" }
let(:send_v1_params) do
{
'condition' => condition,
'notification' => {
'title' => 'Breaking News',
'body' => 'New news story available.'
},
}
end
include_examples 'succesfuly send notification'
end
describe 'send to notification_key' do
let(:notification_key) { 'notification_key' }
let(:send_v1_params) do
{
'notification_key' => notification_key,
'notification' => {
'title' => 'Breaking News',
'body' => 'New news story available.'
}
}
end
include_examples 'succesfuly send notification'
end
context 'when project_name is empty' do
let(:project_name) { '' }
let(:send_v1_params) do
{
'token' => '4sdsx',
'notification' => {
'title' => 'Breaking News',
'body' => 'New news story available.'
}
}
end
it 'should not send notification' do
client.send_v1(send_v1_params)
stub_fcm_send_v1_request.should_not have_been_requested
end
end
describe 'error handling' do
let(:send_v1_params) do
{
'token' => '4sdsx',
'notification' => {
'title' => 'Breaking News',
'body' => 'New news story available.'
}
}
end
context 'when status_code is 400' do
let(:status_code) { 400 }
it 'should raise error' do
response = client.send_v1(send_v1_params)
expect(response[:status_code]).to eq(status_code)
expect(response[:response]).to include('Only applies for JSON requests')
end
end
context 'when status_code is 401' do
let(:status_code) { 401 }
it 'should raise error' do
response = client.send_v1(send_v1_params)
expect(response[:status_code]).to eq(status_code)
expect(response[:response]).to include('There was an error authenticating')
end
end
context 'when status_code is 500' do
let(:status_code) { 500 }
it 'should raise error' do
response = client.send_v1(send_v1_params)
expect(response[:status_code]).to eq(status_code)
expect(response[:response]).to include('There was an internal error')
end
end
context 'when status_code is 503' do
let(:status_code) { 503 }
it 'should raise error' do
response = client.send_v1(send_v1_params)
expect(response[:status_code]).to eq(status_code)
expect(response[:response]).to include('Server is temporarily unavailable')
end
end
end
end
describe '#send_to_topic' do
let(:client) { FCM.new(json_key_path, project_name) }
let(:uri) { "#{FCM::BASE_URI_V1}#{project_name}/messages:send" }
let(:topic) { 'news' }
let(:params) do
{
'topic' => topic
}.merge(options)
end
let(:options) do
{
'data' => {
'story_id' => 'story_12345'
}
}
end
let(:stub_fcm_send_to_topic_request) do
stub_request(:post, uri).with(
body: { 'message' => params }.to_json,
headers: mock_headers
).to_return(
body: "{}",
headers: {},
status: 200,
)
end
before do
stub_fcm_send_to_topic_request
end
it 'should send notification to topic using POST to FCM server' do
client.send_to_topic(topic, options).should eq(
response: 'success', body: '{}', headers: {}, status_code: 200
)
stub_fcm_send_to_topic_request.should have_been_made.times(1)
end
context 'when topic is invalid' do
let(:topic) { '/topics/news$' }
it 'should raise error' do
client.send_to_topic(topic, options)
stub_fcm_send_to_topic_request.should_not have_been_requested
end
end
end
describe "#send_to_topic_condition" do
let(:client) { FCM.new(json_key_path, project_name) }
let(:uri) { "#{FCM::BASE_URI_V1}#{project_name}/messages:send" }
let(:topic_condition) { "'foo' in topics" }
let(:params) do
{
'condition' => topic_condition
}.merge(options)
end
let(:options) do
{
'data' => {
'story_id' => 'story_12345'
}
}
end
let(:stub_fcm_send_to_topic_condition_request) do
stub_request(:post, uri).with(
body: { 'message' => params }.to_json,
headers: mock_headers
).to_return(
body: "{}",
headers: {},
status: 200,
)
end
before do
stub_fcm_send_to_topic_condition_request
end
it 'should send notification to topic_condition using POST to FCM server' do
client.send_to_topic_condition(topic_condition, options).should eq(
response: 'success', body: '{}', headers: {}, status_code: 200
)
stub_fcm_send_to_topic_condition_request.should have_been_made.times(1)
end
context 'when topic_condition is invalid' do
let(:topic_condition) { "'foo' in topics$" }
it 'should raise error' do
client.send_to_topic_condition(topic_condition, options)
stub_fcm_send_to_topic_condition_request.should_not have_been_requested
end
end
end
describe "#get_instance_id_info" do
subject(:get_info) { client.get_instance_id_info(registration_token, options) }
let(:options) { nil }
let(:base_uri) { "#{FCM::INSTANCE_ID_API}/iid/info" }
let(:uri) { "#{base_uri}/#{registration_token}" }
let(:registration_token) { "42" }
context 'without options' do
it 'calls info endpoint' do
endpoint = stub_request(:get, uri).with(headers: mock_headers)
get_info
expect(endpoint).to have_been_requested
end
end
context 'with detail option' do
let(:uri) { "#{base_uri}/#{registration_token}?details=true" }
let(:options) { { details: true } }
it 'calls info endpoint' do
endpoint = stub_request(:get, uri).with(headers: mock_headers)
get_info
expect(endpoint).to have_been_requested
end
end
end
describe "topic subscriptions" do
let(:topic) { 'news' }
let(:registration_token) { "42" }
let(:registration_token_2) { "43" }
let(:registration_tokens) { [registration_token, registration_token_2] }
describe "#topic_subscription" do
subject(:subscribe) { client.topic_subscription(topic, registration_token) }
let(:uri) { "#{FCM::INSTANCE_ID_API}/iid/v1/#{registration_token}/rel/topics/#{topic}" }
it 'subscribes to a topic' do
endpoint = stub_request(:post, uri).with(headers: mock_headers)
subscribe
expect(endpoint).to have_been_requested
end
end
describe "#topic_unsubscription" do
subject(:unsubscribe) { client.topic_unsubscription(topic, registration_token) }
let(:uri) { "#{FCM::INSTANCE_ID_API}/iid/v1:batchRemove" }
let(:params) { { to: "/topics/#{topic}", registration_tokens: [registration_token] } }
it 'unsubscribes from a topic' do
endpoint = stub_request(:post, uri).with(body: params.to_json, headers: mock_headers)
unsubscribe
expect(endpoint).to have_been_requested
end
end
describe "#batch_topic_subscription" do
subject(:batch_subscribe) { client.batch_topic_subscription(topic, registration_tokens) }
let(:uri) { "#{FCM::INSTANCE_ID_API}/iid/v1:batchAdd" }
let(:params) { { to: "/topics/#{topic}", registration_tokens: registration_tokens } }
it 'subscribes to a topic' do
endpoint = stub_request(:post, uri).with(body: params.to_json, headers: mock_headers)
batch_subscribe
expect(endpoint).to have_been_requested
end
end
describe "#batch_topic_unsubscription" do
subject(:batch_unsubscribe) { client.batch_topic_unsubscription(topic, registration_tokens) }
let(:uri) { "#{FCM::INSTANCE_ID_API}/iid/v1:batchRemove" }
let(:params) { { to: "/topics/#{topic}", registration_tokens: registration_tokens } }
it 'unsubscribes from a topic' do
endpoint = stub_request(:post, uri).with(body: params.to_json, headers: mock_headers)
batch_unsubscribe
expect(endpoint).to have_been_requested
end
end
end
end