-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[enchancement] hide other org content from operators #32
- operator is able to see and edit users of the organizations in which they are admin - `is_superuser` column hidden in list view - `is_superuser` column hidden in add/change view - operator is only able to add/change users in their own organization organization Closes #32
- Loading branch information
Showing
7 changed files
with
186 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from django.contrib.auth.models import Permission | ||
from ..models import Organization, User, OrganizationUser | ||
|
||
|
||
class TestOrganizationMixin(object): | ||
def _create_user(self, **kwargs): | ||
opts = dict(username='tester', | ||
password='tester', | ||
first_name='Tester', | ||
last_name='Tester', | ||
email='[email protected]') | ||
opts.update(kwargs) | ||
user = User.objects.create_user(**opts) | ||
return user | ||
|
||
def _create_admin(self, **kwargs): | ||
opts = dict(username='admin', | ||
email='[email protected]', | ||
is_superuser=True, | ||
is_staff=True) | ||
opts.update(kwargs) | ||
return self._create_user(**opts) | ||
|
||
def _create_org(self, **kwargs): | ||
options = { | ||
'name': 'test org', | ||
'is_active': True, | ||
'slug': 'test-org' | ||
} | ||
options.update(kwargs) | ||
org = Organization.objects.create(**options) | ||
return org | ||
|
||
def _create_operator(self): | ||
operator = User.objects.create_user(username='operator', | ||
password='tester', | ||
email='[email protected]', | ||
is_staff=True) | ||
operator.user_permissions.add( | ||
*Permission.objects.filter(codename__endswith='user')) | ||
return operator | ||
|
||
def _get_org(self, org_name='test org'): | ||
try: | ||
return Organization.objects.get(name=org_name) | ||
except Organization.DoesNotExist: | ||
return self._create_org() | ||
|
||
def _get_user(self, username='tester'): | ||
try: | ||
return User.objects.get(username=username) | ||
except User.DoesNotExist: | ||
return self._create_user() | ||
|
||
def _get_admin(self, username='admin'): | ||
try: | ||
return User.objects.get(username=username) | ||
except User.DoesNotExist: | ||
return self._create_admin() | ||
|
||
def _get_operator(self, username='operator'): | ||
try: | ||
return User.objects.get(username=username) | ||
except User.DoesNotExist: | ||
return self._create_operator() | ||
|
||
def _create_org_user(self, **kwargs): | ||
options = { | ||
'organization': self._get_org(), | ||
'is_admin': False, | ||
'user': self._get_user() | ||
} | ||
options.update(kwargs) | ||
org = OrganizationUser.objects.create(**options) | ||
return org |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
from django.contrib.auth.models import Permission | ||
from django.core import mail | ||
from django.test import TestCase | ||
from django.urls import reverse | ||
from openwisp_users.models import User | ||
|
||
from ..models import User | ||
from .utils import TestOrganizationMixin | ||
from . import TestOrganizationMixin | ||
|
||
|
||
class TestUsersAdmin(TestOrganizationMixin, TestCase): | ||
|
@@ -20,14 +19,6 @@ class TestUsersAdmin(TestOrganizationMixin, TestCase): | |
'openwisp_users_organizationuser-MAX_NUM_FORMS': 0 | ||
} | ||
|
||
def _create_operator(self, organizations=[]): | ||
operator = User.objects.create_user(username='operator', | ||
password='tester', | ||
email='[email protected]', | ||
is_staff=True) | ||
operator.user_permissions.add(*Permission.objects.filter(codename__endswith='user')) | ||
return operator | ||
|
||
def test_admin_add_user_auto_email(self): | ||
admin = self._create_admin() | ||
self.client.force_login(admin) | ||
|
@@ -124,8 +115,14 @@ def test_admin_change_user_is_superuser_editable(self): | |
html = '<input type="checkbox" name="is_superuser"' | ||
self.assertContains(response, html) | ||
|
||
def test_admin_change_user_is_superuser_readonly(self): | ||
def test_admin_change_user_is_superuser_absent(self): | ||
operator = self._create_operator() | ||
options = { | ||
'organization': self._get_org(), | ||
'is_admin': True, | ||
'user': self._get_operator() | ||
} | ||
self._create_org_user(**options) | ||
self.client.force_login(operator) | ||
response = self.client.get(reverse('admin:openwisp_users_user_change', args=[operator.pk])) | ||
html = '<input type="checkbox" name="is_superuser" checked id="id_is_superuser">' | ||
|
@@ -140,6 +137,12 @@ def test_admin_change_user_permissions_editable(self): | |
|
||
def test_admin_change_user_permissions_readonly(self): | ||
operator = self._create_operator() | ||
options = { | ||
'organization': self._get_org(), | ||
'is_admin': True, | ||
'user': self._get_operator() | ||
} | ||
self._create_org_user(**options) | ||
self.client.force_login(operator) | ||
response = self.client.get(reverse('admin:openwisp_users_user_change', args=[operator.pk])) | ||
html = '<div class="readonly">openwisp_users' | ||
|
@@ -152,9 +155,48 @@ def test_admin_changelist_user_superusers_hidden(self): | |
response = self.client.get(reverse('admin:openwisp_users_user_changelist')) | ||
self.assertNotContains(response, 'admin</a>') | ||
|
||
def test_admin_changelist_operator_org_users_visible(self): | ||
# Check with operator in same organization and is_admin | ||
self._create_org_user() | ||
operator = self._create_operator() | ||
options = { | ||
'organization': self._get_org(), | ||
'is_admin': True, | ||
'user': operator | ||
} | ||
self._create_org_user(**options) | ||
self.client.force_login(operator) | ||
response = self.client.get(reverse('admin:openwisp_users_user_changelist')) | ||
self.assertContains(response, 'tester</a>') | ||
self.assertContains(response, 'operator</a>') | ||
|
||
def test_operator_changelist_superuser_column_hidden(self): | ||
operator = self._create_operator() | ||
options = { | ||
'organization': self._get_org(), | ||
'is_admin': True, | ||
'user': operator | ||
} | ||
self._create_org_user(**options) | ||
self.client.force_login(operator) | ||
response = self.client.get(reverse('admin:openwisp_users_user_changelist')) | ||
self.assertNotContains(response, 'Superuser status</a>') | ||
|
||
def test_admin_changelist_superuser_column_visible(self): | ||
admin = self._create_admin() | ||
self.client.force_login(admin) | ||
response = self.client.get(reverse('admin:openwisp_users_user_changelist')) | ||
self.assertContains(response, 'Superuser status</a>') | ||
|
||
def test_admin_operator_change_superuser_forbidden(self): | ||
admin = self._create_admin() | ||
operator = self._create_operator() | ||
options = { | ||
'organization': self._get_org(), | ||
'is_admin': True, | ||
'user': self._get_operator() | ||
} | ||
self._create_org_user(**options) | ||
self.client.force_login(operator) | ||
response = self.client.get(reverse('admin:openwisp_users_user_change', args=[operator.pk])) | ||
self.assertEqual(response.status_code, 200) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/bash | ||
set -e | ||
flake8 --max-line-length=110 \ | ||
--exclude=migrations,./tests/*settings*.py || exit 1 | ||
--exclude=migrations,./tests/*settings*.py,./setup.py || exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters