Skip to content

Commit 04c1c35

Browse files
committed
Stop calling fixures as functions in the tests
1 parent 97b807a commit 04c1c35

File tree

7 files changed

+338
-515
lines changed

7 files changed

+338
-515
lines changed

tests/app/invite/test_invite_rest.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from app.models import Notification, SMS_AUTH_TYPE, EMAIL_AUTH_TYPE
55
from tests import create_authorization_header
6+
from tests.app.db import create_invited_user
67

78

89
@pytest.mark.parametrize('extra_args, expected_start_of_invite_url', [
@@ -118,16 +119,11 @@ def test_create_invited_user_invalid_email(client, sample_service, mocker, fake_
118119

119120

120121
def test_get_all_invited_users_by_service(client, notify_db, notify_db_session, sample_service):
121-
122-
from tests.app.conftest import sample_invited_user
123122
invites = []
124123
for i in range(0, 5):
125124
email = 'invited_user_{}@service.gov.uk'.format(i)
125+
invited_user = create_invited_user(sample_service, to_email_address=email)
126126

127-
invited_user = sample_invited_user(notify_db,
128-
notify_db_session,
129-
sample_service,
130-
email)
131127
invites.append(invited_user)
132128

133129
url = '/service/{}/invite'.format(sample_service.id)

tests/app/notifications/rest/test_callbacks.py

Lines changed: 54 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
from app.dao.notifications_dao import (
1212
get_notification_by_id
1313
)
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
1615

1716

1817
def firetext_post(client, data):
@@ -161,15 +160,13 @@ def test_firetext_callback_should_return_400_if_no_status(client, mocker):
161160

162161

163162
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)
170167
with pytest.raises(ClientException) as e:
171168
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'
173170
assert 'Firetext callback failed: status 99 not found.' in str(e.value)
174171

175172

@@ -201,52 +198,40 @@ def test_callback_should_return_200_if_cannot_find_notification_id(
201198

202199

203200
def test_firetext_callback_should_update_notification_status(
204-
notify_db, notify_db_session, client, sample_email_template, mocker
201+
client, mocker, sample_notification
205202
):
206203
mocker.patch('app.statsd_client.incr')
207204
send_mock = mocker.patch(
208205
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
209206
)
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'
217208

218-
original = get_notification_by_id(notification.id)
209+
original = get_notification_by_id(sample_notification.id)
219210
assert original.status == 'sending'
220211
data = 'mobile=441234123123&status=0&time=2016-03-10 14:17:00&reference={}'.format(
221-
notification.id)
212+
sample_notification.id)
222213
response = firetext_post(client, data)
223214

224215
json_resp = json.loads(response.get_data(as_text=True))
225216
assert response.status_code == 200
226217
assert json_resp['result'] == 'success'
227218
assert json_resp['message'] == 'Firetext callback succeeded. reference {} updated'.format(
228-
notification.id
219+
sample_notification.id
229220
)
230-
updated = get_notification_by_id(notification.id)
221+
updated = get_notification_by_id(sample_notification.id)
231222
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")
234225

235226

236227
def test_firetext_callback_should_update_notification_status_failed(
237-
notify_db, notify_db_session, client, sample_template, mocker
228+
client, mocker, sample_template
238229
):
239230
mocker.patch('app.statsd_client.incr')
240231
mocker.patch(
241232
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
242233
)
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')
250235

251236
original = get_notification_by_id(notification.id)
252237
assert original.status == 'sending'
@@ -264,14 +249,13 @@ def test_firetext_callback_should_update_notification_status_failed(
264249
assert get_notification_by_id(notification.id).status == 'permanent-failure'
265250

266251

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):
268253
mocker.patch('app.statsd_client.incr')
269254
mocker.patch(
270255
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
271256
)
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+
275259
original = get_notification_by_id(notification.id)
276260
assert original.status == 'sending'
277261
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):
299283

300284

301285
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
303287
):
304288
mocker.patch(
305289
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
306290
)
307-
notification = create_sample_notification(
308-
notify_db, notify_db_session, status='sending', sent_at=datetime.utcnow()
309-
)
291+
sample_notification.status = 'sending'
310292
data = json.dumps({"reference": "mmg_reference",
311-
"CID": str(notification.id),
293+
"CID": str(sample_notification.id),
312294
"MSISDN": "447777349060",
313295
"status": "3",
314296
"deliverytime": "2016-04-05 16:01:07"})
@@ -318,96 +300,88 @@ def test_process_mmg_response_returns_200_when_cid_is_valid_notification_id(
318300
assert response.status_code == 200
319301
json_data = json.loads(response.data)
320302
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'
323305

324306

325307
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
327309
):
328310
mocker.patch(
329311
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
330312
)
331-
notification = create_sample_notification(
332-
notify_db, notify_db_session, status='sending', sent_at=datetime.utcnow()
333-
)
313+
sample_notification.status = 'sending'
334314

