From d8e48d9681d17bb53aab777acd23b5b7181f69c1 Mon Sep 17 00:00:00 2001 From: Dalai Felinto Date: Thu, 10 Oct 2024 18:16:08 +0200 Subject: [PATCH 1/2] Version Upload API: Handle \n for release notes On my tests, when running the API with CURL via command-line the \n were being converted to string \n, and there would be no way to write a multi-line release notes. --- extensions/tests/test_api.py | 27 +++++++++++++++++++++++++++ extensions/views/api.py | 6 ++++++ 2 files changed, 33 insertions(+) diff --git a/extensions/tests/test_api.py b/extensions/tests/test_api.py index 058f864c..3d4a9ce2 100644 --- a/extensions/tests/test_api.py +++ b/extensions/tests/test_api.py @@ -343,6 +343,33 @@ class VersionUploadAPITest(APITestCase): self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(Version.objects.filter(extension=self.extension).count(), 2) + def test_version_upload_multiline(self): + self.assertEqual(Version.objects.filter(extension=self.extension).count(), 1) + + # Make sure the \n is escaped, to simulate how CURL passes strings. + release_notes = 'First line\\nSecond line' + + with open(self.file_path, 'rb') as version_file: + response = self.client.post( + self._get_upload_url(self.extension.extension_id), + { + 'version_file': version_file, + 'release_notes': release_notes, + }, + format='multipart', + HTTP_AUTHORIZATION=f'Bearer {self.token_key}', + ) + + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(Version.objects.filter(extension=self.extension).count(), 2) + self.extension.refresh_from_db() + + version = self.extension.latest_version + self.assertEqual(version.version, "1.0.8") + + self.assertNotIn('\n', release_notes) + self.assertIn('\n', version.release_notes) + def test_date_last_access(self): self.assertIsNone(self.token.date_last_access) with open(self.file_path, 'rb') as version_file: diff --git a/extensions/views/api.py b/extensions/views/api.py index 4380b87a..879c96c8 100644 --- a/extensions/views/api.py +++ b/extensions/views/api.py @@ -172,6 +172,12 @@ class ExtensionVersionSerializer(serializers.Serializer): version_file = serializers.FileField() release_notes = serializers.CharField(max_length=1024, required=False) + def validate_release_notes(self, value): + r"""Make sure \n and \r are valid after sanitation.""" + if not value: + return value + return value.replace('\\n', '\n').replace('\\r', '\r') + class UploadExtensionVersionView(APIView): permission_classes = [IsAuthenticated] -- 2.30.2 From 399f412e5448e1c258ea8c59c74dcb0d33c36a4f Mon Sep 17 00:00:00 2001 From: Dalai Felinto Date: Thu, 10 Oct 2024 18:47:17 +0200 Subject: [PATCH 2/2] From review: Simplify return --- extensions/views/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/views/api.py b/extensions/views/api.py index 879c96c8..fe61dc73 100644 --- a/extensions/views/api.py +++ b/extensions/views/api.py @@ -174,8 +174,8 @@ class ExtensionVersionSerializer(serializers.Serializer): def validate_release_notes(self, value): r"""Make sure \n and \r are valid after sanitation.""" - if not value: - return value + if value is None: + return return value.replace('\\n', '\n').replace('\\r', '\r') -- 2.30.2