rhoconnect 5.5.18 → 7.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,116 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe "Ping Android FCM" do
4
+ include_examples "SharedRhoconnectHelper", :rhoconnect_data => false
5
+
6
+ before(:each) do
7
+ allow( Google::Auth).to receive(:get_application_default)
8
+ @params = {"device_pin" => @c.device_pin,
9
+ "sources" => [@s.name], "message" => 'hello world',
10
+ "vibrate" => '5', "badge" => '5', "sound" => 'hello.mp3'}
11
+ @response = double('response')
12
+ Rhoconnect.settings[:fcm_project_id] = 'valid_project_id'
13
+ Rhoconnect.settings[:package_name] = 'valid_package_name'
14
+ end
15
+
16
+ it "should ping fcm successfully" do
17
+ stub_request(:post, "https://fcm.googleapis.com/v1/projects/valid_project_id/messages:send").with { |request|
18
+ hash = JSON.parse request.body
19
+ valid = hash["message"]["token"] == "abcd"
20
+ valid = valid && hash["message"]["topic"] == nil
21
+ valid = valid && hash["message"]["android"]["restricted_package_name"] == "valid_package_name"
22
+ valid
23
+ }
24
+ Fcm.ping(@params)
25
+ end
26
+
27
+ it "should raise error on missing fcm_project_id setting" do
28
+ key = Rhoconnect.settings[:fcm_project_id.dup]
29
+ Rhoconnect.settings[:fcm_project_id] = nil
30
+ expect(lambda { Fcm.ping(@params) }).to raise_error(Fcm::InvalidProjectId, 'Missing `:fcm_project_id:` option in settings/settings.yml')
31
+ Rhoconnect.settings[:fcm_project_id] = key
32
+ end
33
+
34
+ it "should raise error on missing package_name setting" do
35
+ key = Rhoconnect.settings[:package_name.dup]
36
+ Rhoconnect.settings[:package_name] = nil
37
+ expect(lambda { Fcm.ping(@params) }).to raise_error(Fcm::InvalidPackageName, 'Missing `:package_name:` option in settings/settings.yml')
38
+ Rhoconnect.settings[:package_name] = key
39
+ end
40
+
41
+ it "should ping fcm with 503 connection error" do
42
+ stub_request(:post, "https://fcm.googleapis.com/v1/projects/valid_project_id/messages:send").to_raise(RestClient::Exception.new(nil,503))
43
+ expect(lambda { Fcm.ping(@params) }).to raise_error(RestClient::Exception)
44
+ end
45
+
46
+ xit "should ping fcm with 200 error message" do
47
+ allow( Google::Auth).to receive(:get_application_default)
48
+
49
+ error = 'error:DeviceMessageRateExceeded'
50
+ allow(@response).to receive(:code).and_return(200)
51
+ allow(@response).to receive(:body).and_return(error)
52
+ allow(@response).to receive(:headers).and_return(nil)
53
+ stub_request(:post, "https://fcm.googleapis.com/v1/projects/valid_project_id/messages:send").to_raise(RestClient::Exception.new(@response,200))
54
+ expect(lambda { Fcm.ping(@params) }).to raise_error(Fcm::FCMPingError, "FCM ping error: DeviceMessageRateExceeded")
55
+ end
56
+
57
+ xit "should fail to ping with bad authentication" do
58
+ error = 'Error=BadAuthentication'
59
+ allow(@response).to receive(:code).and_return(403)
60
+ allow(@response).to receive(:body).and_return(error)
61
+ allow(@response).to receive(:headers).and_return({})
62
+ setup_post_yield(@response)
63
+ expect(Fcm).to receive(:log).twice
64
+ expect(lambda { Fcm.ping(@params) }).to raise_error(
65
+ Fcm::InvalidProjectId, "Invalid FCM project id. Obtain new api key from FCM service."
66
+ )
67
+ end
68
+
69
+ xit "should ping fcm with 401 error message" do
70
+ allow(@response).to receive(:code).and_return(401)
71
+ allow(@response).to receive(:body).and_return('')
72
+ setup_post_yield(@response)
73
+ expect(Fcm).to receive(:log).twice
74
+ expect(lambda { Fcm.ping(@params) }).to raise_error(
75
+ Fcm::InvalidProjectId, "Invalid FCM project id. Obtain new api key from FCM service."
76
+ )
77
+ end
78
+
79
+ it "should compute fcm_message" do
80
+ expect(Google::Apis::Messages::Message).to receive(:new) do |options|
81
+ expect(options[:token]).to eq(@c.device_pin)
82
+ expect(options[:android]["priority"]).to eq("high")
83
+ expect(options[:android]["restricted_package_name"]).to eq(Rhoconnect.settings[:package_name])
84
+ expect(options[:android]["data"]["do_sync"]).to eq(@s.name)
85
+ expect(options[:android]["data"]["alert"]).to eq("hello world")
86
+ expect(options[:android]["data"]["vibrate"]).to eq("5")
87
+ expect(options[:android]["data"]["sound"]).to eq("hello.mp3")
88
+ expect(options[:android]["notification"]["body"]).to eq("hello world")
89
+ end
90
+
91
+ Fcm.fcm_message(Rhoconnect.settings[:package_name], @params)
92
+ end
93
+
94
+ it "should trim empty or nil params from fcm_message" do
95
+ expect(Google::Apis::Messages::Message).to receive(:new) do |options|
96
+ expect(options[:token]).to eq(@c.device_pin)
97
+ expect(options[:android]["priority"]).to eq("high")
98
+ expect(options[:android]["restricted_package_name"]).to eq(Rhoconnect.settings[:package_name])
99
+ expect(options[:android]["data"]["do_sync"]).to eq('')
100
+ expect(options[:android]["data"]["alert"]).to eq(nil)
101
+ expect(options[:android]["data"]["vibrate"]).to eq("5")
102
+ expect(options[:android]["data"]["sound"]).to eq("hello.mp3")
103
+ expect(options[:android]["notification"]["body"]).to eq(nil)
104
+ end
105
+
106
+ params = {
107
+ "device_pin" => @c.device_pin,
108
+ "sources" => [],
109
+ "message" => '',
110
+ "vibrate" => '5',
111
+ "sound" => 'hello.mp3'
112
+ }
113
+
114
+ Fcm.fcm_message(Rhoconnect.settings[:package_name], params)
115
+ end
116
+ end
@@ -1,6 +1,6 @@
1
1
  require File.join(File.dirname(__FILE__),'..','spec_helper')
