From 8fbf4b80688e30f2aaed892c1bfd71ee9b5e8aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lex=20Hermida?= Date: Fri, 13 Apr 2018 10:47:35 +0200 Subject: [PATCH] Add assigned users to webhooks --- taiga/projects/userstories/serializers.py | 7 ---- taiga/projects/userstories/utils.py | 1 - taiga/webhooks/serializers.py | 8 +++++ .../integration/test_webhooks_userstories.py | 32 +++++++++++++++++++ 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/taiga/projects/userstories/serializers.py b/taiga/projects/userstories/serializers.py index ab6d5f47..3177884b 100644 --- a/taiga/projects/userstories/serializers.py +++ b/taiga/projects/userstories/serializers.py @@ -85,13 +85,6 @@ class UserStoryListSerializer(ProjectExtraInfoSerializerMixin, assigned_users = MethodField() - # def get_assigned_users(self, obj): - # assert hasattr(obj, "assigned_users_attr"), "instance must have a assigned_users_attr attribute" - # if not obj.assigned_users_attr: - # return [] - # - # return obj.assigned_users_attr - def get_assigned_users(self, obj): """Get the assigned of an object. diff --git a/taiga/projects/userstories/utils.py b/taiga/projects/userstories/utils.py index 952f4ca6..880f7139 100644 --- a/taiga/projects/userstories/utils.py +++ b/taiga/projects/userstories/utils.py @@ -157,7 +157,6 @@ def attach_extra_info(queryset, user=None, include_attachments=False, include_ta queryset = attach_total_points(queryset) queryset = attach_role_points(queryset) queryset = attach_epics(queryset) - # queryset = attach_assigned_users(queryset) if include_attachments: queryset = attach_basic_attachments(queryset) diff --git a/taiga/webhooks/serializers.py b/taiga/webhooks/serializers.py index 04516c06..9168bc9d 100644 --- a/taiga/webhooks/serializers.py +++ b/taiga/webhooks/serializers.py @@ -359,6 +359,7 @@ class UserStorySerializer(CustomAttributesValuesWebhookSerializerMixin, serializ permalink = serializers.SerializerMethodField("get_permalink") owner = UserSerializer() assigned_to = UserSerializer() + assigned_users = MethodField() points = MethodField() status = UserStoryStatusSerializer() milestone = MilestoneSerializer() @@ -369,6 +370,13 @@ class UserStorySerializer(CustomAttributesValuesWebhookSerializerMixin, serializ def custom_attributes_queryset(self, project): return project.userstorycustomattributes.all() + def get_assigned_users(self, obj): + """Get the assigned of an object. + + :return: User queryset object representing the assigned users + """ + return [user.id for user in obj.assigned_users.all()] + def get_watchers(self, obj): return list(obj.get_watchers().values_list("id", flat=True)) diff --git a/tests/integration/test_webhooks_userstories.py b/tests/integration/test_webhooks_userstories.py index 0cbfef61..67302b49 100644 --- a/tests/integration/test_webhooks_userstories.py +++ b/tests/integration/test_webhooks_userstories.py @@ -82,6 +82,38 @@ def test_webhooks_when_update_user_story(settings): assert data["change"]["diff"]["subject"]["from"] != data["data"]["subject"] +def test_webhooks_when_update_assigned_users_user_story(settings): + settings.WEBHOOKS_ENABLED = True + project = f.ProjectFactory() + f.WebhookFactory.create(project=project) + f.WebhookFactory.create(project=project) + + obj = f.UserStoryFactory.create(project=project) + + with patch('taiga.webhooks.tasks._send_request') as send_request_mock: + services.take_snapshot(obj, user=obj.owner) + assert send_request_mock.call_count == 2 + + user = f.create_user() + obj.assigned_users.add(user) + obj.save() + + with patch('taiga.webhooks.tasks._send_request') as send_request_mock: + services.take_snapshot(obj, user=obj.owner,) + assert send_request_mock.call_count == 2 + + (webhook_id, url, key, data) = send_request_mock.call_args[0] + + assert data["action"] == "change" + assert data["type"] == "userstory" + assert data["by"]["id"] == obj.owner.id + assert len(data["data"]["assigned_users"]) == \ + obj.assigned_users.count() + assert data["data"]["assigned_users"] == [user.id] + assert not data["change"]["diff"]["assigned_users"]["from"] + assert data["change"]["diff"]["assigned_users"]["to"] == user.username + + def test_webhooks_when_delete_user_story(settings): settings.WEBHOOKS_ENABLED = True project = f.ProjectFactory()