diff --git a/app/commands.py b/app/commands.py index b0d1c1964..7b877f2c1 100644 --- a/app/commands.py +++ b/app/commands.py @@ -106,7 +106,7 @@ def purge_functional_test_data(user_email_prefix): """ Remove non-seeded functional test data - users, services, etc. Give an email prefix. Probably "notify-test-preview". + users, services, etc. Give an email prefix. Probably "notify-tests-preview". """ users = User.query.filter(User.email_address.like("{}%".format(user_email_prefix))).all() for usr in users: diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index 171c1b55f..cf2541f60 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -34,6 +34,9 @@ from app.models import ( Service, ServicePermission, ServiceSmsSender, + ServiceEmailReplyTo, + ServiceContactList, + ServiceLetterContact, Template, TemplateHistory, TemplateRedacted, @@ -377,6 +380,9 @@ def delete_service_and_all_associated_db_objects(service): _delete_commit(TemplateRedacted.query.filter(TemplateRedacted.template_id.in_(subq))) _delete_commit(ServiceSmsSender.query.filter_by(service=service)) + _delete_commit(ServiceEmailReplyTo.query.filter_by(service=service)) + _delete_commit(ServiceLetterContact.query.filter_by(service=service)) + _delete_commit(ServiceContactList.query.filter_by(service=service)) _delete_commit(InvitedUser.query.filter_by(service=service)) _delete_commit(Permission.query.filter_by(service=service)) _delete_commit(NotificationHistory.query.filter_by(service=service)) @@ -393,12 +399,14 @@ def delete_service_and_all_associated_db_objects(service): list(map(db.session.delete, verify_codes)) db.session.commit() users = [x for x in service.users] - map(service.users.remove, users) - [service.users.remove(x) for x in users] + for user in users: + user.organisations = [] + service.users.remove(user) _delete_commit(Service.get_history_model().query.filter_by(id=service.id)) db.session.delete(service) db.session.commit() - list(map(db.session.delete, users)) + for user in users: + db.session.delete(user) db.session.commit() diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index dc43e564d..a0e080bfe 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -663,13 +663,15 @@ def test_create_service_and_history_is_transactional(notify_db_session): def test_delete_service_and_associated_objects(notify_db_session): user = create_user() - service = create_service(user=user, service_permissions=None) + organisation = create_organisation() + service = create_service(user=user, service_permissions=None, organisation=organisation) create_user_code(user=user, code='somecode', code_type='email') create_user_code(user=user, code='somecode', code_type='sms') template = create_template(service=service) api_key = create_api_key(service=service) create_notification(template=template, api_key=api_key) create_invited_user(service=service) + user.organisations = [organisation] assert ServicePermission.query.count() == len(( SMS_TYPE, EMAIL_TYPE, LETTER_TYPE, INTERNATIONAL_SMS_TYPE, UPLOAD_LETTERS, @@ -689,6 +691,8 @@ def test_delete_service_and_associated_objects(notify_db_session): assert Service.query.count() == 0 assert Service.get_history_model().query.count() == 0 assert ServicePermission.query.count() == 0 + # the organisation hasn't been deleted + assert Organisation.query.count() == 1 def test_add_existing_user_to_another_service_doesnot_change_old_permissions(notify_db_session):