1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
"""
test_templates
~~~~~~~~~~
Test templates to be W3C valid
:copyright: (c) 2025-2025 by J. Christopher Wagner (jwag).
:license: MIT, see LICENSE for more details.
Note that the validator definitely rate-limits us - so we don't just run
them all.
To run a test pass templates="RESET,LOGIN" on the command line.
"""
from time import sleep
import pytest
import requests
from tests.test_utils import (
authenticate,
logout,
capture_reset_password_requests,
get_form_input_value,
reset_fresh,
setup_tf_sms,
)
from tests.test_webauthn import HackWebauthnUtil, reg_first_key
def check_message(msgs, mtype="error"):
# list of JSON messages from validator
errors = []
for msg in msgs:
if msg["type"] == mtype:
errors.append(msg["message"])
return errors
def check_template(name, client, r):
# returns list of validation errors
t = client.get(name)
if t.status_code != 200:
return [f"{name} error {t.status_code}"]
return check_template_rdata(name, r, t.data)
def check_template_rdata(name, r, rdata):
vout = r.post("https://validator.w3.org/nu/?out=json", rdata)
if vout.status_code == 429:
# we got rate limited try again
sleep(2.0)
vout = r.post("https://validator.w3.org/nu/?out=json", rdata)
if vout.status_code != 200:
return [f"{name} API error {vout.status_code}"]
if vout.status_code != 200:
return [f"{name} API error {vout.status_code}"]
return check_message(vout.json()["messages"])
@pytest.mark.registerable()
@pytest.mark.recoverable()
@pytest.mark.changeable()
@pytest.mark.change_email()
@pytest.mark.change_username()
@pytest.mark.oauth()
@pytest.mark.username_recovery()
@pytest.mark.unified_signin()
@pytest.mark.webauthn(webauthn_util_cls=HackWebauthnUtil)
@pytest.mark.two_factor()
@pytest.mark.settings(
multi_factor_recovery_codes=True,
oauth_enable=True,
)
@pytest.mark.csrf()
def test_valid_html(app, client):
# since we get rate limited - use external pytest option to specify
totry = app.config.get("TEMPLATES", "").split(",")
rsession = requests.session()
rsession.headers.update({"Content-Type": "text/html; charset=utf-8"})
unauth_urls = [
"LOGIN",
"REGISTER",
"RESET",
"US_SIGNIN",
"USERNAME_RECOVERY",
"WAN_SIGNIN",
]
auth_urls = [
"CHANGE",
"CHANGE_USERNAME",
"CHANGE_EMAIL",
"MULTI_FACTOR_RECOVERY_CODES",
"TWO_FACTOR_SETUP",
"US_SETUP",
"US_VERIFY",
"WAN_REGISTER",
"WAN_VERIFY",
]
# MULTI_FACTOR_RECOVERY requires tf-setup and login/password
# TWO_FACTOR_RESCUE has an issue with the RadioField
# TWO_FACTOR_SELECT needs special setup
authenticate(client, csrf=True)
response = client.get("/change")
csrf_token = get_form_input_value(response, "csrf_token")
reg_first_key(client, csrf_token=csrf_token)
logout(client)
terrors = dict()
for t in [u for u in unauth_urls if u in totry]:
terrors[t] = check_template(app.config[f"SECURITY_{t}_URL"], client, rsession)
authenticate(client, csrf=True)
for t in [u for u in auth_urls if u in totry]:
if t == "US_SETUP":
response = client.get("/us-setup")
csrf_token = get_form_input_value(response, "csrf_token")
response = client.post(
"us-setup",
data=dict(chosen_method="authenticator", csrf_token=csrf_token),
)
terrors[t] = check_template_rdata("US_SETUP", rsession, response.data)
continue
if t == "TWO_FACTOR_SETUP":
response = client.get("/tf-setup")
csrf_token = get_form_input_value(response, "csrf_token")
response = client.post(
"tf-setup",
data=dict(setup="authenticator", csrf_token=csrf_token),
)
terrors[t] = check_template_rdata(
"TWO_FACTOR_SETUP", rsession, response.data
)
continue
elif t == "US_VERIFY" or t == "VERIFY":
reset_fresh(client, app.config["SECURITY_FRESHNESS"])
terrors[t] = check_template(app.config[f"SECURITY_{t}_URL"], client, rsession)
print(f"Validated: {totry}")
errors = {k: v for k, v in terrors.items() if v}
assert not any(errors), errors
@pytest.mark.confirmable()
@pytest.mark.csrf()
def test_valid_html_confirm(app, client):
rsession = requests.session()
rsession.headers.update({"Content-Type": "text/html; charset=utf-8"})
# since we get rate limited - use external pytest option to specify
totry = app.config.get("TEMPLATES", "").split(",")
if "CONFIRM" in totry:
print(f"Validated: {totry}")
terrors = check_template(app.config["SECURITY_CONFIRM_URL"], client, rsession)
assert not terrors
@pytest.mark.recoverable()
@pytest.mark.csrf()
def test_valid_html_recover(app, client):
rsession = requests.session()
rsession.headers.update({"Content-Type": "text/html; charset=utf-8"})
# since we get rate limited - use external pytest option to specify
totry = app.config.get("TEMPLATES", "").split(",")
if "RESET" in totry:
print(f"Validated: {totry}")
with capture_reset_password_requests() as resets:
response = client.get("/reset")
csrf_token = get_form_input_value(response, "csrf_token")
client.post("/reset", data=dict(email="joe@lp.com", csrf_token=csrf_token))
token = resets[0]["token"]
terrors = check_template(
f'{app.config[f"SECURITY_RESET_URL"]}/{token}', client, rsession
)
assert not terrors
@pytest.mark.two_factor()
@pytest.mark.csrf()
def test_valid_html_rescue(app, client):
rsession = requests.session()
rsession.headers.update({"Content-Type": "text/html; charset=utf-8"})
# since we get rate limited - use external pytest option to specify
totry = app.config.get("TEMPLATES", "").split(",")
if "TWO_FACTOR_RESCUE" in totry:
authenticate(client, csrf=True)
response = client.get("/tf-setup")
csrf_token = get_form_input_value(response, "csrf_token")
setup_tf_sms(client, csrf_token=csrf_token)
logout(client)
authenticate(client, csrf=True)
print(f"Validated: {totry}")
terrors = check_template(
app.config["SECURITY_TWO_FACTOR_RESCUE_URL"], client, rsession
)
assert not terrors
|