rpush 4.2.0 → 5.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +248 -163
  3. data/README.md +103 -17
  4. data/lib/generators/templates/add_gcm.rb +4 -4
  5. data/lib/generators/templates/add_rpush.rb +4 -4
  6. data/lib/generators/templates/rpush.rb +4 -0
  7. data/lib/generators/templates/rpush_3_3_1_updates.rb +2 -2
  8. data/lib/rpush/cli.rb +1 -1
  9. data/lib/rpush/client/active_model.rb +4 -1
  10. data/lib/rpush/client/active_model/adm/data_validator.rb +1 -1
  11. data/lib/rpush/client/active_model/apns/device_token_format_validator.rb +2 -2
  12. data/lib/rpush/client/active_model/apns/notification.rb +9 -1
  13. data/lib/rpush/client/active_model/apns/notification_payload_size_validator.rb +15 -0
  14. data/lib/rpush/client/active_model/apns2/notification.rb +14 -0
  15. data/lib/rpush/client/active_model/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +1 -1
  16. data/lib/rpush/client/active_model/gcm/notification.rb +2 -2
  17. data/lib/rpush/client/active_model/payload_data_size_validator.rb +1 -1
  18. data/lib/rpush/client/active_model/registration_ids_count_validator.rb +1 -1
  19. data/lib/rpush/client/active_model/webpush/app.rb +41 -0
  20. data/lib/rpush/client/active_model/webpush/notification.rb +66 -0
  21. data/lib/rpush/client/active_record.rb +4 -0
  22. data/lib/rpush/client/active_record/apns/active_record_serializable_notification.rb +65 -0
  23. data/lib/rpush/client/active_record/apns/notification.rb +1 -57
  24. data/lib/rpush/client/active_record/apns2/notification.rb +4 -1
  25. data/lib/rpush/client/active_record/apnsp8/notification.rb +1 -0
  26. data/lib/rpush/client/active_record/webpush/app.rb +11 -0
  27. data/lib/rpush/client/active_record/webpush/notification.rb +12 -0
  28. data/lib/rpush/client/redis.rb +3 -0
  29. data/lib/rpush/client/redis/apns2/notification.rb +1 -0
  30. data/lib/rpush/client/redis/apnsp8/notification.rb +2 -0
  31. data/lib/rpush/client/redis/pushy/notification.rb +0 -1
  32. data/lib/rpush/client/redis/webpush/app.rb +15 -0
  33. data/lib/rpush/client/redis/webpush/notification.rb +15 -0
  34. data/lib/rpush/configuration.rb +3 -2
  35. data/lib/rpush/daemon.rb +4 -1
  36. data/lib/rpush/daemon/apns/feedback_receiver.rb +1 -1
  37. data/lib/rpush/daemon/apns2/delivery.rb +13 -2
  38. data/lib/rpush/daemon/apnsp8/delivery.rb +7 -2
  39. data/lib/rpush/daemon/app_runner.rb +1 -1
  40. data/lib/rpush/daemon/batch.rb +12 -5
  41. data/lib/rpush/daemon/delivery.rb +1 -2
  42. data/lib/rpush/daemon/store/active_record/reconnectable.rb +1 -1
  43. data/lib/rpush/daemon/webpush.rb +10 -0
  44. data/lib/rpush/daemon/webpush/delivery.rb +114 -0
  45. data/lib/rpush/logger.rb +1 -0
  46. data/lib/rpush/version.rb +2 -2
  47. data/spec/functional/apns2_spec.rb +97 -2
  48. data/spec/functional/gcm_priority_spec.rb +40 -0
  49. data/spec/functional/webpush_spec.rb +30 -0
  50. data/spec/spec_helper.rb +2 -0
  51. data/spec/support/simplecov_helper.rb +1 -1
  52. data/spec/unit/client/active_record/adm/app_spec.rb +2 -54
  53. data/spec/unit/client/active_record/adm/notification_spec.rb +2 -39
  54. data/spec/unit/client/active_record/apns/app_spec.rb +3 -26
  55. data/spec/unit/client/active_record/apns/feedback_spec.rb +1 -5
  56. data/spec/unit/client/active_record/apns/notification_spec.rb +29 -293
  57. data/spec/unit/client/active_record/apns2/app_spec.rb +4 -0
  58. data/spec/unit/client/active_record/apns2/notification_spec.rb +65 -0
  59. data/spec/unit/client/active_record/apnsp8/notification_spec.rb +28 -0
  60. data/spec/unit/client/active_record/app_spec.rb +1 -26
  61. data/spec/unit/client/active_record/gcm/app_spec.rb +3 -1
  62. data/spec/unit/client/active_record/gcm/notification_spec.rb +6 -88
  63. data/spec/unit/client/active_record/notification_spec.rb +3 -11
  64. data/spec/unit/client/active_record/pushy/app_spec.rb +2 -13
  65. data/spec/unit/client/active_record/pushy/notification_spec.rb +2 -55
  66. data/spec/unit/client/active_record/shared/app.rb +14 -0
  67. data/spec/unit/{notification_shared.rb → client/active_record/shared/notification.rb} +12 -7
  68. data/spec/unit/client/active_record/webpush/app_spec.rb +6 -0
  69. data/spec/unit/client/active_record/webpush/notification_spec.rb +6 -0
  70. data/spec/unit/client/active_record/wns/badge_notification_spec.rb +1 -11
  71. data/spec/unit/client/active_record/wns/raw_notification_spec.rb +3 -12
  72. data/spec/unit/client/active_record/wpns/app_spec.rb +3 -1
  73. data/spec/unit/client/active_record/wpns/notification_spec.rb +2 -17
  74. data/spec/unit/client/redis/adm/app_spec.rb +5 -0
  75. data/spec/unit/client/redis/adm/notification_spec.rb +5 -0
  76. data/spec/unit/client/redis/apns/app_spec.rb +5 -0
  77. data/spec/unit/client/redis/apns/feedback_spec.rb +5 -0
  78. data/spec/unit/client/redis/apns/notification_spec.rb +50 -0
  79. data/spec/unit/client/redis/apns2/app_spec.rb +4 -0
  80. data/spec/unit/client/redis/apns2/notification_spec.rb +50 -0
  81. data/spec/unit/client/redis/apnsp8/notification_spec.rb +29 -0
  82. data/spec/unit/client/redis/app_spec.rb +5 -0
  83. data/spec/unit/client/redis/gcm/app_spec.rb +5 -0
  84. data/spec/unit/client/redis/gcm/notification_spec.rb +5 -0
  85. data/spec/unit/client/redis/notification_spec.rb +5 -0
  86. data/spec/unit/client/redis/pushy/app_spec.rb +5 -0
  87. data/spec/unit/client/redis/pushy/notification_spec.rb +5 -0
  88. data/spec/unit/client/redis/webpush/app_spec.rb +5 -0
  89. data/spec/unit/client/redis/webpush/notification_spec.rb +5 -0
  90. data/spec/unit/client/redis/wns/badge_notification_spec.rb +5 -0
  91. data/spec/unit/client/redis/wns/raw_notification_spec.rb +22 -0
  92. data/spec/unit/client/redis/wpns/app_spec.rb +5 -0
  93. data/spec/unit/client/redis/wpns/notification_spec.rb +5 -0
  94. data/spec/unit/client/shared/adm/app.rb +51 -0
  95. data/spec/unit/client/shared/adm/notification.rb +39 -0
  96. data/spec/unit/client/shared/apns/app.rb +29 -0
  97. data/spec/unit/client/shared/apns/feedback.rb +9 -0
  98. data/spec/unit/client/shared/apns/notification.rb +262 -0
  99. data/spec/unit/client/shared/app.rb +17 -0
  100. data/spec/unit/client/shared/gcm/app.rb +4 -0
  101. data/spec/unit/client/shared/gcm/notification.rb +77 -0
  102. data/spec/unit/client/shared/notification.rb +10 -0
  103. data/spec/unit/client/shared/pushy/app.rb +17 -0
  104. data/spec/unit/client/shared/pushy/notification.rb +55 -0
  105. data/spec/unit/client/shared/webpush/app.rb +33 -0
  106. data/spec/unit/client/shared/webpush/notification.rb +83 -0
  107. data/spec/unit/client/shared/wns/badge_notification.rb +15 -0
  108. data/spec/unit/client/shared/wns/raw_notification.rb +21 -0
  109. data/spec/unit/client/shared/wpns/app.rb +4 -0
  110. data/spec/unit/client/shared/wpns/notification.rb +18 -0
  111. data/spec/unit/daemon/batch_spec.rb +50 -2
  112. data/spec/unit/daemon/delivery_spec.rb +10 -0
  113. data/spec/unit/daemon/pushy/delivery_spec.rb +5 -3
  114. data/spec/unit/daemon/shared/store.rb +312 -0
  115. data/spec/unit/daemon/store/active_record/reconnectable_spec.rb +7 -7
  116. data/spec/unit/daemon/store/active_record_spec.rb +2 -290
  117. data/spec/unit/daemon/store/redis_spec.rb +2 -291
  118. data/spec/unit/daemon/webpush/delivery_spec.rb +144 -0
  119. data/spec/unit_spec_helper.rb +3 -0
  120. metadata +135 -12
  121. data/lib/rpush/client/active_model/apns/binary_notification_validator.rb +0 -16
