2016-04-14 23:39:37 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
import ujson
|
2016-04-14 23:39:37 +02:00
|
|
|
from django.conf import settings
|
2017-11-02 10:19:24 +01:00
|
|
|
from django.core.management.base import CommandParser
|
2017-11-16 00:43:27 +01:00
|
|
|
from django.test import Client
|
2016-04-14 23:39:37 +02:00
|
|
|
|
2017-10-09 05:12:55 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
|
|
|
from zerver.models import get_realm
|
2016-04-14 23:39:37 +02: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'
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2016-11-03 10:22:19 +01:00
|
|
|
parser.add_argument('-f', '--fixture',
|
|
|
|
dest='fixture',
|
|
|
|
type=str,
|
|
|
|
help='The path to the fixture you\'d like to send '
|
|
|
|
'into Zulip')
|
|
|
|
|
|
|
|
parser.add_argument('-u', '--url',
|
|
|
|
dest='url',
|
|
|
|
type=str,
|
|
|
|
help='The url on your Zulip server that you want '
|
|
|
|
'to post the fixture to')
|
2016-04-14 23:39:37 +02:00
|
|
|
|
2017-10-09 05:12:55 +02:00
|
|
|
self.add_realm_args(parser, help="Specify which realm/subdomain to connect to; default is zulip")
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, **options: str) -> None:
|
2016-04-14 23:39:37 +02:00
|
|
|
if options['fixture'] is None or options['url'] is None:
|
2016-11-22 01:44:16 +01:00
|
|
|
self.print_help('./manage.py', 'send_webhook_fixture_message')
|
2016-04-14 23:39:37 +02:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
full_fixture_path = os.path.join(settings.DEPLOY_ROOT, options['fixture'])
|
|
|
|
|
|
|
|
if not self._does_fixture_path_exist(full_fixture_path):
|
|
|
|
print('Fixture {} does not exist'.format(options['fixture']))
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
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()
|
2017-10-20 18:02:32 +02:00
|
|
|
result = client.post(options['url'], json, content_type="application/json",
|
|
|
|
HTTP_HOST=realm.host)
|
|
|
|
if result.status_code != 200:
|
|
|
|
print('Error status %s: %s' % (result.status_code, result.content))
|
|
|
|
exit(1)
|
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)
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def _get_fixture_as_json(self, fixture_path: str) -> str:
|
2016-04-14 23:39:37 +02:00
|
|
|
return ujson.dumps(ujson.loads(open(fixture_path).read()))
|