Code

Ticket #16605: 16605-with-tests.diff

File 16605-with-tests.diff, 4.4 KB (added by ramiro, 2 years ago)

Patch from btimby, plus tests

Line 
1diff -r 4f4d617956c0 django/test/client.py
2--- a/django/test/client.py     Mon Mar 05 17:30:27 2012 +0000
3+++ b/django/test/client.py     Thu Mar 08 14:26:00 2012 -0300
4@@ -497,8 +497,19 @@
5         not available.
6         """
7         user = authenticate(**credentials)
8-        if user and user.is_active \
9-                and 'django.contrib.sessions.middleware.SessionMiddleware' in settings.MIDDLEWARE_CLASSES:
10+        sessions_enabled = False
11+        if user and user.is_active:
12+            from django.contrib.sessions.middleware import SessionMiddleware
13+            for middleware in settings.MIDDLEWARE_CLASSES:
14+                try:
15+                    mod, dot, klass_name = middleware.rpartition('.')
16+                    mod = import_module(mod)
17+                    if issubclass(getattr(mod, klass_name, None), SessionMiddleware):
18+                        sessions_enabled = True
19+                        break
20+                except ImportError:
21+                    continue
22+        if sessions_enabled:
23             engine = import_module(settings.SESSION_ENGINE)
24 
25             # Create a fake request to store login details.
26diff -r 4f4d617956c0 tests/regressiontests/test_client_regress/middleware.py
27--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
28+++ b/tests/regressiontests/test_client_regress/middleware.py   Thu Mar 08 14:26:00 2012 -0300
29@@ -0,0 +1,9 @@
30+from django.contrib.sessions.middleware import SessionMiddleware
31+
32+
33+class CustomizedDjangoSessionMiddleware(SessionMiddleware):
34+    pass
35+
36+
37+class ReallyCustomSessionMiddleware(object):
38+    pass
39diff -r 4f4d617956c0 tests/regressiontests/test_client_regress/models.py
40--- a/tests/regressiontests/test_client_regress/models.py       Mon Mar 05 17:30:27 2012 +0000
41+++ b/tests/regressiontests/test_client_regress/models.py       Thu Mar 08 14:26:00 2012 -0300
42@@ -2,6 +2,8 @@
43 """
44 Regression tests for the Test Client, especially the customized assertions.
45 """
46+from __future__ import absolute_import
47+
48 import os
49 import warnings
50 
51@@ -17,6 +19,10 @@
52 from django.template.response import SimpleTemplateResponse
53 from django.http import HttpResponse
54 
55+from . import middleware
56+
57+CUSTOM_SESSION_MIDDLEWARE1 = '%s.CustomizedDjangoSessionMiddleware' % middleware.__name__
58+CUSTOM_SESSION_MIDDLEWARE2 = '%s.ReallyCustomSessionMiddleware' % middleware.__name__
59 
60 class AssertContainsTests(TestCase):
61     def setUp(self):
62@@ -564,17 +570,42 @@
63         self.assertEqual(response.context['user'].username, 'testclient')
64 
65 
66-class NoSessionsAppInstalled(SessionEngineTests):
67-    """#7836 - Test client can exercise sessions even when 'django.contrib.sessions' isn't installed."""
68+class SessionMiddlewareTests(TestCase):
69+    fixtures = ['testdata']
70 
71     # Remove the 'session' contrib app from INSTALLED_APPS
72     @override_settings(INSTALLED_APPS=tuple(filter(lambda a: a!='django.contrib.sessions', settings.INSTALLED_APPS)))
73-    def test_session(self):
74+    def test_no_session_app(self):
75+        """Test client can exercise sessions even when 'django.contrib.sessions' isn't installed (#7836)."""
76         # This request sets a session variable.
77         response = self.client.get('/test_client_regress/set_session/')
78         self.assertEqual(response.status_code, 200)
79         self.assertEqual(self.client.session['session_var'], 'YES')
80 
81+    # Use a custom session middleware that inherits from Django's
82+    @override_settings(
83+        MIDDLEWARE_CLASSES=map(
84+            lambda m: m if m!='django.contrib.sessions.middleware.SessionMiddleware' else CUSTOM_SESSION_MIDDLEWARE1,
85+            settings.MIDDLEWARE_CLASSES
86+        )
87+    )
88+    def test_subclassed_session_middleware(self):
89+        """Test client can login when a subclass of Django SessionMiddleware is in use (#16605)."""
90+        login = self.client.login(username='testclient', password='password')
91+        self.assertTrue(login, 'Could not log in')
92+
93+    # Use a custom session middleware
94+    @override_settings(
95+        MIDDLEWARE_CLASSES=map(
96+            lambda m: m if m!='django.contrib.sessions.middleware.SessionMiddleware' else CUSTOM_SESSION_MIDDLEWARE2,
97+            settings.MIDDLEWARE_CLASSES
98+        )
99+    )
100+    def test_custom_session_middleware(self):
101+        """Test client can login when a custom session middleware is in use."""
102+        login = self.client.login(username='testclient', password='password')
103+        self.assertTrue(login, 'Could not log in')
104+
105 
106 class URLEscapingTests(TestCase):
107     def test_simple_argument_get(self):