| 1 |
import pytest |
| 2 |
from flask import g |
| 3 |
from flask import session |
| 4 |
from wtforms import ValidationError |
| 5 |
|
| 6 |
from flask_wtf import FlaskForm |
| 7 |
from flask_wtf.csrf import generate_csrf |
| 8 |
from flask_wtf.csrf import validate_csrf |
| 9 |
|
| 10 |
|
| 11 |
def test_csrf_requires_secret_key(app, req_ctx): |
| 12 |
|
| 13 |
generate_csrf() |
| 14 |
|
| 15 |
app.secret_key = None |
| 16 |
pytest.raises(RuntimeError, generate_csrf) |
| 17 |
|
| 18 |
app.config["WTF_CSRF_SECRET_KEY"] = "wtf_secret" |
| 19 |
generate_csrf() |
| 20 |
del app.config["WTF_CSRF_SECRET_KEY"] |
| 21 |
|
| 22 |
generate_csrf(secret_key="direct") |
| 23 |
|
| 24 |
|
| 25 |
def test_token_stored_by_generate(req_ctx): |
| 26 |
generate_csrf() |
| 27 |
assert "csrf_token" in session |
| 28 |
assert "csrf_token" in g |
| 29 |
|
| 30 |
|
| 31 |
def test_custom_token_key(req_ctx): |
| 32 |
generate_csrf(token_key="oauth_token") |
| 33 |
assert "oauth_token" in session |
| 34 |
assert "oauth_token" in g |
| 35 |
|
| 36 |
|
| 37 |
def test_token_cached(req_ctx): |
| 38 |
assert generate_csrf() == generate_csrf() |
| 39 |
|
| 40 |
|
| 41 |
def test_validate(req_ctx): |
| 42 |
validate_csrf(generate_csrf()) |
| 43 |
|
| 44 |
|
| 45 |
def test_validation_errors(req_ctx): |
| 46 |
e = pytest.raises(ValidationError, validate_csrf, None) |
| 47 |
assert str(e.value) == "The CSRF token is missing." |
| 48 |
|
| 49 |
e = pytest.raises(ValidationError, validate_csrf, "no session") |
| 50 |
assert str(e.value) == "The CSRF session token is missing." |
| 51 |
|
| 52 |
token = generate_csrf() |
| 53 |
e = pytest.raises(ValidationError, validate_csrf, token, time_limit=-1) |
| 54 |
assert str(e.value) == "The CSRF token has expired." |
| 55 |
|
| 56 |
e = pytest.raises(ValidationError, validate_csrf, "invalid") |
| 57 |
assert str(e.value) == "The CSRF token is invalid." |
| 58 |
|
| 59 |
other_token = generate_csrf(token_key="other_csrf") |
| 60 |
e = pytest.raises(ValidationError, validate_csrf, other_token) |
| 61 |
assert str(e.value) == "The CSRF tokens do not match." |
| 62 |
|
| 63 |
|
| 64 |
def test_form_csrf(app, client, app_ctx): |
| 65 |
@app.route("/", methods=["GET", "POST"]) |
| 66 |
def index(): |
| 67 |
f = FlaskForm() |
| 68 |
|
| 69 |
if f.validate_on_submit(): |
| 70 |
return "good" |
| 71 |
|
| 72 |
if f.errors: |
| 73 |
return f.csrf_token.errors[0] |
| 74 |
|
| 75 |
return f.csrf_token.current_token |
| 76 |
|
| 77 |
response = client.get("/") |
| 78 |
assert response.get_data(as_text=True) == g.csrf_token |
| 79 |
|
| 80 |
response = client.post("/") |
| 81 |
assert response.get_data(as_text=True) == "The CSRF token is missing." |
| 82 |
|
| 83 |
response = client.post("/", data={"csrf_token": g.csrf_token}) |
| 84 |
assert response.get_data(as_text=True) == "good" |
| 85 |
|
| 86 |
|
| 87 |
def test_validate_error_logged(req_ctx, monkeypatch): |
| 88 |
from flask_wtf.csrf import logger |
| 89 |
|
| 90 |
messages = [] |
| 91 |
|
| 92 |
def assert_info(message): |
| 93 |
messages.append(message) |
| 94 |
|
| 95 |
monkeypatch.setattr(logger, "info", assert_info) |
| 96 |
FlaskForm().validate() |
| 97 |
assert len(messages) == 1 |
| 98 |
assert messages[0] == "The CSRF token is missing." |