2016-04-14 23:39:37 +02:00
|
|
|
import os
|
2021-08-14 16:51:57 +02:00
|
|
|
from typing import Any, Dict, Optional, Union
|
2019-03-09 11:01:20 +01:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2016-04-14 23:39:37 +02:00
|
|
|
from django.conf import settings
|
2021-07-16 22:11:10 +02:00
|
|
|
from django.core.management.base import CommandError, CommandParser
|
2017-11-16 00:43:27 +01:00
|
|
|
from django.test import Client
|
2016-04-14 23:39:37 +02:00
|
|
|
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2019-06-21 04:41:30 +02:00
|
|
|
from zerver.lib.webhooks.common import standardize_headers
|
2017-10-09 05:12:55 +02:00
|
|
|
from zerver.models import get_realm
|
2016-04-14 23:39:37 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-10-09 05:12:55 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2016-04-14 23:39:37 +02:00
|
|
|
help = """
|
|
|
|
Create webhook message based on given fixture
|
|
|
|
Example:
|
|
|
|
./manage.py send_webhook_fixture_message \
|
2017-10-09 05:12:55 +02:00
|
|
|
[--realm=zulip] \
|
|
|
|
--fixture=zerver/webhooks/integration/fixtures/name.json \
|
2016-04-14 23:39:37 +02:00
|
|
|
'--url=/api/v1/external/integration?stream=stream_name&api_key=api_key'
|
|
|
|
|
2019-03-09 11:01:20 +01:00
|
|
|
To pass custom headers along with the webhook message use the --custom-headers
|
|
|
|
command line option.
|
|
|
|
Example:
|
|
|
|
--custom-headers='{"X-Custom-Header": "value"}'
|
|
|
|
|
|
|
|
The format is a JSON dictionary, so make sure that the header names do
|
|
|
|
not contain any spaces in them and that you use the precise quoting
|
|
|
|
approach shown above.
|
2016-04-14 23:39:37 +02:00
|
|
|
"""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"-f", "--fixture", help="The path to the fixture you'd like to send " "into Zulip"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-11-03 10:22:19 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"-u", "--url", help="The URL on your Zulip server that you want to post the fixture to"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-04-14 23:39:37 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"-H",
|
|
|
|
"--custom-headers",
|
|
|
|
help="The headers you want to provide along with your mock request to Zulip.",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-03-09 11:01:20 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
self.add_realm_args(
|
|
|
|
parser, help="Specify which realm/subdomain to connect to; default is zulip"
|
|
|
|
)
|
2017-10-09 05:12:55 +02:00
|
|
|
|
2019-03-09 11:01:20 +01:00
|
|
|
def parse_headers(self, custom_headers: Union[None, str]) -> Union[None, Dict[str, str]]:
|
2019-06-21 04:41:30 +02:00
|
|
|
if not custom_headers:
|
|
|
|
return {}
|
2019-03-09 11:01:20 +01:00
|
|
|
try:
|
2020-08-07 01:09:47 +02:00
|
|
|
custom_headers_dict = orjson.loads(custom_headers)
|
2020-08-12 20:23:23 +02:00
|
|
|
except orjson.JSONDecodeError as ve:
|
2021-02-12 08:19:30 +01:00
|
|
|
raise CommandError(
|
2021-02-12 08:20:45 +01:00
|
|
|
"Encountered an error while attempting to parse custom headers: {}\n"
|
|
|
|
"Note: all strings must be enclosed within \"\" instead of ''".format(ve)
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-06-21 04:41:30 +02:00
|
|
|
return standardize_headers(custom_headers_dict)
|
2019-03-09 11:01:20 +01:00
|
|
|
|
2021-08-14 16:51:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Optional[str]) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
if options["fixture"] is None or options["url"] is None:
|
|
|
|
self.print_help("./manage.py", "send_webhook_fixture_message")
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError
|
2016-04-14 23:39:37 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
full_fixture_path = os.path.join(settings.DEPLOY_ROOT, options["fixture"])
|
2016-04-14 23:39:37 +02:00
|
|
|
|
|
|
|
if not self._does_fixture_path_exist(full_fixture_path):
|
2021-02-12 08:20:45 +01:00
|
|
|
raise CommandError("Fixture {} does not exist".format(options["fixture"]))
|
2016-04-14 23:39:37 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
headers = self.parse_headers(options["custom_headers"])
|
2016-04-14 23:39:37 +02:00
|
|
|
json = self._get_fixture_as_json(full_fixture_path)
|
2017-10-09 05:12:55 +02:00
|
|
|
realm = self.get_realm(options)
|
|
|
|
if realm is None:
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
|
2016-04-14 23:39:37 +02:00
|
|
|
client = Client()
|
2019-03-09 11:01:20 +01:00
|
|
|
if headers:
|
2021-02-12 08:19:30 +01:00
|
|
|
result = client.post(
|
2021-02-12 08:20:45 +01:00
|
|
|
options["url"],
|
2021-02-12 08:19:30 +01:00
|
|
|
json,
|
|
|
|
content_type="application/json",
|
|
|
|
HTTP_HOST=realm.host,
|
2021-08-14 16:51:57 +02:00
|
|
|
extra=headers,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-03-09 11:01:20 +01:00
|
|
|
else:
|
2021-02-12 08:19:30 +01:00
|
|
|
result = client.post(
|
2021-02-12 08:20:45 +01:00
|
|
|
options["url"], json, content_type="application/json", HTTP_HOST=realm.host
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-10-20 18:02:32 +02:00
|
|
|
if result.status_code != 200:
|
2022-07-19 17:07:44 +02:00
|
|
|
raise CommandError(f"Error status {result.status_code}: {result.content!r}")
|
2016-04-14 23:39:37 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def _does_fixture_path_exist(self, fixture_path: str) -> bool:
|
2016-04-14 23:39:37 +02:00
|
|
|
return os.path.exists(fixture_path)
|
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
def _get_fixture_as_json(self, fixture_path: str) -> bytes:
|
|
|
|
with open(fixture_path, "rb") as f:
|
|
|
|
return orjson.dumps(orjson.loads(f.read()))
|