From 207c34e5361982852a336a49c69b161f5a8a690f Mon Sep 17 00:00:00 2001 From: leigh-mil Date: Thu, 13 Feb 2020 11:17:09 -0500 Subject: [PATCH 1/2] Send email to user when App Role is created --- atst/jobs.py | 12 ++++++++++++ tests/test_jobs.py | 36 +++++++++++++++++++++++++++++++++++- translations.yaml | 5 ++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/atst/jobs.py b/atst/jobs.py index 77a8c2f6..55ff6027 100644 --- a/atst/jobs.py +++ b/atst/jobs.py @@ -111,6 +111,18 @@ def do_create_user(csp: CloudProviderInterface, application_role_ids=None): db.session.add(app_role) db.session.commit() + username = payload.user_principal_name + send_mail( + recipients=[user.email], + subject=translate("email.app_role_created.subject"), + body=translate( + "email.app_role_created.body", + {"url": app.config.get("AZURE_LOGIN_URL"), "username": username}, + ), + ) + app.logger.info( + f"Application role created notification email sent. User id: {user.id}" + ) def do_create_environment(csp: CloudProviderInterface, environment_id=None): diff --git a/tests/test_jobs.py b/tests/test_jobs.py index 9df83264..cb9ea0ff 100644 --- a/tests/test_jobs.py +++ b/tests/test_jobs.py @@ -153,12 +153,46 @@ def test_create_user_job(session, csp, app): cloud_id=None, ) + session.begin_nested() do_create_user(csp, [app_role.id]) - session.refresh(app_role) + session.rollback() assert app_role.cloud_id +def test_create_user_sends_email(monkeypatch, csp): + mock = Mock() + monkeypatch.setattr("atst.jobs.send_mail", mock) + + portfolio = PortfolioFactory.create( + csp_data={ + "tenant_id": str(uuid4()), + "domain_name": "rebelalliance.onmicrosoft.com", + } + ) + application_1 = ApplicationFactory.create(portfolio=portfolio, cloud_id="321") + application_2 = ApplicationFactory.create(portfolio=portfolio, cloud_id="123") + + user = UserFactory.create() + + app_role_1 = ApplicationRoleFactory.create( + user=user, + application=application_1, + status=ApplicationRoleStatus.ACTIVE, + cloud_id=None, + ) + + app_role_2 = ApplicationRoleFactory.create( + user=user, + application=application_2, + status=ApplicationRoleStatus.ACTIVE, + cloud_id=None, + ) + + do_create_user(csp, [app_role_1.id, app_role_2.id]) + assert mock.call_count == 1 + + def test_dispatch_create_environment(session, monkeypatch): # Given that I have a portfolio with an active CLIN and two environments, # one of which is deleted diff --git a/translations.yaml b/translations.yaml index 4bb463cc..9c77b396 100644 --- a/translations.yaml +++ b/translations.yaml @@ -83,7 +83,10 @@ errors: not_found_sub: This page does not exist. email: application_invite: "{inviter_name} has invited you to a JEDI cloud application" - portfolio_invite: "{inviter_name} has invited you to a JEDI cloud portfolio" + app_role_created: + subject: Application Role Created + body: "Your application role has been created.\nVisit {url}, and use your username, {username}, to log in." + portfolio_invite: "{inviter_name} has invited you to a JEDI cloud portfolio." portfolio_ready: subject: Portfolio Provisioned body: "Your portfolio has been provisioned.\nVisit {password_reset_address}, and use your username, {username}, to create a password." From 0deca6afcb3d79d679d420546318daef45f0f6e8 Mon Sep 17 00:00:00 2001 From: leigh-mil Date: Thu, 13 Feb 2020 11:28:15 -0500 Subject: [PATCH 2/2] Refactor tests --- tests/test_jobs.py | 95 +++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/tests/test_jobs.py b/tests/test_jobs.py index cb9ea0ff..25a0b4c8 100644 --- a/tests/test_jobs.py +++ b/tests/test_jobs.py @@ -135,62 +135,63 @@ def test_create_application_job_is_idempotent(csp): csp.create_application.assert_not_called() -def test_create_user_job(session, csp, app): - portfolio = PortfolioFactory.create( - csp_data={ - "tenant_id": str(uuid4()), - "domain_name": f"rebelalliance.{app.config.get('OFFICE_365_DOMAIN')}", - } - ) - application = ApplicationFactory.create(portfolio=portfolio, cloud_id="321") - user = UserFactory.create( - first_name="Han", last_name="Solo", email="han@example.com" - ) - app_role = ApplicationRoleFactory.create( - application=application, - user=user, - status=ApplicationRoleStatus.ACTIVE, - cloud_id=None, - ) +class TestCreateUserJob: + @pytest.fixture + def portfolio(self, app): + return PortfolioFactory.create( + csp_data={ + "tenant_id": str(uuid4()), + "domain_name": f"rebelalliance.{app.config.get('OFFICE_365_DOMAIN')}", + } + ) - session.begin_nested() - do_create_user(csp, [app_role.id]) - session.rollback() + @pytest.fixture + def app_1(self, portfolio): + return ApplicationFactory.create(portfolio=portfolio, cloud_id="321") - assert app_role.cloud_id + @pytest.fixture + def app_2(self, portfolio): + return ApplicationFactory.create(portfolio=portfolio, cloud_id="123") + @pytest.fixture + def user(self): + return UserFactory.create( + first_name="Han", last_name="Solo", email="han@example.com" + ) -def test_create_user_sends_email(monkeypatch, csp): - mock = Mock() - monkeypatch.setattr("atst.jobs.send_mail", mock) + @pytest.fixture + def app_role_1(self, app_1, user): + return ApplicationRoleFactory.create( + application=app_1, + user=user, + status=ApplicationRoleStatus.ACTIVE, + cloud_id=None, + ) - portfolio = PortfolioFactory.create( - csp_data={ - "tenant_id": str(uuid4()), - "domain_name": "rebelalliance.onmicrosoft.com", - } - ) - application_1 = ApplicationFactory.create(portfolio=portfolio, cloud_id="321") - application_2 = ApplicationFactory.create(portfolio=portfolio, cloud_id="123") + @pytest.fixture + def app_role_2(self, app_2, user): + return ApplicationRoleFactory.create( + application=app_2, + user=user, + status=ApplicationRoleStatus.ACTIVE, + cloud_id=None, + ) - user = UserFactory.create() + def test_create_user_job(self, session, csp, app_role_1): + assert not app_role_1.cloud_id - app_role_1 = ApplicationRoleFactory.create( - user=user, - application=application_1, - status=ApplicationRoleStatus.ACTIVE, - cloud_id=None, - ) + session.begin_nested() + do_create_user(csp, [app_role_1.id]) + session.rollback() - app_role_2 = ApplicationRoleFactory.create( - user=user, - application=application_2, - status=ApplicationRoleStatus.ACTIVE, - cloud_id=None, - ) + assert app_role_1.cloud_id - do_create_user(csp, [app_role_1.id, app_role_2.id]) - assert mock.call_count == 1 + def test_create_user_sends_email(self, monkeypatch, csp, app_role_1, app_role_2): + mock = Mock() + monkeypatch.setattr("atst.jobs.send_mail", mock) + + do_create_user(csp, [app_role_1.id, app_role_2.id]) + assert mock.call_count == 1 def test_dispatch_create_environment(session, monkeypatch):