2
2
 
3
- describe "Ping Android GCM" do
3
+ xdescribe "Ping Android GCM" do
4
4
  include_examples "SharedRhoconnectHelper", :rhoconnect_data => false
5
5
 
6
6
  before(:each) do
@@ -13,59 +13,57 @@ describe "Ping Android GCM" do
13
13
 
14
14
  it "should ping gcm successfully" do
15
15
  result = 'id=0:34234234134254%abc123\n'
16
- @response.stub(:code).and_return(200)
17
- @response.stub(:body).and_return(result)
18
- @response.stub(:headers).and_return({})
19
- @response.stub(:return!).and_return(@response)
16
+ allow(@response).to receive(:code).and_return(200)
17
+ allow(@response).to receive(:body).and_return(result)
18
+ allow(@response).to receive(:headers).and_return({})
19
+ allow(@response).to receive(:return!).and_return(@response)
20
20
  setup_post_yield(@response)
21
- Gcm.ping(@params).body.should == result
21
+ expect(Gcm.ping(@params).body).to eq(result)
22
22
  end
23
23
 
24
24
  it "should raise error on missing gcm_api_key setting" do
25
25
  key = Rhoconnect.settings[:gcm_api_key].dup
26
26
  Rhoconnect.settings[:gcm_api_key] = nil
27
- lambda { Gcm.ping(@params) }.should raise_error(
28
- Gcm::InvalidApiKey, 'Missing `:gcm_api_key:` option in settings/settings.yml'
29
- )
27
+ expect(lambda { Gcm.ping(@params) }).to raise_error(Gcm::InvalidApiKey, 'Missing `:gcm_api_key:` option in settings/settings.yml')
30
28
  Rhoconnect.settings[:gcm_api_key] = key
31
29
  end
32
30
 
33
31
  it "should ping gcm with 503 connection error" do
34
32
  error = 'Connection refused'
