@@ -35,6 +35,10 @@ class CustomTokenAuthentication(TokenAuthentication):
35
35
model = CustomToken
36
36
37
37
38
+ class CustomKeywordTokenAuthentication (TokenAuthentication ):
39
+ keyword = 'bearer'
40
+
41
+
38
42
class MockView (APIView ):
39
43
permission_classes = (permissions .IsAuthenticated ,)
40
44
@@ -53,6 +57,7 @@ def put(self, request):
53
57
url (r'^basic/$' , MockView .as_view (authentication_classes = [BasicAuthentication ])),
54
58
url (r'^token/$' , MockView .as_view (authentication_classes = [TokenAuthentication ])),
55
59
url (r'^customtoken/$' , MockView .as_view (authentication_classes = [CustomTokenAuthentication ])),
60
+ url (r'^customkeywordtoken/$' , MockView .as_view (authentication_classes = [CustomKeywordTokenAuthentication ])),
56
61
url (r'^auth-token/$' , 'rest_framework.authtoken.views.obtain_auth_token' ),
57
62
url (r'^auth/' , include ('rest_framework.urls' , namespace = 'rest_framework' )),
58
63
]
@@ -166,6 +171,7 @@ class BaseTokenAuthTests(object):
166
171
urls = 'tests.test_authentication'
167
172
model = None
168
173
path = None
174
+ header_prefix = 'Token '
169
175
170
176
def setUp (self ):
171
177
self .csrf_client = APIClient (enforce_csrf_checks = True )
@@ -179,31 +185,31 @@ def setUp(self):
179
185
180
186
def test_post_form_passing_token_auth (self ):
181
187
"""Ensure POSTing json over token auth with correct credentials passes and does not require CSRF"""
182
- auth = 'Token ' + self .key
188
+ auth = self . header_prefix + self .key
183
189
response = self .csrf_client .post (self .path , {'example' : 'example' }, HTTP_AUTHORIZATION = auth )
184
190
self .assertEqual (response .status_code , status .HTTP_200_OK )
185
191
186
192
def test_fail_post_form_passing_nonexistent_token_auth (self ):
187
193
# use a nonexistent token key
188
- auth = 'Token wxyz6789'
194
+ auth = self . header_prefix + ' wxyz6789'
189
195
response = self .csrf_client .post (self .path , {'example' : 'example' }, HTTP_AUTHORIZATION = auth )
190
196
self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
191
197
192
198
def test_fail_post_form_passing_invalid_token_auth (self ):
193
199
# add an 'invalid' unicode character
194
- auth = 'Token ' + self .key + "¸"
200
+ auth = self . header_prefix + self .key + "¸"
195
201
response = self .csrf_client .post (self .path , {'example' : 'example' }, HTTP_AUTHORIZATION = auth )
196
202
self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
197
203
198
204
def test_post_json_passing_token_auth (self ):
199
205
"""Ensure POSTing form over token auth with correct credentials passes and does not require CSRF"""
200
- auth = "Token " + self .key
206
+ auth = self . header_prefix + self .key
201
207
response = self .csrf_client .post (self .path , {'example' : 'example' }, format = 'json' , HTTP_AUTHORIZATION = auth )
202
208
self .assertEqual (response .status_code , status .HTTP_200_OK )
203
209
204
210
def test_post_json_makes_one_db_query (self ):
205
211
"""Ensure that authenticating a user using a token performs only one DB query"""
206
- auth = "Token " + self .key
212
+ auth = self . header_prefix + self .key
207
213
208
214
def func_to_test ():
209
215
return self .csrf_client .post (self .path , {'example' : 'example' }, format = 'json' , HTTP_AUTHORIZATION = auth )
@@ -273,6 +279,12 @@ class CustomTokenAuthTests(BaseTokenAuthTests, TestCase):
273
279
path = '/customtoken/'
274
280
275
281
282
+ class CustomKeywordTokenAuthTests (BaseTokenAuthTests , TestCase ):
283
+ model = Token
284
+ path = '/customkeywordtoken/'
285
+ header_prefix = 'Bearer '
286
+
287
+
276
288
class IncorrectCredentialsTests (TestCase ):
277
289
def test_incorrect_credentials (self ):
278
290
"""
0 commit comments