14
14
from rest_framework_jwt .authentication import JSONWebTokenAuthentication
15
15
16
16
17
+ DJANGO_OAUTH2_PROVIDER_NOT_INSTALLED = 'django-oauth2-provider not installed'
18
+
17
19
factory = APIRequestFactory ()
18
20
19
21
@@ -31,10 +33,14 @@ def post(self, request):
31
33
'' ,
32
34
(r'^jwt/$' , MockView .as_view (
33
35
authentication_classes = [JSONWebTokenAuthentication ])),
36
+
34
37
(r'^jwt-oauth2/$' , MockView .as_view (
35
- authentication_classes = [JSONWebTokenAuthentication , OAuth2Authentication ])),
38
+ authentication_classes = [
39
+ JSONWebTokenAuthentication , OAuth2Authentication ])),
40
+
36
41
(r'^oauth2-jwt/$' , MockView .as_view (
37
- authentication_classes = [OAuth2Authentication , JSONWebTokenAuthentication ])),
42
+ authentication_classes = [
43
+ OAuth2Authentication , JSONWebTokenAuthentication ])),
38
44
)
39
45
40
46
@@ -56,7 +62,7 @@ def test_post_form_passing_jwt_auth(self):
56
62
payload = utils .jwt_payload_handler (self .user )
57
63
token = utils .jwt_encode_handler (payload )
58
64
59
- auth = 'Bearer {0}' .format (token )
65
+ auth = 'JWT {0}' .format (token )
60
66
response = self .csrf_client .post (
61
67
'/jwt/' , {'example' : 'example' }, HTTP_AUTHORIZATION = auth )
62
68
@@ -70,7 +76,7 @@ def test_post_json_passing_jwt_auth(self):
70
76
payload = utils .jwt_payload_handler (self .user )
71
77
token = utils .jwt_encode_handler (payload )
72
78
73
- auth = 'Bearer {0}' .format (token )
79
+ auth = 'JWT {0}' .format (token )
74
80
response = self .csrf_client .post (
75
81
'/jwt/' , {'example' : 'example' },
76
82
HTTP_AUTHORIZATION = auth , format = 'json' )
@@ -91,38 +97,38 @@ def test_post_json_failing_jwt_auth(self):
91
97
response = self .csrf_client .post ('/jwt/' , {'example' : 'example' },
92
98
format = 'json' )
93
99
self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
94
- self .assertEqual (response ['WWW-Authenticate' ], 'Bearer realm="api"' )
100
+ self .assertEqual (response ['WWW-Authenticate' ], 'JWT realm="api"' )
95
101
96
- def test_post_no_bearer_failing_jwt_auth (self ):
102
+ def test_post_no_jwt_header_failing_jwt_auth (self ):
97
103
"""
98
104
Ensure POSTing over JWT auth without credentials fails
99
105
"""
100
- auth = 'Bearer '
106
+ auth = 'JWT '
101
107
response = self .csrf_client .post (
102
108
'/jwt/' , {'example' : 'example' },
103
109
HTTP_AUTHORIZATION = auth , format = 'json' )
104
110
105
- msg = 'Invalid bearer header. No credentials provided.'
111
+ msg = 'Invalid JWT header. No credentials provided.'
106
112
107
113
self .assertEqual (response .data ['detail' ], msg )
108
114
self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
109
- self .assertEqual (response ['WWW-Authenticate' ], 'Bearer realm="api"' )
115
+ self .assertEqual (response ['WWW-Authenticate' ], 'JWT realm="api"' )
110
116
111
- def test_post_invalid_bearer_failing_jwt_auth (self ):
117
+ def test_post_invalid_jwt_header_failing_jwt_auth (self ):
112
118
"""
113
119
Ensure POSTing over JWT auth without correct credentials fails
114
120
"""
115
- auth = 'Bearer abc abc'
121
+ auth = 'JWT abc abc'
116
122
response = self .csrf_client .post (
117
123
'/jwt/' , {'example' : 'example' },
118
124
HTTP_AUTHORIZATION = auth , format = 'json' )
119
125
120
- msg = ('Invalid bearer header. Credentials string '
126
+ msg = ('Invalid JWT header. Credentials string '
121
127
'should not contain spaces.' )
122
128
123
129
self .assertEqual (response .data ['detail' ], msg )
124
130
self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
125
- self .assertEqual (response ['WWW-Authenticate' ], 'Bearer realm="api"' )
131
+ self .assertEqual (response ['WWW-Authenticate' ], 'JWT realm="api"' )
126
132
127
133
def test_post_expired_token_failing_jwt_auth (self ):
128
134
"""
@@ -132,7 +138,7 @@ def test_post_expired_token_failing_jwt_auth(self):
132
138
payload ['exp' ] = 1
133
139
token = utils .jwt_encode_handler (payload )
134
140
135
- auth = 'Bearer {0}' .format (token )
141
+ auth = 'JWT {0}' .format (token )
136
142
response = self .csrf_client .post (
137
143
'/jwt/' , {'example' : 'example' },
138
144
HTTP_AUTHORIZATION = auth , format = 'json' )
@@ -141,13 +147,13 @@ def test_post_expired_token_failing_jwt_auth(self):
141
147
142
148
self .assertEqual (response .data ['detail' ], msg )
143
149
self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
144
- self .assertEqual (response ['WWW-Authenticate' ], 'Bearer realm="api"' )
150
+ self .assertEqual (response ['WWW-Authenticate' ], 'JWT realm="api"' )
145
151
146
152
def test_post_invalid_token_failing_jwt_auth (self ):
147
153
"""
148
154
Ensure POSTing over JWT auth with invalid token fails
149
155
"""
150
- auth = 'Bearer abc123'
156
+ auth = 'JWT abc123'
151
157
response = self .csrf_client .post (
152
158
'/jwt/' , {'example' : 'example' },
153
159
HTTP_AUTHORIZATION = auth , format = 'json' )
@@ -156,9 +162,9 @@ def test_post_invalid_token_failing_jwt_auth(self):
156
162
157
163
self .assertEqual (response .data ['detail' ], msg )
158
164
self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
159
- self .assertEqual (response ['WWW-Authenticate' ], 'Bearer realm="api"' )
165
+ self .assertEqual (response ['WWW-Authenticate' ], 'JWT realm="api"' )
160
166
161
- @unittest .skipUnless (oauth2_provider , 'django-oauth2-provider not installed' )
167
+ @unittest .skipUnless (oauth2_provider , DJANGO_OAUTH2_PROVIDER_NOT_INSTALLED )
162
168
def test_post_passing_jwt_auth_with_oauth2_priority (self ):
163
169
"""
164
170
Ensure POSTing over JWT auth with correct credentials
@@ -168,14 +174,14 @@ def test_post_passing_jwt_auth_with_oauth2_priority(self):
168
174
payload = utils .jwt_payload_handler (self .user )
169
175
token = utils .jwt_encode_handler (payload )
170
176
171
- auth = 'Bearer {0}' .format (token )
177
+ auth = 'JWT {0}' .format (token )
172
178
response = self .csrf_client .post (
173
179
'/oauth2-jwt/' , {'example' : 'example' },
174
180
HTTP_AUTHORIZATION = auth , format = 'json' )
175
181
176
182
self .assertEqual (response .status_code , status .HTTP_200_OK , response )
177
183
178
- @unittest .skipUnless (oauth2_provider , 'django-oauth2-provider not installed' )
184
+ @unittest .skipUnless (oauth2_provider , DJANGO_OAUTH2_PROVIDER_NOT_INSTALLED )
179
185
def test_post_passing_oauth2_with_jwt_auth_priority (self ):
180
186
"""
181
187
Ensure POSTing over OAuth2 with correct credentials
0 commit comments