-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpush_admin_spec.rb
941 lines (789 loc) · 33.9 KB
/
push_admin_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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
# encoding: utf-8
require 'spec_helper'
describe Ably::Rest::Push::Admin do
include Ably::Modules::Conversions
vary_by_protocol do
let(:default_options) { { key: api_key, environment: environment, protocol: protocol} }
let(:client_options) { default_options }
let(:client) do
Ably::Rest::Client.new(client_options)
end
let(:basic_notification_payload) do
{
notification: {
title: 'Test message',
body: 'Test message body'
}
}
end
let(:basic_recipient) do
{
transport_type: 'apns',
deviceToken: 'foo.bar'
}
end
describe '#publish' do
subject { client.push.admin }
context 'without publish permissions' do
let(:capability) { { :foo => ['subscribe'] } }
before do
client.auth.authorize(capability: capability)
end
it 'raises a permissions issue exception' do
expect { subject.publish(basic_recipient, basic_notification_payload) }.to raise_error Ably::Exceptions::UnauthorizedRequest
end
end
context 'invalid arguments (#RHS1a)' do
it 'raises an exception with a nil recipient' do
expect { subject.publish(nil, {}) }.to raise_error ArgumentError, /Expecting a Hash/
end
it 'raises an exception with a empty recipient' do
expect { subject.publish({}, {}) }.to raise_error ArgumentError, /empty/
end
it 'raises an exception with a nil recipient' do
expect { subject.publish(basic_recipient, nil) }.to raise_error ArgumentError, /Expecting a Hash/
end
it 'raises an exception with a empty recipient' do
expect { subject.publish(basic_recipient, {}) }.to raise_error ArgumentError, /empty/
end
end
context 'invalid recipient (#RSH1a)' do
it 'raises an error after receiving a 40x realtime response' do
expect { subject.publish({ invalid_recipient_details: 'foo.bar' }, basic_notification_payload) }.to raise_error Ably::Exceptions::InvalidRequest
end
end
context 'invalid push data (#RSH1a)' do
it 'raises an error after receiving a 40x realtime response' do
expect { subject.publish(basic_recipient, { invalid_property_only: true }) }.to raise_error Ably::Exceptions::InvalidRequest
end
end
context 'recipient variable case', webmock: true do
let(:recipient_payload) do
{
camel_case: {
second_level_camel_case: 'val'
}
}
end
let(:content_type) do
if protocol == :msgpack
'application/x-msgpack'
else
'application/json'
end
end
let!(:publish_stub) do
stub_request(:post, "#{client.endpoint}/push/publish").
with do |request|
expect(deserialize_body(request.body, protocol)['recipient']['camelCase']['secondLevelCamelCase']).to eql('val')
expect(deserialize_body(request.body, protocol)['recipient']).to_not have_key('camel_case')
true
end.to_return(
:status => 201,
:body => serialize_body({}, protocol),
:headers => { 'Content-Type' => content_type }
)
end
it 'is converted to snakeCase' do
subject.publish(recipient_payload, basic_notification_payload)
expect(publish_stub).to have_been_requested
end
end
it 'accepts valid push data and recipient (#RSH1a)' do
subject.publish(basic_recipient, basic_notification_payload)
end
context 'using test environment channel recipient (#RSH1a)' do
let(:channel) { random_str }
let(:recipient) do
{
'transportType' => 'ablyChannel',
'channel' => channel,
'ablyKey' => api_key,
'ablyUrl' => client.endpoint.to_s
}
end
let(:notification_payload) do
{
notification: {
title: random_str,
},
data: {
foo: random_str
}
}
end
it 'triggers a push notification' do
subject.publish(recipient, notification_payload)
sleep 5
notification_published_on_channel = client.channels.get(channel).history.items.first
expect(notification_published_on_channel.name).to eql('__ably_push__')
expect(JSON.parse(notification_published_on_channel.data)['data']).to eql(JSON.parse(notification_payload[:data].to_json))
end
end
end
describe '#device_registrations (#RSH1b)' do
subject { client.push.admin.device_registrations }
context 'without permissions' do
let(:capability) { { :foo => ['subscribe'] } }
before do
client.auth.authorize(capability: capability)
end
it 'raises a permissions not authorized exception' do
expect { subject.get('does-not-exist') }.to raise_error Ably::Exceptions::UnauthorizedRequest
expect { subject.list }.to raise_error Ably::Exceptions::UnauthorizedRequest
expect { subject.remove('does-not-exist') }.to raise_error Ably::Exceptions::UnauthorizedRequest
expect { subject.remove_where(device_id: 'does-not-exist') }.to raise_error Ably::Exceptions::UnauthorizedRequest
end
end
describe '#list (#RSH1b2)' do
let(:client_id) { random_str }
let(:fixture_count) { 6 }
before(:all) do
# As push tests often use the global scope (devices),
# we need to ensure tests cannot conflict
reload_test_app
end
before do
fixture_count.times.map do |index|
Thread.new do
subject.save({
id: "device-#{client_id}-#{index}",
platform: 'ios',
form_factor: 'phone',
client_id: client_id,
push: {
recipient: {
transport_type: 'fcm',
registration_token: 'secret_token',
}
}
})
end
end.each(&:join) # Wait for all threads to complete
end
after do
subject.remove_where client_id: client_id, full_wait: true
end
it 'returns a PaginatedResult object containing DeviceDetails objects' do
page = subject.list
expect(page).to be_a(Ably::Models::PaginatedResult)
expect(page.items.first).to be_a(Ably::Models::DeviceDetails)
end
it 'returns an empty PaginatedResult if not params match' do
page = subject.list(client_id: 'does-not-exist')
expect(page).to be_a(Ably::Models::PaginatedResult)
expect(page.items).to be_empty
end
it 'supports paging' do
page = subject.list(limit: 3, client_id: client_id)
expect(page).to be_a(Ably::Models::PaginatedResult)
expect(page.items.count).to eql(3)
page = page.next
expect(page.items.count).to eql(3)
page = page.next
expect(page.items.count).to eql(0)
expect(page).to be_last
end
it 'provides filtering' do
page = subject.list(client_id: client_id)
expect(page.items.length).to eql(fixture_count)
page = subject.list(device_id: "device-#{client_id}-0")
expect(page.items.length).to eql(1)
page = subject.list(client_id: random_str)
expect(page.items.length).to eql(0)
end
end
describe '#get (#RSH1b1)' do
let(:fixture_count) { 2 }
let(:client_id) { random_str }
before(:all) do
# As push tests often use the global scope (devices),
# we need to ensure tests cannot conflict
reload_test_app
end
before do
fixture_count.times.map do |index|
Thread.new do
subject.save({
id: "device-#{client_id}-#{index}",
platform: 'ios',
form_factor: 'phone',
client_id: client_id,
push: {
recipient: {
transport_type: 'fcm',
registration_token: 'secret_token',
}
}
})
end
end.each(&:join) # Wait for all threads to complete
end
after do
subject.remove_where client_id: client_id, full_wait: true
end
it 'returns a DeviceDetails object if a device ID string is provided' do
device = subject.get("device-#{client_id}-0")
expect(device).to be_a(Ably::Models::DeviceDetails)
expect(device.platform).to eql('ios')
expect(device.client_id).to eql(client_id)
expect(device.push.recipient.fetch(:transport_type)).to eql('fcm')
end
it 'returns a DeviceDetails object if a DeviceDetails object is provided' do
device = subject.get(Ably::Models::DeviceDetails.new(id: "device-#{client_id}-1"))
expect(device).to be_a(Ably::Models::DeviceDetails)
expect(device.platform).to eql('ios')
expect(device.client_id).to eql(client_id)
expect(device.push.recipient.fetch(:transport_type)).to eql('fcm')
end
it 'raises a ResourceMissing exception if device ID does not exist' do
expect { subject.get("device-does-not-exist") }.to raise_error(Ably::Exceptions::ResourceMissing)
end
end
describe '#save (#RSH1b3)' do
let(:device_id) { random_str }
let(:client_id) { random_str }
let(:transport_token) { random_str }
let(:device_details) do
{
id: device_id,
platform: 'android',
form_factor: 'phone',
client_id: client_id,
metadata: {
foo: 'bar',
deep: {
val: true
}
},
push: {
recipient: {
transport_type: 'apns',
device_token: transport_token,
foo_bar: 'string',
},
error_reason: {
message: "this will be ignored"
},
}
}
end
before(:all) do
# As push tests often use the global scope (devices),
# we need to ensure tests cannot conflict
reload_test_app
end
after do
subject.remove_where client_id: client_id, full_wait: true
end
it 'saves the new DeviceDetails Hash object' do
subject.save(device_details)
device_retrieved = subject.get(device_details.fetch(:id))
expect(device_retrieved).to be_a(Ably::Models::DeviceDetails)
expect(device_retrieved.id).to eql(device_id)
expect(device_retrieved.platform).to eql('android')
expect(device_retrieved.form_factor).to eql('phone')
expect(device_retrieved.client_id).to eql(client_id)
expect(device_retrieved.metadata.keys.length).to eql(2)
expect(device_retrieved.metadata[:foo]).to eql('bar')
expect(device_retrieved.metadata['deep']['val']).to eql(true)
end
it 'saves the associated DevicePushDetails' do
subject.save(device_details)
device_retrieved = subject.list(device_id: device_details.fetch(:id)).items.first
expect(device_retrieved.push).to be_a(Ably::Models::DevicePushDetails)
expect(device_retrieved.push.recipient.fetch(:transport_type)).to eql('apns')
expect(device_retrieved.push.recipient['deviceToken']).to eql(transport_token)
expect(device_retrieved.push.recipient['foo_bar']).to eql('string')
end
context 'with FCM target' do
let(:device_token) { random_str }
it 'saves the associated DevicePushDetails' do
subject.save(device_details.merge(
push: {
recipient: {
transport_type: 'fcm',
registrationToken: device_token
}
}
))
device_retrieved = subject.get(device_details.fetch(:id))
expect(device_retrieved.push.recipient.fetch('transportType')).to eql('fcm')
expect(device_retrieved.push.recipient[:registration_token]).to eql(device_token)
end
end
context 'with web target' do
let(:target_url) { 'http://foo.com/bar' }
let(:p256dh) { random_str }
let(:auth) { random_str }
it 'saves the associated DevicePushDetails' do
subject.save(device_details.merge(
push: {
recipient: {
transport_type: 'web',
targetUrl: target_url,
encryptionKey: {
p256dh: p256dh,
auth: auth
}
}
}
))
device_retrieved = subject.get(device_details.fetch(:id))
expect(device_retrieved.push.recipient[:transport_type]).to eql('web')
expect(device_retrieved.push.recipient['targetUrl']).to eql(target_url)
expect(device_retrieved.push.recipient['encryptionKey']['p256dh']).to eql(p256dh)
expect(device_retrieved.push.recipient['encryptionKey']['auth']).to eql(auth)
end
end
it 'does not allow some fields to be configured' do
subject.save(device_details)
device_retrieved = subject.get(device_details.fetch(:id))
expect(device_retrieved.push.state).to eql('ACTIVE')
expect(device_retrieved.device_secret).to be_nil
# Errors are exclusively configure by Ably
expect(device_retrieved.push.error_reason).to be_nil
end
it 'allows device_secret to be configured' do
device_secret = random_str
subject.save(device_details.merge(device_secret: device_secret))
device_retrieved = subject.get(device_details.fetch(:id))
expect(device_retrieved.device_secret).to eql(device_secret)
end
it 'saves the new DeviceDetails object' do
subject.save(DeviceDetails(device_details))
device_retrieved = subject.get(device_details.fetch(:id))
expect(device_retrieved.id).to eql(device_id)
expect(device_retrieved.metadata[:foo]).to eql('bar')
expect(device_retrieved.push.recipient[:transport_type]).to eql('apns')
end
it 'allows arbitrary number of subsequent saves' do
3.times do
subject.save(DeviceDetails(device_details))
end
device_retrieved = subject.get(device_details.fetch(:id))
expect(device_retrieved.metadata[:foo]).to eql('bar')
subject.save(DeviceDetails(device_details.merge(metadata: { foo: 'changed'})))
device_retrieved = subject.get(device_details.fetch(:id))
expect(device_retrieved.metadata[:foo]).to eql('changed')
end
it 'fails if data is invalid' do
expect { subject.save(id: random_str, foo: 'bar') }.to raise_error Ably::Exceptions::InvalidRequest
end
end
describe '#remove_where (#RSH1b5)' do
let(:device_id) { random_str }
let(:client_id) { random_str }
before(:all) do
# As push tests often use the global scope (devices),
# we need to ensure tests cannot conflict
reload_test_app
end
before do
[
Thread.new do
subject.save({
id: "device-#{client_id}-0",
platform: 'ios',
form_factor: 'phone',
client_id: client_id,
push: {
recipient: {
transport_type: 'fcm',
registrationToken: 'secret_token',
}
}
})
end,
Thread.new do
subject.save({
id: "device-#{client_id}-1",
platform: 'ios',
form_factor: 'phone',
client_id: client_id,
push: {
recipient: {
transport_type: 'fcm',
registration_token: 'secret_token',
}
}
})
end
].each(&:join) # Wait for all threads to complete
end
after do
subject.remove_where client_id: client_id, full_wait: true
end
it 'removes all matching device registrations by client_id' do
subject.remove_where(client_id: client_id, full_wait: true) # undocumented full_wait to compelte synchronously
expect(subject.list.items.count).to eql(0)
end
it 'removes device by device_id' do
subject.remove_where(device_id: "device-#{client_id}-1", full_wait: true) # undocumented full_wait to compelte synchronously
expect(subject.list.items.count).to eql(1)
end
it 'succeeds even if there is no match' do
subject.remove_where(device_id: 'does-not-exist', full_wait: true) # undocumented full_wait to compelte synchronously
expect(subject.list.items.count).to eql(2)
end
end
describe '#remove (#RSH1b4)' do
let(:device_id) { random_str }
let(:client_id) { random_str }
before(:all) do
# As push tests often use the global scope (devices),
# we need to ensure tests cannot conflict
reload_test_app
end
before do
[
Thread.new do
subject.save({
id: "device-#{client_id}-0",
platform: 'ios',
form_factor: 'phone',
client_id: client_id,
push: {
recipient: {
transport_type: 'fcm',
registration_token: 'secret_token',
}
}
})
end,
Thread.new do
subject.save({
id: "device-#{client_id}-1",
platform: 'ios',
form_factor: 'phone',
client_id: client_id,
push: {
recipient: {
transport_type: 'fcm',
registration_token: 'secret_token',
}
}
})
end
].each(&:join) # Wait for all threads to complete
end
after do
subject.remove_where client_id: client_id, full_wait: true
end
it 'removes the provided device id string' do
subject.remove("device-#{client_id}-0")
expect(subject.list.items.count).to eql(1)
end
it 'removes the provided DeviceDetails' do
subject.remove(DeviceDetails(id: "device-#{client_id}-1"))
expect(subject.list.items.count).to eql(1)
end
it 'succeeds if the item does not exist' do
subject.remove('does-not-exist')
expect(subject.list.items.count).to eql(2)
end
end
end
describe '#channel_subscriptions (#RSH1c)' do
let(:client_id) { random_str }
let(:device_id) { random_str }
let(:device_id_2) { random_str }
let(:default_device_attr) {
{
platform: 'ios',
form_factor: 'phone',
client_id: client_id,
push: {
recipient: {
transport_type: 'fcm',
registration_token: 'secret_token',
}
}
}
}
let(:device_registrations) {
client.push.admin.device_registrations
}
subject {
client.push.admin.channel_subscriptions
}
# Set up 2 devices with the same client_id
# and two device with the unique device_id and no client_id
before do
[
lambda { device_registrations.save(default_device_attr.merge(id: device_id, client_id: nil)) },
lambda { device_registrations.save(default_device_attr.merge(id: device_id_2, client_id: nil)) },
lambda { device_registrations.save(default_device_attr.merge(client_id: client_id, id: random_str)) },
lambda { device_registrations.save(default_device_attr.merge(client_id: client_id, id: random_str)) }
].map do |proc|
Thread.new { proc.call }
end.each(&:join) # Wait for all threads to complete
end
after do
device_registrations.remove_where client_id: client_id
device_registrations.remove_where device_id: device_id
end
describe '#list (#RSH1c1)' do
let(:fixture_count) { 6 }
before(:all) do
# As push tests often use the global scope (devices),
# we need to ensure tests cannot conflict
reload_test_app
end
before do
fixture_count.times.map do |index|
Thread.new { subject.save(channel: "pushenabled:#{random_str}", client_id: client_id) }
end + fixture_count.times.map do |index|
Thread.new { subject.save(channel: "pushenabled:#{random_str}", device_id: device_id) }
end.each(&:join) # Wait for all threads to complete
end
it 'returns a PaginatedResult object containing DeviceDetails objects' do
page = subject.list(client_id: client_id)
expect(page).to be_a(Ably::Models::PaginatedResult)
expect(page.items.first).to be_a(Ably::Models::PushChannelSubscription)
end
it 'returns an empty PaginatedResult if params do not match' do
page = subject.list(client_id: 'does-not-exist')
expect(page).to be_a(Ably::Models::PaginatedResult)
expect(page.items).to be_empty
end
it 'supports paging' do
page = subject.list(limit: 3, device_id: device_id)
expect(page).to be_a(Ably::Models::PaginatedResult)
expect(page.items.count).to eql(3)
page = page.next
expect(page.items.count).to eql(3)
page = page.next
expect(page.items.count).to eql(0)
expect(page).to be_last
end
it 'provides filtering' do
page = subject.list(device_id: device_id)
expect(page.items.length).to eql(fixture_count)
page = subject.list(client_id: client_id)
expect(page.items.length).to eql(fixture_count)
random_channel = "pushenabled:#{random_str}"
subject.save(channel: random_channel, client_id: client_id)
page = subject.list(channel: random_channel)
expect(page.items.length).to eql(1)
page = subject.list(channel: random_channel, client_id: client_id)
expect(page.items.length).to eql(1)
page = subject.list(channel: random_channel, device_id: random_str)
expect(page.items.length).to eql(0)
page = subject.list(device_id: random_str)
expect(page.items.length).to eql(0)
page = subject.list(client_id: random_str)
expect(page.items.length).to eql(0)
page = subject.list(channel: random_str)
expect(page.items.length).to eql(0)
end
it 'raises an exception if none of the required filters are provided' do
expect { subject.list({ limit: 100 }) }.to raise_error(ArgumentError)
end
end
describe '#list_channels (#RSH1c2)' do
let(:fixture_count) { 6 }
before(:all) do
# As push tests often use the global scope (devices),
# we need to ensure tests cannot conflict
reload_test_app
end
before do
# Create 6 channel subscriptions to the client ID for this test
fixture_count.times.map do |index|
Thread.new do
subject.save(channel: "pushenabled:#{index}:#{random_str}", client_id: client_id)
end
end.each(&:join) # Wait for all threads to complete
end
after do
subject.remove_where client_id: client_id, full_wait: true # undocumented arg to do deletes synchronously
end
it 'returns a PaginatedResult object containing String objects' do
page = subject.list_channels
expect(page).to be_a(Ably::Models::PaginatedResult)
expect(page.items.first).to be_a(String)
expect(page.items.length).to eql(fixture_count)
end
it 'supports paging' do
subject.list_channels
page = subject.list_channels(limit: 3)
expect(page).to be_a(Ably::Models::PaginatedResult)
expect(page.items.count).to eql(3)
page = page.next
expect(page.items.count).to eql(3)
page = page.next
expect(page.items.count).to eql(0)
expect(page).to be_last
end
# This test is not necessary for client libraries, but was useful when building the Ruby
# lib to ensure the realtime implementation did not suffer from timing issues
it 'returns an accurate number of channels after devices are deleted' do
expect(subject.list_channels.items.length).to eql(fixture_count)
subject.save(channel: "pushenabled:#{random_str}", device_id: device_id)
subject.save(channel: "pushenabled:#{random_str}", device_id: device_id)
expect(subject.list_channels.items.length).to eql(fixture_count + 2)
expect(device_registrations.list(device_id: device_id).items.count).to eql(1)
device_registrations.remove_where device_id: device_id, full_wait: true # undocumented arg to do deletes synchronously
expect(device_registrations.list(device_id: device_id).items.count).to eql(0)
expect(subject.list_channels.items.length).to eql(fixture_count)
subject.remove_where client_id: client_id, full_wait: true # undocumented arg to do deletes synchronously
expect(subject.list_channels.items.length).to eql(0)
end
end
describe '#save (#RSH1c3)' do
let(:channel) { "pushenabled:#{random_str}" }
let(:client_id) { random_str }
let(:device_id) { random_str }
before(:all) do
# As push tests often use the global scope (devices),
# we need to ensure tests cannot conflict
reload_test_app
end
it 'saves the new client_id PushChannelSubscription Hash object' do
subject.save(channel: channel, client_id: client_id)
channel_sub = subject.list(client_id: client_id).items.first
expect(channel_sub).to be_a(Ably::Models::PushChannelSubscription)
expect(channel_sub.channel).to eql(channel)
expect(channel_sub.client_id).to eql(client_id)
expect(channel_sub.device_id).to be_nil
end
it 'saves the new device_id PushChannelSubscription Hash object' do
subject.save(channel: channel, device_id: device_id)
channel_sub = subject.list(device_id: device_id).items.first
expect(channel_sub).to be_a(Ably::Models::PushChannelSubscription)
expect(channel_sub.channel).to eql(channel)
expect(channel_sub.device_id).to eql(device_id)
expect(channel_sub.client_id).to be_nil
end
it 'saves the client_id PushChannelSubscription object' do
subject.save(PushChannelSubscription(channel: channel, client_id: client_id))
channel_sub = subject.list(client_id: client_id).items.first
expect(channel_sub).to be_a(Ably::Models::PushChannelSubscription)
expect(channel_sub.channel).to eql(channel)
expect(channel_sub.client_id).to eql(client_id)
expect(channel_sub.device_id).to be_nil
end
it 'saves the device_id PushChannelSubscription object' do
subject.save(PushChannelSubscription(channel: channel, device_id: device_id))
channel_sub = subject.list(device_id: device_id).items.first
expect(channel_sub).to be_a(Ably::Models::PushChannelSubscription)
expect(channel_sub.channel).to eql(channel)
expect(channel_sub.device_id).to eql(device_id)
expect(channel_sub.client_id).to be_nil
end
it 'allows arbitrary number of subsequent saves' do
10.times do
subject.save(PushChannelSubscription(channel: channel, device_id: device_id))
end
channel_subs = subject.list(device_id: device_id).items
expect(channel_subs.length).to eql(1)
expect(channel_subs.first).to be_a(Ably::Models::PushChannelSubscription)
expect(channel_subs.first.channel).to eql(channel)
expect(channel_subs.first.device_id).to eql(device_id)
expect(channel_subs.first.client_id).to be_nil
end
it 'fails if data is invalid' do
expect { subject.save(channel: '', client_id: '') }.to raise_error ArgumentError
expect { subject.save({}) }.to raise_error ArgumentError
expect { subject.save(channel: 'not-enabled-channel', device_id: 'foo') }.to raise_error Ably::Exceptions::UnauthorizedRequest
expect { subject.save(channel: 'pushenabled:foo', device_id: 'not-registered-so-will-fail') }.to raise_error Ably::Exceptions::InvalidRequest
end
end
describe '#remove_where (#RSH1c5)' do
let(:client_id) { random_str }
let(:device_id) { random_str }
let(:fixed_channel) { "pushenabled:#{random_str}" }
let(:fixture_count) { 6 }
before(:all) do
# As push tests often use the global scope (devices),
# we need to ensure tests cannot conflict
reload_test_app
end
before do
fixture_count.times.map do |index|
[
lambda { subject.save(channel: "pushenabled:#{random_str}", client_id: client_id) },
lambda { subject.save(channel: "pushenabled:#{random_str}", device_id: device_id) },
lambda { subject.save(channel: fixed_channel, device_id: device_id_2) }
]
end.flatten.map do |proc|
Thread.new { proc.call }
end.each(&:join) # Wait for all threads to complete
end
# TODO: Reinstate once delete subscriptions by channel is possible
# See https://github.com/ably/realtime/issues/1359
it 'removes matching channels' do
skip 'deleting subscriptions is not yet supported realtime#1359'
subject.remove_where channel: fixed_channel, full_wait: true
expect(subject.list(channel: fixed_channel).items.count).to eql(0)
expect(subject.list(client_id: client_id).items.count).to eql(0)
expect(subject.list(device_id: device_id).items.count).to eql(0)
end
it 'removes matching client_ids' do
subject.remove_where client_id: client_id, full_wait: true
expect(subject.list(client_id: client_id).items.count).to eql(0)
expect(subject.list(device_id: device_id).items.count).to eql(fixture_count)
end
it 'removes matching device_ids' do
subject.remove_where device_id: device_id, full_wait: true
expect(subject.list(device_id: device_id).items.count).to eql(0)
expect(subject.list(client_id: client_id).items.count).to eql(fixture_count)
end
it 'device_id and client_id filters in the same request are not suppoorted' do
expect { subject.remove_where(device_id: device_id, client_id: client_id) }.to raise_error(Ably::Exceptions::InvalidRequest)
end
it 'succeeds on no match' do
subject.remove_where device_id: random_str, full_wait: true
expect(subject.list(device_id: device_id).items.count).to eql(fixture_count)
subject.remove_where client_id: random_str
expect(subject.list(client_id: client_id).items.count).to eql(fixture_count)
end
end
describe '#remove (#RSH1c4)' do
let(:channel) { "pushenabled:#{random_str}" }
let(:channel2) { "pushenabled:#{random_str}" }
let(:client_id) { random_str }
let(:device_id) { random_str }
before(:all) do
# As push tests often use the global scope (devices),
# we need to ensure tests cannot conflict
reload_test_app
end
before do
[
lambda { subject.save(channel: channel, client_id: client_id) },
lambda { subject.save(channel: channel, device_id: device_id) },
lambda { subject.save(channel: channel2, client_id: client_id) }
].map do |proc|
Thread.new { proc.call }
end.each(&:join) # Wait for all threads to complete
end
it 'removes match for Hash object by channel and client_id' do
subject.remove(channel: channel, client_id: client_id)
expect(subject.list(client_id: client_id).items.count).to eql(1)
end
it 'removes match for PushChannelSubscription object by channel and client_id' do
push_sub = subject.list(channel: channel, client_id: client_id).items.first
expect(push_sub).to be_a(Ably::Models::PushChannelSubscription)
subject.remove(push_sub)
expect(subject.list(client_id: client_id).items.count).to eql(1)
end
it 'removes match for Hash object by channel and device_id' do
subject.remove(channel: channel, device_id: device_id)
expect(subject.list(device_id: device_id).items.count).to eql(0)
end
it 'removes match for PushChannelSubscription object by channel and client_id' do
push_sub = subject.list(channel: channel, device_id: device_id).items.first
expect(push_sub).to be_a(Ably::Models::PushChannelSubscription)
subject.remove(push_sub)
expect(subject.list(device_id: device_id).items.count).to eql(0)
end
it 'succeeds even if there is no match' do
subject.remove(device_id: 'does-not-exist', channel: random_str)
expect(subject.list(device_id: 'does-not-exist').items.count).to eql(0)
end
end
end
end
end