mirror of
https://github.com/zulip/zulip.git
synced 2025-11-06 06:53:25 +00:00
Having an optional stream_name parameter makes it confusing to read the code if you know your webhook is sending private messages. And then the other two callers are already checking topics, so they might as well check stream names, too. We also have the two stream-oriented callers make their own call to "subscribe". And we future-proof this by making sure the exception for no-message-being-sent calls out that gotcha. Somewhat in passing, we now assert that self.STREAM_NAME is not None in the main helper. This is partly to satisfy mypy, but it's also a good sanity check. This also sets the stage for the next commit, where I'll add an assert_stream_message helper.
216 lines
11 KiB
Python
216 lines
11 KiB
Python
from urllib.parse import quote, unquote
|
||
|
||
from zerver.lib.test_classes import WebhookTestCase
|
||
from zerver.lib.users import get_api_key
|
||
from zerver.models import get_display_recipient
|
||
|
||
|
||
class JiraHookTests(WebhookTestCase):
|
||
STREAM_NAME = 'jira'
|
||
URL_TEMPLATE = "/api/v1/external/jira?api_key={api_key}&stream={stream}"
|
||
|
||
def test_custom_stream(self) -> None:
|
||
api_key = get_api_key(self.test_user)
|
||
self.subscribe(self.test_user, "jira_custom")
|
||
url = f"/api/v1/external/jira?api_key={api_key}&stream=jira_custom"
|
||
msg = self.send_webhook_payload(
|
||
self.test_user,
|
||
url,
|
||
self.get_body("created_v2"),
|
||
content_type="application/json",
|
||
)
|
||
self.assertEqual(get_display_recipient(msg.recipient), "jira_custom")
|
||
self.assertEqual(msg.topic_name(), "BUG-15: New bug with hook")
|
||
expected_message = """
|
||
Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):
|
||
|
||
* **Priority**: Major
|
||
* **Assignee**: no one
|
||
""".strip()
|
||
self.assertEqual(msg.content, expected_message)
|
||
|
||
def test_created(self) -> None:
|
||
expected_topic = "BUG-15: New bug with hook"
|
||
expected_message = """
|
||
Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):
|
||
|
||
* **Priority**: Major
|
||
* **Assignee**: no one
|
||
""".strip()
|
||
self.check_webhook("created_v1", expected_topic, expected_message)
|
||
self.check_webhook("created_v2", expected_topic, expected_message)
|
||
|
||
def test_created_with_stream_with_spaces_escaped(self) -> None:
|
||
self.STREAM_NAME = quote('jira alerts')
|
||
self.url = self.build_webhook_url()
|
||
self.subscribe(self.test_user, unquote(self.STREAM_NAME))
|
||
|
||
payload = self.get_body('created_v1')
|
||
result = self.client_post(self.url, payload, content_type='application/json')
|
||
|
||
self.assert_json_success(result)
|
||
|
||
expected_topic = "BUG-15: New bug with hook"
|
||
expected_message = """
|
||
Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):
|
||
|
||
* **Priority**: Major
|
||
* **Assignee**: no one
|
||
""".strip()
|
||
msg = self.get_last_message()
|
||
self.assertEqual(msg.content, expected_message)
|
||
self.assertEqual(msg.topic_name(), expected_topic)
|
||
|
||
def test_created_with_stream_with_spaces_double_escaped(self) -> None:
|
||
self.STREAM_NAME = quote(quote('jira alerts'))
|
||
self.url = self.build_webhook_url()
|
||
self.subscribe(self.test_user, unquote(unquote(self.STREAM_NAME)))
|
||
|
||
payload = self.get_body('created_v1')
|
||
result = self.client_post(self.url, payload, content_type='application/json')
|
||
|
||
self.assert_json_success(result)
|
||
|
||
expected_topic = "BUG-15: New bug with hook"
|
||
expected_message = """
|
||
Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):
|
||
|
||
* **Priority**: Major
|
||
* **Assignee**: no one
|
||
""".strip()
|
||
msg = self.get_last_message()
|
||
self.assertEqual(msg.content, expected_message)
|
||
self.assertEqual(msg.topic_name(), expected_topic)
|
||
|
||
def test_created_with_topic_with_spaces_double_escaped(self) -> None:
|
||
self.url = self.build_webhook_url(topic=quote(quote('alerts test')))
|
||
expected_topic = "alerts test"
|
||
expected_message = """
|
||
Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):
|
||
|
||
* **Priority**: Major
|
||
* **Assignee**: no one
|
||
""".strip()
|
||
self.check_webhook("created_v1", expected_topic, expected_message)
|
||
|
||
def test_created_with_unicode(self) -> None:
|
||
expected_topic = "BUG-15: New bug with à hook"
|
||
expected_message = """
|
||
Leo Franchià created [BUG-15: New bug with à hook](http://lfranchi.com:8080/browse/BUG-15):
|
||
|
||
* **Priority**: Major
|
||
* **Assignee**: no one
|
||
""".strip()
|
||
self.check_webhook("created_with_unicode_v1", expected_topic, expected_message)
|
||
self.check_webhook("created_with_unicode_v2", expected_topic, expected_message)
|
||
|
||
def test_created_assignee(self) -> None:
|
||
expected_topic = "TEST-4: Test Created Assignee"
|
||
expected_message = """
|
||
Leonardo Franchi [Administrator] created [TEST-4: Test Created Assignee](https://zulipp.atlassian.net/browse/TEST-4):
|
||
|
||
* **Priority**: Major
|
||
* **Assignee**: Leonardo Franchi [Administrator]
|
||
""".strip()
|
||
self.check_webhook("created_assignee_v1", expected_topic, expected_message)
|
||
self.check_webhook("created_assignee_v2", expected_topic, expected_message)
|
||
|
||
def test_commented(self) -> None:
|
||
expected_topic = "BUG-15: New bug with hook"
|
||
expected_message = """
|
||
Leo Franchi commented on [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15) (assigned to **Othello, the Moor of Venice**):
|
||
|
||
``` quote
|
||
Adding a comment. Oh, what a comment it is!
|
||
```
|
||
""".strip()
|
||
self.check_webhook("commented_v1", expected_topic, expected_message)
|
||
self.check_webhook("commented_v2", expected_topic, expected_message)
|
||
|
||
def test_commented_with_two_full_links(self) -> None:
|
||
expected_topic = "BUG-15: New bug with hook"
|
||
expected_message = """
|
||
Leo Franchi commented on [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15) (assigned to **Othello, the Moor of Venice**):
|
||
|
||
``` quote
|
||
This is the [first link](https://google.com) and this is the [second link](https://google.com) and this is the end.
|
||
```
|
||
""".strip()
|
||
self.check_webhook("commented_v2_with_two_full_links", expected_topic, expected_message)
|
||
|
||
def test_comment_edited(self) -> None:
|
||
expected_topic = "BUG-15: New bug with hook"
|
||
expected_message = """
|
||
Leo Franchi edited a comment on [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15) (assigned to **Othello, the Moor of Venice**):
|
||
|
||
``` quote
|
||
Adding a comment. Oh, what a comment it is!
|
||
```
|
||
""".strip()
|
||
self.check_webhook("comment_edited_v2", expected_topic, expected_message)
|
||
|
||
def test_comment_deleted(self) -> None:
|
||
expected_topic = "TOM-1: New Issue"
|
||
expected_message = "Tomasz Kolek deleted a comment from [TOM-1: New Issue](https://zuliptomek.atlassian.net/browse/TOM-1) (assigned to **kolaszek@go2.pl**)."
|
||
self.check_webhook("comment_deleted_v2", expected_topic, expected_message)
|
||
|
||
def test_commented_markup(self) -> None:
|
||
expected_topic = "TEST-7: Testing of rich text"
|
||
expected_message = """Leonardo Franchi [Administrator] commented on [TEST-7: Testing of rich text](https://zulipp.atlassian.net/browse/TEST-7):\n\n``` quote\nThis is a comment that likes to **exercise** a lot of _different_ `conventions` that `jira uses`.\r\n\r\n~~~\n\r\nthis code is not highlighted, but monospaced\r\n\n~~~\r\n\r\n~~~\n\r\ndef python():\r\n print "likes to be formatted"\r\n\n~~~\r\n\r\n[http://www.google.com](http://www.google.com) is a bare link, and [Google](http://www.google.com) is given a title.\r\n\r\nThanks!\r\n\r\n~~~ quote\n\r\nSomeone said somewhere\r\n\n~~~\n```"""
|
||
|
||
self.check_webhook("commented_markup_v1", expected_topic, expected_message)
|
||
self.check_webhook("commented_markup_v2", expected_topic, expected_message)
|
||
|
||
def test_deleted(self) -> None:
|
||
expected_topic = "BUG-15: New bug with hook"
|
||
expected_message = "Leo Franchi deleted [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15)."
|
||
self.check_webhook("deleted_v1", expected_topic, expected_message)
|
||
self.check_webhook("deleted_v2", expected_topic, expected_message)
|
||
|
||
def test_reassigned(self) -> None:
|
||
expected_topic = "BUG-15: New bug with hook"
|
||
expected_message = """Leo Franchi updated [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15) (assigned to **Othello, the Moor of Venice**):
|
||
|
||
* Changed assignee to **Othello, the Moor of Venice**"""
|
||
self.check_webhook("reassigned_v1", expected_topic, expected_message)
|
||
self.check_webhook("reassigned_v2", expected_topic, expected_message)
|
||
|
||
def test_priority_updated(self) -> None:
|
||
expected_topic = "TEST-1: Fix That"
|
||
expected_message = """Leonardo Franchi [Administrator] updated [TEST-1: Fix That](https://zulipp.atlassian.net/browse/TEST-1) (assigned to **leo@zulip.com**):
|
||
|
||
* Changed priority from **Critical** to **Major**"""
|
||
self.check_webhook("updated_priority_v1", expected_topic, expected_message)
|
||
self.check_webhook("updated_priority_v2", expected_topic, expected_message)
|
||
|
||
def test_status_changed(self) -> None:
|
||
expected_topic = "TEST-1: Fix That"
|
||
expected_message = """Leonardo Franchi [Administrator] updated [TEST-1: Fix That](https://zulipp.atlassian.net/browse/TEST-1):
|
||
|
||
* Changed status from **To Do** to **In Progress**"""
|
||
self.check_webhook("change_status_v1", expected_topic, expected_message)
|
||
self.check_webhook("change_status_v2", expected_topic, expected_message)
|
||
|
||
def get_body(self, fixture_name: str) -> str:
|
||
return self.webhook_fixture_data('jira', fixture_name)
|
||
|
||
def test_comment_event_comment_created(self) -> None:
|
||
expected_topic = "SP-1: Add support for newer format Jira issue comment events"
|
||
expected_message = """Hemanth V. Alluri commented on issue: *"Add support for newer format Jira issue comment events"*\n``` quote\nSounds like it’s pretty important. I’ll get this fixed ASAP!\n```"""
|
||
self.check_webhook("comment_created", expected_topic, expected_message)
|
||
|
||
def test_comment_event_comment_created_no_issue_details(self) -> None:
|
||
expected_topic = "10000: Upgrade Jira to get the issue title here."
|
||
expected_message = """Hemanth V. Alluri commented on issue: *"Upgrade Jira to get the issue title here."*\n``` quote\nSounds like it’s pretty important. I’ll get this fixed ASAP!\n```"""
|
||
self.check_webhook("comment_created_no_issue_details", expected_topic, expected_message)
|
||
|
||
def test_comment_event_comment_edited(self) -> None:
|
||
expected_topic = "SP-1: Add support for newer format Jira issue comment events"
|
||
expected_message = """Hemanth V. Alluri updated their comment on issue: *"Add support for newer format Jira issue comment events"*\n``` quote\nThis is a very important issue! I’m on it!\n```"""
|
||
self.check_webhook("comment_updated", expected_topic, expected_message)
|
||
|
||
def test_comment_event_comment_deleted(self) -> None:
|
||
expected_topic = "SP-1: Add support for newer format Jira issue comment events"
|
||
expected_message = """Hemanth V. Alluri deleted their comment on issue: *"Add support for newer format Jira issue comment events"*\n``` quote\n~~This is a very important issue! I’m on it!~~\n```"""
|
||
self.check_webhook("comment_deleted", expected_topic, expected_message)
|