|
5 | 5 | describe FCM do |
6 | 6 | let(:api_key) { 'AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA' } |
7 | 7 | 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 |
8 | 18 |
|
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 |
11 | 22 |
|
| 23 | + describe '#send_notification' do |
12 | 24 | let(:stub_fcm_send_request) do |
13 | 25 | stub_request(:post, send_url).with( |
14 | 26 | body: valid_request_body.to_json, |
|
27 | 39 | { registration_ids: [registration_id] } |
28 | 40 | end |
29 | 41 |
|
30 | | - let(:valid_request_headers) do |
31 | | - { |
32 | | - 'Content-Type' => 'application/json', |
33 | | - 'Authorization' => "key=#{api_key}" |
34 | | - } |
35 | | - end |
36 | | - |
37 | 42 | let(:successful_fcm_response) do |
38 | 43 | { |
39 | 44 | response: 'success', |
|
102 | 107 | end |
103 | 108 |
|
104 | 109 | 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 | | - |
113 | 110 | let(:successful_fcm_response) do |
114 | 111 | { |
115 | 112 | response: 'success', |
|
155 | 152 | end |
156 | 153 | end |
157 | 154 |
|
| 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 | + |
158 | 221 | describe '#send_v1' do |
159 | 222 | let(:project_name) { 'project_name' } |
160 | 223 | let(:send_v1_url) { "#{FCM::BASE_URI_V1}#{project_name}/messages:send" } |
|
250 | 313 | # end |
251 | 314 | # let(:invalid_condition_topic) { "'TopicA$' in topics" } |
252 | 315 |
|
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 |
304 | 316 |
|
305 | 317 | # context 'when send_notification responds with failure' do |
306 | 318 | # subject { described_class.new(api_key) } |
|
0 commit comments