-
Notifications
You must be signed in to change notification settings - Fork 310
/
test_form.py
165 lines (116 loc) · 4.02 KB
/
test_form.py
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
from io import BytesIO
from flask import json, request
from wtforms import FileField, HiddenField, IntegerField, StringField
from wtforms.compat import with_metaclass
from wtforms.form import FormMeta
from wtforms.validators import DataRequired
from wtforms.widgets import HiddenInput
from flask_wtf import FlaskForm, Form
from flask_wtf._compat import FlaskWTFDeprecationWarning
class BasicForm(FlaskForm):
class Meta:
csrf = False
name = StringField(validators=[DataRequired()])
avatar = FileField()
def test_populate_from_form(app, client):
@app.route('/', methods=['POST'])
def index():
form = BasicForm()
assert form.name.data == 'form'
client.post('/', data={'name': 'form'})
def test_populate_from_files(app, client):
@app.route('/', methods=['POST'])
def index():
form = BasicForm()
assert form.avatar.data is not None
assert form.avatar.data.filename == 'flask.png'
client.post('/', data={
'name': 'files', 'avatar': (BytesIO(), 'flask.png')
})
def test_populate_from_json(app, client):
@app.route('/', methods=['POST'])
def index():
form = BasicForm()
assert form.name.data == 'json'
client.post(
'/', data=json.dumps({'name': 'json'}),
content_type='application/json'
)
def test_populate_manually(app, client):
@app.route('/', methods=['POST'])
def index():
form = BasicForm(request.args)
assert form.name.data == 'args'
client.post('/', query_string={'name': 'args'})
def test_populate_none(app, client):
@app.route('/', methods=['POST'])
def index():
form = BasicForm(None)
assert form.name.data is None
client.post('/', data={'name': 'ignore'})
def test_validate_on_submit(app, client):
@app.route('/', methods=['POST'])
def index():
form = BasicForm()
assert form.is_submitted()
assert not form.validate_on_submit()
assert 'name' in form.errors
client.post('/')
def test_no_validate_on_get(app, client):
@app.route('/', methods=['GET', 'POST'])
def index():
form = BasicForm()
assert not form.validate_on_submit()
assert 'name' not in form.errors
client.get('/')
def test_hidden_tag(req_ctx):
class F(BasicForm):
class Meta:
csrf = True
key = HiddenField()
count = IntegerField(widget=HiddenInput())
f = F()
out = f.hidden_tag()
assert all(x in out for x in ('csrf_token', 'count', 'key'))
assert 'avatar' not in out
assert 'csrf_token' not in f.hidden_tag('count', 'key')
def test_deprecated_form(req_ctx, recwarn):
class F(Form):
pass
F()
w = recwarn.pop(FlaskWTFDeprecationWarning)
assert 'FlaskForm' in str(w.message)
def test_custom_meta_with_deprecated_form(req_ctx, recwarn):
class FMeta(FormMeta):
pass
class F(with_metaclass(FMeta, Form)):
pass
F()
recwarn.pop(FlaskWTFDeprecationWarning)
def test_deprecated_csrf_enabled(req_ctx, recwarn):
class F(FlaskForm):
pass
F(csrf_enabled=False)
w = recwarn.pop(FlaskWTFDeprecationWarning)
assert "meta={'csrf': False}" in str(w.message)
def test_set_default_message_language(app, client):
@app.route('/default', methods=['POST'])
def default():
form = BasicForm()
assert not form.validate_on_submit()
assert 'This field is required.' in form.name.errors
client.post('/default', data={'name': ' '})
@app.route('/es', methods=['POST'])
def es():
app.config['WTF_I18N_ENABLED'] = False
class MyBaseForm(FlaskForm):
class Meta:
csrf = False
locales = ['es']
class NameForm(MyBaseForm):
name = StringField(validators=[DataRequired()])
form = NameForm()
assert form.meta.locales == ['es']
assert not form.validate_on_submit()
assert 'Este campo es obligatorio.' in form.name.errors
client.post('/es', data={'name': ' '})