@@ -1,58 +1,6 @@
1
1
  require 'unit_spec_helper'
2
2
 
3
3
  describe Rpush::Client::ActiveRecord::Adm::App do
4
- subject { Rpush::Client::ActiveRecord::Adm::App.new(name: 'test', environment: 'development', client_id: 'CLIENT_ID', client_secret: 'CLIENT_SECRET') }
5
- let(:existing_app) { Rpush::Client::ActiveRecord::Adm::App.create!(name: 'existing', environment: 'development', client_id: 'CLIENT_ID', client_secret: 'CLIENT_SECRET') }
6
-
7
- it 'should be valid if properly instantiated' do
8
- expect(subject).to be_valid
9
- end
10
-
11
- it 'should be invalid if name' do
12
- subject.name = nil
13
- expect(subject).not_to be_valid
14
- expect(subject.errors[:name]).to eq ["can't be blank"]
15
- end
16
-
17
- it 'should be invalid if name is not unique within scope' do
18
- subject.name = existing_app.name
19
- expect(subject).not_to be_valid
20
- expect(subject.errors[:name]).to eq ["has already been taken"]
21
- end
22
-
23
- it 'should be invalid if missing client_id' do
24
- subject.client_id = nil
25
- expect(subject).not_to be_valid
26
- expect(subject.errors[:client_id]).to eq ["can't be blank"]
27
- end
28
-
29
- it 'should be invalid if missing client_secret' do
30
- subject.client_secret = nil
31
- expect(subject).not_to be_valid
32
- expect(subject.errors[:client_secret]).to eq ["can't be blank"]
33
- end
34
-
35
- describe '#access_token_expired?' do
36
- before(:each) do
37
- Timecop.freeze(Time.now)
38
- end
39
-
40
- after do
41
- Timecop.return
42
- end
43
-
44
- it 'should return true if access_token_expiration is nil' do
45
- expect(subject.access_token_expired?).to eq(true)
46
- end
47
-
48
- it 'should return true if expired' do
49
- subject.access_token_expiration = Time.now - 5.minutes
50
- expect(subject.access_token_expired?).to eq(true)
51
- end
52
-
53
- it 'should return false if not expired' do
54
- subject.access_token_expiration = Time.now + 5.minutes
55
- expect(subject.access_token_expired?).to eq(false)
56
- end
57
- end
4
+ it_behaves_like 'Rpush::Client::Adm::App'
5
+ it_behaves_like 'Rpush::Client::ActiveRecord::App'
58
6
  end if active_record?
