Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chores:tests] Added AdminActionPermTestMixin #379

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions openwisp_utils/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from unittest import TextTestResult, mock

from django.conf import settings
from django.contrib.auth.models import Permission
from django.db import DEFAULT_DB_ALIAS, connections
from django.test.runner import DiscoverRunner
from django.test.utils import CaptureQueriesContext
Expand Down Expand Up @@ -170,3 +171,86 @@ def assertNumQueries(self, num, func=None, *args, using=DEFAULT_DB_ALIAS, **kwar

with context:
func(*args, **kwargs)


class AdminActionPermTestMixin:
def _test_action_permission(
self,
path,
action,
user,
obj,
message,
message_level='success',
required_perms=None,
extra_payload=None,
):
all_perms = {'add', 'change', 'delete', 'view'}
required_perms = required_perms or all_perms
extra_payload = extra_payload or {}
self.client.force_login(user)
payload = {
'_selected_action': [obj.pk],
'action': action,
'post': 'yes',
**extra_payload,
}
admin_action_option = f'<option value="{action}">'
# Add all permissions to the user except the required permissions
user.user_permissions.add(
*Permission.objects.filter(
codename__in=[
f'{perm}_{obj._meta.model_name}'
for perm in all_perms - set(required_perms)
]
)
)

with self.subTest('Test user lacks necessary permission for action'):
# Verify admin action option is not present on the changelist
response = self.client.get(path)
self.assertNotContains(response, admin_action_option)

# Verify action cannot be performed using forced request
response = self.client.post(path, data=payload, follow=True)
self.assertEqual(response.status_code, 200)
try:
self.assertContains(
response,
'<ul class="messagelist">\n'
'<li class="warning">No action selected.</li>\n'
'</ul>',
html=True,
)
except AssertionError:
# If there is only one admin action available for the user,
# and the user lacks permission for that action, then the
# admin action form will not be displayed on the changelist.
self.assertNotContains(
response, '<label>Action: <select name="action" required>'
)

# Add required permissions to the user
user.user_permissions.add(
*Permission.objects.filter(
codename__in=[
f'{perm}_{obj._meta.model_name}' for perm in required_perms
]
)
)

with self.subTest('Test user has necessary permission for action'):
# Verify admin action option is present on the changelist
response = self.client.get(path, follow=True)
self.assertContains(response, admin_action_option)

# Verify action can be performed
response = self.client.post(path, data=payload, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(
response,
'<ul class="messagelist">\n'
f'<li class="{message_level}">{message}</li>'
'</ul>',
html=True,
)
Loading