Skip to content

Commit

Permalink
Fix #263
Browse files Browse the repository at this point in the history
  • Loading branch information
seav committed Jun 29, 2016
1 parent d344648 commit 852d731
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
10 changes: 7 additions & 3 deletions cadasta/organization/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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


Expand Down
21 changes: 15 additions & 6 deletions cadasta/organization/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 852d731

Please sign in to comment.