|
1 |
| -from unittest.mock import AsyncMock |
| 1 | +from unittest.mock import AsyncMock, MagicMock |
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 | from prefect_slack import SlackCredentials, SlackWebhook
|
5 | 5 | from slack_sdk.web.async_client import AsyncWebClient
|
6 |
| -from slack_sdk.webhook.async_client import AsyncWebhookClient, WebhookResponse |
| 6 | +from slack_sdk.webhook.async_client import AsyncWebhookClient |
| 7 | +from slack_sdk.webhook.webhook_response import WebhookResponse |
7 | 8 |
|
8 | 9 |
|
9 | 10 | def test_slack_credentials():
|
@@ -63,3 +64,52 @@ async def test_slack_webhook_block_handles_raise_on_failure(
|
63 | 64 | with pytest.raises(NotificationError, match="Failed to send message: woops"):
|
64 | 65 | with block.raise_on_failure():
|
65 | 66 | await block.notify("hello", "world")
|
| 67 | + |
| 68 | + |
| 69 | +def test_slack_webhook_sync_notify(monkeypatch): |
| 70 | + """Test the sync notify path""" |
| 71 | + mock_client = MagicMock() |
| 72 | + mock_client.send.return_value = WebhookResponse( |
| 73 | + url="http://test", status_code=200, body="ok", headers={} |
| 74 | + ) |
| 75 | + |
| 76 | + webhook = SlackWebhook(url="http://test") |
| 77 | + monkeypatch.setattr(webhook, "get_client", MagicMock(return_value=mock_client)) |
| 78 | + |
| 79 | + webhook.notify("test message") |
| 80 | + mock_client.send.assert_called_once_with(text="test message") |
| 81 | + |
| 82 | + |
| 83 | +async def test_slack_webhook_async_notify(monkeypatch): |
| 84 | + """Test the async notify path""" |
| 85 | + mock_client = MagicMock() |
| 86 | + mock_client.send = AsyncMock( |
| 87 | + return_value=WebhookResponse( |
| 88 | + url="http://test", status_code=200, body="ok", headers={} |
| 89 | + ) |
| 90 | + ) |
| 91 | + |
| 92 | + webhook = SlackWebhook(url="http://test") |
| 93 | + monkeypatch.setattr(webhook, "get_client", MagicMock(return_value=mock_client)) |
| 94 | + |
| 95 | + await webhook.notify_async("test message") |
| 96 | + mock_client.send.assert_called_once_with(text="test message") |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.parametrize("message", ["test message 1", "test message 2"]) |
| 100 | +async def test_slack_webhook_notify_async_dispatch(monkeypatch, message): |
| 101 | + """Test that async_dispatch properly handles both sync and async contexts""" |
| 102 | + |
| 103 | + mock_response = WebhookResponse( |
| 104 | + url="http://test", status_code=200, body="ok", headers={} |
| 105 | + ) |
| 106 | + |
| 107 | + mock_client = MagicMock() |
| 108 | + mock_client.send = AsyncMock(return_value=mock_response) |
| 109 | + |
| 110 | + webhook = SlackWebhook(url="http://test") |
| 111 | + monkeypatch.setattr(webhook, "get_client", lambda sync_client=False: mock_client) |
| 112 | + |
| 113 | + # Test notification |
| 114 | + await webhook.notify(message) |
| 115 | + mock_client.send.assert_called_once_with(text=message) |
0 commit comments