test_form.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:sanic-wtf 作者: pyx 项目源码 文件源码
def test_no_request_disable_csrf(app):
    app.config['WTF_CSRF_ENABLED'] = True
    app.config['WTF_CSRF_SECRET_KEY'] = 'look ma'

    class TestForm(SanicForm):
        msg = StringField('Note', validators=[DataRequired(), Length(max=10)])
        submit = SubmitField('Submit')

    @app.route('/', methods=['GET', 'POST'])
    async def index(request):
        form = TestForm(formdata=request.form)
        if request.method == 'POST' and form.validate():
            return response.text('validated')
        content = render_form(form)
        return response.html(content)

    payload = {'msg': 'happy'}
    req, resp = app.test_client.post('/', data=payload)
    assert resp.status == 200
    # should be okay, no request means CSRF was disabled
    assert 'validated' in resp.text
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号