diff --git a/frappe/integrations/doctype/webhook/test_webhook.py b/frappe/integrations/doctype/webhook/test_webhook.py index 1701c418f7..d308ec95ab 100644 --- a/frappe/integrations/doctype/webhook/test_webhook.py +++ b/frappe/integrations/doctype/webhook/test_webhook.py @@ -3,6 +3,9 @@ import json from contextlib import contextmanager +import responses +from responses.matchers import json_params_matcher + import frappe from frappe.integrations.doctype.webhook.webhook import ( enqueue_webhook, @@ -94,9 +97,15 @@ class TestWebhook(FrappeTestCase): self.test_user.email = "user1@integration.webhooks.test.com" self.test_user.first_name = "user1" + self.responses = responses.RequestsMock() + self.responses.start() + def tearDown(self) -> None: self.user.delete() self.test_user.delete() + + self.responses.stop() + self.responses.reset() super().tearDown() def test_webhook_trigger_with_enabled_webhooks(self): @@ -172,6 +181,13 @@ class TestWebhook(FrappeTestCase): self.assertEqual(data, {"name": self.user.name}) def test_webhook_req_log_creation(self): + self.responses.add( + responses.POST, + "https://httpbin.org/post", + status=200, + json={}, + ) + if not frappe.db.get_value("User", "user2@integration.webhooks.test.com"): user = frappe.get_doc( {"doctype": "User", "email": "user2@integration.webhooks.test.com", "first_name": "user2"} @@ -185,6 +201,7 @@ class TestWebhook(FrappeTestCase): self.assertTrue(frappe.get_all("Webhook Request Log", pluck="name")) def test_webhook_with_array_body(self): + """Check if array request body are supported.""" wh_config = { "doctype": "Webhook", @@ -194,7 +211,7 @@ class TestWebhook(FrappeTestCase): "request_url": "https://httpbin.org/post", "request_method": "POST", "request_structure": "JSON", - "webhook_json": '[\r\n{% for n in range(3) %}\r\n {\r\n "title": "{{ doc.title }}",\r\n "n": {{ n }}\r\n }\r\n {%- if not loop.last -%}\r\n , \r\n {%endif%}\r\n{%endfor%}\r\n]', + "webhook_json": '[\r\n{% for n in range(3) %}\r\n {\r\n "title": "{{ doc.title }}" }\r\n {%- if not loop.last -%}\r\n , \r\n {%endif%}\r\n{%endfor%}\r\n]', "meets_condition": "Yes", "webhook_headers": [ { @@ -204,13 +221,22 @@ class TestWebhook(FrappeTestCase): ], } - with get_test_webhook(wh_config) as wh: - doc = frappe.new_doc("Note") - doc.title = "Test Webhook Note" + doc = frappe.new_doc("Note") + doc.title = "Test Webhook Note" + expected_req = [{"title": doc.title} for _ in range(3)] + self.responses.add( + responses.POST, + "https://httpbin.org/post", + status=200, + json=expected_req, + match=[json_params_matcher(expected_req)], + ) + + with get_test_webhook(wh_config) as wh: enqueue_webhook(doc, wh) log = frappe.get_last_doc("Webhook Request Log") - self.assertEqual(len(json.loads(log.response)["json"]), 3) + self.assertEqual(len(json.loads(log.response)), 3) def test_webhook_with_dynamic_url_enabled(self): wh_config = { @@ -232,12 +258,16 @@ class TestWebhook(FrappeTestCase): ], } + self.responses.add( + responses.POST, + "https://httpbin.org/anything/Note", + status=200, + ) + with get_test_webhook(wh_config) as wh: doc = frappe.new_doc("Note") doc.title = "Test Webhook Note" enqueue_webhook(doc, wh) - log = frappe.get_last_doc("Webhook Request Log") - self.assertEqual(json.loads(log.response)["url"], "https://httpbin.org/anything/Note") def test_webhook_with_dynamic_url_disabled(self): wh_config = { @@ -259,11 +289,13 @@ class TestWebhook(FrappeTestCase): ], } + self.responses.add( + responses.POST, + "https://httpbin.org/anything/{{doc.doctype}}", + status=200, + ) + with get_test_webhook(wh_config) as wh: doc = frappe.new_doc("Note") doc.title = "Test Webhook Note" enqueue_webhook(doc, wh) - log = frappe.get_last_doc("Webhook Request Log") - self.assertEqual( - json.loads(log.response)["url"], "https://httpbin.org/anything/{{doc.doctype}}" - ) diff --git a/pyproject.toml b/pyproject.toml index 687a08ac0f..b9c1823794 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -105,3 +105,4 @@ pyngrok = "~=6.0.0" unittest-xml-reporting = "~=3.2.0" watchdog = "~=3.0.0" hypothesis = "~=6.77.0" +responses = "~=0.23.1"