2011-04-02 08:44:47 +00:00
|
|
|
from django.contrib.auth import authenticate
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.contrib.auth.context_processors import PermLookupDict, PermWrapper
|
|
|
|
from django.contrib.auth.models import Permission, User
|
2012-09-30 15:13:23 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2011-04-02 08:44:47 +00:00
|
|
|
from django.db.models import Q
|
2015-04-17 21:38:20 +00:00
|
|
|
from django.test import SimpleTestCase, TestCase, override_settings
|
2014-12-17 21:10:57 +00:00
|
|
|
|
2015-11-07 15:12:37 +00:00
|
|
|
from .settings import AUTH_MIDDLEWARE, AUTH_TEMPLATES
|
2011-05-31 15:19:19 +00:00
|
|
|
|
2011-04-02 08:44:47 +00:00
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class MockUser:
|
2012-09-30 15:13:23 +00:00
|
|
|
def has_module_perms(self, perm):
|
2018-01-03 18:24:02 +00:00
|
|
|
return perm == 'mockapp'
|
2012-09-27 12:36:30 +00:00
|
|
|
|
|
|
|
def has_perm(self, perm):
|
2018-01-03 18:24:02 +00:00
|
|
|
return perm == 'mockapp.someperm'
|
2012-09-27 12:36:30 +00:00
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class PermWrapperTests(SimpleTestCase):
|
2012-09-27 12:36:30 +00:00
|
|
|
"""
|
|
|
|
Test some details of the PermWrapper implementation.
|
|
|
|
"""
|
2017-01-19 07:39:46 +00:00
|
|
|
class EQLimiterObject:
|
2012-09-27 12:36:30 +00:00
|
|
|
"""
|
|
|
|
This object makes sure __eq__ will not be called endlessly.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.eq_calls = 0
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if self.eq_calls > 0:
|
|
|
|
return True
|
|
|
|
self.eq_calls += 1
|
|
|
|
return False
|
|
|
|
|
|
|
|
def test_permwrapper_in(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
'something' in PermWrapper works as expected.
|
2012-09-27 12:36:30 +00:00
|
|
|
"""
|
|
|
|
perms = PermWrapper(MockUser())
|
2012-09-30 15:13:23 +00:00
|
|
|
# Works for modules and full permissions.
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn('mockapp', perms)
|
2017-02-03 01:43:21 +00:00
|
|
|
self.assertNotIn('nonexistent', perms)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn('mockapp.someperm', perms)
|
2017-02-03 01:43:21 +00:00
|
|
|
self.assertNotIn('mockapp.nonexistent', perms)
|
2012-09-27 12:36:30 +00:00
|
|
|
|
|
|
|
def test_permlookupdict_in(self):
|
2012-09-30 15:13:23 +00:00
|
|
|
"""
|
|
|
|
No endless loops if accessed with 'in' - refs #18979.
|
|
|
|
"""
|
2012-09-27 12:36:30 +00:00
|
|
|
pldict = PermLookupDict(MockUser(), 'mockapp')
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
self.EQLimiterObject() in pldict
|
|
|
|
|
2017-09-25 18:50:11 +00:00
|
|
|
def test_iter(self):
|
|
|
|
with self.assertRaisesMessage(TypeError, 'PermWrapper is not iterable.'):
|
|
|
|
iter(PermWrapper(MockUser()))
|
|
|
|
|
2012-09-27 12:36:30 +00:00
|
|
|
|
2016-02-05 20:56:52 +00:00
|
|
|
@override_settings(ROOT_URLCONF='auth_tests.urls', TEMPLATES=AUTH_TEMPLATES)
|
2011-04-02 08:44:47 +00:00
|
|
|
class AuthContextProcessorTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for the ``django.contrib.auth.context_processors.auth`` processor
|
|
|
|
"""
|
2015-02-23 00:53:57 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2016-02-05 20:56:52 +00:00
|
|
|
cls.superuser = User.objects.create_superuser(username='super', password='secret', email='super@example.com')
|
2011-04-02 08:44:47 +00:00
|
|
|
|
2015-11-07 15:12:37 +00:00
|
|
|
@override_settings(MIDDLEWARE=AUTH_MIDDLEWARE)
|
2011-04-02 08:44:47 +00:00
|
|
|
def test_session_not_accessed(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
The session is not accessed simply by including
|
2011-04-02 08:44:47 +00:00
|
|
|
the auth context processor
|
|
|
|
"""
|
|
|
|
response = self.client.get('/auth_processor_no_attr_access/')
|
|
|
|
self.assertContains(response, "Session not accessed")
|
|
|
|
|
2015-11-07 15:12:37 +00:00
|
|
|
@override_settings(MIDDLEWARE=AUTH_MIDDLEWARE)
|
2011-04-02 08:44:47 +00:00
|
|
|
def test_session_is_accessed(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
The session is accessed if the auth context processor
|
2011-04-02 08:44:47 +00:00
|
|
|
is used and relevant attributes accessed.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/auth_processor_attr_access/')
|
|
|
|
self.assertContains(response, "Session accessed")
|
|
|
|
|
|
|
|
def test_perms_attrs(self):
|
2012-09-30 15:13:23 +00:00
|
|
|
u = User.objects.create_user(username='normal', password='secret')
|
|
|
|
u.user_permissions.add(
|
|
|
|
Permission.objects.get(
|
|
|
|
content_type=ContentType.objects.get_for_model(Permission),
|
|
|
|
codename='add_permission'))
|
2016-02-05 20:56:52 +00:00
|
|
|
self.client.force_login(u)
|
2011-04-02 08:44:47 +00:00
|
|
|
response = self.client.get('/auth_processor_perms/')
|
|
|
|
self.assertContains(response, "Has auth permissions")
|
2012-09-30 15:13:23 +00:00
|
|
|
self.assertContains(response, "Has auth.add_permission permissions")
|
2017-02-03 01:43:21 +00:00
|
|
|
self.assertNotContains(response, "nonexistent")
|
2013-10-10 20:42:30 +00:00
|
|
|
|
2012-09-30 15:13:23 +00:00
|
|
|
def test_perm_in_perms_attrs(self):
|
|
|
|
u = User.objects.create_user(username='normal', password='secret')
|
|
|
|
u.user_permissions.add(
|
|
|
|
Permission.objects.get(
|
|
|
|
content_type=ContentType.objects.get_for_model(Permission),
|
|
|
|
codename='add_permission'))
|
|
|
|
self.client.login(username='normal', password='secret')
|
|
|
|
response = self.client.get('/auth_processor_perm_in_perms/')
|
|
|
|
self.assertContains(response, "Has auth permissions")
|
|
|
|
self.assertContains(response, "Has auth.add_permission permissions")
|
2017-02-03 01:43:21 +00:00
|
|
|
self.assertNotContains(response, "nonexistent")
|
2011-04-02 08:44:47 +00:00
|
|
|
|
|
|
|
def test_message_attrs(self):
|
2016-02-05 20:56:52 +00:00
|
|
|
self.client.force_login(self.superuser)
|
2011-04-02 08:44:47 +00:00
|
|
|
response = self.client.get('/auth_processor_messages/')
|
|
|
|
self.assertContains(response, "Message 1")
|
|
|
|
|
|
|
|
def test_user_attrs(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
The lazy objects returned behave just like the wrapped objects.
|
2011-04-02 08:44:47 +00:00
|
|
|
"""
|
|
|
|
# These are 'functional' level tests for common use cases. Direct
|
|
|
|
# testing of the implementation (SimpleLazyObject) is in the 'utils'
|
|
|
|
# tests.
|
|
|
|
self.client.login(username='super', password='secret')
|
|
|
|
user = authenticate(username='super', password='secret')
|
|
|
|
response = self.client.get('/auth_processor_user/')
|
|
|
|
self.assertContains(response, "unicode: super")
|
2016-02-05 20:56:52 +00:00
|
|
|
self.assertContains(response, "id: %d" % self.superuser.pk)
|
2011-04-02 08:44:47 +00:00
|
|
|
self.assertContains(response, "username: super")
|
|
|
|
# bug #12037 is tested by the {% url %} in the template:
|
|
|
|
self.assertContains(response, "url: /userpage/super/")
|
|
|
|
|
2017-01-22 01:02:00 +00:00
|
|
|
# A Q() comparing a user and with another Q() (in an AND or OR fashion).
|
2013-08-04 16:17:10 +00:00
|
|
|
Q(user=response.context['user']) & Q(someflag=True)
|
2011-04-02 08:44:47 +00:00
|
|
|
|
|
|
|
# Tests for user equality. This is hard because User defines
|
|
|
|
# equality in a non-duck-typing way
|
|
|
|
# See bug #12060
|
|
|
|
self.assertEqual(response.context['user'], user)
|
2011-05-31 15:19:19 +00:00
|
|
|
self.assertEqual(user, response.context['user'])
|