Ejemplo n.º 1
0
def test_file_size_small_file_passes_validation(form, tmpdir):
    form.file.kwargs['validators'] = [FileSize(max_size=100)]
    test_file_smaller_than_max = tmpdir.join("test_file_smaller_than_max.txt")
    test_file_smaller_than_max.write(b"\0")

    f = form(file=FileStorage(open(test_file_smaller_than_max.strpath, 'rb')))

    assert f.validate()
Ejemplo n.º 2
0
def test_file_size_small_file_passes_validation(form, tmp_path):
    form.file.kwargs["validators"] = [FileSize(max_size=100)]
    path = tmp_path / "test_file_smaller_than_max.txt"
    path.write_bytes(b"\0")

    with path.open("rb") as file:
        f = form(file=FileStorage(file))
        assert f.validate()
Ejemplo n.º 3
0
def test_file_size_invalid_file_size_fails_validation(form, min_size, max_size, invalid_file_size, tmpdir):
    form.file.kwargs['validators'] = [FileSize(min_size=min_size, max_size=max_size)]
    test_file_invalid_size = tmpdir.join("test_file_invalid_size.txt")
    test_file_invalid_size.write(b"\0" * invalid_file_size)

    f = form(file=FileStorage(open(test_file_invalid_size.strpath, 'rb')))

    assert not f.validate()
    assert f.file.errors[0] == 'File must be between {min_size} and {max_size} bytes.'.format(
        min_size=min_size,
        max_size=max_size
    )
Ejemplo n.º 4
0
class EditPost(FlaskForm):
    title = StringField('Заголовок', validators=[DataRequired()])
    description = StringField('Описание', validators=[DataRequired()])
    body = TextAreaField('Текст', validators=[DataRequired()])
    photo = FileField('image',
                      validators=[
                          FileAllowed(['jpg', 'jpeg', 'png'],
                                      'Только фотографии'),
                          FileSize(max_size=12582912,
                                   message="Максимальный размер файла - 12 мб")
                      ],
                      id="image")
    submit = SubmitField('Отправить')
Ejemplo n.º 5
0
def test_file_size_invalid_file_size_fails_validation(form, min_size, max_size,
                                                      invalid_file_size,
                                                      tmp_path):
    form.file.kwargs["validators"] = [
        FileSize(min_size=min_size, max_size=max_size)
    ]
    path = tmp_path / "test_file_invalid_size.txt"
    path.write_bytes(b"\0" * invalid_file_size)

    with path.open("rb") as file:
        f = form(file=FileStorage(file))
        assert not f.validate()
        assert f.file.errors[
            0] == "File must be between {min_size} and {max_size} bytes.".format(
                min_size=min_size, max_size=max_size)
Ejemplo n.º 6
0
def test_file_size_no_file_passes_validation(form):
    form.file.kwargs['validators'] = [FileSize(max_size=100)]
    f = form()
    assert f.validate()