More changes for enums.

Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2024-02-28 12:41:57 -05:00
parent 820ee5a942
commit 43f18eed6a
24 changed files with 157 additions and 111 deletions

View File

@@ -17,7 +17,7 @@ from app.celery.nightly_tasks import (
save_daily_notification_processing_time,
timeout_notifications,
)
from app.models import EMAIL_TYPE, SMS_TYPE, FactProcessingTime, Job
from app.models import FactProcessingTime, Job, NotificationType
from tests.app.db import (
create_job,
create_notification,
@@ -95,10 +95,10 @@ def test_will_remove_csv_files_for_jobs_older_than_retention_period(
service_1 = create_service(service_name="service 1")
service_2 = create_service(service_name="service 2")
create_service_data_retention(
service=service_1, notification_type=SMS_TYPE, days_of_retention=3
service=service_1, notification_type=NotificationType.SMS, days_of_retention=3
)
create_service_data_retention(
service=service_2, notification_type=EMAIL_TYPE, days_of_retention=30
service=service_2, notification_type=NotificationType.EMAIL, days_of_retention=30
)
sms_template_service_1 = create_template(service=service_1)
email_template_service_1 = create_template(service=service_1, template_type="email")

View File

@@ -14,15 +14,14 @@ from app.celery.reporting_tasks import (
from app.config import QueueNames
from app.dao.fact_billing_dao import get_rate
from app.models import (
EMAIL_TYPE,
KEY_TYPE_NORMAL,
KEY_TYPE_TEAM,
KEY_TYPE_TEST,
NOTIFICATION_TYPES,
SMS_TYPE,
FactBilling,
FactNotificationStatus,
Notification,
NotificationType,
)
from tests.app.db import (
create_notification,
@@ -36,9 +35,9 @@ from tests.app.db import (
def mocker_get_rate(
non_letter_rates, notification_type, local_date, rate_multiplier=None
):
if notification_type == SMS_TYPE:
if notification_type == NotificationType.SMS:
return Decimal(1.33)
elif notification_type == EMAIL_TYPE:
elif notification_type == NotificationType.EMAIL:
return Decimal(0)
@@ -83,7 +82,7 @@ def test_create_nightly_notification_status_triggers_tasks(
kwargs={
"service_id": sample_service.id,
"process_day": "2019-07-31",
"notification_type": SMS_TYPE,
"notification_type": NotificationType.SMS,
},
queue=QueueNames.REPORTING,
)
@@ -94,8 +93,8 @@ def test_create_nightly_notification_status_triggers_tasks(
"notification_date, expected_types_aggregated",
[
("2019-08-01", set()),
("2019-07-31", {EMAIL_TYPE, SMS_TYPE}),
("2019-07-28", {EMAIL_TYPE, SMS_TYPE}),
("2019-07-31", {NotificationType.EMAIL, NotificationType.SMS}),
("2019-07-28", {NotificationType.EMAIL, NotificationType.SMS}),
("2019-07-21", set()),
],
)
@@ -148,7 +147,7 @@ def test_create_nightly_billing_for_day_checks_history(
assert len(records) == 1
record = records[0]
assert record.notification_type == SMS_TYPE
assert record.notification_type == NotificationType.SMS
assert record.notifications_sent == 2
@@ -321,14 +320,14 @@ def test_create_nightly_billing_for_day_null_sent_by_sms(
def test_get_rate_for_sms_and_email(notify_db_session):
non_letter_rates = [
create_rate(datetime(2017, 12, 1), 0.15, SMS_TYPE),
create_rate(datetime(2017, 12, 1), 0, EMAIL_TYPE),
create_rate(datetime(2017, 12, 1), 0.15, NotificationType.SMS),
create_rate(datetime(2017, 12, 1), 0, NotificationType.EMAIL),
]
rate = get_rate(non_letter_rates, SMS_TYPE, date(2018, 1, 1))
rate = get_rate(non_letter_rates, NotificationType.SMS, date(2018, 1, 1))
assert rate == Decimal(0.15)
rate = get_rate(non_letter_rates, EMAIL_TYPE, date(2018, 1, 1))
rate = get_rate(non_letter_rates, NotificationType.EMAIL, date(2018, 1, 1))
assert rate == Decimal(0)

View File

@@ -30,15 +30,15 @@ from app.celery.tasks import (
from app.config import QueueNames
from app.dao import jobs_dao, service_email_reply_to_dao, service_sms_sender_dao
from app.models import (
EMAIL_TYPE,
JOB_STATUS_ERROR,
JOB_STATUS_FINISHED,
JOB_STATUS_IN_PROGRESS,
KEY_TYPE_NORMAL,
NOTIFICATION_CREATED,
SMS_TYPE,
Job,
Notification,
NotificationType,
TemplateType,
)
from app.serialised_models import SerialisedService, SerialisedTemplate
from app.utils import DATETIME_FORMAT
@@ -305,8 +305,8 @@ def test_should_process_all_sms_job(sample_job_with_placeholdered_template, mock
@pytest.mark.parametrize(
"template_type, expected_function, expected_queue",
[
(SMS_TYPE, "save_sms", "database-tasks"),
(EMAIL_TYPE, "save_email", "database-tasks"),
(TemplateType.SMS, "save_sms", "database-tasks"),
(TemplateType.EMAIL, "save_email", "database-tasks"),
],
)
def test_process_row_sends_letter_task(
@@ -362,7 +362,7 @@ def test_process_row_when_sender_id_is_provided(mocker, fake_uuid):
mocker.patch("app.celery.tasks.create_uuid", return_value="noti_uuid")
task_mock = mocker.patch("app.celery.tasks.save_sms.apply_async")
encrypt_mock = mocker.patch("app.celery.tasks.encryption.encrypt")
template = Mock(id="template_id", template_type=SMS_TYPE)
template = Mock(id="template_id", template_type=TemplateType.SMS)
job = Mock(id="job_id", template_version="temp_vers")
service = Mock(id="service_id", research_mode=False)
@@ -1384,8 +1384,8 @@ def test_process_incomplete_jobs_sets_status_to_in_progress_and_resets_processin
def test_save_api_email_or_sms(mocker, sample_service, notification_type):
template = (
create_template(sample_service)
if notification_type == SMS_TYPE
else create_template(sample_service, template_type=EMAIL_TYPE)
if notification_type == NotificationType.SMS
else create_template(sample_service, template_type=TemplateType.EMAIL)
)
mock_provider_task = mocker.patch(
f"app.celery.provider_tasks.deliver_{notification_type}.apply_async"
@@ -1407,7 +1407,7 @@ def test_save_api_email_or_sms(mocker, sample_service, notification_type):
"created_at": datetime.utcnow().strftime(DATETIME_FORMAT),
}
if notification_type == EMAIL_TYPE:
if notification_type == NotificationType.EMAIL:
data.update({"to": "jane.citizen@example.com"})
expected_queue = QueueNames.SEND_EMAIL
else:
@@ -1417,7 +1417,7 @@ def test_save_api_email_or_sms(mocker, sample_service, notification_type):
encrypted = encryption.encrypt(data)
assert len(Notification.query.all()) == 0
if notification_type == EMAIL_TYPE:
if notification_type == NotificationType.EMAIL:
save_api_email(encrypted_notification=encrypted)
else:
save_api_sms(encrypted_notification=encrypted)
@@ -1436,8 +1436,8 @@ def test_save_api_email_dont_retry_if_notification_already_exists(
):
template = (
create_template(sample_service)
if notification_type == SMS_TYPE
else create_template(sample_service, template_type=EMAIL_TYPE)
if notification_type == NotificationType.SMS
else create_template(sample_service, template_type=TemplateType.EMAIL)
)
mock_provider_task = mocker.patch(
f"app.celery.provider_tasks.deliver_{notification_type}.apply_async"
@@ -1459,7 +1459,7 @@ def test_save_api_email_dont_retry_if_notification_already_exists(
"created_at": datetime.utcnow().strftime(DATETIME_FORMAT),
}
if notification_type == EMAIL_TYPE:
if notification_type == NotificationType.EMAIL:
data.update({"to": "jane.citizen@example.com"})
expected_queue = QueueNames.SEND_EMAIL
else:
@@ -1469,14 +1469,14 @@ def test_save_api_email_dont_retry_if_notification_already_exists(
encrypted = encryption.encrypt(data)
assert len(Notification.query.all()) == 0
if notification_type == EMAIL_TYPE:
if notification_type == NotificationType.EMAIL:
save_api_email(encrypted_notification=encrypted)
else:
save_api_sms(encrypted_notification=encrypted)
notifications = Notification.query.all()
assert len(notifications) == 1
# call the task again with the same notification
if notification_type == EMAIL_TYPE:
if notification_type == NotificationType.EMAIL:
save_api_email(encrypted_notification=encrypted)
else:
save_api_sms(encrypted_notification=encrypted)

View File

@@ -19,14 +19,13 @@ from app.dao.templates_dao import dao_create_template
from app.dao.users_dao import create_secret_code, create_user_code
from app.history_meta import create_history
from app.models import (
EMAIL_TYPE,
KEY_TYPE_NORMAL,
KEY_TYPE_TEAM,
KEY_TYPE_TEST,
NOTIFICATION_STATUS_TYPES_COMPLETED,
SERVICE_PERMISSION_TYPES,
SMS_TYPE,
ApiKey,
GuestListRecipientType,
InvitedUser,
Job,
Notification,
@@ -38,8 +37,10 @@ from app.models import (
Service,
ServiceEmailReplyTo,
ServiceGuestList,
ServicePermissionType,
Template,
TemplateHistory,
TemplateType,
)
from tests import create_admin_authorization_header
from tests.app.db import (
@@ -246,7 +247,7 @@ def _sample_service_full_permissions(notify_db_session):
@pytest.fixture(scope="function")
def sample_template(sample_user):
service = create_service(
service_permissions=[EMAIL_TYPE, SMS_TYPE], check_if_service_exists=True
service_permissions=[ServicePermissionType.EMAIL, ServicePermissionType.SMS], check_if_service_exists=True
)
data = {
@@ -268,9 +269,9 @@ def sample_template(sample_user):
@pytest.fixture(scope="function")
def sample_template_without_sms_permission(notify_db_session):
service = create_service(
service_permissions=[EMAIL_TYPE], check_if_service_exists=True
service_permissions=[ServicePermissionType.EMAIL], check_if_service_exists=True
)
return create_template(service, template_type=SMS_TYPE)
return create_template(service, template_type=TemplateType.SMS)
@pytest.fixture(scope="function")
@@ -293,12 +294,12 @@ def sample_sms_template_with_html(sample_service):
def sample_email_template(sample_user):
service = create_service(
user=sample_user,
service_permissions=[EMAIL_TYPE, SMS_TYPE],
service_permissions=[ServicePermissionType.EMAIL, ServicePermissionType.SMS],
check_if_service_exists=True,
)
data = {
"name": "Email Template Name",
"template_type": EMAIL_TYPE,
"template_type": TemplateType.EMAIL,
"content": "This is a template",
"service": service,
"created_by": sample_user,
@@ -312,16 +313,16 @@ def sample_email_template(sample_user):
@pytest.fixture(scope="function")
def sample_template_without_email_permission(notify_db_session):
service = create_service(
service_permissions=[SMS_TYPE], check_if_service_exists=True
service_permissions=[ServicePermissionType.SMS], check_if_service_exists=True
)
return create_template(service, template_type=EMAIL_TYPE)
return create_template(service, template_type=TemplateType.EMAIL)
@pytest.fixture(scope="function")
def sample_email_template_with_placeholders(sample_service):
return create_template(
sample_service,
template_type=EMAIL_TYPE,
template_type=TemplateType.EMAIL,
subject="((name))",
content="Hello ((name))\nThis is an email from GOV.UK",
)
@@ -331,7 +332,7 @@ def sample_email_template_with_placeholders(sample_service):
def sample_email_template_with_html(sample_service):
return create_template(
sample_service,
template_type=EMAIL_TYPE,
template_type=TemplateType.EMAIL,
subject="((name)) <em>some HTML</em>",
content="Hello ((name))\nThis is an email from GOV.UK with <em>some HTML</em>",
)
@@ -480,7 +481,7 @@ def sample_notification(notify_db_session):
def sample_email_notification(notify_db_session):
created_at = datetime.utcnow()
service = create_service(check_if_service_exists=True)
template = create_template(service, template_type=EMAIL_TYPE)
template = create_template(service, template_type=TemplateType.EMAIL)
job = create_job(template)
notification_id = uuid.uuid4()
@@ -843,7 +844,7 @@ def notify_service(notify_db_session, sample_user):
def sample_service_guest_list(notify_db_session):
service = create_service(check_if_service_exists=True)
guest_list_user = ServiceGuestList.from_string(
service.id, EMAIL_TYPE, "guest_list_user@digital.fake.gov"
service.id, GuestListRecipientType.EMAIL, "guest_list_user@digital.fake.gov"
)
notify_db_session.add(guest_list_user)

View File

@@ -18,7 +18,6 @@ from app.dao.fact_notification_status_dao import (
update_fact_notification_status,
)
from app.models import (
EMAIL_TYPE,
KEY_TYPE_TEAM,
KEY_TYPE_TEST,
NOTIFICATION_CREATED,
@@ -30,8 +29,8 @@ from app.models import (
NOTIFICATION_SENT,
NOTIFICATION_TECHNICAL_FAILURE,
NOTIFICATION_TEMPORARY_FAILURE,
SMS_TYPE,
FactNotificationStatus,
TemplateType,
)
from tests.app.db import (
create_ft_notification_status,
@@ -167,9 +166,9 @@ def test_fetch_notification_status_for_service_for_today_and_7_previous_days(
notify_db_session,
):
service_1 = create_service(service_name="service_1")
sms_template = create_template(service=service_1, template_type=SMS_TYPE)
sms_template_2 = 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)
sms_template_2 = 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, 25), "sms", service_1, count=8)
@@ -222,15 +221,15 @@ def test_fetch_notification_status_by_template_for_service_for_today_and_7_previ
):
service_1 = create_service(service_name="service_1")
sms_template = create_template(
template_name="sms Template 1", service=service_1, template_type=SMS_TYPE
template_name="sms Template 1", service=service_1, template_type=TemplateType.SMS
)
sms_template_2 = create_template(
template_name="sms Template 2", service=service_1, template_type=SMS_TYPE
template_name="sms Template 2", service=service_1, template_type=TemplateType.SMS
)
email_template = create_template(service=service_1, template_type=EMAIL_TYPE)
email_template = create_template(service=service_1, template_type=TemplateType.EMAIL)
# create unused email template
create_template(service=service_1, template_type=EMAIL_TYPE)
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), "sms", service_1, count=11)
@@ -323,8 +322,8 @@ def test_fetch_notification_status_totals_for_all_services_works_in_est(
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_notification(
sms_template, created_at=datetime(2018, 4, 20, 12, 0, 0), status="delivered"
@@ -367,8 +366,8 @@ def test_fetch_notification_status_totals_for_all_services_works_in_est(
def set_up_data():
service_2 = create_service(service_name="service_2")
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, 24), "sms", service_1, count=8)
create_ft_notification_status(date(2018, 10, 29), "sms", service_1, count=10)
create_ft_notification_status(

View File

@@ -5,7 +5,7 @@ from app.dao.service_guest_list_dao import (
dao_fetch_service_guest_list,
dao_remove_service_guest_list,
)
from app.models import EMAIL_TYPE, ServiceGuestList
from app.models import GuestListRecipientType, ServiceGuestList
from tests.app.db import create_service
@@ -21,7 +21,7 @@ def test_fetch_service_guest_list_ignores_other_service(sample_service_guest_lis
def test_add_and_commit_guest_list_contacts_saves_data(sample_service):
guest_list = ServiceGuestList.from_string(
sample_service.id, EMAIL_TYPE, "foo@example.com"
sample_service.id, GuestListRecipientType.EMAIL, "foo@example.com"
)
dao_add_and_commit_guest_list_contacts([guest_list])
@@ -37,10 +37,10 @@ def test_remove_service_guest_list_only_removes_for_my_service(notify_db_session
dao_add_and_commit_guest_list_contacts(
[
ServiceGuestList.from_string(
service_1.id, EMAIL_TYPE, "service1@example.com"
service_1.id, GuestListRecipientType.EMAIL, "service1@example.com"
),
ServiceGuestList.from_string(
service_2.id, EMAIL_TYPE, "service2@example.com"
service_2.id, GuestListRecipientType.EMAIL, "service2@example.com"
),
]
)

View File

@@ -27,10 +27,7 @@ from app.dao.services_dao import dao_add_user_to_service, dao_create_service
from app.dao.templates_dao import dao_create_template, dao_update_template
from app.dao.users_dao import save_model_user
from app.models import (
EMAIL_TYPE,
KEY_TYPE_NORMAL,
MOBILE_TYPE,
SMS_TYPE,
AnnualBilling,
ApiKey,
Complaint,
@@ -39,6 +36,7 @@ from app.models import (
FactBilling,
FactNotificationStatus,
FactProcessingTime,
GuestListRecipientType,
InboundNumber,
InboundSms,
InvitedOrganizationUser,
@@ -55,9 +53,11 @@ from app.models import (
ServiceGuestList,
ServiceInboundApi,
ServicePermission,
ServicePermissionType,
ServiceSmsSender,
Template,
TemplateFolder,
TemplateType,
User,
WebauthnCredential,
)
@@ -193,7 +193,7 @@ def create_service_with_defined_sms_sender(sms_sender_value="1234567", *args, **
def create_template(
service,
template_type=SMS_TYPE,
template_type=TemplateType.SMS,
template_name=None,
subject="Template subject",
content="Dear Sir/Madam, Hello. Yours Truly, The Government.",
@@ -215,7 +215,7 @@ def create_template(
"folder": folder,
"process_type": process_type,
}
if template_type != SMS_TYPE:
if template_type != TemplateType.SMS:
data["subject"] = subject
template = Template(**data)
dao_create_template(template)
@@ -262,7 +262,7 @@ def create_notification(
if to_field is None:
to_field = (
"+447700900855"
if template.template_type == SMS_TYPE
if template.template_type == TemplateType.SMS
else "test@example.com"
)
@@ -415,9 +415,10 @@ def create_job(
return job
def create_service_permission(service_id, permission=EMAIL_TYPE):
def create_service_permission(service_id, permission=ServicePermissionType.EMAIL):
dao_add_service_permission(
service_id if service_id else create_service().id, permission
service_id if service_id else create_service().id,
permission,
)
service_permissions = ServicePermission.query.all()
@@ -724,15 +725,15 @@ def create_process_time(
def create_service_guest_list(service, email_address=None, mobile_number=None):
if email_address:
guest_list_user = ServiceGuestList.from_string(
service.id, EMAIL_TYPE, email_address
service.id, GuestListRecipientType.EMAIL, email_address
)
elif mobile_number:
guest_list_user = ServiceGuestList.from_string(
service.id, MOBILE_TYPE, mobile_number
service.id, GuestListRecipientType.MOBILE, mobile_number
)
else:
guest_list_user = ServiceGuestList.from_string(
service.id, EMAIL_TYPE, "guest_list_user@digital.fake.gov"
service.id, GuestListRecipientType.EMAIL, "guest_list_user@digital.fake.gov"
)
db.session.add(guest_list_user)

View File

@@ -23,10 +23,10 @@ from app.dao.users_dao import get_user_by_email
from app.models import (
KEY_TYPE_NORMAL,
NOTIFICATION_DELIVERED,
SMS_TYPE,
AnnualBilling,
Job,
Notification,
NotificationType,
Organization,
Service,
Template,
@@ -314,7 +314,7 @@ def test_fix_billable_units(notify_db_session, notify_api, sample_template):
create_notification(template=sample_template)
notification = Notification.query.one()
notification.billable_units = 0
notification.notification_type = SMS_TYPE
notification.notification_type = NotificationType.SMS
notification.status = NOTIFICATION_DELIVERED
notification.sent_at = None
notification.key_type = KEY_TYPE_NORMAL

View File

@@ -6,23 +6,22 @@ from sqlalchemy.exc import IntegrityError
from app import encryption
from app.models import (
EMAIL_TYPE,
MOBILE_TYPE,
NOTIFICATION_CREATED,
NOTIFICATION_FAILED,
NOTIFICATION_PENDING,
NOTIFICATION_STATUS_TYPES_FAILED,
NOTIFICATION_TECHNICAL_FAILURE,
SMS_TYPE,
Agreement,
AgreementStatus,
AgreementType,
AnnualBilling,
GuestListRecipientType,
Notification,
NotificationHistory,
Service,
ServiceGuestList,
ServicePermission,
TemplateType,
User,
VerifyCode,
filter_null_value_fields,
@@ -43,7 +42,7 @@ from tests.app.db import (
@pytest.mark.parametrize("mobile_number", ["+447700900855", "+12348675309"])
def test_should_build_service_guest_list_from_mobile_number(mobile_number):
service_guest_list = ServiceGuestList.from_string(
"service_id", MOBILE_TYPE, mobile_number
"service_id", GuestListRecipientType.MOBILE, mobile_number
)
assert service_guest_list.recipient == mobile_number
@@ -52,7 +51,7 @@ def test_should_build_service_guest_list_from_mobile_number(mobile_number):
@pytest.mark.parametrize("email_address", ["test@example.com"])
def test_should_build_service_guest_list_from_email_address(email_address):
service_guest_list = ServiceGuestList.from_string(
"service_id", EMAIL_TYPE, email_address
"service_id", GuestListRecipientType.EMAIL, email_address
)
assert service_guest_list.recipient == email_address
@@ -60,7 +59,11 @@ def test_should_build_service_guest_list_from_email_address(email_address):
@pytest.mark.parametrize(
"contact, recipient_type",
[("", None), ("07700dsadsad", MOBILE_TYPE), ("gmail.com", EMAIL_TYPE)],
[
("", None),
("07700dsadsad", GuestListRecipientType.MOBILE),
("gmail.com", GuestListRecipientType.EMAIL),
],
)
def test_should_not_build_service_guest_list_from_invalid_contact(
recipient_type, contact
@@ -201,7 +204,7 @@ def test_notification_personalisation_setter_always_sets_empty_dict(
def test_notification_subject_is_none_for_sms(sample_service):
template = create_template(service=sample_service, template_type=SMS_TYPE)
template = create_template(service=sample_service, template_type=TemplateType.SMS)
notification = create_notification(template=template)
assert notification.subject is None