From 3530d26ba3d356533c6f148cb8f162679f419677 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Thu, 16 Dec 2021 14:26:17 +0000 Subject: [PATCH] Use `client` fixture everywhere There were a few tests which weren't using the `client` fixture but were using the code it contains. This simplifies them to use the fixture. --- .../rest/test_send_notification.py | 74 +++++++++---------- tests/app/service/test_api_key_endpoints.py | 27 ++++--- tests/app/service/test_rest.py | 69 +++++++++-------- 3 files changed, 82 insertions(+), 88 deletions(-) diff --git a/tests/app/notifications/rest/test_send_notification.py b/tests/app/notifications/rest/test_send_notification.py index ff2076119..4d1a54845 100644 --- a/tests/app/notifications/rest/test_send_notification.py +++ b/tests/app/notifications/rest/test_send_notification.py @@ -467,56 +467,54 @@ def test_should_not_return_html_in_body(notify_api, sample_service, mocker): assert json.loads(response.get_data(as_text=True))['data']['body'] == 'hello\nthere' -def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api, sample_email_template, mocker): - with notify_api.test_request_context(), notify_api.test_client() as client: - mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') - data = { - 'to': "not-someone-we-trust@email-address.com", - 'template': str(sample_email_template.id), - } +def test_should_not_send_email_if_team_api_key_and_not_a_service_user(client, sample_email_template, mocker): + mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') + data = { + 'to': "not-someone-we-trust@email-address.com", + 'template': str(sample_email_template.id), + } - auth_header = create_service_authorization_header( - service_id=sample_email_template.service_id, key_type=KEY_TYPE_TEAM - ) + auth_header = create_service_authorization_header( + service_id=sample_email_template.service_id, key_type=KEY_TYPE_TEAM + ) - response = client.post( - path='/notifications/email', - data=json.dumps(data), - headers=[('Content-Type', 'application/json'), auth_header]) + response = client.post( + path='/notifications/email', + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) - json_resp = json.loads(response.get_data(as_text=True)) + json_resp = json.loads(response.get_data(as_text=True)) - app.celery.provider_tasks.deliver_email.apply_async.assert_not_called() + app.celery.provider_tasks.deliver_email.apply_async.assert_not_called() - assert response.status_code == 400 - assert [ - 'Can’t send to this recipient using a team-only API key' - ] == json_resp['message']['to'] + assert response.status_code == 400 + assert [ + 'Can’t send to this recipient using a team-only API key' + ] == json_resp['message']['to'] -def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(notify_api, sample_template, mocker): - with notify_api.test_request_context(), notify_api.test_client() as client: - mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') +def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(client, sample_template, mocker): + mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') - data = { - 'to': '07123123123', - 'template': str(sample_template.id), - } + data = { + 'to': '07123123123', + 'template': str(sample_template.id), + } - auth_header = create_service_authorization_header(service_id=sample_template.service_id, key_type=KEY_TYPE_TEAM) + auth_header = create_service_authorization_header(service_id=sample_template.service_id, key_type=KEY_TYPE_TEAM) - response = client.post( - path='/notifications/sms', - data=json.dumps(data), - headers=[('Content-Type', 'application/json'), auth_header]) + response = client.post( + path='/notifications/sms', + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) - json_resp = json.loads(response.get_data(as_text=True)) - app.celery.provider_tasks.deliver_sms.apply_async.assert_not_called() + json_resp = json.loads(response.get_data(as_text=True)) + app.celery.provider_tasks.deliver_sms.apply_async.assert_not_called() - assert response.status_code == 400 - assert [ - 'Can’t send to this recipient using a team-only API key' - ] == json_resp['message']['to'] + assert response.status_code == 400 + assert [ + 'Can’t send to this recipient using a team-only API key' + ] == json_resp['message']['to'] def test_should_send_email_if_team_api_key_and_a_service_user(client, sample_email_template, fake_uuid, mocker): diff --git a/tests/app/service/test_api_key_endpoints.py b/tests/app/service/test_api_key_endpoints.py index 3e7980306..b5685c03b 100644 --- a/tests/app/service/test_api_key_endpoints.py +++ b/tests/app/service/test_api_key_endpoints.py @@ -38,20 +38,19 @@ def test_api_key_should_return_error_when_service_does_not_exist(notify_api, sam assert response.status_code == 404 -def test_create_api_key_without_key_type_rejects(notify_api, sample_service): - with notify_api.test_request_context(), notify_api.test_client() as client: - data = { - 'name': 'some secret name', - 'created_by': str(sample_service.created_by.id) - } - auth_header = create_admin_authorization_header() - response = client.post(url_for('service.create_api_key', service_id=sample_service.id), - data=json.dumps(data), - headers=[('Content-Type', 'application/json'), auth_header]) - assert response.status_code == 400 - json_resp = json.loads(response.get_data(as_text=True)) - assert json_resp['result'] == 'error' - assert json_resp['message'] == {'key_type': ['Missing data for required field.']} +def test_create_api_key_without_key_type_rejects(client, sample_service): + data = { + 'name': 'some secret name', + 'created_by': str(sample_service.created_by.id) + } + auth_header = create_admin_authorization_header() + response = client.post(url_for('service.create_api_key', service_id=sample_service.id), + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) + assert response.status_code == 400 + json_resp = json.loads(response.get_data(as_text=True)) + assert json_resp['result'] == 'error' + assert json_resp['message'] == {'key_type': ['Missing data for required field.']} def test_revoke_should_expire_api_key_for_service(notify_api, sample_api_key): diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 90cd27a9d..ac0f2a0b9 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -1681,33 +1681,32 @@ def test_get_service_and_api_key_history(notify_api, sample_service, sample_api_ assert json_resp['data']['api_key_history'][0]['id'] == str(sample_api_key.id) -def test_get_all_notifications_for_service_in_order(notify_api, notify_db_session): - with notify_api.test_request_context(), notify_api.test_client() as client: - service_1 = create_service(service_name="1", email_from='1') - service_2 = create_service(service_name="2", email_from='2') +def test_get_all_notifications_for_service_in_order(client, notify_db_session): + service_1 = create_service(service_name="1", email_from='1') + service_2 = create_service(service_name="2", email_from='2') - service_1_template = create_template(service_1) - service_2_template = create_template(service_2) + service_1_template = create_template(service_1) + service_2_template = create_template(service_2) - # create notification for service_2 - create_notification(service_2_template) + # create notification for service_2 + create_notification(service_2_template) - notification_1 = create_notification(service_1_template) - notification_2 = create_notification(service_1_template) - notification_3 = create_notification(service_1_template) + notification_1 = create_notification(service_1_template) + notification_2 = create_notification(service_1_template) + notification_3 = create_notification(service_1_template) - auth_header = create_admin_authorization_header() + auth_header = create_admin_authorization_header() - response = client.get( - path='/service/{}/notifications'.format(service_1.id), - headers=[auth_header]) + response = client.get( + path='/service/{}/notifications'.format(service_1.id), + headers=[auth_header]) - resp = json.loads(response.get_data(as_text=True)) - assert len(resp['notifications']) == 3 - assert resp['notifications'][0]['to'] == notification_3.to - assert resp['notifications'][1]['to'] == notification_2.to - assert resp['notifications'][2]['to'] == notification_1.to - assert response.status_code == 200 + resp = json.loads(response.get_data(as_text=True)) + assert len(resp['notifications']) == 3 + assert resp['notifications'][0]['to'] == notification_3.to + assert resp['notifications'][1]['to'] == notification_2.to + assert resp['notifications'][2]['to'] == notification_1.to + assert response.status_code == 200 def test_get_all_notifications_for_service_in_order_with_post_request(client, notify_db_session): @@ -2041,15 +2040,14 @@ def test_set_sms_prefixing_for_service_cant_be_none( ('False', {'requested': 2, 'delivered': 1, 'failed': 0}), ('True', {'requested': 1, 'delivered': 0, 'failed': 0}) ], ids=['seven_days', 'today']) -def test_get_detailed_service(sample_template, notify_api, sample_service, today_only, stats): - with notify_api.test_request_context(), notify_api.test_client() as client: - create_ft_notification_status(date(2000, 1, 1), 'sms', sample_service, count=1) - with freeze_time('2000-01-02T12:00:00'): - create_notification(template=sample_template, status='created') - resp = client.get( - '/service/{}?detailed=True&today_only={}'.format(sample_service.id, today_only), - headers=[create_admin_authorization_header()] - ) +def test_get_detailed_service(sample_template, client, sample_service, today_only, stats): + create_ft_notification_status(date(2000, 1, 1), 'sms', sample_service, count=1) + with freeze_time('2000-01-02T12:00:00'): + create_notification(template=sample_template, status='created') + resp = client.get( + '/service/{}?detailed=True&today_only={}'.format(sample_service.id, today_only), + headers=[create_admin_authorization_header()] + ) assert resp.status_code == 200 service = resp.json['data'] @@ -2082,18 +2080,17 @@ def test_get_services_with_detailed_flag(client, sample_template): } -def test_get_services_with_detailed_flag_excluding_from_test_key(notify_api, sample_template): +def test_get_services_with_detailed_flag_excluding_from_test_key(client, sample_template): create_notification(sample_template, key_type=KEY_TYPE_NORMAL) create_notification(sample_template, key_type=KEY_TYPE_TEAM) create_notification(sample_template, key_type=KEY_TYPE_TEST) create_notification(sample_template, key_type=KEY_TYPE_TEST) create_notification(sample_template, key_type=KEY_TYPE_TEST) - with notify_api.test_request_context(), notify_api.test_client() as client: - resp = client.get( - '/service?detailed=True&include_from_test_key=False', - headers=[create_admin_authorization_header()] - ) + resp = client.get( + '/service?detailed=True&include_from_test_key=False', + headers=[create_admin_authorization_header()] + ) assert resp.status_code == 200 data = resp.json['data']