4
4
from dpytools .http_clients .base import BaseHttpClient
5
5
from dpytools .slack .slack import SlackNotifier
6
6
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 ):
24
9
"""
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
26
11
"""
27
- mock_getenv . return_value = 'http://example.com'
12
+ webhook_url = 'http://example.com'
28
13
mock_response = MagicMock (Response )
29
14
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' })
31
19
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' })
36
21
37
- @patch ('os.getenv' )
38
22
@patch .object (BaseHttpClient , 'post' )
39
- def test_notify (mock_post , mock_getenv ):
23
+ def test_msg_str (mock_post ):
40
24
"""
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
42
26
"""
43
- mock_getenv . return_value = 'http://example.com'
27
+ webhook_url = 'http://example.com'
44
28
mock_response = MagicMock (Response )
45
29
mock_response .status_code = 200
46
30
mock_post .return_value = mock_response
47
31
48
- notifier = SlackNotifier ()
49
- notifier .notify ({ 'text' : ' Test message'} )
32
+ notifier = SlackNotifier (webhook_url )
33
+ notifier .msg_str ( ' Test message' )
50
34
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