2016-11-23 20:15:23 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-04-05 02:52:31 +02:00
|
|
|
from mock import patch, MagicMock
|
|
|
|
from typing import Dict, Union, Text, Optional
|
|
|
|
|
2016-11-23 20:15:23 +01:00
|
|
|
from zerver.lib.test_classes import WebhookTestCase
|
|
|
|
|
|
|
|
class BitbucketHookTests(WebhookTestCase):
|
|
|
|
STREAM_NAME = 'bitbucket'
|
2017-04-21 23:35:40 +02:00
|
|
|
URL_TEMPLATE = "/api/v1/external/bitbucket?stream={stream}"
|
2016-11-23 20:15:23 +01:00
|
|
|
FIXTURE_DIR_NAME = 'bitbucket'
|
|
|
|
EXPECTED_SUBJECT = u"Repository name"
|
|
|
|
EXPECTED_SUBJECT_BRANCH_EVENTS = u"Repository name / master"
|
|
|
|
|
|
|
|
def test_bitbucket_on_push_event(self):
|
|
|
|
# type: () -> None
|
|
|
|
fixture_name = 'push'
|
2017-04-21 23:35:40 +02:00
|
|
|
self.url = self.build_webhook_url(payload=self.get_body(fixture_name))
|
2017-03-15 10:15:03 +01:00
|
|
|
commit_info = u'* c ([25f93d2](https://bitbucket.org/kolaszek/repository-name/commits/25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12))'
|
2017-04-26 00:19:47 +02:00
|
|
|
expected_message = u"kolaszek pushed 1 commit to branch master. Commits by kolaszek (1).\n\n{}".format(commit_info)
|
2016-11-23 20:15:23 +01:00
|
|
|
self.send_and_test_stream_message(fixture_name, self.EXPECTED_SUBJECT_BRANCH_EVENTS, expected_message, **self.api_auth(self.TEST_USER_EMAIL))
|
|
|
|
|
2017-04-05 02:52:31 +02:00
|
|
|
def test_bitbucket_on_push_event_filtered_by_branches(self):
|
|
|
|
# type: () -> None
|
|
|
|
fixture_name = 'push'
|
|
|
|
self.url = self.build_webhook_url(payload=self.get_body(fixture_name),
|
|
|
|
branches='master,development')
|
|
|
|
commit_info = u'* c ([25f93d2](https://bitbucket.org/kolaszek/repository-name/commits/25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12))'
|
2017-04-26 00:19:47 +02:00
|
|
|
expected_message = u"kolaszek pushed 1 commit to branch master. Commits by kolaszek (1).\n\n{}".format(commit_info)
|
2017-04-05 02:52:31 +02:00
|
|
|
self.send_and_test_stream_message(fixture_name, self.EXPECTED_SUBJECT_BRANCH_EVENTS, expected_message, **self.api_auth(self.TEST_USER_EMAIL))
|
|
|
|
|
2016-11-23 20:15:23 +01:00
|
|
|
def test_bitbucket_on_push_commits_above_limit_event(self):
|
|
|
|
# type: () -> None
|
|
|
|
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))
|
2017-03-15 10:15:03 +01:00
|
|
|
commit_info = u'* c ([25f93d2](https://bitbucket.org/kolaszek/repository-name/commits/25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12))\n'
|
2017-04-26 00:19:47 +02:00
|
|
|
expected_message = u"kolaszek pushed 50 commits to branch master. Commits by kolaszek (50).\n\n{}[and 30 more commit(s)]".format(commit_info * 20)
|
2016-11-23 20:15:23 +01:00
|
|
|
self.send_and_test_stream_message(fixture_name, self.EXPECTED_SUBJECT_BRANCH_EVENTS, expected_message, **self.api_auth(self.TEST_USER_EMAIL))
|
|
|
|
|
2017-04-05 02:52:31 +02:00
|
|
|
def test_bitbucket_on_push_commits_above_limit_event_filtered_by_branches(self):
|
|
|
|
# type: () -> None
|
|
|
|
fixture_name = 'push_commits_above_limit'
|
|
|
|
self.url = self.build_webhook_url(payload=self.get_body(fixture_name),
|
|
|
|
branches='master,development')
|
|
|
|
commit_info = u'* c ([25f93d2](https://bitbucket.org/kolaszek/repository-name/commits/25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12))\n'
|
2017-04-26 00:19:47 +02:00
|
|
|
expected_message = u"kolaszek pushed 50 commits to branch master. Commits by kolaszek (50).\n\n{}[and 30 more commit(s)]".format(commit_info * 20)
|
2017-04-05 02:52:31 +02:00
|
|
|
self.send_and_test_stream_message(fixture_name, self.EXPECTED_SUBJECT_BRANCH_EVENTS, expected_message, **self.api_auth(self.TEST_USER_EMAIL))
|
|
|
|
|
2016-11-23 20:15:23 +01:00
|
|
|
def test_bitbucket_on_force_push_event(self):
|
|
|
|
# type: () -> None
|
|
|
|
fixture_name = 'force_push'
|
2017-04-21 23:35:40 +02:00
|
|
|
self.url = self.build_webhook_url(payload=self.get_body(fixture_name))
|
2016-11-23 20:15:23 +01:00
|
|
|
expected_message = u"kolaszek [force pushed](https://bitbucket.org/kolaszek/repository-name)"
|
|
|
|
self.send_and_test_stream_message(fixture_name, self.EXPECTED_SUBJECT, expected_message, **self.api_auth(self.TEST_USER_EMAIL))
|
|
|
|
|
2017-04-05 02:52:31 +02:00
|
|
|
@patch('zerver.webhooks.bitbucket.view.check_send_message')
|
|
|
|
def test_bitbucket_on_push_event_filtered_by_branches_ignore(self, check_send_message_mock):
|
|
|
|
# type: (MagicMock) -> None
|
|
|
|
fixture_name = 'push'
|
|
|
|
payload = self.get_body(fixture_name)
|
|
|
|
self.url = self.build_webhook_url(payload=payload,
|
|
|
|
branches='changes,development')
|
|
|
|
result = self.client_post(self.url, payload,
|
|
|
|
content_type="application/json,",
|
|
|
|
**self.api_auth(self.TEST_USER_EMAIL))
|
|
|
|
self.assertFalse(check_send_message_mock.called)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
@patch('zerver.webhooks.bitbucket.view.check_send_message')
|
|
|
|
def test_bitbucket_push_commits_above_limit_filtered_by_branches_ignore(
|
|
|
|
self, check_send_message_mock):
|
|
|
|
# type: (MagicMock) -> None
|
|
|
|
fixture_name = 'push_commits_above_limit'
|
|
|
|
payload = self.get_body(fixture_name)
|
|
|
|
self.url = self.build_webhook_url(payload=payload,
|
|
|
|
branches='changes,development')
|
|
|
|
result = self.client_post(self.url, payload,
|
|
|
|
content_type="application/json,",
|
|
|
|
**self.api_auth(self.TEST_USER_EMAIL))
|
|
|
|
self.assertFalse(check_send_message_mock.called)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2016-11-23 20:15:23 +01:00
|
|
|
def get_body(self, fixture_name):
|
2017-04-05 02:52:31 +02:00
|
|
|
# type: (Text) -> Union[Text, Dict[str, Text]]
|
2016-11-23 20:15:23 +01:00
|
|
|
return self.fixture_data(self.FIXTURE_DIR_NAME, fixture_name)
|