File tree Expand file tree Collapse file tree 1 file changed +9
-7
lines changed Expand file tree Collapse file tree 1 file changed +9
-7
lines changed Original file line number Diff line number Diff line change 13
13
14
14
15
15
class MyMiddleware (object ):
16
+ def __init__ (self , get_response ):
17
+ self .get_response = get_response
16
18
17
- def process_response (self , request , response ):
19
+ def __call__ (self , request ):
20
+ response = self .get_response (request )
18
21
assert hasattr (request , 'user' ), '`user` is not set on request'
19
- assert request .user .is_authenticated (), '`user` is not authenticated'
22
+ assert request .user .is_authenticated , '`user` is not authenticated'
23
+
20
24
return response
21
25
22
26
23
27
@override_settings (ROOT_URLCONF = 'tests.test_middleware' )
24
28
class TestMiddleware (APITestCase ):
29
+
30
+ @override_settings (MIDDLEWARE = ('tests.test_middleware.MyMiddleware' ,))
25
31
def test_middleware_can_access_user_when_processing_response (self ):
26
32
user = User .
objects .
create_user (
'john' ,
'[email protected] ' ,
'password' )
27
33
key = 'abcd1234'
28
34
Token .objects .create (key = key , user = user )
29
35
30
- with self .settings (
31
- MIDDLEWARE_CLASSES = ('tests.test_middleware.MyMiddleware' ,)
32
- ):
33
- auth = 'Token ' + key
34
- self .client .get ('/' , HTTP_AUTHORIZATION = auth )
36
+ self .client .get ('/auth' , HTTP_AUTHORIZATION = 'Token %s' % key )
You can’t perform that action at this time.
0 commit comments