From df866e40f75d680ca1a61110712d2ee00ddc4cce Mon Sep 17 00:00:00 2001 From: Cliff Hill Date: Thu, 18 Jan 2024 10:26:40 -0500 Subject: [PATCH] More Enum goodness. Signed-off-by: Cliff Hill --- app/models.py | 1 + .../notification_dao/test_notification_dao.py | 11 +- .../dao/test_fact_notification_status_dao.py | 3 +- tests/app/dao/test_jobs_dao.py | 6 +- tests/app/dao/test_service_permissions_dao.py | 29 ++-- tests/app/dao/test_services_dao.py | 59 ++++---- tests/app/dao/test_uploads_dao.py | 4 +- .../test_process_notification.py | 6 +- .../test_receive_notification.py | 30 ++-- tests/app/notifications/test_validators.py | 48 ++++--- tests/app/platform_stats/test_rest.py | 9 +- .../test_send_notification.py | 76 ++++++---- .../test_send_one_off_notification.py | 22 +-- tests/app/service/test_rest.py | 135 +++++++++++------- tests/app/service/test_sender.py | 10 +- tests/app/service/test_service_guest_list.py | 12 +- tests/app/service/test_statistics_rest.py | 19 +-- tests/app/template/test_rest.py | 47 +++--- tests/app/user/test_rest_verify.py | 9 +- .../test_notification_schemas.py | 4 +- .../notifications/test_post_notifications.py | 31 ++-- tests/app/v2/template/test_get_template.py | 12 +- tests/app/v2/template/test_post_template.py | 10 +- .../app/v2/template/test_template_schemas.py | 10 +- tests/app/v2/templates/test_get_templates.py | 18 +-- .../v2/templates/test_templates_schemas.py | 36 ++--- 26 files changed, 379 insertions(+), 278 deletions(-) diff --git a/app/models.py b/app/models.py index 29fde4ffd..a656a926c 100644 --- a/app/models.py +++ b/app/models.py @@ -33,6 +33,7 @@ from app.utils import ( class TemplateType(Enum): SMS = "sms" EMAIL = "email" + LETTER = "letter" class NotificationType(Enum): diff --git a/tests/app/dao/notification_dao/test_notification_dao.py b/tests/app/dao/notification_dao/test_notification_dao.py index 13056105a..f167ca65f 100644 --- a/tests/app/dao/notification_dao/test_notification_dao.py +++ b/tests/app/dao/notification_dao/test_notification_dao.py @@ -36,10 +36,10 @@ from app.models import ( NOTIFICATION_DELIVERED, NOTIFICATION_SENT, NOTIFICATION_STATUS_TYPES, - SMS_TYPE, Job, Notification, NotificationHistory, + NotificationType, ) from tests.app.db import ( create_ft_notification_status, @@ -1748,7 +1748,10 @@ def test_get_service_ids_with_notifications_on_date_respects_gmt_bst( sample_template, created_at_utc, date_to_check, expected_count ): create_notification(template=sample_template, created_at=created_at_utc) - service_ids = get_service_ids_with_notifications_on_date(SMS_TYPE, date_to_check) + service_ids = get_service_ids_with_notifications_on_date( + NotificationType.SMS, + date_to_check, + ) assert len(service_ids) == expected_count @@ -1759,8 +1762,8 @@ def test_get_service_ids_with_notifications_on_date_checks_ft_status( create_ft_notification_status(template=sample_template, local_date="2022-01-02") assert ( - len(get_service_ids_with_notifications_on_date(SMS_TYPE, date(2022, 1, 1))) == 1 + len(get_service_ids_with_notifications_on_date(NotificationType.SMS, date(2022, 1, 1))) == 1 ) assert ( - len(get_service_ids_with_notifications_on_date(SMS_TYPE, date(2022, 1, 2))) == 1 + len(get_service_ids_with_notifications_on_date(NotificationType.SMS, date(2022, 1, 2))) == 1 ) diff --git a/tests/app/dao/test_fact_notification_status_dao.py b/tests/app/dao/test_fact_notification_status_dao.py index c5586be34..1f0e6ab84 100644 --- a/tests/app/dao/test_fact_notification_status_dao.py +++ b/tests/app/dao/test_fact_notification_status_dao.py @@ -30,6 +30,7 @@ from app.models import ( NOTIFICATION_TECHNICAL_FAILURE, NOTIFICATION_TEMPORARY_FAILURE, FactNotificationStatus, + NotificationType, TemplateType, ) from tests.app.db import ( @@ -853,7 +854,7 @@ def test_update_fact_notification_status_respects_gmt_bst( expected_count, ): create_notification(template=sample_template, created_at=created_at_utc) - update_fact_notification_status(process_day, SMS_TYPE, sample_service.id) + update_fact_notification_status(process_day, NotificationType.SMS, sample_service.id) assert ( FactNotificationStatus.query.filter_by( diff --git a/tests/app/dao/test_jobs_dao.py b/tests/app/dao/test_jobs_dao.py index 0b1374614..b7afa713c 100644 --- a/tests/app/dao/test_jobs_dao.py +++ b/tests/app/dao/test_jobs_dao.py @@ -18,7 +18,7 @@ from app.dao.jobs_dao import ( find_jobs_with_missing_rows, find_missing_row_for_job, ) -from app.models import JOB_STATUS_FINISHED, SMS_TYPE, Job +from app.models import JOB_STATUS_FINISHED, Job, NotificationType, TemplateType from tests.app.db import ( create_job, create_notification, @@ -344,7 +344,7 @@ def test_get_jobs_for_service_doesnt_return_test_messages( def test_should_get_jobs_seven_days_old_by_scheduled_for_date(sample_service): six_days_ago = datetime.utcnow() - timedelta(days=6) eight_days_ago = datetime.utcnow() - timedelta(days=8) - sms_template = create_template(sample_service, template_type=SMS_TYPE) + sms_template = create_template(sample_service, template_type=TemplateType.SMS) create_job(sms_template, created_at=eight_days_ago) create_job(sms_template, created_at=eight_days_ago, scheduled_for=eight_days_ago) @@ -352,7 +352,7 @@ def test_should_get_jobs_seven_days_old_by_scheduled_for_date(sample_service): sms_template, created_at=eight_days_ago, scheduled_for=six_days_ago ) - jobs = dao_get_jobs_older_than_data_retention(notification_types=[SMS_TYPE]) + jobs = dao_get_jobs_older_than_data_retention(notification_types=[NotificationType.SMS]) assert len(jobs) == 2 assert job_to_remain.id not in [job.id for job in jobs] diff --git a/tests/app/dao/test_service_permissions_dao.py b/tests/app/dao/test_service_permissions_dao.py index d2298aa43..df87575ae 100644 --- a/tests/app/dao/test_service_permissions_dao.py +++ b/tests/app/dao/test_service_permissions_dao.py @@ -4,7 +4,7 @@ from app.dao.service_permissions_dao import ( dao_fetch_service_permissions, dao_remove_service_permission, ) -from app.models import EMAIL_TYPE, INBOUND_SMS_TYPE, INTERNATIONAL_SMS_TYPE, SMS_TYPE +from app.models import ServicePermissionType from tests.app.db import create_service, create_service_permission @@ -15,22 +15,23 @@ def service_without_permissions(notify_db_session): def test_create_service_permission(service_without_permissions): service_permissions = create_service_permission( - service_id=service_without_permissions.id, permission=SMS_TYPE + service_id=service_without_permissions.id, permission=ServicePermissionType.SMS ) assert len(service_permissions) == 1 assert service_permissions[0].service_id == service_without_permissions.id - assert service_permissions[0].permission == SMS_TYPE + assert service_permissions[0].permission == ServicePermissionType.SMS def test_fetch_service_permissions_gets_service_permissions( service_without_permissions, ): create_service_permission( - service_id=service_without_permissions.id, permission=INTERNATIONAL_SMS_TYPE + service_id=service_without_permissions.id, + permission=ServicePermissionType.INTERNATIONAL_SMS, ) create_service_permission( - service_id=service_without_permissions.id, permission=SMS_TYPE + service_id=service_without_permissions.id, permission=ServicePermissionType.SMS ) service_permissions = dao_fetch_service_permissions(service_without_permissions.id) @@ -40,22 +41,30 @@ def test_fetch_service_permissions_gets_service_permissions( sp.service_id == service_without_permissions.id for sp in service_permissions ) assert all( - sp.permission in [INTERNATIONAL_SMS_TYPE, SMS_TYPE] + sp.permission in { + ServicePermissionType.INTERNATIONAL_SMS, + ServicePermissionType.SMS, + } for sp in service_permissions ) def test_remove_service_permission(service_without_permissions): create_service_permission( - service_id=service_without_permissions.id, permission=EMAIL_TYPE + service_id=service_without_permissions.id, + permission=ServicePermissionType.EMAIL, ) create_service_permission( - service_id=service_without_permissions.id, permission=INBOUND_SMS_TYPE + service_id=service_without_permissions.id, + permission=ServicePermissionType.INBOUND_SMS, ) - dao_remove_service_permission(service_without_permissions.id, EMAIL_TYPE) + dao_remove_service_permission( + service_without_permissions.id, + ServicePermissionType.EMAIL, + ) permissions = dao_fetch_service_permissions(service_without_permissions.id) assert len(permissions) == 1 - assert permissions[0].permission == INBOUND_SMS_TYPE + assert permissions[0].permission == ServicePermissionType.INBOUND_SMS assert permissions[0].service_id == service_without_permissions.id diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 41d003ec7..641a274a5 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -41,12 +41,9 @@ from app.dao.services_dao import ( ) from app.dao.users_dao import create_user_code, save_model_user from app.models import ( - EMAIL_TYPE, - INTERNATIONAL_SMS_TYPE, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST, - SMS_TYPE, ApiKey, InvitedUser, Job, @@ -56,6 +53,7 @@ from app.models import ( Permission, Service, ServicePermission, + ServicePermissionType, ServiceUser, Template, TemplateHistory, @@ -601,9 +599,9 @@ def test_create_service_returns_service_with_default_permissions(notify_db_sessi _assert_service_permissions( service.permissions, ( - SMS_TYPE, - EMAIL_TYPE, - INTERNATIONAL_SMS_TYPE, + ServicePermissionType.SMS, + ServicePermissionType.EMAIL, + ServicePermissionType.INTERNATIONAL_SMS, ), ) @@ -612,17 +610,17 @@ def test_create_service_returns_service_with_default_permissions(notify_db_sessi "permission_to_remove, permissions_remaining", [ ( - SMS_TYPE, + ServicePermissionType.SMS, ( - EMAIL_TYPE, - INTERNATIONAL_SMS_TYPE, + ServicePermissionType.EMAIL, + ServicePermissionType.INTERNATIONAL_SMS, ), ), ( - EMAIL_TYPE, + ServicePermissionType.EMAIL, ( - SMS_TYPE, - INTERNATIONAL_SMS_TYPE, + ServicePermissionType.SMS, + ServicePermissionType.INTERNATIONAL_SMS, ), ), ], @@ -641,10 +639,17 @@ def test_remove_permission_from_service_by_id_returns_service_with_correct_permi def test_removing_all_permission_returns_service_with_no_permissions(notify_db_session): service = create_service() - dao_remove_service_permission(service_id=service.id, permission=SMS_TYPE) - dao_remove_service_permission(service_id=service.id, permission=EMAIL_TYPE) dao_remove_service_permission( - service_id=service.id, permission=INTERNATIONAL_SMS_TYPE + service_id=service.id, + permission=ServicePermissionType.SMS, + ) + dao_remove_service_permission( + service_id=service.id, + permission=ServicePermissionType.EMAIL, + ) + dao_remove_service_permission( + service_id=service.id, + permission=ServicePermissionType.INTERNATIONAL_SMS, ) service = dao_fetch_service_by_id(service.id) @@ -734,16 +739,16 @@ def test_update_service_permission_creates_a_history_record_with_current_data( service, user, service_permissions=[ - SMS_TYPE, - # EMAIL_TYPE, - INTERNATIONAL_SMS_TYPE, + ServicePermissionType.SMS, + # ServicePermissionType.EMAIL, + ServicePermissionType.INTERNATIONAL_SMS, ], ) assert Service.query.count() == 1 service.permissions.append( - ServicePermission(service_id=service.id, permission=EMAIL_TYPE) + ServicePermission(service_id=service.id, permission=ServicePermissionType.EMAIL) ) dao_update_service(service) @@ -757,9 +762,9 @@ def test_update_service_permission_creates_a_history_record_with_current_data( _assert_service_permissions( service.permissions, ( - SMS_TYPE, - EMAIL_TYPE, - INTERNATIONAL_SMS_TYPE, + ServicePermissionType.SMS, + ServicePermissionType.EMAIL, + ServicePermissionType.INTERNATIONAL_SMS, ), ) @@ -775,8 +780,8 @@ def test_update_service_permission_creates_a_history_record_with_current_data( _assert_service_permissions( service.permissions, ( - EMAIL_TYPE, - INTERNATIONAL_SMS_TYPE, + ServicePermissionType.EMAIL, + ServicePermissionType.INTERNATIONAL_SMS, ), ) @@ -831,9 +836,9 @@ def test_delete_service_and_associated_objects(notify_db_session): assert ServicePermission.query.count() == len( ( - SMS_TYPE, - EMAIL_TYPE, - INTERNATIONAL_SMS_TYPE, + ServicePermissionType.SMS, + ServicePermissionType.EMAIL, + ServicePermissionType.INTERNATIONAL_SMS, ) ) diff --git a/tests/app/dao/test_uploads_dao.py b/tests/app/dao/test_uploads_dao.py index b0e144960..016d7a92e 100644 --- a/tests/app/dao/test_uploads_dao.py +++ b/tests/app/dao/test_uploads_dao.py @@ -3,7 +3,7 @@ from datetime import datetime, timedelta from freezegun import freeze_time from app.dao.uploads_dao import dao_get_uploads_by_service_id -from app.models import JOB_STATUS_IN_PROGRESS, LETTER_TYPE +from app.models import JOB_STATUS_IN_PROGRESS from tests.app.db import ( create_job, create_notification, @@ -29,7 +29,7 @@ def create_uploaded_letter(letter_template, service, status="created", created_a def create_uploaded_template(service): return create_template( service, - template_type=LETTER_TYPE, + template_type=TemplateType.LETTER, template_name="Pre-compiled PDF", subject="Pre-compiled PDF", content="", diff --git a/tests/app/notifications/test_process_notification.py b/tests/app/notifications/test_process_notification.py index 2e302476a..d0ea87574 100644 --- a/tests/app/notifications/test_process_notification.py +++ b/tests/app/notifications/test_process_notification.py @@ -11,7 +11,7 @@ from notifications_utils.recipients import ( ) from sqlalchemy.exc import SQLAlchemyError -from app.models import SMS_TYPE, Notification, NotificationHistory +from app.models import Notification, NotificationHistory, ServicePermissionType, TemplateType from app.notifications.process_notifications import ( create_content_for_notification, persist_notification, @@ -403,8 +403,8 @@ def test_persist_email_notification_stores_normalised_email( def test_persist_notification_with_billable_units_stores_correct_info(mocker): - service = create_service(service_permissions=[SMS_TYPE]) - template = create_template(service, template_type=SMS_TYPE) + service = create_service(service_permissions=[ServicePermissionType.SMS]) + template = create_template(service, template_type=TemplateType.SMS) mocker.patch("app.dao.templates_dao.dao_get_template_by_id", return_value=template) persist_notification( template_id=template.id, diff --git a/tests/app/notifications/test_receive_notification.py b/tests/app/notifications/test_receive_notification.py index 01ae9e566..fdc1450f2 100644 --- a/tests/app/notifications/test_receive_notification.py +++ b/tests/app/notifications/test_receive_notification.py @@ -5,7 +5,7 @@ from unittest import mock import pytest from flask import json -from app.models import EMAIL_TYPE, INBOUND_SMS_TYPE, SMS_TYPE, InboundSms +from app.models import InboundSms, ServicePermissionType from app.notifications.receive_notifications import ( create_inbound_sms_object, fetch_potential_service, @@ -72,8 +72,8 @@ def test_receive_notification_returns_received_to_sns( @pytest.mark.parametrize( "permissions", [ - [SMS_TYPE], - [INBOUND_SMS_TYPE], + [ServicePermissionType.SMS], + [ServicePermissionType.INBOUND_SMS], ], ) def test_receive_notification_from_sns_without_permissions_does_not_persist( @@ -139,9 +139,9 @@ def test_receive_notification_without_permissions_does_not_create_inbound_even_w @pytest.mark.parametrize( "permissions,expected_response", [ - ([SMS_TYPE, INBOUND_SMS_TYPE], True), - ([INBOUND_SMS_TYPE], False), - ([SMS_TYPE], False), + ([ServicePermissionType.SMS, ServicePermissionType.INBOUND_SMS], True), + ([ServicePermissionType.INBOUND_SMS], False), + ([ServicePermissionType.SMS], False), ], ) def test_check_permissions_for_inbound_sms( @@ -256,12 +256,20 @@ def test_receive_notification_error_if_not_single_matching_service( create_service_with_inbound_number( inbound_number="dog", service_name="a", - service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE], + service_permissions=[ + ServicePermissionType.EMAIL, + ServicePermissionType.SMS, + ServicePermissionType.INBOUND_SMS, + ], ) create_service_with_inbound_number( inbound_number="bar", service_name="b", - service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE], + service_permissions=[ + ServicePermissionType.EMAIL, + ServicePermissionType.SMS, + ServicePermissionType.INBOUND_SMS + ], ) data = { @@ -303,7 +311,11 @@ def test_sns_inbound_sms_auth( create_service_with_inbound_number( service_name="b", inbound_number="07111111111", - service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE], + service_permissions=[ + ServicePermissionType.EMAIL, + ServicePermissionType.SMS, + ServicePermissionType.INBOUND_SMS, + ], ) data = { diff --git a/tests/app/notifications/test_validators.py b/tests/app/notifications/test_validators.py index 9643a0d0f..a3a098e4d 100644 --- a/tests/app/notifications/test_validators.py +++ b/tests/app/notifications/test_validators.py @@ -5,7 +5,7 @@ from notifications_utils import SMS_CHAR_COUNT_LIMIT import app from app.dao import templates_dao -from app.models import EMAIL_TYPE, KEY_TYPE_NORMAL, SMS_TYPE +from app.models import KEY_TYPE_NORMAL, NotificationType, ServicePermissionType, TemplateType from app.notifications.process_notifications import create_content_for_notification from app.notifications.sns_cert_validator import ( VALID_SNS_TOPICS, @@ -89,7 +89,11 @@ def test_check_application_over_retention_limit_fails( @pytest.mark.parametrize( - "template_type, notification_type", [(EMAIL_TYPE, EMAIL_TYPE), (SMS_TYPE, SMS_TYPE)] + "template_type, notification_type", + [ + (TemplateType.EMAIL, NotificationType.EMAIL), + (TemplateType.SMS, NotificationType.SMS), + ] ) def test_check_template_is_for_notification_type_pass(template_type, notification_type): assert ( @@ -101,7 +105,11 @@ def test_check_template_is_for_notification_type_pass(template_type, notificatio @pytest.mark.parametrize( - "template_type, notification_type", [(SMS_TYPE, EMAIL_TYPE), (EMAIL_TYPE, SMS_TYPE)] + "template_type, notification_type", + [ + (TemplateType.SMS, NotificationType.EMAIL), + (TemplateType.EMAIL, NotificationType.SMS), + ] ) def test_check_template_is_for_notification_type_fails_when_template_type_does_not_match_notification_type( template_type, notification_type @@ -548,11 +556,14 @@ def test_validate_and_format_recipient_fails_when_international_number_and_servi key_type, notify_db_session, ): - service = create_service(service_permissions=[SMS_TYPE]) + service = create_service(service_permissions=[ServicePermissionType.SMS]) service_model = SerialisedService.from_id(service.id) with pytest.raises(BadRequestError) as e: validate_and_format_recipient( - "+20-12-1234-1234", key_type, service_model, SMS_TYPE + "+20-12-1234-1234", + key_type, + service_model, + NotificationType.SMS, ) assert e.value.status_code == 400 assert e.value.message == "Cannot send to international mobile numbers" @@ -565,14 +576,14 @@ def test_validate_and_format_recipient_succeeds_with_international_numbers_if_se ): service_model = SerialisedService.from_id(sample_service_full_permissions.id) result = validate_and_format_recipient( - "+4407513332413", key_type, service_model, SMS_TYPE + "+4407513332413", key_type, service_model, NotificationType.SMS ) assert result == "+447513332413" def test_validate_and_format_recipient_fails_when_no_recipient(): with pytest.raises(BadRequestError) as e: - validate_and_format_recipient(None, "key_type", "service", "SMS_TYPE") + validate_and_format_recipient(None, "key_type", "service", "NotificationType.SMS") assert e.value.status_code == 400 assert e.value.message == "Recipient can't be empty" @@ -586,7 +597,7 @@ def test_check_service_email_reply_to_where_email_reply_to_is_found(sample_servi reply_to_address = create_reply_to_email(sample_service, "test@test.com") assert ( check_service_email_reply_to_id( - sample_service.id, reply_to_address.id, EMAIL_TYPE + sample_service.id, reply_to_address.id, NotificationType.EMAIL ) == "test@test.com" ) @@ -597,7 +608,7 @@ def test_check_service_email_reply_to_id_where_service_id_is_not_found( ): reply_to_address = create_reply_to_email(sample_service, "test@test.com") with pytest.raises(BadRequestError) as e: - check_service_email_reply_to_id(fake_uuid, reply_to_address.id, EMAIL_TYPE) + check_service_email_reply_to_id(fake_uuid, reply_to_address.id, NotificationType.EMAIL) assert e.value.status_code == 400 assert ( e.value.message @@ -611,7 +622,7 @@ def test_check_service_email_reply_to_id_where_reply_to_id_is_not_found( sample_service, fake_uuid ): with pytest.raises(BadRequestError) as e: - check_service_email_reply_to_id(sample_service.id, fake_uuid, EMAIL_TYPE) + check_service_email_reply_to_id(sample_service.id, fake_uuid, NotificationType.EMAIL) assert e.value.status_code == 400 assert ( e.value.message @@ -628,10 +639,11 @@ def test_check_service_sms_sender_id_where_sms_sender_id_is_none(notification_ty def test_check_service_sms_sender_id_where_sms_sender_id_is_found(sample_service): sms_sender = create_service_sms_sender(service=sample_service, sms_sender="123456") - assert ( - check_service_sms_sender_id(sample_service.id, sms_sender.id, SMS_TYPE) - == "123456" - ) + assert check_service_sms_sender_id( + sample_service.id, + sms_sender.id, + NotificationType.SMS, + ) == "123456" def test_check_service_sms_sender_id_where_service_id_is_not_found( @@ -639,7 +651,7 @@ def test_check_service_sms_sender_id_where_service_id_is_not_found( ): sms_sender = create_service_sms_sender(service=sample_service, sms_sender="123456") with pytest.raises(BadRequestError) as e: - check_service_sms_sender_id(fake_uuid, sms_sender.id, SMS_TYPE) + check_service_sms_sender_id(fake_uuid, sms_sender.id, NotificationType.SMS) assert e.value.status_code == 400 assert ( e.value.message @@ -653,7 +665,7 @@ def test_check_service_sms_sender_id_where_sms_sender_is_not_found( sample_service, fake_uuid ): with pytest.raises(BadRequestError) as e: - check_service_sms_sender_id(sample_service.id, fake_uuid, SMS_TYPE) + check_service_sms_sender_id(sample_service.id, fake_uuid, NotificationType.SMS) assert e.value.status_code == 400 assert ( e.value.message @@ -671,14 +683,14 @@ def test_check_reply_to_with_empty_reply_to(sample_service, notification_type): def test_check_reply_to_email_type(sample_service): reply_to_address = create_reply_to_email(sample_service, "test@test.com") assert ( - check_reply_to(sample_service.id, reply_to_address.id, EMAIL_TYPE) + check_reply_to(sample_service.id, reply_to_address.id, NotificationType.EMAIL) == "test@test.com" ) def test_check_reply_to_sms_type(sample_service): sms_sender = create_service_sms_sender(service=sample_service, sms_sender="123456") - assert check_reply_to(sample_service.id, sms_sender.id, SMS_TYPE) == "123456" + assert check_reply_to(sample_service.id, sms_sender.id, NotificationType.SMS) == "123456" def test_check_if_service_can_send_files_by_email_raises_if_no_contact_link_set( diff --git a/tests/app/platform_stats/test_rest.py b/tests/app/platform_stats/test_rest.py index cef9677e7..9c5e3ece8 100644 --- a/tests/app/platform_stats/test_rest.py +++ b/tests/app/platform_stats/test_rest.py @@ -4,7 +4,7 @@ import pytest from freezegun import freeze_time from app.errors import InvalidRequest -from app.models import EMAIL_TYPE, SMS_TYPE +from app.models import TemplateType from app.platform_stats.rest import validate_date_range_is_within_a_financial_year from tests.app.db import ( create_ft_billing, @@ -59,8 +59,11 @@ def test_get_platform_stats_validates_the_date(admin_request): @freeze_time("2018-10-31 14:00") def test_get_platform_stats_with_real_query(admin_request, notify_db_session): service_1 = create_service(service_name="service_1") - sms_template = create_template(service=service_1, template_type=SMS_TYPE) - email_template = create_template(service=service_1, template_type=EMAIL_TYPE) + sms_template = create_template(service=service_1, template_type=TemplateType.SMS) + email_template = create_template( + service=service_1, + template_type=TemplateType.EMAIL, + ) create_ft_notification_status(date(2018, 10, 29), "sms", service_1, count=10) create_ft_notification_status(date(2018, 10, 29), "email", service_1, count=3) diff --git a/tests/app/service/send_notification/test_send_notification.py b/tests/app/service/send_notification/test_send_notification.py index 3ffbb8e2e..254886bf4 100644 --- a/tests/app/service/send_notification/test_send_notification.py +++ b/tests/app/service/send_notification/test_send_notification.py @@ -14,15 +14,15 @@ from app.dao.services_dao import dao_update_service from app.dao.templates_dao import dao_get_all_templates_for_service, dao_update_template from app.errors import InvalidRequest from app.models import ( - EMAIL_TYPE, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST, - SMS_TYPE, ApiKey, Notification, NotificationHistory, + NotificationType, Template, + TemplateType, ) from app.service.send_notification import send_one_off_notification from app.v2.errors import RateLimitError @@ -37,7 +37,7 @@ from tests.app.db import ( ) -@pytest.mark.parametrize("template_type", [SMS_TYPE, EMAIL_TYPE]) +@pytest.mark.parametrize("template_type", [TemplateType.SMS, TemplateType.EMAIL]) def test_create_notification_should_reject_if_missing_required_fields( notify_api, sample_api_key, mocker, template_type ): @@ -96,7 +96,11 @@ def test_should_reject_bad_phone_numbers(notify_api, sample_template, mocker): @pytest.mark.parametrize( - "template_type, to", [(SMS_TYPE, "+447700900855"), (EMAIL_TYPE, "ok@ok.com")] + "template_type, to", + [ + (TemplateType.SMS, "+447700900855"), + (TemplateType.EMAIL, "ok@ok.com"), + ] ) def test_send_notification_invalid_template_id( notify_api, sample_template, mocker, fake_uuid, template_type, to @@ -281,8 +285,8 @@ def test_should_not_send_notification_for_archived_template( @pytest.mark.parametrize( "template_type, to", [ - (SMS_TYPE, "+447700900855"), - (EMAIL_TYPE, "not-someone-we-trust@email-address.com"), + (TemplateType.SMS, "+447700900855"), + (TemplateType.EMAIL, "not-someone-we-trust@email-address.com"), ], ) def test_should_not_send_notification_if_restricted_and_not_a_service_user( @@ -294,7 +298,9 @@ def test_should_not_send_notification_if_restricted_and_not_a_service_user( "app.celery.provider_tasks.deliver_{}.apply_async".format(template_type) ) template = ( - sample_template if template_type == SMS_TYPE else sample_email_template + sample_template + if template_type == TemplateType.SMS + else sample_email_template ) template.service.restricted = True dao_update_service(template.service) @@ -322,7 +328,7 @@ def test_should_not_send_notification_if_restricted_and_not_a_service_user( ] == json_resp["message"]["to"] -@pytest.mark.parametrize("template_type", [SMS_TYPE, EMAIL_TYPE]) +@pytest.mark.parametrize("template_type", [TemplateType.SMS, TemplateType.EMAIL]) def test_should_send_notification_if_restricted_and_a_service_user( notify_api, sample_template, sample_email_template, template_type, mocker ): @@ -333,11 +339,11 @@ def test_should_send_notification_if_restricted_and_a_service_user( ) template = ( - sample_template if template_type == SMS_TYPE else sample_email_template + sample_template if template_type == TemplateType.SMS else sample_email_template ) to = ( template.service.created_by.mobile_number - if template_type == SMS_TYPE + if template_type == TemplateType.SMS else template.service.created_by.email_address ) template.service.restricted = True @@ -358,7 +364,7 @@ def test_should_send_notification_if_restricted_and_a_service_user( assert response.status_code == 201 -@pytest.mark.parametrize("template_type", [SMS_TYPE, EMAIL_TYPE]) +@pytest.mark.parametrize("template_type", [TemplateType.SMS, TemplateType.EMAIL]) def test_should_not_allow_template_from_another_service( notify_api, service_factory, sample_user, mocker, template_type ): @@ -379,7 +385,7 @@ def test_should_not_allow_template_from_another_service( ) to = ( sample_user.mobile_number - if template_type == SMS_TYPE + if template_type == TemplateType.SMS else sample_user.email_address ) data = {"to": to, "template": service_2_templates[0].id} @@ -496,8 +502,8 @@ def test_should_allow_api_call_if_under_day_limit_regardless_of_type( mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async") service = create_service(restricted=restricted, message_limit=2) - email_template = create_template(service, template_type=EMAIL_TYPE) - sms_template = create_template(service, template_type=SMS_TYPE) + email_template = create_template(service, template_type=TemplateType.EMAIL) + sms_template = create_template(service, template_type=TemplateType.SMS) create_notification(template=email_template) data = {"to": sample_user.mobile_number, "template": str(sms_template.id)} @@ -518,7 +524,7 @@ def test_should_not_return_html_in_body(notify_api, sample_service, mocker): with notify_api.test_client() as client: mocker.patch("app.celery.provider_tasks.deliver_email.apply_async") email_template = create_template( - sample_service, template_type=EMAIL_TYPE, content="hello\nthere" + sample_service, template_type=TemplateType.EMAIL, content="hello\nthere" ) data = {"to": "ok@ok.com", "template": str(email_template.id)} @@ -742,7 +748,10 @@ def test_should_send_sms_if_team_api_key_and_a_service_user( @pytest.mark.parametrize( "template_type,queue_name", - [(SMS_TYPE, "send-sms-tasks"), (EMAIL_TYPE, "send-email-tasks")], + [ + (TemplateType.SMS, "send-sms-tasks"), + (TemplateType.EMAIL, "send-email-tasks"), + ], ) def test_should_persist_notification( client, @@ -760,10 +769,10 @@ def test_should_persist_notification( "app.notifications.process_notifications.uuid.uuid4", return_value=fake_uuid ) - template = sample_template if template_type == SMS_TYPE else sample_email_template + template = sample_template if template_type == TemplateType.SMS else sample_email_template to = ( sample_template.service.created_by.mobile_number - if template_type == SMS_TYPE + if template_type == TemplateType.SMS else sample_email_template.service.created_by.email_address ) data = {"to": to, "template": template.id} @@ -798,7 +807,7 @@ def test_should_persist_notification( @pytest.mark.parametrize( "template_type,queue_name", - [(SMS_TYPE, "send-sms-tasks"), (EMAIL_TYPE, "send-email-tasks")], + [(TemplateType.SMS, "send-sms-tasks"), (TemplateType.EMAIL, "send-email-tasks")], ) def test_should_delete_notification_and_return_error_if_redis_fails( client, @@ -817,10 +826,10 @@ def test_should_delete_notification_and_return_error_if_redis_fails( "app.notifications.process_notifications.uuid.uuid4", return_value=fake_uuid ) - template = sample_template if template_type == SMS_TYPE else sample_email_template + template = sample_template if template_type == TemplateType.SMS else sample_email_template to = ( sample_template.service.created_by.mobile_number - if template_type == SMS_TYPE + if template_type == TemplateType.SMS else sample_email_template.service.created_by.email_address ) data = {"to": to, "template": template.id} @@ -907,7 +916,10 @@ def test_should_not_persist_notification_or_send_sms_if_simulated_number( @pytest.mark.parametrize("key_type", [KEY_TYPE_NORMAL, KEY_TYPE_TEAM]) @pytest.mark.parametrize( "notification_type, to", - [(SMS_TYPE, "2028675300"), (EMAIL_TYPE, "non_guest_list_recipient@mail.com")], + [ + (TemplateType.SMS, "2028675300"), + (TemplateType.EMAIL, "non_guest_list_recipient@mail.com"), + ], ) def test_should_not_send_notification_to_non_guest_list_recipient_in_trial_mode( client, sample_service_guest_list, notification_type, to, key_type, mocker @@ -962,8 +974,8 @@ def test_should_not_send_notification_to_non_guest_list_recipient_in_trial_mode( @pytest.mark.parametrize( "notification_type, to, normalized_to", [ - (SMS_TYPE, "2028675300", "+12028675300"), - (EMAIL_TYPE, "guest_list_recipient@mail.com", None), + (NotificationType.SMS, "2028675300", "+12028675300"), + (NotificationType.EMAIL, "guest_list_recipient@mail.com", None), ], ) def test_should_send_notification_to_guest_list_recipient( @@ -983,9 +995,9 @@ def test_should_send_notification_to_guest_list_recipient( "app.celery.provider_tasks.deliver_{}.apply_async".format(notification_type) ) template = create_template(sample_service, template_type=notification_type) - if notification_type == SMS_TYPE: + if notification_type == NotificationType.SMS: service_guest_list = create_service_guest_list(sample_service, mobile_number=to) - elif notification_type == EMAIL_TYPE: + elif notification_type == NotificationType.EMAIL: service_guest_list = create_service_guest_list(sample_service, email_address=to) assert service_guest_list.service_id == sample_service.id @@ -1022,8 +1034,8 @@ def test_should_send_notification_to_guest_list_recipient( @pytest.mark.parametrize( "notification_type, template_type, to", [ - (EMAIL_TYPE, SMS_TYPE, "notify@digital.fake.gov"), - (SMS_TYPE, EMAIL_TYPE, "+12028675309"), + (NotificationType.EMAIL, TemplateType.SMS, "notify@digital.fake.gov"), + (NotificationType.SMS, TemplateType.EMAIL, "+12028675309"), ], ) def test_should_error_if_notification_type_does_not_match_template_type( @@ -1070,7 +1082,11 @@ def test_create_template_doesnt_raise_with_too_much_personalisation( @pytest.mark.parametrize( - "template_type, should_error", [(SMS_TYPE, True), (EMAIL_TYPE, False)] + "template_type, should_error", + [ + (TemplateType.SMS, True), + (TemplateType.EMAIL, False), + ] ) def test_create_template_raises_invalid_request_when_content_too_large( sample_service, template_type, should_error @@ -1338,7 +1354,7 @@ def test_post_notification_should_set_reply_to_text( ) template = create_template(sample_service, template_type=notification_type) expected_reply_to = current_app.config["FROM_NUMBER"] - if notification_type == EMAIL_TYPE: + if notification_type == NotificationType.EMAIL: expected_reply_to = "reply_to@gov.uk" create_reply_to_email( service=sample_service, email_address=expected_reply_to, is_default=True 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 b631420d4..c7b369444 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 @@ -8,13 +8,13 @@ from notifications_utils.recipients import InvalidPhoneError from app.config import QueueNames from app.dao.service_guest_list_dao import dao_add_and_commit_guest_list_contacts from app.models import ( - EMAIL_TYPE, KEY_TYPE_NORMAL, - MOBILE_TYPE, PRIORITY, - SMS_TYPE, + GuestListRecipientType, Notification, + NotificationType, ServiceGuestList, + TemplateType, ) from app.service.send_notification import send_one_off_notification from app.v2.errors import BadRequestError @@ -69,7 +69,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_sms( service = create_service() template = create_template( service=service, - template_type=SMS_TYPE, + template_type=TemplateType.SMS, content="Hello (( Name))\nYour thing is due soon", ) @@ -88,7 +88,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_sms( recipient=post_data["to"], service=template.service, personalisation={"name": "foo"}, - notification_type=SMS_TYPE, + notification_type=NotificationType.SMS, api_key_id=None, key_type=KEY_TYPE_NORMAL, created_by_id=str(service.created_by_id), @@ -104,7 +104,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_international_sms service = create_service(service_permissions=["sms", "international_sms"]) template = create_template( service=service, - template_type=SMS_TYPE, + template_type=TemplateType.SMS, ) post_data = { @@ -125,7 +125,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_email( service = create_service() template = create_template( service=service, - template_type=EMAIL_TYPE, + template_type=TemplateType.EMAIL, subject="Test subject", content="Hello (( Name))\nYour thing is due soon", ) @@ -145,7 +145,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_email( recipient=post_data["to"], service=template.service, personalisation={"name": "foo"}, - notification_type=EMAIL_TYPE, + notification_type=NotificationType.EMAIL, api_key_id=None, key_type=KEY_TYPE_NORMAL, created_by_id=str(service.created_by_id), @@ -203,7 +203,7 @@ def test_send_one_off_notification_raises_if_cant_send_to_recipient( template = create_template(service=service) dao_add_and_commit_guest_list_contacts( [ - ServiceGuestList.from_string(service.id, MOBILE_TYPE, "2028765309"), + ServiceGuestList.from_string(service.id, GuestListRecipientType.MOBILE, "2028765309"), ] ) @@ -286,7 +286,7 @@ def test_send_one_off_notification_should_add_email_reply_to_text_for_notificati def test_send_one_off_sms_notification_should_use_sms_sender_reply_to_text( sample_service, celery_mock ): - template = create_template(service=sample_service, template_type=SMS_TYPE) + template = create_template(service=sample_service, template_type=TemplateType.SMS) sms_sender = create_service_sms_sender( service=sample_service, sms_sender="2028675309", is_default=False ) @@ -310,7 +310,7 @@ def test_send_one_off_sms_notification_should_use_sms_sender_reply_to_text( def test_send_one_off_sms_notification_should_use_default_service_reply_to_text( sample_service, celery_mock ): - template = create_template(service=sample_service, template_type=SMS_TYPE) + template = create_template(service=sample_service, template_type=TemplateType.SMS) sample_service.service_sms_senders[0].is_default = False create_service_sms_sender( service=sample_service, sms_sender="2028675309", is_default=True diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 7a5a92222..d9ab3fd07 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -15,23 +15,21 @@ from app.dao.services_dao import dao_add_user_to_service, dao_remove_user_from_s from app.dao.templates_dao import dao_redact_template from app.dao.users_dao import save_model_user from app.models import ( - EMAIL_AUTH_TYPE, - EMAIL_TYPE, - INBOUND_SMS_TYPE, - INTERNATIONAL_SMS_TYPE, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST, - SMS_TYPE, AnnualBilling, EmailBranding, InboundNumber, Notification, + NotificationType, Permission, Service, ServiceEmailReplyTo, ServicePermission, + ServicePermissionType, ServiceSmsSender, + TemplateType, User, ) from tests import create_admin_authorization_header @@ -285,9 +283,9 @@ def test_get_service_list_has_default_permissions(admin_request, service_factory assert all( set(json["permissions"]) == { - EMAIL_TYPE, - SMS_TYPE, - INTERNATIONAL_SMS_TYPE, + ServicePermissionType.EMAIL, + ServicePermissionType.SMS, + ServicePermissionType.INTERNATIONAL_SMS, } for json in json_resp["data"] ) @@ -301,9 +299,9 @@ def test_get_service_by_id_has_default_service_permissions( ) assert set(json_resp["data"]["permissions"]) == { - EMAIL_TYPE, - SMS_TYPE, - INTERNATIONAL_SMS_TYPE, + ServicePermissionType.EMAIL, + ServicePermissionType.SMS, + ServicePermissionType.INTERNATIONAL_SMS, } @@ -769,7 +767,7 @@ def test_update_service_flags(client, sample_service): json_resp = resp.json assert resp.status_code == 200 assert json_resp["data"]["name"] == sample_service.name - data = {"permissions": [INTERNATIONAL_SMS_TYPE]} + data = {"permissions": {ServicePermissionType.INTERNATIONAL_SMS}} auth_header = create_admin_authorization_header() @@ -780,7 +778,9 @@ def test_update_service_flags(client, sample_service): ) result = resp.json assert resp.status_code == 200 - assert set(result["data"]["permissions"]) == set([INTERNATIONAL_SMS_TYPE]) + assert set(result["data"]["permissions"]) == { + ServicePermissionType.INTERNATIONAL_SMS + } @pytest.mark.parametrize( @@ -854,7 +854,7 @@ def test_update_service_flags_with_service_without_default_service_permissions( ): auth_header = create_admin_authorization_header() data = { - "permissions": [INTERNATIONAL_SMS_TYPE], + "permissions": {ServicePermissionType.INTERNATIONAL_SMS}, } resp = client.post( @@ -865,7 +865,9 @@ def test_update_service_flags_with_service_without_default_service_permissions( result = resp.json assert resp.status_code == 200 - assert set(result["data"]["permissions"]) == set([INTERNATIONAL_SMS_TYPE]) + assert set(result["data"]["permissions"]) == { + ServicePermissionType.INTERNATIONAL_SMS, + } def test_update_service_flags_will_remove_service_permissions( @@ -874,12 +876,18 @@ def test_update_service_flags_will_remove_service_permissions( auth_header = create_admin_authorization_header() service = create_service( - service_permissions=[SMS_TYPE, EMAIL_TYPE, INTERNATIONAL_SMS_TYPE] + service_permissions={ + ServicePermissionType.SMS, + ServicePermissionType.EMAIL, + ServicePermissionType.INTERNATIONAL_SMS, + } ) - assert INTERNATIONAL_SMS_TYPE in [p.permission for p in service.permissions] + assert ServicePermissionType.INTERNATIONAL_SMS in { + p.permission for p in service.permissions + } - data = {"permissions": [SMS_TYPE, EMAIL_TYPE]} + data = {"permissions": {ServicePermissionType.SMS, ServicePermissionType.EMAIL}} resp = client.post( "/service/{}".format(service.id), @@ -889,10 +897,13 @@ def test_update_service_flags_will_remove_service_permissions( result = resp.json assert resp.status_code == 200 - assert INTERNATIONAL_SMS_TYPE not in result["data"]["permissions"] + assert ServicePermissionType.INTERNATIONAL_SMS not in result["data"]["permissions"] permissions = ServicePermission.query.filter_by(service_id=service.id).all() - assert set([p.permission for p in permissions]) == set([SMS_TYPE, EMAIL_TYPE]) + assert {p.permission for p in permissions} == { + ServicePermissionType.SMS, + ServicePermissionType.EMAIL, + } def test_update_permissions_will_override_permission_flags( @@ -900,7 +911,7 @@ def test_update_permissions_will_override_permission_flags( ): auth_header = create_admin_authorization_header() - data = {"permissions": [INTERNATIONAL_SMS_TYPE]} + data = {"permissions": {ServicePermissionType.INTERNATIONAL_SMS}} resp = client.post( "/service/{}".format(service_with_no_permissions.id), @@ -910,7 +921,9 @@ def test_update_permissions_will_override_permission_flags( result = resp.json assert resp.status_code == 200 - assert set(result["data"]["permissions"]) == set([INTERNATIONAL_SMS_TYPE]) + assert set(result["data"]["permissions"]) == { + ServicePermissionType.INTERNATIONAL_SMS + } def test_update_service_permissions_will_add_service_permissions( @@ -918,7 +931,7 @@ def test_update_service_permissions_will_add_service_permissions( ): auth_header = create_admin_authorization_header() - data = {"permissions": [EMAIL_TYPE, SMS_TYPE]} + data = {"permissions": {ServicePermissionType.EMAIL, ServicePermissionType.SMS}} resp = client.post( "/service/{}".format(sample_service.id), @@ -928,17 +941,20 @@ def test_update_service_permissions_will_add_service_permissions( result = resp.json assert resp.status_code == 200 - assert set(result["data"]["permissions"]) == set([SMS_TYPE, EMAIL_TYPE]) + assert set(result["data"]["permissions"]) == { + ServicePermissionType.SMS, + ServicePermissionType.EMAIL, + } @pytest.mark.parametrize( "permission_to_add", [ - (EMAIL_TYPE), - (SMS_TYPE), - (INTERNATIONAL_SMS_TYPE), - (INBOUND_SMS_TYPE), - (EMAIL_AUTH_TYPE), + ServicePermissionType.EMAIL, + ServicePermissionType.SMS, + ServicePermissionType.INTERNATIONAL_SMS, + ServicePermissionType.INBOUND_SMS, + ServicePermissionType.EMAIL_AUTH, ], ) def test_add_service_permission_will_add_permission( @@ -968,7 +984,13 @@ def test_update_permissions_with_an_invalid_permission_will_raise_error( auth_header = create_admin_authorization_header() invalid_permission = "invalid_permission" - data = {"permissions": [EMAIL_TYPE, SMS_TYPE, invalid_permission]} + data = { + "permissions": { + ServicePermissionType.EMAIL, + ServicePermissionType.SMS, + invalid_permission, + } + } resp = client.post( "/service/{}".format(sample_service.id), @@ -990,7 +1012,13 @@ def test_update_permissions_with_duplicate_permissions_will_raise_error( ): auth_header = create_admin_authorization_header() - data = {"permissions": [EMAIL_TYPE, SMS_TYPE, SMS_TYPE]} + data = { + "permissions": { + ServicePermissionType.EMAIL, + ServicePermissionType.SMS, + ServicePermissionType.SMS, + } + } resp = client.post( "/service/{}".format(sample_service.id), @@ -1002,7 +1030,7 @@ def test_update_permissions_with_duplicate_permissions_will_raise_error( assert resp.status_code == 400 assert result["result"] == "error" assert ( - "Duplicate Service Permission: ['{}']".format(SMS_TYPE) + f"Duplicate Service Permission: ['{ServicePermissionType.SMS}']" in result["message"]["permissions"] ) @@ -1695,7 +1723,7 @@ def test_get_all_notifications_for_service_filters_notifications_when_using_post service_2 = create_service(service_name="2") service_1_sms_template = create_template(service_1) - service_1_email_template = create_template(service_1, template_type=EMAIL_TYPE) + service_1_email_template = create_template(service_1, template_type=TemplateType.EMAIL) service_2_sms_template = create_template(service_2) returned_notification = create_notification( @@ -2040,8 +2068,11 @@ def test_get_detailed_service( service = resp.json["data"] assert service["id"] == str(sample_service.id) assert "statistics" in service.keys() - assert set(service["statistics"].keys()) == {SMS_TYPE, EMAIL_TYPE} - assert service["statistics"][SMS_TYPE] == stats + assert set(service["statistics"].keys()) == { + NotificationType.SMS.value, + NotificationType.EMAIL.value, + } + assert service["statistics"][NotificationType.SMS.value] == stats def test_get_services_with_detailed_flag(client, sample_template): @@ -2060,8 +2091,8 @@ def test_get_services_with_detailed_flag(client, sample_template): assert data[0]["name"] == "Sample service" assert data[0]["id"] == str(notifications[0].service_id) assert data[0]["statistics"] == { - EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0}, - SMS_TYPE: {"delivered": 0, "failed": 0, "requested": 3}, + NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0}, + NotificationType.SMS.value: {"delivered": 0, "failed": 0, "requested": 3}, } @@ -2083,8 +2114,8 @@ def test_get_services_with_detailed_flag_excluding_from_test_key( data = resp.json["data"] assert len(data) == 1 assert data[0]["statistics"] == { - EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0}, - SMS_TYPE: {"delivered": 0, "failed": 0, "requested": 2}, + NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0}, + NotificationType.SMS.value: {"delivered": 0, "failed": 0, "requested": 2}, } @@ -2153,13 +2184,13 @@ def test_get_detailed_services_groups_by_service(notify_db_session): assert len(data) == 2 assert data[0]["id"] == str(service_1.id) assert data[0]["statistics"] == { - EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0}, - SMS_TYPE: {"delivered": 1, "failed": 0, "requested": 3}, + NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0}, + NotificationType.SMS.value: {"delivered": 1, "failed": 0, "requested": 3}, } assert data[1]["id"] == str(service_2.id) assert data[1]["statistics"] == { - EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0}, - SMS_TYPE: {"delivered": 0, "failed": 0, "requested": 1}, + NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0}, + NotificationType.SMS.value: {"delivered": 0, "failed": 0, "requested": 1}, } @@ -2182,13 +2213,13 @@ def test_get_detailed_services_includes_services_with_no_notifications( assert len(data) == 2 assert data[0]["id"] == str(service_1.id) assert data[0]["statistics"] == { - EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0}, - SMS_TYPE: {"delivered": 0, "failed": 0, "requested": 1}, + NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0}, + NotificationType.SMS.value: {"delivered": 0, "failed": 0, "requested": 1}, } assert data[1]["id"] == str(service_2.id) assert data[1]["statistics"] == { - EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0}, - SMS_TYPE: {"delivered": 0, "failed": 0, "requested": 0}, + NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0}, + NotificationType.SMS.value: {"delivered": 0, "failed": 0, "requested": 0}, } @@ -2208,8 +2239,8 @@ def test_get_detailed_services_only_includes_todays_notifications(sample_templat assert len(data) == 1 assert data[0]["statistics"] == { - EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0}, - SMS_TYPE: {"delivered": 0, "failed": 0, "requested": 3}, + NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0}, + NotificationType.SMS.value: {"delivered": 0, "failed": 0, "requested": 3}, } @@ -2251,12 +2282,12 @@ def test_get_detailed_services_for_date_range( ) assert len(data) == 1 - assert data[0]["statistics"][EMAIL_TYPE] == { + assert data[0]["statistics"][NotificationType.EMAIL.value] == { "delivered": 0, "failed": 0, "requested": 0, } - assert data[0]["statistics"][SMS_TYPE] == { + assert data[0]["statistics"][NotificationType.SMS.value] == { "delivered": 2, "failed": 0, "requested": 2, @@ -2622,7 +2653,7 @@ def test_get_all_notifications_for_service_includes_template_redacted( # TODO: check whether all hidden templates are also precompiled letters # def test_get_all_notifications_for_service_includes_template_hidden(admin_request, sample_service): -# letter_template = create_template(sample_service, template_type=LETTER_TYPE) +# letter_template = create_template(sample_service, template_type=TemplateType.LETTER) # with freeze_time('2000-01-01'): # letter_noti = create_notification(letter_template) diff --git a/tests/app/service/test_sender.py b/tests/app/service/test_sender.py index fbc8b784b..5f28fefed 100644 --- a/tests/app/service/test_sender.py +++ b/tests/app/service/test_sender.py @@ -2,12 +2,12 @@ import pytest from flask import current_app from app.dao.services_dao import dao_add_user_to_service -from app.models import EMAIL_TYPE, SMS_TYPE, Notification +from app.models import Notification, NotificationType, TemplateType from app.service.sender import send_notification_to_service_users from tests.app.db import create_service, create_template, create_user -@pytest.mark.parametrize("notification_type", [EMAIL_TYPE, SMS_TYPE]) +@pytest.mark.parametrize("notification_type", [NotificationType.EMAIL, NotificationType.SMS]) def test_send_notification_to_service_users_persists_notifications_correctly( notify_service, notification_type, sample_service, mocker ): @@ -37,7 +37,7 @@ def test_send_notification_to_service_users_sends_to_queue( ): send_mock = mocker.patch("app.service.sender.send_notification_to_queue") - template = create_template(sample_service, template_type=EMAIL_TYPE) + template = create_template(sample_service, template_type=NotificationType.EMAIL) send_notification_to_service_users( service_id=sample_service.id, template_id=template.id ) @@ -54,7 +54,7 @@ def test_send_notification_to_service_users_includes_user_fields_in_personalisat user = sample_service.users[0] - template = create_template(sample_service, template_type=EMAIL_TYPE) + template = create_template(sample_service, template_type=TemplateType.EMAIL) send_notification_to_service_users( service_id=sample_service.id, template_id=template.id, @@ -82,7 +82,7 @@ def test_send_notification_to_service_users_sends_to_active_users_only( service = create_service(user=first_active_user) dao_add_user_to_service(service, second_active_user) dao_add_user_to_service(service, pending_user) - template = create_template(service, template_type=EMAIL_TYPE) + template = create_template(service, template_type=TemplateType.EMAIL) send_notification_to_service_users(service_id=service.id, template_id=template.id) diff --git a/tests/app/service/test_service_guest_list.py b/tests/app/service/test_service_guest_list.py index 0e74bce2a..918a8e399 100644 --- a/tests/app/service/test_service_guest_list.py +++ b/tests/app/service/test_service_guest_list.py @@ -2,7 +2,7 @@ import json import uuid from app.dao.service_guest_list_dao import dao_add_and_commit_guest_list_contacts -from app.models import EMAIL_TYPE, MOBILE_TYPE, ServiceGuestList +from app.models import GuestListRecipientType, ServiceGuestList from tests import create_admin_authorization_header @@ -24,11 +24,15 @@ def test_get_guest_list_separates_emails_and_phones(client, sample_service): dao_add_and_commit_guest_list_contacts( [ ServiceGuestList.from_string( - sample_service.id, EMAIL_TYPE, "service@example.com" + sample_service.id, + GuestListRecipientType.EMAIL, + "service@example.com", ), - ServiceGuestList.from_string(sample_service.id, MOBILE_TYPE, "2028675309"), + ServiceGuestList.from_string(sample_service.id, GuestListRecipientType.MOBILE, "2028675309"), ServiceGuestList.from_string( - sample_service.id, MOBILE_TYPE, "+1800-555-5555" + sample_service.id, + GuestListRecipientType.MOBILE, + "+1800-555-5555", ), ] ) diff --git a/tests/app/service/test_statistics_rest.py b/tests/app/service/test_statistics_rest.py index 7059b3a5b..b0cd42b3b 100644 --- a/tests/app/service/test_statistics_rest.py +++ b/tests/app/service/test_statistics_rest.py @@ -5,11 +5,11 @@ import pytest from freezegun import freeze_time from app.models import ( - EMAIL_TYPE, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST, - SMS_TYPE, + NotificationType, + TemplateType, ) from tests.app.db import ( create_ft_notification_status, @@ -58,7 +58,7 @@ def test_get_template_usage_by_month_returns_two_templates( ): template_one = create_template( sample_service, - template_type=SMS_TYPE, + template_type=TemplateType.SMS, template_name="TEST TEMPLATE", hidden=True, ) @@ -123,8 +123,11 @@ def test_get_service_notification_statistics( today_only=today_only, ) - assert set(resp["data"].keys()) == {SMS_TYPE, EMAIL_TYPE} - assert resp["data"][SMS_TYPE] == stats + assert set(resp["data"].keys()) == { + NotificationType.SMS.value, + NotificationType.EMAIL.value, + } + assert resp["data"][NotificationType.SMS.value] == stats def test_get_service_notification_statistics_with_unknown_service(admin_request): @@ -133,8 +136,8 @@ def test_get_service_notification_statistics_with_unknown_service(admin_request) ) assert resp["data"] == { - SMS_TYPE: {"requested": 0, "delivered": 0, "failed": 0}, - EMAIL_TYPE: {"requested": 0, "delivered": 0, "failed": 0}, + NotificationType.SMS.value: {"requested": 0, "delivered": 0, "failed": 0}, + NotificationType.EMAIL.value: {"requested": 0, "delivered": 0, "failed": 0}, } @@ -198,7 +201,7 @@ def test_get_monthly_notification_stats_returns_empty_stats_with_correct_dates( def test_get_monthly_notification_stats_returns_stats(admin_request, sample_service): sms_t1 = create_template(sample_service) sms_t2 = create_template(sample_service) - email_template = create_template(sample_service, template_type=EMAIL_TYPE) + email_template = create_template(sample_service, template_type=TemplateType.EMAIL) create_ft_notification_status(datetime(2016, 6, 1), template=sms_t1) create_ft_notification_status(datetime(2016, 6, 2), template=sms_t1) diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index d4619ea3f..cc145ca0b 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.models import EMAIL_TYPE, SMS_TYPE, Template, TemplateHistory +from app.models import ServicePermissionType, Template, TemplateHistory, TemplateType from tests import create_admin_authorization_header from tests.app.db import create_service, create_template, create_template_folder @@ -17,8 +17,8 @@ from tests.app.db import create_service, create_template, create_template_folder @pytest.mark.parametrize( "template_type, subject", [ - (SMS_TYPE, None), - (EMAIL_TYPE, "subject"), + (TemplateType.SMS, None), + (TemplateType.EMAIL, "subject"), ], ) def test_should_create_a_new_template_for_a_service( @@ -149,7 +149,7 @@ def test_should_raise_error_if_service_does_not_exist_on_create( ): data = { "name": "my template", - "template_type": SMS_TYPE, + "template_type": TemplateType.SMS, "content": "template content", "service": fake_uuid, "created_by": str(sample_user.id), @@ -172,14 +172,14 @@ def test_should_raise_error_if_service_does_not_exist_on_create( "permissions, template_type, subject, expected_error", [ ( - [EMAIL_TYPE], - SMS_TYPE, + [ServicePermissionType.EMAIL], + TemplateType.SMS, None, {"template_type": ["Creating text message templates is not allowed"]}, ), ( - [SMS_TYPE], - EMAIL_TYPE, + [ServicePermissionType.SMS], + TemplateType.EMAIL, "subject", {"template_type": ["Creating email templates is not allowed"]}, ), @@ -217,13 +217,13 @@ def test_should_raise_error_on_create_if_no_permission( "template_type, permissions, expected_error", [ ( - SMS_TYPE, - [EMAIL_TYPE], + TemplateType.SMS, + [ServicePermissionType.EMAIL], {"template_type": ["Updating text message templates is not allowed"]}, ), ( - EMAIL_TYPE, - [SMS_TYPE], + TemplateType.EMAIL, + [ServicePErmissionType.SMS], {"template_type": ["Updating email templates is not allowed"]}, ), ], @@ -261,7 +261,7 @@ def test_should_error_if_created_by_missing(client, sample_user, sample_service) service_id = str(sample_service.id) data = { "name": "my template", - "template_type": SMS_TYPE, + "template_type": TemplateType.SMS, "content": "template content", "service": service_id, } @@ -295,7 +295,7 @@ def test_should_be_error_if_service_does_not_exist_on_update(client, fake_uuid): assert json_resp["message"] == "No result found" -@pytest.mark.parametrize("template_type", [EMAIL_TYPE]) +@pytest.mark.parametrize("template_type", [TemplateType.EMAIL]) def test_must_have_a_subject_on_an_email_template( client, sample_user, sample_service, template_type ): @@ -412,7 +412,7 @@ def test_should_be_able_to_get_all_templates_for_a_service( ): data = { "name": "my template 1", - "template_type": EMAIL_TYPE, + "template_type": TemplateType.EMAIL, "subject": "subject 1", "content": "template content", "service": str(sample_service.id), @@ -421,7 +421,7 @@ def test_should_be_able_to_get_all_templates_for_a_service( data_1 = json.dumps(data) data = { "name": "my template 2", - "template_type": EMAIL_TYPE, + "template_type": TemplateType.EMAIL, "subject": "subject 2", "content": "template content", "service": str(sample_service.id), @@ -529,8 +529,8 @@ def test_should_get_return_all_fields_by_default( @pytest.mark.parametrize( "template_type, expected_content", ( - (EMAIL_TYPE, None), - (SMS_TYPE, None), + (TemplateType.EMAIL, None), + (TemplateType.SMS, None), ), ) def test_should_not_return_content_and_subject_if_requested( @@ -566,9 +566,9 @@ def test_should_not_return_content_and_subject_if_requested( ( "about your ((thing))", "hello ((name)) we’ve received your ((thing))", - EMAIL_TYPE, + TemplateType.EMAIL, ), - (None, "hello ((name)) we’ve received your ((thing))", SMS_TYPE), + (None, "hello ((name)) we’ve received your ((thing))", TemplateType.SMS), ], ) def test_should_get_a_single_template( @@ -640,7 +640,10 @@ def test_should_preview_a_single_template( expected_error, ): template = create_template( - sample_service, template_type=EMAIL_TYPE, subject=subject, content=content + sample_service, + template_type=TemplateType.EMAIL, + subject=subject, + content=content, ) response = client.get( @@ -687,7 +690,7 @@ def test_should_return_404_if_no_templates_for_service_with_id( assert json_resp["message"] == "No result found" -@pytest.mark.parametrize("template_type", (SMS_TYPE,)) +@pytest.mark.parametrize("template_type", (TemplateType.SMS,)) def test_create_400_for_over_limit_content( client, notify_api, diff --git a/tests/app/user/test_rest_verify.py b/tests/app/user/test_rest_verify.py index 0a4185416..7744a8316 100644 --- a/tests/app/user/test_rest_verify.py +++ b/tests/app/user/test_rest_verify.py @@ -11,12 +11,11 @@ from app import db from app.dao.services_dao import dao_fetch_service_by_id from app.dao.users_dao import create_user_code from app.models import ( - EMAIL_TYPE, - SMS_TYPE, USER_AUTH_TYPES, Notification, User, VerifyCode, + VerifyCodeType, ) from tests import create_admin_authorization_header @@ -103,7 +102,7 @@ def test_user_verify_code_rejects_good_code_if_too_many_failed_logins( @freeze_time("2020-04-01 12:00") -@pytest.mark.parametrize("code_type", [EMAIL_TYPE, SMS_TYPE]) +@pytest.mark.parametrize("code_type", [VerifyCodeType.EMAIL, VerifyCodeType.SMS]) def test_user_verify_code_expired_code_and_increments_failed_login_count( code_type, admin_request, sample_user ): @@ -527,7 +526,7 @@ def test_user_verify_email_code(admin_request, sample_user, auth_type): sample_user.email_access_validated_at = datetime.utcnow() - timedelta(days=1) sample_user.auth_type = auth_type magic_code = str(uuid.uuid4()) - verify_code = create_user_code(sample_user, magic_code, EMAIL_TYPE) + verify_code = create_user_code(sample_user, magic_code, VerifyCodeType.EMAIL) data = {"code_type": "email", "code": magic_code} @@ -544,7 +543,7 @@ def test_user_verify_email_code(admin_request, sample_user, auth_type): assert sample_user.current_session_id is not None -@pytest.mark.parametrize("code_type", [EMAIL_TYPE, SMS_TYPE]) +@pytest.mark.parametrize("code_type", [VerifyCodeType.EMAIL, VerifyCodeType.SMS]) @freeze_time("2016-01-01T12:00:00") def test_user_verify_email_code_fails_if_code_already_used( admin_request, sample_user, code_type diff --git a/tests/app/v2/notifications/test_notification_schemas.py b/tests/app/v2/notifications/test_notification_schemas.py index 8ea8ad1c5..fab4e22e7 100644 --- a/tests/app/v2/notifications/test_notification_schemas.py +++ b/tests/app/v2/notifications/test_notification_schemas.py @@ -5,7 +5,7 @@ from flask import json from freezegun import freeze_time from jsonschema import ValidationError -from app.models import EMAIL_TYPE, NOTIFICATION_CREATED +from app.models import NOTIFICATION_CREATED, TemplateType from app.schema_validation import validate from app.v2.notifications.notification_schemas import get_notifications_request from app.v2.notifications.notification_schemas import ( @@ -20,7 +20,7 @@ valid_get_json = {} valid_get_with_optionals_json = { "reference": "test reference", "status": [NOTIFICATION_CREATED], - "template_type": [EMAIL_TYPE], + "template_type": [TemplateType.EMAIL], "include_jobs": "true", "older_than": "a5149c32-f03b-4711-af49-ad6993797d45", } diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index bb9a58453..36bfb5d67 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -9,11 +9,10 @@ from flask import current_app, json from app.dao import templates_dao from app.dao.service_sms_sender_dao import dao_update_service_sms_sender from app.models import ( - EMAIL_TYPE, - INTERNATIONAL_SMS_TYPE, NOTIFICATION_CREATED, - SMS_TYPE, Notification, + NotificationType, + ServicePermissionType, ) from app.schema_validation import validate from app.v2.errors import RateLimitError @@ -529,9 +528,9 @@ def test_post_email_notification_returns_201( @pytest.mark.parametrize( "recipient, notification_type", [ - ("simulate-delivered@notifications.service.gov.uk", EMAIL_TYPE), - ("simulate-delivered-2@notifications.service.gov.uk", EMAIL_TYPE), - ("simulate-delivered-3@notifications.service.gov.uk", EMAIL_TYPE), + ("simulate-delivered@notifications.service.gov.uk", NotificationType.EMAIL), + ("simulate-delivered-2@notifications.service.gov.uk", NotificationType.EMAIL), + ("simulate-delivered-3@notifications.service.gov.uk", NotificationType.EMAIL), ("+14254147167", "sms"), ("+14254147755", "sms"), ], @@ -655,7 +654,7 @@ def test_post_sms_notification_returns_400_if_not_allowed_to_send_int_sms( client, notify_db_session, ): - service = create_service(service_permissions=[SMS_TYPE]) + service = create_service(service_permissions=[ServicePermissionType.SMS]) template = create_template(service=service) data = {"phone_number": "+20-12-1234-1234", "template_id": template.id} @@ -762,7 +761,7 @@ def test_post_sms_notification_returns_400_if_number_not_in_guest_list( notify_db_session, client, restricted ): service = create_service( - restricted=restricted, service_permissions=[SMS_TYPE, INTERNATIONAL_SMS_TYPE] + restricted=restricted, service_permissions=[ServicePermissionType.SMS, ServicePermissionType.INTERNATIONAL_SMS] ) template = create_template(service=service) create_api_key(service=service, key_type="team") @@ -860,7 +859,7 @@ def test_post_notification_raises_bad_request_if_not_valid_notification_type( def test_post_notification_with_wrong_type_of_sender( client, sample_template, sample_email_template, notification_type, fake_uuid ): - if notification_type == EMAIL_TYPE: + if notification_type == NotificationType.EMAIL: template = sample_email_template form_label = "sms_sender_id" data = { @@ -868,7 +867,7 @@ def test_post_notification_with_wrong_type_of_sender( "template_id": str(sample_email_template.id), form_label: fake_uuid, } - elif notification_type == SMS_TYPE: + elif notification_type == ServicePermissionType.SMS: template = sample_template form_label = "email_reply_to_id" data = { @@ -997,7 +996,7 @@ def test_post_email_notification_with_archived_reply_to_id_returns_400( def test_post_notification_with_document_upload( client, notify_db_session, mocker, csv_param ): - service = create_service(service_permissions=[EMAIL_TYPE]) + service = create_service(service_permissions=[ServicePermissionType.EMAIL]) service.contact_link = "contact.me@gov.uk" template = create_template( service=service, @@ -1055,7 +1054,7 @@ def test_post_notification_with_document_upload( def test_post_notification_with_document_upload_simulated( client, notify_db_session, mocker ): - service = create_service(service_permissions=[EMAIL_TYPE]) + service = create_service(service_permissions=[ServicePermissionType.EMAIL]) service.contact_link = "contact.me@gov.uk" template = create_template( service=service, template_type="email", content="Document: ((document))" @@ -1092,7 +1091,7 @@ def test_post_notification_with_document_upload_simulated( def test_post_notification_without_document_upload_permission( client, notify_db_session, mocker ): - service = create_service(service_permissions=[EMAIL_TYPE]) + service = create_service(service_permissions=[ServicePermissionType.EMAIL]) template = create_template( service=service, template_type="email", content="Document: ((document))" ) @@ -1234,7 +1233,7 @@ def test_post_notifications_saves_email_or_sms_to_queue( } data.update( {"email_address": "joe.citizen@example.com"} - ) if notification_type == EMAIL_TYPE else data.update( + ) if notification_type == NotificationType.EMAIL else data.update( {"phone_number": "+447700900855"} ) @@ -1297,7 +1296,7 @@ def test_post_notifications_saves_email_or_sms_normally_if_saving_to_queue_fails } data.update( {"email_address": "joe.citizen@example.com"} - ) if notification_type == EMAIL_TYPE else data.update( + ) if notification_type == NotificationType.EMAIL else data.update( {"phone_number": "+447700900855"} ) @@ -1353,7 +1352,7 @@ def test_post_notifications_doesnt_use_save_queue_for_test_notifications( } data.update( {"email_address": "joe.citizen@example.com"} - ) if notification_type == EMAIL_TYPE else data.update( + ) if notification_type == NotificationType.EMAIL else data.update( {"phone_number": "+447700900855"} ) response = client.post( diff --git a/tests/app/v2/template/test_get_template.py b/tests/app/v2/template/test_get_template.py index a49ab2438..9fe698649 100644 --- a/tests/app/v2/template/test_get_template.py +++ b/tests/app/v2/template/test_get_template.py @@ -1,7 +1,7 @@ import pytest from flask import json -from app.models import EMAIL_TYPE, SMS_TYPE, TEMPLATE_TYPES +from app.models import TemplateType from app.utils import DATETIME_FORMAT from tests import create_service_authorization_header from tests.app.db import create_template @@ -12,8 +12,8 @@ valid_version_params = [None, 1] @pytest.mark.parametrize( "tmp_type, expected_name, expected_subject", [ - (SMS_TYPE, "sms Template Name", None), - (EMAIL_TYPE, "email Template Name", "Template subject"), + (TemplateType.SMS, "sms Template Name", None), + (TemplateType.EMAIL, "email Template Name", "Template subject"), ], ) @pytest.mark.parametrize("version", valid_version_params) @@ -56,7 +56,7 @@ def test_get_template_by_id_returns_200( [ ( { - "template_type": SMS_TYPE, + "template_type": TemplateType.SMS, "content": "Hello ((placeholder)) ((conditional??yes))", }, { @@ -66,7 +66,7 @@ def test_get_template_by_id_returns_200( ), ( { - "template_type": EMAIL_TYPE, + "template_type": TemplateType.EMAIL, "subject": "((subject))", "content": "((content))", }, @@ -120,7 +120,7 @@ def test_get_template_with_non_existent_template_id_returns_404( } -@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES) +@pytest.mark.parametrize("tmp_type", list(TemplateType)) def test_get_template_with_non_existent_version_returns_404( client, sample_service, tmp_type ): diff --git a/tests/app/v2/template/test_post_template.py b/tests/app/v2/template/test_post_template.py index 8985dd623..f9c5b7ff1 100644 --- a/tests/app/v2/template/test_post_template.py +++ b/tests/app/v2/template/test_post_template.py @@ -1,7 +1,7 @@ import pytest from flask import json -from app.models import EMAIL_TYPE, TEMPLATE_TYPES +from app.models import TemplateType from tests import create_service_authorization_header from tests.app.db import create_template @@ -59,7 +59,7 @@ valid_post = [ ] -@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES) +@pytest.mark.parametrize("tmp_type", list(TemplateType)) @pytest.mark.parametrize( "subject,content,post_data,expected_subject,expected_content,expected_html", valid_post, @@ -93,7 +93,7 @@ def test_valid_post_template_returns_200( assert resp_json["id"] == str(template.id) - if tmp_type == EMAIL_TYPE: + if tmp_type == TemplateType.EMAIL: assert expected_subject in resp_json["subject"] assert resp_json["html"] == expected_html else: @@ -105,7 +105,7 @@ def test_valid_post_template_returns_200( def test_email_templates_not_rendered_into_content(client, sample_service): template = create_template( sample_service, - template_type=EMAIL_TYPE, + template_type=TemplateType.EMAIL, subject="Test", content=("Hello\n" "\r\n" "\r\n" "\n" "# This is a heading\n" "\n" "Paragraph"), ) @@ -125,7 +125,7 @@ def test_email_templates_not_rendered_into_content(client, sample_service): assert resp_json["body"] == template.content -@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES) +@pytest.mark.parametrize("tmp_type", list(TemplateType)) def test_invalid_post_template_returns_400(client, sample_service, tmp_type): template = create_template( sample_service, diff --git a/tests/app/v2/template/test_template_schemas.py b/tests/app/v2/template/test_template_schemas.py index 75cf014e0..3c00e62b8 100644 --- a/tests/app/v2/template/test_template_schemas.py +++ b/tests/app/v2/template/test_template_schemas.py @@ -4,7 +4,7 @@ import pytest from flask import json from jsonschema.exceptions import ValidationError -from app.models import EMAIL_TYPE, SMS_TYPE, TEMPLATE_TYPES +from app.models import TemplateType from app.schema_validation import validate from app.v2.template.template_schemas import ( get_template_by_id_request, @@ -15,7 +15,7 @@ from app.v2.template.template_schemas import ( valid_json_get_response = { "id": str(uuid.uuid4()), - "type": SMS_TYPE, + "type": TemplateType.SMS, "created_at": "2017-01-10T18:25:43.511Z", "updated_at": None, "version": 1, @@ -26,7 +26,7 @@ valid_json_get_response = { valid_json_get_response_with_optionals = { "id": str(uuid.uuid4()), - "type": EMAIL_TYPE, + "type": TemplateType.EMAIL, "created_at": "2017-01-10T18:25:43.511Z", "updated_at": None, "version": 1, @@ -114,7 +114,7 @@ def test_get_template_request_schema_against_invalid_args_is_invalid( assert error["message"] in error_message -@pytest.mark.parametrize("template_type", TEMPLATE_TYPES) +@pytest.mark.parametrize("template_type", list(TemplateType)) @pytest.mark.parametrize( "response", [valid_json_get_response, valid_json_get_response_with_optionals] ) @@ -149,7 +149,7 @@ def test_post_template_preview_against_invalid_args_is_invalid(args, error_messa assert error["message"] in error_messages -@pytest.mark.parametrize("template_type", TEMPLATE_TYPES) +@pytest.mark.parametrize("template_type", list(TemplateType)) @pytest.mark.parametrize( "response", [valid_json_post_response, valid_json_post_response_with_optionals] ) diff --git a/tests/app/v2/templates/test_get_templates.py b/tests/app/v2/templates/test_get_templates.py index 17dc5d1b3..c718a76d4 100644 --- a/tests/app/v2/templates/test_get_templates.py +++ b/tests/app/v2/templates/test_get_templates.py @@ -3,7 +3,7 @@ from itertools import product import pytest from flask import json -from app.models import EMAIL_TYPE, TEMPLATE_TYPES +from app.models import TemplateType from tests import create_service_authorization_header from tests.app.db import create_template @@ -13,10 +13,10 @@ def test_get_all_templates_returns_200(client, sample_service): create_template( sample_service, template_type=tmp_type, - subject="subject_{}".format(name) if tmp_type == EMAIL_TYPE else "", + subject="subject_{}".format(name) if tmp_type == TemplateType.EMAIL else "", template_name=name, ) - for name, tmp_type in product(("A", "B", "C"), TEMPLATE_TYPES) + for name, tmp_type in product(("A", "B", "C"), TemplateType) ] auth_header = create_service_authorization_header(service_id=sample_service.id) @@ -37,18 +37,18 @@ def test_get_all_templates_returns_200(client, sample_service): assert template["id"] == str(templates[index].id) assert template["body"] == templates[index].content assert template["type"] == templates[index].template_type - if templates[index].template_type == EMAIL_TYPE: + if templates[index].template_type == TemplateType.EMAIL: assert template["subject"] == templates[index].subject -@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES) +@pytest.mark.parametrize("tmp_type", list(TemplateType)) def test_get_all_templates_for_valid_type_returns_200(client, sample_service, tmp_type): templates = [ create_template( sample_service, template_type=tmp_type, template_name="Template {}".format(i), - subject="subject_{}".format(i) if tmp_type == EMAIL_TYPE else "", + subject="subject_{}".format(i) if tmp_type == TemplateType.EMAIL else "", ) for i in range(3) ] @@ -71,11 +71,11 @@ def test_get_all_templates_for_valid_type_returns_200(client, sample_service, tm assert template["id"] == str(templates[index].id) assert template["body"] == templates[index].content assert template["type"] == tmp_type - if templates[index].template_type == EMAIL_TYPE: + if templates[index].template_type == TemplateType.EMAIL: assert template["subject"] == templates[index].subject -@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES) +@pytest.mark.parametrize("tmp_type", list(TemplateType)) def test_get_correct_num_templates_for_valid_type_returns_200( client, sample_service, tmp_type ): @@ -85,7 +85,7 @@ def test_get_correct_num_templates_for_valid_type_returns_200( for _ in range(num_templates): templates.append(create_template(sample_service, template_type=tmp_type)) - for other_type in TEMPLATE_TYPES: + for other_type in TemplateType: if other_type != tmp_type: templates.append(create_template(sample_service, template_type=other_type)) diff --git a/tests/app/v2/templates/test_templates_schemas.py b/tests/app/v2/templates/test_templates_schemas.py index 1bdf715f2..71ac84df2 100644 --- a/tests/app/v2/templates/test_templates_schemas.py +++ b/tests/app/v2/templates/test_templates_schemas.py @@ -4,7 +4,7 @@ import pytest from flask import json from jsonschema.exceptions import ValidationError -from app.models import EMAIL_TYPE, SMS_TYPE, TEMPLATE_TYPES +from app.models import TemplateType from app.schema_validation import validate from app.v2.templates.templates_schemas import ( get_all_template_request, @@ -16,7 +16,7 @@ valid_json_get_all_response = [ "templates": [ { "id": str(uuid.uuid4()), - "type": SMS_TYPE, + "type": TemplateType.SMS, "created_at": "2017-01-10T18:25:43.511Z", "updated_at": None, "version": 1, @@ -26,7 +26,7 @@ valid_json_get_all_response = [ }, { "id": str(uuid.uuid4()), - "type": EMAIL_TYPE, + "type": TemplateType.EMAIL, "created_at": "2017-02-10T18:25:43.511Z", "updated_at": None, "version": 2, @@ -41,7 +41,7 @@ valid_json_get_all_response = [ "templates": [ { "id": str(uuid.uuid4()), - "type": SMS_TYPE, + "type": TemplateType.SMS, "created_at": "2017-02-10T18:25:43.511Z", "updated_at": None, "version": 2, @@ -60,7 +60,7 @@ invalid_json_get_all_response = [ "templates": [ { "id": "invalid_id", - "type": SMS_TYPE, + "type": TemplateType.SMS, "created_at": "2017-02-10T18:25:43.511Z", "updated_at": None, "version": 1, @@ -77,7 +77,7 @@ invalid_json_get_all_response = [ "templates": [ { "id": str(uuid.uuid4()), - "type": SMS_TYPE, + "type": TemplateType.SMS, "created_at": "2017-02-10T18:25:43.511Z", "updated_at": None, "version": "invalid_version", @@ -94,7 +94,7 @@ invalid_json_get_all_response = [ "templates": [ { "id": str(uuid.uuid4()), - "type": SMS_TYPE, + "type": TemplateType.SMS, "created_at": "invalid_created_at", "updated_at": None, "version": 1, @@ -111,7 +111,7 @@ invalid_json_get_all_response = [ { "templates": [ { - "type": SMS_TYPE, + "type": TemplateType.SMS, "created_at": "2017-02-10T18:25:43.511Z", "updated_at": None, "version": 1, @@ -128,7 +128,7 @@ invalid_json_get_all_response = [ "templates": [ { "id": str(uuid.uuid4()), - "type": SMS_TYPE, + "type": TemplateType.SMS, "created_at": "2017-02-10T18:25:43.511Z", "updated_at": None, "version": 1, @@ -160,7 +160,7 @@ invalid_json_get_all_response = [ "templates": [ { "id": str(uuid.uuid4()), - "type": SMS_TYPE, + "type": TemplateType.SMS, "updated_at": None, "version": 1, "created_by": "someone@test.com", @@ -176,7 +176,7 @@ invalid_json_get_all_response = [ "templates": [ { "id": str(uuid.uuid4()), - "type": SMS_TYPE, + "type": TemplateType.SMS, "created_at": "2017-02-10T18:25:43.511Z", "version": 1, "created_by": "someone@test.com", @@ -192,7 +192,7 @@ invalid_json_get_all_response = [ "templates": [ { "id": str(uuid.uuid4()), - "type": SMS_TYPE, + "type": TemplateType.SMS, "created_at": "2017-02-10T18:25:43.511Z", "updated_at": None, "created_by": "someone@test.com", @@ -208,7 +208,7 @@ invalid_json_get_all_response = [ "templates": [ { "id": str(uuid.uuid4()), - "type": SMS_TYPE, + "type": TemplateType.SMS, "created_at": "2017-02-10T18:25:43.511Z", "updated_at": None, "version": 1, @@ -224,7 +224,7 @@ invalid_json_get_all_response = [ "templates": [ { "id": str(uuid.uuid4()), - "type": SMS_TYPE, + "type": TemplateType.SMS, "created_at": "2017-02-10T18:25:43.511Z", "updated_at": None, "version": 1, @@ -239,7 +239,7 @@ invalid_json_get_all_response = [ { "templates": [ { - "type": SMS_TYPE, + "type": TemplateType.SMS, "created_at": "2017-02-10T18:25:43.511Z", "updated_at": None, "created_by": "someone@test.com", @@ -256,19 +256,19 @@ invalid_json_get_all_response = [ ] -@pytest.mark.parametrize("template_type", TEMPLATE_TYPES) +@pytest.mark.parametrize("template_type", list(TemplateType)) def test_get_all_template_request_schema_against_no_args_is_valid(template_type): data = {} assert validate(data, get_all_template_request) == data -@pytest.mark.parametrize("template_type", TEMPLATE_TYPES) +@pytest.mark.parametrize("template_type", list(TemplateType)) def test_get_all_template_request_schema_against_valid_args_is_valid(template_type): data = {"type": template_type} assert validate(data, get_all_template_request) == data -@pytest.mark.parametrize("template_type", TEMPLATE_TYPES) +@pytest.mark.parametrize("template_type", list(TemplateType)) def test_get_all_template_request_schema_against_invalid_args_is_invalid(template_type): data = {"type": "unknown"}