35
- @response.stub(:body).and_return(error)
36
- RestClient.stub(:post).and_return { raise RestClient::Exception.new(@response,503) }
37
- Gcm.should_receive(:log).twice
38
- lambda { Gcm.ping(@params) }.should raise_error(RestClient::Exception)
33
+ allow(@response).to receive(:body).and_return(error)
34
+ allow(RestClient).to receive(:post).and_raise(RestClient::Exception.new(@response,503))
35
+ allow(Gcm).to receive(:log).twice
36
+ expect(lambda { Gcm.ping(@params) }).to raise_error(RestClient::Exception)
39
37
  end
40
38
 
41
39
  it "should ping gcm with 200 error message" do
42
40
  error = 'Error=QuotaExceeded'
43
- @response.stub(:code).and_return(200)
44
- @response.stub(:body).and_return(error)
45
- @response.stub(:headers).and_return(nil)
41
+ allow(@response).to receive(:code).and_return(200)
42
+ allow(@response).to receive(:body).and_return(error)
43
+ allow(@response).to receive(:headers).and_return(nil)
46
44
  setup_post_yield(@response)
47
- Gcm.should_receive(:log).twice
48
- lambda { Gcm.ping(@params) }.should raise_error(Gcm::GCMPingError, "GCM ping error: QuotaExceeded")
45
+ expect(Gcm).to receive(:log).twice
46
+ expect(lambda { Gcm.ping(@params) }).to raise_error(Gcm::GCMPingError, "GCM ping error: QuotaExceeded")
49
47
  end
50
48
 
51
49
  it "should fail to ping with bad authentication" do
52
50
  error = 'Error=BadAuthentication'
53
- @response.stub(:code).and_return(403)
54
- @response.stub(:body).and_return(error)
55
- @response.stub(:headers).and_return({})
51
+ allow(@response).to receive(:code).and_return(403)
52
+ allow(@response).to receive(:body).and_return(error)
53
+ allow(@response).to receive(:headers).and_return({})
56
54
  setup_post_yield(@response)
57
- Gcm.should_receive(:log).twice
58
- lambda { Gcm.ping(@params) }.should raise_error(
55
+ expect(Gcm).to receive(:log).twice
56
+ expect(lambda { Gcm.ping(@params) }).to raise_error(
59
57
  Gcm::InvalidApiKey, "Invalid GCM api key. Obtain new api key from GCM service."
60
58
  )
61
59
  end
62
60
 
63
61
  it "should ping gcm with 401 error message" do
64
- @response.stub(:code).and_return(401)
65
- @response.stub(:body).and_return('')
62
+ allow(@response).to receive(:code).and_return(401)
63
+ allow(@response).to receive(:body).and_return('')
66
64
  setup_post_yield(@response)
67
- Gcm.should_receive(:log).twice
68
- lambda { Gcm.ping(@params) }.should raise_error(
65
+ expect(Gcm).to receive(:log).twice
66
+ expect(lambda { Gcm.ping(@params) }).to raise_error(
69
67
  Gcm::InvalidApiKey, "Invalid GCM api key. Obtain new api key from GCM service."
70
68
  )
71
69
  end
@@ -83,7 +81,7 @@ describe "Ping Android GCM" do
83
81
  }
84
82
  actual = Gcm.gcm_message(@params)
85
83
  actual['collapse_key'] = "RAND_KEY" unless actual['collapse_key'].nil?
86
- actual.should == expected
84
+ expect(actual).to eq(expected)
87
85
  end
88
86
 
89
87
  it "should trim empty or nil params from gcm_message" do
@@ -93,6 +91,6 @@ describe "Ping Android GCM" do
93
91
  "sources" => [], "message" => '', "vibrate" => '5', "sound" => 'hello.mp3'}
94
92
  actual = Gcm.gcm_message(params)
95
93
  actual['collapse_key'] = "RAND_KEY" unless actual['collapse_key'].nil?
96
- actual.should == expected
94
+ expect(actual).to eq(expected)
97
95
  end
98
96
  end
@@ -13,18 +13,18 @@ describe "Ping using RhoConnect push" do
13
13
 
14
14
  it "should ping rhoconnect push successfully" do
15
15
  result = ''
16
- @response.stub(:code).and_return(204)
17
- @response.stub(:body).and_return(result)
18
- @response.stub(:return!).and_return(@response)
19
- RestClient.stub(:post).and_return(@response)
16
+ allow(@response).to receive(:code).and_return(204)
17
+ allow(@response).to receive(:body).and_return(result)
18
+ allow(@response).to receive(:return!).and_return(@response)
19
+ allow(RestClient).to receive(:post).and_return(@response)
20
20
  res = RhoconnectPush.ping(@params)
