Skip to content

Commit a2d9d8e

Browse files
committed
rspec fix 4
1 parent 313ac28 commit a2d9d8e

File tree

1 file changed

+80
-68
lines changed

1 file changed

+80
-68
lines changed

spec/fcm_spec.rb

Lines changed: 80 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@
55
describe FCM do
66
let(:api_key) { 'AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA' }
77
let(:fcm) { described_class.new(api_key) }
8+
let(:send_url) { "#{FCM::BASE_URI}/fcm/send" }
9+
let(:valid_condition) do
10+
"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"
11+
end
12+
let(:valid_request_headers) do
13+
{
14+
'Content-Type' => 'application/json',
15+
'Authorization' => "key=#{api_key}"
16+
}
17+
end
818

9-
describe '#send_notification' do
10-
let(:send_url) { "#{FCM::BASE_URI}/fcm/send" }
19+
it 'raises an error if the api key is not provided' do
20+
expect { described_class.new }.to raise_error(ArgumentError)
21+
end
1122

23+
describe '#send_notification' do
1224
let(:stub_fcm_send_request) do
1325
stub_request(:post, send_url).with(
1426
body: valid_request_body.to_json,
@@ -27,13 +39,6 @@
2739
{ registration_ids: [registration_id] }
2840
end
2941

30-
let(:valid_request_headers) do
31-
{
32-
'Content-Type' => 'application/json',
33-
'Authorization' => "key=#{api_key}"
34-
}
35-
end
36-
3742
let(:successful_fcm_response) do
3843
{
3944
response: 'success',
@@ -102,14 +107,6 @@
102107
end
103108

104109
describe '#send_to_topic' do
105-
let(:send_url) { "#{FCM::BASE_URI}/fcm/send" }
106-
let(:valid_request_headers) do
107-
{
108-
'Content-Type' => 'application/json',
109-
'Authorization' => "key=#{api_key}"
110-
}
111-
end
112-
113110
let(:successful_fcm_response) do
114111
{
115112
response: 'success',
@@ -155,6 +152,72 @@
155152
end
156153
end
157154

155+
describe '#send_to_topic_condition' do
156+
context 'when sending notification to a topic condition' do
157+
let!(:stub_with_valid_condition) do
158+
stub_request(:post, send_url)
159+
.with(
160+
body: '{"condition":"\'TopicA\' in topics && (\'TopicB\' in topics'\
161+
' || \'TopicC\' in topics)","data":{"score":"5x1","time":"15:10"}}',
162+
headers: valid_request_headers
163+
).to_return(status: 200, body: '', headers: {})
164+
end
165+
166+
it 'sends the data in a post request to fcm' do
167+
fcm.send_to_topic_condition(
168+
valid_condition,
169+
data: { score: '5x1', time: '15:10' }
170+
)
171+
stub_with_valid_condition.should have_been_requested
172+
end
173+
end
174+
175+
context 'when sending notification to an invalid condition' do
176+
let!(:stub_with_invalid_condition) do
177+
stub_request(:post, send_url)
178+
.with(
179+
body:
180+
'{"condition":"\'TopicA\' in topics and some other text'\
181+
' (\'TopicB\' in topics || \'TopicC\' in topics)","data":'\
182+
'{"score":"5x1","time":"15:10"}}',
183+
headers: valid_request_headers
184+
).to_return(status: 200, body: '', headers: {})
185+
end
186+
187+
let(:invalid_condition) do
188+
"'TopicA' in topics and some other text'\
189+
' ('TopicB' in topics || 'TopicC' in topics)"
190+
end
191+
192+
it 'does not send to invalid conditions' do
193+
fcm.send_to_topic_condition(invalid_condition,
194+
data: { score: '5x1', time: '15:10' })
195+
stub_with_invalid_condition.should_not have_been_requested
196+
end
197+
end
198+
199+
context 'when sending notification to an invalid condition topic' do
200+
let(:invalid_condition_topic) { "'TopicA$' in topics" }
201+
let!(:stub_with_invalid_condition_topic) do
202+
stub_request(:post, send_url)
203+
.with(
204+
body:
205+
'{"condition":"\'TopicA$\' in topics","data"'\
206+
':{"score":"5x1","time":"15:10"}}',
207+
headers: valid_request_headers
208+
).to_return(status: 200, body: '', headers: {})
209+
end
210+
211+
it 'does not send to invalid topics in a condition' do
212+
fcm.send_to_topic_condition(
213+
invalid_condition_topic,
214+
data: { score: '5x1', time: '15:10' }
215+
)
216+
stub_with_invalid_condition_topic.should_not have_been_requested
217+
end
218+
end
219+
end
220+
158221
describe '#send_v1' do
159222
let(:project_name) { 'project_name' }
160223
let(:send_v1_url) { "#{FCM::BASE_URI_V1}#{project_name}/messages:send" }
@@ -250,57 +313,6 @@
250313
# end
251314
# let(:invalid_condition_topic) { "'TopicA$' in topics" }
252315

253-
# it 'raises an error if the api key is not provided' do
254-
# expect { described_class.new }.to raise_error(ArgumentError)
255-
# end
256-
257-
# it 'raises error if time_to_live is given' do
258-
# # ref: https://firebase.google.com/docs/cloud-messaging/http-server-ref#ttl
259-
# end
260-
261-
# context 'sending notification to a topic condition' do
262-
# let!(:stub_with_valid_condition) do
263-
# stub_request(:post, send_url)
264-
# .with(body: '{"condition":"\'TopicA\' in topics && (\'TopicB\' in topics || \'TopicC\' in topics)","data":{"score":"5x1","time":"15:10"}}',
265-
# headers: valid_request_headers)
266-
# .to_return(status: 200, body: '', headers: {})
267-
# end
268-
# let!(:stub_with_invalid_condition) do
269-
# stub_request(:post, send_url)
270-
# .with(body: '{"condition":"\'TopicA\' in topics and some other text (\'TopicB\' in topics || \'TopicC\' in topics)","data":{"score":"5x1","time":"15:10"}}',
271-
# headers: valid_request_headers)
272-
# .to_return(status: 200, body: '', headers: {})
273-
# end
274-
# let!(:stub_with_invalid_condition_topic) do
275-
# stub_request(:post, send_url)
276-
# .with(body: '{"condition":"\'TopicA$\' in topics","data":{"score":"5x1","time":"15:10"}}',
277-
# headers: valid_request_headers)
278-
# .to_return(status: 200, body: '', headers: {})
279-
# end
280-
281-
# describe '#send_to_topic_condition' do
282-
# it 'sends the data in a post request to fcm' do
283-
# fcm = described_class.new(api_key)
284-
# fcm.send_to_topic_condition(valid_condition,
285-
# data: { score: '5x1', time: '15:10' })
286-
# stub_with_valid_condition.should have_been_requested
287-
# end
288-
289-
# it 'does not send to invalid conditions' do
290-
# fcm = described_class.new(api_key)
291-
# fcm.send_to_topic_condition(invalid_condition,
292-
# data: { score: '5x1', time: '15:10' })
293-
# stub_with_invalid_condition.should_not have_been_requested
294-
# end
295-
296-
# it 'does not send to invalid topics in a condition' do
297-
# fcm = described_class.new(api_key)
298-
# fcm.send_to_topic_condition(invalid_condition_topic,
299-
# data: { score: '5x1', time: '15:10' })
300-
# stub_with_invalid_condition_topic.should_not have_been_requested
301-
# end
302-
# end
303-
# end
304316

305317
# context 'when send_notification responds with failure' do
306318
# subject { described_class.new(api_key) }

0 commit comments

Comments
 (0)