From 13f7fecd5b42d08a2102d31455d5ebec2a5c2b32 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Fri, 22 May 2020 09:36:07 +0100 Subject: [PATCH 1/2] Move function to get archived email address value This function will be used when archiving services too, so it has been renamed and moved to `app/utils.py`. --- app/dao/users_dao.py | 9 ++------- app/utils.py | 5 +++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/dao/users_dao.py b/app/dao/users_dao.py index 3e3924acd..33acd854a 100644 --- a/app/dao/users_dao.py +++ b/app/dao/users_dao.py @@ -11,7 +11,7 @@ from app.dao.service_user_dao import dao_get_service_users_by_user_id from app.dao.dao_utils import transactional from app.errors import InvalidRequest from app.models import (EMAIL_AUTH_TYPE, User, VerifyCode) -from app.utils import escape_special_characters +from app.utils import escape_special_characters, get_archived_db_column_value def _remove_values_for_keys_if_present(dict, keys): @@ -161,7 +161,7 @@ def dao_archive_user(user): user.organisations = [] user.auth_type = EMAIL_AUTH_TYPE - user.email_address = get_archived_email_address(user.email_address) + user.email_address = get_archived_db_column_value(user.email_address) user.mobile_number = None user.password = str(uuid.uuid4()) # Changing the current_session_id signs the user out @@ -185,8 +185,3 @@ def user_can_be_archived(user): return False return True - - -def get_archived_email_address(email_address): - date = datetime.utcnow().strftime("%Y-%m-%d") - return '_archived_{}_{}'.format(date, email_address) diff --git a/app/utils.py b/app/utils.py index 9c9fe915a..e4066c3cb 100644 --- a/app/utils.py +++ b/app/utils.py @@ -126,3 +126,8 @@ def get_notification_table_to_use(service, notification_type, process_day, has_d days_of_retention += 1 return Notification if days_ago <= timedelta(days=days_of_retention) else NotificationHistory + + +def get_archived_db_column_value(column): + date = datetime.utcnow().strftime("%Y-%m-%d") + return f'_archived_{date}_{column}' From 64cd8f39c2ca80f7e0fe7ab89249a3a15b73917f Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Fri, 22 May 2020 09:37:45 +0100 Subject: [PATCH 2/2] Add the date to the service name and email_reply_to when archiving This copies what we do to a user's email address when archiving the user by prefixing it with `_archived_{date}`. We already prefixed the service name and email_reply_to with `_archived`, but this didn't allow a service with the same name to be archived more than once. --- app/dao/services_dao.py | 12 +++++++++--- tests/app/service/test_archived_service.py | 14 ++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index 8a5e35101..3aaf6c6b1 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -53,7 +53,13 @@ from app.models import ( LETTER_TYPE, UPLOAD_LETTERS, ) -from app.utils import email_address_is_nhs, escape_special_characters, get_london_midnight_in_utc, midnight_n_days_ago +from app.utils import ( + email_address_is_nhs, + escape_special_characters, + get_archived_db_column_value, + get_london_midnight_in_utc, + midnight_n_days_ago, +) DEFAULT_SERVICE_PERMISSIONS = [ SMS_TYPE, @@ -260,8 +266,8 @@ def dao_archive_service(service_id): ).filter(Service.id == service_id).one() service.active = False - service.name = '_archived_' + service.name - service.email_from = '_archived_' + service.email_from + service.name = get_archived_db_column_value(service.name) + service.email_from = get_archived_db_column_value(service.email_from) for template in service.templates: if not template.archived: diff --git a/tests/app/service/test_archived_service.py b/tests/app/service/test_archived_service.py index 2b9d59979..9642e5a3e 100644 --- a/tests/app/service/test_archived_service.py +++ b/tests/app/service/test_archived_service.py @@ -6,7 +6,7 @@ from freezegun import freeze_time from app import db from app.models import Service -from app.dao.services_dao import dao_archive_service +from app.dao.services_dao import dao_archive_service, dao_fetch_service_by_id from app.dao.api_key_dao import expire_api_key from app.dao.templates_dao import dao_update_template @@ -50,9 +50,15 @@ def archived_service(client, notify_db, sample_service): return sample_service -def test_deactivating_service_changes_name_and_email(archived_service): - assert archived_service.name == '_archived_Sample service' - assert archived_service.email_from == '_archived_sample.service' +@freeze_time('2018-07-07 12:00:00') +def test_deactivating_service_changes_name_and_email(client, sample_service): + auth_header = create_authorization_header() + client.post('/service/{}/archive'.format(sample_service.id), headers=[auth_header]) + + archived_service = dao_fetch_service_by_id(sample_service.id) + + assert archived_service.name == '_archived_2018-07-07_Sample service' + assert archived_service.email_from == '_archived_2018-07-07_sample.service' def test_deactivating_service_revokes_api_keys(archived_service):