From aa7b5d39051bc6b765370d658bc2c2765a93f2f6 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Mon, 6 Nov 2023 13:31:03 -0800 Subject: [PATCH 1/2] notify-api-533 add flask command to promote user to platform admin --- app/commands.py | 15 +++++++++++++++ app/dao/__init__.py | 7 ------- tests/app/conftest.py | 5 +++++ tests/app/db.py | 2 ++ tests/app/test_commands.py | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 7 deletions(-) diff --git a/app/commands.py b/app/commands.py index feab587d4..39d399a2e 100644 --- a/app/commands.py +++ b/app/commands.py @@ -830,3 +830,18 @@ def create_new_service(name, message_limit, restricted, email_from, created_by_i except IntegrityError: print("duplicate service", service.name) db.session.rollback() + + +@notify_command(name="promote-user-to-platform-admin") +@click.option("-u", "--user-email-address", required=True, prompt=True) +def promote_user_to_platform_admin(user_email_address): + print("enter") + user = get_user_by_email(user_email_address) + print(f"user is {user}") + # if not user: + # raise ValueError(f"could not find user for {user_email_address}") + print("ok to proceed") + user.platform_admin = True + db.session.add(user) + db.session.commit() + print("finished") diff --git a/app/dao/__init__.py b/app/dao/__init__.py index 5727c115f..ea889ce47 100644 --- a/app/dao/__init__.py +++ b/app/dao/__init__.py @@ -17,13 +17,6 @@ class DAOClass(object): if _commit: db.session.commit() - def update_instance(self, inst, update_dict, _commit=True): - # Make sure the id is not included in the update_dict - update_dict.pop("id") - self.Meta.model.query.filter_by(id=inst.id).update(update_dict) - if _commit: - db.session.commit() - def delete_instance(self, inst, _commit=True): db.session.delete(inst) if _commit: diff --git a/tests/app/conftest.py b/tests/app/conftest.py index ec31f19c8..ed3e42ea3 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -182,6 +182,11 @@ def sample_user(notify_db_session): return create_user(email="notify@digital.fake.gov") +@pytest.fixture(scope="function") +def sample_platform_admin(notify_db_session): + return create_user(email="notify_pa@digital.fake.gov", platform_admin=True) + + @pytest.fixture(scope="function") def notify_user(notify_db_session): return create_user( diff --git a/tests/app/db.py b/tests/app/db.py index e6adec973..423fccf3b 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -70,6 +70,7 @@ def create_user( state="active", id_=None, name="Test User", + platform_admin=False, ): data = { "id": id_ or uuid.uuid4(), @@ -78,6 +79,7 @@ def create_user( "password": "password", "mobile_number": mobile_number, "state": state, + "platform_admin": platform_admin, } user = User.query.filter_by(email_address=email).first() if not user: diff --git a/tests/app/test_commands.py b/tests/app/test_commands.py index 1d81f1f95..bc9010b6a 100644 --- a/tests/app/test_commands.py +++ b/tests/app/test_commands.py @@ -14,10 +14,12 @@ from app.commands import ( populate_annual_billing_with_the_previous_years_allowance, populate_organization_agreement_details_from_file, populate_organizations_from_file, + promote_user_to_platform_admin, purge_functional_test_data, update_jobs_archived_flag, ) from app.dao.inbound_numbers_dao import dao_get_available_inbound_numbers +from app.dao.users_dao import get_user_by_email from app.models import ( KEY_TYPE_NORMAL, NOTIFICATION_DELIVERED, @@ -395,3 +397,38 @@ def test_create_service_command(notify_db_session, notify_api): assert service.email_from == "somebody@fake.gov" assert service.restricted is False assert service.message_limit == 40000 + + +def test_promote_user_to_platform_admin( + notify_db_session, notify_api, sample_user, sample_platform_admin +): + assert sample_user.platform_admin is False + assert sample_platform_admin.platform_admin is True + + result = notify_api.test_cli_runner().invoke( + promote_user_to_platform_admin, + [ + "-u", + "notify@digital.fake.gov", + ], + ) + print(result) + + user = get_user_by_email("notify@digital.fake.gov") + assert user.platform_admin is True + + +def test_promote_user_to_platform_admin_no_result_found( + notify_db_session, notify_api, sample_user +): + assert sample_user.platform_admin is False + + result = notify_api.test_cli_runner().invoke( + promote_user_to_platform_admin, + [ + "-u", + "notify@digital.fake.asefasefasefasef", + ], + ) + assert "NoResultFound" in str(result) + assert sample_user.platform_admin is False From 7392663b1042f8a9cfdc33f752b3b859b11f009e Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 7 Nov 2023 07:08:42 -0800 Subject: [PATCH 2/2] cleanup --- app/commands.py | 8 ++------ tests/app/test_commands.py | 3 +-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/app/commands.py b/app/commands.py index 39d399a2e..d0f7df409 100644 --- a/app/commands.py +++ b/app/commands.py @@ -835,13 +835,9 @@ def create_new_service(name, message_limit, restricted, email_from, created_by_i @notify_command(name="promote-user-to-platform-admin") @click.option("-u", "--user-email-address", required=True, prompt=True) def promote_user_to_platform_admin(user_email_address): - print("enter") + # If the email address is wrong, sqlalchemy will automatically raise a NoResultFound error which is what we want. + # See tests. user = get_user_by_email(user_email_address) - print(f"user is {user}") - # if not user: - # raise ValueError(f"could not find user for {user_email_address}") - print("ok to proceed") user.platform_admin = True db.session.add(user) db.session.commit() - print("finished") diff --git a/tests/app/test_commands.py b/tests/app/test_commands.py index bc9010b6a..c13ac3dd3 100644 --- a/tests/app/test_commands.py +++ b/tests/app/test_commands.py @@ -405,14 +405,13 @@ def test_promote_user_to_platform_admin( assert sample_user.platform_admin is False assert sample_platform_admin.platform_admin is True - result = notify_api.test_cli_runner().invoke( + notify_api.test_cli_runner().invoke( promote_user_to_platform_admin, [ "-u", "notify@digital.fake.gov", ], ) - print(result) user = get_user_by_email("notify@digital.fake.gov") assert user.platform_admin is True