Skip to content

Commit

Permalink
webhook: add regression tests for webhook content
Browse files Browse the repository at this point in the history
  • Loading branch information
bghira committed Sep 30, 2024
1 parent c071ec5 commit 97adf06
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions tests/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
class TestWebhookHandler(unittest.TestCase):
def setUp(self):
# Create a mock for the WebhookConfig
mock_config_instance = MagicMock(spec=WebhookConfig)
mock_config_instance.webhook_url = "http://example.com/webhook"
mock_config_instance.webhook_type = "discord"
mock_config_instance.log_level = "info"
mock_config_instance.message_prefix = "TestPrefix"
mock_config_instance.values = {
self.mock_config_instance = MagicMock(spec=WebhookConfig)
self.mock_config_instance.webhook_url = "http://example.com/webhook"
self.mock_config_instance.webhook_type = "discord"
self.mock_config_instance.log_level = "info"
self.mock_config_instance.message_prefix = "TestPrefix"
self.mock_config_instance.values = {
"webhook_url": "http://example.com/webhook",
"webhook_type": "discord",
"log_level": "info",
Expand All @@ -30,14 +30,20 @@ def setUp(self):
config_path="dummy_path",
accelerator=self.mock_accelerator,
project_name="TestProject",
mock_webhook_config=mock_config_instance,
mock_webhook_config=self.mock_config_instance,
)

@patch("requests.post")
def test_send_message_info_level(self, mock_post):
# Test sending a simple info level message
self.handler.send("Test message", message_level="info")
message = "Test message"
self.handler.send(message, message_level="info")
mock_post.assert_called_once()
# Capture the call arguments
args, kwargs = mock_post.call_args
# Assuming the message is sent in 'data' parameter
self.assertIn("json", kwargs)
self.assertIn(message, kwargs["json"].get("content"))

@patch("requests.post")
def test_debug_message_wont_send(self, mock_post):
Expand All @@ -56,12 +62,16 @@ def test_do_not_send_lower_than_configured_level(self, mock_post):
def test_send_with_images(self, mock_post):
# Test sending messages with images
image = Image.new("RGB", (60, 30), color="red")
self.handler.send(
"Test message with image", images=[image], message_level="info"
)
message = "Test message with image"
self.handler.send(message, images=[image], message_level="info")
args, kwargs = mock_post.call_args
self.assertIn("files", kwargs)
self.assertEqual(len(kwargs["files"]), 1)
# Check that the message is in the 'data' parameter
content = kwargs.get("json", {}).get("content", "")
self.assertIn(self.mock_config_instance.values.get("message_prefix"), content)
self.assertIn("json", kwargs, f"Check data for contents: {kwargs}")
self.assertIn(message, content)

@patch("requests.post")
def test_response_storage(self, mock_post):
Expand All @@ -72,6 +82,11 @@ def test_response_storage(self, mock_post):

self.handler.send("Test message", message_level="info", store_response=True)
self.assertEqual(self.handler.stored_response, mock_response.headers)
# Also check that the message is sent
args, kwargs = mock_post.call_args
content = kwargs.get("json", {}).get("content", "")
self.assertIn(self.mock_config_instance.values.get("message_prefix"), content)
self.assertIn("Test message", content)


if __name__ == "__main__":
Expand Down

0 comments on commit 97adf06

Please sign in to comment.