diff --git a/app/service/statistics.py b/app/service/statistics.py index 6ef87c9ff..184a5ab0c 100644 --- a/app/service/statistics.py +++ b/app/service/statistics.py @@ -2,7 +2,7 @@ from collections import defaultdict from datetime import datetime from app.dao.date_util import get_months_for_financial_year -from app.enums import NotificationStatus, StatisticsType, TemplateType +from app.enums import KeyType, NotificationStatus, StatisticsType, TemplateType def format_statistics(statistics): @@ -23,7 +23,7 @@ def format_admin_stats(statistics): counts = create_stats_dict() for row in statistics: - if row.key_type == "test": + if row.key_type == KeyType.TEST: counts[row.notification_type]["test-key"] += row.count else: counts[row.notification_type]["total"] += row.count diff --git a/app/v2/errors.py b/app/v2/errors.py index 63c3fce53..ccb353428 100644 --- a/app/v2/errors.py +++ b/app/v2/errors.py @@ -7,6 +7,7 @@ from sqlalchemy.exc import DataError from sqlalchemy.orm.exc import NoResultFound from app.authentication.auth import AuthError +from app.enums import KeyType from app.errors import InvalidRequest @@ -35,7 +36,7 @@ class RateLimitError(InvalidRequest): def __init__(self, sending_limit, interval, key_type): # normal keys are spoken of as "live" in the documentation # so using this in the error messaging - if key_type == "normal": + if key_type == KeyType.NORMAL: key_type = "live" self.message = self.message_template.format( diff --git a/tests/app/conftest.py b/tests/app/conftest.py index b4714eeb8..fef9e0406 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -25,6 +25,7 @@ from app.enums import ( PermissionType, RecipientType, ServicePermissionType, + TemplateProcessType, TemplateType, ) from app.history_meta import create_history @@ -261,7 +262,7 @@ def sample_template(sample_user): "created_by": sample_user, "archived": False, "hidden": False, - "process_type": "normal", + "process_type": TemplateProcessType.NORMAL, } template = Template(**data) dao_create_template(template) diff --git a/tests/app/dao/test_fact_billing_dao.py b/tests/app/dao/test_fact_billing_dao.py index c717aa730..cebed26d8 100644 --- a/tests/app/dao/test_fact_billing_dao.py +++ b/tests/app/dao/test_fact_billing_dao.py @@ -21,7 +21,7 @@ from app.dao.fact_billing_dao import ( query_organization_sms_usage_for_year, ) from app.dao.organization_dao import dao_add_service_to_organization -from app.enums import NotificationStatus, NotificationType, TemplateType +from app.enums import KeyType, NotificationStatus, NotificationType, TemplateType from app.models import FactBilling from tests.app.db import ( create_annual_billing, @@ -94,7 +94,7 @@ def test_fetch_billing_data_for_today_includes_data_with_the_right_key_type( ): service = create_service() template = create_template(service=service, template_type=TemplateType.EMAIL) - for key_type in ["normal", "test", "team"]: + for key_type in [KeyType.NORMAL, KeyType.TEST, KeyType.TEAM]: create_notification( template=template, status=NotificationStatus.DELIVERED, diff --git a/tests/app/dao/test_invited_user_dao.py b/tests/app/dao/test_invited_user_dao.py index fb1154c96..5331f1851 100644 --- a/tests/app/dao/test_invited_user_dao.py +++ b/tests/app/dao/test_invited_user_dao.py @@ -38,8 +38,8 @@ def test_create_invited_user(notify_db_session, sample_service): assert invited_user.from_user == invite_from permissions = invited_user.get_permissions() assert len(permissions) == 2 - assert "send_messages" in permissions - assert "manage_service" in permissions + assert PermissionType.SEND_EMAILS in permissions + assert PermissionType.MANAGE_SETTINGS in permissions assert invited_user.folder_permissions == [] diff --git a/tests/app/dao/test_jobs_dao.py b/tests/app/dao/test_jobs_dao.py index 515d453be..0831999e1 100644 --- a/tests/app/dao/test_jobs_dao.py +++ b/tests/app/dao/test_jobs_dao.py @@ -216,13 +216,13 @@ def test_get_jobs_for_service_in_processed_at_then_created_at_order( def test_update_job(sample_job): assert sample_job.job_status == JobStatus.PENDING - sample_job.job_status = "in progress" + sample_job.job_status = JobStatus.IN_PROGRESS dao_update_job(sample_job) job_from_db = Job.query.get(sample_job.id) - assert job_from_db.job_status == "in progress" + assert job_from_db.job_status == JobStatus.IN_PROGRESS def test_set_scheduled_jobs_to_pending_gets_all_jobs_in_scheduled_state_before_now( @@ -231,10 +231,14 @@ def test_set_scheduled_jobs_to_pending_gets_all_jobs_in_scheduled_state_before_n one_minute_ago = datetime.utcnow() - timedelta(minutes=1) one_hour_ago = datetime.utcnow() - timedelta(minutes=60) job_new = create_job( - sample_template, scheduled_for=one_minute_ago, job_status="scheduled" + sample_template, + scheduled_for=one_minute_ago, + job_status=JobStatus.SCHEDULED, ) job_old = create_job( - sample_template, scheduled_for=one_hour_ago, job_status="scheduled" + sample_template, + scheduled_for=one_hour_ago, + job_status=JobStatus.SCHEDULED, ) jobs = dao_set_scheduled_jobs_to_pending() assert len(jobs) == 2 @@ -247,7 +251,9 @@ def test_set_scheduled_jobs_to_pending_gets_ignores_jobs_not_scheduled( ): one_minute_ago = datetime.utcnow() - timedelta(minutes=1) job_scheduled = create_job( - sample_template, scheduled_for=one_minute_ago, job_status="scheduled" + sample_template, + scheduled_for=one_minute_ago, + job_status=JobStatus.SCHEDULED, ) jobs = dao_set_scheduled_jobs_to_pending() assert len(jobs) == 1 @@ -264,8 +270,16 @@ def test_set_scheduled_jobs_to_pending_gets_ignores_jobs_scheduled_in_the_future def test_set_scheduled_jobs_to_pending_updates_rows(sample_template): one_minute_ago = datetime.utcnow() - timedelta(minutes=1) one_hour_ago = datetime.utcnow() - timedelta(minutes=60) - create_job(sample_template, scheduled_for=one_minute_ago, job_status="scheduled") - create_job(sample_template, scheduled_for=one_hour_ago, job_status="scheduled") + create_job( + sample_template, + scheduled_for=one_minute_ago, + job_status=JobStatus.SCHEDULED, + ) + create_job( + sample_template, + scheduled_for=one_hour_ago, + job_status=JobStatus.SCHEDULED, + ) jobs = dao_set_scheduled_jobs_to_pending() assert len(jobs) == 2 assert jobs[0].job_status == JobStatus.PENDING @@ -444,7 +458,15 @@ def test_find_jobs_with_missing_rows_returns_nothing_for_a_job_completed_more_th assert len(results) == 0 -@pytest.mark.parametrize("status", [JobStatus.PENDING, JobStatus.IN_PROGRESS, JobStatus.CANCELLED, JobStatus.SCHEDULED,],) +@pytest.mark.parametrize( + "status", + [ + JobStatus.PENDING, + JobStatus.IN_PROGRESS, + JobStatus.CANCELLED, + JobStatus.SCHEDULED, + ], +) def test_find_jobs_with_missing_rows_doesnt_return_jobs_that_are_not_finished( sample_email_template, status ): diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index da9aa0e10..2686b7977 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -46,6 +46,7 @@ from app.enums import ( NotificationStatus, NotificationType, OrganizationType, + PermissionType, ServicePermissionType, TemplateType, ) @@ -916,7 +917,7 @@ def test_add_existing_user_to_another_service_doesnot_change_old_permissions( # adding the other_user to service_one should leave all other_user permissions on service_two intact permissions = [] - for p in ["send_emails", "send_texts"]: + for p in [PermissionType.SEND_EMAILS, PermissionType.SEND_TEXTS]: permissions.append(Permission(permission=p)) dao_add_user_to_service(service_one, other_user, permissions=permissions) diff --git a/tests/app/dao/test_users_dao.py b/tests/app/dao/test_users_dao.py index 548840eb1..91c52c170 100644 --- a/tests/app/dao/test_users_dao.py +++ b/tests/app/dao/test_users_dao.py @@ -209,7 +209,12 @@ def test_dao_archive_user(sample_user, sample_organization, fake_uuid): service_1_user = create_user(email="1@test.com") service_1.users = [sample_user, service_1_user] create_permissions(sample_user, service_1, PermissionType.MANAGE_SETTINGS) - create_permissions(service_1_user, service_1, PermissionType.MANAGE_SETTINGS, PermissionType.VIEW_ACTIVITY,) + create_permissions( + service_1_user, + service_1, + PermissionType.MANAGE_SETTINGS, + PermissionType.VIEW_ACTIVITY, + ) service_2 = create_service(service_name="Service 2") service_2_user = create_user(email="2@test.com") @@ -266,9 +271,18 @@ def test_user_can_be_archived_if_the_other_service_members_have_the_manage_setti sample_service.users = [user_1, user_2, user_3] create_permissions(user_1, sample_service, PermissionType.MANAGE_SETTINGS) - create_permissions(user_2, sample_service, PermissionType.MANAGE_SETTINGS, PermissionType.VIEW_ACTIVITY,) create_permissions( - user_3, sample_service, PermissionType.MANAGE_SETTINGS, PermissionType.SEND_EMAILS, PermissionType.SEND_TEXTS, + user_2, + sample_service, + PermissionType.MANAGE_SETTINGS, + PermissionType.VIEW_ACTIVITY, + ) + create_permissions( + user_3, + sample_service, + PermissionType.MANAGE_SETTINGS, + PermissionType.SEND_EMAILS, + PermissionType.SEND_TEXTS, ) assert len(sample_service.users) == 3 @@ -306,7 +320,12 @@ def test_user_cannot_be_archived_if_the_other_service_members_do_not_have_the_ma create_permissions(active_user, sample_service, PermissionType.MANAGE_SETTINGS) create_permissions(pending_user, sample_service, PermissionType.VIEW_ACTIVITY) - create_permissions(inactive_user, sample_service, PermissionType.SEND_EMAILS, PermissionType.SEND_TEXTS,) + create_permissions( + inactive_user, + sample_service, + PermissionType.SEND_EMAILS, + PermissionType.SEND_TEXTS, + ) assert len(sample_service.users) == 3 assert not user_can_be_archived(active_user) diff --git a/tests/app/delivery/test_send_to_providers.py b/tests/app/delivery/test_send_to_providers.py index cb4369a0e..2f29a314a 100644 --- a/tests/app/delivery/test_send_to_providers.py +++ b/tests/app/delivery/test_send_to_providers.py @@ -153,7 +153,10 @@ def test_should_not_send_email_message_when_service_is_inactive_notifcation_is_i send_to_providers.send_email_to_provider(sample_notification) assert str(sample_notification.id) in str(e.value) send_mock.assert_not_called() - assert Notification.query.get(sample_notification.id).status == NotificationStatus.TECHNICAL_FAILURE + assert ( + Notification.query.get(sample_notification.id).status + == NotificationStatus.TECHNICAL_FAILURE + ) def test_should_not_send_sms_message_when_service_is_inactive_notification_is_in_tech_failure( @@ -166,7 +169,10 @@ def test_should_not_send_sms_message_when_service_is_inactive_notification_is_in send_to_providers.send_sms_to_provider(sample_notification) assert str(sample_notification.id) in str(e.value) send_mock.assert_not_called() - assert Notification.query.get(sample_notification.id).status == NotificationStatus.TECHNICAL_FAILURE + assert ( + Notification.query.get(sample_notification.id).status + == NotificationStatus.TECHNICAL_FAILURE + ) def test_send_sms_should_use_template_version_from_notification_not_latest( diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index d622be907..1166f3034 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -1365,7 +1365,10 @@ def test_add_existing_user_to_another_service_with_send_permissions( json_resp = resp.json permissions = json_resp["data"]["permissions"][str(sample_service.id)] - expected_permissions = ["send_texts", "send_emails"] + expected_permissions = [ + PermissionType.SEND_TEXTS, + PermissionType.SEND_EMAILS, + ] assert sorted(expected_permissions) == sorted(permissions) @@ -1498,7 +1501,7 @@ def test_add_existing_user_to_another_service_with_manage_api_keys( json_resp = resp.json permissions = json_resp["data"]["permissions"][str(sample_service.id)] - expected_permissions = ["manage_api_keys"] + expected_permissions = [PermissionType.MANAGE_API_KEYS] assert sorted(expected_permissions) == sorted(permissions) diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index 7691d8966..fb6fdcf74 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -587,7 +587,7 @@ def test_should_get_a_single_template( assert response.status_code == 200 assert data["content"] == content assert data["subject"] == subject - assert data["process_type"] == "normal" + assert data["process_type"] == TemplateProcessType.NORMAL assert not data["redact_personalisation"] diff --git a/tests/app/template/test_rest_history.py b/tests/app/template/test_rest_history.py index 4a8834877..ee902d381 100644 --- a/tests/app/template/test_rest_history.py +++ b/tests/app/template/test_rest_history.py @@ -4,6 +4,7 @@ from datetime import datetime from flask import url_for from app.dao.templates_dao import dao_update_template +from app.enums import TemplateProcessType from tests import create_admin_authorization_header @@ -25,7 +26,7 @@ def test_template_history_version(notify_api, sample_user, sample_template): assert json_resp["data"]["id"] == str(sample_template.id) assert json_resp["data"]["content"] == sample_template.content 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"]["name"] == sample_user.name assert ( datetime.strptime( @@ -57,7 +58,7 @@ def test_previous_template_history_version(notify_api, sample_template): assert json_resp["data"]["id"] == str(sample_template.id) assert json_resp["data"]["version"] == 1 assert json_resp["data"]["content"] == old_content - assert json_resp["data"]["process_type"] == "normal" + assert json_resp["data"]["process_type"] == TemplateProcessType.NORMAL def test_404_missing_template_version(notify_api, sample_template): diff --git a/tests/app/user/test_rest.py b/tests/app/user/test_rest.py index fdbd9171d..ff59fb0cb 100644 --- a/tests/app/user/test_rest.py +++ b/tests/app/user/test_rest.py @@ -8,7 +8,7 @@ from flask import current_app from freezegun import freeze_time from app.dao.service_user_dao import dao_get_service_user, dao_update_service_user -from app.enums import AuthType, NotificationType, PermissionType +from app.enums import AuthType, KeyType, NotificationType, PermissionType from app.models import Notification, Permission, User from tests.app.db import ( create_organization, @@ -261,7 +261,7 @@ def test_post_user_attribute(admin_request, sample_user, user_attribute, user_va "newuser@mail.com", dict( api_key_id=None, - key_type="normal", + key_type=KeyType.NORMAL, notification_type=NotificationType.EMAIL, personalisation={ "name": "Test User", @@ -280,7 +280,7 @@ def test_post_user_attribute(admin_request, sample_user, user_attribute, user_va "+4407700900460", dict( api_key_id=None, - key_type="normal", + key_type=KeyType.NORMAL, notification_type=NotificationType.SMS, personalisation={ "name": "Test User", diff --git a/tests/app/v2/notifications/test_get_notifications.py b/tests/app/v2/notifications/test_get_notifications.py index c86dad592..974d26a51 100644 --- a/tests/app/v2/notifications/test_get_notifications.py +++ b/tests/app/v2/notifications/test_get_notifications.py @@ -415,7 +415,10 @@ def test_get_all_notifications_filter_by_template_type_invalid_template_type( def test_get_all_notifications_filter_by_single_status(client, sample_template): - notification = create_notification(template=sample_template, status=NotificationStatus.PENDING,) + notification = create_notification( + template=sample_template, + status=NotificationStatus.PENDING, + ) create_notification(template=sample_template) auth_header = create_service_authorization_header( @@ -476,7 +479,8 @@ def test_get_all_notifications_filter_by_multiple_statuses(client, sample_templa ] ] failed_notification = create_notification( - template=sample_template, status=NotificationStatus.PERMANENT_FAILURE, + template=sample_template, + status=NotificationStatus.PERMANENT_FAILURE, ) auth_header = create_service_authorization_header( diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index b7d98fab6..12359a477 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -1406,7 +1406,8 @@ def test_post_notifications_doesnt_use_save_queue_for_test_notifications( headers=[ ("Content-Type", "application/json"), create_service_authorization_header( - service_id=service.id, key_type="test" + service_id=service.id, + key_type=KeyType.TEST, ), ], )