mirror of https://github.com/zulip/zulip.git
webhooks/front: Eliminate unnecessary error handling.
This commit is contained in:
parent
f97cff54aa
commit
428e5057f5
|
@ -8,69 +8,6 @@ class FrontHookTests(WebhookTestCase):
|
|||
URL_TEMPLATE = "/api/v1/external/front?&api_key={api_key}&stream={stream}"
|
||||
FIXTURE_DIR_NAME = 'front'
|
||||
|
||||
def _test_no_message_data(self, fixture_name: Text) -> None:
|
||||
payload = self.get_body(fixture_name)
|
||||
payload_json = ujson.loads(payload)
|
||||
del payload_json['conversation']['subject']
|
||||
result = self.client_post(self.url, ujson.dumps(payload_json),
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
self.assert_json_error(result, "Missing required data")
|
||||
|
||||
def _test_no_source_name(self, fixture_name: Text) -> None:
|
||||
payload = self.get_body(fixture_name)
|
||||
payload_json = ujson.loads(payload)
|
||||
del payload_json['source']['data']['first_name']
|
||||
result = self.client_post(self.url, ujson.dumps(payload_json),
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
self.assert_json_error(result, "Missing required data")
|
||||
|
||||
def _test_no_target_name(self, fixture_name: Text) -> None:
|
||||
payload = self.get_body(fixture_name)
|
||||
payload_json = ujson.loads(payload)
|
||||
del payload_json['target']['data']['first_name']
|
||||
result = self.client_post(self.url, ujson.dumps(payload_json),
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
self.assert_json_error(result, "Missing required data")
|
||||
|
||||
def _test_no_comment(self, fixture_name: Text) -> None:
|
||||
payload = self.get_body(fixture_name)
|
||||
payload_json = ujson.loads(payload)
|
||||
del payload_json['target']['data']['body']
|
||||
result = self.client_post(self.url, ujson.dumps(payload_json),
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
self.assert_json_error(result, "Missing required data")
|
||||
|
||||
def _test_no_tag(self, fixture_name: Text) -> None:
|
||||
payload = self.get_body(fixture_name)
|
||||
payload_json = ujson.loads(payload)
|
||||
del payload_json['target']['data']['name']
|
||||
result = self.client_post(self.url, ujson.dumps(payload_json),
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
self.assert_json_error(result, "Missing required data")
|
||||
|
||||
def test_no_event_type(self) -> None:
|
||||
payload = self.get_body('1_conversation_assigned_outbound')
|
||||
payload_json = ujson.loads(payload)
|
||||
del payload_json['type']
|
||||
result = self.client_post(self.url, ujson.dumps(payload_json),
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
self.assert_json_error(result, "Missing required data")
|
||||
|
||||
def test_no_conversation_id(self) -> None:
|
||||
payload = self.get_body('1_conversation_assigned_outbound')
|
||||
payload_json = ujson.loads(payload)
|
||||
del payload_json['conversation']['id']
|
||||
result = self.client_post(self.url, ujson.dumps(payload_json),
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
self.assert_json_error(result, "Missing required data")
|
||||
|
||||
# Scenario 1: Conversation starts from an outbound message.
|
||||
|
||||
# Conversation automatically assigned to a teammate who started it.
|
||||
|
@ -95,9 +32,6 @@ class FrontHookTests(WebhookTestCase):
|
|||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_outbound_message_error(self) -> None:
|
||||
self._test_no_message_data('2_outbound_message')
|
||||
|
||||
def test_conversation_archived(self) -> None:
|
||||
expected_subject = 'cnv_keo696'
|
||||
expected_message = "Archived by **Leela Turanga**."
|
||||
|
@ -107,9 +41,6 @@ class FrontHookTests(WebhookTestCase):
|
|||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_conversation_archived_error(self) -> None:
|
||||
self._test_no_source_name('3_conversation_archived')
|
||||
|
||||
def test_conversation_reopened(self) -> None:
|
||||
expected_subject = 'cnv_keo696'
|
||||
expected_message = "Reopened by **Leela Turanga**."
|
||||
|
@ -119,9 +50,6 @@ class FrontHookTests(WebhookTestCase):
|
|||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_conversation_reopened_error(self) -> None:
|
||||
self._test_no_source_name('4_conversation_reopened')
|
||||
|
||||
def test_conversation_deleted(self) -> None:
|
||||
expected_subject = 'cnv_keo696'
|
||||
expected_message = "Deleted by **Leela Turanga**."
|
||||
|
@ -131,9 +59,6 @@ class FrontHookTests(WebhookTestCase):
|
|||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_conversation_deleted_error(self) -> None:
|
||||
self._test_no_source_name('5_conversation_deleted')
|
||||
|
||||
def test_conversation_restored(self) -> None:
|
||||
expected_subject = 'cnv_keo696'
|
||||
expected_message = "Restored by **Leela Turanga**."
|
||||
|
@ -143,9 +68,6 @@ class FrontHookTests(WebhookTestCase):
|
|||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_conversation_restored_error(self) -> None:
|
||||
self._test_no_source_name('6_conversation_restored')
|
||||
|
||||
def test_conversation_unassigned(self) -> None:
|
||||
expected_subject = 'cnv_keo696'
|
||||
expected_message = "Unassined by **Leela Turanga**."
|
||||
|
@ -155,9 +77,6 @@ class FrontHookTests(WebhookTestCase):
|
|||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_conversation_unassigned_error(self) -> None:
|
||||
self._test_no_source_name('7_conversation_unassigned')
|
||||
|
||||
def test_mention_all(self) -> None:
|
||||
expected_subject = 'cnv_keo696'
|
||||
expected_message = "**Leela Turanga** left a comment:\n" \
|
||||
|
@ -182,9 +101,6 @@ class FrontHookTests(WebhookTestCase):
|
|||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_inbound_message_error(self) -> None:
|
||||
self._test_no_message_data('9_inbound_message')
|
||||
|
||||
def test_conversation_tagged(self) -> None:
|
||||
expected_subject = 'cnv_keocka'
|
||||
expected_message = "**Leela Turanga** added tag **Urgent**."
|
||||
|
@ -194,9 +110,6 @@ class FrontHookTests(WebhookTestCase):
|
|||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_conversation_tagged_error(self) -> None:
|
||||
self._test_no_tag('10_conversation_tagged')
|
||||
|
||||
# Conversation automatically assigned to a teammate who replied to it.
|
||||
def test_conversation_assigned_reply(self) -> None:
|
||||
expected_subject = 'cnv_keocka'
|
||||
|
@ -218,9 +131,6 @@ class FrontHookTests(WebhookTestCase):
|
|||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_outbound_reply_error(self) -> None:
|
||||
self._test_no_message_data('12_outbound_reply')
|
||||
|
||||
def test_conversation_untagged(self) -> None:
|
||||
expected_subject = 'cnv_keocka'
|
||||
expected_message = "**Leela Turanga** removed tag **Urgent**."
|
||||
|
@ -230,9 +140,6 @@ class FrontHookTests(WebhookTestCase):
|
|||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_conversation_untagged_error(self) -> None:
|
||||
self._test_no_tag('13_conversation_untagged')
|
||||
|
||||
def test_mention(self) -> None:
|
||||
expected_subject = 'cnv_keocka'
|
||||
expected_message = "**Leela Turanga** left a comment:\n" \
|
||||
|
@ -253,9 +160,6 @@ class FrontHookTests(WebhookTestCase):
|
|||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_comment_error(self) -> None:
|
||||
self._test_no_comment('15_comment')
|
||||
|
||||
# Conversation manually assigned to another teammate.
|
||||
def test_conversation_assigned(self) -> None:
|
||||
expected_subject = 'cnv_keocka'
|
||||
|
@ -266,9 +170,6 @@ class FrontHookTests(WebhookTestCase):
|
|||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_conversation_assigned_error(self) -> None:
|
||||
self._test_no_target_name('16_conversation_assigned')
|
||||
|
||||
def test_unknown_webhook_request(self) -> None:
|
||||
payload = self.get_body('16_conversation_assigned')
|
||||
payload_json = ujson.loads(payload)
|
||||
|
|
|
@ -9,102 +9,58 @@ from zerver.lib.response import json_error, json_success
|
|||
from zerver.lib.webhooks.common import check_send_webhook_message
|
||||
from zerver.models import UserProfile
|
||||
|
||||
def get_message_data(payload: Dict[Text, Any]) -> Optional[Tuple[Text, Text, Text, Text]]:
|
||||
try:
|
||||
link = "https://app.frontapp.com/open/" + payload['target']['data']['id']
|
||||
outbox = payload['conversation']['recipient']['handle']
|
||||
inbox = payload['source']['data'][0]['address']
|
||||
subject = payload['conversation']['subject']
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
def get_message_data(payload: Dict[Text, Any]) -> Tuple[Text, Text, Text, Text]:
|
||||
link = "https://app.frontapp.com/open/" + payload['target']['data']['id']
|
||||
outbox = payload['conversation']['recipient']['handle']
|
||||
inbox = payload['source']['data'][0]['address']
|
||||
subject = payload['conversation']['subject']
|
||||
return link, outbox, inbox, subject
|
||||
|
||||
def get_source_name(payload: Dict[Text, Any]) -> Optional[Text]:
|
||||
try:
|
||||
first_name = payload['source']['data']['first_name']
|
||||
last_name = payload['source']['data']['last_name']
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
def get_source_name(payload: Dict[Text, Any]) -> Text:
|
||||
first_name = payload['source']['data']['first_name']
|
||||
last_name = payload['source']['data']['last_name']
|
||||
return "%s %s" % (first_name, last_name)
|
||||
|
||||
def get_target_name(payload: Dict[Text, Any]) -> Optional[Text]:
|
||||
try:
|
||||
first_name = payload['target']['data']['first_name']
|
||||
last_name = payload['target']['data']['last_name']
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
def get_target_name(payload: Dict[Text, Any]) -> Text:
|
||||
first_name = payload['target']['data']['first_name']
|
||||
last_name = payload['target']['data']['last_name']
|
||||
return "%s %s" % (first_name, last_name)
|
||||
|
||||
def get_comment(payload: Dict[Text, Any]) -> Optional[Text]:
|
||||
try:
|
||||
comment = payload['target']['data']['body']
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
return comment
|
||||
|
||||
def get_tag(payload: Dict[Text, Any]) -> Optional[Text]:
|
||||
try:
|
||||
tag = payload['target']['data']['name']
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
return tag
|
||||
|
||||
@api_key_only_webhook_view('Front')
|
||||
@has_request_variables
|
||||
def api_front_webhook(request: HttpRequest, user_profile: UserProfile,
|
||||
payload: Dict[Text, Any]=REQ(argument_type='body')) -> HttpResponse:
|
||||
try:
|
||||
event_type = payload['type']
|
||||
conversation_id = payload['conversation']['id']
|
||||
except KeyError:
|
||||
return json_error(_("Missing required data"))
|
||||
|
||||
event_type = payload['type']
|
||||
conversation_id = payload['conversation']['id']
|
||||
|
||||
# Each topic corresponds to a separate conversation in Front.
|
||||
topic = conversation_id
|
||||
|
||||
# Inbound message
|
||||
if event_type == 'inbound':
|
||||
message_data = get_message_data(payload)
|
||||
if not message_data:
|
||||
return json_error(_("Missing required data"))
|
||||
|
||||
link, outbox, inbox, subject = message_data
|
||||
link, outbox, inbox, subject = get_message_data(payload)
|
||||
body = "[Inbound message]({link}) from **{outbox}** to **{inbox}**.\n" \
|
||||
"```quote\n*Subject*: {subject}\n```" \
|
||||
.format(link=link, outbox=outbox, inbox=inbox, subject=subject)
|
||||
|
||||
# Outbound message
|
||||
elif event_type == 'outbound':
|
||||
message_data = get_message_data(payload)
|
||||
if not message_data:
|
||||
return json_error(_("Missing required data"))
|
||||
|
||||
link, outbox, inbox, subject = message_data
|
||||
link, outbox, inbox, subject = get_message_data(payload)
|
||||
body = "[Outbound message]({link}) from **{inbox}** to **{outbox}**.\n" \
|
||||
"```quote\n*Subject*: {subject}\n```" \
|
||||
.format(link=link, inbox=inbox, outbox=outbox, subject=subject)
|
||||
|
||||
# Outbound reply
|
||||
elif event_type == 'out_reply':
|
||||
message_data = get_message_data(payload)
|
||||
if not message_data:
|
||||
return json_error(_("Missing required data"))
|
||||
|
||||
link, outbox, inbox, subject = message_data
|
||||
link, outbox, inbox, subject = get_message_data(payload)
|
||||
body = "[Outbound reply]({link}) from **{inbox}** to **{outbox}**." \
|
||||
.format(link=link, inbox=inbox, outbox=outbox)
|
||||
|
||||
# Comment or mention
|
||||
elif event_type == 'comment' or event_type == 'mention':
|
||||
name, comment = get_source_name(payload), get_comment(payload)
|
||||
if not (name and comment):
|
||||
return json_error(_("Missing required data"))
|
||||
|
||||
name = get_source_name(payload)
|
||||
comment = payload['target']['data']['body']
|
||||
body = "**{name}** left a comment:\n```quote\n{comment}\n```" \
|
||||
.format(name=name, comment=comment)
|
||||
|
||||
|
@ -113,9 +69,6 @@ def api_front_webhook(request: HttpRequest, user_profile: UserProfile,
|
|||
source_name = get_source_name(payload)
|
||||
target_name = get_target_name(payload)
|
||||
|
||||
if not (source_name and target_name):
|
||||
return json_error(_("Missing required data"))
|
||||
|
||||
if source_name == target_name:
|
||||
body = "**{source_name}** assigned themselves." \
|
||||
.format(source_name=source_name)
|
||||
|
@ -126,57 +79,38 @@ def api_front_webhook(request: HttpRequest, user_profile: UserProfile,
|
|||
# Conversation unassigned
|
||||
elif event_type == 'unassign':
|
||||
name = get_source_name(payload)
|
||||
if not name:
|
||||
return json_error(_("Missing required data"))
|
||||
|
||||
body = "Unassined by **{name}**.".format(name=name)
|
||||
|
||||
# Conversation archived
|
||||
elif event_type == 'archive':
|
||||
name = get_source_name(payload)
|
||||
if not name:
|
||||
return json_error(_("Missing required data"))
|
||||
|
||||
body = "Archived by **{name}**.".format(name=name)
|
||||
|
||||
# Conversation reopened
|
||||
elif event_type == 'reopen':
|
||||
name = get_source_name(payload)
|
||||
if not name:
|
||||
return json_error(_("Missing required data"))
|
||||
|
||||
body = "Reopened by **{name}**.".format(name=name)
|
||||
|
||||
# Conversation deleted
|
||||
elif event_type == 'trash':
|
||||
name = get_source_name(payload)
|
||||
if not name:
|
||||
return json_error(_("Missing required data"))
|
||||
|
||||
body = "Deleted by **{name}**.".format(name=name)
|
||||
|
||||
# Conversation restored
|
||||
elif event_type == 'restore':
|
||||
name = get_source_name(payload)
|
||||
if not name:
|
||||
return json_error(_("Missing required data"))
|
||||
|
||||
body = "Restored by **{name}**.".format(name=name)
|
||||
|
||||
# Conversation tagged
|
||||
elif event_type == 'tag':
|
||||
name, tag = get_source_name(payload), get_tag(payload)
|
||||
if not (name and tag):
|
||||
return json_error(_("Missing required data"))
|
||||
|
||||
name = get_source_name(payload)
|
||||
tag = payload['target']['data']['name']
|
||||
body = "**{name}** added tag **{tag}**.".format(name=name, tag=tag)
|
||||
|
||||
# Conversation untagged
|
||||
elif event_type == 'untag':
|
||||
name, tag = get_source_name(payload), get_tag(payload)
|
||||
if not (name and tag):
|
||||
return json_error(_("Missing required data"))
|
||||
|
||||
name = get_source_name(payload)
|
||||
tag = payload['target']['data']['name']
|
||||
body = "**{name}** removed tag **{tag}**.".format(name=name, tag=tag)
|
||||
else:
|
||||
return json_error(_("Unknown webhook request"))
|
||||
|
|
Loading…
Reference in New Issue