Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check for existing member before inviting to avoid IntegrityError #167

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions taiga/projects/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#
# Copyright (c) 2021-present Kaleidos INC

from collections import Counter

from django.db.models import Q
from django.utils.translation import gettext as _

Expand Down Expand Up @@ -270,10 +272,17 @@ class MembersBulkValidator(ProjectExistsValidator, validators.Validator):
def validate_bulk_memberships(self, attrs, source):
project_id = attrs["project_id"]
role_ids = [r["role_id"] for r in attrs["bulk_memberships"]]
username_members = [r["username"] for r in attrs["bulk_memberships"]]

if Role.objects.filter(project_id=project_id, id__in=role_ids).count() != len(set(role_ids)):
raise ValidationError(_("Invalid role ids. All roles must belong to the same project."))

if any(username > 1 for username in Counter(username_members).values()):
raise ValidationError(_("You are attempting to invite the same email address multiple times."))

if models.Membership.objects.filter(project_id=project_id, email__in=username_members).exists():
raise ValidationError(_("The user with this email address is already added to the project."))

return attrs


Expand Down
43 changes: 43 additions & 0 deletions tests/integration/test_memberships.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,49 @@ def test_api_create_bulk_members(client):
assert(user_ids.issubset(response_user_ids))


def test_api_create_bulk_members_with_same_email(client):
project = f.ProjectFactory()
tester = f.RoleFactory(project=project, name="Tester")
gamer = f.RoleFactory(project=project, name="Gamer")
f.MembershipFactory(project=project, user=project.owner, is_admin=True)

url = reverse("memberships-bulk-create")

data = {
"project_id": project.id,
"bulk_memberships": [
{"role_id": tester.pk, "username": "[email protected]"},
{"role_id": gamer.pk, "username": "[email protected]"},
],
}
client.login(project.owner)
response = client.json.post(url, json.dumps(data))

assert response.status_code == 400
assert "bulk_memberships" in response.data


def test_api_create_bulk_members_exists_member(client):
project = f.ProjectFactory()
tester = f.RoleFactory(project=project, name="Tester")
f.MembershipFactory(project=project, user=project.owner, is_admin=True)
f.MembershipFactory(project=project, email="[email protected]")

url = reverse("memberships-bulk-create")

data = {
"project_id": project.id,
"bulk_memberships": [
{"role_id": tester.pk, "username": "[email protected]"},
],
}
client.login(project.owner)
response = client.json.post(url, json.dumps(data))

assert response.status_code == 400
assert "bulk_memberships" in response.data


def test_api_create_bulk_members_invalid_user_id(client):
project = f.ProjectFactory()
john = f.UserFactory.create()
Expand Down
Loading