Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file type validation #231

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions tests/test_binder_file_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ def test_post_allowed_extension_fail(self):

self.assertEqual(response.status_code, 400)

response = self.client.post('/zoo/%s/binder_file_custom_extensions/' % zoo.id, data={
'file': ContentFile(b'foobar', name='foobar.md'),
})

self.assertEqual(response.status_code, 400)

def test_post_without_extension_fails(self):
zoo = Zoo(name='Apenheul')
zoo.save()
Expand All @@ -321,6 +327,15 @@ def test_post_without_extension_fails(self):
self.assertEqual(content['code'], 'FileTypeIncorrect')
self.assertEqual(content['allowed_types'], [{"extension": "png"}])

response = self.client.post('/zoo/%s/binder_file_custom_extensions/' % zoo.id, data={
'file': ContentFile(b'foobar', name='foobar'),
})

self.assertEqual(response.status_code, 400)
content = jsonloads(response.content)
self.assertEqual(content['code'], 'FileTypeIncorrect')
self.assertEqual(content['allowed_types'], [{"extension": "txt"}])

def test_post_allowed_extension_success(self):
for filename in ['foobar.png', 'foobar.PNG', 'foobar.Png', 'foobar.pNg', 'foobar.pnG']:
with self.subTest(filename=filename):
Expand Down Expand Up @@ -351,3 +366,31 @@ def test_post_allowed_extension_success(self):
data['data']['binder_picture_custom_extensions'],
'/zoo/{}/binder_picture_custom_extensions/?h={}&content_type=image/png&filename={}'.format(zoo.pk, PNG_HASH, filename),
)

for filename in ['foobar.txt', 'foobar.Txt', 'foobar.tXt', 'foobar.txT']:
with self.subTest(filename=filename):
zoo = Zoo(name='Apenheul')
zoo.save()

response = self.client.post('/zoo/%s/binder_file_custom_extensions/' % zoo.id, data={
'file': ContentFile(b'foobar', name=filename),
})
self.assertEqual(response.status_code, 200)
content = jsonloads(response.content)

# Remove once Django 3 lands with: https://docs.djangoproject.com/en/3.1/howto/custom-file-storage/#django.core.files.storage.get_alternative_name
zoo.refresh_from_db()
filename = basename(zoo.binder_file_custom_extensions.name) # Without folders foo/bar/

self.assertIn(
'content_type=text/plain&filename={}'.format(filename),
content['data']['binder_file_custom_extensions'],
)

response = self.client.get('/zoo/{}/'.format(zoo.pk))
self.assertEqual(response.status_code, 200)
data = jsonloads(response.content)
self.assertIn(
'content_type=text/plain&filename={}'.format(filename),
content['data']['binder_file_custom_extensions'],
)
3 changes: 2 additions & 1 deletion tests/testapp/models/zoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models.signals import post_delete
from binder.models import BinderModel, BinderImageField
from binder.models import BinderModel, BinderImageField, BinderFileField

def delete_files(sender, instance=None, **kwargs):
for field in sender._meta.fields:
Expand Down Expand Up @@ -42,6 +42,7 @@ class Binder:
binder_picture_not_null = BinderImageField(blank=True)

binder_picture_custom_extensions = BinderImageField(allowed_extensions=['png'], blank=True, null=True)
binder_file_custom_extensions = BinderFileField(allowed_extensions=['txt'], blank=True, null=True)

def __str__(self):
return 'zoo %d: %s' % (self.pk, self.name)
Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/views/zoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ZooView(PermissionView):
m2m_fields = ['contacts', 'zoo_employees', 'most_popular_animals']
model = Zoo
file_fields = ['floor_plan', 'django_picture', 'binder_picture', 'django_picture_not_null',
'binder_picture_not_null', 'binder_picture_custom_extensions']
'binder_picture_not_null', 'binder_picture_custom_extensions', 'binder_file_custom_extensions']
shown_properties = ['animal_count']
image_resize_threshold = {
'floor_plan': 500,
Expand Down
Loading