2020-05-26 07:16:25 +02:00
from unittest . mock import MagicMock , patch
2017-01-28 18:42:59 +01:00
2017-11-16 00:43:10 +01:00
from zerver . lib . test_classes import WebhookTestCase
from zerver . lib . webhooks . git import COMMITS_LIMIT
2017-01-28 18:42:59 +01:00
2020-01-14 22:06:24 +01:00
2017-01-28 18:42:59 +01:00
class GogsHookTests ( WebhookTestCase ) :
STREAM_NAME = ' commits '
2018-03-16 22:53:50 +01:00
URL_TEMPLATE = " /api/v1/external/gogs?&api_key= {api_key} &stream= {stream} "
2017-01-28 18:42:59 +01:00
FIXTURE_DIR_NAME = ' gogs '
2017-11-04 07:47:46 +01:00
def test_push ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " try-git / master "
expected_message = """ john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 1 commit to branch master. Commits by John (1).
2017-01-28 18:42:59 +01:00
* Webhook Test ( [ d8fce16 ] ( http : / / localhost : 3000 / john / try - git / commit / d8fce16c72a2ff56a5afc8a08645a6ce45491794 ) ) """
2019-07-04 20:48:51 +02:00
self . send_and_test_stream_message ( ' push ' , expected_topic , expected_message )
2017-01-28 18:42:59 +01:00
2017-11-04 07:47:46 +01:00
def test_push_multiple_committers ( self ) - > None :
2020-04-09 21:51:58 +02:00
commit_info = ' * Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794)) \n '
expected_topic = " try-git / master "
2020-06-09 00:25:09 +02:00
expected_message = f """ john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 2 commits to branch master. Commits by Benjamin (1) and John (1). \n \n { commit_info } * Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794)) """
2019-07-04 20:48:51 +02:00
self . send_and_test_stream_message ( ' push__commits_multiple_committers ' , expected_topic , expected_message )
2017-04-05 09:12:19 +02:00
2017-11-04 07:47:46 +01:00
def test_push_multiple_committers_filtered_by_branches ( self ) - > None :
2017-04-06 23:26:29 +02:00
self . url = self . build_webhook_url ( branches = ' master,development ' )
2020-04-09 21:51:58 +02:00
commit_info = ' * Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794)) \n '
expected_topic = " try-git / master "
2020-06-09 00:25:09 +02:00
expected_message = f """ john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 2 commits to branch master. Commits by Benjamin (1) and John (1). \n \n { commit_info } * Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794)) """
2019-07-04 20:48:51 +02:00
self . send_and_test_stream_message ( ' push__commits_multiple_committers ' , expected_topic , expected_message )
2017-04-06 23:26:29 +02:00
2017-11-04 07:47:46 +01:00
def test_push_filtered_by_branches ( self ) - > None :
2017-04-06 23:26:29 +02:00
self . url = self . build_webhook_url ( branches = ' master,development ' )
2020-04-09 21:51:58 +02:00
expected_topic = " try-git / master "
expected_message = """ john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 1 commit to branch master. Commits by John (1).
2017-04-06 23:26:29 +02:00
* Webhook Test ( [ d8fce16 ] ( http : / / localhost : 3000 / john / try - git / commit / d8fce16c72a2ff56a5afc8a08645a6ce45491794 ) ) """
2019-07-04 20:48:51 +02:00
self . send_and_test_stream_message ( ' push ' , expected_topic , expected_message )
2017-04-06 23:26:29 +02:00
2017-11-04 07:47:46 +01:00
def test_push_commits_more_than_limits ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " try-git / master "
2017-01-28 18:42:59 +01:00
commits_info = " * Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794)) \n "
2020-06-10 06:40:53 +02:00
expected_message = f " john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 30 commits to branch master. Commits by John (30). \n \n { commits_info * COMMITS_LIMIT } [and { 30 - COMMITS_LIMIT } more commit(s)] "
2019-07-04 20:48:51 +02:00
self . send_and_test_stream_message ( ' push__commits_more_than_limits ' , expected_topic , expected_message )
2017-01-28 18:42:59 +01:00
2017-11-04 07:47:46 +01:00
def test_push_commits_more_than_limits_filtered_by_branches ( self ) - > None :
2017-04-06 23:26:29 +02:00
self . url = self . build_webhook_url ( branches = ' master,development ' )
2020-04-09 21:51:58 +02:00
expected_topic = " try-git / master "
2017-04-06 23:26:29 +02:00
commits_info = " * Webhook Test ([d8fce16](http://localhost:3000/john/try-git/commit/d8fce16c72a2ff56a5afc8a08645a6ce45491794)) \n "
2020-06-10 06:40:53 +02:00
expected_message = f " john [pushed](http://localhost:3000/john/try-git/compare/479e6b772b7fba19412457483f50b201286d0103...d8fce16c72a2ff56a5afc8a08645a6ce45491794) 30 commits to branch master. Commits by John (30). \n \n { commits_info * COMMITS_LIMIT } [and { 30 - COMMITS_LIMIT } more commit(s)] "
2019-07-04 20:48:51 +02:00
self . send_and_test_stream_message ( ' push__commits_more_than_limits ' , expected_topic , expected_message )
2017-04-06 23:26:29 +02:00
2017-11-04 07:47:46 +01:00
def test_new_branch ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " try-git / my_feature "
expected_message = " john created [my_feature](http://localhost:3000/john/try-git/src/my_feature) branch. "
2019-07-04 20:48:51 +02:00
self . send_and_test_stream_message ( ' create__branch ' , expected_topic , expected_message )
2017-01-28 18:42:59 +01:00
2017-11-04 07:47:46 +01:00
def test_pull_request_opened ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " try-git / PR #1 Title Text for Pull Request "
expected_message = """ john opened [PR #1](http://localhost:3000/john/try-git/pulls/1) from `feature` to `master`. """
2019-07-04 20:48:51 +02:00
self . send_and_test_stream_message ( ' pull_request__opened ' , expected_topic , expected_message )
2017-01-28 18:42:59 +01:00
2018-07-23 20:14:24 +02:00
def test_pull_request_opened_with_custom_topic_in_url ( self ) - > None :
self . url = self . build_webhook_url ( topic = ' notifications ' )
2020-04-09 21:51:58 +02:00
expected_topic = " notifications "
expected_message = """ john opened [PR #1 Title Text for Pull Request](http://localhost:3000/john/try-git/pulls/1) from `feature` to `master`. """
2019-07-04 20:48:51 +02:00
self . send_and_test_stream_message ( ' pull_request__opened ' , expected_topic , expected_message )
2018-07-23 20:14:24 +02:00
2017-11-04 07:47:46 +01:00
def test_pull_request_closed ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " try-git / PR #1 Title Text for Pull Request "
expected_message = """ john closed [PR #1](http://localhost:3000/john/try-git/pulls/1) from `feature` to `master`. """
2019-07-04 20:48:51 +02:00
self . send_and_test_stream_message ( ' pull_request__closed ' , expected_topic , expected_message )
2017-01-28 18:42:59 +01:00
2017-11-04 07:47:46 +01:00
def test_pull_request_merged ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " try-git / PR #2 Title Text for Pull Request "
expected_message = """ john merged [PR #2](http://localhost:3000/john/try-git/pulls/2) from `feature` to `master`. """
2019-07-04 20:48:51 +02:00
self . send_and_test_stream_message ( ' pull_request__merged ' , expected_topic , expected_message )
2017-04-06 23:26:29 +02:00
2019-11-18 03:41:13 +01:00
def test_pull_request_reopened ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " test / PR #1349 reopened "
expected_message = """ kostekIV reopened [PR #2](https://try.gogs.io/kostekIV/test/pulls/2) from `c` to `master`. """
2019-11-18 03:41:13 +01:00
self . send_and_test_stream_message ( ' pull_request__reopened ' , expected_topic , expected_message )
def test_pull_request_edited ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " test / PR #1349 Test "
expected_message = """ kostekIV edited [PR #2](https://try.gogs.io/kostekIV/test/pulls/2) from `c` to `master`. """
2019-11-18 03:41:13 +01:00
self . send_and_test_stream_message ( ' pull_request__edited ' , expected_topic , expected_message )
def test_pull_request_assigned ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " test / PR #1349 Test "
expected_message = """ kostekIV assigned [PR #2](https://try.gogs.io/kostekIV/test/pulls/2) from `c` to `master`. """
2019-11-18 03:41:13 +01:00
self . send_and_test_stream_message ( ' pull_request__assigned ' , expected_topic , expected_message )
def test_pull_request_synchronized ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " test / PR #1349 Test "
expected_message = """ kostekIV synchronized [PR #2](https://try.gogs.io/kostekIV/test/pulls/2) from `c` to `master`. """
2019-11-18 03:41:13 +01:00
self . send_and_test_stream_message ( ' pull_request__synchronized ' , expected_topic , expected_message )
def test_issues_opened ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " test / Issue #3 New test issue "
expected_message = """ kostekIV opened [Issue #3](https://try.gogs.io/kostekIV/test/issues/3): \n \n ~~~ quote \n Test \n ~~~ """
2019-11-18 03:41:13 +01:00
self . send_and_test_stream_message ( ' issues__opened ' , expected_topic , expected_message )
def test_issues_reopened ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " test / Issue #3 New test issue "
expected_message = """ kostekIV reopened [Issue #3](https://try.gogs.io/kostekIV/test/issues/3): \n \n ~~~ quote \n Test \n ~~~ """
2019-11-18 03:41:13 +01:00
self . send_and_test_stream_message ( ' issues__reopened ' , expected_topic , expected_message )
def test_issues_edited ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " test / Issue #3 New test issue "
expected_message = """ kostekIV edited [Issue #3](https://try.gogs.io/kostekIV/test/issues/3): \n \n ~~~ quote \n Test edit \n ~~~ """
2019-11-18 03:41:13 +01:00
self . send_and_test_stream_message ( ' issues__edited ' , expected_topic , expected_message )
def test_issues_assignee ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " test / Issue #3 New test issue "
expected_message = """ kostekIV assigned [Issue #3](https://try.gogs.io/kostekIV/test/issues/3) (assigned to kostekIV): \n \n ~~~ quote \n Test \n ~~~ """
2019-11-18 03:41:13 +01:00
self . send_and_test_stream_message ( ' issues__assigned ' , expected_topic , expected_message )
def test_issues_closed ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " test / Issue #3 New test issue "
expected_message = """ kostekIV closed [Issue #3](https://try.gogs.io/kostekIV/test/issues/3): \n \n ~~~ quote \n Closed #3 \n ~~~ """
2019-11-18 03:41:13 +01:00
self . send_and_test_stream_message ( ' issues__closed ' , expected_topic , expected_message )
def test_issue_comment_new ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " test / Issue #3 New test issue "
expected_message = """ kostekIV [commented](https://try.gogs.io/kostekIV/test/issues/3#issuecomment-3635) on [Issue #3](https://try.gogs.io/kostekIV/test/issues/3): \n \n ~~~ quote \n Test comment \n ~~~ """
2019-11-18 03:41:13 +01:00
self . send_and_test_stream_message ( ' issue_comment__new ' , expected_topic , expected_message )
def test_issue_comment_edited ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " test / Issue #3 New test issue "
expected_message = """ kostekIV edited a [comment](https://try.gogs.io/kostekIV/test/issues/3#issuecomment-3634) on [Issue #3](https://try.gogs.io/kostekIV/test/issues/3): \n \n ~~~ quote \n edit comment \n ~~~ """
2019-11-18 03:41:13 +01:00
self . send_and_test_stream_message ( ' issue_comment__edited ' , expected_topic , expected_message )
2020-05-11 08:58:53 +02:00
def test_release_published ( self ) - > None :
expected_topic = " zulip_test / v1.4 Title "
expected_message = """ cestrell published release [Title](https://try.gogs.io/cestrell/zulip_test) for tag v1.4. """
self . send_and_test_stream_message ( ' release__published ' , expected_topic , expected_message )
2018-03-16 22:53:50 +01:00
@patch ( ' zerver.webhooks.gogs.view.check_send_webhook_message ' )
def test_push_filtered_by_branches_ignore ( self , check_send_webhook_message_mock : MagicMock ) - > None :
2017-04-06 23:26:29 +02:00
self . url = self . build_webhook_url ( branches = ' changes,development ' )
payload = self . get_body ( ' push ' )
result = self . client_post ( self . url , payload , HTTP_X_GOGS_EVENT = ' push ' ,
content_type = " application/json " )
2018-03-16 22:53:50 +01:00
self . assertFalse ( check_send_webhook_message_mock . called )
2017-04-06 23:26:29 +02:00
self . assert_json_success ( result )
2018-03-16 22:53:50 +01:00
@patch ( ' zerver.webhooks.gogs.view.check_send_webhook_message ' )
2017-04-06 23:26:29 +02:00
def test_push_commits_more_than_limits_filtered_by_branches_ignore (
2018-03-16 22:53:50 +01:00
self , check_send_webhook_message_mock : MagicMock ) - > None :
2017-04-06 23:26:29 +02:00
self . url = self . build_webhook_url ( branches = ' changes,development ' )
2019-07-04 20:48:51 +02:00
payload = self . get_body ( ' push__commits_more_than_limits ' )
2017-04-06 23:26:29 +02:00
result = self . client_post ( self . url , payload , HTTP_X_GOGS_EVENT = ' push ' ,
content_type = " application/json " )
2018-03-16 22:53:50 +01:00
self . assertFalse ( check_send_webhook_message_mock . called )
2017-04-06 23:26:29 +02:00
self . assert_json_success ( result )
2018-03-16 22:53:50 +01:00
@patch ( ' zerver.webhooks.gogs.view.check_send_webhook_message ' )
2017-04-06 23:26:29 +02:00
def test_push_multiple_committers_filtered_by_branches_ignore (
2018-03-16 22:53:50 +01:00
self , check_send_webhook_message_mock : MagicMock ) - > None :
2017-04-06 23:26:29 +02:00
self . url = self . build_webhook_url ( branches = ' changes,development ' )
2019-07-04 20:48:51 +02:00
payload = self . get_body ( ' push__commits_multiple_committers ' )
2017-04-06 23:26:29 +02:00
result = self . client_post ( self . url , payload , HTTP_X_GOGS_EVENT = ' push ' ,
content_type = " application/json " )
2018-03-16 22:53:50 +01:00
self . assertFalse ( check_send_webhook_message_mock . called )
2017-04-06 23:26:29 +02:00
self . assert_json_success ( result )