Skip to content

Commit

Permalink
Use new response.get_json() in flask 1.0 for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vimalloc committed Apr 27, 2018
1 parent fb57613 commit 2eb6998
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 153 deletions.
8 changes: 3 additions & 5 deletions tests/test_asymmetric_crypto.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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'}
23 changes: 8 additions & 15 deletions tests/test_blacklist.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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


Expand All @@ -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


Expand All @@ -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


Expand All @@ -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


Expand Down Expand Up @@ -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


Expand All @@ -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
14 changes: 5 additions & 9 deletions tests/test_claims_verification.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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


Expand All @@ -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


Expand All @@ -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


Expand All @@ -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
41 changes: 14 additions & 27 deletions tests/test_cookies.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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", [
Expand All @@ -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", [
Expand All @@ -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", [
Expand All @@ -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", [
Expand All @@ -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", [
Expand All @@ -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):
Expand Down Expand Up @@ -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'}
26 changes: 9 additions & 17 deletions tests/test_headers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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 <JWT>'"}
assert response.get_json() == {'msg': "Bad Authorization header. Expected value 'JWT <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 '<JWT>'"}
assert response.get_json() == {'msg': "Bad Authorization header. Expected value '<JWT>'"}
assert response.status_code == 422


Expand All @@ -85,16 +79,14 @@ 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
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"}
11 changes: 4 additions & 7 deletions tests/test_headers_and_cookies.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'}
Loading

0 comments on commit 2eb6998

Please sign in to comment.