返回 last30days-skill
test_watchlist_delivery.py
根目录 / tests / test_watchlist_delivery.py
1 """Tests for watchlist.py delivery functions (PR #86 feature)."""
2
3 from unittest.mock import patch
4
5 import pytest
6
7 import watchlist
8 from lib.http import HTTPError
9
10 # === Tests for _format_delivery_message() ===
11
12
13 def test_format_message_announce_mode():
14 """Test announce mode formatting (default mode with emoji)."""
15 message = watchlist._format_delivery_message(
16 "Test Topic", {"new": 5, "updated": 2}, "announce"
17 )
18
19 assert "📰" in message
20 assert "Test Topic" in message
21 assert "5 new" in message
22 assert "2 updated" in message
23
24
25 def test_format_message_silent_mode():
26 """Test silent mode formatting (no emoji)."""
27 message = watchlist._format_delivery_message(
28 "Test Topic", {"new": 5, "updated": 2}, "silent"
29 )
30
31 assert "📰" not in message
32 assert "Test Topic" in message
33 assert "5 new" in message
34
35
36 def test_format_message_default_mode():
37 """Test default mode formatting."""
38 message = watchlist._format_delivery_message(
39 "Test Topic", {"new": 5, "updated": 2}, "default"
40 )
41
42 assert "complete" in message.lower()
43 assert "Test Topic" in message
44
45
46 def test_format_message_handles_zero_counts():
47 """Test formatting with zero counts."""
48 message = watchlist._format_delivery_message(
49 "Test Topic", {"new": 0, "updated": 0}, "announce"
50 )
51
52 assert "0 new" in message
53 assert "0 updated" in message
54
55 # === Tests for _send_slack_webhook() ===
56
57 @patch('watchlist.http.post_raw')
58
59
60 def test_send_slack_webhook_format(mock_post):
61 """Test that Slack webhook uses correct format."""
62 watchlist._send_slack_webhook(
63 "https://hooks.slack.com/services/TEST",
64 "Test message"
65 )
66
67 assert mock_post.called
68 call_args = mock_post.call_args
69
70 assert call_args[0][0] == "https://hooks.slack.com/services/TEST"
71 assert call_args[1]["json_data"] == {"text": "Test message"}
72 assert call_args[1]["timeout"] == 10
73
74 @patch('watchlist.http.post_raw')
75
76
77 def test_send_slack_webhook_raises_on_error(mock_post):
78 """Test that Slack webhook raises on HTTP error."""
79 mock_post.side_effect = HTTPError("HTTP 400", 400)
80
81 with pytest.raises(HTTPError, match="HTTP 400"):
82 watchlist._send_slack_webhook(
83 "https://hooks.slack.com/services/TEST",
84 "Test message"
85 )
86
87 # === Tests for _send_generic_webhook() ===
88
89 @patch('watchlist.http.post_raw')
90
91
92 def test_send_generic_webhook_format(mock_post):
93 """Test that generic webhook uses correct format."""
94 watchlist._send_generic_webhook(
95 "https://webhook.example.com/hook",
96 "Test message"
97 )
98
99 assert mock_post.called
100 call_args = mock_post.call_args
101
102 assert call_args[0][0] == "https://webhook.example.com/hook"
103
104 json_data = call_args[1]["json_data"]
105 assert json_data["message"] == "Test message"
106 assert json_data["source"] == "last30days"
107 assert "timestamp" in json_data
108 assert isinstance(json_data["timestamp"], float)
109
110 @patch('watchlist.http.post_raw')
111
112
113 def test_send_generic_webhook_raises_on_error(mock_post):
114 """Test that generic webhook raises on HTTP error."""
115 mock_post.side_effect = HTTPError("HTTP 500", 500)
116
117 with pytest.raises(HTTPError, match="HTTP 500"):
118 watchlist._send_generic_webhook(
119 "https://webhook.example.com/hook",
120 "Test message"
121 )
122
123 # === Tests for _deliver_findings() ===
124
125 @patch('watchlist.store.get_setting')
126 @patch('watchlist.http.post_raw')
127
128
129 def test_deliver_findings_sends_when_new_greater_than_zero(mock_post, mock_get_setting):
130 """Test that delivery fires when new > 0."""
131 mock_get_setting.side_effect = lambda key, default="": {
132 "delivery_channel": "https://webhook.example.com/test",
133 "delivery_mode": "announce",
134 }.get(key, default)
135
136 watchlist._deliver_findings("Test Topic", {"new": 5, "updated": 2})
137
138 assert mock_post.called
139
140 @patch('watchlist.store.get_setting')
141 @patch('watchlist.http.post_raw')
142
143
144 def test_deliver_findings_skips_when_new_is_zero(mock_post, mock_get_setting):
145 """Test that delivery is skipped when new=0."""
146 mock_get_setting.side_effect = lambda key, default="": {
147 "delivery_channel": "https://webhook.example.com/test",
148 "delivery_mode": "announce",
149 }.get(key, default)
150
151 watchlist._deliver_findings("Test Topic", {"new": 0, "updated": 5})
152
153 assert not mock_post.called
154
155 @patch('watchlist.store.get_setting')
156 @patch('watchlist.http.post_raw')
157
158
159 def test_deliver_findings_skips_when_channel_empty(mock_post, mock_get_setting):
160 """Test that delivery is skipped when delivery_channel is empty."""
161 mock_get_setting.side_effect = lambda key, default="": {
162 "delivery_channel": "",
163 "delivery_mode": "announce",
164 }.get(key, default)
165
166 watchlist._deliver_findings("Test Topic", {"new": 5, "updated": 2})
167
168 assert not mock_post.called
169
170 @patch('watchlist.store.get_setting')
171 @patch('watchlist.http.post_raw')
172
173
174 def test_deliver_findings_uses_slack_format_for_slack_urls(mock_post, mock_get_setting):
175 """Test that Slack URLs trigger Slack-specific format."""
176 mock_get_setting.side_effect = lambda key, default="": {
177 "delivery_channel": "https://hooks.slack.com/services/TEST",
178 "delivery_mode": "announce",
179 }.get(key, default)
180
181 watchlist._deliver_findings("Test Topic", {"new": 5, "updated": 2})
182
183 json_data = mock_post.call_args[1]["json_data"]
184 assert "text" in json_data
185 assert "Test Topic" in json_data["text"]
186
187 @patch('watchlist.store.get_setting')
188 @patch('watchlist.http.post_raw')
189
190
191 def test_deliver_findings_uses_generic_format_for_other_urls(mock_post, mock_get_setting):
192 """Test that non-Slack URLs trigger generic format."""
193 mock_get_setting.side_effect = lambda key, default="": {
194 "delivery_channel": "https://webhook.example.com/test",
195 "delivery_mode": "announce",
196 }.get(key, default)
197
198 watchlist._deliver_findings("Test Topic", {"new": 5, "updated": 2})
199
200 json_data = mock_post.call_args[1]["json_data"]
201 assert "message" in json_data
202 assert "source" in json_data
203 assert "timestamp" in json_data
204
205 @patch('watchlist.store.get_setting')
206 @patch('watchlist.http.post_raw')
207
208
209 def test_deliver_findings_handles_failure_gracefully(mock_post, mock_get_setting, capsys):
210 """Test that delivery failures don't crash the process."""
211 mock_get_setting.side_effect = lambda key, default="": {
212 "delivery_channel": "https://webhook.example.com/test",
213 "delivery_mode": "announce",
214 }.get(key, default)
215
216 credential = "dummy-webhook-failure-credential"
217 mock_post.side_effect = HTTPError(f"HTTP 500 {credential}", 500)
218
219 # Should not raise, just log to stderr
220 watchlist._deliver_findings("Test Topic", {"new": 5, "updated": 2})
221
222 captured = capsys.readouterr()
223 assert "Delivery failed: HTTP 500" in captured.err
224 assert credential not in captured.err
225
226
227 def test_webhook_transport_keeps_url_out_of_diagnostics():
228 url = "https://hooks.slack.com/services/dummy-webhook-credential"
229 with patch("watchlist.http._open_request") as open_request, patch("watchlist.http.log") as log:
230 response = open_request.return_value.__enter__.return_value
231 response.status = 200
232 response.read.return_value = b"{}"
233
234 watchlist._send_slack_webhook(url, "Test message")
235
236 request, _timeout = open_request.call_args.args
237 assert request.full_url == url
238 assert log.call_args_list == []
239
240 @patch('watchlist.store.get_setting')
241 @patch('watchlist.http.post_raw')
242
243
244 def test_deliver_findings_rejects_non_https_slack_substring(mock_post, mock_get_setting, capsys):
245 """A non-https channel that merely contains 'hooks.slack.com' must not be
246 sent. The old substring match POSTed it in cleartext to whatever host the
247 URL actually named."""
248 mock_get_setting.side_effect = lambda key, default="": {
249 "delivery_channel": "http://evil.example/hooks.slack.com",
250 "delivery_mode": "announce",
251 }.get(key, default)
252
253 watchlist._deliver_findings("Test Topic", {"new": 5, "updated": 2})
254
255 assert not mock_post.called
256 assert "https://" in capsys.readouterr().err
257
258 @patch('watchlist.store.get_setting')
259 @patch('watchlist.http.post_raw')
260
261
262 def test_deliver_findings_slack_match_is_exact_host(mock_post, mock_get_setting):
263 """An https URL with 'hooks.slack.com' only in the path routes as generic,
264 not Slack — the match is on the exact hostname, not a substring."""
265 mock_get_setting.side_effect = lambda key, default="": {
266 "delivery_channel": "https://webhook.example.com/hooks.slack.com/x",
267 "delivery_mode": "announce",
268 }.get(key, default)
269
270 watchlist._deliver_findings("Test Topic", {"new": 5, "updated": 2})
271
272 json_data = mock_post.call_args[1]["json_data"]
273 assert "message" in json_data # generic payload shape
274 assert "text" not in json_data # not the Slack {"text": ...} shape
275
276 @patch('watchlist.store.get_setting')
277 @patch('watchlist.http.post_raw')
278
279
280 def test_deliver_findings_respects_delivery_mode(mock_post, mock_get_setting):
281 """Test that different delivery modes produce different messages."""
282 mock_get_setting.side_effect = lambda key, default="": {
283 "delivery_channel": "https://webhook.example.com/test",
284 "delivery_mode": "announce",
285 }.get(key, default)
286
287 watchlist._deliver_findings("Test Topic", {"new": 5, "updated": 2})
288
289 announce_message = mock_post.call_args[1]["json_data"]["message"]
290 assert "📰" in announce_message
291
292 mock_get_setting.side_effect = lambda key, default="": {
293 "delivery_channel": "https://webhook.example.com/test",
294 "delivery_mode": "silent",
295 }.get(key, default)
296
297 watchlist._deliver_findings("Test Topic", {"new": 5, "updated": 2})
298
299 silent_message = mock_post.call_args[1]["json_data"]["message"]
300 assert "📰" not in silent_message
301
302 if __name__ == "__main__":
303 pytest.main([__file__, "-v"])
304
304 lines PYTHON