11
11
from rest_framework .views import APIView
12
12
13
13
from rest_framework_jwt import utils
14
+ from rest_framework_jwt .settings import api_settings , DEFAULTS
14
15
from rest_framework_jwt .authentication import JSONWebTokenAuthentication
15
16
16
17
@@ -56,7 +57,7 @@ def setUp(self):
56
57
57
58
def test_post_form_passing_jwt_auth (self ):
58
59
"""
59
- Ensure POSTing json over JWT auth with correct credentials
60
+ Ensure POSTing form over JWT auth with correct credentials
60
61
passes and does not require CSRF
61
62
"""
62
63
payload = utils .jwt_payload_handler (self .user )
@@ -70,7 +71,7 @@ def test_post_form_passing_jwt_auth(self):
70
71
71
72
def test_post_json_passing_jwt_auth (self ):
72
73
"""
73
- Ensure POSTing form over JWT auth with correct credentials
74
+ Ensure POSTing JSON over JWT auth with correct credentials
74
75
passes and does not require CSRF
75
76
"""
76
77
payload = utils .jwt_payload_handler (self .user )
@@ -108,7 +109,7 @@ def test_post_no_jwt_header_failing_jwt_auth(self):
108
109
'/jwt/' , {'example' : 'example' },
109
110
HTTP_AUTHORIZATION = auth , format = 'json' )
110
111
111
- msg = 'Invalid JWT header. No credentials provided.'
112
+ msg = 'Invalid Authorization header. No credentials provided.'
112
113
113
114
self .assertEqual (response .data ['detail' ], msg )
114
115
self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
@@ -123,7 +124,7 @@ def test_post_invalid_jwt_header_failing_jwt_auth(self):
123
124
'/jwt/' , {'example' : 'example' },
124
125
HTTP_AUTHORIZATION = auth , format = 'json' )
125
126
126
- msg = ('Invalid JWT header. Credentials string '
127
+ msg = ('Invalid Authorization header. Credentials string '
127
128
'should not contain spaces.' )
128
129
129
130
self .assertEqual (response .data ['detail' ], msg )
@@ -223,3 +224,23 @@ def test_post_form_passing_jwt_invalid_payload(self):
223
224
224
225
self .assertEqual (response .data ['detail' ], msg )
225
226
self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
227
+
228
+ def test_different_auth_header_prefix (self ):
229
+ """
230
+ Ensure using a different setting for `JWT_AUTH_HEADER_PREFIX` and
231
+ with correct credentials passes.
232
+ """
233
+ api_settings .JWT_AUTH_HEADER_PREFIX = 'Bearer'
234
+
235
+ payload = utils .jwt_payload_handler (self .user )
236
+ token = utils .jwt_encode_handler (payload )
237
+
238
+ auth = 'Bearer {0}' .format (token )
239
+ response = self .csrf_client .post (
240
+ '/jwt/' , {'example' : 'example' },
241
+ HTTP_AUTHORIZATION = auth , format = 'json' )
242
+
243
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
244
+
245
+ # Restore original settings
246
+ api_settings .JWT_AUTH_HEADER_PREFIX = DEFAULTS ['JWT_AUTH_HEADER_PREFIX' ]
0 commit comments