| 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 |
import logging |
| 29 |
import os |
| 30 |
from unittest import mock |
| 31 |
|
| 32 |
from helpers import AppriseURLTester |
| 33 |
import requests |
| 34 |
|
| 35 |
import apprise |
| 36 |
from apprise.exception import AppriseImproperlyConfigured |
| 37 |
from apprise.plugins.signalgrid import NotifySignalgrid |
| 38 |
|
| 39 |
logging.disable(logging.CRITICAL) |
| 40 |
|
| 41 |
# Attachment Directory |
| 42 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 43 |
|
| 44 |
|
| 45 |
apprise_url_tests = ( |
| 46 |
( |
| 47 |
"signalgrid://", |
| 48 |
{ |
| 49 |
"instance": AppriseImproperlyConfigured, |
| 50 |
}, |
| 51 |
), |
| 52 |
( |
| 53 |
"signalgrid://CLIENTKEY/CHANNEL", |
| 54 |
{ |
| 55 |
"instance": NotifySignalgrid, |
| 56 |
"privacy_url": "signalgrid://C...Y/CHANNEL/", |
| 57 |
}, |
| 58 |
), |
| 59 |
( |
| 60 |
"signalgrid://CLIENTKEY/CHANNEL1/CHANNEL2", |
| 61 |
{ |
| 62 |
"instance": NotifySignalgrid, |
| 63 |
}, |
| 64 |
), |
| 65 |
( |
| 66 |
"signalgrid://CLIENTKEY/CHANNEL?critical=yes", |
| 67 |
{ |
| 68 |
"instance": NotifySignalgrid, |
| 69 |
}, |
| 70 |
), |
| 71 |
( |
| 72 |
"signalgrid://CLIENTKEY/CHANNEL?to=CHANNEL2,CHANNEL3", |
| 73 |
{ |
| 74 |
"instance": NotifySignalgrid, |
| 75 |
}, |
| 76 |
), |
| 77 |
( |
| 78 |
# No channels specified at all; the plugin still loads, but has |
| 79 |
# nothing to notify |
| 80 |
"signalgrid://CLIENTKEY", |
| 81 |
{ |
| 82 |
"instance": NotifySignalgrid, |
| 83 |
"notify_response": False, |
| 84 |
}, |
| 85 |
), |
| 86 |
( |
| 87 |
"signalgrid://CLIENTKEY/CHANNEL", |
| 88 |
{ |
| 89 |
"instance": NotifySignalgrid, |
| 90 |
"response": False, |
| 91 |
"requests_response_code": requests.codes.internal_server_error, |
| 92 |
}, |
| 93 |
), |
| 94 |
( |
| 95 |
"signalgrid://CLIENTKEY/CHANNEL", |
| 96 |
{ |
| 97 |
"instance": NotifySignalgrid, |
| 98 |
"test_requests_exceptions": True, |
| 99 |
}, |
| 100 |
), |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
def test_plugin_signalgrid_urls(): |
| 105 |
"""NotifySignalgrid() Apprise URLs.""" |
| 106 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 107 |
|
| 108 |
|
| 109 |
@mock.patch("requests.post") |
| 110 |
def test_plugin_signalgrid_payload(mock_post): |
| 111 |
"""Test Signalgrid request payload.""" |
| 112 |
|
| 113 |
response = mock.Mock() |
| 114 |
response.status_code = requests.codes.ok |
| 115 |
response.content = b"OK" |
| 116 |
mock_post.return_value = response |
| 117 |
|
| 118 |
obj = NotifySignalgrid( |
| 119 |
client_key="CLIENTKEY", |
| 120 |
targets=["CHANNEL"], |
| 121 |
critical=True, |
| 122 |
) |
| 123 |
|
| 124 |
assert ( |
| 125 |
obj.notify( |
| 126 |
title="Server Down", |
| 127 |
body="api01 is unreachable", |
| 128 |
notify_type=apprise.NotifyType.FAILURE, |
| 129 |
) |
| 130 |
is True |
| 131 |
) |
| 132 |
|
| 133 |
assert mock_post.call_count == 1 |
| 134 |
|
| 135 |
args, kwargs = mock_post.call_args |
| 136 |
|
| 137 |
assert args[0] == "https://api.signalgrid.co/v1/push" |
| 138 |
assert kwargs["data"]["client_key"] == "CLIENTKEY" |
| 139 |
assert kwargs["data"]["channel"] == "CHANNEL" |
| 140 |
assert kwargs["data"]["title"] == "Server Down" |
| 141 |
assert kwargs["data"]["body"] == "api01 is unreachable" |
| 142 |
assert kwargs["data"]["type"] == "CRIT" |
| 143 |
assert kwargs["data"]["critical"] == "true" |
| 144 |
|
| 145 |
|
| 146 |
@mock.patch("requests.post") |
| 147 |
def test_plugin_signalgrid_type_mapping(mock_post): |
| 148 |
"""Test Apprise to Signalgrid type mapping.""" |
| 149 |
|
| 150 |
response = mock.Mock() |
| 151 |
response.status_code = requests.codes.ok |
| 152 |
mock_post.return_value = response |
| 153 |
|
| 154 |
obj = NotifySignalgrid( |
| 155 |
client_key="CLIENTKEY", |
| 156 |
targets=["CHANNEL"], |
| 157 |
) |
| 158 |
|
| 159 |
mappings = ( |
| 160 |
(apprise.NotifyType.INFO, "INFO"), |
| 161 |
(apprise.NotifyType.SUCCESS, "SUCCESS"), |
| 162 |
(apprise.NotifyType.WARNING, "WARN"), |
| 163 |
(apprise.NotifyType.FAILURE, "CRIT"), |
| 164 |
) |
| 165 |
|
| 166 |
for notify_type, expected in mappings: |
| 167 |
mock_post.reset_mock() |
| 168 |
|
| 169 |
assert ( |
| 170 |
obj.notify( |
| 171 |
title="Test", |
| 172 |
body="Test", |
| 173 |
notify_type=notify_type, |
| 174 |
) |
| 175 |
is True |
| 176 |
) |
| 177 |
|
| 178 |
assert mock_post.call_args.kwargs["data"]["type"] == expected |
| 179 |
assert mock_post.call_args.kwargs["data"]["critical"] == "false" |
| 180 |
|
| 181 |
|
| 182 |
@mock.patch("requests.post") |
| 183 |
def test_plugin_signalgrid_multi_channel(mock_post): |
| 184 |
"""Test Signalgrid sends one request per channel.""" |
| 185 |
|
| 186 |
response = mock.Mock() |
| 187 |
response.status_code = requests.codes.ok |
| 188 |
mock_post.return_value = response |
| 189 |
|
| 190 |
obj = NotifySignalgrid( |
| 191 |
client_key="CLIENTKEY", |
| 192 |
targets=["CHANNEL1", "CHANNEL2"], |
| 193 |
) |
| 194 |
|
| 195 |
assert len(obj) == 2 |
| 196 |
|
| 197 |
assert obj.notify(title="Test", body="Test") is True |
| 198 |
assert mock_post.call_count == 2 |
| 199 |
|
| 200 |
sent_channels = { |
| 201 |
call.kwargs["data"]["channel"] for call in mock_post.call_args_list |
| 202 |
} |
| 203 |
assert sent_channels == {"CHANNEL1", "CHANNEL2"} |
| 204 |
|
| 205 |
|
| 206 |
@mock.patch("requests.post") |
| 207 |
def test_plugin_signalgrid_multi_channel_partial_failure(mock_post): |
| 208 |
"""Test one failed channel still notifies the rest and reports False.""" |
| 209 |
|
| 210 |
ok_response = mock.Mock() |
| 211 |
ok_response.status_code = requests.codes.ok |
| 212 |
|
| 213 |
error_response = mock.Mock() |
| 214 |
error_response.status_code = requests.codes.internal_server_error |
| 215 |
error_response.content = b"error" |
| 216 |
|
| 217 |
mock_post.side_effect = [error_response, ok_response] |
| 218 |
|
| 219 |
obj = NotifySignalgrid( |
| 220 |
client_key="CLIENTKEY", |
| 221 |
targets=["CHANNEL1", "CHANNEL2"], |
| 222 |
) |
| 223 |
|
| 224 |
assert obj.notify(title="Test", body="Test") is False |
| 225 |
assert mock_post.call_count == 2 |
| 226 |
|
| 227 |
|
| 228 |
def test_plugin_signalgrid_no_targets(): |
| 229 |
"""Test that a Signalgrid object with no channels loads but has |
| 230 |
nothing to notify.""" |
| 231 |
|
| 232 |
obj = NotifySignalgrid(client_key="CLIENTKEY") |
| 233 |
|
| 234 |
assert len(obj) == 1 |
| 235 |
assert obj.notify(title="Test", body="Test") is False |
| 236 |
|
| 237 |
|
| 238 |
def test_plugin_signalgrid_url_roundtrip(): |
| 239 |
"""Test Signalgrid URL parsing and reconstruction.""" |
| 240 |
|
| 241 |
obj = NotifySignalgrid( |
| 242 |
client_key="CLIENTKEY", |
| 243 |
targets=["CHANNEL1", "CHANNEL2"], |
| 244 |
critical=True, |
| 245 |
) |
| 246 |
|
| 247 |
generated = obj.url() |
| 248 |
parsed = NotifySignalgrid.parse_url(generated) |
| 249 |
|
| 250 |
assert parsed is not None |
| 251 |
assert parsed["client_key"] == "CLIENTKEY" |
| 252 |
assert parsed["targets"] == ["CHANNEL1", "CHANNEL2"] |
| 253 |
assert parsed["critical"] is True |
| 254 |
|
| 255 |
obj2 = NotifySignalgrid(**parsed) |
| 256 |
|
| 257 |
assert obj2.client_key == "CLIENTKEY" |
| 258 |
assert obj2.targets == ["CHANNEL1", "CHANNEL2"] |
| 259 |
assert obj2.critical is True |
| 260 |
|
| 261 |
# Two connections that share a client key differ only in the channels |
| 262 |
# they notify, so they still identify as the same connection. |
| 263 |
assert obj.url_identifier == obj2.url_identifier |
| 264 |
|
| 265 |
|
| 266 |
def test_plugin_signalgrid_client_key_override(): |
| 267 |
"""Test that ?client_key= overrides the hostname-derived value.""" |
| 268 |
|
| 269 |
parsed = NotifySignalgrid.parse_url( |
| 270 |
"signalgrid://IGNOREME/CHANNEL?client_key=REALKEY" |
| 271 |
) |
| 272 |
|
| 273 |
assert parsed is not None |
| 274 |
assert parsed["client_key"] == "REALKEY" |
| 275 |
|
| 276 |
|
| 277 |
@mock.patch("requests.post") |
| 278 |
def test_plugin_signalgrid_attach_warning(mock_post): |
| 279 |
"""Test that attachments are ignored with a warning.""" |
| 280 |
|
| 281 |
response = mock.Mock() |
| 282 |
response.status_code = requests.codes.ok |
| 283 |
mock_post.return_value = response |
| 284 |
|
| 285 |
obj = NotifySignalgrid(client_key="CLIENTKEY", targets=["CHANNEL"]) |
| 286 |
|
| 287 |
attach = apprise.AppriseAttachment( |
| 288 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 289 |
) |
| 290 |
|
| 291 |
with mock.patch.object(obj, "logger") as mock_logger: |
| 292 |
assert obj.notify(title="Test", body="Test", attach=attach) is True |
| 293 |
assert mock_logger.warning.called |