@@ -1,43 +1,6 @@
1
1
  require 'unit_spec_helper'
2
- require 'unit/notification_shared.rb'
3
2
 
4
3
  describe Rpush::Client::ActiveRecord::Adm::Notification do
5
- it_should_behave_like 'an Notification subclass'
6
-
7
- let(:app) { Rpush::Client::ActiveRecord::Adm::App.create!(name: 'test', client_id: 'CLIENT_ID', client_secret: 'CLIENT_SECRET') }
8
- let(:notification_class) { Rpush::Client::ActiveRecord::Adm::Notification }
9
- let(:notification) { notification_class.new }
10
-
11
- it "has a 'data' payload limit of 6144 bytes" do
12
- notification.data = { key: "a" * 6144 }
13
- expect(notification.valid?).to eq(false)
14
- expect(notification.errors[:base]).to eq ["Notification payload data cannot be larger than 6144 bytes."]
15
- end
16
-
17
- it 'limits the number of registration ids to 100' do
18
- notification.registration_ids = ['a'] * (100 + 1)
19
- expect(notification.valid?).to eq(false)
20
- expect(notification.errors[:base]).to eq ["Number of registration_ids cannot be larger than 100."]
21
- end
22
-
23
- it 'validates data can be blank if collapse_key is set' do
24
- notification.app = app
25
- notification.registration_ids = 'a'
26
- notification.collapse_key = 'test'
27
- notification.data = nil
28
- expect(notification.valid?).to eq(true)
29
- expect(notification.errors[:data]).to be_empty
30
- end
31
-
32
- it 'validates data is present if collapse_key is not set' do
33
- notification.collapse_key = nil
34
- notification.data = nil
35
- expect(notification.valid?).to eq(false)
36
- expect(notification.errors[:data]).to eq ['must be set unless collapse_key is specified']
37
- end
38
-
39
- it 'includes expiresAfter in the payload' do
40
- notification.expiry = 100
41
- expect(notification.as_json['expiresAfter']).to eq 100
42
- end
4
+ it_behaves_like 'Rpush::Client::Adm::Notification'
5
+ it_behaves_like 'Rpush::Client::ActiveRecord::Notification'
43
6
  end if active_record?
