| 1 |
""" |
| 2 |
test_response |
| 3 |
~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Tests for validating default and plugable responses. |
| 6 |
|
| 7 |
:copyright: (c) 2019-2020 by J. Christopher Wagner (jwag). |
| 8 |
:license: MIT, see LICENSE for more details. |
| 9 |
""" |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from flask import jsonify |
| 14 |
|
| 15 |
from tests.test_utils import authenticate |
| 16 |
|
| 17 |
|
| 18 |
def test_render_json(app, client): |
| 19 |
@app.security.render_json |
| 20 |
def my_json(payload, code, headers=None, user=None): |
| 21 |
return jsonify(dict(myresponse=payload, code=code)) |
| 22 |
|
| 23 |
response = client.get( |
| 24 |
"/login", data={}, headers={"Content-Type": "application/json"} |
| 25 |
) |
| 26 |
assert "myresponse" in response.json |
| 27 |
assert response.json["code"] == 200 |
| 28 |
|
| 29 |
|
| 30 |
def _my_json(payload, code, headers=None, user=None): |
| 31 |
return jsonify(dict(myresponse=payload, code=code)) |
| 32 |
|
| 33 |
|
| 34 |
def test_render_json2(app, client): |
| 35 |
app.extensions["security"].render_json(_my_json) |
| 36 |
response = client.get( |
| 37 |
"/login", data={}, headers={"Content-Type": "application/json"} |
| 38 |
) |
| 39 |
assert "myresponse" in response.json |
| 40 |
assert response.json["code"] == 200 |
| 41 |
|
| 42 |
|
| 43 |
def test_render_json_logout(app, client): |
| 44 |
app.extensions["security"].render_json(_my_json) |
| 45 |
response = client.post("/logout", headers={"Content-Type": "application/json"}) |
| 46 |
assert "myresponse" in response.json |
| 47 |
assert response.json["code"] == 200 |
| 48 |
|
| 49 |
|
| 50 |
def test_default_unauthn(app, client): |
| 51 |
"""Test default unauthn handler with and without json""" |
| 52 |
|
| 53 |
response = client.get("/profile") |
| 54 |
assert response.status_code in [302, 303] |
| 55 |
assert response.location == "/login?next=/profile" |
| 56 |
|
| 57 |
response = client.get("/profile", headers={"Accept": "application/json"}) |
| 58 |
assert response.status_code == 401 |
| 59 |
assert response.json["meta"]["code"] == 401 |
| 60 |
# While "Basic" is acceptable, we never get a WWW-Authenticate header back since |
| 61 |
# most browsers intercept it. |
| 62 |
assert "WWW-Authenticate" not in response.headers |
| 63 |
|
| 64 |
|
| 65 |
@pytest.mark.settings(login_url="/mylogin", url_prefix="/myprefix") |
| 66 |
def test_default_unauthn_bp(app, client): |
| 67 |
"""Test default unauthn handler with blueprint prefix and login url""" |
| 68 |
|
| 69 |
response = client.get("/profile") |
| 70 |
assert response.status_code in [302, 303] |
| 71 |
assert response.location == "/myprefix/mylogin?next=/profile" |
| 72 |
|
| 73 |
|
| 74 |
def test_default_unauthn_myjson(app, client): |
| 75 |
"""Make sure render_json gets called for unauthn errors""" |
| 76 |
|
| 77 |
@app.security.render_json |
| 78 |
def my_json(payload, code, headers=None, user=None): |
| 79 |
return jsonify(dict(myresponse=payload, code=code)), code, headers |
| 80 |
|
| 81 |
response = client.get("/multi_auth", headers={"Accept": "application/json"}) |
| 82 |
assert response.status_code == 401 |
| 83 |
assert response.json["code"] == 401 |
| 84 |
assert "myresponse" in response.json |
| 85 |
|
| 86 |
|
| 87 |
def test_my_unauthn_handler(app, client): |
| 88 |
@app.security.unauthn_handler |
| 89 |
def my_unauthn(mechanisms, headers=None): |
| 90 |
return app.security._render_json({"mechanisms": mechanisms}, 401, headers, None) |
| 91 |
|
| 92 |
response = client.get("/multi_auth", headers={"Accept": "application/json"}) |
| 93 |
assert response.status_code == 401 |
| 94 |
assert all( |
| 95 |
m in response.json["response"]["mechanisms"] |
| 96 |
for m in ["session", "token", "basic"] |
| 97 |
) |
| 98 |
|
| 99 |
|
| 100 |
def test_default_unauthz(app, client): |
| 101 |
"""Test default unauthz handler with and without json""" |
| 102 |
authenticate(client, "[email protected]", "password") |
| 103 |
|
| 104 |
response = client.get("/admin") |
| 105 |
# This is the result of abort(403) since there is no UNAUTHORIZED_VIEW |
| 106 |
assert response.status_code == 403 |
| 107 |
|
| 108 |
response = client.get("/admin", headers={"Accept": "application/json"}) |
| 109 |
assert response.status_code == 403 |
| 110 |
assert response.json["meta"]["code"] == 403 |
| 111 |
|
| 112 |
|
| 113 |
def test_default_unauthz_myjson(app, client): |
| 114 |
"""Make sure render_json gets called for unauthn errors""" |
| 115 |
|
| 116 |
@app.security.render_json |
| 117 |
def my_json(payload, code, headers=None, user=None): |
| 118 |
return jsonify(dict(myresponse=payload, code=code)), code, headers |
| 119 |
|
| 120 |
authenticate(client, "[email protected]", "password") |
| 121 |
|
| 122 |
response = client.get("/admin", headers={"Accept": "application/json"}) |
| 123 |
assert response.status_code == 403 |
| 124 |
assert response.json["code"] == 403 |
| 125 |
|
| 126 |
|
| 127 |
def test_my_unauthz_handler(app, client): |
| 128 |
@app.security.unauthz_handler |
| 129 |
def my_unauthz(func_name, params): |
| 130 |
return ( |
| 131 |
jsonify(dict(myresponse={"func": func_name, "params": params}, code=403)), |
| 132 |
403, |
| 133 |
) |
| 134 |
|
| 135 |
authenticate(client, "[email protected]", "password") |
| 136 |
|
| 137 |
response = client.get("/admin", headers={"Accept": "application/json"}) |
| 138 |
assert response.status_code == 403 |
| 139 |
assert response.json["code"] == 403 |
| 140 |
assert response.json["myresponse"]["func"] == "roles_required" |
| 141 |
assert response.json["myresponse"]["params"] == ["admin"] |
| 142 |
|
| 143 |
|
| 144 |
def test_my_unauthz_handler_exc(app, client): |
| 145 |
"""Verify that can use exceptions in unauthz handler""" |
| 146 |
|
| 147 |
@app.security.unauthz_handler |
| 148 |
def my_unauthz(func, params): |
| 149 |
raise ValueError("Bad Value") |
| 150 |
|
| 151 |
@app.errorhandler(ValueError) |
| 152 |
def error_handler(ex): |
| 153 |
return jsonify(dict(code=403)), 403 |
| 154 |
|
| 155 |
authenticate(client, "[email protected]", "password") |
| 156 |
|
| 157 |
response = client.get("/admin", headers={"Accept": "application/json"}) |
| 158 |
assert response.status_code == 403 |