Ticket #15260: test-cache-anonymous-only.patch

File test-cache-anonymous-only.patch, 3.0 KB (added by Brodie Rao, 14 years ago)
  • tests/regressiontests/cache/tests.py

    # HG changeset patch
    # User Brodie Rao <brodie@bitheap.org>
    # Date 1297306155 28800
    # Branch releases/1.2.X
    # Node ID f9460598a915c96ec3d4d883c965eaa06b880713
    # Parent  61c0665bc6c15ed9db42a7ca5da2678efa551934
    Added tests for logged in users and CACHE_MIDDLEWARE_ANONYMOUS_ONLY
    
    diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
    a b import unittest  
    1010import warnings
    1111
    1212from django.conf import settings
     13from django.contrib.auth.models import User
    1314from django.core import management
    1415from django.core.cache import get_cache
    1516from django.core.cache.backends.base import CacheKeyWarning
    class CacheMiddlewareAnonymousOnlyTests(  
    670671        response = self.client.get('/')
    671672        self.failIf('Cookie' in response.get('Vary', ''))
    672673
     674    def test_cache_middleware_anonymous_only_while_logged_in(self):
     675        settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
     676
     677        response = self.client.get('/')
     678        self.assertEqual(response['Cache-Control'], 'max-age=600')
     679
     680        User.objects.create_user(username='user', email='foo@bar.com',
     681                                 password='password')
     682        login = self.client.login(username='user', password='password')
     683        self.assertTrue(login)
     684
     685        response = self.client.get('/user')
     686        self.failIf('Cache-Control' in response)
     687        response = self.client.get('/user-cache-page')
     688        self.failIf('Cache-Control' in response)
     689        response = self.client.get('/user-vary-on-cookie-cache-page')
     690        self.failIf('Cache-Control' in response)
     691        response = self.client.get('/user-cache-page-vary-on-cookie')
     692        self.failIf('Cache-Control' in response)
    673693
    674694if __name__ == '__main__':
    675695    unittest.main()
  • tests/regressiontests/cache/urls.py

    diff --git a/tests/regressiontests/cache/urls.py b/tests/regressiontests/cache/urls.py
    a b from django.conf.urls.defaults import pa  
    22
    33urlpatterns = patterns('regressiontests.cache.views',
    44    (r'^$', 'home'),
     5    (r'^user$', 'user'),
     6    (r'^user-cache-page$', 'user_cache_page'),
     7    (r'^user-vary-on-cookie-cache-page$', 'user_vary_on_cookie_cache_page'),
     8    (r'^user-cache-page-vary-on-cookie$', 'user_cache_page_vary_on_cookie'),
    59)
  • tests/regressiontests/cache/views.py

    diff --git a/tests/regressiontests/cache/views.py b/tests/regressiontests/cache/views.py
    a b  
    11from django.http import HttpResponse
     2from django.views.decorators.cache import cache_page
     3from django.views.decorators.vary import vary_on_cookie
    24
    35def home(request):
    46    return HttpResponse('Hello World!')
     7
     8def user(request):
     9    return HttpResponse(request.user.username)
     10
     11user_cache_page = cache_page(60 * 15)(user)
     12user_vary_on_cookie_cache_page = vary_on_cookie(user_cache_page)
     13user_cache_page_vary_on_cookie = cache_page(60 * 15)(vary_on_cookie(user))
Back to Top