@@ -1,29 +1,6 @@
1
1
  require 'unit_spec_helper'
2
2
 
3
- describe Rpush::Client::ActiveRecord::App do
4
- it 'does not validate an app with an invalid certificate' do
5
- app = Rpush::Client::ActiveRecord::Apns::App.new(name: 'test', environment: 'development', certificate: 'foo')
6
- app.valid?
7
- expect(app.errors[:certificate]).to eq ['value must contain a certificate and a private key.']
8
- end
9
-
10
- it 'validates a certificate without a password' do
11
- app = Rpush::Client::ActiveRecord::Apns::App.new name: 'test', environment: 'development', certificate: TEST_CERT
12
- app.valid?
13
- expect(app.errors[:certificate]).to eq []
14
- end
15
-
16
- it 'validates a certificate with a password' do
17
- app = Rpush::Client::ActiveRecord::Apns::App.new name: 'test', environment: 'development',
18
- certificate: TEST_CERT_WITH_PASSWORD, password: 'fubar'
19
- app.valid?
20
- expect(app.errors[:certificate]).to eq []
21
- end
22
-
23
- it 'validates a certificate with an incorrect password' do
24
- app = Rpush::Client::ActiveRecord::Apns::App.new name: 'test', environment: 'development',
25
- certificate: TEST_CERT_WITH_PASSWORD, password: 'incorrect'
26
- app.valid?
27
- expect(app.errors[:certificate]).to eq ['value must contain a certificate and a private key.']
28
- end
3
+ describe Rpush::Client::ActiveRecord::Apns::App do
4
+ it_behaves_like 'Rpush::Client::Apns::App'
5
+ it_behaves_like 'Rpush::Client::ActiveRecord::App'
29
6
  end if active_record?
@@ -1,9 +1,5 @@
1
1
  require 'unit_spec_helper'
2
2
 
3
3
  describe Rpush::Client::ActiveRecord::Apns::Feedback do
4
- it 'validates the format of the device_token' do
5
- notification = Rpush::Client::ActiveRecord::Apns::Feedback.new(device_token: "{$%^&*()}")
6
- expect(notification.valid?).to be_falsey
7
- expect(notification.errors[:device_token]).to include('is invalid')
8
- end
4
+ it_behaves_like 'Rpush::Client::Apns::Feedback'
9
5
  end if active_record?
@@ -1,329 +1,65 @@
1
- # encoding: US-ASCII
2
-
3
1
  require "unit_spec_helper"
4
- require 'unit/notification_shared.rb'
5
2
 
6
3
  describe Rpush::Client::ActiveRecord::Apns::Notification do
