11
11
from app .dao .notifications_dao import (
12
12
get_notification_by_id
13
13
)
14
- from tests .app .conftest import sample_notification as create_sample_notification
15
- from tests .app .db import create_service_callback_api
14
+ from tests .app .db import create_notification , create_service_callback_api
16
15
17
16
18
17
def firetext_post (client , data ):
@@ -161,15 +160,13 @@ def test_firetext_callback_should_return_400_if_no_status(client, mocker):
161
160
162
161
163
162
def test_firetext_callback_should_set_status_technical_failure_if_status_unknown (
164
- client , notify_db , notify_db_session , mocker ):
165
- notification = create_sample_notification (
166
- notify_db , notify_db_session , status = 'sending' , sent_at = datetime .utcnow ()
167
- )
168
- mocker .patch ('app.statsd_client.incr' )
169
- data = 'mobile=441234123123&status=99&time=2016-03-10 14:17:00&reference={}' .format (notification .id )
163
+ client , mocker , sample_notification ):
164
+ sample_notification .status = 'sending'
165
+ # mocker.patch('app.statsd_client.incr')
166
+ data = 'mobile=441234123123&status=99&time=2016-03-10 14:17:00&reference={}' .format (sample_notification .id )
170
167
with pytest .raises (ClientException ) as e :
171
168
firetext_post (client , data )
172
- assert get_notification_by_id (notification .id ).status == 'technical-failure'
169
+ assert get_notification_by_id (sample_notification .id ).status == 'technical-failure'
173
170
assert 'Firetext callback failed: status 99 not found.' in str (e .value )
174
171
175
172
@@ -201,52 +198,40 @@ def test_callback_should_return_200_if_cannot_find_notification_id(
201
198
202
199
203
200
def test_firetext_callback_should_update_notification_status (
204
- notify_db , notify_db_session , client , sample_email_template , mocker
201
+ client , mocker , sample_notification
205
202
):
206
203
mocker .patch ('app.statsd_client.incr' )
207
204
send_mock = mocker .patch (
208
205
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
209
206
)
210
- notification = create_sample_notification (
211
- notify_db ,
212
- notify_db_session ,
213
- template = sample_email_template ,
214
- reference = 'ref' ,
215
- status = 'sending' ,
216
- sent_at = datetime .utcnow ())
207
+ sample_notification .status = 'sending'
217
208
218
- original = get_notification_by_id (notification .id )
209
+ original = get_notification_by_id (sample_notification .id )
219
210
assert original .status == 'sending'
220
211
data = 'mobile=441234123123&status=0&time=2016-03-10 14:17:00&reference={}' .format (
221
- notification .id )
212
+ sample_notification .id )
222
213
response = firetext_post (client , data )
223
214
224
215
json_resp = json .loads (response .get_data (as_text = True ))
225
216
assert response .status_code == 200
226
217
assert json_resp ['result' ] == 'success'
227
218
assert json_resp ['message' ] == 'Firetext callback succeeded. reference {} updated' .format (
228
- notification .id
219
+ sample_notification .id
229
220
)
230
- updated = get_notification_by_id (notification .id )
221
+ updated = get_notification_by_id (sample_notification .id )
231
222
assert updated .status == 'delivered'
232
- assert get_notification_by_id (notification .id ).status == 'delivered'
233
- assert send_mock .called_once_with ([notification .id ], queue = "notify-internal-tasks" )
223
+ assert get_notification_by_id (sample_notification .id ).status == 'delivered'
224
+ assert send_mock .called_once_with ([sample_notification .id ], queue = "notify-internal-tasks" )
234
225
235
226
236
227
def test_firetext_callback_should_update_notification_status_failed (
237
- notify_db , notify_db_session , client , sample_template , mocker
228
+ client , mocker , sample_template
238
229
):
239
230
mocker .patch ('app.statsd_client.incr' )
240
231
mocker .patch (
241
232
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
242
233
)
243
- notification = create_sample_notification (
244
- notify_db ,
245
- notify_db_session ,
246
- template = sample_template ,
247
- reference = 'ref' ,
248
- status = 'sending' ,
249
- sent_at = datetime .utcnow ())
234
+ notification = create_notification (template = sample_template , status = 'sending' )
250
235
251
236
original = get_notification_by_id (notification .id )
252
237
assert original .status == 'sending'
@@ -264,14 +249,13 @@ def test_firetext_callback_should_update_notification_status_failed(
264
249
assert get_notification_by_id (notification .id ).status == 'permanent-failure'
265
250
266
251
267
- def test_firetext_callback_should_update_notification_status_pending (client , notify_db , notify_db_session , mocker ):
252
+ def test_firetext_callback_should_update_notification_status_pending (client , sample_template , mocker ):
268
253
mocker .patch ('app.statsd_client.incr' )
269
254
mocker .patch (
270
255
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
271
256
)
272
- notification = create_sample_notification (
273
- notify_db , notify_db_session , status = 'sending' , sent_at = datetime .utcnow ()
274
- )
257
+ notification = create_notification (template = sample_template , status = 'sending' )
258
+
275
259
original = get_notification_by_id (notification .id )
276
260
assert original .status == 'sending'
277
261
data = 'mobile=441234123123&status=2&time=2016-03-10 14:17:00&reference={}' .format (
@@ -299,16 +283,14 @@ def test_process_mmg_response_return_200_when_cid_is_send_sms_code(client):
299
283
300
284
301
285
def test_process_mmg_response_returns_200_when_cid_is_valid_notification_id (
302
- notify_db , notify_db_session , client , mocker
286
+ sample_notification , client , mocker
303
287
):
304
288
mocker .patch (
305
289
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
306
290
)
307
- notification = create_sample_notification (
308
- notify_db , notify_db_session , status = 'sending' , sent_at = datetime .utcnow ()
309
- )
291
+ sample_notification .status = 'sending'
310
292
data = json .dumps ({"reference" : "mmg_reference" ,
311
- "CID" : str (notification .id ),
293
+ "CID" : str (sample_notification .id ),
312
294
"MSISDN" : "447777349060" ,
313
295
"status" : "3" ,
314
296
"deliverytime" : "2016-04-05 16:01:07" })
@@ -318,96 +300,88 @@ def test_process_mmg_response_returns_200_when_cid_is_valid_notification_id(
318
300
assert response .status_code == 200
319
301
json_data = json .loads (response .data )
320
302
assert json_data ['result' ] == 'success'
321
- assert json_data ['message' ] == 'MMG callback succeeded. reference {} updated' .format (notification .id )
322
- assert get_notification_by_id (notification .id ).status == 'delivered'
303
+ assert json_data ['message' ] == 'MMG callback succeeded. reference {} updated' .format (sample_notification .id )
304
+ assert get_notification_by_id (sample_notification .id ).status == 'delivered'
323
305
324
306
325
307
def test_process_mmg_response_status_5_updates_notification_with_permanently_failed (
326
- notify_db , notify_db_session , client , mocker
308
+ sample_notification , client , mocker
327
309
):
328
310
mocker .patch (
329
311
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
330
312
)
331
- notification = create_sample_notification (
332
- notify_db , notify_db_session , status = 'sending' , sent_at = datetime .utcnow ()
333
- )
313
+ sample_notification .status = 'sending'
334
314
335
315
data = json .dumps ({"reference" : "mmg_reference" ,
336
- "CID" : str (notification .id ),
316
+ "CID" : str (sample_notification .id ),
337
317
"MSISDN" : "447777349060" ,
338
318
"status" : 5 })
339
319
340
320
response = mmg_post (client , data )
341
321
assert response .status_code == 200
342
322
json_data = json .loads (response .data )
343
323
assert json_data ['result' ] == 'success'
344
- assert json_data ['message' ] == 'MMG callback succeeded. reference {} updated' .format (notification .id )
345
- assert get_notification_by_id (notification .id ).status == 'permanent-failure'
324
+ assert json_data ['message' ] == 'MMG callback succeeded. reference {} updated' .format (sample_notification .id )
325
+ assert get_notification_by_id (sample_notification .id ).status == 'permanent-failure'
346
326
347
327
348
328
def test_process_mmg_response_status_2_updates_notification_with_permanently_failed (
349
- notify_db , notify_db_session , client , mocker
329
+ sample_notification , client , mocker
350
330
):
351
331
mocker .patch (
352
332
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
353
333
)
354
- notification = create_sample_notification (
355
- notify_db , notify_db_session , status = 'sending' , sent_at = datetime .utcnow ()
356
- )
334
+ sample_notification .status = 'sending'
357
335
data = json .dumps ({"reference" : "mmg_reference" ,
358
- "CID" : str (notification .id ),
336
+ "CID" : str (sample_notification .id ),
359
337
"MSISDN" : "447777349060" ,
360
338
"status" : 2 })
361
339
362
340
response = mmg_post (client , data )
363
341
assert response .status_code == 200
364
342
json_data = json .loads (response .data )
365
343
assert json_data ['result' ] == 'success'
366
- assert json_data ['message' ] == 'MMG callback succeeded. reference {} updated' .format (notification .id )
367
- assert get_notification_by_id (notification .id ).status == 'permanent-failure'
344
+ assert json_data ['message' ] == 'MMG callback succeeded. reference {} updated' .format (sample_notification .id )
345
+ assert get_notification_by_id (sample_notification .id ).status == 'permanent-failure'
368
346
369
347
370
348
def test_process_mmg_response_status_4_updates_notification_with_temporary_failed (
371
- notify_db , notify_db_session , client , mocker
349
+ sample_notification , client , mocker
372
350
):
373
351
mocker .patch (
374
352
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
375
353
)
376
- notification = create_sample_notification (
377
- notify_db , notify_db_session , status = 'sending' , sent_at = datetime .utcnow ()
378
- )
354
+ sample_notification .status = 'sending'
379
355
380
356
data = json .dumps ({"reference" : "mmg_reference" ,
381
- "CID" : str (notification .id ),
357
+ "CID" : str (sample_notification .id ),
382
358
"MSISDN" : "447777349060" ,
383
359
"status" : 4 })
384
360
385
361
response = mmg_post (client , data )
386
362
assert response .status_code == 200
387
363
json_data = json .loads (response .data )
388
364
assert json_data ['result' ] == 'success'
389
- assert json_data ['message' ] == 'MMG callback succeeded. reference {} updated' .format (notification .id )
390
- assert get_notification_by_id (notification .id ).status == 'temporary-failure'
365
+ assert json_data ['message' ] == 'MMG callback succeeded. reference {} updated' .format (sample_notification .id )
366
+ assert get_notification_by_id (sample_notification .id ).status == 'temporary-failure'
391
367
392
368
393
369
def test_process_mmg_response_unknown_status_updates_notification_with_technical_failure (
394
- notify_db , notify_db_session , client , mocker
370
+ sample_notification , client , mocker
395
371
):
396
372
send_mock = mocker .patch (
397
373
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
398
374
)
399
- notification = create_sample_notification (
400
- notify_db , notify_db_session , status = 'sending' , sent_at = datetime .utcnow ()
401
- )
375
+ sample_notification .status = 'sending'
402
376
data = json .dumps ({"reference" : "mmg_reference" ,
403
- "CID" : str (notification .id ),
377
+ "CID" : str (sample_notification .id ),
404
378
"MSISDN" : "447777349060" ,
405
379
"status" : 10 })
406
- create_service_callback_api (service = notification .service , url = "https://original_url.com" )
380
+ create_service_callback_api (service = sample_notification .service , url = "https://original_url.com" )
407
381
with pytest .raises (ClientException ) as e :
408
382
mmg_post (client , data )
409
383
assert 'MMG callback failed: status 10 not found.' in str (e .value )
410
- assert get_notification_by_id (notification .id ).status == 'technical-failure'
384
+ assert get_notification_by_id (sample_notification .id ).status == 'technical-failure'
411
385
assert send_mock .called
412
386
413
387
@@ -445,20 +419,19 @@ def test_mmg_callback_returns_400_when_notification_id_is_not_a_valid_uuid(clien
445
419
assert json_resp ['message' ] == 'MMG callback with invalid reference 1234'
446
420
447
421
448
- def test_process_mmg_response_records_statsd (notify_db , notify_db_session , client , mocker ):
422
+ def test_process_mmg_response_records_statsd (sample_notification , client , mocker ):
449
423
with freeze_time ('2001-01-01T12:00:00' ):
450
424
451
425
mocker .patch ('app.statsd_client.incr' )
452
426
mocker .patch ('app.statsd_client.timing_with_dates' )
453
427
mocker .patch (
454
428
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
455
429
)
456
- notification = create_sample_notification (
457
- notify_db , notify_db_session , status = 'sending' , sent_at = datetime .utcnow ()
458
- )
430
+ sample_notification .status = 'sending'
431
+ sample_notification .sent_at = datetime .now ()
459
432
460
433
data = json .dumps ({"reference" : "mmg_reference" ,
461
- "CID" : str (notification .id ),
434
+ "CID" : str (sample_notification .id ),
462
435
"MSISDN" : "447777349060" ,
463
436
"status" : "3" ,
464
437
"deliverytime" : "2016-04-05 16:01:07" })
@@ -467,28 +440,27 @@ def test_process_mmg_response_records_statsd(notify_db, notify_db_session, clien
467
440
468
441
app .statsd_client .incr .assert_any_call ("callback.mmg.delivered" )
469
442
app .statsd_client .timing_with_dates .assert_any_call (
470
- "callback.mmg.elapsed-time" , datetime .utcnow (), notification .sent_at
443
+ "callback.mmg.elapsed-time" , datetime .utcnow (), sample_notification .sent_at
471
444
)
472
445
473
446
474
- def test_firetext_callback_should_record_statsd (client , notify_db , notify_db_session , mocker ):
447
+ def test_firetext_callback_should_record_statsd (client , sample_notification , mocker ):
475
448
with freeze_time ('2001-01-01T12:00:00' ):
476
449
477
450
mocker .patch ('app.statsd_client.incr' )
478
451
mocker .patch ('app.statsd_client.timing_with_dates' )
479
452
mocker .patch (
480
453
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
481
454
)
482
- notification = create_sample_notification (
483
- notify_db , notify_db_session , status = 'sending' , sent_at = datetime .utcnow ()
484
- )
455
+ sample_notification .status = 'sending'
456
+ sample_notification .sent_at = datetime .now ()
485
457
486
458
data = 'mobile=441234123123&status=0&time=2016-03-10 14:17:00&code=101&reference={}' .format (
487
- notification .id )
459
+ sample_notification .id )
488
460
firetext_post (client , data )
489
461
490
462
app .statsd_client .timing_with_dates .assert_any_call (
491
- "callback.firetext.elapsed-time" , datetime .utcnow (), notification .sent_at
463
+ "callback.firetext.elapsed-time" , datetime .utcnow (), sample_notification .sent_at
492
464
)
493
465
app .statsd_client .incr .assert_any_call ("callback.firetext.delivered" )
494
466
0 commit comments