Skip to content

Commit 5c8fe55

Browse files
committed
Made the requested changes
1 parent 2813ec6 commit 5c8fe55

File tree

2 files changed

+34
-44
lines changed

2 files changed

+34
-44
lines changed

dpytools/slack/slack.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
import os
21
import logging
32
from dpytools.http_clients.base import BaseHttpClient
43

54
class SlackNotifier:
65

7-
def __init__(self):
8-
self.webhook_url = os.getenv("SLACK_WEBHOOK_URL")
9-
if not self.webhook_url:
10-
raise ValueError('SLACK_WEBHOOK_URL is not set')
6+
def __init__(self, webhook_url):
7+
if not webhook_url:
8+
raise ValueError('webhook_url is not set')
9+
self.webhook_url = webhook_url
1110
self.http_client = BaseHttpClient()
12-
self.validate_webhook_url()
1311

14-
def validate_webhook_url(self):
15-
response = self.http_client.get(self.webhook_url)
16-
if response.status_code != 200:
17-
logging.error(f'Invalid SLACK_WEBHOOK_URL: {response.status_code}')
18-
raise ValueError('Invalid SLACK_WEBHOOK_URL')
12+
def notify(self, msg_dict: dict):
13+
"""
14+
Send a message to the Slack webhook.
1915
20-
def notify(self, msg: dict):
16+
The msg_dict parameter should be a dictionary that matches the
17+
structure documented at https://api.slack.com/messaging/webhooks
18+
"""
2119
try:
22-
response = self.http_client.post(self.webhook_url, json=msg)
20+
response = self.http_client.post(self.webhook_url, json=msg_dict)
2321
response.raise_for_status()
2422
except Exception as e:
2523
logging.error(f'Failed to send notification: {e}')
24+
25+
def msg_str(self, msg: str):
26+
"""
27+
Send a string message to the Slack webhook.
28+
29+
The msg parameter is wrapped into a dictionary before being sent.
30+
"""
31+
self.notify({'text': msg})

tests/test_slack.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,32 @@
44
from dpytools.http_clients.base import BaseHttpClient
55
from dpytools.slack.slack import SlackNotifier
66

7-
@patch('os.getenv')
8-
@patch.object(BaseHttpClient, 'get')
9-
def test_validate_webhook_url(mock_get, mock_getenv):
10-
"""
11-
Test that the validate_webhook_url method raises an exception for invalid URLs
12-
"""
13-
mock_getenv.return_value = 'http://example.com'
14-
mock_response = MagicMock(Response)
15-
mock_response.status_code = 404
16-
mock_get.return_value = mock_response
17-
18-
with pytest.raises(ValueError):
19-
notifier = SlackNotifier()
20-
21-
@patch('os.getenv')
22-
@patch.object(BaseHttpClient, 'get')
23-
def test_validate_webhook_url_success(mock_get, mock_getenv):
7+
@patch.object(BaseHttpClient, 'post')
8+
def test_notify(mock_post):
249
"""
25-
Test that the validate_webhook_url method does not raise an exception for valid URLs
10+
Test that the notify method sends a POST request
2611
"""
27-
mock_getenv.return_value = 'http://example.com'
12+
webhook_url = 'http://example.com'
2813
mock_response = MagicMock(Response)
2914
mock_response.status_code = 200
30-
mock_get.return_value = mock_response
15+
mock_post.return_value = mock_response
16+
17+
notifier = SlackNotifier(webhook_url)
18+
notifier.notify({'text': 'Test message'})
3119

32-
try:
33-
notifier = SlackNotifier()
34-
except ValueError:
35-
pytest.fail("Unexpected ValueError ..")
20+
mock_post.assert_called_once_with(webhook_url, json={'text': 'Test message'})
3621

37-
@patch('os.getenv')
3822
@patch.object(BaseHttpClient, 'post')
39-
def test_notify(mock_post, mock_getenv):
23+
def test_msg_str(mock_post):
4024
"""
41-
Test that the notify method sends a POST request
25+
Test that the msg_str method sends a POST request with a string message
4226
"""
43-
mock_getenv.return_value = 'http://example.com'
27+
webhook_url = 'http://example.com'
4428
mock_response = MagicMock(Response)
4529
mock_response.status_code = 200
4630
mock_post.return_value = mock_response
4731

48-
notifier = SlackNotifier()
49-
notifier.notify({'text': 'Test message'})
32+
notifier = SlackNotifier(webhook_url)
33+
notifier.msg_str('Test message')
5034

51-
mock_post.assert_called_once_with('http://example.com', json={'text': 'Test message'})
35+
mock_post.assert_called_once_with(webhook_url, json={'text': 'Test message'})

0 commit comments

Comments
 (0)