7
- it_should_behave_like 'an Notification subclass'
8
-
9
- let(:app) { Rpush::Client::ActiveRecord::Apns::App.create!(name: 'my_app', environment: 'development', certificate: TEST_CERT) }
10
- let(:notification_class) { Rpush::Client::ActiveRecord::Apns::Notification }
11
- let(:notification) { notification_class.new }
4
+ subject(:notification) { described_class.new }
12
5
 
13
- it "should validate the format of the device_token" do
14
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new(device_token: "{$%^&*()}")
15
- expect(notification.valid?).to be_falsey
16
- expect(notification.errors[:device_token].include?("is invalid")).to be_truthy
17
- end
6
+ it_behaves_like 'Rpush::Client::Apns::Notification'
7
+ it_behaves_like 'Rpush::Client::ActiveRecord::Notification'
18
8
 
19
9
  it "should validate the length of the binary conversion of the notification" do
10
+ notification = described_class.new
11
+ notification.app = Rpush::Apns2::App.create(name: 'test', environment: 'development')
20
12
  notification.device_token = "a" * 108
21
- notification.alert = "way too long!" * 200
22
- expect(notification.valid?).to be_falsey
23
- expect(notification.errors[:base].include?("APN notification cannot be larger than 2048 bytes. Try condensing your alert and device attributes.")).to be_truthy
24
- end
13
+ notification.alert = ""
25
14
 
26
- it "should store long alerts" do
27
- notification.app = app
28
- notification.device_token = "a" * 108
29
- notification.alert = "*" * 300
15
+ notification.alert << "a" until notification.payload.bytesize == 2048
30
16
  expect(notification.valid?).to be_truthy
17
+ expect(notification.errors[:base]).to be_empty
31
18
 
32
- notification.save!
33
- notification.reload
34
- expect(notification.alert).to eq("*" * 300)
35
- end
36
-
37
- it "should default the sound to nil" do
38
- expect(notification.sound).to be_nil
39
- end
40
-
41
- it "should default the expiry to 1 day" do
42
- expect(notification.expiry).to eq 1.day.to_i
43
- end
44
- end if active_record?
45
-
46
- describe Rpush::Client::ActiveRecord::Apns::Notification, "when assigning the device token" do
47
- it "should strip spaces from the given string" do
48
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new(device_token: "o m g")
49
- expect(notification.device_token).to eq "omg"
50
- end
51
-
52
- it "should strip chevrons from the given string" do
53
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new(device_token: "<omg>")
54
- expect(notification.device_token).to eq "omg"
55
- end
56
- end if active_record?
57
-
58
- describe Rpush::Client::ActiveRecord::Apns::Notification, "as_json" do
59
- it "should include the alert if present" do
60
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new(alert: "hi mom")
61
- expect(notification.as_json["aps"]["alert"]).to eq "hi mom"
62
- end
63
-
64
- it "should not include the alert key if the alert is not present" do
65
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new(alert: nil)
66
- expect(notification.as_json["aps"].key?("alert")).to be_falsey
67
- end
68
-
69
- it "should encode the alert as JSON if it is a Hash" do
70
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new(alert: { 'body' => "hi mom", 'alert-loc-key' => "View" })
71
- expect(notification.as_json["aps"]["alert"]).to eq('body' => "hi mom", 'alert-loc-key' => "View")
72
- end
73
-
74
- it "should include the badge if present" do
75
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new(badge: 6)
76
- expect(notification.as_json["aps"]["badge"]).to eq 6
77
- end
78
-
79
- it "should not include the badge key if the badge is not present" do
80
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new(badge: nil)
81
- expect(notification.as_json["aps"].key?("badge")).to be_falsey
82
- end
83
-
84
- it "should include the sound if present" do
85
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new(sound: "my_sound.aiff")
86
- expect(notification.as_json["aps"]["sound"]).to eq "my_sound.aiff"
87
- end
88
-
89
- it "should not include the sound key if the sound is not present" do
90
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new(sound: nil)
91
- expect(notification.as_json["aps"].key?("sound")).to be_falsey
92
- end
93
-
94
- it "should encode the sound as JSON if it is a Hash" do
95
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new(sound: { 'name' => "my_sound.aiff", 'critical' => 1, 'volume' => 0.5 })
96
- expect(notification.as_json["aps"]["sound"]).to eq('name' => "my_sound.aiff", 'critical' => 1, 'volume' => 0.5)
97
- end
98
-
99
- it "should include attributes for the device" do
100
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new
101
- notification.data = { omg: :lol, wtf: :dunno }
102
- expect(notification.as_json["omg"]).to eq "lol"
103
- expect(notification.as_json["wtf"]).to eq "dunno"
104
- end
105
-
106
- it "should allow attributes to include a hash" do
107
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new
108
- notification.data = { omg: { ilike: :hashes } }
109
- expect(notification.as_json["omg"]["ilike"]).to eq "hashes"
110
- end
111
- end if active_record?
112
-
113
- describe Rpush::Client::ActiveRecord::Apns::Notification, 'MDM' do
114
- let(:magic) { 'abc123' }
115
- let(:notification) { Rpush::Client::ActiveRecord::Apns::Notification.new }
116
-
117
- before do
118
- notification.device_token = "a" * 108
119
- notification.id = 1234
120
- end
121
-
122
- it 'includes the mdm magic in the payload' do
123
- notification.mdm = magic
124
- expect(notification.as_json).to eq('mdm' => magic)
125
- end
126
-
127
- it 'does not include aps attribute' do
128
- notification.alert = "i'm doomed"
129
- notification.mdm = magic
130
- expect(notification.as_json.key?('aps')).to be_falsey
131
- end
132
-
133
- it 'can be converted to binary' do
134
- notification.mdm = magic
135
- expect(notification.to_binary).to be_present
19
+ notification.alert << "a"
20
+ expect(notification.valid?).to be_falsey
21
+ expect(notification.errors[:base].include?("APN notification cannot be larger than 2048 bytes. Try condensing your alert and device attributes.")).to be_truthy
136
22
  end
