Skip to content

Commit 1abcd2c

Browse files
committed
Adding more test cases
1 parent 7c6dcbe commit 1abcd2c

File tree

1 file changed

+104
-14
lines changed

1 file changed

+104
-14
lines changed

spec/fcm_spec.rb

Lines changed: 104 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
let(:client) { FCM.new(json_key_path, project_name) }
4242

4343
let(:uri) { "#{FCM::BASE_URI_V1}#{project_name}/messages:send" }
44+
let(:status_code) { 200 }
4445

4546
let(:stub_fcm_send_v1_request) do
4647
stub_request(:post, uri).with(
@@ -50,15 +51,15 @@
5051
# ref: https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream
5152
body: "{}",
5253
headers: {},
53-
status: 200,
54+
status: status_code,
5455
)
5556
end
5657

5758
before do
5859
stub_fcm_send_v1_request
5960
end
6061

61-
shared_examples "succesfuly send notification" do
62+
shared_examples 'succesfuly send notification' do
6263
it 'should send notification of HTTP V1 using POST to FCM server' do
6364
client.send_v1(send_v1_params).should eq(
6465
response: 'success', body: '{}', headers: {}, status_code: 200
@@ -67,7 +68,7 @@
6768
end
6869
end
6970

70-
describe "send to token" do
71+
describe 'send to token' do
7172
let(:token) { '4sdsx' }
7273
let(:send_v1_params) do
7374
{
@@ -95,10 +96,20 @@
9596
}
9697
end
9798

98-
include_examples "succesfuly send notification"
99+
include_examples 'succesfuly send notification'
100+
101+
it 'includes all the response' do
102+
response = client.send_v1(send_v1_params)
103+
expect(response[:status_code]).to eq(status_code)
104+
expect(response[:response]).to eq('success')
105+
expect(response[:body]).to eq('{}')
106+
expect(response[:headers]).to eq({})
107+
expect(response[:canonical_ids]).to be_nil
108+
expect(response[:not_registered_ids]).to be_nil
109+
end
99110
end
100111

101-
describe "send to multiple tokens" do
112+
describe 'send to multiple tokens' do
102113
let(:tokens) { ['4sdsx', '4sdsy'] }
103114
let(:send_v1_params) do
104115
{
@@ -113,7 +124,7 @@
113124
include_examples 'succesfuly send notification'
114125
end
115126

116-
describe "send to topic" do
127+
describe 'send to topic' do
117128
let(:topic) { 'news' }
118129
let(:send_v1_params) do
119130
{
@@ -125,9 +136,9 @@
125136
}
126137
end
127138

128-
include_examples "succesfuly send notification"
139+
include_examples 'succesfuly send notification'
129140

130-
context "when topic is invalid" do
141+
context 'when topic is invalid' do
131142
let(:topic) { '/topics/news$' }
132143

133144
it 'should raise error' do
@@ -136,7 +147,7 @@
136147
end
137148
end
138149

139-
describe "send to condition" do
150+
describe 'send to condition' do
140151
let(:condition) { "'foo' in topics" }
141152
let(:send_v1_params) do
142153
{
@@ -148,10 +159,10 @@
148159
}
149160
end
150161

151-
include_examples "succesfuly send notification"
162+
include_examples 'succesfuly send notification'
152163
end
153164

154-
describe "send to notification_key" do
165+
describe 'send to notification_key' do
155166
let(:notification_key) { 'notification_key' }
156167
let(:send_v1_params) do
157168
{
@@ -163,11 +174,81 @@
163174
}
164175
end
165176

166-
include_examples "succesfuly send notification"
177+
include_examples 'succesfuly send notification'
178+
end
179+
180+
context 'when project_name is empty' do
181+
let(:project_name) { '' }
182+
let(:send_v1_params) do
183+
{
184+
'token' => '4sdsx',
185+
'notification' => {
186+
'title' => 'Breaking News',
187+
'body' => 'New news story available.'
188+
}
189+
}
190+
end
191+
192+
it 'should not send notification' do
193+
client.send_v1(send_v1_params)
194+
stub_fcm_send_v1_request.should_not have_been_requested
195+
end
196+
end
197+
198+
describe 'error handling' do
199+
let(:send_v1_params) do
200+
{
201+
'token' => '4sdsx',
202+
'notification' => {
203+
'title' => 'Breaking News',
204+
'body' => 'New news story available.'
205+
}
206+
}
207+
end
208+
209+
context 'when status_code is 400' do
210+
let(:status_code) { 400 }
211+
212+
it 'should raise error' do
213+
response = client.send_v1(send_v1_params)
214+
expect(response[:status_code]).to eq(status_code)
215+
expect(response[:response]).to include('Only applies for JSON requests')
216+
end
217+
end
218+
219+
context 'when status_code is 401' do
220+
let(:status_code) { 401 }
221+
222+
it 'should raise error' do
223+
response = client.send_v1(send_v1_params)
224+
expect(response[:status_code]).to eq(status_code)
225+
expect(response[:response]).to include('There was an error authenticating')
226+
end
227+
end
228+
229+
context 'when status_code is 500' do
230+
let(:status_code) { 500 }
231+
232+
it 'should raise error' do
233+
response = client.send_v1(send_v1_params)
234+
expect(response[:status_code]).to eq(status_code)
235+
expect(response[:response]).to include('There was an internal error')
236+
end
237+
end
238+
239+
context 'when status_code is 503' do
240+
let(:status_code) { 503 }
241+
242+
it 'should raise error' do
243+
response = client.send_v1(send_v1_params)
244+
expect(response[:status_code]).to eq(status_code)
245+
expect(response[:response]).to include('Server is temporarily unavailable')
246+
end
247+
end
167248
end
168249
end
169250

170-
describe "#send_to_topic" do
251+
describe '#send_to_topic' do
171252
let(:client) { FCM.new(json_key_path, project_name) }
172253

173254
let(:uri) { "#{FCM::BASE_URI_V1}#{project_name}/messages:send" }
@@ -208,7 +289,7 @@
208289
stub_fcm_send_to_topic_request.should have_been_made.times(1)
209290
end
210291

211-
context "when topic is invalid" do
292+
context 'when topic is invalid' do
212293
let(:topic) { '/topics/news$' }
213294

214295
it 'should raise error' do
@@ -258,6 +339,15 @@
258339
)
259340
stub_fcm_send_to_topic_condition_request.should have_been_made.times(1)
260341
end
342+
343+
context 'when topic_condition is invalid' do
344+
let(:topic_condition) { "'foo' in topics$" }
345+
346+
it 'should raise error' do
347+
client.send_to_topic_condition(topic_condition, options)
348+
stub_fcm_send_to_topic_condition_request.should_not have_been_requested
349+
end
350+
end
261351
end
262352

263353
describe "#get_instance_id_info" do

0 commit comments

Comments
 (0)