diff -r 4f4d617956c0 django/test/client.py
a
|
b
|
|
497 | 497 | not available. |
498 | 498 | """ |
499 | 499 | user = authenticate(**credentials) |
500 | | if user and user.is_active \ |
501 | | and 'django.contrib.sessions.middleware.SessionMiddleware' in settings.MIDDLEWARE_CLASSES: |
| 500 | sessions_enabled = False |
| 501 | if user and user.is_active: |
| 502 | from django.contrib.sessions.middleware import SessionMiddleware |
| 503 | for middleware in settings.MIDDLEWARE_CLASSES: |
| 504 | try: |
| 505 | mod, dot, klass_name = middleware.rpartition('.') |
| 506 | mod = import_module(mod) |
| 507 | if issubclass(getattr(mod, klass_name, None), SessionMiddleware): |
| 508 | sessions_enabled = True |
| 509 | break |
| 510 | except ImportError: |
| 511 | continue |
| 512 | if sessions_enabled: |
502 | 513 | engine = import_module(settings.SESSION_ENGINE) |
503 | 514 | |
504 | 515 | # Create a fake request to store login details. |
diff -r 4f4d617956c0 tests/regressiontests/test_client_regress/middleware.py
-
|
+
|
|
| 1 | from django.contrib.sessions.middleware import SessionMiddleware |
| 2 | |
| 3 | |
| 4 | class CustomizedDjangoSessionMiddleware(SessionMiddleware): |
| 5 | pass |
| 6 | |
| 7 | |
| 8 | class ReallyCustomSessionMiddleware(object): |
| 9 | pass |
diff -r 4f4d617956c0 tests/regressiontests/test_client_regress/models.py
a
|
b
|
|
2 | 2 | """ |
3 | 3 | Regression tests for the Test Client, especially the customized assertions. |
4 | 4 | """ |
| 5 | from __future__ import absolute_import |
| 6 | |
5 | 7 | import os |
6 | 8 | import warnings |
7 | 9 | |
… |
… |
|
17 | 19 | from django.template.response import SimpleTemplateResponse |
18 | 20 | from django.http import HttpResponse |
19 | 21 | |
| 22 | from . import middleware |
| 23 | |
| 24 | CUSTOM_SESSION_MIDDLEWARE1 = '%s.CustomizedDjangoSessionMiddleware' % middleware.__name__ |
| 25 | CUSTOM_SESSION_MIDDLEWARE2 = '%s.ReallyCustomSessionMiddleware' % middleware.__name__ |
20 | 26 | |
21 | 27 | class AssertContainsTests(TestCase): |
22 | 28 | def setUp(self): |
… |
… |
|
564 | 570 | self.assertEqual(response.context['user'].username, 'testclient') |
565 | 571 | |
566 | 572 | |
567 | | class NoSessionsAppInstalled(SessionEngineTests): |
568 | | """#7836 - Test client can exercise sessions even when 'django.contrib.sessions' isn't installed.""" |
| 573 | class SessionMiddlewareTests(TestCase): |
| 574 | fixtures = ['testdata'] |
569 | 575 | |
570 | 576 | # Remove the 'session' contrib app from INSTALLED_APPS |
571 | 577 | @override_settings(INSTALLED_APPS=tuple(filter(lambda a: a!='django.contrib.sessions', settings.INSTALLED_APPS))) |
572 | | def test_session(self): |
| 578 | def test_no_session_app(self): |
| 579 | """Test client can exercise sessions even when 'django.contrib.sessions' isn't installed (#7836).""" |
573 | 580 | # This request sets a session variable. |
574 | 581 | response = self.client.get('/test_client_regress/set_session/') |
575 | 582 | self.assertEqual(response.status_code, 200) |
576 | 583 | self.assertEqual(self.client.session['session_var'], 'YES') |
577 | 584 | |
| 585 | # Use a custom session middleware that inherits from Django's |
| 586 | @override_settings( |
| 587 | MIDDLEWARE_CLASSES=map( |
| 588 | lambda m: m if m!='django.contrib.sessions.middleware.SessionMiddleware' else CUSTOM_SESSION_MIDDLEWARE1, |
| 589 | settings.MIDDLEWARE_CLASSES |
| 590 | ) |
| 591 | ) |
| 592 | def test_subclassed_session_middleware(self): |
| 593 | """Test client can login when a subclass of Django SessionMiddleware is in use (#16605).""" |
| 594 | login = self.client.login(username='testclient', password='password') |
| 595 | self.assertTrue(login, 'Could not log in') |
| 596 | |
| 597 | # Use a custom session middleware |
| 598 | @override_settings( |
| 599 | MIDDLEWARE_CLASSES=map( |
| 600 | lambda m: m if m!='django.contrib.sessions.middleware.SessionMiddleware' else CUSTOM_SESSION_MIDDLEWARE2, |
| 601 | settings.MIDDLEWARE_CLASSES |
| 602 | ) |
| 603 | ) |
| 604 | def test_custom_session_middleware(self): |
| 605 | """Test client can login when a custom session middleware is in use.""" |
| 606 | login = self.client.login(username='testclient', password='password') |
| 607 | self.assertTrue(login, 'Could not log in') |
| 608 | |
578 | 609 | |
579 | 610 | class URLEscapingTests(TestCase): |
580 | 611 | def test_simple_argument_get(self): |