Skip to content

Commit

Permalink
Merge pull request #276 from DevDataPlatform/275-invitations-bug
Browse files Browse the repository at this point in the history
allow user to be invited to more than one org at a time
  • Loading branch information
Abhishek-N authored Aug 16, 2023
2 parents 40dd8fb + 6af3e78 commit 9c8a9d9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ USE_AWS_SECRETS_MANAGER=

DBNAME=
DBHOST=
DBPORT=5432
DBUSER=
DBPASSWORD=
DBADMINUSER=
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ jobs:
run: |
eval `ssh-agent -s`
ssh-add - <<< "${{ secrets.SSH_PRIVATE_KEY }}"
ssh -L 8080:localhost:8080 ${{ secrets.SERVER }} -f -N
ssh -L 8080:localhost:8080 -L 5433:${{ secrets.DBHOST }}:5432 ${{ secrets.SERVER }} -f -N
- name: Test with pytest
env:
AIRBYTE_SERVER_HOST: localhost
AIRBYTE_SERVER_PORT: 8000
AIRBYTE_SERVER_APIVER: v1
AIRBYTE_API_TOKEN: ${{ secrets.AIRBYTE_API_TOKEN }}
DBHOST: ${{ secrets.DBHOST }}
DBHOST: localhost
DBPORT: 5433
DBNAME: ${{ secrets.DBNAME }}
DBUSER: ${{ secrets.DBUSER }}
DBPASSWORD: ${{ secrets.DBPASSWORD }}
Expand Down
9 changes: 7 additions & 2 deletions ddpui/api/client/user_org_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ def post_organization_user_invite(request, payload: InvitationSchema):
orguser: OrgUser = request.orguser
frontend_url = os.getenv("FRONTEND_URL")

logger.info(payload)

if orguser.org is None:
raise HttpError(400, "create an organization first")

Expand All @@ -511,7 +513,9 @@ def post_organization_user_invite(request, payload: InvitationSchema):
if invited_role > orguser.role:
raise HttpError(403, "Insufficient permissions for this operation")

invitation = Invitation.objects.filter(invited_email__iexact=invited_email).first()
invitation = Invitation.objects.filter(
invited_email__iexact=invited_email, invited_by__org=orguser.org
).first()
if invitation:
invitation.invited_on = datetime.utcnow()
# if the invitation is already present - trigger the email again
Expand All @@ -520,14 +524,15 @@ def post_organization_user_invite(request, payload: InvitationSchema):
invitation.invited_email, invitation.invited_by.user.email, invite_url
)
logger.info(
f"Invited {invited_email} to join {orguser.org.name} "
f"Resent invitation to {invited_email} to join {orguser.org.name} "
f"with invite code {invitation.invite_code}",
)
return InvitationSchema.from_invitation(invitation)

payload.invited_by = OrgUserResponse.from_orguser(orguser)
payload.invited_on = datetime.utcnow()
payload.invite_code = str(uuid4())

invitation = Invitation.objects.create(
invited_email=invited_email,
invited_role=invited_role,
Expand Down
1 change: 1 addition & 0 deletions ddpui/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"HOST": os.getenv("DBHOST"),
"USER": os.getenv("DBUSER"),
"PASSWORD": os.getenv("DBPASSWORD"),
"PORT": os.getenv("DBPORT"),
}
}

Expand Down
48 changes: 48 additions & 0 deletions ddpui/tests/api_tests/test_user_org_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,54 @@ def test_post_organization_user_invite(mock_sendgrid, orguser):
mock_sendgrid.assert_called_once()


@patch("ddpui.utils.sendgrid.send_invite_user_email", mock_sendgrid=Mock())
def test_post_organization_user_invite_multiple_open_invites(mock_sendgrid, orguser):
"""success test, inviting a new user"""
another_org = Org.objects.create(name="anotherorg", slug="anotherorg")
another_user = User.objects.create(username="anotheruser", email="anotheruser")
another_org_user = OrgUser.objects.create(
org=another_org, user=another_user, role=OrgUserRole.PIPELINE_MANAGER
)
Invitation.objects.create(
invited_email="inivted_email",
invited_role=OrgUserRole.PIPELINE_MANAGER,
invited_by=another_org_user,
invited_on=timezone.as_ist(datetime.now()),
invite_code="invite_code_existing",
)
payload = InvitationSchema(
invited_email="inivted_email",
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

assert (
Invitation.objects.filter(
invited_email=payload.invited_email, invited_by=orguser
).count()
== 0
)
response = post_organization_user_invite(mock_request, payload)
assert (
Invitation.objects.filter(
invited_email=payload.invited_email, invited_by=orguser
).count()
== 1
)

assert response.invited_by.email == orguser.user.email
assert response.invited_by.role == orguser.role
assert response.invited_role == payload.invited_role
assert response.invited_on == payload.invited_on
assert response.invite_code == payload.invite_code
mock_sendgrid.assert_called_once()


@patch("ddpui.utils.sendgrid.send_invite_user_email", mock_sendgrid=Mock())
def test_post_organization_user_invite_lowercase_email(mock_sendgrid, orguser: OrgUser):
"""success test, inviting a new user"""
Expand Down

0 comments on commit 9c8a9d9

Please sign in to comment.