Send email to user when env role is created

This commit is contained in:
leigh-mil 2020-02-18 13:26:41 -05:00
parent f8dd072340
commit 2c37b16963
3 changed files with 54 additions and 17 deletions

View File

@ -177,7 +177,20 @@ def do_create_environment_role(csp: CloudProviderInterface, environment_role_id=
env_role.cloud_id = result.id
db.session.add(env_role)
db.session.commit()
# TODO: should send notification email to the user, maybe with their portal login name
user = env_role.application_role.user
mail_name = user.full_name.replace(" ", ".").lower()
username = f"{mail_name}@{csp_details.get('tennant_id')}.{app.config.get('OFFICE_365_DOMAIN')}"
send_mail(
recipients=[user.email],
subject=translate("email.azure_account_update.subject"),
body=translate(
"email.azure_account_update.body",
{"url": app.config.get("AZURE_LOGIN_URL"), "username": username},
),
)
app.logger.info(
f"Notification email sent for enivornment role creation. User id: {user.id}"
)
def render_email(template_path, context):

View File

@ -399,23 +399,44 @@ def test_dispatch_create_environment_role(monkeypatch):
mock.delay.assert_called_once_with(environment_role_id=env_role.id)
def test_create_environment_role():
portfolio = PortfolioFactory.create(csp_data={"tenant_id": "123"})
app = ApplicationFactory.create(portfolio=portfolio)
app_role = ApplicationRoleFactory.create(
application=app, status=ApplicationRoleStatus.ACTIVE, cloud_id="123",
)
env = EnvironmentFactory.create(application=app, cloud_id="123")
env_role = EnvironmentRoleFactory.create(
environment=env, application_role=app_role, cloud_id=None
)
class TestCreateEnvironmentRole:
def test_success(self):
portfolio = PortfolioFactory.create(csp_data={"tenant_id": "123"})
app = ApplicationFactory.create(portfolio=portfolio)
app_role = ApplicationRoleFactory.create(
application=app, status=ApplicationRoleStatus.ACTIVE, cloud_id="123",
)
env = EnvironmentFactory.create(application=app, cloud_id="123")
env_role = EnvironmentRoleFactory.create(
environment=env, application_role=app_role, cloud_id=None
)
csp = Mock()
result = UserRoleCSPResult(id="a-cloud-id")
csp.create_user_role = MagicMock(return_value=result)
do_create_environment_role(csp, environment_role_id=env_role.id)
csp = Mock()
result = UserRoleCSPResult(id="a-cloud-id")
csp.create_user_role = MagicMock(return_value=result)
do_create_environment_role(csp, environment_role_id=env_role.id)
assert env_role.cloud_id == "a-cloud-id"
assert env_role.cloud_id == "a-cloud-id"
def test_sends_email(self, monkeypatch):
send_mail = Mock()
monkeypatch.setattr("atst.jobs.send_mail", send_mail)
portfolio = PortfolioFactory.create(csp_data={"tenant_id": "123"})
app = ApplicationFactory.create(portfolio=portfolio)
app_role = ApplicationRoleFactory.create(
application=app, status=ApplicationRoleStatus.ACTIVE, cloud_id="123",
)
env = EnvironmentFactory.create(application=app, cloud_id="123")
env_role = EnvironmentRoleFactory.create(
environment=env, application_role=app_role, cloud_id=None
)
csp = Mock()
result = UserRoleCSPResult(id="a-cloud-id")
csp.create_user_role = MagicMock(return_value=result)
do_create_environment_role(csp, environment_role_id=env_role.id)
assert send_mail.call_count == 1
class TestSendTaskOrderFiles:

View File

@ -86,7 +86,10 @@ email:
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."
azure_account_update:
subject: Azure account update
body: "There has been an update to your Azure account. \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."