Ticket #16605: 16605-with-tests.diff

File 16605-with-tests.diff, 4.4 KB (added by Ramiro Morales, 13 years ago)

Patch from btimby, plus tests

  • django/test/client.py

    diff -r 4f4d617956c0 django/test/client.py
    a b  
    497497        not available.
    498498        """
    499499        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:
    502513            engine = import_module(settings.SESSION_ENGINE)
    503514
    504515            # Create a fake request to store login details.
  • new file tests/regressiontests/test_client_regress/middleware.py

    diff -r 4f4d617956c0 tests/regressiontests/test_client_regress/middleware.py
    - +  
     1from django.contrib.sessions.middleware import SessionMiddleware
     2
     3
     4class CustomizedDjangoSessionMiddleware(SessionMiddleware):
     5    pass
     6
     7
     8class ReallyCustomSessionMiddleware(object):
     9    pass
  • tests/regressiontests/test_client_regress/models.py

    diff -r 4f4d617956c0 tests/regressiontests/test_client_regress/models.py
    a b  
    22"""
    33Regression tests for the Test Client, especially the customized assertions.
    44"""
     5from __future__ import absolute_import
     6
    57import os
    68import warnings
    79
     
    1719from django.template.response import SimpleTemplateResponse
    1820from django.http import HttpResponse
    1921
     22from . import middleware
     23
     24CUSTOM_SESSION_MIDDLEWARE1 = '%s.CustomizedDjangoSessionMiddleware' % middleware.__name__
     25CUSTOM_SESSION_MIDDLEWARE2 = '%s.ReallyCustomSessionMiddleware' % middleware.__name__
    2026
    2127class AssertContainsTests(TestCase):
    2228    def setUp(self):
     
    564570        self.assertEqual(response.context['user'].username, 'testclient')
    565571
    566572
    567 class NoSessionsAppInstalled(SessionEngineTests):
    568     """#7836 - Test client can exercise sessions even when 'django.contrib.sessions' isn't installed."""
     573class SessionMiddlewareTests(TestCase):
     574    fixtures = ['testdata']
    569575
    570576    # Remove the 'session' contrib app from INSTALLED_APPS
    571577    @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)."""
    573580        # This request sets a session variable.
    574581        response = self.client.get('/test_client_regress/set_session/')
    575582        self.assertEqual(response.status_code, 200)
    576583        self.assertEqual(self.client.session['session_var'], 'YES')
    577584
     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
    578609
    579610class URLEscapingTests(TestCase):
    580611    def test_simple_argument_get(self):
Back to Top