2020-08-22 00:11:59 +02:00
from unittest . mock import patch
2018-11-06 17:07:04 +01:00
from urllib . parse import quote , unquote
2016-11-10 19:30:09 +01:00
from zerver . lib . test_classes import WebhookTestCase
2018-08-01 10:53:40 +02:00
from zerver . lib . users import get_api_key
2016-09-20 22:51:11 +02:00
2020-01-14 22:06:24 +01:00
2016-09-20 22:51:11 +02:00
class JiraHookTests ( WebhookTestCase ) :
2021-02-12 08:20:45 +01:00
STREAM_NAME = " jira "
2020-04-09 21:51:58 +02:00
URL_TEMPLATE = " /api/v1/external/jira?api_key= {api_key} &stream= {stream} "
2021-06-26 09:18:33 +02:00
WEBHOOK_DIR_NAME = " jira "
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_custom_stream ( self ) - > None :
2018-08-01 10:53:40 +02:00
api_key = get_api_key ( self . test_user )
2020-08-23 19:30:12 +02:00
self . subscribe ( self . test_user , " jira_custom " )
2020-06-10 06:41:04 +02:00
url = f " /api/v1/external/jira?api_key= { api_key } &stream=jira_custom "
2020-08-23 19:09:27 +02:00
msg = self . send_webhook_payload (
self . test_user ,
url ,
self . get_body ( " created_v2 " ) ,
content_type = " application/json " ,
)
2020-08-24 14:21:58 +02:00
expected_content = """
2019-05-09 03:40:38 +02:00
Leo Franchi created [ BUG - 15 : New bug with hook ] ( http : / / lfranchi . com : 8080 / browse / BUG - 15 ) :
2016-09-20 22:51:11 +02:00
2019-05-09 03:40:38 +02:00
* * * Priority * * : Major
* * * Assignee * * : no one
""" .strip()
2020-08-24 14:21:58 +02:00
self . assert_stream_message (
message = msg ,
stream_name = " jira_custom " ,
topic_name = " BUG-15: New bug with hook " ,
content = expected_content ,
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_created ( self ) - > None :
2018-11-09 20:33:58 +01:00
expected_topic = " BUG-15: New bug with hook "
2019-05-09 03:40:38 +02:00
expected_message = """
Leo Franchi created [ BUG - 15 : New bug with hook ] ( http : / / lfranchi . com : 8080 / browse / BUG - 15 ) :
2016-09-20 22:51:11 +02:00
2019-05-09 03:40:38 +02:00
* * * Priority * * : Major
* * * Assignee * * : no one
""" .strip()
2020-08-23 15:49:24 +02:00
self . check_webhook ( " created_v1 " , expected_topic , expected_message )
self . check_webhook ( " created_v2 " , expected_topic , expected_message )
2016-09-20 22:51:11 +02:00
2020-08-22 00:11:59 +02:00
def test_ignored_events ( self ) - > None :
ignored_actions = [
" attachment_created " ,
" issuelink_created " ,
" issuelink_deleted " ,
2020-08-22 14:37:45 +02:00
" jira:version_released " ,
2020-08-22 15:15:07 +02:00
" jira:worklog_updated " ,
2020-08-22 00:11:59 +02:00
" sprint_closed " ,
" sprint_started " ,
" worklog_created " ,
" worklog_updated " ,
]
for action in ignored_actions :
url = self . build_webhook_url ( )
payload = dict ( webhookEvent = action )
with patch ( " zerver.webhooks.jira.view.check_send_webhook_message " ) as m :
2021-02-12 08:19:30 +01:00
result = self . client_post ( url , payload , content_type = " application/json " )
2020-08-22 00:11:59 +02:00
self . assertFalse ( m . called )
self . assert_json_success ( result )
2018-11-06 17:07:04 +01:00
def test_created_with_stream_with_spaces_escaped ( self ) - > None :
2021-02-12 08:20:45 +01:00
self . STREAM_NAME = quote ( " jira alerts " )
2018-11-06 17:07:04 +01:00
self . url = self . build_webhook_url ( )
self . subscribe ( self . test_user , unquote ( self . STREAM_NAME ) )
2021-02-12 08:20:45 +01:00
payload = self . get_body ( " created_v1 " )
result = self . client_post ( self . url , payload , content_type = " application/json " )
2018-11-06 17:07:04 +01:00
self . assert_json_success ( result )
2018-11-09 20:33:58 +01:00
expected_topic = " BUG-15: New bug with hook "
2019-05-09 03:40:38 +02:00
expected_message = """
Leo Franchi created [ BUG - 15 : New bug with hook ] ( http : / / lfranchi . com : 8080 / browse / BUG - 15 ) :
2018-11-06 17:07:04 +01:00
2019-05-09 03:40:38 +02:00
* * * Priority * * : Major
* * * Assignee * * : no one
""" .strip()
2018-11-07 01:14:21 +01:00
msg = self . get_last_message ( )
self . assertEqual ( msg . content , expected_message )
2018-11-09 20:33:58 +01:00
self . assertEqual ( msg . topic_name ( ) , expected_topic )
2018-11-07 01:14:21 +01:00
def test_created_with_stream_with_spaces_double_escaped ( self ) - > None :
2021-02-12 08:20:45 +01:00
self . STREAM_NAME = quote ( quote ( " jira alerts " ) )
2018-11-07 01:14:21 +01:00
self . url = self . build_webhook_url ( )
self . subscribe ( self . test_user , unquote ( unquote ( self . STREAM_NAME ) ) )
2021-02-12 08:20:45 +01:00
payload = self . get_body ( " created_v1 " )
result = self . client_post ( self . url , payload , content_type = " application/json " )
2018-11-07 01:14:21 +01:00
self . assert_json_success ( result )
2018-11-09 20:33:58 +01:00
expected_topic = " BUG-15: New bug with hook "
2019-05-09 03:40:38 +02:00
expected_message = """
Leo Franchi created [ BUG - 15 : New bug with hook ] ( http : / / lfranchi . com : 8080 / browse / BUG - 15 ) :
2018-11-07 01:14:21 +01:00
2019-05-09 03:40:38 +02:00
* * * Priority * * : Major
* * * Assignee * * : no one
""" .strip()
2018-11-06 17:07:04 +01:00
msg = self . get_last_message ( )
self . assertEqual ( msg . content , expected_message )
2018-11-09 20:33:58 +01:00
self . assertEqual ( msg . topic_name ( ) , expected_topic )
2018-11-06 17:07:04 +01:00
2018-12-04 01:59:39 +01:00
def test_created_with_topic_with_spaces_double_escaped ( self ) - > None :
2021-02-12 08:20:45 +01:00
self . url = self . build_webhook_url ( topic = quote ( quote ( " alerts test " ) ) )
2018-12-04 01:59:39 +01:00
expected_topic = " alerts test "
2019-05-09 03:40:38 +02:00
expected_message = """
Leo Franchi created [ BUG - 15 : New bug with hook ] ( http : / / lfranchi . com : 8080 / browse / BUG - 15 ) :
2018-12-04 01:59:39 +01:00
2019-05-09 03:40:38 +02:00
* * * Priority * * : Major
* * * Assignee * * : no one
""" .strip()
2020-08-23 15:49:24 +02:00
self . check_webhook ( " created_v1 " , expected_topic , expected_message )
2018-12-04 01:59:39 +01:00
2017-11-04 07:47:46 +01:00
def test_created_with_unicode ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " BUG-15: New bug with à hook "
2019-05-09 03:40:38 +02:00
expected_message = """
Leo Franchià created [ BUG - 15 : New bug with à hook ] ( http : / / lfranchi . com : 8080 / browse / BUG - 15 ) :
2017-03-15 21:23:51 +01:00
2019-05-09 03:40:38 +02:00
* * * Priority * * : Major
* * * Assignee * * : no one
""" .strip()
2020-08-23 15:49:24 +02:00
self . check_webhook ( " created_with_unicode_v1 " , expected_topic , expected_message )
self . check_webhook ( " created_with_unicode_v2 " , expected_topic , expected_message )
2017-03-15 21:23:51 +01:00
2017-11-04 07:47:46 +01:00
def test_created_assignee ( self ) - > None :
2018-11-09 20:33:58 +01:00
expected_topic = " TEST-4: Test Created Assignee "
2019-05-09 03:40:38 +02:00
expected_message = """
Leonardo Franchi [ Administrator ] created [ TEST - 4 : Test Created Assignee ] ( https : / / zulipp . atlassian . net / browse / TEST - 4 ) :
2016-09-20 22:51:11 +02:00
2019-05-09 03:40:38 +02:00
* * * Priority * * : Major
* * * Assignee * * : Leonardo Franchi [ Administrator ]
""" .strip()
2020-08-23 15:49:24 +02:00
self . check_webhook ( " created_assignee_v1 " , expected_topic , expected_message )
self . check_webhook ( " created_assignee_v2 " , expected_topic , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_commented ( self ) - > None :
2018-11-09 20:33:58 +01:00
expected_topic = " BUG-15: New bug with hook "
2019-05-09 03:40:38 +02:00
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 * * ) :
2016-09-20 22:51:11 +02:00
2019-05-09 03:40:38 +02:00
` ` ` quote
Adding a comment . Oh , what a comment it is !
` ` `
""" .strip()
2020-08-23 15:49:24 +02:00
self . check_webhook ( " commented_v1 " , expected_topic , expected_message )
self . check_webhook ( " commented_v2 " , expected_topic , expected_message )
2016-09-20 22:51:11 +02:00
2019-05-23 10:17:17 +02:00
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()
2020-08-23 15:49:24 +02:00
self . check_webhook ( " commented_v2_with_two_full_links " , expected_topic , expected_message )
2019-05-23 10:17:17 +02:00
2017-11-04 07:47:46 +01:00
def test_comment_edited ( self ) - > None :
2019-01-31 14:32:37 +01:00
expected_topic = " BUG-15: New bug with hook "
2019-05-09 03:40:38 +02:00
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 * * ) :
2017-01-03 18:44:13 +01:00
2019-05-09 03:40:38 +02:00
` ` ` quote
Adding a comment . Oh , what a comment it is !
` ` `
""" .strip()
2020-08-23 15:49:24 +02:00
self . check_webhook ( " comment_edited_v2 " , expected_topic , expected_message )
2017-01-03 18:44:13 +01:00
2017-11-04 07:47:46 +01:00
def test_comment_deleted ( self ) - > None :
2018-11-09 20:33:58 +01:00
expected_topic = " TOM-1: New Issue "
2019-05-09 03:40:38 +02:00
expected_message = " Tomasz Kolek deleted a comment from [TOM-1: New Issue](https://zuliptomek.atlassian.net/browse/TOM-1) (assigned to **kolaszek@go2.pl**). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " comment_deleted_v2 " , expected_topic , expected_message )
2017-01-03 18:44:13 +01:00
2017-11-04 07:47:46 +01:00
def test_commented_markup ( self ) - > None :
2018-11-09 20:33:58 +01:00
expected_topic = " TEST-7: Testing of rich text "
2019-05-09 03:40:38 +02:00
expected_message = """ Leonardo Franchi [Administrator] commented on [TEST-7: Testing of rich text](https://zulipp.atlassian.net/browse/TEST-7): \n \n ``` quote \n This is a comment that likes to **exercise** a lot of _different_ `conventions` that `jira uses`. \r \n \r \n ~~~ \n \r \n this code is not highlighted, but monospaced \r \n \n ~~~ \r \n \r \n ~~~ \n \r \n def 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 \n Thanks! \r \n \r \n ~~~ quote \n \r \n Someone said somewhere \r \n \n ~~~ \n ``` """
2020-08-23 15:49:24 +02:00
self . check_webhook ( " commented_markup_v1 " , expected_topic , expected_message )
self . check_webhook ( " commented_markup_v2 " , expected_topic , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_deleted ( self ) - > None :
2018-11-09 20:33:58 +01:00
expected_topic = " BUG-15: New bug with hook "
2019-05-09 03:40:38 +02:00
expected_message = " Leo Franchi deleted [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " deleted_v1 " , expected_topic , expected_message )
self . check_webhook ( " deleted_v2 " , expected_topic , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_reassigned ( self ) - > None :
2018-11-09 20:33:58 +01:00
expected_topic = " BUG-15: New bug with hook "
2019-05-09 03:40:38 +02:00
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**):
2016-09-20 22:51:11 +02:00
2017-02-12 04:22:13 +01:00
* Changed assignee to * * Othello , the Moor of Venice * * """
2020-08-23 15:49:24 +02:00
self . check_webhook ( " reassigned_v1 " , expected_topic , expected_message )
self . check_webhook ( " reassigned_v2 " , expected_topic , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_priority_updated ( self ) - > None :
2018-11-09 20:33:58 +01:00
expected_topic = " TEST-1: Fix That "
2019-05-09 03:40:38 +02:00
expected_message = """ Leonardo Franchi [Administrator] updated [TEST-1: Fix That](https://zulipp.atlassian.net/browse/TEST-1) (assigned to **leo@zulip.com**):
2016-09-20 22:51:11 +02:00
2017-02-12 04:22:13 +01:00
* Changed priority from * * Critical * * to * * Major * * """
2020-08-23 15:49:24 +02:00
self . check_webhook ( " updated_priority_v1 " , expected_topic , expected_message )
self . check_webhook ( " updated_priority_v2 " , expected_topic , expected_message )
2017-01-30 19:26:48 +01:00
2017-11-04 07:47:46 +01:00
def test_status_changed ( self ) - > None :
2019-01-31 14:32:37 +01:00
expected_topic = " TEST-1: Fix That "
2019-05-09 03:40:38 +02:00
expected_message = """ Leonardo Franchi [Administrator] updated [TEST-1: Fix That](https://zulipp.atlassian.net/browse/TEST-1):
2017-01-30 19:26:48 +01:00
2017-02-12 04:22:13 +01:00
* Changed status from * * To Do * * to * * In Progress * * """
2020-08-23 15:49:24 +02:00
self . check_webhook ( " change_status_v1 " , expected_topic , expected_message )
self . check_webhook ( " change_status_v2 " , expected_topic , expected_message )
2016-09-20 22:51:11 +02:00
2019-08-16 17:20:07 +02:00
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 \n Sounds like it’ s pretty important. I’ ll get this fixed ASAP! \n ``` """
2020-08-23 15:49:24 +02:00
self . check_webhook ( " comment_created " , expected_topic , expected_message )
2019-08-16 17:20:07 +02:00
2020-02-26 22:10:25 +01:00
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 \n Sounds like it’ s pretty important. I’ ll get this fixed ASAP! \n ``` """
2020-08-23 15:49:24 +02:00
self . check_webhook ( " comment_created_no_issue_details " , expected_topic , expected_message )
2020-02-26 22:10:25 +01:00
2019-08-16 17:20:07 +02:00
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 \n This is a very important issue! I’ m on it! \n ``` """
2020-08-23 15:49:24 +02:00
self . check_webhook ( " comment_updated " , expected_topic , expected_message )
2019-08-16 17:20:07 +02:00
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 ``` """
2020-08-23 15:49:24 +02:00
self . check_webhook ( " comment_deleted " , expected_topic , expected_message )
2021-09-13 20:23:54 +02:00
def test_anomalous_webhook_payload_error ( self ) - > None :
with self . assertRaises ( AssertionError ) as e :
self . check_webhook (
fixture_name = " example_anomalous_payload " ,
expected_topic = " " ,
expected_message = " " ,
expect_noop = True ,
)
self . assertIn (
" Unable to parse request: Did Jira generate this event? " ,
e . exception . args [ 0 ] ,
)