2
2
from django .core .urlresolvers import reverse
3
3
from django .test import TestCase
4
4
from django .test .client import Client
5
-
5
+ from django . test . utils import override_settings
6
6
from cached_auth import CACHE_KEY
7
+ from django .conf import settings
8
+
9
+ import cached_auth
7
10
8
11
try :
9
12
from django .contrib .auth import get_user_model
12
15
from django .contrib .auth .models import User
13
16
14
17
18
+ def auth_preprocessor (user , request ):
19
+ user .username = 'test_auth'
20
+ return user
21
+
22
+
15
23
class MiddlewareTest (TestCase ):
16
24
17
25
def setUp (self ):
@@ -25,18 +33,17 @@ def test_anonymous(self):
25
33
# Anonymous user doesn't cause cache to be set
26
34
client = Client ()
27
35
key = CACHE_KEY % self .user .id
28
- response = client .get (reverse ('admin:index' ))
36
+ client .get (reverse ('admin:index' ))
29
37
self .assertEqual (cache .get (key ), None )
30
38
31
-
32
39
def test_cached_middleware (self ):
33
40
client = Client ()
34
41
key = CACHE_KEY % self .user .id
35
42
self .assertEqual (cache .get (key ), None )
36
43
37
44
# Visiting admin causes the cache to be populated
38
45
client .login (username = 'test' , password = 'a' )
39
- response = client .get (reverse ('admin:index' ))
46
+ client .get (reverse ('admin:index' ))
40
47
self .assertEqual (cache .get (key ), self .user )
41
48
42
49
# Changing user model invalidates cache
@@ -48,3 +55,15 @@ def test_cached_middleware(self):
48
55
self .assertEqual (cache .get (key ), self .user )
49
56
self .user .delete ()
50
57
self .assertEqual (cache .get (key ), None )
58
+
59
+ @override_settings (CACHED_AUTH_PREPROCESSOR = 'cached_auth.tests.auth_preprocessor' )
60
+ def test_cached_auth_preprocessor_function (self ):
61
+ reload (cached_auth )
62
+ client = Client ()
63
+ key = CACHE_KEY % self .user .id
64
+ self .assertEqual (cache .get (key ), None )
65
+
66
+ client .login (username = 'test' , password = 'a' )
67
+ client .get (reverse ('admin:index' ))
68
+ user = cache .get (key )
69
+ self .assertEqual (user .username , 'test_auth' )
0 commit comments