Skip to content

Commit

Permalink
[fix] Fixes Organization.add_user INTEGRITY error #360
Browse files Browse the repository at this point in the history
- overode add_user method and removed organization owner logic
as it is being handled by the signal within the package.
- added test.

Fixes #360
  • Loading branch information
mohebmithani committed Oct 27, 2023
1 parent 8403e5a commit 19f307c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions openwisp_users/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ def __str__(self):
class Meta:
abstract = True

def add_user(self, user, is_admin=False, **kwargs):
"""
We override this method from the upstream dependency to
skip the creation of the organization owner, which we perform
automatically via a signal receiver.
Without this change, the add_user method would throw IntegrityError.
"""

if self.users.all().exists():
is_admin = True

OrganizationUser = load_model('openwisp_users', 'OrganizationUser')
return OrganizationUser.objects.create(
user=user, organization=self, is_admin=is_admin
)


class BaseOrganizationUser(models.Model):
"""
Expand Down
7 changes: 7 additions & 0 deletions openwisp_users/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,10 @@ def test_organization_user_string_representation(self):

with self.subTest('Test user first and last names are empty'):
self.assertEqual(str(org_user), f'{user.username} ({org.name})')

def test_add_user_method(self):
org = self._get_org()
user = self._create_user()
org_user = org.add_user(user)

self.assertIsInstance(org_user, OrganizationUser)

0 comments on commit 19f307c

Please sign in to comment.