From fb3a6cca685f06c64c3330da412ab9c8a5cf19bb Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Wed, 30 Aug 2023 13:13:43 -0700 Subject: [PATCH] notify-api-59 make command to create new service --- Makefile | 2 +- app/commands.py | 24 ++++++++++++++++++++++++ tests/app/test_commands.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index cdb3b558c..ede9f6bcf 100644 --- a/Makefile +++ b/Makefile @@ -76,7 +76,7 @@ test: ## Run tests and create coverage report pipenv run black . pipenv run flake8 . pipenv run isort --check-only ./app ./tests - pipenv run coverage run -m pytest -vv --maxfail=10 + pipenv run coverage run -m pytest --maxfail=10 pipenv run coverage report -m --fail-under=95 pipenv run coverage html -d .coverage_cache diff --git a/app/commands.py b/app/commands.py index 88ddde0e3..f25bf6770 100644 --- a/app/commands.py +++ b/app/commands.py @@ -792,3 +792,27 @@ def update_templates(): data = json.load(f) for d in data: _update_template(d["id"], d["name"], d["type"], d["content"], d["subject"]) + + +@notify_command(name="create-new-service") +@click.option("-n", "--name", required=True, prompt=True) +@click.option("-l", "--message_limit", required=False, default=40000) +@click.option("-r", "--restricted", required=False, default=False) +@click.option("-e", "--email_from", required=True) +@click.option("-c", "--created_by_id", required=True) +def create_new_service(name, message_limit, restricted, email_from, created_by_id): + data = { + "name": name, + "message_limit": message_limit, + "restricted": restricted, + "email_from": email_from, + "created_by_id": created_by_id, + } + + service = Service(**data) + try: + db.session.add(service) + db.session.commit() + except IntegrityError: + print("duplicate service", service.name) + db.session.rollback() diff --git a/tests/app/test_commands.py b/tests/app/test_commands.py index 12274d5ea..adc91273d 100644 --- a/tests/app/test_commands.py +++ b/tests/app/test_commands.py @@ -5,6 +5,7 @@ import pytest from app.commands import ( _update_template, + create_new_service, create_test_user, fix_billable_units, insert_inbound_numbers_from_file, @@ -24,6 +25,7 @@ from app.models import ( Job, Notification, Organization, + Service, Template, User, ) @@ -324,3 +326,39 @@ def test_update_template(notify_db_session, email_2fa_code_template): t = Template.query.all() assert t[0].name == "Example text message template!" + + +def test_create_service_command(notify_db_session, notify_api): + notify_api.test_cli_runner().invoke( + create_test_user, + [ + "--email", + "somebody@fake.gov", + "--mobile_number", + "202-555-5555", + "--password", + "correct horse battery staple", + "--name", + "Fake Personson", + ], + ) + + user = User.query.first() + + service_count = Service.query.count() + + # run the command + result = notify_api.test_cli_runner().invoke( + create_new_service, + ["-e", "somebody@fake.gov", "-n", "Fake Service", "-c", user.id], + ) + print(result) + + # there should be one more service + assert Service.query.count() == service_count + 1 + + # that service should be the one we added + service = Service.query.filter_by(name="Fake Service").first() + assert service.email_from == "somebody@fake.gov" + assert service.restricted is False + assert service.message_limit == 40000