Skip to content

Commit

Permalink
Merge pull request #337 from DalgoT4D/324-dont-force-the-invitation-f…
Browse files Browse the repository at this point in the history
…low-for-existing-users

324 dont force the invitation flow for existing users
  • Loading branch information
Ishankoradia authored Sep 26, 2023
2 parents 8a0701c + 0f84852 commit 3b352c5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -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=
13 changes: 13 additions & 0 deletions ddpui/api/client/user_org_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
25 changes: 25 additions & 0 deletions ddpui/tests/api_tests/test_user_org_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,31 @@ 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"""
Expand Down
15 changes: 15 additions & 0 deletions ddpui/utils/sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ 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"),
},
)

0 comments on commit 3b352c5

Please sign in to comment.