Repository: zulip/zulip

Issue:
Create Integration for Json Printing any Webhook Payload
Create Integration for JSON Printing any Webhook Payload

Useful for webhook testing

https://chat.zulip.org/#narrow/stream/127-integrations/topic/webhook.20testing


Discussion:
@zulipbot claim
@zulipbot add "area: integrations"
Hello @zulip/server-integrations members, this issue was labeled with the "area: integrations" label, so you may want to check it out!

<!-- areaLabelAddition -->

Resolving patch:
diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.py
--- a/zerver/lib/integrations.py
+++ b/zerver/lib/integrations.py
@@ -398,6 +398,7 @@ def __init__(self, name: str, *args: Any, **kwargs: Any) -> None:
     WebhookIntegration("intercom", ["customer-support"], display_name="Intercom"),
     WebhookIntegration("jira", ["project-management"], display_name="JIRA"),
     WebhookIntegration("jotform", ["misc"], display_name="Jotform"),
+    WebhookIntegration("json", ["misc"], display_name="JSON formatter"),
     WebhookIntegration("librato", ["monitoring"]),
     WebhookIntegration("mention", ["marketing"], display_name="Mention"),
     WebhookIntegration("netlify", ["continuous-integration", "deployment"], display_name="Netlify"),
@@ -730,6 +731,7 @@ def __init__(self, name: str, *args: Any, **kwargs: Any) -> None:
     "intercom": [ScreenshotConfig("conversation_admin_replied.json")],
     "jira": [ScreenshotConfig("created_v1.json")],
     "jotform": [ScreenshotConfig("response.json")],
+    "json": [ScreenshotConfig("json_github_push__1_commit.json")],
     "librato": [ScreenshotConfig("three_conditions_alert.json", payload_as_query_param=True)],
     "mention": [ScreenshotConfig("webfeeds.json")],
     "netlify": [ScreenshotConfig("deploy_building.json")],
diff --git a/zerver/webhooks/json/__init__.py b/zerver/webhooks/json/__init__.py
new file mode 100644
diff --git a/zerver/webhooks/json/view.py b/zerver/webhooks/json/view.py
new file mode 100644
--- /dev/null
+++ b/zerver/webhooks/json/view.py
@@ -0,0 +1,39 @@
+import json
+from typing import Any, Dict
+
+from django.http import HttpRequest, HttpResponse
+
+from zerver.decorator import REQ, has_request_variables, webhook_view
+from zerver.lib.response import json_success
+from zerver.lib.webhooks.common import check_send_webhook_message
+from zerver.models import UserProfile
+
+JSON_MESSAGE_TEMPLATE = """
+```json
+{webhook_payload}
+```
+""".strip()
+
+
+@webhook_view("JSON")
+@has_request_variables
+def api_json_webhook(
+    request: HttpRequest,
+    user_profile: UserProfile,
+    payload: Dict[str, Any] = REQ(argument_type="body"),
+) -> HttpResponse:
+
+    body = get_body_for_http_request(payload)
+    subject = get_subject_for_http_request(payload)
+
+    check_send_webhook_message(request, user_profile, subject, body)
+    return json_success()
+
+
+def get_subject_for_http_request(payload: Dict[str, Any]) -> str:
+    return "JSON"
+
+
+def get_body_for_http_request(payload: Dict[str, Any]) -> str:
+    prettypayload = json.dumps(payload, indent=2)
+    return JSON_MESSAGE_TEMPLATE.format(webhook_payload=prettypayload, sort_keys=True)


Test patch:
diff --git a/zerver/webhooks/json/tests.py b/zerver/webhooks/json/tests.py
new file mode 100644
--- /dev/null
+++ b/zerver/webhooks/json/tests.py
@@ -0,0 +1,54 @@
+import json
+
+from zerver.lib.test_classes import WebhookTestCase
+
+
+class JsonHookTests(WebhookTestCase):
+    STREAM_NAME = "json"
+    URL_TEMPLATE = "/api/v1/external/json?api_key={api_key}&stream={stream}"
+    FIXTURE_DIR_NAME = "json"
+
+    def test_json_github_push__1_commit_message(self) -> None:
+        """
+        Tests if json github push 1 commit is handled correctly
+        """
+        with open("zerver/webhooks/json/fixtures/json_github_push__1_commit.json") as f:
+            original_fixture = json.load(f)
+
+        expected_topic = "JSON"
+        expected_message = """```json
+{original_fixture}
+```""".format(
+            original_fixture=json.dumps(original_fixture, indent=2)
+        )
+        self.check_webhook("json_github_push__1_commit", expected_topic, expected_message)
+
+    def test_json_pingdom_http_up_to_down_message(self) -> None:
+        """
+        Tests if json pingdom http up to down is handled correctly
+        """
+        with open("zerver/webhooks/json/fixtures/json_pingdom_http_up_to_down.json") as f:
+            original_fixture = json.load(f)
+
+        expected_topic = "JSON"
+        expected_message = """```json
+{original_fixture}
+```""".format(
+            original_fixture=json.dumps(original_fixture, indent=2)
+        )
+        self.check_webhook("json_pingdom_http_up_to_down", expected_topic, expected_message)
+
+    def test_json_sentry_event_for_exception_js_message(self) -> None:
+        """
+        Tests if json sentry event for exception js is handled correctly
+        """
+        with open("zerver/webhooks/json/fixtures/json_sentry_event_for_exception_js.json") as f:
+            original_fixture = json.load(f)
+
+        expected_topic = "JSON"
+        expected_message = """```json
+{original_fixture}
+```""".format(
+            original_fixture=json.dumps(original_fixture, indent=2)
+        )
+        self.check_webhook("json_sentry_event_for_exception_js", expected_topic, expected_message)