| 1 | from io import BytesIO |
| 2 | |
| 3 | from flask import json |
| 4 | from flask import request |
| 5 | from wtforms import FileField |
| 6 | from wtforms import HiddenField |
| 7 | from wtforms import IntegerField |
| 8 | from wtforms import StringField |
| 9 | from wtforms.validators import DataRequired |
| 10 | from wtforms.widgets import HiddenInput |
| 11 | |
| 12 | from flask_wtf import FlaskForm |
| 13 | |
| 14 | |
| 15 | class BasicForm(FlaskForm): |
| 16 | class Meta: |
| 17 | csrf = False |
| 18 | |
| 19 | name = StringField(validators=[DataRequired()]) |
| 20 | avatar = FileField() |
| 21 | |
| 22 | |
| 23 | def test_populate_from_form(app, client): |
| 24 | @app.route("/", methods=["POST"]) |
| 25 | def index(): |
| 26 | form = BasicForm() |
| 27 | assert form.name.data == "form" |
| 28 | |
| 29 | client.post("/", data={"name": "form"}) |
| 30 | |
| 31 | |
| 32 | def test_populate_from_files(app, client): |
| 33 | @app.route("/", methods=["POST"]) |
| 34 | def index(): |
| 35 | form = BasicForm() |
| 36 | assert form.avatar.data is not None |
| 37 | assert form.avatar.data.filename == "flask.png" |
| 38 | |
| 39 | client.post("/", data={"name": "files", "avatar": (BytesIO(), "flask.png")}) |
| 40 | |
| 41 | |
| 42 | def test_populate_from_json(app, client): |
| 43 | @app.route("/", methods=["POST"]) |
| 44 | def index(): |
| 45 | form = BasicForm() |
| 46 | assert form.name.data == "json" |
| 47 | |
| 48 | client.post("/", data=json.dumps({"name": "json"}), content_type="application/json") |
| 49 | |
| 50 | |
| 51 | def test_populate_manually(app, client): |
| 52 | @app.route("/", methods=["POST"]) |
| 53 | def index(): |
| 54 | form = BasicForm(request.args) |
| 55 | assert form.name.data == "args" |
| 56 | |
| 57 | client.post("/", query_string={"name": "args"}) |
| 58 | |
| 59 | |
| 60 | def test_populate_none(app, client): |
| 61 | @app.route("/", methods=["POST"]) |
| 62 | def index(): |
| 63 | form = BasicForm(None) |
| 64 | assert form.name.data is None |
| 65 | |
| 66 | client.post("/", data={"name": "ignore"}) |
| 67 | |
| 68 | |
| 69 | def test_validate_on_submit(app, client): |
| 70 | @app.route("/", methods=["POST"]) |
| 71 | def index(): |
| 72 | form = BasicForm() |
| 73 | assert form.is_submitted() |
| 74 | assert not form.validate_on_submit() |
| 75 | assert "name" in form.errors |
| 76 | |
| 77 | client.post("/") |
| 78 | |
| 79 | |
| 80 | def test_no_validate_on_get(app, client): |
| 81 | @app.route("/", methods=["GET", "POST"]) |
| 82 | def index(): |
| 83 | form = BasicForm() |
| 84 | assert not form.validate_on_submit() |
| 85 | assert "name" not in form.errors |
| 86 | |
| 87 | client.get("/") |
| 88 | |
| 89 | |
| 90 | def test_hidden_tag(req_ctx): |
| 91 | class F(BasicForm): |
| 92 | class Meta: |
| 93 | csrf = True |
| 94 | |
| 95 | key = HiddenField() |
| 96 | count = IntegerField(widget=HiddenInput()) |
| 97 | |
| 98 | f = F() |
| 99 | out = f.hidden_tag() |
| 100 | assert all(x in out for x in ("csrf_token", "count", "key")) |
| 101 | assert "avatar" not in out |
| 102 | assert "csrf_token" not in f.hidden_tag("count", "key") |
| 103 | |
| 104 | |
| 105 | def test_set_default_message_language(app, client): |
| 106 | @app.route("/default", methods=["POST"]) |
| 107 | def default(): |
| 108 | form = BasicForm() |
| 109 | assert not form.validate_on_submit() |
| 110 | assert "This field is required." in form.name.errors |
| 111 | |
| 112 | @app.route("/es", methods=["POST"]) |
| 113 | def es(): |
| 114 | app.config["WTF_I18N_ENABLED"] = False |
| 115 | |
| 116 | class MyBaseForm(FlaskForm): |
| 117 | class Meta: |
| 118 | csrf = False |
| 119 | locales = ["es"] |
| 120 | |
| 121 | class NameForm(MyBaseForm): |
| 122 | name = StringField(validators=[DataRequired()]) |
| 123 | |
| 124 | form = NameForm() |
| 125 | assert form.meta.locales == ["es"] |
| 126 | assert not form.validate_on_submit() |
| 127 | assert "Este campo es obligatorio." in form.name.errors |
| 128 | |
| 129 | client.post("/default", data={"name": " "}) |
| 130 | client.post("/es", data={"name": " "}) |