From a8c448930dca3ecc0f1cd2f986e23f8377a31c52 Mon Sep 17 00:00:00 2001 From: Goyo Date: Wed, 9 Oct 2024 15:59:47 +0200 Subject: [PATCH] None description shouldnt be updated (#1508) * description shouldnt be updated * linter --- gateway/api/serializers.py | 6 +++- gateway/tests/api/test_v1_program.py | 46 ++++++++++++++++++++++++ gateway/tests/api/test_v1_serializers.py | 45 +++++++++++++++++++++++ gateway/tests/fixtures/fixtures.json | 1 + 4 files changed, 97 insertions(+), 1 deletion(-) diff --git a/gateway/api/serializers.py b/gateway/api/serializers.py index 1ac2b0a4c..f2e3a9795 100644 --- a/gateway/api/serializers.py +++ b/gateway/api/serializers.py @@ -117,7 +117,11 @@ def update(self, instance, validated_data): instance.artifact = validated_data.get("artifact") instance.author = validated_data.get("author") instance.image = validated_data.get("image") - instance.description = validated_data.get("description") + + description = validated_data.get("description") + if description is not None: + instance.description = description + instance.save() return instance diff --git a/gateway/tests/api/test_v1_program.py b/gateway/tests/api/test_v1_program.py index a5391fc02..4163ce41d 100644 --- a/gateway/tests/api/test_v1_program.py +++ b/gateway/tests/api/test_v1_program.py @@ -485,3 +485,49 @@ def test_get_jobs(self): ) self.assertEqual(len(response.data), 1) self.assertEqual(response.status_code, status.HTTP_200_OK) + + def test_upload_private_function_update_without_description(self): + """Tests upload end-point authorized.""" + + fake_file = ContentFile(b"print('Hello World')") + fake_file.name = "test_run.tar" + + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) + programs_response = self.client.post( + "/api/v1/programs/upload/", + data={ + "title": "Program", + "entrypoint": "test_user_2_program.py", + "dependencies": "[]", + "artifact": fake_file, + }, + ) + + self.assertEqual(programs_response.status_code, status.HTTP_200_OK) + self.assertEqual( + programs_response.data.get("description"), "Program description test" + ) + + def test_upload_private_function_update_description(self): + """Tests upload end-point authorized.""" + + fake_file = ContentFile(b"print('Hello World')") + fake_file.name = "test_run.tar" + + user = models.User.objects.get(username="test_user") + self.client.force_authenticate(user=user) + description = "New program description test" + programs_response = self.client.post( + "/api/v1/programs/upload/", + data={ + "title": "Program", + "entrypoint": "test_user_2_program.py", + "description": description, + "dependencies": "[]", + "artifact": fake_file, + }, + ) + + self.assertEqual(programs_response.status_code, status.HTTP_200_OK) + self.assertEqual(programs_response.data.get("description"), description) diff --git a/gateway/tests/api/test_v1_serializers.py b/gateway/tests/api/test_v1_serializers.py index 76fafaeaa..23ba62cbd 100644 --- a/gateway/tests/api/test_v1_serializers.py +++ b/gateway/tests/api/test_v1_serializers.py @@ -323,3 +323,48 @@ def test_upload_program_serializer_blocked_dependency(self): serializer = UploadProgramSerializer(data=data) self.assertFalse(serializer.is_valid()) + + def test_upload_program_serializer_updates_program_without_description(self): + path_to_resource_artifact = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + "..", + "resources", + "artifact.tar", + ) + file_data = File(open(path_to_resource_artifact, "rb")) + upload_file = SimpleUploadedFile( + "artifact.tar", file_data.read(), content_type="multipart/form-data" + ) + + user = models.User.objects.get(username="test_user") + + title = "Hello world" + entrypoint = "pattern.py" + arguments = "{}" + dependencies = "[]" + description = "This is my old description" + + data = {} + data["title"] = title + data["entrypoint"] = entrypoint + data["arguments"] = arguments + data["dependencies"] = dependencies + data["description"] = description + data["artifact"] = upload_file + + serializer = UploadProgramSerializer(data=data) + serializer.is_valid() + program: Program = serializer.save(author=user) + self.assertEqual(description, program.description) + + data_without_description = {} + data_without_description["title"] = title + data_without_description["entrypoint"] = entrypoint + data_without_description["arguments"] = arguments + data_without_description["dependencies"] = dependencies + data_without_description["artifact"] = upload_file + + serializer_2 = UploadProgramSerializer(program, data=data_without_description) + serializer_2.is_valid() + program_2: Program = serializer_2.save(author=user) + self.assertEqual(description, program_2.description) diff --git a/gateway/tests/fixtures/fixtures.json b/gateway/tests/fixtures/fixtures.json index ed80f732d..8f56c8f1e 100644 --- a/gateway/tests/fixtures/fixtures.json +++ b/gateway/tests/fixtures/fixtures.json @@ -59,6 +59,7 @@ "fields": { "created": "2023-02-01T15:30:43.281796Z", "title": "Program", + "description": "Program description test", "entrypoint": "program.py", "artifact": "path", "author": 1,