137
- end if active_record?
138
23
 
139
- describe Rpush::Client::ActiveRecord::Apns::Notification, 'mutable-content' do
140
- let(:notification) { Rpush::Client::ActiveRecord::Apns::Notification.new }
141
-
142
- it 'includes mutable-content in the payload' do
143
- notification.mutable_content = true
144
- expect(notification.as_json['aps']['mutable-content']).to eq 1
145
- end
24
+ describe "multi_json usage" do
25
+ describe "alert" do
26
+ subject(:notification) { described_class.new(alert: { a: 1 }, alert_is_json: true) }
146
27
 
147
- it 'does not include content-available in the payload if not set' do
148
- expect(notification.as_json['aps'].key?('mutable-content')).to be_falsey
149
- end
28
+ it "should call MultiJson.load when multi_json version is 1.3.0" do
29
+ allow(Gem).to receive(:loaded_specs).and_return('multi_json' => Gem::Specification.new('multi_json', '1.3.0'))
30
+ expect(MultiJson).to receive(:load).with(any_args)
31
+ notification.alert
32
+ end
150
33
 
151
- it 'does not include mutable-content as a non-aps attribute' do
152
- notification.mutable_content = true
153
- expect(notification.as_json.key?('mutable-content')).to be_falsey
34
+ it "should call MultiJson.decode when multi_json version is 1.2.9" do
35
+ allow(Gem).to receive(:loaded_specs).and_return('multi_json' => Gem::Specification.new('multi_json', '1.2.9'))
36
+ expect(MultiJson).to receive(:decode).with(any_args)
37
+ notification.alert
38
+ end
39
+ end
154
40
  end
155
41
 
156
- it 'does not overwrite existing attributes for the device' do
157
- notification.data = { hi: :mom }
158
- notification.mutable_content = true
159
- expect(notification.as_json['aps']['mutable-content']).to eq 1
160
- expect(notification.as_json['hi']).to eq 'mom'
42
+ it "should default the sound to nil" do
43
+ expect(notification.sound).to be_nil
161
44
  end
162
45
 
163
46
  it 'does not overwrite the mutable-content flag when setting attributes for the device' do
164
47
  notification.mutable_content = true
