def test_file_upload(app):
app.config['WTF_CSRF_ENABLED'] = False
class TestForm(SanicForm):
upload = FileField('upload file')
submit = SubmitField('Upload')
@app.route('/upload', methods=['GET', 'POST'])
async def upload(request):
form = TestForm(request)
if form.validate_on_submit():
return response.text(form.upload.data.name)
content = render_form(form)
return response.html(content)
req, resp = app.test_client.post(
'/upload', data={'upload': open(__file__, 'rb')})
assert resp.status == 200
assert resp.text == os.path.basename(__file__)