10
10
from django .urls import path
11
11
12
12
from rest_framework import fields , serializers
13
+ from rest_framework .authtoken .models import Token
13
14
from rest_framework .decorators import api_view
14
15
from rest_framework .response import Response
15
16
from rest_framework .test import (
19
20
20
21
@api_view (['GET' , 'POST' , 'PUT' , 'PATCH' , 'DELETE' , 'OPTIONS' ])
21
22
def view (request ):
22
- return Response ({
23
- 'auth' : request .META .get ('HTTP_AUTHORIZATION' , b'' ),
24
- 'user' : request .user .username
25
- })
23
+ data = {'auth' : request .META .get ('HTTP_AUTHORIZATION' , b'' )}
24
+ if request .user :
25
+ data ['user' ] = request .user .username
26
+ if request .auth :
27
+ data ['token' ] = request .auth .key
28
+ return Response (data )
26
29
27
30
28
31
@api_view (['GET' , 'POST' ])
@@ -78,14 +81,46 @@ def test_credentials(self):
78
81
response = self .client .get ('/view/' )
79
82
assert response .data ['auth' ] == 'example'
80
83
81
- def test_force_authenticate (self ):
84
+ def test_force_authenticate_with_user (self ):
82
85
"""
83
- Setting `.force_authenticate()` forcibly authenticates each request.
86
+ Setting `.force_authenticate()` with a user forcibly authenticates each
87
+ request with that user.
84
88
"""
85
89
user = User .
objects .
create_user (
'example' ,
'[email protected] ' )
86
- self .client .force_authenticate (user )
90
+
91
+ self .client .force_authenticate (user = user )
92
+ response = self .client .get ('/view/' )
93
+
94
+ assert response .data ['user' ] == 'example'
95
+ assert 'token' not in response .data
96
+
97
+ def test_force_authenticate_with_token (self ):
98
+ """
99
+ Setting `.force_authenticate()` with a token forcibly authenticates each
100
+ request with that token.
101
+ """
102
+ user = User .
objects .
create_user (
'example' ,
'[email protected] ' )
103
+ token = Token .objects .create (key = 'xyz' , user = user )
104
+
105
+ self .client .force_authenticate (token = token )
87
106
response = self .client .get ('/view/' )
107
+
108
+ assert response .data ['token' ] == 'xyz'
109
+ assert 'user' not in response .data
110
+
111
+ def test_force_authenticate_with_user_and_token (self ):
112
+ """
113
+ Setting `.force_authenticate()` with a user and token forcibly
114
+ authenticates each request with that user and token.
115
+ """
116
+ user = User .
objects .
create_user (
'example' ,
'[email protected] ' )
117
+ token = Token .objects .create (key = 'xyz' , user = user )
118
+
119
+ self .client .force_authenticate (user = user , token = token )
120
+ response = self .client .get ('/view/' )
121
+
88
122
assert response .data ['user' ] == 'example'
123
+ assert response .data ['token' ] == 'xyz'
89
124
90
125
def test_force_authenticate_with_sessions (self ):
91
126
"""
@@ -102,8 +137,9 @@ def test_force_authenticate_with_sessions(self):
102
137
response = self .client .get ('/session-view/' )
103
138
assert response .data ['active_session' ] is True
104
139
105
- # Force authenticating as `None` should also logout the user session.
106
- self .client .force_authenticate (None )
140
+ # Force authenticating with `None` user and token should also logout
141
+ # the user session.
142
+ self .client .force_authenticate (user = None , token = None )
107
143
response = self .client .get ('/session-view/' )
108
144
assert response .data ['active_session' ] is False
109
145
0 commit comments