Debian Codegraph @

tests/test_plugin_stackfield.py

raw symbols SCIP
1 # BSD 2-Clause License
2 #
3 # Apprise - Push Notification Library.
4 # Copyright (c) 2026, Chris Caron <[email protected]>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27
28 # Disable logging for a cleaner testing output
29 import logging
30 from unittest import mock
31 from urllib.parse import urlparse
32
33 from helpers import AppriseURLTester
34 import pytest
35 import requests
36
37 from apprise import Apprise
38 from apprise.plugins.stackfield import NotifyStackfield
39
40 logging.disable(logging.CRITICAL)
41
42 # A valid Stackfield webhook token (UUID format)
43 TEST_TOKEN = "11111111-2222-3333-4444-555555555555"
44
45 apprise_url_tests = (
46 # Missing token
47 (
48 "stackfield://",
49 {
50 "instance": TypeError,
51 },
52 ),
53 # Invalid token -- not a UUID
54 (
55 "stackfield://not-a-valid-uuid",
56 {
57 "instance": TypeError,
58 },
59 ),
60 # Invalid token -- wrong UUID length
61 (
62 "stackfield://11111111-2222-3333-4444-55555555555",
63 {
64 "instance": TypeError,
65 },
66 ),
67 # Valid token in URL path
68 (
69 f"stackfield://{TEST_TOKEN}",
70 {
71 "instance": NotifyStackfield,
72 "privacy_url": "stackfield://****/",
73 },
74 ),
75 # Valid token via ?token= query parameter
76 (
77 f"stackfield://?token={TEST_TOKEN}",
78 {
79 "instance": NotifyStackfield,
80 "privacy_url": "stackfield://****/",
81 },
82 ),
83 # Native Stackfield webhook URL
84 (
85 f"https://www.stackfield.com/apiwh/{TEST_TOKEN}",
86 {
87 "instance": NotifyStackfield,
88 },
89 ),
90 # Native URL without www prefix
91 (
92 f"https://stackfield.com/apiwh/{TEST_TOKEN}",
93 {
94 "instance": NotifyStackfield,
95 },
96 ),
97 # HTTP 500 response
98 (
99 f"stackfield://{TEST_TOKEN}",
100 {
101 "instance": NotifyStackfield,
102 "response": False,
103 "requests_response_code": requests.codes.internal_server_error,
104 },
105 ),
106 # Unknown HTTP error code
107 (
108 f"stackfield://{TEST_TOKEN}",
109 {
110 "instance": NotifyStackfield,
111 "response": False,
112 "requests_response_code": 999,
113 },
114 ),
115 # RequestException
116 (
117 "stackfield://abcdef01-2345-6789-abcd-ef0123456789",
118 {
119 "instance": NotifyStackfield,
120 "test_requests_exceptions": True,
121 },
122 ),
123 )
124
125
126 def test_plugin_stackfield_urls():
127 """Run the standard AppriseURLTester against all URL test cases."""
128 AppriseURLTester(tests=apprise_url_tests).run_all()
129
130
131 def test_plugin_stackfield_init():
132 """Test NotifyStackfield init, url(), and url_identifier."""
133
134 # Valid instantiation
135 obj = NotifyStackfield(token=TEST_TOKEN)
136 assert isinstance(obj, NotifyStackfield)
137
138 # url_identifier returns (schema, token)
139 assert obj.url_identifier == ("stackfield", TEST_TOKEN)
140
141 # url() round-trips correctly
142 url = obj.url()
143 assert url.startswith("stackfield://")
144 assert TEST_TOKEN in url
145
146 # privacy URL masks the token
147 privacy = obj.url(privacy=True)
148 assert TEST_TOKEN not in privacy
149 assert "stackfield://" in privacy
150
151 # Round-trip: parse_url(url()) re-creates the same object
152 result = NotifyStackfield.parse_url(obj.url())
153 assert result is not None
154 obj2 = NotifyStackfield(**result)
155 assert obj2.url_identifier == obj.url_identifier
156
157 # Missing token
158 with pytest.raises(TypeError):
159 NotifyStackfield(token=None)
160
161 # Empty token
162 with pytest.raises(TypeError):
163 NotifyStackfield(token="")
164
165 # Invalid token format (not a UUID)
166 with pytest.raises(TypeError):
167 NotifyStackfield(token="not-a-valid-uuid-at-all")
168
169 # Wrong UUID length (too short)
170 with pytest.raises(TypeError):
171 NotifyStackfield(token="11111111-2222-3333-4444-55555555555")
172
173
174 @mock.patch("requests.post")
175 def test_plugin_stackfield_send(mock_post):
176 """Test send() HTTP success, HTTP error, and RequestException paths."""
177
178 def _mk_resp(code=requests.codes.ok):
179 r = mock.Mock()
180 r.status_code = code
181 r.content = b""
182 return r
183
184 obj = NotifyStackfield(token=TEST_TOKEN)
185
186 # Successful notification
187 mock_post.return_value = _mk_resp(requests.codes.ok)
188 assert obj.send(body="Hello, Stackfield!") is True
189 assert mock_post.call_count == 1
190
191 # Verify POST target hostname
192 call_url = mock_post.call_args[0][0]
193 assert urlparse(call_url).hostname == "www.stackfield.com"
194
195 mock_post.reset_mock()
196
197 # HTTP 500 -> send() returns False
198 mock_post.return_value = _mk_resp(requests.codes.internal_server_error)
199 assert obj.send(body="test") is False
200
201 mock_post.reset_mock()
202
203 # Unknown HTTP code 999 -> send() returns False
204 mock_post.return_value = _mk_resp(999)
205 assert obj.send(body="test") is False
206
207 mock_post.reset_mock()
208
209 # RequestException -> send() returns False
210 mock_post.side_effect = requests.RequestException("connection error")
211 assert obj.send(body="test") is False
212
213
214 def test_plugin_stackfield_url_parsing():
215 """Test parse_url() and parse_native_url() edge cases."""
216
217 # Token from URL path (host position)
218 result = NotifyStackfield.parse_url(f"stackfield://{TEST_TOKEN}")
219 assert result is not None
220 assert result["token"] == TEST_TOKEN
221
222 # Token from ?token= query string
223 result = NotifyStackfield.parse_url(f"stackfield://?token={TEST_TOKEN}")
224 assert result is not None
225 assert result["token"] == TEST_TOKEN
226
227 # ?token= wins over host position when both are present
228 other = "abcdef01-2345-6789-abcd-ef0123456789"
229 result = NotifyStackfield.parse_url(
230 f"stackfield://{TEST_TOKEN}?token={other}"
231 )
232 assert result is not None
233 assert result["token"] == other
234
235 # parse_native_url: full https://www.stackfield.com/apiwh/TOKEN
236 result = NotifyStackfield.parse_native_url(
237 f"https://www.stackfield.com/apiwh/{TEST_TOKEN}"
238 )
239 assert result is not None
240 assert result["token"] == TEST_TOKEN
241
242 # parse_native_url: without www
243 result = NotifyStackfield.parse_native_url(
244 f"https://stackfield.com/apiwh/{TEST_TOKEN}"
245 )
246 assert result is not None
247 assert result["token"] == TEST_TOKEN
248
249 # parse_native_url: http variant
250 result = NotifyStackfield.parse_native_url(
251 f"http://www.stackfield.com/apiwh/{TEST_TOKEN}"
252 )
253 assert result is not None
254 assert result["token"] == TEST_TOKEN
255
256 # parse_native_url: invalid token format -> returns None
257 result = NotifyStackfield.parse_native_url(
258 "https://www.stackfield.com/apiwh/not-a-valid-uuid"
259 )
260 assert result is None
261
262 # parse_native_url: unrelated URL -> returns None
263 result = NotifyStackfield.parse_native_url(
264 "https://example.com/webhook/12345"
265 )
266 assert result is None
267
268
269 @mock.patch("requests.post")
270 def test_plugin_stackfield_apprise_integration(mock_post):
271 """Test that Apprise correctly loads and fires a Stackfield URL."""
272
273 r = mock.Mock()
274 r.status_code = requests.codes.ok
275 r.content = b""
276 mock_post.return_value = r
277
278 a = Apprise()
279 assert a.add(f"stackfield://{TEST_TOKEN}") is True
280 assert a.notify(title="Test", body="Integration test") is True
281 assert mock_post.call_count == 1