[Backport] Fixing webhooks test API call if the response isnt a valid json

remotes/origin/issue/4217/improving-mail-design
Alejandro Alonso 2016-10-05 09:17:37 +02:00 committed by David Barragán Merino
parent 057804248f
commit a10bf440ad
3 changed files with 69 additions and 4 deletions

View File

@ -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:

View File

@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2014-2016 Andrey Antukh <niwi@niwi.nz>
# Copyright (C) 2014-2016 Jesús Espino <jespinog@gmail.com>
# Copyright (C) 2014-2016 David Barragán <bameda@dbarragan.com>
# Copyright (C) 2014-2016 Alejandro Alonso <alejandro.alonso@kaleidos.net>
# Copyright (C) 2014-2016 Anler Hernández <hello@anler.me>
# 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 <http://www.gnu.org/licenses/>.
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"}

View File

@ -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: