diff --git a/cadasta/organization/forms.py b/cadasta/organization/forms.py index ce23b8ba5..98708fd05 100644 --- a/cadasta/organization/forms.py +++ b/cadasta/organization/forms.py @@ -127,6 +127,12 @@ def clean_identifier(self): identifier=identifier) except (User.DoesNotExist, User.MultipleObjectsReturned) as e: raise forms.ValidationError(e) + num_roles = OrganizationRole.objects.filter( + user=self.user, organization=self.organization).count() + if num_roles != 0: + raise forms.ValidationError( + _("User is already a member of the organization."), + code='member_already') def save(self): if self.errors: @@ -136,9 +142,7 @@ def save(self): ) self.instance = OrganizationRole.objects.create( - user=self.user, - organization=self.organization - ) + organization=self.organization, user=self.user) return self.instance diff --git a/cadasta/organization/tests/test_forms.py b/cadasta/organization/tests/test_forms.py index 28614e021..7ad533a5a 100644 --- a/cadasta/organization/tests/test_forms.py +++ b/cadasta/organization/tests/test_forms.py @@ -129,23 +129,27 @@ def test_remove_all_contacts(self): class AddOrganizationMemberFormTest(UserTestCase): + def setUp(self): + super().setUp() + self.org = OrganizationFactory.create() + self.user = UserFactory.create() + def _save(self, identifier=None, identifier_field=None, ok=True): - org = OrganizationFactory.create() - user = UserFactory.create() if identifier_field is not None: - identifier = getattr(user, identifier_field) + identifier = getattr(self.user, identifier_field) data = {'identifier': identifier} - form = forms.AddOrganizationMemberForm(data, organization=org) + form = forms.AddOrganizationMemberForm(data, organization=self.org) + num_roles_before = len(OrganizationRole.objects.all()) if ok: form.save() assert form.is_valid() is True assert OrganizationRole.objects.filter( - organization=org, user=user).count() == 1 + organization=self.org, user=self.user).count() == 1 else: with raises(ValueError): form.save() assert form.is_valid() is False - assert OrganizationRole.objects.count() == 0 + assert OrganizationRole.objects.count() == num_roles_before def test_add_with_username(self): self._save(identifier_field='username') @@ -156,6 +160,11 @@ def test_add_with_email(self): def test_add_non_existing_user(self): self._save(identifier='some-user', ok=False) + def test_add_already_member_user(self): + OrganizationRole.objects.create( + organization=self.org, user=self.user) + self._save(identifier_field='username', ok=False) + class EditOrganizationMemberFormTest(UserTestCase): def test_edit_org_role(self): diff --git a/functional_tests/organizations/test_organization_member_list.py b/functional_tests/organizations/test_organization_member_list.py index e2deae89a..2ac300ae9 100644 --- a/functional_tests/organizations/test_organization_member_list.py +++ b/functional_tests/organizations/test_organization_member_list.py @@ -59,6 +59,13 @@ def test_adding_members(self): error_message = 'User with username or email darthvader does not exist' assert error_list == error_message + input_box = page.click_on_input() + input_box.clear() + input_box.send_keys("admin_user") + error_list = page.click_submit_button(success=False) + error_message = 'User is already a member of the organization.' + assert error_list == error_message + input_box = page.click_on_input() input_box.clear() input_box.send_keys("hansolo")