Fix #241: 500 Error when submitting extension without "type" #242

Merged
Dalai Felinto merged 3 commits from fix-241-missing-type into main 2024-08-29 13:03:57 +02:00
2 changed files with 53 additions and 22 deletions

View File

@ -47,7 +47,7 @@ class CreateFileTest(TestCase):
"id": "blender_kitsu", "id": "blender_kitsu",
"version": "0.1.5", "version": "0.1.5",
} }
self.file = self._create_file_from_data("blender_kitsu_1.5.0.zip", file_data, self.user) self.file = self._create_file_from_data("blender_kitsu_1.5.0.zip", file_data)
def tearDown(self): def tearDown(self):
super().tearDown() super().tearDown()
@ -61,15 +61,19 @@ class CreateFileTest(TestCase):
status=File.STATUSES.APPROVED, status=File.STATUSES.APPROVED,
) )
def _create_file_from_data(self, filename, file_data, user): def _create_file_from_data(self, filename, file_data, use_meta_data=True):
output_path = os.path.join(self.temp_directory, filename) output_path = os.path.join(self.temp_directory, filename)
manifest_path = os.path.join(self.temp_directory, "blender_manifest.toml") manifest_path = os.path.join(self.temp_directory, "blender_manifest.toml")
combined_meta_data = META_DATA.copy()
combined_meta_data.update(file_data) if use_meta_data:
combined_meta_data = META_DATA.copy()
combined_meta_data.update(file_data)
else:
combined_meta_data = file_data
version = combined_meta_data.get("version", "0.1.0") version = combined_meta_data.get("version", "0.1.0")
extension_id = combined_meta_data.get("id", "foobar").strip() extension_id = combined_meta_data.get("id", "foobar").strip()
type_slug = combined_meta_data['type'] type_slug = combined_meta_data.get('type')
init_path = None init_path = None
if type_slug == 'add-on': if type_slug == 'add-on':
@ -105,7 +109,7 @@ class ValidateManifestTest(CreateFileTest):
"id": "id-with-hyphens", "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)
with open(bad_file, 'rb') as fp: with open(bad_file, 'rb') as fp:
response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True}) response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True})
@ -123,7 +127,7 @@ class ValidateManifestTest(CreateFileTest):
"id": "id with spaces", "id": "id with spaces",
} }
bad_file = self._create_file_from_data("theme.zip", file_data, self.user) bad_file = self._create_file_from_data("theme.zip", file_data)
with open(bad_file, 'rb') as fp: with open(bad_file, 'rb') as fp:
response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True}) response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True})
@ -146,7 +150,7 @@ class ValidateManifestTest(CreateFileTest):
"version": "0.1.5", "version": "0.1.5",
} }
extension_file = self._create_file_from_data("theme.zip", kitsu_1_5, self.user) extension_file = self._create_file_from_data("theme.zip", kitsu_1_5)
with open(extension_file, 'rb') as fp: with open(extension_file, 'rb') as fp:
response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True}) response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True})
@ -170,7 +174,7 @@ class ValidateManifestTest(CreateFileTest):
"version": "0.1.5", "version": "0.1.5",
} }
extension_file = self._create_file_from_data("theme.zip", kitsu_1_5, self.user) extension_file = self._create_file_from_data("theme.zip", kitsu_1_5)
with open(extension_file, 'rb') as fp: with open(extension_file, 'rb') as fp:
response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True}) response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True})
@ -194,7 +198,7 @@ class ValidateManifestTest(CreateFileTest):
"version": "0.1.6", "version": "0.1.6",
} }
extension_file = self._create_file_from_data("theme.zip", non_kitsu_1_6, self.user) extension_file = self._create_file_from_data("theme.zip", non_kitsu_1_6)
with open(extension_file, 'rb') as fp: with open(extension_file, 'rb') as fp:
response = self.client.post( response = self.client.post(
version.extension.get_new_version_url(), {'source': fp, 'agreed_with_terms': True} version.extension.get_new_version_url(), {'source': fp, 'agreed_with_terms': True}
@ -221,9 +225,7 @@ class ValidateManifestTest(CreateFileTest):
"version": version.version, "version": version.version,
} }
extension_file = self._create_file_from_data( extension_file = self._create_file_from_data("kitsu_clash.zip", kitsu_version_clash)
"kitsu_clash.zip", kitsu_version_clash, self.user
)
with open(extension_file, 'rb') as fp: with open(extension_file, 'rb') as fp:
response = self.client.post( response = self.client.post(
version.extension.get_new_version_url(), {'source': fp, 'agreed_with_terms': True} version.extension.get_new_version_url(), {'source': fp, 'agreed_with_terms': True}
@ -262,7 +264,7 @@ class ValidateManifestTest(CreateFileTest):
"version": '0.1.6', "version": '0.1.6',
} }
extension_file = self._create_file_from_data("updated_kitsu.zip", updated_kitsu, self.user) extension_file = self._create_file_from_data("updated_kitsu.zip", updated_kitsu)
with open(extension_file, 'rb') as fp: with open(extension_file, 'rb') as fp:
response = self.client.post( response = self.client.post(
version.extension.get_new_version_url(), {'source': fp, 'agreed_with_terms': True} version.extension.get_new_version_url(), {'source': fp, 'agreed_with_terms': True}
@ -284,7 +286,7 @@ class ValidateManifestTest(CreateFileTest):
"id": "id-with-hyphens", "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)
with open(bad_file, 'rb') as fp: with open(bad_file, 'rb') as fp:
response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True}) response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True})
@ -303,7 +305,7 @@ class ValidateManifestTest(CreateFileTest):
"name": "Name. - With Extra spaces and other characters Ж", "name": "Name. - With Extra spaces and other characters Ж",
} }
extension_file = self._create_file_from_data("theme.zip", file_data, self.user) extension_file = self._create_file_from_data("theme.zip", file_data)
with open(extension_file, 'rb') as fp: with open(extension_file, 'rb') as fp:
response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True}) response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True})
@ -314,10 +316,12 @@ class ValidateManifestTest(CreateFileTest):
self.assertEqual(extension.name, 'Name. - With Extra spaces and other characters Ж') self.assertEqual(extension.name, 'Name. - With Extra spaces and other characters Ж')
class ValidateManifestFields(TestCase): class ValidateManifestFields(CreateFileTest):
fixtures = ['licenses', 'version_permissions'] fixtures = ['licenses', 'version_permissions']
def setUp(self): def setUp(self):
super().setUp()
self.mandatory_fields = { self.mandatory_fields = {
key: item.example for (key, item) in ManifestValidator.mandatory_fields.items() key: item.example for (key, item) in ManifestValidator.mandatory_fields.items()
} }
@ -725,6 +729,27 @@ class ValidateManifestFields(TestCase):
ManifestValidator(data) ManifestValidator(data)
self.assertEqual(1, len(e.exception.messages)) self.assertEqual(1, len(e.exception.messages))
def test_type_missing(self):
user = UserFactory()
self.client.force_login(user)
file_data = {
**self.mandatory_fields,
**self.optional_fields,
}
del file_data['type']
del file_data['build']
del file_data['wheels']
bad_file = self._create_file_from_data("extension.zip", file_data, use_meta_data=False)
with open(bad_file, 'rb') as fp:
response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True})
self.assertEqual(response.status_code, 200)
error = response.context['form'].errors.get('source')[0]
self.assertIn("missing", error)
self.assertIn("type", error)
def test_schema_version(self): def test_schema_version(self):
data = { data = {
**self.mandatory_fields, **self.mandatory_fields,
@ -782,7 +807,7 @@ class VersionPermissionsTest(CreateFileTest):
} }
# Step 1: submit the file # Step 1: submit the file
extension_file = self._create_file_from_data("kitsu-0.1.6.zip", new_kitsu, self.user) extension_file = self._create_file_from_data("kitsu-0.1.6.zip", new_kitsu)
with open(extension_file, 'rb') as fp: with open(extension_file, 'rb') as fp:
response = self.client.post( response = self.client.post(
extension.get_new_version_url(), {'source': fp, 'agreed_with_terms': True} extension.get_new_version_url(), {'source': fp, 'agreed_with_terms': True}

View File

@ -21,7 +21,13 @@ import clamd
import magic import magic
import requests import requests
from constants.base import THUMBNAIL_FORMAT, THUMBNAIL_SIZES, THUMBNAIL_QUALITY from constants.base import (
EXTENSION_TYPE_CHOICES,
EXTENSION_TYPE_SLUGS_SINGULAR,
THUMBNAIL_FORMAT,
THUMBNAIL_SIZES,
THUMBNAIL_QUALITY,
)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
FORBIDDEN_FILEPATHS = [ FORBIDDEN_FILEPATHS = [
@ -200,14 +206,14 @@ def validate_file_list(toml_content, manifest_filepath, file_list):
'params': {'paths': ', '.join(found_forbidden_filepaths)}, 'params': {'paths': ', '.join(found_forbidden_filepaths)},
} }
) )
type_slug = toml_content['type'] type_slug = toml_content.get('type')
if type_slug == 'theme': if type_slug == EXTENSION_TYPE_SLUGS_SINGULAR[EXTENSION_TYPE_CHOICES.THEME]:
theme_xmls = filter_paths_by_ext(file_list, '.xml') theme_xmls = filter_paths_by_ext(file_list, '.xml')
# Special treatment for Mac, so the same problem (__MACOSX folders) # Special treatment for Mac, so the same problem (__MACOSX folders)
# doesn't lead to two errors showing. # doesn't lead to two errors showing.
if len(list(theme_xmls)) != 1 and '__MACOSX/' not in found_forbidden_filepaths: if len(list(theme_xmls)) != 1 and '__MACOSX/' not in found_forbidden_filepaths:
error_codes.append('missing_or_multiple_theme_xml') error_codes.append('missing_or_multiple_theme_xml')
elif type_slug == 'add-on': elif type_slug == EXTENSION_TYPE_SLUGS_SINGULAR[EXTENSION_TYPE_CHOICES.BPY]:
# __init__.py is expected to be next to the manifest # __init__.py is expected to be next to the manifest
expected_init_path = _canonical_path('__init__.py', manifest_filepath) expected_init_path = _canonical_path('__init__.py', manifest_filepath)
init_filepath = find_exact_path(file_list, expected_init_path) init_filepath = find_exact_path(file_list, expected_init_path)