Skip to content

Commit

Permalink
Make allowed_extensions case insensitive (#186)
Browse files Browse the repository at this point in the history
So there is no difference between `.jpg` or `.JPG`.
  • Loading branch information
abzainuddin authored Aug 24, 2021
1 parent 074f1cc commit 671daf6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
4 changes: 2 additions & 2 deletions binder/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2290,9 +2290,9 @@ def dispatch_file_field(self, request, pk=None, file_field=None):
field = self.model._meta.get_field(file_field_name)

if getattr(field, 'allowed_extensions', None) is not None:
extension = None if '.' not in file.name else file.name.split('.')[-1]
extension = None if '.' not in file.name else file.name.split('.')[-1].lower()

if extension not in field.allowed_extensions:
if extension not in (ex.lower() for ex in field.allowed_extensions):
raise BinderFileTypeIncorrect([{'extension': t} for t in field.allowed_extensions])

if isinstance(field, models.fields.files.ImageField):
Expand Down
64 changes: 41 additions & 23 deletions tests/test_binder_file_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,34 +267,52 @@ def test_post_allowed_extension_fail(self):
response = self.client.post('/zoo/%s/binder_picture_custom_extensions/' % zoo.id, data={
'file': ContentFile(CONTENT, name='foobar.jpg'),
})

self.assertEqual(response.status_code, 400)

def test_post_allowed_extension_success(self):
filename = 'foobar.png'
content = jsonloads(response.content)
self.assertEqual(content['code'], 'FileTypeIncorrect')
self.assertEqual(content['allowed_types'], [{"extension": "png"}])

def test_post_without_extension_fails(self):
zoo = Zoo(name='Apenheul')
zoo.save()

response = self.client.post('/zoo/%s/binder_picture_custom_extensions/' % zoo.id, data={
'file': ContentFile(CONTENT, name=filename),
'file': ContentFile(CONTENT, name='foobar'),
})
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_picture_custom_extensions.name) # Without folders foo/bar/


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

self.assertEqual(
content['data']['binder_picture_custom_extensions'],
'/zoo/{}/binder_picture_custom_extensions/?h={}&content_type=image/png&filename={}'.format(zoo.pk, HASH, filename),
)

response = self.client.get('/zoo/{}/'.format(zoo.pk))
self.assertEqual(response.status_code, 200)
data = jsonloads(response.content)
self.assertEqual(
data['data']['binder_picture_custom_extensions'],
'/zoo/{}/binder_picture_custom_extensions/?h={}&content_type=image/png&filename={}'.format(zoo.pk, HASH, filename),
)
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):
zoo = Zoo(name='Apenheul')
zoo.save()

response = self.client.post('/zoo/%s/binder_picture_custom_extensions/' % zoo.id, data={
'file': ContentFile(CONTENT, 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_picture_custom_extensions.name) # Without folders foo/bar/



self.assertEqual(
content['data']['binder_picture_custom_extensions'],
'/zoo/{}/binder_picture_custom_extensions/?h={}&content_type=image/png&filename={}'.format(zoo.pk, HASH, filename),
)

response = self.client.get('/zoo/{}/'.format(zoo.pk))
self.assertEqual(response.status_code, 200)
data = jsonloads(response.content)
self.assertEqual(
data['data']['binder_picture_custom_extensions'],
'/zoo/{}/binder_picture_custom_extensions/?h={}&content_type=image/png&filename={}'.format(zoo.pk, HASH, filename),
)

0 comments on commit 671daf6

Please sign in to comment.