From 2520eb3d077a14c8f33162f2337ec9f66dda0e06 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 5 Oct 2016 09:17:37 +0200 Subject: [PATCH] Fixing webhooks test API call if the response isnt a valid json --- taiga/webhooks/tasks.py | 6 ++- tests/integration/test_webhooks.py | 61 ++++++++++++++++++++++ tests/integration/test_webhooks_signals.py | 6 +-- 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 tests/integration/test_webhooks.py diff --git a/taiga/webhooks/tasks.py b/taiga/webhooks/tasks.py index 9e9489ab..a0b737d0 100644 --- a/taiga/webhooks/tasks.py +++ b/taiga/webhooks/tasks.py @@ -22,6 +22,7 @@ import requests from requests.exceptions import RequestException from taiga.base.api.renderers import UnicodeJSONRenderer +from taiga.base.utils import json from taiga.base.utils.db import get_typename_for_model_instance from taiga.celery import app @@ -86,11 +87,14 @@ def _send_request(webhook_id, url, key, data): duration=0) else: # Webhook was sent successfully + + # response.content can be a not valid json so we encapsulate it + response_data = json.dumps({"content": response.text}) webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url, status=response.status_code, request_data=data, request_headers=dict(prepared_request.headers), - response_data=response.content, + response_data=response_data, response_headers=dict(response.headers), duration=response.elapsed.total_seconds()) finally: diff --git a/tests/integration/test_webhooks.py b/tests/integration/test_webhooks.py new file mode 100644 index 00000000..071bf89f --- /dev/null +++ b/tests/integration/test_webhooks.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2014-2016 Andrey Antukh +# Copyright (C) 2014-2016 Jesús Espino +# Copyright (C) 2014-2016 David Barragán +# Copyright (C) 2014-2016 Alejandro Alonso +# Copyright (C) 2014-2016 Anler Hernández +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import pytest + +from django.core.urlresolvers import reverse +from unittest.mock import patch +from unittest.mock import Mock + +from taiga.base.utils import json + +from .. import factories as f + +pytestmark = pytest.mark.django_db + + +@pytest.fixture +def data(): + m = type("Models", (object,), {}) + m.project_owner = f.UserFactory.create() + + m.project1 = f.ProjectFactory(is_private=True, + anon_permissions=[], + public_permissions=[], + owner=m.project_owner) + f.MembershipFactory(project=m.project1, + user=m.project_owner, + is_admin=True) + m.webhook1 = f.WebhookFactory(project=m.project1) + m.webhooklog1 = f.WebhookLogFactory(webhook=m.webhook1) + + return m + + +def test_webhook_action_test_transform_to_json(client, data): + url = reverse('webhooks-test', kwargs={"pk": data.webhook1.pk}) + + response = Mock(status_code=200, headers={}, text="ok") + response.elapsed.total_seconds.return_value = 100 + + with patch("taiga.webhooks.tasks.requests.Session.send", return_value=response) as session_send_mock: + client.login(data.project_owner) + response = client.json.post(url) + assert response.status_code == 200 + assert json.loads(response.data["response_data"]) == {"content": "ok"} diff --git a/tests/integration/test_webhooks_signals.py b/tests/integration/test_webhooks_signals.py index 1db818e6..2d6e5bc4 100644 --- a/tests/integration/test_webhooks_signals.py +++ b/tests/integration/test_webhooks_signals.py @@ -40,7 +40,7 @@ def test_new_object_with_one_webhook_signal(settings): f.WikiPageFactory.create(project=project) ] - response = Mock(status_code=200, headers={}, content="ok") + response = Mock(status_code=200, headers={}, text="ok") response.elapsed.total_seconds.return_value = 100 for obj in objects: @@ -77,7 +77,7 @@ def test_new_object_with_two_webhook_signals(settings): f.WikiPageFactory.create(project=project) ] - response = Mock(status_code=200, headers={}, content="ok") + response = Mock(status_code=200, headers={}, text="ok") response.elapsed.total_seconds.return_value = 100 for obj in objects: @@ -113,7 +113,7 @@ def test_send_request_one_webhook_signal(settings): f.WikiPageFactory.create(project=project) ] - response = Mock(status_code=200, headers={}, content="ok") + response = Mock(status_code=200, headers={}, text="ok") response.elapsed.total_seconds.return_value = 100 for obj in objects: