2016-09-20 22:51:11 +02:00
# -*- coding: utf-8 -*-
2016-10-07 17:53:27 +02:00
from six import text_type
from typing import Union
2016-10-12 23:27:24 +02:00
from zerver . lib . webhooks . git import COMMITS_LIMIT
2016-09-20 22:51:11 +02:00
from zerver . lib . test_helpers import WebhookTestCase
class Bitbucket2HookTests ( WebhookTestCase ) :
STREAM_NAME = ' bitbucket2 '
URL_TEMPLATE = " /api/v1/external/bitbucket2?stream= {stream} &api_key= {api_key} "
2016-10-07 16:12:52 +02:00
FIXTURE_DIR_NAME = ' bitbucket '
2016-10-02 09:44:33 +02:00
EXPECTED_SUBJECT = u " Repository name "
2016-10-11 18:55:39 +02:00
EXPECTED_SUBJECT_PR_EVENTS = u " Repository name / PR #1 new commit "
2016-10-19 23:44:02 +02:00
EXPECTED_SUBJECT_ISSUE_EVENTS = u " Repository name / Issue #1 Bug "
2016-10-05 19:48:15 +02:00
EXPECTED_SUBJECT_BRANCH_EVENTS = u " Repository name / master "
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_push_event ( self ) :
# type: () -> None
2016-10-07 15:12:51 +02:00
commit_info = u ' * [84b96ad](https://bitbucket.org/kolaszek/repository-name/commits/84b96adc644a30fd6465b3d196369d880762afed): first commit '
expected_message = u " kolaszek [pushed](https://bitbucket.org/kolaszek/repository-name/branch/master) to branch master \n \n {} " . format ( commit_info )
2016-10-07 16:12:52 +02:00
self . send_and_test_stream_message ( ' v2_push ' , self . EXPECTED_SUBJECT_BRANCH_EVENTS , expected_message )
2016-09-20 22:51:11 +02:00
2016-10-07 15:12:51 +02:00
def test_bitbucket2_on_push_commits_above_limit_event ( self ) :
# type: () -> None
2016-10-12 23:27:24 +02:00
number_of_hidden_commits = 50 - COMMITS_LIMIT
2016-10-07 15:12:51 +02:00
commit_info = ' * [84b96ad](https://bitbucket.org/kolaszek/repository-name/commits/84b96adc644a30fd6465b3d196369d880762afed): first commit \n '
expected_message = u " kolaszek [pushed](https://bitbucket.org/kolaszek/repository-name/branch/master) to branch master \n \n {} [and {} more commit(s)] " . format (
( commit_info * 10 ) ,
number_of_hidden_commits
)
2016-10-07 16:12:52 +02:00
self . send_and_test_stream_message ( ' v2_push_commits_above_limit ' , self . EXPECTED_SUBJECT_BRANCH_EVENTS , expected_message )
2016-10-07 15:12:51 +02:00
2016-10-06 15:32:10 +02:00
def test_bitbucket2_on_force_push_event ( self ) :
# type: () -> None
expected_message = u " kolaszek [force pushed](https://bitbucket.org/kolaszek/repository-name/branch/master) to branch master. Head is now 25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12 "
2016-10-07 16:12:52 +02:00
self . send_and_test_stream_message ( ' v2_force_push ' , self . EXPECTED_SUBJECT_BRANCH_EVENTS , expected_message )
2016-10-06 15:32:10 +02:00
2016-10-06 16:14:51 +02:00
def test_bitbucket2_on_remove_branch_event ( self ) :
# type: () -> None
expected_message = u " kolaszek deleted branch master "
2016-10-07 16:12:52 +02:00
self . send_and_test_stream_message ( ' v2_remove_branch ' , self . EXPECTED_SUBJECT_BRANCH_EVENTS , expected_message )
2016-10-06 16:14:51 +02:00
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_fork_event ( self ) :
# type: () -> None
expected_message = u " User Tomasz(login: kolaszek) forked the repository into [kolaszek/repository-name2](https://bitbucket.org/kolaszek/repository-name2). "
2016-10-07 16:12:52 +02:00
self . send_and_test_stream_message ( ' v2_fork ' , self . EXPECTED_SUBJECT , expected_message )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_commit_comment_created_event ( self ) :
# type: () -> None
2016-10-27 21:43:15 +02:00
expected_message = u " kolaszek [commented](https://bitbucket.org/kolaszek/repository-name/commits/32c4ea19aa3af10acd08e419e2c354941a365d74#comment-3354963) on [32c4ea1](https://bitbucket.org/kolaszek/repository-name/commits/32c4ea19aa3af10acd08e419e2c354941a365d74) \n ~~~ quote \n Nice fix! \n ~~~ "
2016-10-07 16:12:52 +02:00
self . send_and_test_stream_message ( ' v2_commit_comment_created ' , self . EXPECTED_SUBJECT , expected_message )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_commit_status_changed_event ( self ) :
# type: () -> None
expected_message = u " [System mybuildtool](https://my-build-tool.com/builds/MY-PROJECT/BUILD-777) changed status of https://bitbucket.org/kolaszek/repository-name/9fec847784abb10b2fa567ee63b85bd238955d0e to SUCCESSFUL. "
2016-10-07 16:12:52 +02:00
self . send_and_test_stream_message ( ' v2_commit_status_changed ' , self . EXPECTED_SUBJECT , expected_message )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_issue_created_event ( self ) :
# type: () -> None
2016-10-26 21:13:00 +02:00
expected_message = u " kolaszek created [Issue #1](https://bitbucket.org/kolaszek/repository-name/issues/2/bug)(assigned to kolaszek) \n \n ~~~ quote \n Such a bug \n ~~~ "
2016-10-19 23:44:02 +02:00
self . send_and_test_stream_message ( ' v2_issue_created ' , self . EXPECTED_SUBJECT_ISSUE_EVENTS , expected_message )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_issue_updated_event ( self ) :
# type: () -> None
2016-10-26 21:13:00 +02:00
expected_message = u " kolaszek updated [Issue #1](https://bitbucket.org/kolaszek/repository-name/issues/2/bug) "
2016-10-19 23:44:02 +02:00
self . send_and_test_stream_message ( ' v2_issue_updated ' , self . EXPECTED_SUBJECT_ISSUE_EVENTS , expected_message )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_issue_commented_event ( self ) :
# type: () -> None
2016-10-26 21:13:00 +02:00
expected_message = u " kolaszek [commented](https://bitbucket.org/kolaszek/repository-name/issues/2#comment-28973596) on [Issue #1](https://bitbucket.org/kolaszek/repository-name/issues/2/bug) "
2016-10-19 23:44:02 +02:00
self . send_and_test_stream_message ( ' v2_issue_commented ' , self . EXPECTED_SUBJECT_ISSUE_EVENTS , expected_message )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_pull_request_created_event ( self ) :
# type: () -> None
2016-10-26 21:13:00 +02:00
expected_message = u " kolaszek created [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)(assigned to tkolek) \n from `new-branch` to `master` \n \n ~~~ quote \n description \n ~~~ "
2016-09-20 22:51:11 +02:00
kwargs = {
" HTTP_X_EVENT_KEY " : ' pullrequest:created '
}
2016-10-11 18:55:39 +02:00
self . send_and_test_stream_message ( ' v2_pull_request_created_or_updated ' , self . EXPECTED_SUBJECT_PR_EVENTS , expected_message , * * kwargs )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_pull_request_updated_event ( self ) :
# type: () -> None
2016-10-26 21:13:00 +02:00
expected_message = u " kolaszek updated [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)(assigned to tkolek) \n from `new-branch` to `master` \n \n ~~~ quote \n description \n ~~~ "
2016-09-20 22:51:11 +02:00
kwargs = {
" HTTP_X_EVENT_KEY " : ' pullrequest:updated '
}
2016-10-11 18:55:39 +02:00
self . send_and_test_stream_message ( ' v2_pull_request_created_or_updated ' , self . EXPECTED_SUBJECT_PR_EVENTS , expected_message , * * kwargs )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_pull_request_approved_event ( self ) :
# type: () -> None
2016-10-26 21:13:00 +02:00
expected_message = u " kolaszek approved [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/1) "
2016-09-20 22:51:11 +02:00
kwargs = {
" HTTP_X_EVENT_KEY " : ' pullrequest:approved '
}
2016-10-11 18:55:39 +02:00
self . send_and_test_stream_message ( ' v2_pull_request_approved_or_unapproved ' , self . EXPECTED_SUBJECT_PR_EVENTS , expected_message , * * kwargs )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_pull_request_unapproved_event ( self ) :
# type: () -> None
2016-10-26 21:13:00 +02:00
expected_message = u " kolaszek unapproved [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/1) "
2016-09-20 22:51:11 +02:00
kwargs = {
" HTTP_X_EVENT_KEY " : ' pullrequest:unapproved '
}
2016-10-11 18:55:39 +02:00
self . send_and_test_stream_message ( ' v2_pull_request_approved_or_unapproved ' , self . EXPECTED_SUBJECT_PR_EVENTS , expected_message , * * kwargs )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_pull_request_declined_event ( self ) :
# type: () -> None
2016-10-26 21:13:00 +02:00
expected_message = u " kolaszek rejected [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/1) "
2016-09-20 22:51:11 +02:00
kwargs = {
" HTTP_X_EVENT_KEY " : ' pullrequest:rejected '
}
2016-10-11 18:55:39 +02:00
self . send_and_test_stream_message ( ' v2_pull_request_merged_or_rejected ' , self . EXPECTED_SUBJECT_PR_EVENTS , expected_message , * * kwargs )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_pull_request_merged_event ( self ) :
# type: () -> None
2016-10-26 21:13:00 +02:00
expected_message = u " kolaszek merged [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/1) "
2016-09-20 22:51:11 +02:00
kwargs = {
" HTTP_X_EVENT_KEY " : ' pullrequest:merged '
}
2016-10-11 18:55:39 +02:00
self . send_and_test_stream_message ( ' v2_pull_request_merged_or_rejected ' , self . EXPECTED_SUBJECT_PR_EVENTS , expected_message , * * kwargs )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_pull_request_comment_created_event ( self ) :
# type: () -> None
2016-10-26 21:13:00 +02:00
expected_message = u " kolaszek [commented](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503) on [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/3) \n \n ~~~ quote \n Comment1 \n ~~~ "
2016-09-20 22:51:11 +02:00
kwargs = {
" HTTP_X_EVENT_KEY " : ' pullrequest:comment_created '
}
2016-10-11 18:55:39 +02:00
self . send_and_test_stream_message ( ' v2_pull_request_comment_action ' , self . EXPECTED_SUBJECT_PR_EVENTS , expected_message , * * kwargs )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_pull_request_comment_updated_event ( self ) :
# type: () -> None
2016-10-26 21:13:00 +02:00
expected_message = u " kolaszek updated [comment](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503) on [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/3) \n \n ~~~ quote \n Comment1 \n ~~~ "
2016-09-20 22:51:11 +02:00
kwargs = {
" HTTP_X_EVENT_KEY " : ' pullrequest:comment_updated '
}
2016-10-11 18:55:39 +02:00
self . send_and_test_stream_message ( ' v2_pull_request_comment_action ' , self . EXPECTED_SUBJECT_PR_EVENTS , expected_message , * * kwargs )
2016-09-20 22:51:11 +02:00
def test_bitbucket2_on_pull_request_comment_deleted_event ( self ) :
# type: () -> None
2016-10-26 21:13:00 +02:00
expected_message = u " kolaszek deleted [comment](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503) on [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/3) \n \n ~~~ quote \n Comment1 \n ~~~ "
2016-09-20 22:51:11 +02:00
kwargs = {
" HTTP_X_EVENT_KEY " : ' pullrequest:comment_deleted '
}
2016-10-11 18:55:39 +02:00
self . send_and_test_stream_message ( ' v2_pull_request_comment_action ' , self . EXPECTED_SUBJECT_PR_EVENTS , expected_message , * * kwargs )
2016-10-07 17:53:27 +02:00
class BitbucketHookTests ( WebhookTestCase ) :
STREAM_NAME = ' bitbucket '
URL_TEMPLATE = " /api/v1/external/bitbucket?payload= {payload} &stream= {stream} "
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 '
self . url = self . build_url ( fixture_name )
commit_info = u ' * [25f93d2](https://bitbucket.org/kolaszek/repository-name/commits/25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12): c '
expected_message = u " kolaszek pushed to branch master \n \n {} " . format ( commit_info )
self . send_and_test_stream_message ( fixture_name , self . EXPECTED_SUBJECT_BRANCH_EVENTS , expected_message , * * self . api_auth ( self . TEST_USER_EMAIL ) )
def test_bitbucket_on_push_commits_above_limit_event ( self ) :
# type: () -> None
fixture_name = ' push_commits_above_limit '
self . url = self . build_url ( fixture_name )
commit_info = u ' * [25f93d2](https://bitbucket.org/kolaszek/repository-name/commits/25f93d22b719e2d678a7ad5ee0ef0d1fcdf39c12): c \n '
expected_message = u " kolaszek pushed to branch master \n \n {} [and 40 more commit(s)] " . format ( commit_info * 10 )
self . send_and_test_stream_message ( fixture_name , self . EXPECTED_SUBJECT_BRANCH_EVENTS , expected_message , * * self . api_auth ( self . TEST_USER_EMAIL ) )
def test_bitbucket_on_force_push_event ( self ) :
# type: () -> None
fixture_name = ' force_push '
self . url = self . build_url ( fixture_name )
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 ) )
def get_body ( self , fixture_name ) :
# type: (text_type) -> Union[text_type, Dict[str, text_type]]
return { }
def get_payload ( self , fixture_name ) :
# type: (text_type) -> Union[text_type, Dict[str, text_type]]
return self . fixture_data ( self . FIXTURE_DIR_NAME , fixture_name )
def build_webhook_url ( self ) :
# type: () -> text_type
return ' '
def build_url ( self , fixture_name ) :
# type: (text_type) -> text_type
return self . URL_TEMPLATE . format ( payload = self . get_payload ( fixture_name ) , stream = self . STREAM_NAME )