From b6873031ce1d8b6699ae7bfd499d22eabb35d79b Mon Sep 17 00:00:00 2001 From: Rohit Chatterjee Date: Sat, 23 Sep 2023 19:55:21 +0530 Subject: [PATCH 1/3] send_youve_been_added_email --- .env.template | 1 + ddpui/utils/sendgrid.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/.env.template b/.env.template index 4655b423..c39e1e56 100644 --- a/.env.template +++ b/.env.template @@ -37,5 +37,6 @@ SENDGRID_SENDER= SENDGRID_RESET_PASSWORD_TEMPLATE= SENDGRID_SIGNUP_TEMPLATE= SENDGRID_INVITE_USER_TEMPLATE= +SENDGRID_YOUVE_BEEN_ADDED_TEMPLATE= PREFECT_NOTIFICATIONS_WEBHOOK_KEY= \ No newline at end of file diff --git a/ddpui/utils/sendgrid.py b/ddpui/utils/sendgrid.py index fead6ee5..a15302ba 100644 --- a/ddpui/utils/sendgrid.py +++ b/ddpui/utils/sendgrid.py @@ -72,3 +72,17 @@ def send_invite_user_email( to_email, {"url": invite_url, "invited_by_email": invited_by_email}, ) + + +def send_youve_been_added_email(to_email: str, added_by: str, org_name: str) -> None: + """sends an email notification informing an existing dalgo user that they have + been granted access to a new org""" + send_template_message( + os.getenv("SENDGRID_YOUVE_BEEN_ADDED_TEMPLATE"), + to_email, + { + "org_name": org_name, + "added_by": added_by, + "url": os.getenv("FRONTEND_URL"), + }, + ) From fe15710113d6dd4c79a1c163c55ecc3402f9df89 Mon Sep 17 00:00:00 2001 From: Rohit Chatterjee Date: Sat, 23 Sep 2023 20:10:30 +0530 Subject: [PATCH 2/3] auto-add to new org if user already exists --- ddpui/api/client/user_org_api.py | 13 +++++++++++ ddpui/tests/api_tests/test_user_org_api.py | 26 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/ddpui/api/client/user_org_api.py b/ddpui/api/client/user_org_api.py index 0b6fd0f1..38905a22 100644 --- a/ddpui/api/client/user_org_api.py +++ b/ddpui/api/client/user_org_api.py @@ -536,6 +536,19 @@ def post_organization_user_invite(request, payload: InvitationSchema): if invited_role > orguser.role: raise HttpError(403, "Insufficient permissions for this operation") + existing_user = User.objects.filter(email__iexact=invited_email).first() + + if existing_user: + logger.info("user exists, creating new OrgUser") + OrgUser.objects.create(user=existing_user, org=orguser.org, role=invited_role) + sendgrid.send_youve_been_added_email( + invited_email, orguser.user.email, orguser.org.name + ) + return InvitationSchema( + invited_email=invited_email, + invited_role_slug=payload.invited_role_slug, + ) + invitation = Invitation.objects.filter( invited_email__iexact=invited_email, invited_by__org=orguser.org ).first() diff --git a/ddpui/tests/api_tests/test_user_org_api.py b/ddpui/tests/api_tests/test_user_org_api.py index 5710f447..ed104092 100644 --- a/ddpui/tests/api_tests/test_user_org_api.py +++ b/ddpui/tests/api_tests/test_user_org_api.py @@ -830,6 +830,32 @@ def test_post_organization_user_invite_lowercase_email(mock_sendgrid, orguser: O mock_sendgrid.assert_called_once() +@patch("ddpui.utils.sendgrid.send_youve_been_added_email", mock_sendgrid=Mock()) +def test_post_organization_user_invite_user_exists(mock_sendgrid, orguser: OrgUser): + """success test, inviting an existing user""" + + user = User.objects.create(email="existinguser", username="existinguser") + assert OrgUser.objects.filter(user=user).count() == 0 + + payload = InvitationSchema( + invited_email="existinguser", + invited_role_slug="report_viewer", + invited_by=None, + invited_on=timezone.as_ist(datetime.now()), + invite_code="invite_code", + ) + + mock_request = Mock() + mock_request.orguser = orguser + + response = post_organization_user_invite(mock_request, payload) + mock_sendgrid.assert_called_once() + + assert OrgUser.objects.filter(user=user).count() == 1 + assert OrgUser.objects.filter(user=user, org=orguser.org).count() == 1 + assert response.invited_role == payload.invited_role + + # ================================================================================ def test_post_organization_user_accept_invite_fail(orguser): """failing test, invalid invite code""" From 0f84852f1f0b15a57b57c1acf9c017254577c64d Mon Sep 17 00:00:00 2001 From: Rohit Chatterjee Date: Sat, 23 Sep 2023 20:15:53 +0530 Subject: [PATCH 3/3] deepsource fixes --- ddpui/tests/api_tests/test_user_org_api.py | 1 - ddpui/utils/sendgrid.py | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ddpui/tests/api_tests/test_user_org_api.py b/ddpui/tests/api_tests/test_user_org_api.py index ed104092..41c52f53 100644 --- a/ddpui/tests/api_tests/test_user_org_api.py +++ b/ddpui/tests/api_tests/test_user_org_api.py @@ -833,7 +833,6 @@ def test_post_organization_user_invite_lowercase_email(mock_sendgrid, orguser: O @patch("ddpui.utils.sendgrid.send_youve_been_added_email", mock_sendgrid=Mock()) def test_post_organization_user_invite_user_exists(mock_sendgrid, orguser: OrgUser): """success test, inviting an existing user""" - user = User.objects.create(email="existinguser", username="existinguser") assert OrgUser.objects.filter(user=user).count() == 0 diff --git a/ddpui/utils/sendgrid.py b/ddpui/utils/sendgrid.py index a15302ba..b0ed9739 100644 --- a/ddpui/utils/sendgrid.py +++ b/ddpui/utils/sendgrid.py @@ -76,7 +76,8 @@ def send_invite_user_email( def send_youve_been_added_email(to_email: str, added_by: str, org_name: str) -> None: """sends an email notification informing an existing dalgo user that they have - been granted access to a new org""" + been granted access to a new org + """ send_template_message( os.getenv("SENDGRID_YOUVE_BEEN_ADDED_TEMPLATE"), to_email,