2020-05-26 07:16:25 +02:00
|
|
|
from unittest.mock import patch
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
|
|
import ujson
|
|
|
|
|
2016-11-10 19:30:09 +01:00
|
|
|
from zerver.lib.test_classes import WebhookTestCase
|
2016-09-20 22:51:11 +02:00
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2016-09-20 22:51:11 +02:00
|
|
|
class SemaphoreHookTests(WebhookTestCase):
|
|
|
|
STREAM_NAME = 'semaphore'
|
|
|
|
URL_TEMPLATE = "/api/v1/external/semaphore?stream={stream}&api_key={api_key}"
|
|
|
|
|
|
|
|
# Messages are generated by Semaphore on git push. The subject lines below
|
|
|
|
# contain information on the repo and branch, and the message has links and
|
|
|
|
# details about the build, deploy, server, author, and commit
|
|
|
|
|
2020-03-14 13:12:03 +01:00
|
|
|
# Tests for Semaphore Classic
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_semaphore_build(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "knighthood/master" # repo/branch
|
2019-04-17 03:57:31 +02:00
|
|
|
expected_message = """
|
|
|
|
[Build 314](https://semaphoreci.com/donquixote/knighthood/branches/master/builds/314) passed:
|
|
|
|
* **Commit**: [a490b8d: Create user account for Rocinante](https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf)
|
|
|
|
* **Author**: don@lamancha.com
|
|
|
|
""".strip()
|
2018-11-09 20:33:58 +01:00
|
|
|
self.send_and_test_stream_message('build', expected_topic, expected_message,
|
2016-09-20 22:51:11 +02:00
|
|
|
content_type="application/x-www-form-urlencoded")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_semaphore_deploy(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "knighthood/master"
|
2019-04-17 03:57:31 +02:00
|
|
|
expected_message = """
|
|
|
|
[Deploy 17](https://semaphoreci.com/donquixote/knighthood/servers/lamancha-271/deploys/17) of [build 314](https://semaphoreci.com/donquixote/knighthood/branches/master/builds/314) passed:
|
|
|
|
* **Commit**: [a490b8d: Create user account for Rocinante](https://github.com/donquixote/knighthood/commit/a490b8d508ebbdab1d77a5c2aefa35ceb2d62daf)
|
|
|
|
* **Author**: don@lamancha.com
|
|
|
|
* **Server**: lamancha-271
|
|
|
|
""".strip()
|
2018-11-09 20:33:58 +01:00
|
|
|
self.send_and_test_stream_message('deploy', expected_topic, expected_message,
|
2016-09-20 22:51:11 +02:00
|
|
|
content_type="application/x-www-form-urlencoded")
|
|
|
|
|
2020-03-14 13:12:03 +01:00
|
|
|
# Tests For Semaphore 2.0
|
|
|
|
|
|
|
|
def test_semaphore2_push(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications/rw/webhook_impl" # repo/branch
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/acabe58e-4bcc-4d39-be06-e98d71917703) pipeline **stopped**:
|
|
|
|
* **Commit**: [(2d9f5fc)](https://github.com/renderedtext/notifications/commit/2d9f5fcec1ca7c68fa7bd44dd58ec4ff65814563) Implement webhooks for SemaphoreCI
|
|
|
|
* **Branch**: rw/webhook_impl
|
|
|
|
* **Author**: [radwo](https://github.com/radwo)
|
|
|
|
""".strip()
|
|
|
|
self.send_and_test_stream_message('push', expected_topic, expected_message,
|
|
|
|
content_type="application/json")
|
|
|
|
|
|
|
|
def test_semaphore2_push_non_gh_repo(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications/rw/webhook_impl" # repo/branch
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/acabe58e-4bcc-4d39-be06-e98d71917703) pipeline **stopped**:
|
|
|
|
* **Commit**: (2d9f5fc) Implement webhooks for SemaphoreCI
|
|
|
|
* **Branch**: rw/webhook_impl
|
|
|
|
* **Author**: radwo
|
|
|
|
""".strip()
|
|
|
|
with patch('zerver.webhooks.semaphore.view.is_github_repo', return_value=False):
|
|
|
|
self.send_and_test_stream_message('push', expected_topic, expected_message,
|
|
|
|
content_type="application/json")
|
|
|
|
|
|
|
|
def test_semaphore_pull_request(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications/test-notifications"
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/84383f37-d025-4811-b719-61c6acc92a1e) pipeline **failed**:
|
|
|
|
* **Pull Request**: [Testing PR notifications](https://github.com/renderedtext/notifications/pull/3)
|
|
|
|
* **Branch**: test-notifications
|
|
|
|
* **Author**: [radwo](https://github.com/radwo)
|
|
|
|
""".strip()
|
|
|
|
self.send_and_test_stream_message('pull_request', expected_topic, expected_message,
|
|
|
|
content_type="application/json")
|
|
|
|
|
|
|
|
def test_semaphore_pull_request_non_gh_repo(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications/test-notifications"
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/84383f37-d025-4811-b719-61c6acc92a1e) pipeline **failed**:
|
|
|
|
* **Pull Request**: Testing PR notifications (#3)
|
|
|
|
* **Branch**: test-notifications
|
|
|
|
* **Author**: radwo
|
|
|
|
""".strip()
|
|
|
|
with patch('zerver.webhooks.semaphore.view.is_github_repo', return_value=False):
|
|
|
|
self.send_and_test_stream_message('pull_request', expected_topic, expected_message,
|
|
|
|
content_type="application/json")
|
|
|
|
|
|
|
|
def test_semaphore_tag(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications"
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/a8704319-2422-4828-9b11-6b2afa3554e6) pipeline **stopped**:
|
|
|
|
* **Tag**: [v1.0.1](https://github.com/renderedtext/notifications/tree/v1.0.1)
|
|
|
|
* **Author**: [radwo](https://github.com/radwo)
|
|
|
|
""".strip()
|
|
|
|
self.send_and_test_stream_message('tag', expected_topic, expected_message,
|
|
|
|
content_type="application/json")
|
|
|
|
|
|
|
|
def test_semaphore_tag_non_gh_repo(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications"
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/a8704319-2422-4828-9b11-6b2afa3554e6) pipeline **stopped**:
|
|
|
|
* **Tag**: v1.0.1
|
|
|
|
* **Author**: radwo
|
|
|
|
""".strip()
|
|
|
|
with patch('zerver.webhooks.semaphore.view.is_github_repo', return_value=False):
|
|
|
|
self.send_and_test_stream_message('tag', expected_topic, expected_message,
|
|
|
|
content_type="application/json")
|
|
|
|
|
|
|
|
def test_semaphore_unknown_event(self) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_topic = "notifications"
|
2020-03-14 13:12:03 +01:00
|
|
|
expected_message = """
|
|
|
|
[Notifications](https://semaphore.semaphoreci.com/workflows/a8704319-2422-4828-9b11-6b2afa3554e6) pipeline **stopped** for unknown event
|
|
|
|
""".strip()
|
|
|
|
with patch('zerver.webhooks.semaphore.tests.SemaphoreHookTests.get_body', self.get_unknown_event):
|
|
|
|
self.send_and_test_stream_message('tag', expected_topic, expected_message,
|
|
|
|
content_type="application/json")
|
|
|
|
|
2018-05-10 19:34:01 +02:00
|
|
|
def get_body(self, fixture_name: str) -> str:
|
2018-04-20 03:57:21 +02:00
|
|
|
return self.webhook_fixture_data("semaphore", fixture_name, file_type="json")
|
2020-03-14 13:12:03 +01:00
|
|
|
|
|
|
|
def get_unknown_event(self, fixture_name: str) -> str:
|
|
|
|
"""Return modified payload with revision.reference_type changed"""
|
|
|
|
fixture_data = ujson.loads(self.webhook_fixture_data("semaphore", fixture_name, file_type="json"))
|
|
|
|
fixture_data['revision']['reference_type'] = 'unknown'
|
|
|
|
return fixture_data
|