1+ import pytest
2+ from unittest .mock import patch , MagicMock
3+
4+ from use_notify .channels .chanify import Chanify
5+
6+
7+ @pytest .fixture
8+ def chanify_config ():
9+ return {
10+ "token" : "your_token"
11+ }
12+
13+
14+ @pytest .fixture
15+ def custom_chanify_config ():
16+ return {
17+ "token" : "your_token" ,
18+ "base_url" : "https://chanify.example.com"
19+ }
20+
21+
22+ def test_chanify_send (chanify_config ):
23+ # Create a mock for httpx.Client
24+ mock_client = MagicMock ()
25+ mock_response = MagicMock ()
26+ mock_client .return_value .__enter__ .return_value .post .return_value = mock_response
27+
28+ # Create Chanify instance
29+ chanify = Chanify (chanify_config )
30+
31+ # Mock the httpx.Client
32+ with patch ('httpx.Client' , mock_client ):
33+ chanify .send ("Test Content" , "Test Title" )
34+
35+ # Verify the request was made correctly
36+ mock_client .return_value .__enter__ .return_value .post .assert_called_once ()
37+
38+ # Get the call arguments
39+ call_args = mock_client .return_value .__enter__ .return_value .post .call_args
40+ url = call_args [0 ][0 ]
41+ kwargs = call_args [1 ]
42+
43+ # Verify URL and headers
44+ assert url == "https://api.chanify.net/v1/sender/your_token"
45+ assert kwargs ["headers" ]["Content-Type" ] == "application/x-www-form-urlencoded"
46+
47+ # Verify payload
48+ payload = kwargs ["data" ]
49+ assert payload ["text" ] == "Test Title\n Test Content"
50+
51+
52+ @pytest .mark .asyncio
53+ async def test_chanify_send_async (chanify_config ):
54+ # Create a mock for httpx.AsyncClient
55+ mock_client = MagicMock ()
56+ mock_response = MagicMock ()
57+ mock_client .return_value .__aenter__ .return_value .post .return_value = mock_response
58+
59+ # Create Chanify instance
60+ chanify = Chanify (chanify_config )
61+
62+ # Mock the httpx.AsyncClient
63+ with patch ('httpx.AsyncClient' , mock_client ):
64+ await chanify .send_async ("Test Content" , "Test Title" )
65+
66+ # Verify the request was made correctly
67+ mock_client .return_value .__aenter__ .return_value .post .assert_called_once ()
68+
69+ # Get the call arguments
70+ call_args = mock_client .return_value .__aenter__ .return_value .post .call_args
71+ url = call_args [0 ][0 ]
72+ kwargs = call_args [1 ]
73+
74+ # Verify URL and headers
75+ assert url == "https://api.chanify.net/v1/sender/your_token"
76+ assert kwargs ["headers" ]["Content-Type" ] == "application/x-www-form-urlencoded"
77+
78+ # Verify payload
79+ payload = kwargs ["data" ]
80+ assert payload ["text" ] == "Test Title\n Test Content"
81+
82+
83+ def test_chanify_custom_url (custom_chanify_config ):
84+ # Create a mock for httpx.Client
85+ mock_client = MagicMock ()
86+ mock_response = MagicMock ()
87+ mock_client .return_value .__enter__ .return_value .post .return_value = mock_response
88+
89+ # Create Chanify instance with custom base URL
90+ chanify = Chanify (custom_chanify_config )
91+
92+ # Mock the httpx.Client
93+ with patch ('httpx.Client' , mock_client ):
94+ chanify .send ("Test Content" , "Test Title" )
95+
96+ # Verify the request was made correctly
97+ mock_client .return_value .__enter__ .return_value .post .assert_called_once ()
98+
99+ # Get the call arguments
100+ call_args = mock_client .return_value .__enter__ .return_value .post .call_args
101+ url = call_args [0 ][0 ]
102+
103+ # Verify custom URL is used
104+ assert url == "https://chanify.example.com/v1/sender/your_token"
105+
106+
107+ def test_chanify_url_with_trailing_slash ():
108+ # Config with trailing slash in base_url
109+ config = {
110+ "token" : "your_token" ,
111+ "base_url" : "https://chanify.example.com/"
112+ }
113+
114+ # Create a mock for httpx.Client
115+ mock_client = MagicMock ()
116+ mock_response = MagicMock ()
117+ mock_client .return_value .__enter__ .return_value .post .return_value = mock_response
118+
119+ # Create Chanify instance
120+ chanify = Chanify (config )
121+
122+ # Mock the httpx.Client
123+ with patch ('httpx.Client' , mock_client ):
124+ chanify .send ("Test Content" )
125+
126+ # Get the call arguments
127+ call_args = mock_client .return_value .__enter__ .return_value .post .call_args
128+ url = call_args [0 ][0 ]
129+
130+ # Verify URL doesn't have double slashes
131+ assert url == "https://chanify.example.com/v1/sender/your_token"
0 commit comments