rpush 1.0.0 → 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/README.md +12 -14
  4. data/bin/rpush +11 -2
  5. data/lib/generators/rpush_generator.rb +2 -0
  6. data/lib/generators/templates/add_adm.rb +5 -5
  7. data/lib/generators/templates/add_alert_is_json_to_rapns_notifications.rb +1 -1
  8. data/lib/generators/templates/add_app_to_rapns.rb +2 -2
  9. data/lib/generators/templates/add_fail_after_to_rpush_notifications.rb +1 -1
  10. data/lib/generators/templates/add_gcm.rb +32 -32
  11. data/lib/generators/templates/add_rpush.rb +67 -67
  12. data/lib/generators/templates/add_wpns.rb +2 -2
  13. data/lib/generators/templates/create_rapns_apps.rb +5 -5
  14. data/lib/generators/templates/create_rapns_feedback.rb +2 -2
  15. data/lib/generators/templates/create_rapns_notifications.rb +15 -15
  16. data/lib/generators/templates/rpush.rb +10 -7
  17. data/lib/generators/templates/rpush_2_0_0_updates.rb +23 -0
  18. data/lib/rpush.rb +4 -28
  19. data/lib/rpush/client/active_model.rb +21 -0
  20. data/lib/rpush/client/active_model/adm/app.rb +23 -0
  21. data/lib/rpush/client/active_model/adm/data_validator.rb +14 -0
  22. data/lib/rpush/client/active_model/adm/notification.rb +28 -0
  23. data/lib/rpush/client/active_model/apns/app.rb +37 -0
  24. data/lib/rpush/client/active_model/apns/binary_notification_validator.rb +16 -0
  25. data/lib/rpush/client/active_model/apns/device_token_format_validator.rb +14 -0
  26. data/lib/rpush/client/active_model/apns/notification.rb +90 -0
  27. data/lib/rpush/client/active_model/gcm/app.rb +19 -0
  28. data/lib/rpush/client/active_model/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +14 -0
  29. data/lib/rpush/client/active_model/gcm/notification.rb +31 -0
  30. data/lib/rpush/client/active_model/notification.rb +26 -0
  31. data/lib/rpush/client/active_model/payload_data_size_validator.rb +13 -0
  32. data/lib/rpush/client/active_model/registration_ids_count_validator.rb +13 -0
  33. data/lib/rpush/client/active_model/wpns/app.rb +13 -0
  34. data/lib/rpush/client/active_model/wpns/notification.rb +17 -0
  35. data/lib/rpush/client/active_record.rb +19 -0
  36. data/lib/rpush/client/active_record/adm/app.rb +11 -0
  37. data/lib/rpush/client/active_record/adm/notification.rb +11 -0
  38. data/lib/rpush/client/active_record/apns/app.rb +11 -0
  39. data/lib/rpush/client/active_record/apns/feedback.rb +20 -0
  40. data/lib/rpush/client/active_record/apns/notification.rb +46 -0
  41. data/lib/rpush/client/active_record/app.rb +17 -0
  42. data/lib/rpush/client/active_record/gcm/app.rb +11 -0
  43. data/lib/rpush/client/active_record/gcm/notification.rb +11 -0
  44. data/lib/rpush/client/active_record/notification.rb +38 -0
  45. data/lib/rpush/client/active_record/wpns/app.rb +11 -0
  46. data/lib/rpush/client/active_record/wpns/notification.rb +11 -0
  47. data/lib/rpush/client/redis.rb +35 -0
  48. data/lib/rpush/client/redis/adm/app.rb +14 -0
  49. data/lib/rpush/client/redis/adm/notification.rb +11 -0
  50. data/lib/rpush/client/redis/apns/app.rb +11 -0
  51. data/lib/rpush/client/redis/apns/feedback.rb +20 -0
  52. data/lib/rpush/client/redis/apns/notification.rb +11 -0
  53. data/lib/rpush/client/redis/app.rb +22 -0
  54. data/lib/rpush/client/redis/gcm/app.rb +11 -0
  55. data/lib/rpush/client/redis/gcm/notification.rb +11 -0
  56. data/lib/rpush/client/redis/notification.rb +68 -0
  57. data/lib/rpush/client/redis/wpns/app.rb +11 -0
  58. data/lib/rpush/client/redis/wpns/notification.rb +11 -0
  59. data/lib/rpush/configuration.rb +27 -6
  60. data/lib/rpush/daemon.rb +36 -56
  61. data/lib/rpush/daemon/adm/delivery.rb +50 -52
  62. data/lib/rpush/daemon/apns.rb +6 -5
  63. data/lib/rpush/daemon/apns/delivery.rb +20 -44
  64. data/lib/rpush/daemon/apns/feedback_receiver.rb +11 -8
  65. data/lib/rpush/daemon/app_runner.rb +67 -60
  66. data/lib/rpush/daemon/batch.rb +54 -40
  67. data/lib/rpush/daemon/delivery.rb +13 -3
  68. data/lib/rpush/daemon/delivery_error.rb +10 -2
  69. data/lib/rpush/daemon/dispatcher/apns_tcp.rb +106 -0
  70. data/lib/rpush/daemon/dispatcher/http.rb +3 -3
  71. data/lib/rpush/daemon/dispatcher/tcp.rb +3 -3
  72. data/lib/rpush/daemon/dispatcher_loop.rb +15 -6
  73. data/lib/rpush/daemon/errors.rb +18 -0
  74. data/lib/rpush/daemon/feeder.rb +28 -39
  75. data/lib/rpush/daemon/gcm/delivery.rb +19 -20
  76. data/lib/rpush/daemon/interruptible_sleep.rb +26 -45
  77. data/lib/rpush/daemon/loggable.rb +2 -4
  78. data/lib/rpush/daemon/proc_title.rb +16 -0
  79. data/lib/rpush/daemon/queue_payload.rb +12 -0
  80. data/lib/rpush/daemon/reflectable.rb +3 -5
  81. data/lib/rpush/daemon/retry_header_parser.rb +6 -6
  82. data/lib/rpush/daemon/ring_buffer.rb +16 -0
  83. data/lib/rpush/daemon/service_config_methods.rb +23 -7
  84. data/lib/rpush/daemon/signal_handler.rb +51 -0
  85. data/lib/rpush/daemon/store/active_record.rb +71 -38
  86. data/lib/rpush/daemon/store/active_record/reconnectable.rb +15 -15
  87. data/lib/rpush/daemon/store/interface.rb +19 -0
  88. data/lib/rpush/daemon/store/redis.rb +149 -0
  89. data/lib/rpush/daemon/tcp_connection.rb +6 -11
  90. data/lib/rpush/daemon/wpns/delivery.rb +21 -30
  91. data/lib/rpush/deprecatable.rb +4 -3
  92. data/lib/rpush/deprecation.rb +7 -10
  93. data/lib/rpush/embed.rb +7 -2
  94. data/lib/rpush/logger.rb +11 -15
  95. data/lib/rpush/push.rb +0 -1
  96. data/lib/rpush/reflection.rb +6 -12
  97. data/lib/rpush/version.rb +1 -1
  98. data/lib/tasks/quality.rake +34 -0
  99. data/spec/.rubocop.yml +4 -0
  100. data/spec/functional/adm_spec.rb +3 -6
  101. data/spec/functional/apns_spec.rb +118 -24
  102. data/spec/functional/embed_spec.rb +22 -20
  103. data/spec/functional/gcm_spec.rb +4 -7
  104. data/spec/functional/new_app_spec.rb +61 -0
  105. data/spec/functional/retry_spec.rb +46 -0
  106. data/spec/functional/wpns_spec.rb +3 -6
  107. data/spec/functional_spec_helper.rb +26 -0
  108. data/spec/integration/rpush_spec.rb +13 -0
  109. data/spec/integration/support/gcm_success_response.json +1 -0
  110. data/spec/spec_helper.rb +60 -0
  111. data/spec/support/active_record_setup.rb +48 -0
  112. data/{config → spec/support/config}/database.yml +0 -0
  113. data/spec/support/install.sh +43 -7
  114. data/spec/support/simplecov_helper.rb +9 -5
  115. data/spec/support/simplecov_quality_formatter.rb +10 -6
  116. data/spec/unit/apns_feedback_spec.rb +3 -3
  117. data/spec/unit/{adm → client/active_record/adm}/app_spec.rb +3 -3
  118. data/spec/unit/{adm → client/active_record/adm}/notification_spec.rb +5 -7
  119. data/spec/unit/client/active_record/apns/app_spec.rb +29 -0
  120. data/spec/unit/client/active_record/apns/feedback_spec.rb +9 -0
  121. data/spec/unit/client/active_record/apns/notification_spec.rb +231 -0
  122. data/spec/unit/client/active_record/app_spec.rb +30 -0
  123. data/spec/unit/client/active_record/gcm/app_spec.rb +4 -0
  124. data/spec/unit/{gcm → client/active_record/gcm}/notification_spec.rb +5 -7
  125. data/spec/unit/client/active_record/notification_spec.rb +15 -0
  126. data/spec/unit/client/active_record/wpns/app_spec.rb +4 -0
  127. data/spec/unit/client/active_record/wpns/notification_spec.rb +21 -0
  128. data/spec/unit/configuration_spec.rb +12 -5
  129. data/spec/unit/daemon/adm/delivery_spec.rb +57 -54
  130. data/spec/unit/daemon/apns/certificate_expired_error_spec.rb +3 -3
  131. data/spec/unit/daemon/apns/delivery_spec.rb +90 -83
  132. data/spec/unit/daemon/apns/feedback_receiver_spec.rb +24 -17
  133. data/spec/unit/daemon/app_runner_spec.rb +66 -123
  134. data/spec/unit/daemon/batch_spec.rb +52 -115
  135. data/spec/unit/daemon/delivery_spec.rb +15 -1
  136. data/spec/unit/daemon/dispatcher/http_spec.rb +3 -2
  137. data/spec/unit/daemon/dispatcher/tcp_spec.rb +10 -9
  138. data/spec/unit/daemon/dispatcher_loop_spec.rb +7 -12
  139. data/spec/unit/daemon/feeder_spec.rb +40 -39
  140. data/spec/unit/daemon/gcm/delivery_spec.rb +108 -89
  141. data/spec/unit/daemon/reflectable_spec.rb +2 -2
  142. data/spec/unit/daemon/retryable_error_spec.rb +1 -1
  143. data/spec/unit/daemon/service_config_methods_spec.rb +6 -3
  144. data/spec/unit/daemon/signal_handler_spec.rb +72 -0
  145. data/spec/unit/daemon/store/active_record/reconnectable_spec.rb +9 -9
  146. data/spec/unit/daemon/store/active_record_spec.rb +38 -47
  147. data/spec/unit/daemon/tcp_connection_spec.rb +22 -34
  148. data/spec/unit/daemon/too_many_requests_error_spec.rb +1 -1
  149. data/spec/unit/daemon/wpns/delivery_spec.rb +61 -50
  150. data/spec/unit/daemon_spec.rb +46 -81
  151. data/spec/unit/embed_spec.rb +4 -2
  152. data/spec/unit/logger_spec.rb +30 -40
  153. data/spec/unit/notification_shared.rb +9 -79
  154. data/spec/unit/push_spec.rb +3 -8
  155. data/spec/unit/reflection_spec.rb +25 -25
  156. data/spec/unit/rpush_spec.rb +1 -2
  157. data/spec/unit_spec_helper.rb +33 -88
  158. metadata +119 -67
  159. data/lib/rpush/TODO +0 -3
  160. data/lib/rpush/adm/app.rb +0 -15
  161. data/lib/rpush/adm/data_validator.rb +0 -11
  162. data/lib/rpush/adm/notification.rb +0 -29
  163. data/lib/rpush/apns/app.rb +0 -29
  164. data/lib/rpush/apns/binary_notification_validator.rb +0 -12
  165. data/lib/rpush/apns/device_token_format_validator.rb +0 -12
  166. data/lib/rpush/apns/feedback.rb +0 -16
  167. data/lib/rpush/apns/notification.rb +0 -84
  168. data/lib/rpush/app.rb +0 -18
  169. data/lib/rpush/daemon/apns/certificate_expired_error.rb +0 -20
  170. data/lib/rpush/daemon/apns/disconnection_error.rb +0 -20
  171. data/lib/rpush/gcm/app.rb +0 -11
  172. data/lib/rpush/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +0 -11
  173. data/lib/rpush/gcm/notification.rb +0 -30
  174. data/lib/rpush/notification.rb +0 -69
  175. data/lib/rpush/notifier.rb +0 -52
  176. data/lib/rpush/payload_data_size_validator.rb +0 -10
  177. data/lib/rpush/railtie.rb +0 -11
  178. data/lib/rpush/registration_ids_count_validator.rb +0 -10
  179. data/lib/rpush/wpns/app.rb +0 -9
  180. data/lib/rpush/wpns/notification.rb +0 -26
  181. data/lib/tasks/cane.rake +0 -18
  182. data/lib/tasks/rpush.rake +0 -16
  183. data/spec/unit/apns/app_spec.rb +0 -29
  184. data/spec/unit/apns/feedback_spec.rb +0 -9
  185. data/spec/unit/apns/notification_spec.rb +0 -208
  186. data/spec/unit/app_spec.rb +0 -30
  187. data/spec/unit/daemon/apns/disconnection_error_spec.rb +0 -18
  188. data/spec/unit/daemon/interruptible_sleep_spec.rb +0 -68
  189. data/spec/unit/gcm/app_spec.rb +0 -4
  190. data/spec/unit/notification_spec.rb +0 -15
  191. data/spec/unit/notifier_spec.rb +0 -49
  192. data/spec/unit/wpns/app_spec.rb +0 -4
  193. data/spec/unit/wpns/notification_spec.rb +0 -30
