| 1 | # -*- coding: UTF-8 -*- |
| 2 | |
| 3 | from django.core.cache import get_cache |
| 4 | from django.core.cache.backends.filebased import CacheClass as FileCache |
| 5 | from django.http import HttpRequest |
| 6 | from django.http import HttpResponse |
| 7 | from django.utils import cache |
| 8 | from django.utils import translation |
| 9 | |
| 10 | import shutil |
| 11 | import tempfile |
| 12 | import unittest |
| 13 | from django.conf import settings |
| 14 | from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware |
| 15 | |
| 16 | class CacheI18nTest(unittest.TestCase): |
| 17 | def setUp(self): |
| 18 | # Special-case the file cache so we can clean up after ourselves. |
| 19 | if isinstance(cache, FileCache): |
| 20 | self.cache_dir = tempfile.mkdtemp() |
| 21 | self.cache = get_cache("file://%s" % self.cache_dir) |
| 22 | else: |
| 23 | self.cache_dir = None |
| 24 | self.cache = cache |
| 25 | |
| 26 | self.orig_cache_middleware_seconds = settings.CACHE_MIDDLEWARE_SECONDS |
| 27 | self.orig_cache_middleware_key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX |
| 28 | self.orig_cache_backend = settings.CACHE_BACKEND |
| 29 | self.orig_use_i18n = settings.USE_I18N |
| 30 | self.orig_languages = settings.LANGUAGES |
| 31 | |
| 32 | settings.LANGUAGES = ( |
| 33 | ('en', 'English'), |
| 34 | ('es', 'Spanish'), |
| 35 | ) |
| 36 | |
| 37 | def tearDown(self): |
| 38 | if self.cache_dir is not None: |
| 39 | shutil.rmtree(self.cache_dir) |
| 40 | |
| 41 | settings.CACHE_MIDDLEWARE_SECONDS = self.orig_cache_middleware_seconds |
| 42 | settings.CACHE_MIDDLEWARE_KEY_PREFIX = self.orig_cache_middleware_key_prefix |
| 43 | settings.CACHE_BACKEND = self.orig_cache_backend |
| 44 | settings.USE_I18N = self.orig_use_i18n |
| 45 | settings.LANGUAGES = self.orig_languages |
| 46 | translation.deactivate() |
| 47 | |
| 48 | def _get_request(self): |
| 49 | request = HttpRequest() |
| 50 | request.META = { |
| 51 | 'SERVER_NAME': 'testserver', |
| 52 | 'SERVER_PORT': 80, |
| 53 | } |
| 54 | request.path = request.path_info = "/agenda/index/" |
| 55 | return request |
| 56 | |
| 57 | def _get_request_cache(self): |
| 58 | request = HttpRequest() |
| 59 | request.META = { |
| 60 | 'SERVER_NAME': 'testserver', |
| 61 | 'SERVER_PORT': 80, |
| 62 | } |
| 63 | request.path = request.path_info = "/agenda/index/" |
| 64 | request._cache_update_cache = True |
| 65 | request.method = 'GET' |
| 66 | request.session = {} |
| 67 | return request |
| 68 | |
| 69 | def test_cache_key_i18n(self): |
| 70 | settings.USE_I18N = True |
| 71 | request = self._get_request() |
| 72 | lang = translation.get_language() |
| 73 | response = HttpResponse() |
| 74 | key = cache.learn_cache_key(request, response) |
| 75 | self.assertTrue(key.endswith(lang), "Cache keys should include the language name when i18n is active") |
| 76 | key2 = cache.get_cache_key(request) |
| 77 | self.assertEqual(key, key2) |
| 78 | |
| 79 | def test_cache_key_no_i18n (self): |
| 80 | settings.USE_I18N = False |
| 81 | request = self._get_request() |
| 82 | lang = translation.get_language() |
| 83 | response = HttpResponse() |
| 84 | key = cache.learn_cache_key(request, response) |
| 85 | self.assertFalse(key.endswith(lang), "Cache keys shouldn't include the language name when i18n is inactive") |
| 86 | |
| 87 | def test_middleware(self): |
| 88 | |
| 89 | def set_cache(request, lang, msg): |
| 90 | translation.activate(lang) |
| 91 | response = HttpResponse() |
| 92 | response.content= msg |
| 93 | return UpdateCacheMiddleware().process_response(request, response) |
| 94 | |
| 95 | settings.CACHE_MIDDLEWARE_SECONDS = 60 |
| 96 | settings.CACHE_MIDDLEWARE_KEY_PREFIX="test" |
| 97 | settings.CACHE_BACKEND='locmem:///' |
| 98 | settings.USE_I18N = True |
| 99 | en_message ="Hello world!" |
| 100 | es_message ="Hola mundo!" |
| 101 | |
| 102 | request = self._get_request_cache() |
| 103 | set_cache(request, 'en', en_message) |
| 104 | get_cache_data = FetchFromCacheMiddleware().process_request(request) |
| 105 | # Check that we can recover the cache |
| 106 | self.assertNotEqual(get_cache_data.content, None) |
| 107 | self.assertEqual(en_message, get_cache_data.content) |
| 108 | # change the session language and set content |
| 109 | request = self._get_request_cache() |
| 110 | set_cache(request, 'es', es_message) |
| 111 | # change again the language |
| 112 | translation.activate('en') |
| 113 | # retrieve the content from cache |
| 114 | get_cache_data = FetchFromCacheMiddleware().process_request(request) |
| 115 | self.assertEqual(get_cache_data.content, en_message) |
| 116 | # change again the language |
| 117 | translation.activate('es') |
| 118 | get_cache_data = FetchFromCacheMiddleware().process_request(request) |
| 119 | self.assertEqual(get_cache_data.content, es_message) |