11import pytest
2- from flask import Flask , jsonify , json
2+ from flask import Flask , jsonify
33
44from flask_jwt_extended import (
55 jwt_required , JWTManager , jwt_refresh_token_required , create_access_token ,
@@ -84,23 +84,20 @@ def test_jwt_refresh_required_with_cookies(app, options):
8484
8585 # Test without cookies
8686 response = test_client .get (protected_url )
87- json_data = json .loads (response .get_data (as_text = True ))
8887 assert response .status_code == 401
89- assert json_data == {'msg' : 'Missing cookie "{}"' .format (cookie_name )}
88+ assert response . get_json () == {'msg' : 'Missing cookie "{}"' .format (cookie_name )}
9089
9190 # Test after receiving cookies
9291 test_client .get (auth_url )
9392 response = test_client .get (protected_url )
94- json_data = json .loads (response .get_data (as_text = True ))
9593 assert response .status_code == 200
96- assert json_data == {'foo' : 'bar' }
94+ assert response . get_json () == {'foo' : 'bar' }
9795
9896 # Test after issuing a 'logout' to delete the cookies
9997 test_client .get ('/delete_tokens' )
10098 response = test_client .get (protected_url )
101- json_data = json .loads (response .get_data (as_text = True ))
10299 assert response .status_code == 401
103- assert json_data == {'msg' : 'Missing cookie "{}"' .format (cookie_name )}
100+ assert response . get_json () == {'msg' : 'Missing cookie "{}"' .format (cookie_name )}
104101
105102
106103@pytest .mark .parametrize ("options" , [
@@ -117,16 +114,14 @@ def test_default_access_csrf_protection(app, options):
117114
118115 # Test you cannot post without the additional csrf protection
119116 response = test_client .post (post_url )
120- json_data = json .loads (response .get_data (as_text = True ))
121117 assert response .status_code == 401
122- assert json_data == {'msg' : 'Missing CSRF token in headers' }
118+ assert response . get_json () == {'msg' : 'Missing CSRF token in headers' }
123119
124120 # Test that you can post with the csrf double submit value
125121 csrf_headers = {'X-CSRF-TOKEN' : csrf_token }
126122 response = test_client .post (post_url , headers = csrf_headers )
127- json_data = json .loads (response .get_data (as_text = True ))
128123 assert response .status_code == 200
129- assert json_data == {'foo' : 'bar' }
124+ assert response . get_json () == {'foo' : 'bar' }
130125
131126
132127@pytest .mark .parametrize ("options" , [
@@ -141,9 +136,8 @@ def test_non_matching_csrf_token(app, options):
141136 test_client .get (auth_url )
142137 csrf_headers = {'X-CSRF-TOKEN' : 'totally_wrong_token' }
143138 response = test_client .post (post_url , headers = csrf_headers )
144- json_data = json .loads (response .get_data (as_text = True ))
145139 assert response .status_code == 401
146- assert json_data == {'msg' : 'CSRF double submit tokens do not match' }
140+ assert response . get_json () == {'msg' : 'CSRF double submit tokens do not match' }
147141
148142
149143@pytest .mark .parametrize ("options" , [
@@ -158,9 +152,8 @@ def test_csrf_disabled(app, options):
158152 # Get the jwt cookies and csrf double submit tokens
159153 test_client .get (auth_url )
160154 response = test_client .post (post_url )
161- json_data = json .loads (response .get_data (as_text = True ))
162155 assert response .status_code == 200
163- assert json_data == {'foo' : 'bar' }
156+ assert response . get_json () == {'foo' : 'bar' }
164157
165158
166159@pytest .mark .parametrize ("options" , [
@@ -180,9 +173,8 @@ def test_csrf_with_custom_header_names(app, options):
180173 # Test that you can post with the csrf double submit value
181174 csrf_headers = {'FOO' : csrf_token }
182175 response = test_client .post (post_url , headers = csrf_headers )
183- json_data = json .loads (response .get_data (as_text = True ))
184176 assert response .status_code == 200
185- assert json_data == {'foo' : 'bar' }
177+ assert response . get_json () == {'foo' : 'bar' }
186178
187179
188180@pytest .mark .parametrize ("options" , [
@@ -200,22 +192,19 @@ def test_custom_csrf_methods(app, options):
200192
201193 # Insure we can now do posts without csrf
202194 response = test_client .post (post_url )
203- json_data = json .loads (response .get_data (as_text = True ))
204195 assert response .status_code == 200
205- assert json_data == {'foo' : 'bar' }
196+ assert response . get_json () == {'foo' : 'bar' }
206197
207198 # Insure GET requests now fail without csrf
208199 response = test_client .get (get_url )
209- json_data = json .loads (response .get_data (as_text = True ))
210200 assert response .status_code == 401
211- assert json_data == {'msg' : 'Missing CSRF token in headers' }
201+ assert response . get_json () == {'msg' : 'Missing CSRF token in headers' }
212202
213203 # Insure GET requests now succeed with csrf
214204 csrf_headers = {'X-CSRF-TOKEN' : csrf_token }
215205 response = test_client .get (get_url , headers = csrf_headers )
216- json_data = json .loads (response .get_data (as_text = True ))
217206 assert response .status_code == 200
218- assert json_data == {'foo' : 'bar' }
207+ assert response . get_json () == {'foo' : 'bar' }
219208
220209
221210def test_setting_cookies_wihout_cookies_enabled (app ):
@@ -403,14 +392,12 @@ def test_jwt_optional_with_csrf_enabled(app):
403392 # User without a token should be able to reach the endpoint without
404393 # getting a CSRF error
405394 response = test_client .post ('/optional_post_protected' )
406- json_data = json .loads (response .get_data (as_text = True ))
407395 assert response .status_code == 200
408- assert json_data == {'foo' : 'bar' }
396+ assert response . get_json () == {'foo' : 'bar' }
409397
410398 # User with a token should still get a CSRF error if csrf not present
411399 response = test_client .get ('/access_token' )
412400 csrf_token = _get_cookie_from_response (response , 'csrf_access_token' )['csrf_access_token' ]
413401 response = test_client .post ('/optional_post_protected' )
414- json_data = json .loads (response .get_data (as_text = True ))
415402 assert response .status_code == 401
416- assert json_data == {'msg' : 'Missing CSRF token in headers' }
403+ assert response . get_json () == {'msg' : 'Missing CSRF token in headers' }
0 commit comments