21
- res.body.should == result
22
- res.code.should == 204
21
+ expect(res.body).to eq(result)
22
+ expect(res.code).to eq(204)
23
23
  end
24
24
 
25
25
  it "should ping rhoconnect push with missing push_server property" do
26
- RhoconnectPush.stub(:get_config).and_return({:test => {}})
27
- lambda { RhoconnectPush.ping(@params) }.should raise_error(
26
+ allow(RhoconnectPush).to receive(:get_config).and_return({:test => {}})
27
+ expect(lambda { RhoconnectPush.ping(@params) }).to raise_error(
28
28
  RhoconnectPush::InvalidPushServer, "Missing or invalid `:push_server` in settings/settings.yml."
29
29
  )
30
30
  end
@@ -32,11 +32,11 @@ describe "Ping using RhoConnect push" do
32
32
 
33
33
  it "should ping rhoconnect push with 400 response" do
34
34
  result = ''
35
- @response.stub(:code).and_return(400)
36
- @response.stub(:body).and_return(result)
37
- @response.stub(:return!).and_return(@response)
35
+ allow(@response).to receive(:code).and_return(400)
36
+ allow(@response).to receive(:body).and_return(result)
37
+ allow(@response).to receive(:return!).and_return(@response)
38
38
  setup_post_yield(@response)
39
- lambda { RhoconnectPush.ping(@params) }.should raise_error(
39
+ expect(lambda { RhoconnectPush.ping(@params) }).to raise_error(
40
40
  RhoconnectPush::InvalidPushRequest, "Invalid push request."
41
41
  )
42
42
  end
@@ -52,6 +52,6 @@ describe "Ping using RhoConnect push" do
52
52
  }
53
53
  }
54
54
  actual = RhoconnectPush.push_message(@params)
55
- JSON.parse(actual).should == expected
55
+ expect(JSON.parse(actual)).to eq(expected)
56
56
  end
57
57
  end
@@ -22,7 +22,7 @@ describe "Rhoconnect" do
22
22
  Rhoconnect.environment.should == :test
23
23
  Rhoconnect.stats.should == false
24
24
  Rhoconnect.cookie_expire.should == 9999999
25
- App.is_exist?(test_app_name).should be_true
25
+ App.is_exist?(test_app_name).should be true
26
26
  end
27
27
 
28
28
  it "should bootstrap Rhoconnect with RHO_ENV provided" do
@@ -69,4 +69,4 @@ describe "Rhoconnect" do
69
69
  s = Source.load('SampleAdapter',{:app_id => test_app_name,:user_id => '*'})
70
70
  s.has_many.should == "FixedSchemaAdapter,brand"
71
71
  end
72
- end
72
+ end
@@ -53,9 +53,9 @@ describe "CORS middleware" do
53
53
  allow.resource nil, :headers => :any, :methods => [:get, :post, :put, :delete], :credentials => true
54
54
  rescue Exception => e
55
55
  exception_happens = true
56
- e.is_a?(TypeError).should == true
56
+ expect(e.is_a?(TypeError)).to be true
57
57
  end
58
- exception_happens.should == true
58
+ expect(exception_happens).to be true
59
59
 
60
60
  allow.resource "/*", :headers => :any, :methods => [:get, :post, :put, :delete], :credentials => true
61
61
  end
@@ -68,8 +68,8 @@ describe "CORS middleware" do
68
68
  'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST'
69
69
  }
70
70
  status, headers, body = mv.call(env)
71
- 200.should == status
72
- headers['Access-Control-Allow-Origin'].should == '*'
71
+ expect(200).to eq(status)
72
+ expect(headers['Access-Control-Allow-Origin']).to eq('*')
73
73
  end
74
74
 
75
75
  it "preflight check should disable unknown origins" do
@@ -80,8 +80,8 @@ describe "CORS middleware" do
80
80
  'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST'
81
81
  }
82
82
  status, headers, body = @middleware.call(env)
83
- 200.should == status
84
- headers['Access-Control-Allow-Origin'].should_not == 'wrong_origin'
83
+ expect(200).to eq(status)
84
+ expect(headers['Access-Control-Allow-Origin']).not_to eq('wrong_origin')
85
85
  end
86
86
 
87
87
  it "preflight check should allow known origins" do
