mirror of
https://github.com/zulip/zulip.git
synced 2025-10-28 18:43:52 +00:00
Almost all webhook tests use this helper, except a few webhooks that write to private streams. Being concise is important here, and the name `self.send_and_test_stream_message` always confused me, since it sounds you're sending a stream message, and it leaves out the webhook piece. We should consider renaming `send_and_test_private_message` to something like `check_webhook_private`, but I couldn't decide on a great name, and it's very rarely used. So for now I just made sure the docstrings of the two sibling functions reference each other.
41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
from unittest.mock import MagicMock, patch
|
|
|
|
from zerver.lib.test_classes import WebhookTestCase
|
|
|
|
|
|
class HarborHookTests(WebhookTestCase):
|
|
STREAM_NAME = "harbor"
|
|
URL_TEMPLATE = "/api/v1/external/harbor?api_key={api_key}&stream={stream}"
|
|
|
|
def test_push_image(self) -> None:
|
|
expected_topic = "example/test"
|
|
expected_message = """**admin** pushed image `example/test:latest`"""
|
|
self.check_webhook("push_image", expected_topic, expected_message)
|
|
|
|
@patch('zerver.lib.webhooks.common.check_send_webhook_message')
|
|
def test_delete_image_ignored(
|
|
self, check_send_webhook_message_mock: MagicMock) -> None:
|
|
self.url = self.build_webhook_url()
|
|
payload = self.get_body('delete_image')
|
|
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_scanning_completed(self) -> None:
|
|
expected_topic = "example/test"
|
|
|
|
expected_message = """
|
|
Image scan completed for `example/test:latest`. Vulnerabilities by severity:
|
|
|
|
* High: **12**
|
|
* Medium: **16**
|
|
* Low: **7**
|
|
* Unknown: **2**
|
|
* None: **131**
|
|
""".strip()
|
|
|
|
self.check_webhook("scanning_completed", expected_topic, expected_message)
|
|
|
|
def get_body(self, fixture_name: str) -> str:
|
|
return self.webhook_fixture_data("harbor", fixture_name, file_type="json")
|