From 2eb69983b61691af245500928f6bf393f1700f9d Mon Sep 17 00:00:00 2001 From: Landon GB Date: Fri, 27 Apr 2018 16:44:13 -0600 Subject: [PATCH] Use new `response.get_json()` in flask 1.0 for tests --- tests/test_asymmetric_crypto.py | 8 ++- tests/test_blacklist.py | 23 +++------ tests/test_claims_verification.py | 14 ++---- tests/test_cookies.py | 41 ++++++--------- tests/test_headers.py | 26 ++++------ tests/test_headers_and_cookies.py | 11 ++-- tests/test_options_method.py | 6 +-- tests/test_user_claims_loader.py | 11 ++-- tests/test_user_loader.py | 11 ++-- tests/test_view_decorators.py | 83 +++++++++++-------------------- 10 files changed, 81 insertions(+), 153 deletions(-) diff --git a/tests/test_asymmetric_crypto.py b/tests/test_asymmetric_crypto.py index 73850cd7..d0b67f54 100644 --- a/tests/test_asymmetric_crypto.py +++ b/tests/test_asymmetric_crypto.py @@ -1,5 +1,5 @@ import pytest -from flask import Flask, jsonify, json +from flask import Flask, jsonify from flask_jwt_extended import JWTManager, jwt_required, create_access_token @@ -57,13 +57,11 @@ def test_asymmetric_cropto(app): # Insure the symmetric token does not work now access_headers = {'Authorization': 'Bearer {}'.format(hs256_token)} response = test_client.get('/protected', headers=access_headers) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 422 - assert json_data == {'msg': 'The specified alg value is not allowed'} + assert response.get_json() == {'msg': 'The specified alg value is not allowed'} # Insure the asymmetric token does work access_headers = {'Authorization': 'Bearer {}'.format(rs256_token)} response = test_client.get('/protected', headers=access_headers) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} diff --git a/tests/test_blacklist.py b/tests/test_blacklist.py index 817f5062..2c1412f4 100644 --- a/tests/test_blacklist.py +++ b/tests/test_blacklist.py @@ -1,5 +1,5 @@ import pytest -from flask import Flask, jsonify, json +from flask import Flask, jsonify from flask_jwt_extended import ( JWTManager, jwt_required, create_access_token, @@ -43,8 +43,7 @@ def check_blacklisted(decrypted_token): test_client = app.test_client() response = test_client.get('/protected', headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} assert response.status_code == 200 @@ -62,8 +61,7 @@ def check_blacklisted(decrypted_token): test_client = app.test_client() response = test_client.get('/protected', headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'msg': 'Token has been revoked'} + assert response.get_json() == {'msg': 'Token has been revoked'} assert response.status_code == 401 @@ -81,8 +79,7 @@ def check_blacklisted(decrypted_token): test_client = app.test_client() response = test_client.get('/refresh_protected', headers=make_headers(refresh_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} assert response.status_code == 200 @@ -100,8 +97,7 @@ def check_blacklisted(decrypted_token): test_client = app.test_client() response = test_client.get('/refresh_protected', headers=make_headers(refresh_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'msg': 'Token has been revoked'} + assert response.get_json() == {'msg': 'Token has been revoked'} assert response.status_code == 401 @@ -130,14 +126,12 @@ def check_blacklisted(decrypted_token): app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access'] response = test_client.get('/refresh_protected', headers=make_headers(refresh_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} assert response.status_code == 200 app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['refresh'] response = test_client.get('/protected', headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} assert response.status_code == 200 @@ -157,6 +151,5 @@ def custom_error(): test_client = app.test_client() response = test_client.get('/protected', headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'baz': 'foo'} + assert response.get_json() == {'baz': 'foo'} assert response.status_code == 404 diff --git a/tests/test_claims_verification.py b/tests/test_claims_verification.py index dad565f2..1893a7a8 100644 --- a/tests/test_claims_verification.py +++ b/tests/test_claims_verification.py @@ -1,5 +1,5 @@ import pytest -from flask import Flask, jsonify, json +from flask import Flask, jsonify from flask_jwt_extended import ( JWTManager, jwt_required, create_access_token, get_jwt_identity, @@ -49,8 +49,7 @@ def user_load_callback(user_claims): access_token = create_access_token('username', fresh=True) response = test_client.get(url, headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} assert response.status_code == 200 @@ -67,8 +66,7 @@ def user_load_callback(user_claims): access_token = create_access_token('username', fresh=True) response = test_client.get(url, headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'msg': 'User claims verification failed'} + assert response.get_json() == {'msg': 'User claims verification failed'} assert response.status_code == 400 @@ -91,8 +89,7 @@ def custom_error(): access_token = create_access_token('username', fresh=True) response = test_client.get(url, headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'msg': 'claims failed for username'} + assert response.get_json() == {'msg': 'claims failed for username'} assert response.status_code == 404 @@ -111,6 +108,5 @@ def user_load_callback(user_claims): access_token = create_access_token('username', fresh=True) response = test_client.get(url, headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} assert response.status_code == 200 diff --git a/tests/test_cookies.py b/tests/test_cookies.py index 1fddf2f9..72da4572 100644 --- a/tests/test_cookies.py +++ b/tests/test_cookies.py @@ -1,5 +1,5 @@ import pytest -from flask import Flask, jsonify, json +from flask import Flask, jsonify from flask_jwt_extended import ( jwt_required, JWTManager, jwt_refresh_token_required, create_access_token, @@ -84,23 +84,20 @@ def test_jwt_refresh_required_with_cookies(app, options): # Test without cookies response = test_client.get(protected_url) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Missing cookie "{}"'.format(cookie_name)} + assert response.get_json() == {'msg': 'Missing cookie "{}"'.format(cookie_name)} # Test after receiving cookies test_client.get(auth_url) response = test_client.get(protected_url) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} # Test after issuing a 'logout' to delete the cookies test_client.get('/delete_tokens') response = test_client.get(protected_url) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Missing cookie "{}"'.format(cookie_name)} + assert response.get_json() == {'msg': 'Missing cookie "{}"'.format(cookie_name)} @pytest.mark.parametrize("options", [ @@ -117,16 +114,14 @@ def test_default_access_csrf_protection(app, options): # Test you cannot post without the additional csrf protection response = test_client.post(post_url) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Missing CSRF token in headers'} + assert response.get_json() == {'msg': 'Missing CSRF token in headers'} # Test that you can post with the csrf double submit value csrf_headers = {'X-CSRF-TOKEN': csrf_token} response = test_client.post(post_url, headers=csrf_headers) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} @pytest.mark.parametrize("options", [ @@ -141,9 +136,8 @@ def test_non_matching_csrf_token(app, options): test_client.get(auth_url) csrf_headers = {'X-CSRF-TOKEN': 'totally_wrong_token'} response = test_client.post(post_url, headers=csrf_headers) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'CSRF double submit tokens do not match'} + assert response.get_json() == {'msg': 'CSRF double submit tokens do not match'} @pytest.mark.parametrize("options", [ @@ -158,9 +152,8 @@ def test_csrf_disabled(app, options): # Get the jwt cookies and csrf double submit tokens test_client.get(auth_url) response = test_client.post(post_url) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} @pytest.mark.parametrize("options", [ @@ -180,9 +173,8 @@ def test_csrf_with_custom_header_names(app, options): # Test that you can post with the csrf double submit value csrf_headers = {'FOO': csrf_token} response = test_client.post(post_url, headers=csrf_headers) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} @pytest.mark.parametrize("options", [ @@ -200,22 +192,19 @@ def test_custom_csrf_methods(app, options): # Insure we can now do posts without csrf response = test_client.post(post_url) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} # Insure GET requests now fail without csrf response = test_client.get(get_url) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Missing CSRF token in headers'} + assert response.get_json() == {'msg': 'Missing CSRF token in headers'} # Insure GET requests now succeed with csrf csrf_headers = {'X-CSRF-TOKEN': csrf_token} response = test_client.get(get_url, headers=csrf_headers) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} def test_setting_cookies_wihout_cookies_enabled(app): @@ -403,14 +392,12 @@ def test_jwt_optional_with_csrf_enabled(app): # User without a token should be able to reach the endpoint without # getting a CSRF error response = test_client.post('/optional_post_protected') - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} # User with a token should still get a CSRF error if csrf not present response = test_client.get('/access_token') csrf_token = _get_cookie_from_response(response, 'csrf_access_token')['csrf_access_token'] response = test_client.post('/optional_post_protected') - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Missing CSRF token in headers'} + assert response.get_json() == {'msg': 'Missing CSRF token in headers'} diff --git a/tests/test_headers.py b/tests/test_headers.py index 143f310b..c9f94094 100644 --- a/tests/test_headers.py +++ b/tests/test_headers.py @@ -1,5 +1,5 @@ import pytest -from flask import Flask, jsonify, json +from flask import Flask, jsonify from flask_jwt_extended import JWTManager, jwt_required, create_access_token from tests.utils import get_jwt_manager @@ -29,16 +29,14 @@ def test_custom_header_name(app): # Insure 'default' headers no longer work access_headers = {'Authorization': 'Bearer {}'.format(access_token)} response = test_client.get('/protected', headers=access_headers) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Missing Foo Header'} + assert response.get_json() == {'msg': 'Missing Foo Header'} # Insure new headers do work access_headers = {'Foo': 'Bearer {}'.format(access_token)} response = test_client.get('/protected', headers=access_headers) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} def test_custom_header_type(app): @@ -51,31 +49,27 @@ def test_custom_header_type(app): # Insure 'default' headers no longer work access_headers = {'Authorization': 'Bearer {}'.format(access_token)} response = test_client.get('/protected', headers=access_headers) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 422 - assert json_data == {'msg': "Bad Authorization header. Expected value 'JWT '"} + assert response.get_json() == {'msg': "Bad Authorization header. Expected value 'JWT '"} # Insure new headers do work access_headers = {'Authorization': 'JWT {}'.format(access_token)} response = test_client.get('/protected', headers=access_headers) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} # Insure new headers without a type also work app.config['JWT_HEADER_TYPE'] = '' access_headers = {'Authorization': access_token} response = test_client.get('/protected', headers=access_headers) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} # Insure header with too many parts fails app.config['JWT_HEADER_TYPE'] = '' access_headers = {'Authorization': 'Bearer {}'.format(access_token)} response = test_client.get('/protected', headers=access_headers) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'msg': "Bad Authorization header. Expected value ''"} + assert response.get_json() == {'msg': "Bad Authorization header. Expected value ''"} assert response.status_code == 422 @@ -85,9 +79,8 @@ def test_missing_headers(app): # Insure 'default' no headers response response = test_client.get('/protected', headers=None) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': "Missing Authorization Header"} + assert response.get_json() == {'msg': "Missing Authorization Header"} # Test custom no headers response @jwtM.unauthorized_loader @@ -95,6 +88,5 @@ def custom_response(err_str): return jsonify(foo='bar'), 201 response = test_client.get('/protected', headers=None) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 201 - assert json_data == {'foo': "bar"} + assert response.get_json() == {'foo': "bar"} diff --git a/tests/test_headers_and_cookies.py b/tests/test_headers_and_cookies.py index 9af43d32..1f55a898 100644 --- a/tests/test_headers_and_cookies.py +++ b/tests/test_headers_and_cookies.py @@ -1,5 +1,5 @@ import pytest -from flask import Flask, jsonify, json +from flask import Flask, jsonify from flask_jwt_extended import ( JWTManager, jwt_required, create_access_token, set_access_cookies @@ -35,23 +35,20 @@ def test_header_access(app): access_headers = {'Authorization': 'Bearer {}'.format(access_token)} response = test_client.get('/protected', headers=access_headers) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} def test_cookie_access(app): test_client = app.test_client() test_client.get('/cookie_login') response = test_client.get('/protected') - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} def test_no_jwt_in_request(app): test_client = app.test_client() response = test_client.get('/protected') - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Missing JWT in headers and cookies'} + assert response.get_json() == {'msg': 'Missing JWT in headers and cookies'} diff --git a/tests/test_options_method.py b/tests/test_options_method.py index 2527c464..f135a09c 100644 --- a/tests/test_options_method.py +++ b/tests/test_options_method.py @@ -1,7 +1,7 @@ -from flask import Flask, Blueprint +from flask import Flask from flask_jwt_extended import ( JWTManager, jwt_required, fresh_jwt_required, jwt_refresh_token_required - ) +) import pytest @pytest.fixture(scope='function') @@ -25,8 +25,6 @@ def fresh_jwt_required_endpoint(): def jwt_refresh_token_required_endpoint(): return b'ok' - - return app def test_access_jwt_required_enpoint(app): diff --git a/tests/test_user_claims_loader.py b/tests/test_user_claims_loader.py index 0e24d7bd..51f1900d 100644 --- a/tests/test_user_claims_loader.py +++ b/tests/test_user_claims_loader.py @@ -1,5 +1,5 @@ import pytest -from flask import Flask, json, jsonify +from flask import Flask, jsonify from flask_jwt_extended import ( JWTManager, create_access_token, jwt_required, get_jwt_claims, @@ -34,8 +34,7 @@ def add_claims(identity): test_client = app.test_client() response = test_client.get('/protected', headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} assert response.status_code == 200 @@ -76,8 +75,7 @@ def add_claims(test_obj): test_client = app.test_client() response = test_client.get('/protected', headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'username': 'username'} + assert response.get_json() == {'username': 'username'} assert response.status_code == 200 @@ -99,6 +97,5 @@ def add_claims(identity): # Make sure the correct data is returned to us from the full call test_client = app.test_client() response = test_client.get('/protected', headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} assert response.status_code == 200 diff --git a/tests/test_user_loader.py b/tests/test_user_loader.py index 4b3e5267..01631bbb 100644 --- a/tests/test_user_loader.py +++ b/tests/test_user_loader.py @@ -1,5 +1,5 @@ import pytest -from flask import Flask, jsonify, json +from flask import Flask, jsonify from flask_jwt_extended import ( JWTManager, jwt_required, current_user, get_current_user, @@ -40,9 +40,8 @@ def user_load_callback(identity): access_token = create_access_token('username') response = test_client.get(url, headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'username'} + assert response.get_json() == {'foo': 'username'} @pytest.mark.parametrize("url", ['/get_user1', '/get_user2']) @@ -58,9 +57,8 @@ def user_load_callback(identity): access_token = create_access_token('username') response = test_client.get(url, headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': "Error loading the user username"} + assert response.get_json() == {'msg': "Error loading the user username"} @pytest.mark.parametrize("url", ['/get_user1', '/get_user2']) @@ -80,6 +78,5 @@ def user_loader_error(identity): access_token = create_access_token('username') response = test_client.get(url, headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 201 - assert json_data == {'foo': "bar"} + assert response.get_json() == {'foo': "bar"} diff --git a/tests/test_view_decorators.py b/tests/test_view_decorators.py index 96e47edf..fe65810f 100644 --- a/tests/test_view_decorators.py +++ b/tests/test_view_decorators.py @@ -1,6 +1,6 @@ import pytest from datetime import timedelta -from flask import Flask, jsonify, json +from flask import Flask, jsonify from flask_jwt_extended import ( jwt_required, fresh_jwt_required, JWTManager, jwt_refresh_token_required, @@ -53,21 +53,18 @@ def test_jwt_required(app): # Access and fresh access should be able to access this for token in (access_token, fresh_access_token): response = test_client.get(url, headers=make_headers(token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} # Test accessing jwt_required with no jwt in the request response = test_client.get(url, headers=None) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Missing Authorization Header'} + assert response.get_json() == {'msg': 'Missing Authorization Header'} # Test refresh token access to jwt_required response = test_client.get(url, headers=make_headers(refresh_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 422 - assert json_data == {'msg': 'Only access tokens are allowed'} + assert response.get_json() == {'msg': 'Only access tokens are allowed'} def test_fresh_jwt_required(app): @@ -83,34 +80,28 @@ def test_fresh_jwt_required(app): refresh_token = create_refresh_token('username') response = test_client.get(url, headers=make_headers(fresh_access_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} response = test_client.get(url, headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Fresh token required'} + assert response.get_json() == {'msg': 'Fresh token required'} response = test_client.get(url, headers=make_headers(fresh_timed_access_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} response = test_client.get(url, headers=make_headers(stale_timed_access_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Fresh token required'} + assert response.get_json() == {'msg': 'Fresh token required'} response = test_client.get(url, headers=None) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Missing Authorization Header'} + assert response.get_json() == {'msg': 'Missing Authorization Header'} response = test_client.get(url, headers=make_headers(refresh_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 422 - assert json_data == {'msg': 'Only access tokens are allowed'} + assert response.get_json() == {'msg': 'Only access tokens are allowed'} # Test with custom response @jwtM.needs_fresh_token_loader @@ -118,9 +109,8 @@ def custom_response(): return jsonify(msg='foobar'), 201 response = test_client.get(url, headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 201 - assert json_data == {'msg': 'foobar'} + assert response.get_json() == {'msg': 'foobar'} def test_refresh_jwt_required(app): @@ -133,24 +123,20 @@ def test_refresh_jwt_required(app): refresh_token = create_refresh_token('username') response = test_client.get(url, headers=make_headers(fresh_access_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 422 - assert json_data == {'msg': 'Only refresh tokens are allowed'} + assert response.get_json() == {'msg': 'Only refresh tokens are allowed'} response = test_client.get(url, headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 422 - assert json_data == {'msg': 'Only refresh tokens are allowed'} + assert response.get_json() == {'msg': 'Only refresh tokens are allowed'} response = test_client.get(url, headers=None) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Missing Authorization Header'} + assert response.get_json() == {'msg': 'Missing Authorization Header'} response = test_client.get(url, headers=make_headers(refresh_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} def test_jwt_optional(app): @@ -164,29 +150,24 @@ def test_jwt_optional(app): refresh_token = create_refresh_token('username') response = test_client.get(url, headers=make_headers(fresh_access_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'baz'} + assert response.get_json() == {'foo': 'baz'} response = test_client.get(url, headers=make_headers(access_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'baz'} + assert response.get_json() == {'foo': 'baz'} response = test_client.get(url, headers=make_headers(refresh_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 422 - assert json_data == {'msg': 'Only access tokens are allowed'} + assert response.get_json() == {'msg': 'Only access tokens are allowed'} response = test_client.get(url, headers=None) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_data == {'foo': 'bar'} + assert response.get_json() == {'foo': 'bar'} response = test_client.get(url, headers=make_headers(expired_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Token has expired'} + assert response.get_json() == {'msg': 'Token has expired'} def test_invalid_jwt(app): @@ -197,9 +178,8 @@ def test_invalid_jwt(app): # Test default response response = test_client.get(url, headers=make_headers(invalid_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 422 - assert json_data == {'msg': 'Invalid header padding'} + assert response.get_json() == {'msg': 'Invalid header padding'} # Test custom response @jwtM.invalid_token_loader @@ -207,9 +187,8 @@ def custom_response(err_str): return jsonify(msg='foobar'), 201 response = test_client.get(url, headers=make_headers(invalid_token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 201 - assert json_data == {'msg': 'foobar'} + assert response.get_json() == {'msg': 'foobar'} def test_jwt_missing_claims(app): @@ -218,9 +197,8 @@ def test_jwt_missing_claims(app): token = encode_token(app, {'foo': 'bar'}) response = test_client.get(url, headers=make_headers(token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 422 - assert json_data == {'msg': 'Missing claim: jti'} + assert response.get_json() == {'msg': 'Missing claim: jti'} def test_expired_token(app): @@ -232,9 +210,8 @@ def test_expired_token(app): # Test default response response = test_client.get(url, headers=make_headers(token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Token has expired'} + assert response.get_json() == {'msg': 'Token has expired'} # Test custom response @jwtM.expired_token_loader @@ -242,9 +219,8 @@ def custom_response(): return jsonify(msg='foobar'), 201 response = test_client.get(url, headers=make_headers(token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 201 - assert json_data == {'msg': 'foobar'} + assert response.get_json() == {'msg': 'foobar'} def test_no_token(app): @@ -254,9 +230,8 @@ def test_no_token(app): # Test default response response = test_client.get(url, headers=None) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 - assert json_data == {'msg': 'Missing Authorization Header'} + assert response.get_json() == {'msg': 'Missing Authorization Header'} # Test custom response @jwtM.unauthorized_loader @@ -264,9 +239,8 @@ def custom_response(err_str): return jsonify(msg='foobar'), 201 response = test_client.get(url, headers=None) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 201 - assert json_data == {'msg': 'foobar'} + assert response.get_json() == {'msg': 'foobar'} def test_different_token_algorightm(app): @@ -278,6 +252,5 @@ def test_different_token_algorightm(app): app.config['JWT_ALGORITHM'] = 'HS512' response = test_client.get(url, headers=make_headers(token)) - json_data = json.loads(response.get_data(as_text=True)) assert response.status_code == 422 - assert json_data == {'msg': 'The specified alg value is not allowed'} + assert response.get_json() == {'msg': 'The specified alg value is not allowed'}