From 15607c0977a43a308a96813d3a48b26623d8faeb Mon Sep 17 00:00:00 2001 From: Nicholas Staples Date: Wed, 11 May 2016 15:52:49 +0100 Subject: [PATCH] Add server_commands and update readme.md Update command to search for services from the user. --- README.md | 16 ++++++++++++++++ app/commands.py | 28 ++++++++++++---------------- app/dao/api_key_dao.py | 1 - app/dao/services_dao.py | 3 +++ server_commands.py | 29 +++++++++++++++++++++++++++++ tests/app/dao/test_services_dao.py | 3 +++ 6 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 server_commands.py diff --git a/README.md b/README.md index 2ab8131e6..8f1f8cdf5 100644 --- a/README.md +++ b/README.md @@ -73,3 +73,19 @@ scripts/run_celery.sh ``` scripts/run_celery_beat.sh ``` + + +## To remove functional test data + +NOTE: There is assumption that both the server name prefix and user name prefix are followed by a uuid. +The script will search for all services/users with that prefix and only remove it if the prefix is followed by a uuid otherwise it will be skipped. + +Locally +``` +python application.py purge_functional_test_data -u # Remove the user and associated services. +``` + +On the server +``` +python server_commands.py purge_functional_test_data -u # Remove the user and associated services. +``` diff --git a/app/commands.py b/app/commands.py index 25afef91a..197403eba 100644 --- a/app/commands.py +++ b/app/commands.py @@ -3,7 +3,10 @@ from datetime import datetime from decimal import Decimal from flask.ext.script import Command, Manager, Option from app.models import (PROVIDERS, Service, User) -from app.dao.services_dao import delete_service_and_all_associated_db_objects +from app.dao.services_dao import ( + delete_service_and_all_associated_db_objects, + dao_fetch_all_services_by_user +) from app.dao.provider_rates_dao import create_provider_rates from app.dao.users_dao import (delete_model_user, delete_user_verify_codes) @@ -36,22 +39,10 @@ class CreateProviderRateCommand(Command): class PurgeFunctionalTestDataCommand(Command): option_list = ( - Option('-n', '-service-name-prefix', dest="service_name_prefix", help='Functional service name prefix.'), - Option('-u', '-user-email-prefix', dest='user_email_prefix', help="Functional test user email prefix.") + Option('-u', '-user-email-prefix', dest='user_email_prefix', help="Functional test user email prefix."), ) def run(self, service_name_prefix=None, user_email_prefix=None): - if service_name_prefix: - services = Service.query.filter(Service.name.like("{}%".format(service_name_prefix))).all() - for service in services: - # Make sure the second part of the service name is a uuid. - # Just in case someone decides to create a service with that name included in it. - try: - uuid.UUID(service.name.split(service_name_prefix)[1]) - except ValueError: - print("Skipping {} as the service name doesn't contain a UUID.".format(service.name)) - else: - delete_service_and_all_associated_db_objects(service) if user_email_prefix: users = User.query.filter(User.email_address.like("{}%".format(user_email_prefix))).all() for usr in users: @@ -62,5 +53,10 @@ class PurgeFunctionalTestDataCommand(Command): except ValueError: print("Skipping {} as the user email doesn't contain a UUID.".format(usr.email_address)) else: - delete_user_verify_codes(usr) - delete_model_user(usr) + services = dao_fetch_all_services_by_user(usr.id) + if services: + for service in services: + delete_service_and_all_associated_db_objects(service) + else: + delete_user_verify_codes(usr) + delete_model_user(usr) diff --git a/app/dao/api_key_dao.py b/app/dao/api_key_dao.py index d44d16d00..a44adf472 100644 --- a/app/dao/api_key_dao.py +++ b/app/dao/api_key_dao.py @@ -50,7 +50,6 @@ def get_unsigned_secret(key_id): def _generate_secret(token=None): - import uuid if not token: token = uuid.uuid4() serializer = URLSafeSerializer(current_app.config.get('SECRET_KEY')) diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index e59481e74..1563e7fb1 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -95,9 +95,11 @@ def delete_service_and_all_associated_db_objects(service): _delete_commit(InvitedUser.query.filter_by(service=service)) _delete_commit(Permission.query.filter_by(service=service)) _delete_commit(ApiKey.query.filter_by(service=service)) + _delete_commit(ApiKey.get_history_model().query.filter_by(service_id=service.id)) _delete_commit(Notification.query.filter_by(service=service)) _delete_commit(Job.query.filter_by(service=service)) _delete_commit(Template.query.filter_by(service=service)) + _delete_commit(Template.get_history_model().query.filter_by(service_id=service.id)) verify_codes = VerifyCode.query.join(User).filter(User.id.in_([x.id for x in service.users])) list(map(db.session.delete, verify_codes)) @@ -105,6 +107,7 @@ def delete_service_and_all_associated_db_objects(service): users = [x for x in service.users] map(service.users.remove, users) [service.users.remove(x) for x in users] + _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)) diff --git a/server_commands.py b/server_commands.py new file mode 100644 index 000000000..0b72ee425 --- /dev/null +++ b/server_commands.py @@ -0,0 +1,29 @@ +from flask.ext.script import Manager, Server +from flask_migrate import Migrate, MigrateCommand +from app import (create_app, db, commands) +from credstash import getAllSecrets +import os + +default_env_file = '/home/ubuntu/environment' +environment = 'live' + +if os.path.isfile(default_env_file): + with open(default_env_file, 'r') as environment_file: + environment = environment_file.readline().strip() + +# on aws get secrets and export to env +os.environ.update(getAllSecrets(region="eu-west-1")) + +from config import configs + +os.environ['NOTIFY_API_ENVIRONMENT'] = configs[environment] + +application = create_app() + +manager = Manager(application) +migrate = Migrate(application, db) +manager.add_command('db', MigrateCommand) +manager.add_command('purge_functional_test_data', commands.PurgeFunctionalTestDataCommand) + +if __name__ == '__main__': + manager.run() diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 88931aca2..0e431d4e2 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -323,10 +323,13 @@ def test_delete_service_and_associated_objects(notify_db, assert ProviderStatistics.query.count() == 0 assert VerifyCode.query.count() == 0 assert ApiKey.query.count() == 0 + assert ApiKey.get_history_model().query.count() == 0 assert Template.query.count() == 0 + assert Template.get_history_model().query.count() == 0 assert Job.query.count() == 0 assert Notification.query.count() == 0 assert Permission.query.count() == 0 assert User.query.count() == 0 assert InvitedUser.query.count() == 0 assert Service.query.count() == 0 + assert Service.get_history_model().query.count() == 0