Extra validation of the uploaded ZIP #73

Merged
Anna Sirota merged 13 commits from validation-single-theme-xml into main 2024-04-11 12:32:50 +02:00
4 changed files with 8 additions and 9 deletions
Showing only changes of commit 34dfb0bb47 - Show all commits

View File

@ -271,7 +271,7 @@ class ValidateManifestTest(CreateFileTest):
self.client.force_login(user)
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)
@ -283,7 +283,7 @@ class ValidateManifestTest(CreateFileTest):
self.assertEqual(response.status_code, 200)
error = response.context['form'].errors.get('source')
self.assertEqual(len(error), 1)
self.assertIn('"&lt;b&gt;id-with-hyphens&lt;/b&gt;"', error[0])
self.assertIn('"id-with-hyphens"', error[0])
class ValidateManifestFields(TestCase):

View File

@ -66,7 +66,6 @@ EXPECTED_VALIDATION_ERRORS = {
'invalid-manifest-path.zip': {
'source': [
'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': {

View File

@ -139,9 +139,13 @@ class FileForm(forms.ModelForm):
errors = []
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)
for code in error_codes:
errors.append(forms.ValidationError(self.error_messages[code]))
if errors:
self.add_error('source', errors)
if manifest:
ManifestValidator(manifest)
@ -150,9 +154,4 @@ class FileForm(forms.ModelForm):
self.cleaned_data['metadata'] = manifest
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

View File

@ -114,6 +114,7 @@ def read_manifest_from_zip(archive_path):
# Manifest file is expected to be no deeper than one directory down
if os.path.dirname(os.path.dirname(manifest_filepath)) != '':
error_codes.append('invalid_manifest_path')
return None, error_codes
# Extract the file content
with myzip.open(manifest_filepath) as file_content: