-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1350 from maykinmedia/limited-file-upload-field-o…
…verridable-params Make parameters of LimitedUploadFileField overridable
- Loading branch information
Showing
3 changed files
with
130 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/open_inwoner/utils/tests/test_limited_file_upload_field.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from unittest import TestCase | ||
|
||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
from django.forms import Form | ||
|
||
from open_inwoner.utils.forms import LimitedUploadFileField | ||
|
||
|
||
def form_class_factory(*args, **kwargs): | ||
class TestForm(Form): | ||
file = LimitedUploadFileField(*args, **kwargs) | ||
|
||
return TestForm | ||
|
||
|
||
class LimitedUploadFileFieldTests(TestCase): | ||
def test_title_starting_lowercase(self): | ||
|
||
with self.assertRaises(ValueError): | ||
form_class_factory(allowed_mime_types=["i-am-not-a/valid-mime-type"]) | ||
|
||
def test_file_too_small(self): | ||
TestForm = form_class_factory(min_upload_size=2) | ||
form = TestForm( | ||
files={ | ||
"file": SimpleUploadedFile( | ||
"test_file.txt", b"1", content_type="application/pdf" | ||
) | ||
} | ||
) | ||
form.is_valid() | ||
|
||
self.assertEqual( | ||
form.errors["file"], | ||
[ | ||
"Een aangeleverd bestand dient minimaal 2\xa0bytes te zijn, uw bestand is te klein." | ||
], | ||
) | ||
|
||
def test_file_too_big(self): | ||
TestForm = form_class_factory(max_upload_size=2) | ||
form = TestForm( | ||
files={ | ||
"file": SimpleUploadedFile( | ||
"test_file.txt", b"111", content_type="application/pdf" | ||
) | ||
} | ||
) | ||
form.is_valid() | ||
|
||
self.assertEqual( | ||
form.errors["file"], | ||
[ | ||
"Een aangeleverd bestand dient maximaal 2\xa0bytes te zijn, uw bestand is te groot." | ||
], | ||
) | ||
|
||
def test_file_has_wrong_type(self): | ||
TestForm = form_class_factory() | ||
form = TestForm( | ||
files={ | ||
"file": SimpleUploadedFile( | ||
"test_file.txt", b"111", content_type="i-am-not-a/valid-mime-type" | ||
) | ||
} | ||
) | ||
form.is_valid() | ||
|
||
self.assertEqual( | ||
form.errors["file"], | ||
[ | ||
"Het type bestand dat u hebt geüpload is ongeldig. Geldige bestandstypen zijn: docx, doc, xlsx, xls, txt, odt, odf, ods, pdf, jpg, png" | ||
], | ||
) |