Browse Source

Update clean method in Member, fix #1651

pull/1654/head
Hironsan 2 years ago
parent
commit
6edf4fc894
2 changed files with 13 additions and 4 deletions
  1. 2
      backend/members/models.py
  2. 15
      backend/members/tests.py

2
backend/members/models.py

@ -56,7 +56,7 @@ class Member(models.Model):
objects = MemberManager()
def clean(self):
members = self.objects.exclude(id=self.id)
members = self.__class__.objects.exclude(id=self.id)
if members.filter(user=self.user, project=self.project).exists():
message = 'This user is already assigned to a role in this project.'
raise ValidationError(message)

15
backend/members/tests.py

@ -1,6 +1,9 @@
from django.conf import settings
from django.test import TestCase
from django.core.exceptions import ValidationError
from rest_framework import status
from rest_framework.reverse import reverse
from model_mommy import mommy
from roles.models import Role
from members.models import Member
@ -116,9 +119,6 @@ class TestMemberFilter(CRUDMixin):
class TestMemberManager(CRUDMixin):
def setUp(self):
pass
def test_has_role(self):
project = prepare_project()
admin = project.users[0]
@ -129,3 +129,12 @@ class TestMemberManager(CRUDMixin):
]
for role, expect in expected:
self.assertEqual(Member.objects.has_role(project.item, admin, role), expect)
class TestMember(TestCase):
def test_clean(self):
member = mommy.make('Member')
same_user = Member(project=member.project, user=member.user, role=member.role)
with self.assertRaises(ValidationError):
same_user.clean()
Loading…
Cancel
Save