2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
from zerver.lib.test_classes import WebhookTestCase
|
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2018-02-08 19:46:15 +01:00
|
|
|
class FrontHookTests(WebhookTestCase):
|
2024-05-04 22:02:50 +02:00
|
|
|
CHANNEL_NAME = "front"
|
2018-03-16 22:53:50 +01:00
|
|
|
URL_TEMPLATE = "/api/v1/external/front?&api_key={api_key}&stream={stream}"
|
2021-06-26 09:18:33 +02:00
|
|
|
WEBHOOK_DIR_NAME = "front"
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
# Scenario 1: Conversation starts from an outbound message.
|
|
|
|
|
|
|
|
# Conversation automatically assigned to a teammate who started it.
|
|
|
|
def test_conversation_assigned_outbound(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keo696"
|
2018-02-08 19:46:15 +01:00
|
|
|
expected_message = "**Leela Turanga** assigned themselves."
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"conversation_assigned_outbound",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
def test_outbound_message(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keo696"
|
2021-02-12 08:19:30 +01:00
|
|
|
expected_message = (
|
|
|
|
"[Outbound message](https://app.frontapp.com/open/msg_1176ie2) "
|
|
|
|
"from **support@planet-express.com** "
|
|
|
|
"to **calculon@momsbot.com**:\n"
|
|
|
|
"```quote\n*Subject*: Your next delivery is on Epsilon 96Z\n```"
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"outbound_message",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
def test_conversation_archived(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keo696"
|
2018-02-08 19:46:15 +01:00
|
|
|
expected_message = "Archived by **Leela Turanga**."
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"conversation_archived",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
def test_conversation_reopened(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keo696"
|
2018-02-08 19:46:15 +01:00
|
|
|
expected_message = "Reopened by **Leela Turanga**."
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"conversation_reopened",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
def test_conversation_deleted(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keo696"
|
2018-02-08 19:46:15 +01:00
|
|
|
expected_message = "Deleted by **Leela Turanga**."
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"conversation_deleted",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
def test_conversation_restored(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keo696"
|
2018-02-08 19:46:15 +01:00
|
|
|
expected_message = "Restored by **Leela Turanga**."
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"conversation_restored",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
def test_conversation_unassigned(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keo696"
|
2019-05-09 02:23:38 +02:00
|
|
|
expected_message = "Unassigned by **Leela Turanga**."
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"conversation_unassigned",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
def test_mention_all(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keo696"
|
2021-02-12 08:19:30 +01:00
|
|
|
expected_message = (
|
|
|
|
"**Leela Turanga** left a comment:\n"
|
|
|
|
"```quote\n@all Could someone else take this?\n```"
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"mention_all",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
# Scenario 2: Conversation starts from an inbound message.
|
|
|
|
|
|
|
|
def test_inbound_message(self) -> None:
|
2024-11-19 07:29:51 +01:00
|
|
|
expected_topic_name = "2x9c5v"
|
2021-02-12 08:19:30 +01:00
|
|
|
expected_message = (
|
|
|
|
"[Inbound message](https://app.frontapp.com/open/msg_1176r8y) "
|
|
|
|
"from **calculon@momsbot.com** "
|
|
|
|
"to **support@planet-express.com**:\n"
|
2024-11-19 07:29:51 +01:00
|
|
|
"```quote\n*Subject*: Cancellation Request for Subscription #SUB-67890\n```"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"inbound_message",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
def test_conversation_tagged(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keocka"
|
2018-02-08 19:46:15 +01:00
|
|
|
expected_message = "**Leela Turanga** added tag **Urgent**."
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"conversation_tagged",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
# Conversation automatically assigned to a teammate who replied to it.
|
|
|
|
def test_conversation_assigned_reply(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keocka"
|
2018-02-08 19:46:15 +01:00
|
|
|
expected_message = "**Leela Turanga** assigned themselves."
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"conversation_assigned_reply",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
def test_outbound_reply(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keocka"
|
2021-02-12 08:19:30 +01:00
|
|
|
expected_message = (
|
|
|
|
"[Outbound reply](https://app.frontapp.com/open/msg_1176ryy) "
|
|
|
|
"from **support@planet-express.com** "
|
|
|
|
"to **calculon@momsbot.com**."
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"outbound_reply",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
def test_conversation_untagged(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keocka"
|
2018-02-08 19:46:15 +01:00
|
|
|
expected_message = "**Leela Turanga** removed tag **Urgent**."
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"conversation_untagged",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
def test_mention(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keocka"
|
2021-02-12 08:19:30 +01:00
|
|
|
expected_message = (
|
|
|
|
"**Leela Turanga** left a comment:\n"
|
|
|
|
"```quote\n@bender Could you take it from here?\n```"
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"mention",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
def test_comment(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keocka"
|
2021-02-12 03:52:14 +01:00
|
|
|
expected_message = "**Bender Rodriguez** left a comment:\n```quote\nSure.\n```"
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"comment",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
# Conversation manually assigned to another teammate.
|
|
|
|
def test_conversation_assigned(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keocka"
|
2018-02-08 19:46:15 +01:00
|
|
|
expected_message = "**Leela Turanga** assigned **Bender Rodriguez**."
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"conversation_assigned",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
2023-05-11 17:38:16 +02:00
|
|
|
def test_conversation_assigned_by_rule(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keocka"
|
2023-05-11 17:38:16 +02:00
|
|
|
expected_message = "**'Important deliveries' rule** assigned **Bender Rodriguez**."
|
|
|
|
|
|
|
|
self.check_webhook(
|
|
|
|
"rule",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2023-05-11 17:38:16 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_conversation_assigned_by_gmail(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "cnv_keo696"
|
2023-05-11 17:38:16 +02:00
|
|
|
expected_message = "Archived by **(gmail)**."
|
|
|
|
|
|
|
|
self.check_webhook(
|
|
|
|
"gmail",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2023-05-11 17:38:16 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
|
|
|
|
2018-02-08 19:46:15 +01:00
|
|
|
def test_unknown_webhook_request(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
payload = self.get_body("conversation_assigned")
|
2020-08-07 01:09:47 +02:00
|
|
|
payload_json = orjson.loads(payload)
|
2021-02-12 08:20:45 +01:00
|
|
|
payload_json["type"] = "qwerty"
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
|
|
|
self.url, orjson.dumps(payload_json), content_type="application/x-www-form-urlencoded"
|
|
|
|
)
|
2018-02-08 19:46:15 +01:00
|
|
|
|
|
|
|
self.assert_json_error(result, "Unknown webhook request")
|