| 1 |
from django.contrib.auth import authenticate |
| 2 |
from django.contrib.auth.context_processors import PermLookupDict, PermWrapper |
| 3 |
from django.contrib.auth.models import Permission, User |
| 4 |
from django.contrib.contenttypes.models import ContentType |
| 5 |
from django.db.models import Q |
| 6 |
from django.test import SimpleTestCase, TestCase, override_settings |
| 7 |
|
| 8 |
from .settings import AUTH_MIDDLEWARE, AUTH_TEMPLATES |
| 9 |
|
| 10 |
|
| 11 |
class MockUser: |
| 12 |
def __repr__(self): |
| 13 |
return "MockUser()" |
| 14 |
|
| 15 |
def has_module_perms(self, perm): |
| 16 |
return perm == "mockapp" |
| 17 |
|
| 18 |
def has_perm(self, perm, obj=None): |
| 19 |
return perm == "mockapp.someperm" |
| 20 |
|
| 21 |
|
| 22 |
class PermWrapperTests(SimpleTestCase): |
| 23 |
""" |
| 24 |
Test some details of the PermWrapper implementation. |
| 25 |
""" |
| 26 |
|
| 27 |
class EQLimiterObject: |
| 28 |
""" |
| 29 |
This object makes sure __eq__ will not be called endlessly. |
| 30 |
""" |
| 31 |
|
| 32 |
def __init__(self): |
| 33 |
self.eq_calls = 0 |
| 34 |
|
| 35 |
def __eq__(self, other): |
| 36 |
if self.eq_calls > 0: |
| 37 |
return True |
| 38 |
self.eq_calls += 1 |
| 39 |
return False |
| 40 |
|
| 41 |
def test_repr(self): |
| 42 |
perms = PermWrapper(MockUser()) |
| 43 |
self.assertEqual(repr(perms), "PermWrapper(MockUser())") |
| 44 |
|
| 45 |
def test_permwrapper_in(self): |
| 46 |
""" |
| 47 |
'something' in PermWrapper works as expected. |
| 48 |
""" |
| 49 |
perms = PermWrapper(MockUser()) |
| 50 |
# Works for modules and full permissions. |
| 51 |
self.assertIn("mockapp", perms) |
| 52 |
self.assertNotIn("nonexistent", perms) |
| 53 |
self.assertIn("mockapp.someperm", perms) |
| 54 |
self.assertNotIn("mockapp.nonexistent", perms) |
| 55 |
|
| 56 |
def test_permlookupdict_in(self): |
| 57 |
""" |
| 58 |
No endless loops if accessed with 'in' - refs #18979. |
| 59 |
""" |
| 60 |
pldict = PermLookupDict(MockUser(), "mockapp") |
| 61 |
with self.assertRaises(TypeError): |
| 62 |
self.EQLimiterObject() in pldict |
| 63 |
|
| 64 |
def test_iter(self): |
| 65 |
with self.assertRaisesMessage(TypeError, "PermWrapper is not iterable."): |
| 66 |
iter(PermWrapper(MockUser())) |
| 67 |
|
| 68 |
|
| 69 |
@override_settings(ROOT_URLCONF="auth_tests.urls", TEMPLATES=AUTH_TEMPLATES) |
| 70 |
class AuthContextProcessorTests(TestCase): |
| 71 |
""" |
| 72 |
Tests for the ``django.contrib.auth.context_processors.auth`` processor |
| 73 |
""" |
| 74 |
|
| 75 |
@classmethod |
| 76 |
def setUpTestData(cls): |
| 77 |
cls.superuser = User.objects.create_superuser( |
| 78 |
username="super", password="secret", email="[email protected]" |
| 79 |
) |
| 80 |
|
| 81 |
@override_settings(MIDDLEWARE=AUTH_MIDDLEWARE) |
| 82 |
def test_session_not_accessed(self): |
| 83 |
""" |
| 84 |
The session is not accessed simply by including |
| 85 |
the auth context processor |
| 86 |
""" |
| 87 |
response = self.client.get("/auth_processor_no_attr_access/") |
| 88 |
self.assertContains(response, "Session not accessed") |
| 89 |
|
| 90 |
@override_settings(MIDDLEWARE=AUTH_MIDDLEWARE) |
| 91 |
def test_session_is_accessed(self): |
| 92 |
""" |
| 93 |
The session is accessed if the auth context processor |
| 94 |
is used and relevant attributes accessed. |
| 95 |
""" |
| 96 |
response = self.client.get("/auth_processor_attr_access/") |
| 97 |
self.assertContains(response, "Session accessed") |
| 98 |
|
| 99 |
def test_perms_attrs(self): |
| 100 |
u = User.objects.create_user(username="normal", password="secret") |
| 101 |
u.user_permissions.add( |
| 102 |
Permission.objects.get( |
| 103 |
content_type=ContentType.objects.get_for_model(Permission), |
| 104 |
codename="add_permission", |
| 105 |
) |
| 106 |
) |
| 107 |
self.client.force_login(u) |
| 108 |
response = self.client.get("/auth_processor_perms/") |
| 109 |
self.assertContains(response, "Has auth permissions") |
| 110 |
self.assertContains(response, "Has auth.add_permission permissions") |
| 111 |
self.assertNotContains(response, "nonexistent") |
| 112 |
|
| 113 |
def test_perm_in_perms_attrs(self): |
| 114 |
u = User.objects.create_user(username="normal", password="secret") |
| 115 |
u.user_permissions.add( |
| 116 |
Permission.objects.get( |
| 117 |
content_type=ContentType.objects.get_for_model(Permission), |
| 118 |
codename="add_permission", |
| 119 |
) |
| 120 |
) |
| 121 |
self.client.login(username="normal", password="secret") |
| 122 |
response = self.client.get("/auth_processor_perm_in_perms/") |
| 123 |
self.assertContains(response, "Has auth permissions") |
| 124 |
self.assertContains(response, "Has auth.add_permission permissions") |
| 125 |
self.assertNotContains(response, "nonexistent") |
| 126 |
|
| 127 |
def test_message_attrs(self): |
| 128 |
self.client.force_login(self.superuser) |
| 129 |
response = self.client.get("/auth_processor_messages/") |
| 130 |
self.assertContains(response, "Message 1") |
| 131 |
|
| 132 |
def test_user_attrs(self): |
| 133 |
""" |
| 134 |
The lazy objects returned behave just like the wrapped objects. |
| 135 |
""" |
| 136 |
# These are 'functional' level tests for common use cases. Direct |
| 137 |
# testing of the implementation (SimpleLazyObject) is in the 'utils' |
| 138 |
# tests. |
| 139 |
self.client.login(username="super", password="secret") |
| 140 |
user = authenticate(username="super", password="secret") |
| 141 |
response = self.client.get("/auth_processor_user/") |
| 142 |
self.assertContains(response, "unicode: super") |
| 143 |
self.assertContains(response, "id: %d" % self.superuser.pk) |
| 144 |
self.assertContains(response, "username: super") |
| 145 |
# bug #12037 is tested by the {% url %} in the template: |
| 146 |
self.assertContains(response, "url: /userpage/super/") |
| 147 |
|
| 148 |
# A Q() comparing a user and with another Q() (in an AND or OR fashion). |
| 149 |
Q(user=response.context["user"]) & Q(someflag=True) |
| 150 |
|
| 151 |
# Tests for user equality. This is hard because User defines |
| 152 |
# equality in a non-duck-typing way |
| 153 |
# See bug #12060 |
| 154 |
self.assertEqual(response.context["user"], user) |
| 155 |
self.assertEqual(user, response.context["user"]) |