165
- notification.data = { hi: :mom }
48
+ notification.data = { 'hi' => 'mom' }
166
49
  expect(notification.as_json['aps']['mutable-content']).to eq 1
167
50
  expect(notification.as_json['hi']).to eq 'mom'
168
51
  end
169
- end if active_record?
170
-
171
- describe Rpush::Client::ActiveRecord::Apns::Notification, 'content-available' do
172
- let(:notification) { Rpush::Client::ActiveRecord::Apns::Notification.new }
173
-
174
- it 'includes content-available in the payload' do
175
- notification.content_available = true
176
- expect(notification.as_json['aps']['content-available']).to eq 1
177
- end
178
-
179
- it 'does not include content-available in the payload if not set' do
180
- expect(notification.as_json['aps'].key?('content-available')).to be_falsey
181
- end
182
-
183
- it 'does not include content-available as a non-aps attribute' do
184
- notification.content_available = true
185
- expect(notification.as_json.key?('content-available')).to be_falsey
186
- end
187
-
188
- it 'does not overwrite existing attributes for the device' do
189
- notification.data = { hi: :mom }
190
- notification.content_available = true
191
- expect(notification.as_json['aps']['content-available']).to eq 1
192
- expect(notification.as_json['hi']).to eq 'mom'
193
- end
194
52
 
195
53
  it 'does not overwrite the content-available flag when setting attributes for the device' do
196
54
  notification.content_available = true
197
- notification.data = { hi: :mom }
55
+ notification.data = { 'hi' => 'mom' }
198
56
  expect(notification.as_json['aps']['content-available']).to eq 1
199
57
  expect(notification.as_json['hi']).to eq 'mom'
200
58
  end
201
- end if active_record?
202
-
203
- describe Rpush::Client::ActiveRecord::Apns::Notification, 'url-args' do
204
- let(:notification) { Rpush::Client::ActiveRecord::Apns::Notification.new }
205
-
206
- it 'includes url-args in the payload' do
207
- notification.url_args = ['url-arg-1']
208
- expect(notification.as_json['aps']['url-args']).to eq ['url-arg-1']
209
- end
210
-
211
- it 'does not include url-args in the payload if not set' do
212
- expect(notification.as_json['aps'].key?('url-args')).to be_falsey
213
- end
214
- end if active_record?
215
-
216
- describe Rpush::Client::ActiveRecord::Apns::Notification, 'category' do
217
- let(:notification) { Rpush::Client::ActiveRecord::Apns::Notification.new }
218
-
219
- it 'includes category in the payload' do
220
- notification.category = 'INVITE_CATEGORY'
221
- expect(notification.as_json['aps']['category']).to eq 'INVITE_CATEGORY'
222
- end
223
-
224
- it 'does not include category in the payload if not set' do
225
- expect(notification.as_json['aps'].key?('category')).to be_falsey
226
- end
227
- end if active_record?
228
-
229
- describe Rpush::Client::ActiveRecord::Apns::Notification, 'to_binary' do
230
- let(:notification) { Rpush::Client::ActiveRecord::Apns::Notification.new }
231
-
232
- before do
233
- notification.device_token = "a" * 108
234
- notification.id = 1234
235
- end
236
-
237
- it 'uses APNS_PRIORITY_CONSERVE_POWER if content-available is the only key' do
238
- notification.alert = nil
239
- notification.badge = nil
240
- notification.sound = nil
241
- notification.content_available = true
242
- bytes = notification.to_binary.bytes.to_a[-4..-1]
243
- expect(bytes.first).to eq 5 # priority item ID
244
- expect(bytes.last).to eq Rpush::Client::ActiveRecord::Apns::Notification::APNS_PRIORITY_CONSERVE_POWER
245
- end
246
-
247
- it 'uses APNS_PRIORITY_IMMEDIATE if content-available is not the only key' do
248
- notification.alert = "New stuff!"
249
- notification.badge = nil
250
- notification.sound = nil
251
- notification.content_available = true
252
- bytes = notification.to_binary.bytes.to_a[-4..-1]
253
- expect(bytes.first).to eq 5 # priority item ID
254
- expect(bytes.last).to eq Rpush::Client::ActiveRecord::Apns::Notification::APNS_PRIORITY_IMMEDIATE
255
- end
256
-
257
- it "should correctly convert the notification to binary" do
258
- notification.sound = "1.aiff"
259
- notification.badge = 3
260
- notification.alert = "Don't panic Mr Mainwaring, don't panic!"
261
- notification.data = { hi: :mom }
262
- notification.expiry = 86_400 # 1 day
263
- notification.priority = Rpush::Client::ActiveRecord::Apns::Notification::APNS_PRIORITY_IMMEDIATE
264
- notification.app = Rpush::Client::ActiveRecord::Apns::App.new(name: 'my_app', environment: 'development', certificate: TEST_CERT)
265
- now = Time.now
266
- allow(Time).to receive_messages(now: now)
267
- expect(notification.to_binary).to eq "\x02\x00\x00\x00\xAF\x01\x00 \xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\x02\x00a{\"aps\":{\"alert\":\"Don't panic Mr Mainwaring, don't panic!\",\"badge\":3,\"sound\":\"1.aiff\"},\"hi\":\"mom\"}\x03\x00\x04\x00\x00\x04\xD2\x04\x00\x04#{[now.to_i + 86_400].pack('N')}\x05\x00\x01\n"
268
- end
269
- end if active_record?
270
-
271
- describe Rpush::Client::ActiveRecord::Apns::Notification, "bug #31" do
272
- it 'does not confuse a JSON looking string as JSON' do
273
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new
274
- notification.alert = "{\"one\":2}"
275
- expect(notification.alert).to eq "{\"one\":2}"
276
- end
277
59
 
