More cleanup.

Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2024-02-13 11:04:02 -05:00
parent acecf9792d
commit 63aa12e4ef
15 changed files with 662 additions and 203 deletions

View File

@@ -40,7 +40,15 @@ from app.dao.services_dao import (
get_services_by_partial_name,
)
from app.dao.users_dao import create_user_code, save_model_user
from app.enums import KeyType, OrganizationType
from app.enums import (
CodeType,
KeyType,
NotificationStatus,
NotificationType,
OrganizationType,
ServicePermissionType,
TemplateType,
)
from app.models import (
ApiKey,
InvitedUser,
@@ -51,7 +59,6 @@ from app.models import (
Permission,
Service,
ServicePermission,
ServicePermissionType,
ServiceUser,
Template,
TemplateHistory,
@@ -766,7 +773,9 @@ def test_update_service_permission_creates_a_history_record_with_current_data(
),
)
permission = [p for p in service.permissions if p.permission == "sms"][0]
permission = [
p for p in service.permissions if p.permission == ServicePermissionType.SMS
][0]
service.permissions.remove(permission)
dao_update_service(service)
@@ -824,8 +833,8 @@ def test_delete_service_and_associated_objects(notify_db_session):
service = create_service(
user=user, service_permissions=None, organization=organization
)
create_user_code(user=user, code="somecode", code_type="email")
create_user_code(user=user, code="somecode", code_type="sms")
create_user_code(user=user, code="somecode", code_type=CodeType.EMAIL)
create_user_code(user=user, code="somecode", code_type=CodeType.SMS)
template = create_template(service=service)
api_key = create_api_key(service=service)
create_notification(template=template, api_key=api_key)
@@ -953,27 +962,29 @@ def test_fetch_stats_ignores_historical_notification_data(sample_template):
def test_dao_fetch_todays_stats_for_service(notify_db_session):
service = create_service()
sms_template = create_template(service=service)
email_template = create_template(service=service, template_type="email")
email_template = create_template(service=service, template_type=TemplateType.EMAIL)
# two created email, one failed email, and one created sms
create_notification(template=email_template, status="created")
create_notification(template=email_template, status="created")
create_notification(template=email_template, status="technical-failure")
create_notification(template=sms_template, status="created")
create_notification(template=email_template, status=NotificationStatus.CREATED)
create_notification(template=email_template, status=NotificationStatus.CREATED)
create_notification(
template=email_template, status=NotificationStatus.TECHNICAL_FAILURE
)
create_notification(template=sms_template, status=NotificationStatus.CREATED)
stats = dao_fetch_todays_stats_for_service(service.id)
stats = sorted(stats, key=lambda x: (x.notification_type, x.status))
assert len(stats) == 3
assert stats[0].notification_type == "email"
assert stats[0].status == "created"
assert stats[0].notification_type == NotificationType.EMAIL
assert stats[0].status == NotificationStatus.CREATED
assert stats[0].count == 2
assert stats[1].notification_type == "email"
assert stats[1].status == "technical-failure"
assert stats[1].notification_type == NotificationType.EMAIL
assert stats[1].status == NotificationStatus.TECHNICAL_FAILURE
assert stats[1].count == 1
assert stats[2].notification_type == "sms"
assert stats[2].status == "created"
assert stats[2].notification_type == NotificationType.SMS
assert stats[2].status == NotificationStatus.CREATED
assert stats[2].count == 1
@@ -998,8 +1009,8 @@ def test_dao_fetch_todays_stats_for_service_should_ignore_test_key(notify_db_ses
stats = dao_fetch_todays_stats_for_service(service.id)
assert len(stats) == 1
assert stats[0].notification_type == "sms"
assert stats[0].status == "created"
assert stats[0].notification_type == NotificationType.SMS
assert stats[0].status == NotificationStatus.CREATED
assert stats[0].count == 3
@@ -1008,15 +1019,27 @@ def test_dao_fetch_todays_stats_for_service_only_includes_today(notify_db_sessio
# two created email, one failed email, and one created sms
with freeze_time("2001-01-02T04:59:00"):
# just_before_midnight_yesterday
create_notification(template=template, to_field="1", status="delivered")
create_notification(
template=template,
to_field="1",
status=NotificationStatus.DELIVERED,
)
with freeze_time("2001-01-02T05:01:00"):
# just_after_midnight_today
create_notification(template=template, to_field="2", status="failed")
create_notification(
template=template,
to_field="2",
status=NotificationStatus.FAILED,
)
with freeze_time("2001-01-02T12:00:00"):
# right_now
create_notification(template=template, to_field="3", status="created")
create_notification(
template=template,
to_field="3",
status=NotificationStatus.CREATED,
)
stats = dao_fetch_todays_stats_for_service(template.service_id)
@@ -1033,14 +1056,26 @@ def test_dao_fetch_todays_stats_for_service_only_includes_today_when_clocks_spri
template = create_template(service=create_service())
with freeze_time("2021-03-27T23:59:59"):
# just before midnight yesterday in UTC -- not included
create_notification(template=template, to_field="1", status="permanent-failure")
create_notification(
template=template,
to_field="1",
status=NotificationStatus.PERMANENT_FAILURE,
)
with freeze_time("2021-03-28T00:01:00"):
# just after midnight yesterday in UTC -- included
create_notification(template=template, to_field="2", status="failed")
create_notification(
template=template,
to_field="2",
status=NotificationStatus.FAILED,
)
with freeze_time("2021-03-28T12:00:00"):
# we have entered BST at this point but had not for the previous two notifications --included
# collect stats for this timestamp
create_notification(template=template, to_field="3", status="created")
create_notification(
template=template,
to_field="3",
status=NotificationStatus.CREATED,
)
stats = dao_fetch_todays_stats_for_service(template.service_id)
stats = {row.status: row.count for row in stats}
@@ -1057,14 +1092,20 @@ def test_dao_fetch_todays_stats_for_service_only_includes_today_during_bst(
template = create_template(service=create_service())
with freeze_time("2021-03-28T23:59:59"):
# just before midnight BST -- not included
create_notification(template=template, to_field="1", status="permanent-failure")
create_notification(
template=template, to_field="1", status=NotificationStatus.PERMANENT_FAILURE
)
with freeze_time("2021-03-29T04:00:01"):
# just after midnight BST -- included
create_notification(template=template, to_field="2", status="failed")
create_notification(
template=template, to_field="2", status=NotificationStatus.FAILED
)
with freeze_time("2021-03-29T12:00:00"):
# well after midnight BST -- included
# collect stats for this timestamp
create_notification(template=template, to_field="3", status="created")
create_notification(
template=template, to_field="3", status=NotificationStatus.CREATED
)
stats = dao_fetch_todays_stats_for_service(template.service_id)
stats = {row.status: row.count for row in stats}
@@ -1080,15 +1121,21 @@ def test_dao_fetch_todays_stats_for_service_only_includes_today_when_clocks_fall
template = create_template(service=create_service())
with freeze_time("2021-10-30T22:59:59"):
# just before midnight BST -- not included
create_notification(template=template, to_field="1", status="permanent-failure")
create_notification(
template=template, to_field="1", status=NotificationStatus.PERMANENT_FAILURE
)
with freeze_time("2021-10-31T23:00:01"):
# just after midnight BST -- included
create_notification(template=template, to_field="2", status="failed")
create_notification(
template=template, to_field="2", status=NotificationStatus.FAILED
)
# clocks go back to UTC on 31 October at 2am
with freeze_time("2021-10-31T12:00:00"):
# well after midnight -- included
# collect stats for this timestamp
create_notification(template=template, to_field="3", status="created")
create_notification(
template=template, to_field="3", status=NotificationStatus.CREATED
)
stats = dao_fetch_todays_stats_for_service(template.service_id)
stats = {row.status: row.count for row in stats}
@@ -1102,15 +1149,21 @@ def test_dao_fetch_todays_stats_for_service_only_includes_during_utc(notify_db_s
template = create_template(service=create_service())
with freeze_time("2021-10-30T12:59:59"):
# just before midnight UTC -- not included
create_notification(template=template, to_field="1", status="permanent-failure")
create_notification(
template=template, to_field="1", status=NotificationStatus.PERMANENT_FAILURE
)
with freeze_time("2021-10-31T05:00:01"):
# just after midnight UTC -- included
create_notification(template=template, to_field="2", status="failed")
create_notification(
template=template, to_field="2", status=NotificationStatus.FAILED
)
# clocks go back to UTC on 31 October at 2am
with freeze_time("2021-10-31T12:00:00"):
# well after midnight -- included
# collect stats for this timestamp
create_notification(template=template, to_field="3", status="created")
create_notification(
template=template, to_field="3", status=NotificationStatus.CREATED
)
stats = dao_fetch_todays_stats_for_service(template.service_id)
stats = {row.status: row.count for row in stats}
@@ -1126,10 +1179,14 @@ def test_dao_fetch_todays_stats_for_all_services_includes_all_services(
# two services, each with an email and sms notification
service1 = create_service(service_name="service 1", email_from="service.1")
service2 = create_service(service_name="service 2", email_from="service.2")
template_email_one = create_template(service=service1, template_type="email")
template_sms_one = create_template(service=service1, template_type="sms")
template_email_two = create_template(service=service2, template_type="email")
template_sms_two = create_template(service=service2, template_type="sms")
template_email_one = create_template(
service=service1, template_type=TemplateType.EMAIL
)
template_sms_one = create_template(service=service1, template_type=TemplateType.SMS)
template_email_two = create_template(
service=service2, template_type=TemplateType.EMAIL
)
template_sms_two = create_template(service=service2, template_type=TemplateType.SMS)
create_notification(template=template_email_one)
create_notification(template=template_sms_one)
create_notification(template=template_email_two)
@@ -1146,11 +1203,15 @@ def test_dao_fetch_todays_stats_for_all_services_only_includes_today(notify_db_s
template = create_template(service=create_service())
with freeze_time("2001-01-01T23:59:00"):
# just_before_midnight_yesterday
create_notification(template=template, to_field="1", status="delivered")
create_notification(
template=template, to_field="1", status=NotificationStatus.DELIVERED
)
with freeze_time("2001-01-02T05:01:00"):
# just_after_midnight_today
create_notification(template=template, to_field="2", status="failed")
create_notification(
template=template, to_field="2", status=NotificationStatus.FAILED
)
with freeze_time("2001-01-02T12:00:00"):
stats = dao_fetch_todays_stats_for_all_services()
@@ -1164,12 +1225,12 @@ def test_dao_fetch_todays_stats_for_all_services_groups_correctly(notify_db_sess
service1 = create_service(service_name="service 1", email_from="service.1")
service2 = create_service(service_name="service 2", email_from="service.2")
template_sms = create_template(service=service1)
template_email = create_template(service=service1, template_type="email")
template_email = create_template(service=service1, template_type=TemplateType.EMAIL)
template_two = create_template(service=service2)
# service1: 2 sms with status "created" and one "failed", and one email
create_notification(template=template_sms)
create_notification(template=template_sms)
create_notification(template=template_sms, status="failed")
create_notification(template=template_sms, status=NotificationStatus.FAILED)
create_notification(template=template_email)
# service2: 1 sms "created"
create_notification(template=template_two)
@@ -1182,8 +1243,8 @@ def test_dao_fetch_todays_stats_for_all_services_groups_correctly(notify_db_sess
service1.restricted,
service1.active,
service1.created_at,
"sms",
"created",
NotificationType.SMS,
NotificationStatus.CREATED,
2,
) in stats
assert (
@@ -1192,8 +1253,8 @@ def test_dao_fetch_todays_stats_for_all_services_groups_correctly(notify_db_sess
service1.restricted,
service1.active,
service1.created_at,
"sms",
"failed",
NotificationType.SMS,
NotificationStatus.FAILED,
1,
) in stats
assert (
@@ -1202,8 +1263,8 @@ def test_dao_fetch_todays_stats_for_all_services_groups_correctly(notify_db_sess
service1.restricted,
service1.active,
service1.created_at,
"email",
"created",
NotificationType.EMAIL,
NotificationStatus.CREATED,
1,
) in stats
assert (
@@ -1212,8 +1273,8 @@ def test_dao_fetch_todays_stats_for_all_services_groups_correctly(notify_db_sess
service2.restricted,
service2.active,
service2.created_at,
"sms",
"created",
NotificationType.SMS,
NotificationType.CREATED,
1,
) in stats
@@ -1388,7 +1449,9 @@ def test_dao_find_services_sending_to_tv_numbers(notify_db_session, fake_uuid):
template = create_template(service)
for _ in range(0, 5):
create_notification(
template, normalised_to=tv_number, status="permanent-failure"
template,
normalised_to=tv_number,
status=NotificationStatus.PERMANENT_FAILURE,
)
service_6 = create_service(
@@ -1398,27 +1461,35 @@ def test_dao_find_services_sending_to_tv_numbers(notify_db_session, fake_uuid):
template_6 = create_template(service_6)
for _ in range(0, 5):
create_notification(
template_6, normalised_to=tv_number, status="permanent-failure"
template_6,
normalised_to=tv_number,
status=NotificationStatus.PERMANENT_FAILURE,
)
service_2 = create_service(service_name="Service 2") # below threshold is excluded
template_2 = create_template(service_2)
create_notification(template_2, normalised_to=tv_number, status="permanent-failure")
create_notification(
template_2,
normalised_to=tv_number,
status=NotificationStatus.PERMANENT_FAILURE,
)
for _ in range(0, 5):
# test key type is excluded
create_notification(
template_2,
normalised_to=tv_number,
status="permanent-failure",
key_type="test",
status=NotificationStatus.PERMANENT_FAILURE,
key_type=KeyType.TEST,
)
for _ in range(0, 5):
# normal numbers are not counted by the query
create_notification(template_2, normalised_to=normal_number, status="delivered")
create_notification(
template_2, normalised_to=normal_number, status=NotificationStatus.DELIVERED
)
create_notification(
template_2,
normalised_to=normal_number_resembling_tv_number,
status="delivered",
status=NotificationStatus.DELIVERED,
)
start_date = datetime.utcnow() - timedelta(days=1)
@@ -1442,27 +1513,31 @@ def test_dao_find_services_with_high_failure_rates(notify_db_session, fake_uuid)
for service in services:
template = create_template(service)
for _ in range(0, 3):
create_notification(template, status="permanent-failure")
create_notification(template, status="delivered")
create_notification(template, status="sending")
create_notification(template, status="temporary-failure")
create_notification(template, status=NotificationStatus.PERMANENT_FAILURE)
create_notification(template, status=NotificationStatus.DELIVERED)
create_notification(template, status=NotificationStatus.SENDING)
create_notification(template, status=NotificationStatus.TEMPORART_FAILURE)
service_6 = create_service(service_name="Service 6")
with freeze_time("2019-11-30 15:00:00.000000"):
template_6 = create_template(service_6)
for _ in range(0, 4):
create_notification(
template_6, status="permanent-failure"
template_6,
status=NotificationStatus.PERMANENT_FAILURE,
) # notifications too old are excluded
service_2 = create_service(service_name="Service 2")
template_2 = create_template(service_2)
for _ in range(0, 4):
create_notification(
template_2, status="permanent-failure", key_type="test"
template_2,
status=NotificationStatus.PERMANENT_FAILURE,
key_type=KeyType.TEST,
) # test key type is excluded
create_notification(
template_2, status="permanent-failure"
template_2,
status=NotificationStatus.PERMANET_FAILURE,
) # below threshold is excluded
start_date = datetime.utcnow() - timedelta(days=1)