rapns_rails_2 3.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +15 -0
  2. data/CHANGELOG.md +83 -0
  3. data/LICENSE +7 -0
  4. data/README.md +168 -0
  5. data/bin/rapns +37 -0
  6. data/config/database.yml +44 -0
  7. data/lib/generators/rapns_generator.rb +25 -0
  8. data/lib/generators/templates/add_alert_is_json_to_rapns_notifications.rb +9 -0
  9. data/lib/generators/templates/add_app_to_rapns.rb +11 -0
  10. data/lib/generators/templates/add_gcm.rb +95 -0
  11. data/lib/generators/templates/create_rapns_apps.rb +16 -0
  12. data/lib/generators/templates/create_rapns_feedback.rb +15 -0
  13. data/lib/generators/templates/create_rapns_notifications.rb +26 -0
  14. data/lib/generators/templates/rapns.rb +87 -0
  15. data/lib/rapns/TODO +3 -0
  16. data/lib/rapns/apns/app.rb +25 -0
  17. data/lib/rapns/apns/binary_notification_validator.rb +12 -0
  18. data/lib/rapns/apns/device_token_format_validator.rb +12 -0
  19. data/lib/rapns/apns/feedback.rb +16 -0
  20. data/lib/rapns/apns/notification.rb +91 -0
  21. data/lib/rapns/apns_feedback.rb +13 -0
  22. data/lib/rapns/app.rb +16 -0
  23. data/lib/rapns/configuration.rb +89 -0
  24. data/lib/rapns/daemon/apns/app_runner.rb +26 -0
  25. data/lib/rapns/daemon/apns/certificate_expired_error.rb +20 -0
  26. data/lib/rapns/daemon/apns/connection.rb +142 -0
  27. data/lib/rapns/daemon/apns/delivery.rb +64 -0
  28. data/lib/rapns/daemon/apns/delivery_handler.rb +35 -0
  29. data/lib/rapns/daemon/apns/disconnection_error.rb +20 -0
  30. data/lib/rapns/daemon/apns/feedback_receiver.rb +89 -0
  31. data/lib/rapns/daemon/app_runner.rb +179 -0
  32. data/lib/rapns/daemon/batch.rb +112 -0
  33. data/lib/rapns/daemon/delivery.rb +23 -0
  34. data/lib/rapns/daemon/delivery_error.rb +19 -0
  35. data/lib/rapns/daemon/delivery_handler.rb +52 -0
  36. data/lib/rapns/daemon/delivery_handler_collection.rb +33 -0
  37. data/lib/rapns/daemon/feeder.rb +65 -0
  38. data/lib/rapns/daemon/gcm/app_runner.rb +13 -0
  39. data/lib/rapns/daemon/gcm/delivery.rb +228 -0
  40. data/lib/rapns/daemon/gcm/delivery_handler.rb +20 -0
  41. data/lib/rapns/daemon/interruptible_sleep.rb +65 -0
  42. data/lib/rapns/daemon/reflectable.rb +13 -0
  43. data/lib/rapns/daemon/store/active_record/reconnectable.rb +66 -0
  44. data/lib/rapns/daemon/store/active_record.rb +128 -0
  45. data/lib/rapns/daemon.rb +129 -0
  46. data/lib/rapns/deprecatable.rb +23 -0
  47. data/lib/rapns/deprecation.rb +23 -0
  48. data/lib/rapns/embed.rb +28 -0
  49. data/lib/rapns/gcm/app.rb +7 -0
  50. data/lib/rapns/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +11 -0
  51. data/lib/rapns/gcm/notification.rb +37 -0
  52. data/lib/rapns/gcm/payload_data_size_validator.rb +13 -0
  53. data/lib/rapns/gcm/registration_ids_count_validator.rb +13 -0
  54. data/lib/rapns/logger.rb +76 -0
  55. data/lib/rapns/multi_json_helper.rb +16 -0
  56. data/lib/rapns/notification.rb +62 -0
  57. data/lib/rapns/notifier.rb +35 -0
  58. data/lib/rapns/push.rb +17 -0
  59. data/lib/rapns/rails-2-compatibility.rb +34 -0
  60. data/lib/rapns/reflection.rb +44 -0
  61. data/lib/rapns/upgraded.rb +31 -0
  62. data/lib/rapns/version.rb +3 -0
  63. data/lib/rapns_rails_2.rb +67 -0
  64. data/lib/tasks/cane.rake +18 -0
  65. data/lib/tasks/test.rake +38 -0
  66. data/spec/support/cert_with_password.pem +90 -0
  67. data/spec/support/cert_without_password.pem +59 -0
  68. data/spec/support/simplecov_helper.rb +13 -0
  69. data/spec/support/simplecov_quality_formatter.rb +8 -0
  70. data/spec/tmp/.gitkeep +0 -0
  71. data/spec/unit/apns/app_spec.rb +29 -0
  72. data/spec/unit/apns/feedback_spec.rb +9 -0
  73. data/spec/unit/apns/notification_spec.rb +215 -0
  74. data/spec/unit/apns_feedback_spec.rb +21 -0
  75. data/spec/unit/app_spec.rb +16 -0
  76. data/spec/unit/configuration_spec.rb +55 -0
  77. data/spec/unit/daemon/apns/app_runner_spec.rb +45 -0
  78. data/spec/unit/daemon/apns/certificate_expired_error_spec.rb +11 -0
  79. data/spec/unit/daemon/apns/connection_spec.rb +287 -0
  80. data/spec/unit/daemon/apns/delivery_handler_spec.rb +59 -0
  81. data/spec/unit/daemon/apns/delivery_spec.rb +101 -0
  82. data/spec/unit/daemon/apns/disconnection_error_spec.rb +18 -0
  83. data/spec/unit/daemon/apns/feedback_receiver_spec.rb +134 -0
  84. data/spec/unit/daemon/app_runner_shared.rb +83 -0
  85. data/spec/unit/daemon/app_runner_spec.rb +170 -0
  86. data/spec/unit/daemon/batch_spec.rb +219 -0
  87. data/spec/unit/daemon/delivery_error_spec.rb +13 -0
  88. data/spec/unit/daemon/delivery_handler_collection_spec.rb +37 -0
  89. data/spec/unit/daemon/delivery_handler_shared.rb +45 -0
  90. data/spec/unit/daemon/feeder_spec.rb +81 -0
  91. data/spec/unit/daemon/gcm/app_runner_spec.rb +19 -0
  92. data/spec/unit/daemon/gcm/delivery_handler_spec.rb +44 -0
  93. data/spec/unit/daemon/gcm/delivery_spec.rb +289 -0
  94. data/spec/unit/daemon/interruptible_sleep_spec.rb +68 -0
  95. data/spec/unit/daemon/reflectable_spec.rb +27 -0
  96. data/spec/unit/daemon/store/active_record/reconnectable_spec.rb +114 -0
  97. data/spec/unit/daemon/store/active_record_spec.rb +281 -0
  98. data/spec/unit/daemon_spec.rb +157 -0
  99. data/spec/unit/deprecatable_spec.rb +32 -0
  100. data/spec/unit/deprecation_spec.rb +15 -0
  101. data/spec/unit/embed_spec.rb +50 -0
  102. data/spec/unit/gcm/app_spec.rb +4 -0
  103. data/spec/unit/gcm/notification_spec.rb +52 -0
  104. data/spec/unit/logger_spec.rb +180 -0
  105. data/spec/unit/notification_shared.rb +45 -0
  106. data/spec/unit/notification_spec.rb +4 -0
  107. data/spec/unit/notifier_spec.rb +32 -0
  108. data/spec/unit/push_spec.rb +44 -0
  109. data/spec/unit/rapns_spec.rb +9 -0
  110. data/spec/unit/reflection_spec.rb +30 -0
  111. data/spec/unit/upgraded_spec.rb +40 -0
  112. data/spec/unit_spec_helper.rb +137 -0
  113. metadata +232 -0
