From d6a3df95e113679e268f448a632a063dcc88ea0d Mon Sep 17 00:00:00 2001 From: Dalai Felinto Date: Thu, 29 Aug 2024 11:39:57 +0200 Subject: [PATCH 1/3] Cleanup: Remove unecessary "user" argument from create_file_from_data --- extensions/tests/test_manifest.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/extensions/tests/test_manifest.py b/extensions/tests/test_manifest.py index 4e38ce21..1bb6dd10 100644 --- a/extensions/tests/test_manifest.py +++ b/extensions/tests/test_manifest.py @@ -47,7 +47,7 @@ class CreateFileTest(TestCase): "id": "blender_kitsu", "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): super().tearDown() @@ -61,7 +61,7 @@ class CreateFileTest(TestCase): status=File.STATUSES.APPROVED, ) - def _create_file_from_data(self, filename, file_data, user): + def _create_file_from_data(self, filename, file_data): output_path = os.path.join(self.temp_directory, filename) manifest_path = os.path.join(self.temp_directory, "blender_manifest.toml") combined_meta_data = META_DATA.copy() @@ -105,7 +105,7 @@ class ValidateManifestTest(CreateFileTest): "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: response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True}) @@ -123,7 +123,7 @@ class ValidateManifestTest(CreateFileTest): "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: response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True}) @@ -146,7 +146,7 @@ class ValidateManifestTest(CreateFileTest): "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: response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True}) @@ -170,7 +170,7 @@ class ValidateManifestTest(CreateFileTest): "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: response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True}) @@ -194,7 +194,7 @@ class ValidateManifestTest(CreateFileTest): "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: response = self.client.post( version.extension.get_new_version_url(), {'source': fp, 'agreed_with_terms': True} @@ -221,9 +221,7 @@ class ValidateManifestTest(CreateFileTest): "version": version.version, } - extension_file = self._create_file_from_data( - "kitsu_clash.zip", kitsu_version_clash, self.user - ) + extension_file = self._create_file_from_data("kitsu_clash.zip", kitsu_version_clash) with open(extension_file, 'rb') as fp: response = self.client.post( version.extension.get_new_version_url(), {'source': fp, 'agreed_with_terms': True} @@ -262,7 +260,7 @@ class ValidateManifestTest(CreateFileTest): "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: response = self.client.post( version.extension.get_new_version_url(), {'source': fp, 'agreed_with_terms': True} @@ -284,7 +282,7 @@ class ValidateManifestTest(CreateFileTest): "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: response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True}) @@ -303,7 +301,7 @@ class ValidateManifestTest(CreateFileTest): "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: response = self.client.post(self.submit_url, {'source': fp, 'agreed_with_terms': True}) @@ -782,7 +780,7 @@ class VersionPermissionsTest(CreateFileTest): } # 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: response = self.client.post( extension.get_new_version_url(), {'source': fp, 'agreed_with_terms': True} -- 2.30.2 From e6d3b9976a4298d4a81fa06d681c704c1dea0fe3 Mon Sep 17 00:00:00 2001 From: Dalai Felinto Date: Thu, 29 Aug 2024 11:41:15 +0200 Subject: [PATCH 2/3] Cleanup: Avoid hardcoded themes/add-ons values --- files/utils.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/files/utils.py b/files/utils.py index 318cfe55..68e7d54a 100644 --- a/files/utils.py +++ b/files/utils.py @@ -21,7 +21,13 @@ import clamd import magic 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__) FORBIDDEN_FILEPATHS = [ @@ -201,13 +207,13 @@ def validate_file_list(toml_content, manifest_filepath, file_list): } ) type_slug = toml_content['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') # Special treatment for Mac, so the same problem (__MACOSX folders) # doesn't lead to two errors showing. if len(list(theme_xmls)) != 1 and '__MACOSX/' not in found_forbidden_filepaths: 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 expected_init_path = _canonical_path('__init__.py', manifest_filepath) init_filepath = find_exact_path(file_list, expected_init_path) -- 2.30.2 From 3a814d332292691c4d97527c3f7bf07dca8cd5d3 Mon Sep 17 00:00:00 2001 From: Dalai Felinto Date: Thu, 29 Aug 2024 12:50:32 +0200 Subject: [PATCH 3/3] Fix #241: 500 Error when submitting extension without "type" The fix is a one-line in utils.py. What makes the patch bigger is the uniitest. I need use the file submission framework to even run into the faulty part of the code. --- extensions/tests/test_manifest.py | 37 ++++++++++++++++++++++++++----- files/utils.py | 2 +- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/extensions/tests/test_manifest.py b/extensions/tests/test_manifest.py index 1bb6dd10..22f24d96 100644 --- a/extensions/tests/test_manifest.py +++ b/extensions/tests/test_manifest.py @@ -61,15 +61,19 @@ class CreateFileTest(TestCase): status=File.STATUSES.APPROVED, ) - def _create_file_from_data(self, filename, file_data): + def _create_file_from_data(self, filename, file_data, use_meta_data=True): output_path = os.path.join(self.temp_directory, filename) 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") 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 if type_slug == 'add-on': @@ -312,10 +316,12 @@ class ValidateManifestTest(CreateFileTest): self.assertEqual(extension.name, 'Name. - With Extra spaces and other characters Ж') -class ValidateManifestFields(TestCase): +class ValidateManifestFields(CreateFileTest): fixtures = ['licenses', 'version_permissions'] def setUp(self): + super().setUp() + self.mandatory_fields = { key: item.example for (key, item) in ManifestValidator.mandatory_fields.items() } @@ -723,6 +729,27 @@ class ValidateManifestFields(TestCase): ManifestValidator(data) 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): data = { **self.mandatory_fields, diff --git a/files/utils.py b/files/utils.py index 68e7d54a..60a57459 100644 --- a/files/utils.py +++ b/files/utils.py @@ -206,7 +206,7 @@ def validate_file_list(toml_content, manifest_filepath, file_list): 'params': {'paths': ', '.join(found_forbidden_filepaths)}, } ) - type_slug = toml_content['type'] + type_slug = toml_content.get('type') if type_slug == EXTENSION_TYPE_SLUGS_SINGULAR[EXTENSION_TYPE_CHOICES.THEME]: theme_xmls = filter_paths_by_ext(file_list, '.xml') # Special treatment for Mac, so the same problem (__MACOSX folders) -- 2.30.2