| 1 | from io import BytesIO |
| 2 | |
| 3 | import pytest |
| 4 | from werkzeug.datastructures import FileStorage |
| 5 | from werkzeug.datastructures import ImmutableMultiDict |
| 6 | from werkzeug.datastructures import MultiDict |
| 7 | from wtforms import FileField as BaseFileField |
| 8 | from wtforms import MultipleFileField as BaseMultipleFileField |
| 9 | from wtforms.validators import Length |
| 10 | |
| 11 | from flask_wtf import FlaskForm |
| 12 | from flask_wtf.file import FileAllowed |
| 13 | from flask_wtf.file import FileField |
| 14 | from flask_wtf.file import FileRequired |
| 15 | from flask_wtf.file import FileSize |
| 16 | from flask_wtf.file import MultipleFileField |
| 17 | |
| 18 | |
| 19 | @pytest.fixture |
| 20 | def form(req_ctx): |
| 21 | class UploadForm(FlaskForm): |
| 22 | class Meta: |
| 23 | csrf = False |
| 24 | |
| 25 | file = FileField() |
| 26 | files = MultipleFileField() |
| 27 | |
| 28 | return UploadForm |
| 29 | |
| 30 | |
| 31 | def test_process_formdata(form): |
| 32 | assert form(MultiDict((("file", FileStorage()),))).file.data is None |
| 33 | assert ( |
| 34 | form(MultiDict((("file", FileStorage(filename="real")),))).file.data is not None |
| 35 | ) |
| 36 | |
| 37 | |
| 38 | def test_file_required(form): |
| 39 | form.file.kwargs["validators"] = [FileRequired()] |
| 40 | |
| 41 | f = form() |
| 42 | assert not f.validate() |
| 43 | assert f.file.errors[0] == "This field is required." |
| 44 | |
| 45 | f = form(file="not a file") |
| 46 | assert not f.validate() |
| 47 | assert f.file.errors[0] == "This field is required." |
| 48 | |
| 49 | f = form(file=FileStorage()) |
| 50 | assert not f.validate() |
| 51 | |
| 52 | f = form(file=FileStorage(filename="real")) |
| 53 | assert f.validate() |
| 54 | |
| 55 | |
| 56 | def test_file_allowed(form): |
| 57 | form.file.kwargs["validators"] = [FileAllowed(("txt",))] |
| 58 | |
| 59 | f = form() |
| 60 | assert f.validate() |
| 61 | |
| 62 | f = form(file=FileStorage(filename="test.txt")) |
| 63 | assert f.validate() |
| 64 | |
| 65 | f = form(file=FileStorage(filename="test.png")) |
| 66 | assert not f.validate() |
| 67 | assert f.file.errors[0] == "File does not have an approved extension: txt" |
| 68 | |
| 69 | |
| 70 | def test_file_allowed_uploadset(app, form): |
| 71 | pytest.importorskip("flask_uploads") |
| 72 | from flask_uploads import configure_uploads |
| 73 | from flask_uploads import UploadSet |
| 74 | |
| 75 | app.config["UPLOADS_DEFAULT_DEST"] = "uploads" |
| 76 | txt = UploadSet("txt", extensions=("txt",)) |
| 77 | configure_uploads(app, (txt,)) |
| 78 | form.file.kwargs["validators"] = [FileAllowed(txt)] |
| 79 | |
| 80 | f = form() |
| 81 | assert f.validate() |
| 82 | |
| 83 | f = form(file=FileStorage(filename="test.txt")) |
| 84 | assert f.validate() |
| 85 | |
| 86 | f = form(file=FileStorage(filename="test.png")) |
| 87 | assert not f.validate() |
| 88 | assert f.file.errors[0] == "File does not have an approved extension." |
| 89 | |
| 90 | |
| 91 | def test_file_size_no_file_passes_validation(form): |
| 92 | form.file.kwargs["validators"] = [FileSize(max_size=100)] |
| 93 | f = form() |
| 94 | assert f.validate() |
| 95 | |
| 96 | |
| 97 | def test_file_size_small_file_passes_validation(form, tmp_path): |
| 98 | form.file.kwargs["validators"] = [FileSize(max_size=100)] |
| 99 | path = tmp_path / "test_file_smaller_than_max.txt" |
| 100 | path.write_bytes(b"\0") |
| 101 | |
| 102 | with path.open("rb") as file: |
| 103 | f = form(file=FileStorage(file)) |
| 104 | assert f.validate() |
| 105 | |
| 106 | |
| 107 | def test_file_size_bytesio_passes_validation(form): |
| 108 | form.file.kwargs["validators"] = [FileSize(max_size=100)] |
| 109 | contents = BytesIO(b"small") |
| 110 | f = form(file=FileStorage(contents, filename="bytes")) |
| 111 | assert f.validate() |
| 112 | |
| 113 | |
| 114 | def test_file_size_bytesio_too_big_fails_validation(form): |
| 115 | form.file.kwargs["validators"] = [FileSize(max_size=100)] |
| 116 | contents = BytesIO(b"small" * 30) |
| 117 | f = form(file=FileStorage(contents, filename="bytes")) |
| 118 | assert not f.validate() |
| 119 | assert f.file.errors[0] == "File must be between 0 and 100 bytes." |
| 120 | |
| 121 | |
| 122 | def test_file_size_seekable_file_passes_validation(form, tmp_path): |
| 123 | form.file.kwargs["validators"] = [FileSize(max_size=100)] |
| 124 | path = tmp_path / "test_seekable.txt" |
| 125 | path.write_bytes(b"test content") |
| 126 | |
| 127 | with path.open("rb") as file: |
| 128 | file_storage = FileStorage(file, filename="test.txt") |
| 129 | f = form(file=file_storage) |
| 130 | assert f.validate() |
| 131 | assert file.tell() == 0 |
| 132 | |
| 133 | |
| 134 | def test_file_size_non_seekable_stream_raises_error(form): |
| 135 | class NonSeekableStream: |
| 136 | def __init__(self, data): |
| 137 | self._data = BytesIO(data) |
| 138 | |
| 139 | def read(self, size=-1): |
| 140 | return self._data.read(size) |
| 141 | |
| 142 | def seekable(self): |
| 143 | return False |
| 144 | |
| 145 | form.file.kwargs["validators"] = [FileSize(max_size=100)] |
| 146 | stream = NonSeekableStream(b"test") |
| 147 | f = form(file=FileStorage(stream, filename="test.txt")) |
| 148 | with pytest.raises(TypeError, match="not seekable"): |
| 149 | f.validate() |
| 150 | |
| 151 | |
| 152 | @pytest.mark.parametrize( |
| 153 | "min_size, max_size, invalid_file_size", [(1, 100, 0), (0, 100, 101)] |
| 154 | ) |
| 155 | def test_file_size_invalid_file_size_fails_validation( |
| 156 | form, min_size, max_size, invalid_file_size, tmp_path |
| 157 | ): |
| 158 | form.file.kwargs["validators"] = [FileSize(min_size=min_size, max_size=max_size)] |
| 159 | path = tmp_path / "test_file_invalid_size.txt" |
| 160 | path.write_bytes(b"\0" * invalid_file_size) |
| 161 | |
| 162 | with path.open("rb") as file: |
| 163 | f = form(file=FileStorage(file)) |
| 164 | assert not f.validate() |
| 165 | assert ( |
| 166 | f.file.errors[0] == f"File must be between {min_size} and {max_size} bytes." |
| 167 | ) |
| 168 | |
| 169 | |
| 170 | def test_validate_base_field(req_ctx): |
| 171 | class F(FlaskForm): |
| 172 | class Meta: |
| 173 | csrf = False |
| 174 | |
| 175 | f = BaseFileField(validators=[FileRequired()]) |
| 176 | |
| 177 | assert not F().validate() |
| 178 | assert not F(f=FileStorage()).validate() |
| 179 | assert F(f=FileStorage(filename="real")).validate() |
| 180 | assert F(f=FileStorage(filename="real")).validate() |
| 181 | |
| 182 | |
| 183 | def test_process_formdata_for_files(form): |
| 184 | assert ( |
| 185 | form( |
| 186 | ImmutableMultiDict([("files", FileStorage()), ("files", FileStorage())]) |
| 187 | ).files.data |
| 188 | is None |
| 189 | ) |
| 190 | assert ( |
| 191 | form( |
| 192 | ImmutableMultiDict( |
| 193 | [ |
| 194 | ("files", FileStorage(filename="a.jpg")), |
| 195 | ("files", FileStorage(filename="b.jpg")), |
| 196 | ] |
| 197 | ) |
| 198 | ).files.data |
| 199 | is not None |
| 200 | ) |
| 201 | |
| 202 | |
| 203 | def test_files_required(form): |
| 204 | form.files.kwargs["validators"] = [FileRequired()] |
| 205 | |
| 206 | f = form() |
| 207 | assert not f.validate() |
| 208 | assert f.files.errors[0] == "This field is required." |
| 209 | |
| 210 | f = form(files="not a file") |
| 211 | assert not f.validate() |
| 212 | assert f.files.errors[0] == "This field is required." |
| 213 | |
| 214 | f = form(files=[FileStorage()]) |
| 215 | assert not f.validate() |
| 216 | |
| 217 | f = form(files=[FileStorage(filename="real")]) |
| 218 | assert f.validate() |
| 219 | |
| 220 | |
| 221 | def test_files_allowed(form): |
| 222 | form.files.kwargs["validators"] = [FileAllowed(("txt",))] |
| 223 | |
| 224 | f = form() |
| 225 | assert f.validate() |
| 226 | |
| 227 | f = form( |
| 228 | files=[FileStorage(filename="test.txt"), FileStorage(filename="test2.txt")] |
| 229 | ) |
| 230 | assert f.validate() |
| 231 | |
| 232 | f = form(files=[FileStorage(filename="test.txt"), FileStorage(filename="test.png")]) |
| 233 | assert not f.validate() |
| 234 | assert f.files.errors[0] == "File does not have an approved extension: txt" |
| 235 | |
| 236 | |
| 237 | def test_files_allowed_uploadset(app, form): |
| 238 | pytest.importorskip("flask_uploads") |
| 239 | from flask_uploads import configure_uploads |
| 240 | from flask_uploads import UploadSet |
| 241 | |
| 242 | app.config["UPLOADS_DEFAULT_DEST"] = "uploads" |
| 243 | txt = UploadSet("txt", extensions=("txt",)) |
| 244 | configure_uploads(app, (txt,)) |
| 245 | form.files.kwargs["validators"] = [FileAllowed(txt)] |
| 246 | |
| 247 | f = form() |
| 248 | assert f.validate() |
| 249 | |
| 250 | f = form( |
| 251 | files=[FileStorage(filename="test.txt"), FileStorage(filename="test2.txt")] |
| 252 | ) |
| 253 | assert f.validate() |
| 254 | |
| 255 | f = form(files=[FileStorage(filename="test.txt"), FileStorage(filename="test.png")]) |
| 256 | assert not f.validate() |
| 257 | assert f.files.errors[0] == "File does not have an approved extension." |
| 258 | |
| 259 | |
| 260 | def test_validate_base_multiple_field(req_ctx): |
| 261 | class F(FlaskForm): |
| 262 | class Meta: |
| 263 | csrf = False |
| 264 | |
| 265 | f = BaseMultipleFileField(validators=[FileRequired()]) |
| 266 | |
| 267 | assert not F().validate() |
| 268 | assert not F(f=[FileStorage()]).validate() |
| 269 | assert F(f=[FileStorage(filename="real")]).validate() |
| 270 | |
| 271 | |
| 272 | def test_file_size_small_files_pass_validation(form, tmp_path): |
| 273 | form.files.kwargs["validators"] = [FileSize(max_size=100)] |
| 274 | path = tmp_path / "test_file_smaller_than_max.txt" |
| 275 | path.write_bytes(b"\0") |
| 276 | |
| 277 | with path.open("rb") as file: |
| 278 | f = form(files=[FileStorage(file)]) |
| 279 | assert f.validate() |
| 280 | |
| 281 | |
| 282 | @pytest.mark.parametrize( |
| 283 | "min_size, max_size, invalid_file_size", [(1, 100, 0), (0, 100, 101)] |
| 284 | ) |
| 285 | def test_file_size_invalid_file_sizes_fails_validation( |
| 286 | form, min_size, max_size, invalid_file_size, tmp_path |
| 287 | ): |
| 288 | form.files.kwargs["validators"] = [FileSize(min_size=min_size, max_size=max_size)] |
| 289 | path = tmp_path / "test_file_invalid_size.txt" |
| 290 | path.write_bytes(b"\0" * invalid_file_size) |
| 291 | |
| 292 | with path.open("rb") as file: |
| 293 | f = form(files=[FileStorage(file)]) |
| 294 | assert not f.validate() |
| 295 | assert ( |
| 296 | f.files.errors[0] |
| 297 | == f"File must be between {min_size} and {max_size} bytes." |
| 298 | ) |
| 299 | |
| 300 | |
| 301 | def test_files_length(form, min_num=2, max_num=3): |
| 302 | form.files.kwargs["validators"] = [Length(min_num, max_num)] |
| 303 | |
| 304 | f = form(files=[FileStorage("1")]) |
| 305 | assert not f.validate() |
| 306 | assert ( |
| 307 | f.files.errors[0] |
| 308 | == f"Field must be between {min_num} and {max_num} characters long." |
| 309 | ) |
| 310 | |
| 311 | f = form( |
| 312 | files=[ |
| 313 | FileStorage(filename="1"), |
| 314 | FileStorage(filename="2"), |
| 315 | ] |
| 316 | ) |
| 317 | assert f.validate() |