@@ -1,10 +0,0 @@
1
- module Rpush
2
- class PayloadDataSizeValidator < ActiveModel::Validator
3
- def validate(record)
4
- limit = options[:limit] || 1024
5
- if !record.data.nil? && record.payload_data_size > limit
6
- record.errors[:base] << "Notification payload data cannot be larger than #{limit} bytes."
7
- end
8
- end
9
- end
10
- end
@@ -1,11 +0,0 @@
1
- require 'rails'
2
-
3
- module MyPlugin
4
- class Railtie < Rails::Railtie
5
- railtie_name :rpush
6
-
7
- rake_tasks do
8
- load "tasks/rpush.rake"
9
- end
10
- end
11
- end
@@ -1,10 +0,0 @@
1
- module Rpush
2
- class RegistrationIdsCountValidator < ActiveModel::Validator
3
- def validate(record)
4
- limit = options[:limit] || 100
5
- if record.registration_ids && record.registration_ids.size > limit
6
- record.errors[:base] << "Number of registration_ids cannot be larger than #{limit}."
7
- end
8
- end
9
- end
10
- end
@@ -1,9 +0,0 @@
1
- module Rpush
2
- module Wpns
3
- class App < Rpush::App
4
- def service_name
5
- 'wpns'
6
- end
7
- end
8
- end
9
- end
@@ -1,26 +0,0 @@
1
- module Rpush
2
- module Wpns
3
- class Notification < Rpush::Notification
4
- validates :uri, presence: true
5
- validates :uri, format: { with: /https?:\/\/[\S]+/ }
6
- validates :alert, presence: true
7
-
8
- def as_json
9
- json = {
10
- 'message' => alert,
11
- 'uri' => uri
12
- }
13
-
14
- if collapse_key
15
- json['consolidationKey'] = collapse_key
16
- end
17
-
18
- json
19
- end
20
-
21
- def uri_is_valid?
22
- return (/https?:\/\/[\S]+/.match(uri) != nil)
23
- end
24
- end
25
- end
26
- end
@@ -1,18 +0,0 @@
1
- begin
2
- require 'cane/rake_task'
3
-
4
- desc "Run cane to check quality metrics"
5
- Cane::RakeTask.new(:quality) do |cane|
6
- cane.add_threshold 'coverage/covered_percent', :>=, 98
7
- cane.no_style = false
8
- cane.style_measure = 1000
9
- cane.no_doc = true
10
- cane.abc_max = 20
11
- end
12
-
13
- namespace :spec do
14
- task :cane => ['spec', 'quality']
15
- end
16
- rescue LoadError
17
- warn "cane not available, quality task not provided."
18
- end
@@ -1,16 +0,0 @@
1
- namespace :rpush do
2
- namespace :notifications do
3
- desc "Delete completed notifications older than number of days (DAYS > 0)"
4
- task :clean => :environment do
5
- date = (ENV['DAYS'].blank? || ENV['DAYS'].to_i <= 0) ? nil : Time.zone.now.end_of_day - (ENV['DAYS'].to_i).days
6
-
7
- if(date.nil?)
8
- puts "DAYS is required and must be greater than 0"
9
- else
10
- puts "BEG: Delete completed notifications before '#{date}'\n"
11
- deleted_count = Rpush::Notification.completed_and_older_than(date).delete_all
12
- puts "END: Deleted #{deleted_count} notifications"
13
- end
14
- end
15
- end
16
- end
@@ -1,29 +0,0 @@
1
- require 'unit_spec_helper'
2
-
3
- describe Rpush::App do
4
- it 'does not validate an app with an invalid certificate' do
5
- app = Rpush::Apns::App.new(:name => 'test', :environment => 'development', :certificate => 'foo')
6
- app.valid?
7
- app.errors[:certificate].should eq ['Certificate value must contain a certificate and a private key.']
8
- end
9
-
10
- it 'validates a certificate without a password' do
11
- app = Rpush::Apns::App.new :name => 'test', :environment => 'development', :certificate => TEST_CERT
12
- app.valid?
13
- app.errors[:certificate].should eq []
14
- end
15
-
16
- it 'validates a certificate with a password' do
17
- app = Rpush::Apns::App.new :name => 'test', :environment => 'development',
18
- :certificate => TEST_CERT_WITH_PASSWORD, :password => 'fubar'
19
- app.valid?
20
- app.errors[:certificate].should eq []
21
- end
22
-
23
- it 'validates a certificate with an incorrect password' do
24
- app = Rpush::Apns::App.new :name => 'test', :environment => 'development',
25
- :certificate => TEST_CERT_WITH_PASSWORD, :password => 'incorrect'
26
- app.valid?
27
- app.errors[:certificate].should eq ["Certificate value must contain a certificate and a private key."]
28
- end
29
- end
@@ -1,9 +0,0 @@
1
- require "unit_spec_helper"
2
-
3
- describe Rpush::Apns::Feedback do
4
- it "should validate the format of the device_token" do
5
- notification = Rpush::Apns::Feedback.new(:device_token => "{$%^&*()}")
6
- notification.valid?.should be_false
7
- notification.errors[:device_token].include?("is invalid").should be_true
8
- end
9
- end
@@ -1,208 +0,0 @@
1
- # encoding: US-ASCII
2
-
3
- require "unit_spec_helper"
4
- require 'unit/notification_shared.rb'
5
-
6
- describe Rpush::Apns::Notification do
7
- it_should_behave_like 'an Notification subclass'
8
-
9
- let(:app) { Rpush::Apns::App.create!(:name => 'my_app', :environment => 'development', :certificate => TEST_CERT) }
10
- let(:notification_class) { Rpush::Apns::Notification }
11
- let(:notification) { notification_class.new }
12
- let(:data_setter) { 'attributes_for_device=' }
13
- let(:data_getter) { 'attributes_for_device' }
14
-
15
- it "should validate the format of the device_token" do
16
- notification = Rpush::Apns::Notification.new(:device_token => "{$%^&*()}")
17
- notification.valid?.should be_false
18
- notification.errors[:device_token].include?("is invalid").should be_true
19
- end
20
-
21
- it "should validate the length of the binary conversion of the notification" do
22
- notification.device_token = "a" * 64
23
- notification.alert = "way too long!" * 100
24
- notification.valid?.should be_false
25
- notification.errors[:base].include?("APN notification cannot be larger than 256 bytes. Try condensing your alert and device attributes.").should be_true
26
- end
27
-
28
- it "should default the sound to 'default'" do
29
- notification.sound.should eq('default')
30
- end
31
-
32
- it "should default the expiry to 1 day" do
33
- notification.expiry.should eq 1.day.to_i
34
- end
35
- end
36
-
37
- describe Rpush::Apns::Notification, "when assigning the device token" do
38
- it "should strip spaces from the given string" do
39
- notification = Rpush::Apns::Notification.new(:device_token => "o m g")
40
- notification.device_token.should eq "omg"
41
- end
42
-
43
- it "should strip chevrons from the given string" do
44
- notification = Rpush::Apns::Notification.new(:device_token => "<omg>")
45
- notification.device_token.should eq "omg"
46
- end
47
- end
48
-
49
- describe Rpush::Apns::Notification, "as_json" do
50
- it "should include the alert if present" do
51
- notification = Rpush::Apns::Notification.new(:alert => "hi mom")
52
- notification.as_json["aps"]["alert"].should eq "hi mom"
53
- end
54
-
55
- it "should not include the alert key if the alert is not present" do
56
- notification = Rpush::Apns::Notification.new(:alert => nil)
57
- notification.as_json["aps"].key?("alert").should be_false
58
- end
59
-
60
- it "should encode the alert as JSON if it is a Hash" do
61
- notification = Rpush::Apns::Notification.new(:alert => { 'body' => "hi mom", 'alert-loc-key' => "View" })
62
- notification.as_json["aps"]["alert"].should eq ({ 'body' => "hi mom", 'alert-loc-key' => "View" })
63
- end
64
-
65
- it "should include the badge if present" do
66
- notification = Rpush::Apns::Notification.new(:badge => 6)
67
- notification.as_json["aps"]["badge"].should eq 6
68
- end
69
-
70
- it "should not include the badge key if the badge is not present" do
71
- notification = Rpush::Apns::Notification.new(:badge => nil)
72
- notification.as_json["aps"].key?("badge").should be_false
73
- end
74
-
75
- it "should include the sound if present" do
76
- notification = Rpush::Apns::Notification.new(:alert => "my_sound.aiff")
77
- notification.as_json["aps"]["alert"].should eq "my_sound.aiff"
78
- end
79
-
80
- it "should not include the sound key if the sound is not present" do
81
- notification = Rpush::Apns::Notification.new(:sound => false)
82
- notification.as_json["aps"].key?("sound").should be_false
83
- end
84
-
85
- it "should include attributes for the device" do
86
- notification = Rpush::Apns::Notification.new
87
- notification.attributes_for_device = {:omg => :lol, :wtf => :dunno}
88
- notification.as_json["omg"].should eq "lol"
89
- notification.as_json["wtf"].should eq "dunno"
90
- end
91
-
92
- it "should allow attributes to include a hash" do
93
- notification = Rpush::Apns::Notification.new
94
- notification.attributes_for_device = {:omg => {:ilike => :hashes}}
95
- notification.as_json["omg"]["ilike"].should eq "hashes"
96
- end
97
-
98
- end
99
-
100
- describe Rpush::Apns::Notification, 'MDM' do
101
- let(:magic) { 'abc123' }
102
- let(:notification) { Rpush::Apns::Notification.new }
103
-
104
- it 'includes the mdm magic in the payload' do
105
- notification.mdm = magic
106
- notification.as_json.should eq ({'mdm' => magic})
107
- end
108
-
109
- it 'does not include aps attribute' do
110
- notification.alert = "i'm doomed"
111
- notification.mdm = magic
112
- notification.as_json.key?('aps').should be_false
113
- end
114
- end
115
-
116
- describe Rpush::Apns::Notification, 'content-available' do
117
- let(:notification) { Rpush::Apns::Notification.new }
118
-
119
- it 'includes content-available in the payload' do
120
- notification.content_available = true
121
- notification.as_json['aps']['content-available'].should eq 1
122
- end
123
-
124
- it 'does not include content-available in the payload if not set' do
125
- notification.as_json['aps'].key?('content-available').should be_false
126
- end
127
-
128
- it 'does not include content-available as a non-aps attribute' do
129
- notification.content_available = true
130
- notification.as_json.key?('content-available').should be_false
131
- end
132
-
133
- it 'does not overwrite existing attributes for the device' do
134
- notification.data = {:hi => :mom}
135
- notification.content_available = true
136
- notification.as_json['aps']['content-available'].should eq 1
137
- notification.as_json['hi'].should eq 'mom'
138
- end
139
-
140
- it 'does not overwrite the content-available flag when setting attributes for the device' do
141
- notification.content_available = true
142
- notification.data = {:hi => :mom}
143
- notification.as_json['aps']['content-available'].should eq 1
144
- notification.as_json['hi'].should eq 'mom'
145
- end
146
- end
147
-
148
- describe Rpush::Apns::Notification, "to_binary" do
149
- it "should correctly convert the notification to binary" do
150
- notification = Rpush::Apns::Notification.new
151
- notification.device_token = "a" * 64
152
- notification.sound = "1.aiff"
153
- notification.badge = 3
154
- notification.alert = "Don't panic Mr Mainwaring, don't panic!"
155
- notification.attributes_for_device = {:hi => :mom}
156
- notification.expiry = 86400 # 1 day, \x00\x01Q\x80
157
- notification.app = Rpush::Apns::App.new(:name => 'my_app', :environment => 'development', :certificate => TEST_CERT)
158
- notification.stub(:id).and_return(1234)
159
- notification.to_binary.should eq "\x01\x00\x00\x04\xD2\x00\x01Q\x80\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\x00a{\"aps\":{\"alert\":\"Don't panic Mr Mainwaring, don't panic!\",\"badge\":3,\"sound\":\"1.aiff\"},\"hi\":\"mom\"}"
160
- end
161
- end
162
-
163
- describe Rpush::Apns::Notification, "bug #31" do
164
- it 'does not confuse a JSON looking string as JSON' do
165
- notification = Rpush::Apns::Notification.new
166
- notification.alert = "{\"one\":2}"
167
- notification.alert.should eq "{\"one\":2}"
168
- end
169
-
170
- it 'does confuse a JSON looking string as JSON if the alert_is_json attribute is not present' do
171
- notification = Rpush::Apns::Notification.new
172
- notification.stub(:has_attribute? => false)
173
- notification.alert = "{\"one\":2}"
174
- notification.alert.should eq ({"one" => 2})
175
- end
176
- end
177
-
178
- describe Rpush::Apns::Notification, "bug #35" do
179
- it "should limit payload size to 256 bytes but not the entire packet" do
180
- notification = Rpush::Apns::Notification.new do |n|
181
- n.device_token = "a" * 64
182
- n.alert = "a" * 210
183
- n.app = Rpush::Apns::App.create!(:name => 'my_app', :environment => 'development', :certificate => TEST_CERT)
184
- end
185
-
186
- notification.to_binary(:for_validation => true).bytesize.should > 256
187
- notification.payload_size.should < 256
188
- notification.should be_valid
189
- end
190
- end
191
-
192
- describe Rpush::Apns::Notification, "multi_json usage" do
193
- describe Rpush::Apns::Notification, "alert" do
194
- it "should call MultiJson.load when multi_json version is 1.3.0" do
195
- notification = Rpush::Apns::Notification.new(:alert => { :a => 1 }, :alert_is_json => true)
196
- Gem.stub(:loaded_specs).and_return( { 'multi_json' => Gem::Specification.new('multi_json', '1.3.0') } )
197
- MultiJson.should_receive(:load).with(any_args())
198
- notification.alert
199
- end
200
-
201
- it "should call MultiJson.decode when multi_json version is 1.2.9" do
202
- notification = Rpush::Apns::Notification.new(:alert => { :a => 1 }, :alert_is_json => true)
203
- Gem.stub(:loaded_specs).and_return( { 'multi_json' => Gem::Specification.new('multi_json', '1.2.9') } )
204
- MultiJson.should_receive(:decode).with(any_args())
205
- notification.alert
206
- end
207
- end
208
- end
@@ -1,30 +0,0 @@
1
- require 'unit_spec_helper'
2
-
3
- describe Rpush::App do
4
- it 'validates the uniqueness of name within type and environment' do
5
- Rpush::Apns::App.create!(:name => 'test', :environment => 'production', :certificate => TEST_CERT)
6
- app = Rpush::Apns::App.new(:name => 'test', :environment => 'production', :certificate => TEST_CERT)
7
- app.valid?.should be_false
8
- app.errors[:name].should eq ['has already been taken']
9
-
10
- app = Rpush::Apns::App.new(:name => 'test', :environment => 'development', :certificate => TEST_CERT)
11
- app.valid?.should be_true
12
-
13
- app = Rpush::Gcm::App.new(:name => 'test', :environment => 'production', :auth_key => TEST_CERT)
14
- app.valid?.should be_true
15
- end
16
-
17
- context 'validating certificates' do
18
- it 'rescues from certificate error' do
19
- app = Rpush::Apns::App.new(:name => 'test', :environment => 'development', :certificate => 'bad')
20
- expect{app.valid?}.not_to raise_error
21
- expect(app.valid?).to be_false
22
- end
23
-
24
- it 'raises other errors' do
25
- app = Rpush::Apns::App.new(:name => 'test', :environment => 'development', :certificate => 'bad')
26
- OpenSSL::X509::Certificate.stub(:new).and_raise(NameError, 'simulating no openssl')
27
- expect{app.valid?}.to raise_error(NameError)
28
- end
29
- end
30
- end
@@ -1,18 +0,0 @@
1
- require 'unit_spec_helper'
2
-
3
- describe Rpush::Apns::DisconnectionError do
4
- let(:error) { Rpush::Apns::DisconnectionError.new }
5
-
6
- it 'returns a nil error code' do
7
- error.code.should be_nil
8
- end
9
-
10
- it 'contains an error description' do
11
- error.description
12
- end
13
-
14
- it 'returns a message' do
15
- error.message
16
- error.to_s
17
- end
18
- end
@@ -1,68 +0,0 @@
1
- require "unit_spec_helper"
2
-
3
- describe Rpush::Daemon::InterruptibleSleep do
4
-
5
- let(:rd) { double(:close => nil) }
6
- let(:wr) { double(:close => nil) }
7
-
8
- subject { Rpush::Daemon::InterruptibleSleep.new }
9
-
10
- it 'creates a new pipe' do
11
- IO.should_receive(:pipe)
12
- subject
13
- end
14
-
15
- it 'selects on the reader' do
16
- IO.stub(:pipe => [rd, wr])
17
- IO.should_receive(:select).with([rd], nil, nil, 1)
18
- subject.sleep(1)
19
- end
20
-
21
- it 'closes the writer' do
22
- IO.stub(:pipe => [rd, wr])
23
- rd.should_receive(:close)
24
- wr.should_receive(:close)
25
- subject.close
26
- end
27
-
28
- it 'returns false when timeout occurs' do
29
- expect(subject.sleep(0.01)).to be_false
30
- end
31
-
32
- it 'returns true when sleep does not timeout' do
33
- subject.interrupt_sleep
34
- expect(subject.sleep(0.01)).to be_true
35
- end
36
-
37
- context 'with UDP socket connected' do
38
- before :each do
39
- @host, @port = subject.enable_wake_on_udp('127.0.0.1', 0)
40
- end
41
-
42
- it 'times out with no udp activity' do
43
- expect(subject.sleep(0.01)).to be_false
44
- end
45
-
46
- unless defined? JRUBY_VERSION
47
- it 'wakes on UDPSocket' do
48
- waker = UDPSocket.new
49
- waker.connect(@host, @port)
50
- waker.write('x')
51
- expect(subject.sleep(0.01)).to be_true
52
- end
53
-
54
- it 'consumes all data on udp socket' do
55
- waker = UDPSocket.new
56
- waker.connect(@host, @port)
57
- waker.send('x', 0)
58
- waker.send('x', 0)
59
- waker.send('x', 0)
60
- # true since there is data to be read => no timeout
61
- expect(subject.sleep(0.01)).to be_true
62
- # false since data is consumed => wait for full timeout
63
- expect(subject.sleep(0.01)).to be_false
64
- end
65
- end
66
- end
67
-
68
- end