diff --git a/binder/views.py b/binder/views.py index c12c1760..0072f3f4 100644 --- a/binder/views.py +++ b/binder/views.py @@ -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): diff --git a/tests/test_binder_file_field.py b/tests/test_binder_file_field.py index faf0c183..652d7b27 100644 --- a/tests/test_binder_file_field.py +++ b/tests/test_binder_file_field.py @@ -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), + )