| 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.exception import AppriseImproperlyConfigured |
| 39 |
from apprise.plugins.stackfield import NotifyStackfield |
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
# A valid Stackfield webhook token (UUID format) |
| 44 |
TEST_TOKEN = "11111111-2222-3333-4444-555555555555" |
| 45 |
|
| 46 |
apprise_url_tests = ( |
| 47 |
# Missing token |
| 48 |
( |
| 49 |
"stackfield://", |
| 50 |
{ |
| 51 |
"instance": AppriseImproperlyConfigured, |
| 52 |
}, |
| 53 |
), |
| 54 |
# Invalid token -- not a UUID |
| 55 |
( |
| 56 |
"stackfield://not-a-valid-uuid", |
| 57 |
{ |
| 58 |
"instance": AppriseImproperlyConfigured, |
| 59 |
}, |
| 60 |
), |
| 61 |
# Invalid token -- wrong UUID length |
| 62 |
( |
| 63 |
"stackfield://11111111-2222-3333-4444-55555555555", |
| 64 |
{ |
| 65 |
"instance": AppriseImproperlyConfigured, |
| 66 |
}, |
| 67 |
), |
| 68 |
# Valid token in URL path |
| 69 |
( |
| 70 |
f"stackfield://{TEST_TOKEN}", |
| 71 |
{ |
| 72 |
"instance": NotifyStackfield, |
| 73 |
"privacy_url": "stackfield://****/", |
| 74 |
}, |
| 75 |
), |
| 76 |
# Valid token via ?token= query parameter |
| 77 |
( |
| 78 |
f"stackfield://?token={TEST_TOKEN}", |
| 79 |
{ |
| 80 |
"instance": NotifyStackfield, |
| 81 |
"privacy_url": "stackfield://****/", |
| 82 |
}, |
| 83 |
), |
| 84 |
# Native Stackfield webhook URL |
| 85 |
( |
| 86 |
f"https://www.stackfield.com/apiwh/{TEST_TOKEN}", |
| 87 |
{ |
| 88 |
"instance": NotifyStackfield, |
| 89 |
}, |
| 90 |
), |
| 91 |
# Native URL without www prefix |
| 92 |
( |
| 93 |
f"https://stackfield.com/apiwh/{TEST_TOKEN}", |
| 94 |
{ |
| 95 |
"instance": NotifyStackfield, |
| 96 |
}, |
| 97 |
), |
| 98 |
# HTTP 500 response |
| 99 |
( |
| 100 |
f"stackfield://{TEST_TOKEN}", |
| 101 |
{ |
| 102 |
"instance": NotifyStackfield, |
| 103 |
"response": False, |
| 104 |
"requests_response_code": requests.codes.internal_server_error, |
| 105 |
}, |
| 106 |
), |
| 107 |
# Unknown HTTP error code |
| 108 |
( |
| 109 |
f"stackfield://{TEST_TOKEN}", |
| 110 |
{ |
| 111 |
"instance": NotifyStackfield, |
| 112 |
"response": False, |
| 113 |
"requests_response_code": 999, |
| 114 |
}, |
| 115 |
), |
| 116 |
# RequestException |
| 117 |
( |
| 118 |
"stackfield://abcdef01-2345-6789-abcd-ef0123456789", |
| 119 |
{ |
| 120 |
"instance": NotifyStackfield, |
| 121 |
"test_requests_exceptions": True, |
| 122 |
}, |
| 123 |
), |
| 124 |
) |
| 125 |
|
| 126 |
|
| 127 |
def test_plugin_stackfield_urls(): |
| 128 |
"""Run the standard AppriseURLTester against all URL test cases.""" |
| 129 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 130 |
|
| 131 |
|
| 132 |
def test_plugin_stackfield_init(): |
| 133 |
"""Test NotifyStackfield init, url(), and url_identifier.""" |
| 134 |
|
| 135 |
# Valid instantiation |
| 136 |
obj = NotifyStackfield(token=TEST_TOKEN) |
| 137 |
assert isinstance(obj, NotifyStackfield) |
| 138 |
|
| 139 |
# url_identifier returns (schema, token) |
| 140 |
assert obj.url_identifier == ("stackfield", TEST_TOKEN) |
| 141 |
|
| 142 |
# url() round-trips correctly |
| 143 |
url = obj.url() |
| 144 |
assert url.startswith("stackfield://") |
| 145 |
assert TEST_TOKEN in url |
| 146 |
|
| 147 |
# privacy URL masks the token |
| 148 |
privacy = obj.url(privacy=True) |
| 149 |
assert TEST_TOKEN not in privacy |
| 150 |
assert "stackfield://" in privacy |
| 151 |
|
| 152 |
# Round-trip: parse_url(url()) re-creates the same object |
| 153 |
result = NotifyStackfield.parse_url(obj.url()) |
| 154 |
assert result is not None |
| 155 |
obj2 = NotifyStackfield(**result) |
| 156 |
assert obj2.url_identifier == obj.url_identifier |
| 157 |
|
| 158 |
# Missing token |
| 159 |
with pytest.raises(AppriseImproperlyConfigured): |
| 160 |
NotifyStackfield(token=None) |
| 161 |
|
| 162 |
# Empty token |
| 163 |
with pytest.raises(AppriseImproperlyConfigured): |
| 164 |
NotifyStackfield(token="") |
| 165 |
|
| 166 |
# Invalid token format (not a UUID) |
| 167 |
with pytest.raises(AppriseImproperlyConfigured): |
| 168 |
NotifyStackfield(token="not-a-valid-uuid-at-all") |
| 169 |
|
| 170 |
# Wrong UUID length (too short) |
| 171 |
with pytest.raises(AppriseImproperlyConfigured): |
| 172 |
NotifyStackfield(token="11111111-2222-3333-4444-55555555555") |
| 173 |
|
| 174 |
|
| 175 |
@mock.patch("requests.post") |
| 176 |
def test_plugin_stackfield_send(mock_post): |
| 177 |
"""Test send() HTTP success, HTTP error, and RequestException paths.""" |
| 178 |
|
| 179 |
def _mk_resp(code=requests.codes.ok): |
| 180 |
r = mock.Mock() |
| 181 |
r.status_code = code |
| 182 |
r.content = b"" |
| 183 |
return r |
| 184 |
|
| 185 |
obj = NotifyStackfield(token=TEST_TOKEN) |
| 186 |
|
| 187 |
# Successful notification |
| 188 |
mock_post.return_value = _mk_resp(requests.codes.ok) |
| 189 |
assert obj.send(body="Hello, Stackfield!") is True |
| 190 |
assert mock_post.call_count == 1 |
| 191 |
|
| 192 |
# Verify POST target hostname |
| 193 |
call_url = mock_post.call_args[0][0] |
| 194 |
assert urlparse(call_url).hostname == "www.stackfield.com" |
| 195 |
|
| 196 |
mock_post.reset_mock() |
| 197 |
|
| 198 |
# HTTP 500 -> send() returns False |
| 199 |
mock_post.return_value = _mk_resp(requests.codes.internal_server_error) |
| 200 |
assert obj.send(body="test") is False |
| 201 |
|
| 202 |
mock_post.reset_mock() |
| 203 |
|
| 204 |
# Unknown HTTP code 999 -> send() returns False |
| 205 |
mock_post.return_value = _mk_resp(999) |
| 206 |
assert obj.send(body="test") is False |
| 207 |
|
| 208 |
mock_post.reset_mock() |
| 209 |
|
| 210 |
# RequestException -> send() returns False |
| 211 |
mock_post.side_effect = requests.RequestException("connection error") |
| 212 |
assert obj.send(body="test") is False |
| 213 |
|
| 214 |
|
| 215 |
def test_plugin_stackfield_url_parsing(): |
| 216 |
"""Test parse_url() and parse_native_url() edge cases.""" |
| 217 |
|
| 218 |
# Token from URL path (host position) |
| 219 |
result = NotifyStackfield.parse_url(f"stackfield://{TEST_TOKEN}") |
| 220 |
assert result is not None |
| 221 |
assert result["token"] == TEST_TOKEN |
| 222 |
|
| 223 |
# Token from ?token= query string |
| 224 |
result = NotifyStackfield.parse_url(f"stackfield://?token={TEST_TOKEN}") |
| 225 |
assert result is not None |
| 226 |
assert result["token"] == TEST_TOKEN |
| 227 |
|
| 228 |
# ?token= wins over host position when both are present |
| 229 |
other = "abcdef01-2345-6789-abcd-ef0123456789" |
| 230 |
result = NotifyStackfield.parse_url( |
| 231 |
f"stackfield://{TEST_TOKEN}?token={other}" |
| 232 |
) |
| 233 |
assert result is not None |
| 234 |
assert result["token"] == other |
| 235 |
|
| 236 |
# parse_native_url: full https://www.stackfield.com/apiwh/TOKEN |
| 237 |
result = NotifyStackfield.parse_native_url( |
| 238 |
f"https://www.stackfield.com/apiwh/{TEST_TOKEN}" |
| 239 |
) |
| 240 |
assert result is not None |
| 241 |
assert result["token"] == TEST_TOKEN |
| 242 |
|
| 243 |
# parse_native_url: without www |
| 244 |
result = NotifyStackfield.parse_native_url( |
| 245 |
f"https://stackfield.com/apiwh/{TEST_TOKEN}" |
| 246 |
) |
| 247 |
assert result is not None |
| 248 |
assert result["token"] == TEST_TOKEN |
| 249 |
|
| 250 |
# parse_native_url: http variant |
| 251 |
result = NotifyStackfield.parse_native_url( |
| 252 |
f"http://www.stackfield.com/apiwh/{TEST_TOKEN}" |
| 253 |
) |
| 254 |
assert result is not None |
| 255 |
assert result["token"] == TEST_TOKEN |
| 256 |
|
| 257 |
# parse_native_url: invalid token format -> returns None |
| 258 |
result = NotifyStackfield.parse_native_url( |
| 259 |
"https://www.stackfield.com/apiwh/not-a-valid-uuid" |
| 260 |
) |
| 261 |
assert result is None |
| 262 |
|
| 263 |
# parse_native_url: unrelated URL -> returns None |
| 264 |
result = NotifyStackfield.parse_native_url( |
| 265 |
"https://example.com/webhook/12345" |
| 266 |
) |
| 267 |
assert result is None |
| 268 |
|
| 269 |
|
| 270 |
@mock.patch("requests.post") |
| 271 |
def test_plugin_stackfield_apprise_integration(mock_post): |
| 272 |
"""Test that Apprise correctly loads and fires a Stackfield URL.""" |
| 273 |
|
| 274 |
r = mock.Mock() |
| 275 |
r.status_code = requests.codes.ok |
| 276 |
r.content = b"" |
| 277 |
mock_post.return_value = r |
| 278 |
|
| 279 |
a = Apprise() |
| 280 |
assert a.add(f"stackfield://{TEST_TOKEN}") is True |
| 281 |
assert bool(a.notify(title="Test", body="Integration test")) is True |
| 282 |
assert mock_post.call_count == 1 |