summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2012-09-27 15:36:30 +0300
committerAnssi Kääriäinen <akaariai@gmail.com>2012-09-27 15:36:30 +0300
commit50d573d2c0b3e17cbf1aa240b03b52e4ad0c32cd (patch)
treee0df549efffdafd854a93b399e6f32ab64651564
parent3cbe686af6b6a3f23a607b2eb7497c55d88e8345 (diff)
Fixed #18979 -- Avoid endless loop caused by "val in PermLookupDict"
Fixed by defining __iter__ which raises TypeError. This was done to PermWrapper earlier.
-rw-r--r--django/contrib/auth/context_processors.py5
-rw-r--r--django/contrib/auth/tests/context_processors.py44
2 files changed, 49 insertions, 0 deletions
diff --git a/django/contrib/auth/context_processors.py b/django/contrib/auth/context_processors.py
index 1b6c2eedd0..77face01a7 100644
--- a/django/contrib/auth/context_processors.py
+++ b/django/contrib/auth/context_processors.py
@@ -11,6 +11,11 @@ class PermLookupDict(object):
def __getitem__(self, perm_name):
return self.user.has_perm("%s.%s" % (self.module_name, perm_name))
+ def __iter__(self):
+ # To fix 'item in perms.someapp' and __getitem__ iteraction we need to
+ # define __iter__. See #18979 for details.
+ raise TypeError("PermLookupDict is not iterable.")
+
def __bool__(self):
return self.user.has_module_perms(self.module_name)
__nonzero__ = __bool__ # Python 2
diff --git a/django/contrib/auth/tests/context_processors.py b/django/contrib/auth/tests/context_processors.py
index 4e914133d0..8d87e0ae15 100644
--- a/django/contrib/auth/tests/context_processors.py
+++ b/django/contrib/auth/tests/context_processors.py
@@ -3,11 +3,55 @@ import os
from django.conf import global_settings
from django.contrib.auth import authenticate
from django.contrib.auth.tests.utils import skipIfCustomUser
+from django.contrib.auth.context_processors import PermWrapper, PermLookupDict
from django.db.models import Q
from django.test import TestCase
from django.test.utils import override_settings
+class MockUser(object):
+ def has_module_perm(self, perm):
+ if perm == 'mockapp.someapp':
+ return True
+ return False
+
+ def has_perm(self, perm):
+ if perm == 'someperm':
+ return True
+ return False
+
+
+class PermWrapperTests(TestCase):
+ """
+ Test some details of the PermWrapper implementation.
+ """
+ class EQLimiterObject(object):
+ """
+ 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):
+ """
+ Test that 'something' in PermWrapper doesn't end up in endless loop.
+ """
+ perms = PermWrapper(MockUser())
+ with self.assertRaises(TypeError):
+ self.EQLimiterObject() in perms
+
+ def test_permlookupdict_in(self):
+ pldict = PermLookupDict(MockUser(), 'mockapp')
+ with self.assertRaises(TypeError):
+ self.EQLimiterObject() in pldict
+
+
@skipIfCustomUser
@override_settings(
TEMPLATE_DIRS=(