@@ -81,27 +81,44 @@ def test_credentials(self):
81
81
response = self .client .get ('/view/' )
82
82
assert response .data ['auth' ] == 'example'
83
83
84
- def test_force_authenticate (self ):
84
+ def test_force_authenticate_with_user (self ):
85
85
"""
86
- Setting `.force_authenticate()` forcibly authenticates each request.
86
+ Setting `.force_authenticate()` with a user forcibly authenticates each
87
+ request with that user.
87
88
"""
88
- # User only
89
89
user = User .
objects .
create_user (
'example' ,
'[email protected] ' )
90
+
90
91
self .client .force_authenticate (user = user )
91
92
response = self .client .get ('/view/' )
93
+
92
94
assert response .data ['user' ] == 'example'
93
95
assert 'token' not in response .data
94
96
95
- # Token only
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] ' )
96
103
token = Token .objects .create (key = 'xyz' , user = user )
104
+
97
105
self .client .force_authenticate (token = token )
98
106
response = self .client .get ('/view/' )
107
+
99
108
assert response .data ['token' ] == 'xyz'
100
109
assert 'user' not in response .data
101
110
102
- # User and token
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
+
103
119
self .client .force_authenticate (user = user , token = token )
104
120
response = self .client .get ('/view/' )
121
+
105
122
assert response .data ['user' ] == 'example'
106
123
assert response .data ['token' ] == 'xyz'
107
124
0 commit comments