2022-07-17 13:28:15 +02:00
from unittest . mock import MagicMock , patch
from zerver . lib . test_classes import WebhookTestCase
from zerver . lib . webhooks . git import COMMITS_LIMIT
class AzuredevopsHookTests ( WebhookTestCase ) :
2024-05-04 22:02:50 +02:00
CHANNEL_NAME = " azure-devops "
2022-07-17 13:28:15 +02:00
URL_TEMPLATE = " /api/v1/external/azuredevops?&api_key= {api_key} &stream= {stream} "
WEBHOOK_DIR_NAME = " azuredevops "
def test_push_event_message ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " test-zulip / main "
2022-11-07 21:15:53 +01:00
expected_message = " Yuro Itaki [pushed](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/branchCompare?baseVersion=GC51515957669f93c543df09f8f3e7f47c3613c879&targetVersion=GCb0ce2f2009c3c87dbefadf61d7eb2c0697a6f369&_a=files) 1 commit to branch main. \n \n * Modify readme ([b0ce2f2009c](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/commit/b0ce2f2009c3c87dbefadf61d7eb2c0697a6f369)) "
2024-01-17 15:53:30 +01:00
self . check_webhook ( " code_push " , expected_topic_name , expected_message )
2022-07-17 13:28:15 +02:00
def test_push_event_message_filtered_by_branches ( self ) - > None :
self . url = self . build_webhook_url ( branches = " main,dev " )
2024-01-17 15:53:30 +01:00
expected_topic_name = " test-zulip / main "
2022-11-07 21:15:53 +01:00
expected_message = " Yuro Itaki [pushed](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/branchCompare?baseVersion=GC51515957669f93c543df09f8f3e7f47c3613c879&targetVersion=GCb0ce2f2009c3c87dbefadf61d7eb2c0697a6f369&_a=files) 1 commit to branch main. \n \n * Modify readme ([b0ce2f2009c](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/commit/b0ce2f2009c3c87dbefadf61d7eb2c0697a6f369)) "
2024-01-17 15:53:30 +01:00
self . check_webhook ( " code_push " , expected_topic_name , expected_message )
2022-07-17 13:28:15 +02:00
@patch ( " zerver.lib.webhooks.common.check_send_webhook_message " )
def test_push_event_message_filtered_by_branches_ignore (
self , check_send_webhook_message_mock : MagicMock
) - > None :
self . url = self . build_webhook_url ( branches = " development " )
payload = self . get_body ( " code_push " )
result = self . client_post ( self . url , payload , content_type = " application/json " )
self . assertFalse ( check_send_webhook_message_mock . called )
self . assert_json_success ( result )
def test_push_local_branch_without_commits ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " test-zulip / dev "
2022-07-17 13:28:15 +02:00
expected_message = " Yuro Itaki [pushed](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/branchCompare?baseVersion=GC0000000000000000000000000000000000000000&targetVersion=GC0929a3404b39f6e39076a640779b2c1c961e19b5&_a=files) the branch dev. "
self . check_webhook (
2024-01-17 15:53:30 +01:00
" code_push__local_branch_without_commits " , expected_topic_name , expected_message
2022-07-17 13:28:15 +02:00
)
def test_push_multiple_committers ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " test-zulip / main "
2022-11-07 21:15:53 +01:00
expected_message = " Yuro Itaki [pushed](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/branchCompare?baseVersion=GCcc21b940719cc372b364d932eb39e528b0ec2a91&targetVersion=GC0929a3404b39f6e39076a640779b2c1c961e19b5&_a=files) 2 commits to branch main. Commits by Itachi Sensei (1) and Yuro Itaki (1). \n \n * Add reply ([0929a3404b3](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/commit/0929a3404b39f6e39076a640779b2c1c961e19b5)) \n * Add how are you ([819ce8de51b](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/commit/819ce8de51bedfc250c202edcaee0ce8dc70bf3b)) "
2024-01-17 15:53:30 +01:00
self . check_webhook ( " code_push__multiple_committers " , expected_topic_name , expected_message )
2022-07-17 13:28:15 +02:00
def test_push_multiple_committers_with_others ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " test-zulip / main "
2022-11-07 21:15:53 +01:00
commits_info = " * Add how are you ([819ce8de51b](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/commit/819ce8de51bedfc250c202edcaee0ce8dc70bf3b)) \n "
expected_message = f " Yuro Itaki [pushed](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/branchCompare?baseVersion=GCcc21b940719cc372b364d932eb39e528b0ec2a91&targetVersion=GC0929a3404b39f6e39076a640779b2c1c961e19b5&_a=files) 6 commits to branch main. Commits by Itachi Sensei (2), Yuro Itaki (2), Jonas Nielsen (1) and others (1). \n \n * Add reply ([0929a3404b3](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/commit/0929a3404b39f6e39076a640779b2c1c961e19b5)) \n { commits_info * 4 } * Add reply ([0929a3404b3](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/commit/0929a3404b39f6e39076a640779b2c1c961e19b5)) "
2022-07-17 13:28:15 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" code_push__multiple_committers_with_others " , expected_topic_name , expected_message
2022-07-17 13:28:15 +02:00
)
def test_push_commits_more_than_limit ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " test-zulip / main "
2022-11-07 21:15:53 +01:00
commits_info = " * Modify readme ([b0ce2f2009c](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/commit/b0ce2f2009c3c87dbefadf61d7eb2c0697a6f369)) \n "
2022-07-17 13:28:15 +02:00
expected_message = f " Yuro Itaki [pushed](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/branchCompare?baseVersion=GC51515957669f93c543df09f8f3e7f47c3613c879&targetVersion=GCb0ce2f2009c3c87dbefadf61d7eb2c0697a6f369&_a=files) 50 commits to branch main. \n \n { commits_info * COMMITS_LIMIT } [and { 50 - COMMITS_LIMIT } more commit(s)] "
2024-01-17 15:53:30 +01:00
self . check_webhook (
" code_push__commits_more_than_limit " , expected_topic_name , expected_message
)
2022-07-17 13:28:15 +02:00
def test_push_remove_branch ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " test-zulip / dev "
2022-07-17 13:28:15 +02:00
expected_message = " Yuro Itaki [pushed](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/branchCompare?baseVersion=GC0929a3404b39f6e39076a640779b2c1c961e19b5&targetVersion=GC0000000000000000000000000000000000000000&_a=files) the branch dev. "
2024-01-17 15:53:30 +01:00
self . check_webhook ( " code_push__remove_branch " , expected_topic_name , expected_message )
2022-07-17 13:28:15 +02:00
def test_pull_request_opened ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " test-zulip / PR #1 Add PR request "
2022-07-17 13:28:15 +02:00
expected_message = " Yuro Itaki created [PR #1 Add PR request](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/pullrequest/1) from `dev` to `main`: \n \n ~~~ quote \n Add PR request \n ~~~ "
2024-01-17 15:53:30 +01:00
self . check_webhook ( " code_pull_request__opened " , expected_topic_name , expected_message )
2022-07-17 13:28:15 +02:00
def test_pull_request_opened_without_description ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " test-zulip / PR #2 Raised 2nd PR! "
2022-07-17 13:28:15 +02:00
expected_message = " Yuro Itaki created [PR #2 Raised 2nd PR!](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/pullrequest/2) from `stg` to `main`. "
self . check_webhook (
2024-01-17 15:53:30 +01:00
" code_pull_request__opened_without_description " , expected_topic_name , expected_message
2022-07-17 13:28:15 +02:00
)
def test_pull_request_merged ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " test-zulip / PR #1 Add PR request "
2023-03-16 23:12:08 +01:00
expected_message = " Yuro Itaki merged [PR #1 Add PR request](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/pullrequest/1) from `dev` to `main`. "
2024-01-17 15:53:30 +01:00
self . check_webhook ( " code_pull_request__merged " , expected_topic_name , expected_message )
2022-07-17 13:28:15 +02:00
@patch ( " zerver.lib.webhooks.common.check_send_webhook_message " )
def test_pull_request_merge_attempted_ignore (
self , check_send_webhook_message_mock : MagicMock
) - > None :
self . url = self . build_webhook_url ( )
payload = self . get_body ( " code_pull_request__merge_attempted " )
result = self . client_post ( self . url , payload , content_type = " application/json " )
self . assertFalse ( check_send_webhook_message_mock . called )
self . assert_json_success ( result )
def test_pull_request_updated ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " test-zulip / PR #2 Raised 2nd PR! "
2022-07-17 13:28:15 +02:00
expected_message = " Yuro Itaki updated [PR #2 Raised 2nd PR!](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/pullrequest/2) \n \n ~~~ quote \n Yuro Itaki updated the source branch of [pull request 2](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/pullrequest/2) (Raised 2nd PR!) in [test-zulip](https://dev.azure.com/ttchong/test-zulip/_git/test-zulip/) \r \n Raised 2nd PR! \r \n \n ~~~ "
2024-01-17 15:53:30 +01:00
self . check_webhook ( " code_pull_request__updated " , expected_topic_name , expected_message )