@@ -92,8 +92,8 @@ describe "CORS middleware" do
92
92
  'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST'
93
93
  }
94
94
  status, headers, body = @middleware.call(env)
95
- 200.should == status
96
- headers['Access-Control-Allow-Origin'].should == 'allowed_origin'
95
+ expect(200).to eq(status)
96
+ expect(headers['Access-Control-Allow-Origin']).to eq('allowed_origin')
97
97
  end
98
98
 
99
99
  it "able to use fallback X-ORIGIN request header if ORIGIN header is undefined" do
@@ -104,8 +104,8 @@ describe "CORS middleware" do
104
104
  'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST'
105
105
  }
106
106
  status, headers, body = @middleware.call(env)
107
- 200.should == status
108
- headers['Access-Control-Allow-Origin'].should == 'allowed_origin'
107
+ expect(200).to eq(status)
108
+ expect(headers['Access-Control-Allow-Origin']).to eq('allowed_origin')
109
109
  end
110
110
 
111
111
  it "preflight check treats empty 'null' origin as 'file://' one" do
@@ -116,8 +116,8 @@ describe "CORS middleware" do
116
116
  'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST'
117
117
  }
118
118
  status, headers, body = @middleware.call(env)
119
- 200.should == status
120
- headers['Access-Control-Allow-Origin'].should == 'file://'
119
+ expect(200).to eq(status)
120
+ expect(headers['Access-Control-Allow-Origin']).to eq('file://')
121
121
  end
122
122
 
123
123
  it "preflight check should enable allowed request headers" do
@@ -129,9 +129,9 @@ describe "CORS middleware" do
129
129
  'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' => 'allowed_header'
130
130
  }
131
131
  status, headers, body = @middleware.call(env)
132
- 200.should == status
132
+ expect(200).to eq(status)
133
133
  #headers['Access-Control-Allow-Origin'].should == 'allowed_origin'
134
- headers['Access-Control-Allow-Headers'].should == 'allowed_header'
134
+ expect(headers['Access-Control-Allow-Headers']).to eq('allowed_header')
135
135
  end
136
136
 
137
137
  it "preflight check should disable not allowed request headers" do
@@ -143,9 +143,9 @@ describe "CORS middleware" do
143
143
  'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' => 'not_allowed_header'
144
144
  }
145
145
  status, headers, body = @middleware.call(env)
146
- 200.should == status
146
+ expect(200).to eq(status)
147
147
  #headers['Access-Control-Allow-Origin'].should == 'allowed_origin'
148
- headers['Access-Control-Allow-Headers'].should_not == 'not_allowed_header'
148
+ expect(headers['Access-Control-Allow-Headers']).not_to eq('not_allowed_header')
149
149
  end
150
150
 
151
151
  it "preflight check should allow any request headers if configured so" do
@@ -157,9 +157,9 @@ describe "CORS middleware" do
157
157
  'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' => 'not_allowed_header'
158
158
  }
159
159
  status, headers, body = @middleware.call(env)
160
- 200.should == status
160
+ expect(200).to eq(status)
161
161
  #headers['Access-Control-Allow-Origin'].should == 'allowed_origin'
162
- headers['Access-Control-Allow-Headers'].should == 'not_allowed_header'
162
+ expect(headers['Access-Control-Allow-Headers']).to eq('not_allowed_header')
163
163
  end
164
164
 
165
165
 
@@ -171,8 +171,8 @@ describe "CORS middleware" do
171
171
  'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST'
172
172
  }
173
173
  status, headers, body = @middleware.call(env)
174
- 200.should == status
175
- headers['Access-Control-Expose-Headers'].should == 'Content-Length'
174
+ expect(200).to eq(status)
175
+ expect(headers['Access-Control-Expose-Headers']).to eq('Content-Length')
176
176
  end
177
177
 
178
178
  it "no response headers should be exposed by default" do
@@ -183,8 +183,8 @@ describe "CORS middleware" do
183
183
  'HTTP_ACCESS_CONTROL_REQUEST_METHOD' => 'POST'
184
184
  }
185
185
  status, headers, body = @middleware.call(env)
186
- 200.should == status
187
- headers['Access-Control-Expose-Headers'].should == ''
186
+ expect(200).to eq(status)
187
+ expect(headers['Access-Control-Expose-Headers']).to eq('')
188
188
  end
189
189
 
190
190