|
1 | 1 | import django.apps |
2 | 2 | from django.contrib import admin |
3 | 3 | from django.contrib.auth import get_user_model |
4 | | -from django.urls import reverse |
| 4 | +from django.contrib.auth.models import AnonymousUser |
| 5 | +from django.http import HttpRequest |
5 | 6 |
|
6 | 7 | from .dojo_test_case import DojoTestCase |
7 | 8 |
|
@@ -30,37 +31,48 @@ class AdminAccessGate(DojoTestCase): |
30 | 31 | is_staff is a near-superuser bypass under the legacy OS auth model, so |
31 | 32 | /admin/ must require is_superuser. Django's default UserAdmin change |
32 | 33 | form would otherwise let any is_staff user with auth.change_user tick |
33 | | - is_superuser on themselves. |
| 34 | + is_superuser on themselves. Tested at the gate-function level so the |
| 35 | + assertions hold regardless of whether DD_DJANGO_ADMIN_ENABLED mounts |
| 36 | + the admin URLConf in the current environment. |
34 | 37 | """ |
35 | 38 |
|
36 | | - def test_staff_non_superuser_blocked_from_admin(self): |
| 39 | + @staticmethod |
| 40 | + def _request_for(user): |
| 41 | + req = HttpRequest() |
| 42 | + req.user = user |
| 43 | + return req |
| 44 | + |
| 45 | + def test_staff_non_superuser_denied(self): |
37 | 46 | User = get_user_model() |
38 | 47 | password = "testTEST1234!@#$" |
39 | 48 | staff = User.objects.create_user( |
40 | 49 | username="staff-no-root", password=password, is_staff=True, |
41 | 50 | ) |
42 | | - self.client.force_login(staff) |
| 51 | + self.assertFalse(admin.site.has_permission(self._request_for(staff))) |
43 | 52 |
|
44 | | - for url in ( |
45 | | - reverse("admin:index"), |
46 | | - reverse("admin:auth_user_changelist"), |
47 | | - reverse("admin:auth_user_change", args=[staff.id]), |
48 | | - ): |
49 | | - with self.subTest(url=url): |
50 | | - resp = self.client.get(url) |
51 | | - self.assertEqual(resp.status_code, 302, url) |
52 | | - self.assertIn("/admin/login/", resp["Location"], url) |
| 53 | + def test_non_staff_non_superuser_denied(self): |
| 54 | + User = get_user_model() |
| 55 | + password = "testTEST1234!@#$" |
| 56 | + plain = User.objects.create_user(username="plain-user", password=password) |
| 57 | + self.assertFalse(admin.site.has_permission(self._request_for(plain))) |
53 | 58 |
|
54 | | - def test_superuser_can_reach_admin(self): |
| 59 | + def test_anonymous_denied(self): |
| 60 | + self.assertFalse(admin.site.has_permission(self._request_for(AnonymousUser()))) |
| 61 | + |
| 62 | + def test_inactive_superuser_denied(self): |
55 | 63 | User = get_user_model() |
56 | 64 | password = "testTEST1234!@#$" |
57 | 65 | root = User.objects.create_superuser( |
58 | | - username="root-test", email="r@example.com", password=password, |
| 66 | + username="inactive-root", email="i@example.com", password=password, |
59 | 67 | ) |
60 | | - self.client.force_login(root) |
| 68 | + root.is_active = False |
| 69 | + root.save(update_fields=["is_active"]) |
| 70 | + self.assertFalse(admin.site.has_permission(self._request_for(root))) |
61 | 71 |
|
62 | | - self.assertEqual(self.client.get(reverse("admin:index")).status_code, 200) |
63 | | - self.assertEqual( |
64 | | - self.client.get(reverse("admin:auth_user_changelist")).status_code, |
65 | | - 200, |
| 72 | + def test_active_superuser_allowed(self): |
| 73 | + User = get_user_model() |
| 74 | + password = "testTEST1234!@#$" |
| 75 | + root = User.objects.create_superuser( |
| 76 | + username="root-test", email="r@example.com", password=password, |
66 | 77 | ) |
| 78 | + self.assertTrue(admin.site.has_permission(self._request_for(root))) |
0 commit comments