335315
data = json.dumps({"reference": "mmg_reference",
336-
"CID": str(notification.id),
316+
"CID": str(sample_notification.id),
337317
"MSISDN": "447777349060",
338318
"status": 5})
339319

340320
response = mmg_post(client, data)
341321
assert response.status_code == 200
342322
json_data = json.loads(response.data)
343323
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'
346326

347327

348328
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
350330
):
351331
mocker.patch(
352332
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
353333
)
354-
notification = create_sample_notification(
355-
notify_db, notify_db_session, status='sending', sent_at=datetime.utcnow()
356-
)
334+
sample_notification.status = 'sending'
357335
data = json.dumps({"reference": "mmg_reference",
358-
"CID": str(notification.id),
336+
"CID": str(sample_notification.id),
359337
"MSISDN": "447777349060",
360338
"status": 2})
361339

362340
response = mmg_post(client, data)
363341
assert response.status_code == 200
364342
json_data = json.loads(response.data)
365343
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'
368346

369347

370348
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
372350
):
373351
mocker.patch(
374352
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
375353
)
376-
notification = create_sample_notification(
377-
notify_db, notify_db_session, status='sending', sent_at=datetime.utcnow()
378-
)
354+
sample_notification.status = 'sending'
379355

380356
data = json.dumps({"reference": "mmg_reference",
381-
"CID": str(notification.id),
357+
"CID": str(sample_notification.id),
382358
"MSISDN": "447777349060",
383359
"status": 4})
384360

385361
response = mmg_post(client, data)
386362
assert response.status_code == 200
387363
json_data = json.loads(response.data)
388364
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'
391367

392368

393369
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
395371
):
396372
send_mock = mocker.patch(
397373
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
398374
)
399-
notification = create_sample_notification(
400-
notify_db, notify_db_session, status='sending', sent_at=datetime.utcnow()
401-
)
375+
sample_notification.status = 'sending'
402376
data = json.dumps({"reference": "mmg_reference",
403-
"CID": str(notification.id),
377+
"CID": str(sample_notification.id),
404378
"MSISDN": "447777349060",
405379
"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")
407381
with pytest.raises(ClientException) as e:
408382
mmg_post(client, data)
409383
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'
411385
assert send_mock.called
412386

413387

@@ -445,20 +419,19 @@ def test_mmg_callback_returns_400_when_notification_id_is_not_a_valid_uuid(clien
445419
assert json_resp['message'] == 'MMG callback with invalid reference 1234'
446420

447421

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):
449423
with freeze_time('2001-01-01T12:00:00'):
450424

451425
mocker.patch('app.statsd_client.incr')
452426
mocker.patch('app.statsd_client.timing_with_dates')
453427
mocker.patch(
454428
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
455429
)
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()
459432

460433
data = json.dumps({"reference": "mmg_reference",
461-
"CID": str(notification.id),
434+
"CID": str(sample_notification.id),
462435
"MSISDN": "447777349060",
463436
"status": "3",
464437
"deliverytime": "2016-04-05 16:01:07"})
@@ -467,28 +440,27 @@ def test_process_mmg_response_records_statsd(notify_db, notify_db_session, clien
467440

468441
app.statsd_client.incr.assert_any_call("callback.mmg.delivered")
469442
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
471444
)
472445

473446

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):
475448
with freeze_time('2001-01-01T12:00:00'):
476449

477450
mocker.patch('app.statsd_client.incr')
478451
mocker.patch('app.statsd_client.timing_with_dates')
479452
mocker.patch(
480453
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
481454
)
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()
485457

486458
data = 'mobile=441234123123&status=0&time=2016-03-10 14:17:00&code=101&reference={}'.format(
487-
notification.id)
459+
sample_notification.id)
488460
firetext_post(client, data)
489461

490462
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
492464
)
493465
app.statsd_client.incr.assert_any_call("callback.firetext.delivered")
494466

0 commit comments

Comments
 (0)