|
41 | 41 | let(:client) { FCM.new(json_key_path, project_name) } |
42 | 42 |
|
43 | 43 | let(:uri) { "#{FCM::BASE_URI_V1}#{project_name}/messages:send" } |
| 44 | + let(:status_code) { 200 } |
44 | 45 |
|
45 | 46 | let(:stub_fcm_send_v1_request) do |
46 | 47 | stub_request(:post, uri).with( |
|
50 | 51 | # ref: https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream |
51 | 52 | body: "{}", |
52 | 53 | headers: {}, |
53 | | - status: 200, |
| 54 | + status: status_code, |
54 | 55 | ) |
55 | 56 | end |
56 | 57 |
|
57 | 58 | before do |
58 | 59 | stub_fcm_send_v1_request |
59 | 60 | end |
60 | 61 |
|
61 | | - shared_examples "succesfuly send notification" do |
| 62 | + shared_examples 'succesfuly send notification' do |
62 | 63 | it 'should send notification of HTTP V1 using POST to FCM server' do |
63 | 64 | client.send_v1(send_v1_params).should eq( |
64 | 65 | response: 'success', body: '{}', headers: {}, status_code: 200 |
|
67 | 68 | end |
68 | 69 | end |
69 | 70 |
|
70 | | - describe "send to token" do |
| 71 | + describe 'send to token' do |
71 | 72 | let(:token) { '4sdsx' } |
72 | 73 | let(:send_v1_params) do |
73 | 74 | { |
|
95 | 96 | } |
96 | 97 | end |
97 | 98 |
|
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 |
99 | 110 | end |
100 | 111 |
|
101 | | - describe "send to multiple tokens" do |
| 112 | + describe 'send to multiple tokens' do |
102 | 113 | let(:tokens) { ['4sdsx', '4sdsy'] } |
103 | 114 | let(:send_v1_params) do |
104 | 115 | { |
|
113 | 124 | include_examples 'succesfuly send notification' |
114 | 125 | end |
115 | 126 |
|
116 | | - describe "send to topic" do |
| 127 | + describe 'send to topic' do |
117 | 128 | let(:topic) { 'news' } |
118 | 129 | let(:send_v1_params) do |
119 | 130 | { |
|
125 | 136 | } |
126 | 137 | end |
127 | 138 |
|
128 | | - include_examples "succesfuly send notification" |
| 139 | + include_examples 'succesfuly send notification' |
129 | 140 |
|
130 | | - context "when topic is invalid" do |
| 141 | + context 'when topic is invalid' do |
131 | 142 | let(:topic) { '/topics/news$' } |
132 | 143 |
|
133 | 144 | it 'should raise error' do |
|
136 | 147 | end |
137 | 148 | end |
138 | 149 |
|
139 | | - describe "send to condition" do |
| 150 | + describe 'send to condition' do |
140 | 151 | let(:condition) { "'foo' in topics" } |
141 | 152 | let(:send_v1_params) do |
142 | 153 | { |
|
148 | 159 | } |
149 | 160 | end |
150 | 161 |
|
151 | | - include_examples "succesfuly send notification" |
| 162 | + include_examples 'succesfuly send notification' |
152 | 163 | end |
153 | 164 |
|
154 | | - describe "send to notification_key" do |
| 165 | + describe 'send to notification_key' do |
155 | 166 | let(:notification_key) { 'notification_key' } |
156 | 167 | let(:send_v1_params) do |
157 | 168 | { |
|
163 | 174 | } |
164 | 175 | end |
165 | 176 |
|
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 |
167 | 248 | end |
168 | 249 | end |
169 | 250 |
|
170 | | - describe "#send_to_topic" do |
| 251 | + describe '#send_to_topic' do |
171 | 252 | let(:client) { FCM.new(json_key_path, project_name) } |
172 | 253 |
|
173 | 254 | let(:uri) { "#{FCM::BASE_URI_V1}#{project_name}/messages:send" } |
|
208 | 289 | stub_fcm_send_to_topic_request.should have_been_made.times(1) |
209 | 290 | end |
210 | 291 |
|
211 | | - context "when topic is invalid" do |
| 292 | + context 'when topic is invalid' do |
212 | 293 | let(:topic) { '/topics/news$' } |
213 | 294 |
|
214 | 295 | it 'should raise error' do |
|
258 | 339 | ) |
259 | 340 | stub_fcm_send_to_topic_condition_request.should have_been_made.times(1) |
260 | 341 | 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 |
261 | 351 | end |
262 | 352 |
|
263 | 353 | describe "#get_instance_id_info" do |
|
0 commit comments