@@ -0,0 +1,180 @@
1
+ require File.expand_path("spec/unit_spec_helper")
2
+
3
+ module Rails
4
+ def self.logger
5
+ @logger
6
+ end
7
+
8
+ def self.logger=(logger)
9
+ @logger = logger
10
+ end
11
+ end
12
+
13
+ module HoptoadNotifier
14
+ def self.notify(e)
15
+ end
16
+ end
17
+
18
+ module Airbrake
19
+ def self.notify_or_ignore(e)
20
+ end
21
+ end
22
+
23
+ describe Rapns::Logger do
24
+ let(:log) { double(:sync= => true) }
25
+ let(:config) { double(:airbrake_notify => true) }
26
+
27
+ before do
28
+ Rails.stub(:root).and_return("/rails_root")
29
+
30
+ @logger_class = if defined?(ActiveSupport::BufferedLogger)
31
+ ActiveSupport::BufferedLogger
32
+ else
33
+ ActiveSupport::Logger
34
+ end
35
+
36
+ @logger = double(@logger_class.name, :info => nil, :error => nil, :level => 0, :auto_flushing => 1, :auto_flushing= => nil)
37
+ @logger_class.stub(:new).and_return(@logger)
38
+ Rails.logger = @logger
39
+ File.stub(:open => log)
40
+ STDERR.stub(:puts)
41
+ end
42
+
43
+ it "disables logging if the log file cannot be opened" do
44
+ File.stub(:open).and_raise(Errno::ENOENT)
45
+ STDERR.should_receive(:puts).with(/No such file or directory/)
46
+ STDERR.should_receive(:puts).with(/Logging disabled/)
47
+ Rapns::Logger.new(:foreground => true)
48
+ end
49
+
50
+ it "should open the a log file in the Rails log directory" do
51
+ File.should_receive(:open).with('/rails_root/log/rapns.log', 'a')
52
+ Rapns::Logger.new(:foreground => true)
53
+ end
54
+
55
+ it 'sets sync mode on the log descriptor' do
56
+ log.should_receive(:sync=).with(true)
57
+ Rapns::Logger.new(:foreground => true)
58
+ end
59
+
60
+ it 'uses the user-defined logger' do
61
+ my_logger = double
62
+ Rapns.config.logger = my_logger
63
+ logger = Rapns::Logger.new({})
64
+ my_logger.should_receive(:info)
65
+ logger.info('test')
66
+ end
67
+
68
+ it 'uses ActiveSupport::BufferedLogger if a user-defined logger is not set' do
69
+ if ActiveSupport.const_defined?('BufferedLogger')
70
+ ActiveSupport::BufferedLogger.should_receive(:new).with(log, Rails.logger.level)
71
+ Rapns::Logger.new(:foreground => true)
72
+ end
73
+ end
74
+
75
+ it 'uses ActiveSupport::Logger if BufferedLogger does not exist' do
76
+ ActiveSupport.stub_constants(:Logger => double) do
77
+ ActiveSupport.stub(:const_defined? => false)
78
+ ActiveSupport::Logger.should_receive(:new).with(log, Rails.logger.level)
79
+ Rapns::Logger.new(:foreground => true)
80
+ end
81
+ end
82
+
83
+ it "should print out the msg if running in the foreground" do
84
+ logger = Rapns::Logger.new(:foreground => true)
85
+ STDOUT.should_receive(:puts).with(/hi mom/)
86
+ logger.info("hi mom")
87
+ end
88
+
89
+ it "should not print out the msg if not running in the foreground" do
90
+ logger = Rapns::Logger.new(:foreground => false)
91
+ STDOUT.should_not_receive(:puts).with(/hi mom/)
92
+ logger.info("hi mom")
93
+ end
94
+
95
+ it "should prefix log lines with the current time" do
96
+ now = Time.now
97
+ Time.stub(:now).and_return(now)
98
+ logger = Rapns::Logger.new(:foreground => false)
99
+ @logger.should_receive(:info).with(/#{Regexp.escape("[#{now.to_s(:db)}]")}/)
100
+ logger.info("blah")
101
+ end
102
+
103
+ it "should prefix error logs with the ERROR label" do
104
+ logger = Rapns::Logger.new(:foreground => false)
105
+ @logger.should_receive(:error).with(/#{Regexp.escape("[ERROR]")}/)
106
+ logger.error("eeek")
107
+ end
108
+
109
+ it "should prefix warn logs with the WARNING label" do
110
+ logger = Rapns::Logger.new(:foreground => false)
111
+ @logger.should_receive(:warn).with(/#{Regexp.escape("[WARNING]")}/)
112
+ logger.warn("eeek")
113
+ end
114
+
115
+ it "should handle an Exception instance" do
116
+ e = RuntimeError.new("hi mom")
117
+ e.stub(:backtrace => [])
118
+ logger = Rapns::Logger.new(:foreground => false)
119
+ @logger.should_receive(:error).with(/RuntimeError, hi mom/)
120
+ logger.error(e)
121
+ end
122
+
123
+ it "should notify Airbrake of the exception" do
124
+ e = RuntimeError.new("hi mom")
125
+ e.stub(:backtrace => [])
126
+ logger = Rapns::Logger.new(:foreground => false, :airbrake_notify => true)
127
+ Airbrake.should_receive(:notify_or_ignore).with(e)
128
+ logger.error(e)
129
+ end
130
+
131
+ context "without Airbrake defined" do
132
+ before do
133
+ Object.send(:remove_const, :Airbrake)
134
+ end
135
+
136
+ after do
137
+ module Airbrake
138
+ def self.notify_or_ignore(e)
139
+ end
140
+ end
141
+ end
142
+
143
+ it "should notify using HoptoadNotifier" do
144
+ e = RuntimeError.new("hi mom")
145
+ e.stub(:backtrace => [])
146
+ logger = Rapns::Logger.new(:foreground => false, :airbrake_notify => true)
147
+ HoptoadNotifier.should_receive(:notify_or_ignore).with(e)
148
+ logger.error(e)
149
+ end
150
+ end
151
+
152
+ it "should not notify Airbrake of the exception if the airbrake_notify option is false" do
153
+ e = RuntimeError.new("hi mom")
154
+ e.stub(:backtrace => [])
155
+ logger = Rapns::Logger.new(:foreground => false, :airbrake_notify => false)
156
+ Airbrake.should_not_receive(:notify_or_ignore).with(e)
157
+ logger.error(e)
158
+ end
159
+
160
+ it "should not notify Airbrake if explicitly disabled in the call to error" do
161
+ e = RuntimeError.new("hi mom")
162
+ e.stub(:backtrace => [])
163
+ logger = Rapns::Logger.new(:foreground => false, :airbrake_notify => true)
164
+ Airbrake.should_not_receive(:notify_or_ignore).with(e)
165
+ logger.error(e, :airbrake_notify => false)
166
+ end
167
+
168
+ it "should not attempt to notify Airbrake of the error is not an Exception" do
169
+ logger = Rapns::Logger.new(:foreground => false)
170
+ Airbrake.should_not_receive(:notify_or_ignore)
171
+ logger.error("string error message")
172
+ end
173
+
174
+ it 'defaults auto_flushing to true if the Rails logger does not respond to auto_flushing' do
175
+ rails_logger = double(:info => nil, :error => nil, :level => 0)
176
+ Rails.logger = rails_logger
177
+ logger = Rapns::Logger.new({})
178
+ @logger.auto_flushing.should be_true
179
+ end
180
+ end
@@ -0,0 +1,45 @@
1
+ shared_examples_for "an Notification subclass" do
2
+ describe "when assigning data for the device" do
3
+ before { Rapns::Deprecation.stub(:warn) }
4
+
5
+ it "calls MultiJson.dump when multi_json responds to :dump" do
6
+ Object.stub_constants(:MultiJson => mock) do
7
+ MultiJson.should_receive(:dump).with(any_args())
8
+ notification = notification_class.new
9
+ notification.send(data_setter, { :pirates => 1 })
10
+ end
11
+ end
12
+
13
+ it "calls MultiJson.encode when multi_json does not respond to :dump" do
14
+ Object.stub_constants(:MultiJson => mock) do
15
+ notification = notification_class.new
16
+ MultiJson.should_receive(:encode).with(any_args())
17
+ notification.send(data_setter, { :ninjas => 1 })
18
+ end
19
+ end
20
+
21
+ it "raises an ArgumentError if something other than a Hash is assigned" do
22
+ expect do
23
+ notification.send(data_setter, Array.new)
24
+ end.to raise_error(ArgumentError, "must be a Hash")
25
+ end
26
+
27
+ it "encodes the given Hash as JSON" do
28
+ notification.send(data_setter, { :hi => "mom" })
29
+ notification.read_attribute(:data).should == "{\"hi\":\"mom\"}"
30
+ end
31
+
32
+ it "decodes the JSON when using the reader method" do
33
+ notification.send(data_setter, { :hi => "mom" })
34
+ notification.send(data_getter).should == {"hi" => "mom"}
35
+ end
36
+
37
+ if Rapns.is_rails2_or_3?
38
+ it 'warns if attributes_for_device is assigned via mass-assignment' do
39
+ RAILS_DEFAULT_LOGGER.stub!(:debug)
40
+ Rapns::Deprecation.should_receive(:warn).with(':attributes_for_device via mass-assignment is deprecated. Use :data or the attributes_for_device= instance method.')
41
+ notification_class.new(:attributes_for_device => {:hi => 'mom'})
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,4 @@
1
+ require File.expand_path("spec/unit_spec_helper")
2
+
3
+ describe Rapns::Notification do
4
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path("spec/unit_spec_helper")
2
+ require 'rapns/notifier'
3
+
4
+ describe Rapns::Notifier do
5
+
6
+ before(:each) { @port = 5000 }
7
+ subject { Rapns::Notifier.new('127.0.0.1', @port) }
8
+ its(:socket) { should_not be_nil }
9
+
10
+ context "when connected" do
11
+ before :each do
12
+ @reader = UDPSocket.new
13
+ @reader.bind('127.0.0.1', 0)
14
+ @port = @reader.addr[1]
15
+ end
16
+
17
+ describe "notify" do
18
+ it "calls write on the socket" do
19
+ sock = UDPSocket.new
20
+ UDPSocket.should_receive(:new).and_return(sock)
21
+ sock.should_receive(:write)
22
+
23
+ subject.notify
24
+ end
25
+
26
+ it "writes data that can be read from socket" do
27
+ subject.notify
28
+ @reader.recvfrom(4096).should be_an(Array)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,44 @@
1
+ require File.expand_path("spec/unit_spec_helper")
2
+
3
+ describe Rapns, 'push' do
4
+ before do
5
+ Rapns::Upgraded.stub(:check => nil)
6
+ Rapns::Daemon::AppRunner.stub(:sync => nil, :wait => nil)
7
+ Rapns::Daemon::Feeder.stub(:start => nil)
8
+ end
9
+
10
+ it 'sets the push config option to true' do
11
+ Rapns.push
12
+ Rapns.config.push.should be_true
13
+ end
14
+
15
+ it 'initializes the store' do
16
+ Rapns::Daemon.should_receive(:initialize_store)
17
+ Rapns.push
18
+ end
19
+
20
+ it 'syncs the app runner' do
21
+ Rapns::Daemon::AppRunner.should_receive(:sync)
22
+ Rapns.push
23
+ end
24
+
25
+ it 'starts the feeder' do
26
+ Rapns::Daemon::Feeder.should_receive(:start)
27
+ Rapns.push
28
+ end
29
+
30
+ it 'waits on the app runner' do
31
+ Rapns::Daemon::AppRunner.should_receive(:wait)
32
+ Rapns.push
33
+ end
34
+
35
+ it 'stops on the app runner' do
36
+ Rapns::Daemon::AppRunner.should_receive(:stop)
37
+ Rapns.push
38
+ end
39
+
40
+ it 'overrides the default config options with those given as a hash' do
41
+ Rapns.config.batch_size = 20
42
+ expect { Rapns.push(:batch_size => 10) }.to change(Rapns.config, :batch_size).to(10)
43
+ end
44
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path("spec/unit_spec_helper")
2
+
3
+ describe Rapns do
4
+ it "lazy initializes the logger" do
5
+ Rapns.config.stub(:airbrake_notify => true, :foreground => true)
6
+ Rapns::Logger.should_receive(:new).with(:foreground => true, :airbrake_notify => true)
7
+ Rapns.logger
8
+ end
9
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path("spec/unit_spec_helper")
2
+
3
+ describe Rapns do
4
+ it 'yields reflections for configuration' do
5
+ did_yield = false
6
+ Rapns.reflect { |on| did_yield = true }
7
+ did_yield.should be_true
8
+ end
9
+
10
+ it 'returns all reflections' do
11
+ Rapns.reflections.should be_kind_of(Rapns::Reflections)
12
+ end
13
+ end
14
+
15
+ describe Rapns::Reflections do
16
+ it 'dispatches the given reflection' do
17
+ did_yield = false
18
+ Rapns.reflect do |on|
19
+ on.error { did_yield = true }
20
+ end
21
+ Rapns.reflections.__dispatch(:error)
22
+ did_yield.should be_true
23
+ end
24
+
25
+ it 'raises an error when trying to dispatch and unknown reflection' do
26
+ expect do
27
+ Rapns.reflections.__dispatch(:unknown)
28
+ end.to raise_error(Rapns::Reflections::NoSuchReflectionError)
29
+ end
30
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path("spec/unit_spec_helper")
2
+
3
+ describe Rapns::Upgraded do
4
+ let(:logger) { double(:logger, :warn => nil) }
5
+ let(:config) { double(:config) }
6
+
7
+ before do
8
+ Rails.stub(:root).and_return('/rails_root')
9
+ Rapns.stub(:logger => logger, :config => config)
10
+ end
11
+
12
+ it 'prints a warning and exists if rapns has not been upgraded' do
13
+ Rapns::App.stub(:count).and_raise(ActiveRecord::StatementInvalid)
14
+ Rapns::Upgraded.stub(:puts)
15
+ Rapns::Upgraded.should_receive(:exit).with(1)
16
+ Rapns::Upgraded.check(:exit => true)
17
+ end
18
+
19
+ it 'does not exit if Rapns has not been upgraded and :exit is false' do
20
+ Rapns.config.stub(:embedded => true)
21
+ Rapns::App.stub(:count).and_raise(ActiveRecord::StatementInvalid)
22
+ Rapns::Upgraded.stub(:puts)
23
+ Rapns::Upgraded.should_not_receive(:exit)
24
+ Rapns::Upgraded.check(:exit => false)
25
+ end
26
+
27
+ it 'does not exit if Rapns has not been upgraded and is in push mode' do
28
+ Rapns.config.stub(:push => true)
29
+ Rapns::App.stub(:count).and_raise(ActiveRecord::StatementInvalid)
30
+ Rapns::Upgraded.stub(:puts)
31
+ Rapns::Upgraded.should_not_receive(:exit)
32
+ Rapns::Upgraded.check(:exit => false)
33
+ end
34
+
35
+ it 'warns if rapns.yml still exists' do
36
+ File.should_receive(:exists?).with('/rails_root/config/rapns/rapns.yml').and_return(true)
37
+ Rapns.logger.should_receive(:warn).with("Since 2.0.0 rapns uses command-line options and a Ruby based configuration file.\nPlease run 'rails g rapns' to generate a new configuration file into config/initializers.\nRemove config/rapns/rapns.yml to avoid this warning.\n")
38
+ Rapns::Upgraded.check(:exit => false)
39
+ end
40
+ end
@@ -0,0 +1,137 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+
3
+ require 'bundler'
4
+ Bundler.require(:default)
5
+
6
+ require 'active_record'
7
+ require 'database_cleaner'
8
+
9
+ unless ENV['TRAVIS'] && ENV['QUALITY'] == 'false'
10
+ begin
11
+ require './spec/support/simplecov_helper'
12
+ include SimpleCovHelper
13
+ start_simple_cov("unit-#{RUBY_VERSION}")
14
+ rescue LoadError
15
+ puts "Coverage disabled."
16
+ end
17
+ end
18
+
19
+ jruby = defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
20
+
21
+ $adapter = ENV['ADAPTER'] || 'postgresql'
22
+ $adapter = 'jdbc' + $adapter if jruby
23
+
24
+ DATABASE_CONFIG = YAML.load_file(File.expand_path("../config/database.yml", File.dirname(__FILE__)))
25
+
26
+ if DATABASE_CONFIG[$adapter].nil?
27
+ puts "No such adapter '#{$adapter}'. Valid adapters are #{DATABASE_CONFIG.keys.join(', ')}."
28
+ exit 1
29
+ end
30
+
31
+ if ENV['TRAVIS']
32
+ DATABASE_CONFIG[$adapter]['username'] = 'postgres'
33
+ else
34
+ require 'etc'
35
+ username = $adapter =~ /mysql/ ? 'root' : Etc.getlogin
36
+ DATABASE_CONFIG[$adapter]['username'] = username
37
+ end
38
+
39
+ puts "Using #{$adapter} adapter."
40
+
41
+ ActiveRecord::Base.establish_connection(DATABASE_CONFIG[$adapter])
42
+
43
+ require 'generators/templates/create_rapns_notifications'
44
+ require 'generators/templates/create_rapns_feedback'
45
+ require 'generators/templates/add_alert_is_json_to_rapns_notifications'
46
+ require 'generators/templates/add_app_to_rapns'
47
+ require 'generators/templates/create_rapns_apps'
48
+ require 'generators/templates/add_gcm'
49
+
50
+ [CreateRapnsNotifications, CreateRapnsFeedback,
51
+ AddAlertIsJsonToRapnsNotifications, AddAppToRapns, CreateRapnsApps, AddGcm].each do |migration|
52
+ migration.down rescue ActiveRecord::StatementInvalid
53
+ migration.up
54
+ end
55
+
56
+ DatabaseCleaner.strategy = :truncation
57
+
58
+ require 'rapns_rails_2'
59
+ require 'rapns/daemon'
60
+
61
+ Rapns::Notification.reset_column_information
62
+ Rapns::App.reset_column_information
63
+ Rapns::Apns::Feedback.reset_column_information
64
+
65
+ require 'spec'
66
+ require 'spec/mocks'
67
+
68
+ Spec::Runner.configure do |config|
69
+ # config.before :suite do
70
+ # PerfTools::CpuProfiler.start('/tmp/rapns_profile')
71
+ # end
72
+ # config.after :suite do
73
+ # PerfTools::CpuProfiler.stop
74
+ # end
75
+
76
+ config.before(:all) do
77
+ unless defined? RAILS_DEFAULT_LOGGER
78
+ logger = mock
79
+ logger.stub(:debug) { |a| puts "DEBUG:" + a }
80
+ logger.as_null_object
81
+ RAILS_DEFAULT_LOGGER = logger
82
+ end
83
+ ActiveRecord::Base.logger = RAILS_DEFAULT_LOGGER
84
+
85
+ ActiveRecord::Base.default_timezone = :utc
86
+ end
87
+
88
+ config.before(:each) do
89
+ DatabaseCleaner.clean
90
+ end
91
+
92
+ config.after(:each) do
93
+ Rapns.logger = nil
94
+ Rapns::Daemon.store = nil
95
+ Rapns::Deprecation.muted do
96
+ Rapns.config.set_defaults if Rapns.config.kind_of?(Rapns::Configuration)
97
+ end
98
+ end
99
+ end
100
+
101
+ def stub_constants(constants, &block)
102
+ defined_constants = {}
103
+ saved_constants = {}
104
+
105
+ constants.each do |constant, val|
106
+ defined = defined_constants[ constant ] = self.const_defined?(constant)
107
+ saved_constants[ constant ] = self.const_get( constant ) if defined
108
+ Kernel::silence_warnings { self.const_set( constant, val ) }
109
+ end
110
+
111
+ begin
112
+ block.call
113
+ ensure
114
+ constants.each do |constant, val|
115
+ if defined_constants[ constant ]
116
+ Kernel::silence_warnings { self.const_set( constant, saved_constants[ constant ] ) }
117
+ else
118
+ Kernel::silence_warnings { self.send(:remove_const, constant ) }
119
+ end
120
+ end
121
+ end
122
+ end
123
+ public :stub_constants
124
+
125
+ # a test certificate that contains both an X509 certificate and
126
+ # a private key, similar to those used for connecting to Apple
127
+ # push notification servers.
128
+ #
129
+ # Note that we cannot validate the certificate and private key
130
+ # because we are missing the certificate chain used to validate
131
+ # the certificate, and this is private to Apple. So if the app
132
+ # has a certificate and a private key in it, the only way to find
133
+ # out if it really is valid is to connect to Apple's servers.
134
+
135
+ path = File.join(File.dirname(__FILE__), 'support')
136
+ TEST_CERT = File.read(File.join(path, 'cert_without_password.pem'))
137
+ TEST_CERT_WITH_PASSWORD = File.read(File.join(path, 'cert_with_password.pem'))