From 720c866e064638ff016a8ddc3a725031f8eccddb Mon Sep 17 00:00:00 2001 From: "Hemanth V. Alluri" Date: Fri, 7 Jun 2019 23:36:06 +0530 Subject: [PATCH] devtools: Tweak the integrations tool to use http headers fixtures. Now that we store HTTP headers in a way that is easy to retreive by specifying the integration name and fixture name, we should use it to pre-load the "Custom HTTP Headers" field in the integrations dev panel. --- static/js/portico/integrations_dev_panel.js | 5 ++++- zerver/views/development/integrations.py | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/static/js/portico/integrations_dev_panel.js b/static/js/portico/integrations_dev_panel.js index 6549d82dea..ad2eb38b83 100644 --- a/static/js/portico/integrations_dev_panel.js +++ b/static/js/portico/integrations_dev_panel.js @@ -105,7 +105,9 @@ function load_fixture_body(fixture_name) { /* Given a fixture name, use the loaded_fixtures dictionary to set * the fixture body field. */ var integration_name = get_selected_integration_name(); - var fixture_body = loaded_fixtures[integration_name][fixture_name]; + var fixture = loaded_fixtures[integration_name][fixture_name]; + var fixture_body = fixture.body; + var headers = fixture.headers; if (fixture_body === undefined) { set_results_notice("Fixture does not have a body.", "warning"); return; @@ -115,6 +117,7 @@ function load_fixture_body(fixture_name) { fixture_body = JSON.stringify(fixture_body, null, 4); } $("#fixture_body")[0].value = fixture_body; + $("#custom_http_headers")[0].value = JSON.stringify(headers, null, 4); return; } diff --git a/zerver/views/development/integrations.py b/zerver/views/development/integrations.py index d60914028b..a8ac85eff2 100644 --- a/zerver/views/development/integrations.py +++ b/zerver/views/development/integrations.py @@ -11,6 +11,7 @@ from zerver.lib.request import has_request_variables, REQ from zerver.lib.response import json_success, json_error from zerver.models import UserProfile, get_realm from zerver.management.commands.send_webhook_fixture_message import parse_headers +from zerver.lib.webhooks.common import get_fixture_http_headers ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../') @@ -63,12 +64,22 @@ def get_fixtures(request: HttpResponse, for fixture in os.listdir(fixtures_dir): fixture_path = os.path.join(fixtures_dir, fixture) - content = open(fixture_path).read() + body = open(fixture_path).read() try: - content = ujson.loads(content) + body = ujson.loads(body) except ValueError: pass # The file extension will be used to determine the type. - fixtures[fixture] = content + + headers_raw = get_fixture_http_headers(integration_name, + "".join(fixture.split(".")[:-1])) + headers = {} + for header in headers_raw: + if header.startswith("HTTP_"): # HTTP_ is a prefix intended for Django. + headers[header.lstrip("HTTP_")] = headers_raw[header] + else: + headers[header] = headers_raw[header] + + fixtures[fixture] = {"body": body, "headers": headers} return json_success({"fixtures": fixtures})