From 7f2b3c2802e3033e07c36c54b28727bfda529275 Mon Sep 17 00:00:00 2001 From: David Moreau Simard Date: Thu, 4 Oct 2018 09:58:58 -0500 Subject: [PATCH] Expose the sha1 in the API file serializer Each filecontent is stored uniquely by sha1 so multiple files can refer to the same filecontent. Let's expose the sha1 for files so we can use this client side if need be. Change-Id: Iddd4701f8b9cc5f4b579854f495fffc6efbf04ac --- ara/api/serializers.py | 5 +++++ ara/api/tests/tests_file.py | 3 +++ 2 files changed, 8 insertions(+) diff --git a/ara/api/serializers.py b/ara/api/serializers.py index 4e9e5b5..c5efa59 100644 --- a/ara/api/serializers.py +++ b/ara/api/serializers.py @@ -103,8 +103,13 @@ class FileSerializer(serializers.ModelSerializer): model = models.File fields = "__all__" + sha1 = serializers.SerializerMethodField() content = FileContentField() + @staticmethod + def get_sha1(obj): + return obj.content.sha1 + class HostSerializer(serializers.ModelSerializer): class Meta: diff --git a/ara/api/tests/tests_file.py b/ara/api/tests/tests_file.py index ff598e4..a6002d5 100644 --- a/ara/api/tests/tests_file.py +++ b/ara/api/tests/tests_file.py @@ -73,9 +73,11 @@ class FileTestCase(APITestCase): file = factories.FileFactory() request = self.client.get("/api/v1/files/%s" % file.id) self.assertEqual(file.path, request.data["path"]) + self.assertEqual(file.content.sha1, request.data["sha1"]) def test_update_file(self): file = factories.FileFactory() + old_sha1 = file.content.sha1 self.assertNotEqual("/path/new_playbook.yml", file.path) request = self.client.put( "/api/v1/files/%s" % file.id, {"path": "/path/new_playbook.yml", "content": "# playbook"} @@ -83,6 +85,7 @@ class FileTestCase(APITestCase): self.assertEqual(200, request.status_code) file_updated = models.File.objects.get(id=file.id) self.assertEqual("/path/new_playbook.yml", file_updated.path) + self.assertNotEqual(old_sha1, file_updated.content.sha1) def test_partial_update_file(self): file = factories.FileFactory()