Extra validation of the uploaded ZIP #73
@ -271,7 +271,7 @@ class ValidateManifestTest(CreateFileTest):
|
|||||||
self.client.force_login(user)
|
self.client.force_login(user)
|
||||||
|
|
||||||
file_data = {
|
file_data = {
|
||||||
"id": "<b>id-with-hyphens</b>",
|
"id": "id-with-hyphens",
|
||||||
}
|
}
|
||||||
|
|
||||||
bad_file = self._create_file_from_data("theme.zip", file_data, self.user)
|
bad_file = self._create_file_from_data("theme.zip", file_data, self.user)
|
||||||
@ -283,7 +283,7 @@ class ValidateManifestTest(CreateFileTest):
|
|||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
error = response.context['form'].errors.get('source')
|
error = response.context['form'].errors.get('source')
|
||||||
self.assertEqual(len(error), 1)
|
self.assertEqual(len(error), 1)
|
||||||
self.assertIn('"<b>id-with-hyphens</b>"', error[0])
|
self.assertIn('"id-with-hyphens"', error[0])
|
||||||
|
|
||||||
|
|
||||||
class ValidateManifestFields(TestCase):
|
class ValidateManifestFields(TestCase):
|
||||||
|
@ -66,7 +66,6 @@ EXPECTED_VALIDATION_ERRORS = {
|
|||||||
'invalid-manifest-path.zip': {
|
'invalid-manifest-path.zip': {
|
||||||
'source': [
|
'source': [
|
||||||
'The manifest file should be at the top level of the archive, or one level deep.',
|
'The manifest file should be at the top level of the archive, or one level deep.',
|
||||||
'An add-on should have an __init__.py file.',
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
'invalid-addon-no-init.zip': {
|
'invalid-addon-no-init.zip': {
|
||||||
|
@ -139,9 +139,13 @@ class FileForm(forms.ModelForm):
|
|||||||
|
|
||||||
errors = []
|
errors = []
|
||||||
if not zipfile.is_zipfile(file_path):
|
if not zipfile.is_zipfile(file_path):
|
||||||
errors.append(forms.ValidationError(self.error_messages['invalid_zip_archive']))
|
raise forms.ValidationError(self.error_messages['invalid_zip_archive'])
|
||||||
|
|
||||||
manifest, error_codes = utils.read_manifest_from_zip(file_path)
|
manifest, error_codes = utils.read_manifest_from_zip(file_path)
|
||||||
|
for code in error_codes:
|
||||||
|
errors.append(forms.ValidationError(self.error_messages[code]))
|
||||||
|
if errors:
|
||||||
|
self.add_error('source', errors)
|
||||||
|
|
||||||
if manifest:
|
if manifest:
|
||||||
ManifestValidator(manifest)
|
ManifestValidator(manifest)
|
||||||
@ -150,9 +154,4 @@ class FileForm(forms.ModelForm):
|
|||||||
self.cleaned_data['metadata'] = manifest
|
self.cleaned_data['metadata'] = manifest
|
||||||
self.cleaned_data['type'] = EXTENSION_SLUG_TYPES[manifest['type']]
|
self.cleaned_data['type'] = EXTENSION_SLUG_TYPES[manifest['type']]
|
||||||
|
|
||||||
for code in error_codes:
|
|
||||||
errors.append(forms.ValidationError(self.error_messages[code]))
|
|
||||||
if errors:
|
|
||||||
self.add_error('source', errors)
|
|
||||||
|
|
||||||
return self.cleaned_data
|
return self.cleaned_data
|
||||||
|
@ -114,6 +114,7 @@ def read_manifest_from_zip(archive_path):
|
|||||||
# Manifest file is expected to be no deeper than one directory down
|
# Manifest file is expected to be no deeper than one directory down
|
||||||
if os.path.dirname(os.path.dirname(manifest_filepath)) != '':
|
if os.path.dirname(os.path.dirname(manifest_filepath)) != '':
|
||||||
error_codes.append('invalid_manifest_path')
|
error_codes.append('invalid_manifest_path')
|
||||||
|
return None, error_codes
|
||||||
|
|
||||||
# Extract the file content
|
# Extract the file content
|
||||||
with myzip.open(manifest_filepath) as file_content:
|
with myzip.open(manifest_filepath) as file_content:
|
||||||
|
Loading…
Reference in New Issue
Block a user