278
60
  it 'does confuse a JSON looking string as JSON if the alert_is_json attribute is not present' do
279
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new
280
61
  allow(notification).to receive_messages(has_attribute?: false)
281
62
  notification.alert = "{\"one\":2}"
282
63
  expect(notification.alert).to eq('one' => 2)
283
64
  end
284
65
  end if active_record?
285
-
286
- describe Rpush::Client::ActiveRecord::Apns::Notification, "bug #35" do
287
- it "should limit payload size to 256 bytes but not the entire packet" do
288
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new do |n|
289
- n.device_token = "a" * 108
290
- n.alert = "a" * 210
291
- n.app = Rpush::Client::ActiveRecord::Apns::App.create!(name: 'my_app', environment: 'development', certificate: TEST_CERT)
292
- end
293
-
294
- expect(notification.to_binary(for_validation: true).bytesize).to be > 256
295
- expect(notification.payload.bytesize).to be < 256
296
- expect(notification).to be_valid
297
- end
298
- end if active_record?
299
-
300
- describe Rpush::Client::ActiveRecord::Apns::Notification, "multi_json usage" do
301
- describe Rpush::Client::ActiveRecord::Apns::Notification, "alert" do
302
- it "should call MultiJson.load when multi_json version is 1.3.0" do
303
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new(alert: { a: 1 }, alert_is_json: true)
304
- allow(Gem).to receive(:loaded_specs).and_return('multi_json' => Gem::Specification.new('multi_json', '1.3.0'))
305
- expect(MultiJson).to receive(:load).with(any_args)
306
- notification.alert
307
- end
308
-
309
- it "should call MultiJson.decode when multi_json version is 1.2.9" do
310
- notification = Rpush::Client::ActiveRecord::Apns::Notification.new(alert: { a: 1 }, alert_is_json: true)
311
- allow(Gem).to receive(:loaded_specs).and_return('multi_json' => Gem::Specification.new('multi_json', '1.2.9'))
312
- expect(MultiJson).to receive(:decode).with(any_args)
313
- notification.alert
314
- end
315
- end
316
- end if active_record?
317
-
318
- describe Rpush::Client::ActiveRecord::Apns::Notification, 'thread-id' do
319
- let(:notification) { Rpush::Client::ActiveRecord::Apns::Notification.new }
320
-
321
- it 'includes thread-id in the payload' do
322
- notification.thread_id = 'THREAD-ID'
323
- expect(notification.as_json['aps']['thread-id']).to eq 'THREAD-ID'
324
- end
325
-
326
- it 'does not include thread-id in the payload if not set' do
327
- expect(notification.as_json['aps']).to_not have_key('thread-id')
328
- end
329
- end if active_record?