2020-05-26 07:16:25 +02:00
|
|
|
from unittest.mock import MagicMock, patch
|
2017-04-05 02:52:31 +02:00
|
|
|
|
2016-11-23 20:15:23 +01:00
|
|
|
from zerver.lib.test_classes import WebhookTestCase
|
|
|
|
|
2020-08-22 17:16:32 +02:00
|
|
|
TOPIC = "Repository name"
|
|
|
|
TOPIC_BRANCH_EVENTS = "Repository name / master"
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2016-11-23 20:15:23 +01:00
|
|
|
class BitbucketHookTests(WebhookTestCase):
|
2021-02-12 08:20:45 +01:00
|
|
|
STREAM_NAME = "bitbucket"
|
2017-04-21 23:35:40 +02:00
|
|
|
URL_TEMPLATE = "/api/v1/external/bitbucket?stream={stream}"
|
2021-02-12 08:20:45 +01:00
|
|
|
FIXTURE_DIR_NAME = "bitbucket"
|
2016-11-23 20:15:23 +01:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_bitbucket_on_push_event(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
fixture_name = "push"
|
2017-04-21 23:35:40 +02:00
|
|
|
self.url = self.build_webhook_url(payload=self.get_body(fixture_name))
|
2021-02-12 08:20:45 +01:00
|
|
|
commit_info = "* c ([25f93d2](https://bitbucket.org/kolaszek/repository-name/commits/25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12))"
|
2020-06-09 00:25:09 +02:00
|
|
|
expected_message = f"kolaszek pushed 1 commit to branch master.\n\n{commit_info}"
|
2021-02-12 08:19:30 +01:00
|
|
|
self.api_stream_message(self.test_user, fixture_name, TOPIC_BRANCH_EVENTS, expected_message)
|
2016-11-23 20:15:23 +01:00
|
|
|
|
2019-03-16 18:39:03 +01:00
|
|
|
def test_bitbucket_on_push_event_without_user_info(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
fixture_name = "push_without_user_info"
|
2019-03-16 18:39:03 +01:00
|
|
|
self.url = self.build_webhook_url(payload=self.get_body(fixture_name))
|
2021-02-12 08:20:45 +01:00
|
|
|
commit_info = "* c ([25f93d2](https://bitbucket.org/kolaszek/repository-name/commits/25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12))"
|
2021-02-12 08:19:30 +01:00
|
|
|
expected_message = (
|
|
|
|
f"Someone pushed 1 commit to branch master. Commits by eeshangarg (1).\n\n{commit_info}"
|
|
|
|
)
|
|
|
|
self.api_stream_message(self.test_user, fixture_name, TOPIC_BRANCH_EVENTS, expected_message)
|
2019-03-16 18:39:03 +01:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_bitbucket_on_push_event_filtered_by_branches(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
fixture_name = "push"
|
2021-02-12 08:19:30 +01:00
|
|
|
self.url = self.build_webhook_url(
|
2021-02-12 08:20:45 +01:00
|
|
|
payload=self.get_body(fixture_name), branches="master,development"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
commit_info = "* c ([25f93d2](https://bitbucket.org/kolaszek/repository-name/commits/25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12))"
|
2020-06-09 00:25:09 +02:00
|
|
|
expected_message = f"kolaszek pushed 1 commit to branch master.\n\n{commit_info}"
|
2021-02-12 08:19:30 +01:00
|
|
|
self.api_stream_message(self.test_user, fixture_name, TOPIC_BRANCH_EVENTS, expected_message)
|
2017-04-05 02:52:31 +02:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_bitbucket_on_push_commits_above_limit_event(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
fixture_name = "push_commits_above_limit"
|
2017-04-21 23:35:40 +02:00
|
|
|
self.url = self.build_webhook_url(payload=self.get_body(fixture_name))
|
2021-02-12 08:20:45 +01:00
|
|
|
commit_info = "* c ([25f93d2](https://bitbucket.org/kolaszek/repository-name/commits/25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12))\n"
|
2020-06-09 00:25:09 +02:00
|
|
|
expected_message = f"kolaszek pushed 50 commits to branch master.\n\n{commit_info * 20}[and 30 more commit(s)]"
|
2021-02-12 08:19:30 +01:00
|
|
|
self.api_stream_message(self.test_user, fixture_name, TOPIC_BRANCH_EVENTS, expected_message)
|
2016-11-23 20:15:23 +01:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_bitbucket_on_push_commits_above_limit_event_filtered_by_branches(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
fixture_name = "push_commits_above_limit"
|
2021-02-12 08:19:30 +01:00
|
|
|
self.url = self.build_webhook_url(
|
2021-02-12 08:20:45 +01:00
|
|
|
payload=self.get_body(fixture_name), branches="master,development"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
commit_info = "* c ([25f93d2](https://bitbucket.org/kolaszek/repository-name/commits/25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12))\n"
|
2020-06-09 00:25:09 +02:00
|
|
|
expected_message = f"kolaszek pushed 50 commits to branch master.\n\n{commit_info * 20}[and 30 more commit(s)]"
|
2021-02-12 08:19:30 +01:00
|
|
|
self.api_stream_message(self.test_user, fixture_name, TOPIC_BRANCH_EVENTS, expected_message)
|
2017-04-05 02:52:31 +02:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_bitbucket_on_force_push_event(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
fixture_name = "force_push"
|
2017-04-21 23:35:40 +02:00
|
|
|
self.url = self.build_webhook_url(payload=self.get_body(fixture_name))
|
2021-02-12 08:19:30 +01:00
|
|
|
expected_message = (
|
|
|
|
"kolaszek [force pushed](https://bitbucket.org/kolaszek/repository-name)."
|
|
|
|
)
|
|
|
|
self.api_stream_message(self.test_user, fixture_name, TOPIC, expected_message)
|
2016-11-23 20:15:23 +01:00
|
|
|
|
2019-03-21 20:39:47 +01:00
|
|
|
def test_bitbucket_on_force_push_event_without_user_info(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
fixture_name = "force_push_without_user_info"
|
2019-03-21 20:39:47 +01:00
|
|
|
self.url = self.build_webhook_url(payload=self.get_body(fixture_name))
|
2021-02-12 08:19:30 +01:00
|
|
|
expected_message = (
|
|
|
|
"Someone [force pushed](https://bitbucket.org/kolaszek/repository-name/)."
|
|
|
|
)
|
|
|
|
self.api_stream_message(self.test_user, fixture_name, TOPIC, expected_message)
|
2019-03-21 20:39:47 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@patch("zerver.webhooks.bitbucket.view.check_send_webhook_message")
|
2021-02-12 08:19:30 +01:00
|
|
|
def test_bitbucket_on_push_event_filtered_by_branches_ignore(
|
|
|
|
self, check_send_webhook_message_mock: MagicMock
|
|
|
|
) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
fixture_name = "push"
|
2017-04-05 02:52:31 +02:00
|
|
|
payload = self.get_body(fixture_name)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.url = self.build_webhook_url(payload=payload, branches="changes,development")
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_post(self.test_user, self.url, payload, content_type="application/json,")
|
2018-03-13 23:43:02 +01:00
|
|
|
self.assertFalse(check_send_webhook_message_mock.called)
|
2017-04-05 02:52:31 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@patch("zerver.webhooks.bitbucket.view.check_send_webhook_message")
|
2017-04-05 02:52:31 +02:00
|
|
|
def test_bitbucket_push_commits_above_limit_filtered_by_branches_ignore(
|
2021-02-12 08:19:30 +01:00
|
|
|
self, check_send_webhook_message_mock: MagicMock
|
|
|
|
) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
fixture_name = "push_commits_above_limit"
|
2017-04-05 02:52:31 +02:00
|
|
|
payload = self.get_body(fixture_name)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.url = self.build_webhook_url(payload=payload, branches="changes,development")
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_post(self.test_user, self.url, payload, content_type="application/json,")
|
2018-03-13 23:43:02 +01:00
|
|
|
self.assertFalse(check_send_webhook_message_mock.called)
|
2017-04-05 02:52:31 +02:00
|
|
|
self.assert_json_success(result)
|