diff --git a/tests/app/service/send_notification/test_send_notification.py b/tests/app/service/send_notification/test_send_notification.py index 74db646a0..e940dfe54 100644 --- a/tests/app/service/send_notification/test_send_notification.py +++ b/tests/app/service/send_notification/test_send_notification.py @@ -99,7 +99,7 @@ def test_send_notification_invalid_template_id( with notify_api.test_request_context(): with notify_api.test_client() as client: mocked = mocker.patch( - "app.celery.provider_tasks.deliver_{}.apply_async".format(template_type) + f"app.celery.provider_tasks.deliver_{template_type}.apply_async" ) data = {"to": to, "template": fake_uuid} @@ -108,7 +108,7 @@ def test_send_notification_invalid_template_id( ) response = client.post( - path="/notifications/{}".format(template_type), + path=f"/notifications/{template_type}", data=json.dumps(data), headers=[("Content-Type", "application/json"), auth_header], ) @@ -653,7 +653,7 @@ def test_should_send_sms_to_anyone_with_test_key( data=json.dumps(data), headers=[ ("Content-Type", "application/json"), - ("Authorization", "Bearer {}".format(auth_header)), + ("Authorization", f"Bearer {auth_header}"), ], ) app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with( @@ -691,7 +691,7 @@ def test_should_send_email_to_anyone_with_test_key( data=json.dumps(data), headers=[ ("Content-Type", "application/json"), - ("Authorization", "Bearer {}".format(auth_header)), + ("Authorization", f"Bearer {auth_header}"), ], ) @@ -756,7 +756,7 @@ def test_should_persist_notification( queue_name, ): mocked = mocker.patch( - "app.celery.provider_tasks.deliver_{}.apply_async".format(template_type) + f"app.celery.provider_tasks.deliver_{template_type}.apply_async" ) mocker.patch( "app.notifications.process_notifications.uuid.uuid4", return_value=fake_uuid @@ -843,11 +843,11 @@ def test_should_delete_notification_and_return_error_if_redis_fails( with pytest.raises(expected_exception=Exception) as e: client.post( - path="/notifications/{}".format(template_type), + path=f"/notifications/{template_type}", data=json.dumps(data), headers=[ ("Content-Type", "application/json"), - ("Authorization", "Bearer {}".format(auth_header)), + ("Authorization", f"Bearer {auth_header}"), ], ) assert str(e.value) == "failed to talk to redis" @@ -926,7 +926,7 @@ def test_should_not_send_notification_to_non_guest_list_recipient_in_trial_mode( service.message_limit = 2 apply_async = mocker.patch( - "app.celery.provider_tasks.deliver_{}.apply_async".format(notification_type) + f"app.celery.provider_tasks.deliver_{notification_type}.apply_async" ) template = create_template(service, template_type=notification_type) assert sample_service_guest_list.service_id == service.id @@ -946,7 +946,7 @@ def test_should_not_send_notification_to_non_guest_list_recipient_in_trial_mode( data=json.dumps(data), headers=[ ("Content-Type", "application/json"), - ("Authorization", "Bearer {}".format(auth_header)), + ("Authorization", f"Bearer {auth_header}"), ], ) @@ -989,7 +989,7 @@ def test_should_send_notification_to_guest_list_recipient( sample_service.restricted = service_restricted apply_async = mocker.patch( - "app.celery.provider_tasks.deliver_{}.apply_async".format(notification_type) + f"app.celery.provider_tasks.deliver_{notification_type}.apply_async" ) template = create_template(sample_service, template_type=notification_type) if notification_type == NotificationType.SMS: @@ -1016,7 +1016,7 @@ def test_should_send_notification_to_guest_list_recipient( data=json.dumps(data), headers=[ ("Content-Type", "application/json"), - ("Authorization", "Bearer {}".format(auth_header)), + ("Authorization", f"Bearer {auth_header}"), ], ) @@ -1119,7 +1119,14 @@ def test_create_template_raises_invalid_request_when_content_too_large( @pytest.mark.parametrize( - "notification_type, send_to", [("sms", "2028675309"), ("email", "sample@email.com")] + "notification_type,send_to", + [ + (NotificationType.SMS, "2028675309"), + ( + NotificationType.EMAIL, + "sample@email.com", + ), + ], ) def test_send_notification_uses_priority_queue_when_template_is_marked_as_priority( client, @@ -1132,7 +1139,7 @@ def test_send_notification_uses_priority_queue_when_template_is_marked_as_priori sample_service, template_type=notification_type, process_type="priority" ) mocked = mocker.patch( - "app.celery.provider_tasks.deliver_{}.apply_async".format(notification_type) + f"app.celery.provider_tasks.deliver_{notification_type}.apply_async" ) data = {"to": send_to, "template": str(sample.id)} @@ -1140,7 +1147,7 @@ def test_send_notification_uses_priority_queue_when_template_is_marked_as_priori auth_header = create_service_authorization_header(service_id=sample.service_id) response = client.post( - path="/notifications/{}".format(notification_type), + path=f"/notifications/{notification_type}", data=json.dumps(data), headers=[("Content-Type", "application/json"), auth_header], ) @@ -1153,7 +1160,14 @@ def test_send_notification_uses_priority_queue_when_template_is_marked_as_priori @pytest.mark.parametrize( - "notification_type, send_to", [("sms", "2028675309"), ("email", "sample@email.com")] + "notification_type, send_to", + [ + (NotificationType.SMS, "2028675309"), + ( + NotificationType.EMAIL, + "sample@email.com", + ), + ], ) def test_returns_a_429_limit_exceeded_if_rate_limit_exceeded( client, sample_service, mocker, notification_type, send_to @@ -1172,7 +1186,7 @@ def test_returns_a_429_limit_exceeded_if_rate_limit_exceeded( auth_header = create_service_authorization_header(service_id=sample.service_id) response = client.post( - path="/notifications/{}".format(notification_type), + path=f"/notifications/{notification_type}", data=json.dumps(data), headers=[("Content-Type", "application/json"), auth_header], ) @@ -1181,9 +1195,9 @@ def test_returns_a_429_limit_exceeded_if_rate_limit_exceeded( result = json.loads(response.data)["result"] assert response.status_code == 429 assert result == "error" - assert ( - message - == "Exceeded rate limit for key type TYPE of LIMIT requests per INTERVAL seconds" + assert message == ( + "Exceeded rate limit for key type TYPE of LIMIT " + "requests per INTERVAL seconds" ) assert not persist_mock.called @@ -1332,7 +1346,7 @@ def test_should_throw_exception_if_notification_type_is_invalid( ): auth_header = create_service_authorization_header(service_id=sample_service.id) response = client.post( - path="/notifications/{}".format(notification_type), + path=f"/notifications/{notification_type}", data={}, headers=[("Content-Type", "application/json"), auth_header], ) @@ -1341,14 +1355,19 @@ def test_should_throw_exception_if_notification_type_is_invalid( @pytest.mark.parametrize( - "notification_type, recipient", [("sms", "2028675309"), ("email", "test@gov.uk")] + "notification_type, recipient", + [ + (NotificationType.SMS, "2028675309"), + ( + NotificationType.EMAIL, + "test@gov.uk", + ), + ], ) def test_post_notification_should_set_reply_to_text( client, sample_service, mocker, notification_type, recipient ): - mocker.patch( - "app.celery.provider_tasks.deliver_{}.apply_async".format(notification_type) - ) + mocker.patch(f"app.celery.provider_tasks.deliver_{notification_type}.apply_async") template = create_template(sample_service, template_type=notification_type) expected_reply_to = current_app.config["FROM_NUMBER"] if notification_type == NotificationType.EMAIL: @@ -1359,7 +1378,7 @@ def test_post_notification_should_set_reply_to_text( data = {"to": recipient, "template": str(template.id)} response = client.post( - "/notifications/{}".format(notification_type), + f"/notifications/{notification_type}", data=json.dumps(data), headers=[ ("Content-Type", "application/json"), diff --git a/tests/app/service/send_notification/test_send_one_off_notification.py b/tests/app/service/send_notification/test_send_one_off_notification.py index 9e342369d..231b42be0 100644 --- a/tests/app/service/send_notification/test_send_one_off_notification.py +++ b/tests/app/service/send_notification/test_send_one_off_notification.py @@ -11,6 +11,7 @@ from app.enums import ( KeyType, NotificationType, RecipientType, + ServicePermissionType, TemplateProcessType, TemplateType, ) @@ -100,7 +101,12 @@ def test_send_one_off_notification_calls_persist_correctly_for_sms( def test_send_one_off_notification_calls_persist_correctly_for_international_sms( persist_mock, celery_mock, notify_db_session ): - service = create_service(service_permissions=["sms", "international_sms"]) + service = create_service( + service_permissions=[ + ServicePermissionType.SMS, + ServicePermissionType.INTERNATIONAL_SMS, + ], + ) template = create_template( service=service, template_type=TemplateType.SMS, diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index 311d0e4d7..7691d8966 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -9,7 +9,7 @@ from freezegun import freeze_time from notifications_utils import SMS_CHAR_COUNT_LIMIT from app.dao.templates_dao import dao_get_template_by_id, dao_redact_template -from app.enums import ServicePermissionType, TemplateType +from app.enums import ServicePermissionType, TemplateProcessType, TemplateType from app.models import Template, TemplateHistory from tests import create_admin_authorization_header from tests.app.db import create_service, create_template, create_template_folder @@ -39,7 +39,7 @@ def test_should_create_a_new_template_for_a_service( auth_header = create_admin_authorization_header() response = client.post( - "/service/{}/template".format(service.id), + f"/service/{service.id}/template", headers=[("Content-Type", "application/json"), auth_header], data=data, ) @@ -51,7 +51,7 @@ def test_should_create_a_new_template_for_a_service( assert json_resp["data"]["service"] == str(service.id) assert json_resp["data"]["id"] assert json_resp["data"]["version"] == 1 - assert json_resp["data"]["process_type"] == "normal" + assert json_resp["data"]["process_type"] == TemplateProcessType.NORMAL assert json_resp["data"]["created_by"] == str(sample_user.id) if subject: assert json_resp["data"]["subject"] == "subject" @@ -71,7 +71,7 @@ def test_create_a_new_template_for_a_service_adds_folder_relationship( data = { "name": "my template", - "template_type": "sms", + "template_type": TemplateType.SMS, "content": "template content", "service": str(sample_service.id), "created_by": str(sample_service.users[0].id), @@ -81,7 +81,7 @@ def test_create_a_new_template_for_a_service_adds_folder_relationship( auth_header = create_admin_authorization_header() response = client.post( - "/service/{}/template".format(sample_service.id), + f"/service/{sample_service.id}/template", headers=[("Content-Type", "application/json"), auth_header], data=data, ) @@ -98,7 +98,7 @@ def test_create_template_should_return_400_if_folder_is_for_a_different_service( data = { "name": "my template", - "template_type": "sms", + "template_type": TemplateType.SMS, "content": "template content", "service": str(sample_service.id), "created_by": str(sample_service.users[0].id), @@ -108,7 +108,7 @@ def test_create_template_should_return_400_if_folder_is_for_a_different_service( auth_header = create_admin_authorization_header() response = client.post( - "/service/{}/template".format(sample_service.id), + f"/service/{sample_service.id}/template", headers=[("Content-Type", "application/json"), auth_header], data=data, ) @@ -124,7 +124,7 @@ def test_create_template_should_return_400_if_folder_does_not_exist( ): data = { "name": "my template", - "template_type": "sms", + "template_type": TemplateType.SMS, "content": "template content", "service": str(sample_service.id), "created_by": str(sample_service.users[0].id), @@ -134,7 +134,7 @@ def test_create_template_should_return_400_if_folder_does_not_exist( auth_header = create_admin_authorization_header() response = client.post( - "/service/{}/template".format(sample_service.id), + f"/service/{sample_service.id}/template", headers=[("Content-Type", "application/json"), auth_header], data=data, ) @@ -159,7 +159,7 @@ def test_should_raise_error_if_service_does_not_exist_on_create( auth_header = create_admin_authorization_header() response = client.post( - "/service/{}/template".format(fake_uuid), + f"/service/{fake_uuid}/template", headers=[("Content-Type", "application/json"), auth_header], data=data, ) @@ -204,7 +204,7 @@ def test_should_raise_error_on_create_if_no_permission( auth_header = create_admin_authorization_header() response = client.post( - "/service/{}/template".format(service.id), + f"/service/{service.id}/template", headers=[("Content-Type", "application/json"), auth_header], data=data, ) @@ -245,9 +245,7 @@ def test_should_be_error_on_update_if_no_permission( auth_header = create_admin_authorization_header() update_response = client.post( - "/service/{}/template/{}".format( - template_without_permission.service_id, template_without_permission.id - ), + f"/service/{template_without_permission.service_id}/template/{template_without_permission.id}", headers=[("Content-Type", "application/json"), auth_header], data=data, ) @@ -270,7 +268,7 @@ def test_should_error_if_created_by_missing(client, sample_user, sample_service) auth_header = create_admin_authorization_header() response = client.post( - "/service/{}/template".format(service_id), + f"/service/{service_id}/template", headers=[("Content-Type", "application/json"), auth_header], data=data, ) @@ -286,7 +284,7 @@ def test_should_be_error_if_service_does_not_exist_on_update(client, fake_uuid): auth_header = create_admin_authorization_header() response = client.post( - "/service/{}/template/{}".format(fake_uuid, fake_uuid), + f"/service/{fake_uuid}/template/{fake_uuid}", headers=[("Content-Type", "application/json"), auth_header], data=data, ) @@ -322,7 +320,7 @@ def test_must_have_a_subject_on_an_email_template( def test_update_should_update_a_template(client, sample_user): service = create_service() - template = create_template(service, template_type="sms") + template = create_template(service, template_type=TemplateType.SMS) assert template.created_by == service.created_by assert template.created_by != sample_user @@ -335,7 +333,7 @@ def test_update_should_update_a_template(client, sample_user): auth_header = create_admin_authorization_header() update_response = client.post( - "/service/{}/template/{}".format(service.id, template.id), + f"/service/{service.id}/template/{template.id}", headers=[("Content-Type", "application/json"), auth_header], data=data, ) @@ -373,9 +371,7 @@ def test_should_be_able_to_archive_template(client, sample_template): auth_header = create_admin_authorization_header() resp = client.post( - "/service/{}/template/{}".format( - sample_template.service.id, sample_template.id - ), + f"/service/{sample_template.service.id}/template/{sample_template.id}", headers=[("Content-Type", "application/json"), auth_header], data=json_data, ) @@ -431,14 +427,14 @@ def test_should_be_able_to_get_all_templates_for_a_service( data_2 = json.dumps(data) auth_header = create_admin_authorization_header() client.post( - "/service/{}/template".format(sample_service.id), + f"/service/{sample_service.id}/template", headers=[("Content-Type", "application/json"), auth_header], data=data_1, ) auth_header = create_admin_authorization_header() client.post( - "/service/{}/template".format(sample_service.id), + f"/service/{sample_service.id}/template", headers=[("Content-Type", "application/json"), auth_header], data=data_2, ) @@ -446,7 +442,7 @@ def test_should_be_able_to_get_all_templates_for_a_service( auth_header = create_admin_authorization_header() response = client.get( - "/service/{}/template".format(sample_service.id), headers=[auth_header] + f"/service/{sample_service.id}/template", headers=[auth_header] ) assert response.status_code == 200 @@ -467,10 +463,12 @@ def test_should_get_only_templates_for_that_service(admin_request, notify_db_ses id_3 = create_template(service_2).id json_resp_1 = admin_request.get( - "template.get_all_templates_for_service", service_id=service_1.id + "template.get_all_templates_for_service", + service_id=service_1.id, ) json_resp_2 = admin_request.get( - "template.get_all_templates_for_service", service_id=service_2.id + "template.get_all_templates_for_service", + service_id=service_2.id, ) assert {template["id"] for template in json_resp_1["data"]} == { @@ -580,7 +578,7 @@ def test_should_get_a_single_template( ) response = client.get( - "/service/{}/template/{}".format(sample_service.id, template.id), + f"/service/{sample_service.id}/template/{template.id}", headers=[create_admin_authorization_header()], ) @@ -667,7 +665,7 @@ def test_should_return_empty_array_if_no_templates_for_service(client, sample_se auth_header = create_admin_authorization_header() response = client.get( - "/service/{}/template".format(sample_service.id), headers=[auth_header] + f"/service/{sample_service.id}/template", headers=[auth_header] ) assert response.status_code == 200 @@ -681,7 +679,7 @@ def test_should_return_404_if_no_templates_for_service_with_id( auth_header = create_admin_authorization_header() response = client.get( - "/service/{}/template/{}".format(sample_service.id, fake_uuid), + f"/service/{sample_service.id}/template/{fake_uuid}", headers=[auth_header], ) @@ -715,7 +713,7 @@ def test_create_400_for_over_limit_content( auth_header = create_admin_authorization_header() response = client.post( - "/service/{}/template".format(sample_service.id), + f"/service/{sample_service.id}/template", headers=[("Content-Type", "application/json"), auth_header], data=data, ) @@ -740,9 +738,7 @@ def test_update_400_for_over_limit_content( ) auth_header = create_admin_authorization_header() resp = client.post( - "/service/{}/template/{}".format( - sample_template.service.id, sample_template.id - ), + f"/service/{sample_template.service.id}/template/{sample_template.id}", headers=[("Content-Type", "application/json"), auth_header], data=json_data, ) @@ -766,9 +762,7 @@ def test_should_return_all_template_versions_for_service_and_template_id( auth_header = create_admin_authorization_header() resp = client.get( - "/service/{}/template/{}/versions".format( - sample_template.service_id, sample_template.id - ), + f"/service/{sample_template.service_id}/template/{sample_template.id}/versions", headers=[("Content-Type", "application/json"), auth_header], ) assert resp.status_code == 200 @@ -792,9 +786,7 @@ def test_update_does_not_create_new_version_when_there_is_no_change( "content": sample_template.content, } resp = client.post( - "/service/{}/template/{}".format( - sample_template.service_id, sample_template.id - ), + f"/service/{sample_template.service_id}/template/{sample_template.id}", data=json.dumps(data), headers=[("Content-Type", "application/json"), auth_header], ) @@ -808,9 +800,7 @@ def test_update_set_process_type_on_template(client, sample_template): auth_header = create_admin_authorization_header() data = {"process_type": "priority"} resp = client.post( - "/service/{}/template/{}".format( - sample_template.service_id, sample_template.id - ), + f"/service/{sample_template.service_id}/template/{sample_template.id}", data=json.dumps(data), headers=[("Content-Type", "application/json"), auth_header], ) diff --git a/tests/app/template_statistics/test_rest.py b/tests/app/template_statistics/test_rest.py index 9148839be..be6b368ab 100644 --- a/tests/app/template_statistics/test_rest.py +++ b/tests/app/template_statistics/test_rest.py @@ -5,6 +5,7 @@ from unittest.mock import Mock import pytest from freezegun import freeze_time +from app.enums import NotificationStatus, TemplateType from app.utils import DATETIME_FORMAT from tests.app.db import create_ft_notification_status, create_notification @@ -48,7 +49,7 @@ def test_get_template_statistics_for_service_by_day_returns_template_info( assert json_resp["data"][0]["count"] == 1 assert json_resp["data"][0]["template_id"] == str(sample_notification.template_id) assert json_resp["data"][0]["template_name"] == "sms Template Name" - assert json_resp["data"][0]["template_type"] == "sms" + assert json_resp["data"][0]["template_type"] == TemplateType.SMS @pytest.mark.parametrize("var_name", ["limit_days", "whole_days"]) @@ -77,7 +78,7 @@ def test_get_template_statistics_for_service_by_day_goes_to_db( count=3, template_name=sample_template.name, notification_type=sample_template.template_type, - status="created", + status=NotificationStatus.CREATED, ) ], ) @@ -93,7 +94,7 @@ def test_get_template_statistics_for_service_by_day_goes_to_db( "count": 3, "template_name": sample_template.name, "template_type": sample_template.template_type, - "status": "created", + "status": NotificationStatus.CREATED, } ] # dao only called for 2nd, since redis returned values for first call