2020-05-26 07:16:25 +02:00
|
|
|
from unittest.mock import MagicMock, patch
|
2019-10-20 02:12:00 +02:00
|
|
|
|
|
|
|
from zerver.lib.test_classes import WebhookTestCase
|
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2019-10-20 02:12:00 +02:00
|
|
|
class HarborHookTests(WebhookTestCase):
|
2024-05-04 22:02:50 +02:00
|
|
|
CHANNEL_NAME = "harbor"
|
2020-04-09 21:51:58 +02:00
|
|
|
URL_TEMPLATE = "/api/v1/external/harbor?api_key={api_key}&stream={stream}"
|
2021-06-26 09:18:33 +02:00
|
|
|
WEBHOOK_DIR_NAME = "harbor"
|
2019-10-20 02:12:00 +02:00
|
|
|
|
|
|
|
def test_push_image(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "example/test"
|
2019-10-20 02:12:00 +02:00
|
|
|
expected_message = """**admin** pushed image `example/test:latest`"""
|
2024-01-17 15:53:30 +01:00
|
|
|
self.check_webhook("push_image", expected_topic_name, expected_message)
|
2019-10-20 02:12:00 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@patch("zerver.lib.webhooks.common.check_send_webhook_message")
|
2021-02-12 08:19:30 +01:00
|
|
|
def test_delete_image_ignored(self, check_send_webhook_message_mock: MagicMock) -> None:
|
2019-10-20 02:12:00 +02:00
|
|
|
self.url = self.build_webhook_url()
|
2021-02-12 08:20:45 +01:00
|
|
|
payload = self.get_body("delete_image")
|
2019-10-20 02:12:00 +02:00
|
|
|
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:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "test/alpine/helm"
|
2019-10-20 02:12:00 +02:00
|
|
|
|
|
|
|
expected_message = """
|
2022-06-18 11:30:25 +02:00
|
|
|
Image scan completed for `test/alpine/helm:3.8.1`. Vulnerabilities by severity:
|
2019-10-20 02:12:00 +02:00
|
|
|
|
2022-06-18 11:30:25 +02:00
|
|
|
* High: **4**
|
|
|
|
* Unknown: **1**
|
2019-10-20 02:12:00 +02:00
|
|
|
""".strip()
|
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
self.check_webhook("scanning_completed", expected_topic_name, expected_message)
|
2022-06-18 11:30:25 +02:00
|
|
|
|
|
|
|
def test_scanning_completed_no_vulnerability(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "test123/test-image"
|
2022-06-18 11:30:25 +02:00
|
|
|
|
|
|
|
expected_message = """
|
|
|
|
Image scan completed for `test123/test-image:latest`. Vulnerabilities by severity:
|
|
|
|
|
|
|
|
None
|
|
|
|
""".strip()
|
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
self.check_webhook(
|
|
|
|
"scanning_completed_no_vulnerability", expected_topic_name, expected_message
|
|
|
|
)
|
2023-05-11 19:14:01 +02:00
|
|
|
|
|
|
|
def test_scanning_completed_no_tag(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "test/alpine/helm"
|
2023-05-11 19:14:01 +02:00
|
|
|
|
|
|
|
expected_message = """
|
|
|
|
Image scan completed for `test/alpine/helm@sha256:b50334049354ed01330403212605dce2f4676a4e787ed113506861d9cf3c5424`. Vulnerabilities by severity:
|
|
|
|
|
|
|
|
* High: **4**
|
|
|
|
* Unknown: **1**
|
|
|
|
""".strip()
|
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
self.check_webhook("scanning_completed_no_tag", expected_topic_name, expected_message)
|