| 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 |
from json import loads |
| 30 |
import logging |
| 31 |
from unittest import mock |
| 32 |
|
| 33 |
from helpers import AppriseURLTester |
| 34 |
import pytest |
| 35 |
import requests |
| 36 |
|
| 37 |
import apprise |
| 38 |
from apprise import NotifyType |
| 39 |
from apprise.exception import AppriseImproperlyConfigured |
| 40 |
from apprise.plugins.pinglet import NotifyPinglet, PingletPriority |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
# Our Testing URLs |
| 45 |
apprise_url_tests = ( |
| 46 |
( |
| 47 |
"pinglet://", |
| 48 |
{ |
| 49 |
"instance": None, |
| 50 |
}, |
| 51 |
), |
| 52 |
# An invalid url |
| 53 |
( |
| 54 |
"pinglet://:@/", |
| 55 |
{ |
| 56 |
"instance": None, |
| 57 |
}, |
| 58 |
), |
| 59 |
# No API Key specified |
| 60 |
( |
| 61 |
"pinglet://hostname/acme/deploys", |
| 62 |
{ |
| 63 |
"instance": AppriseImproperlyConfigured, |
| 64 |
}, |
| 65 |
), |
| 66 |
# No namespace and/or topic specified |
| 67 |
( |
| 68 |
"pinglet://token@hostname", |
| 69 |
{ |
| 70 |
"instance": AppriseImproperlyConfigured, |
| 71 |
}, |
| 72 |
), |
| 73 |
( |
| 74 |
"pinglet://token@hostname/deploys", |
| 75 |
{ |
| 76 |
"instance": AppriseImproperlyConfigured, |
| 77 |
}, |
| 78 |
), |
| 79 |
# Provide an API Key, namespace, and topic |
| 80 |
( |
| 81 |
"pinglet://abc123@hostname/acme/deploys", |
| 82 |
{ |
| 83 |
"instance": NotifyPinglet, |
| 84 |
# Our expected url(privacy=True) startswith() response: |
| 85 |
"privacy_url": "pinglet://****@hostname/acme/deploys", |
| 86 |
}, |
| 87 |
), |
| 88 |
# Secure protocol |
| 89 |
( |
| 90 |
"pinglets://abc123@hostname/acme/deploys", |
| 91 |
{ |
| 92 |
"instance": NotifyPinglet, |
| 93 |
"privacy_url": "pinglets://****@hostname/acme/deploys", |
| 94 |
}, |
| 95 |
), |
| 96 |
# API Key as a query argument |
| 97 |
( |
| 98 |
"pinglets://hostname/acme/deploys?token=abc123", |
| 99 |
{ |
| 100 |
"instance": NotifyPinglet, |
| 101 |
"privacy_url": "pinglets://****@hostname/acme/deploys", |
| 102 |
}, |
| 103 |
), |
| 104 |
# A path prefix (such as a reverse-proxy mount point) |
| 105 |
( |
| 106 |
"pinglet://abc123@hostname/prefix/acme/deploys", |
| 107 |
{ |
| 108 |
"instance": NotifyPinglet, |
| 109 |
"privacy_url": "pinglet://****@hostname/prefix/acme/deploys", |
| 110 |
}, |
| 111 |
), |
| 112 |
# Host with a port |
| 113 |
( |
| 114 |
"pinglet://abc123@hostname:8080/acme/deploys", |
| 115 |
{ |
| 116 |
"instance": NotifyPinglet, |
| 117 |
"privacy_url": "pinglet://****@hostname:8080/acme/deploys", |
| 118 |
}, |
| 119 |
), |
| 120 |
# Provide a priority |
| 121 |
( |
| 122 |
"pinglet://abc123@hostname/acme/deploys?priority=urgent", |
| 123 |
{ |
| 124 |
"instance": NotifyPinglet, |
| 125 |
}, |
| 126 |
), |
| 127 |
# Priorities support prefix matching (s => silent) |
| 128 |
( |
| 129 |
"pinglet://abc123@hostname/acme/deploys?priority=s", |
| 130 |
{ |
| 131 |
"instance": NotifyPinglet, |
| 132 |
}, |
| 133 |
), |
| 134 |
# An invalid priority takes on the default (normal) |
| 135 |
( |
| 136 |
"pinglet://abc123@hostname/acme/deploys?priority=invalid", |
| 137 |
{ |
| 138 |
"instance": NotifyPinglet, |
| 139 |
}, |
| 140 |
), |
| 141 |
# Badges (:key=value) and metadata (+key=value) |
| 142 |
( |
| 143 |
"pinglet://abc123@hostname/acme/deploys?:CPU=95%25&+region=eu-west", |
| 144 |
{ |
| 145 |
"instance": NotifyPinglet, |
| 146 |
}, |
| 147 |
), |
| 148 |
( |
| 149 |
"pinglet://abc123@hostname/acme/deploys", |
| 150 |
{ |
| 151 |
"instance": NotifyPinglet, |
| 152 |
# force a failure |
| 153 |
"response": False, |
| 154 |
"requests_response_code": requests.codes.internal_server_error, |
| 155 |
}, |
| 156 |
), |
| 157 |
( |
| 158 |
"pinglets://abc123@localhost/acme/deploys", |
| 159 |
{ |
| 160 |
"instance": NotifyPinglet, |
| 161 |
# throw a bizarre code forcing us to fail to look it up |
| 162 |
"response": False, |
| 163 |
"requests_response_code": 999, |
| 164 |
}, |
| 165 |
), |
| 166 |
( |
| 167 |
"pinglet://abc123@localhost/acme/deploys", |
| 168 |
{ |
| 169 |
"instance": NotifyPinglet, |
| 170 |
# Throws a series of i/o exceptions with this flag |
| 171 |
# is set and tests that we gracefully handle them |
| 172 |
"test_requests_exceptions": True, |
| 173 |
}, |
| 174 |
), |
| 175 |
) |
| 176 |
|
| 177 |
|
| 178 |
def test_plugin_pinglet_urls(): |
| 179 |
"""NotifyPinglet() Apprise URLs.""" |
| 180 |
|
| 181 |
# Run our general tests |
| 182 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 183 |
|
| 184 |
|
| 185 |
def test_plugin_pinglet_edge_cases(): |
| 186 |
"""NotifyPinglet() Edge Cases.""" |
| 187 |
# Initializes the plugin with an invalid token |
| 188 |
with pytest.raises(AppriseImproperlyConfigured): |
| 189 |
NotifyPinglet(token=None, namespace="acme", topic="deploys") |
| 190 |
# Whitespace also acts as an invalid token value |
| 191 |
with pytest.raises(AppriseImproperlyConfigured): |
| 192 |
NotifyPinglet(token=" ", namespace="acme", topic="deploys") |
| 193 |
|
| 194 |
# Missing namespace and/or topic |
| 195 |
with pytest.raises(AppriseImproperlyConfigured): |
| 196 |
NotifyPinglet(token="abc123", namespace=None, topic="deploys") |
| 197 |
with pytest.raises(AppriseImproperlyConfigured): |
| 198 |
NotifyPinglet(token="abc123", namespace="acme", topic=None) |
| 199 |
|
| 200 |
# Direct instantiation without a fullpath defaults to "/" |
| 201 |
obj = NotifyPinglet(token="abc123", namespace="acme", topic="deploys") |
| 202 |
assert obj.fullpath == "/" |
| 203 |
|
| 204 |
|
| 205 |
@mock.patch("requests.post") |
| 206 |
def test_plugin_pinglet_payload(mock_post): |
| 207 |
"""NotifyPinglet() Payload Construction.""" |
| 208 |
|
| 209 |
# Prepare Mock |
| 210 |
mock_post.return_value = requests.Request() |
| 211 |
mock_post.return_value.status_code = requests.codes.ok |
| 212 |
|
| 213 |
aobj = apprise.Apprise() |
| 214 |
assert aobj.add( |
| 215 |
"pinglets://[email protected]/acme/deploys" |
| 216 |
"?priority=urgent&:CPU=95%25&:Host=web-1&+region=eu-west" |
| 217 |
) |
| 218 |
|
| 219 |
assert bool( |
| 220 |
aobj.notify( |
| 221 |
title="Hello", |
| 222 |
body="It works", |
| 223 |
notify_type=NotifyType.FAILURE, |
| 224 |
) |
| 225 |
) |
| 226 |
|
| 227 |
assert mock_post.call_count == 1 |
| 228 |
assert ( |
| 229 |
mock_post.call_args_list[0][0][0] |
| 230 |
== "https://app.pinglet.co.uk/acme/deploys" |
| 231 |
) |
| 232 |
|
| 233 |
headers = mock_post.call_args_list[0][1]["headers"] |
| 234 |
assert headers["Authorization"] == "Bearer mykey" |
| 235 |
|
| 236 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 237 |
assert payload["title"] == "Hello" |
| 238 |
assert payload["message"] == "It works" |
| 239 |
assert payload["priority"] == PingletPriority.URGENT |
| 240 |
# The FAILURE notification type maps to Pinglet's 'error' level |
| 241 |
assert payload["level"] == "error" |
| 242 |
assert payload["badges"] == {"CPU": "95%", "Host": "web-1"} |
| 243 |
assert payload["data"] == {"region": "eu-west"} |
| 244 |
|
| 245 |
|
| 246 |
@mock.patch("requests.post") |
| 247 |
def test_plugin_pinglet_payload_defaults(mock_post): |
| 248 |
"""NotifyPinglet() Payload Defaults.""" |
| 249 |
|
| 250 |
# Prepare Mock |
| 251 |
mock_post.return_value = requests.Request() |
| 252 |
mock_post.return_value.status_code = requests.codes.ok |
| 253 |
|
| 254 |
aobj = apprise.Apprise() |
| 255 |
assert aobj.add("pinglet://mykey@hostname/acme/deploys") |
| 256 |
|
| 257 |
assert bool(aobj.notify(body="It works")) |
| 258 |
|
| 259 |
assert mock_post.call_count == 1 |
| 260 |
assert mock_post.call_args_list[0][0][0] == "http://hostname/acme/deploys" |
| 261 |
|
| 262 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 263 |
# An empty title is omitted entirely |
| 264 |
assert "title" not in payload |
| 265 |
# No badges/metadata were specified so they are omitted |
| 266 |
assert "badges" not in payload |
| 267 |
assert "data" not in payload |
| 268 |
assert payload["priority"] == PingletPriority.NORMAL |
| 269 |
# The INFO notification type maps to Pinglet's 'info' level |
| 270 |
assert payload["level"] == "info" |
| 271 |
|
| 272 |
|
| 273 |
def test_plugin_pinglet_limits(): |
| 274 |
"""NotifyPinglet() Badge and Metadata Limits.""" |
| 275 |
|
| 276 |
# Badges are capped at 3 entries; keys are truncated to 24 |
| 277 |
# characters, and values to 32 |
| 278 |
obj = apprise.Apprise.instantiate( |
| 279 |
"pinglet://abc123@hostname/acme/deploys" |
| 280 |
"?:{}={}&:b=2&:c=3&:d=4&:e=5".format("k" * 30, "v" * 40) |
| 281 |
) |
| 282 |
assert isinstance(obj, NotifyPinglet) |
| 283 |
assert len(obj.badges) == 3 |
| 284 |
assert "k" * 24 in obj.badges |
| 285 |
assert obj.badges["k" * 24] == "v" * 32 |
| 286 |
|
| 287 |
# Metadata keys are truncated to 64 characters, and values to 256 |
| 288 |
obj = apprise.Apprise.instantiate( |
| 289 |
"pinglet://abc123@hostname/acme/deploys?+{}={}".format( |
| 290 |
"k" * 70, "v" * 300 |
| 291 |
) |
| 292 |
) |
| 293 |
assert isinstance(obj, NotifyPinglet) |
| 294 |
assert obj.data == {"k" * 64: "v" * 256} |
| 295 |
|
| 296 |
|
| 297 |
def test_plugin_pinglet_priorities(): |
| 298 |
"""NotifyPinglet() Priority Handling.""" |
| 299 |
|
| 300 |
for priority, expected in ( |
| 301 |
("silent", PingletPriority.SILENT), |
| 302 |
("s", PingletPriority.SILENT), |
| 303 |
("NORMAL", PingletPriority.NORMAL), |
| 304 |
("urgent", PingletPriority.URGENT), |
| 305 |
("u", PingletPriority.URGENT), |
| 306 |
# Invalid entries take on the default |
| 307 |
("invalid", PingletPriority.NORMAL), |
| 308 |
("", PingletPriority.NORMAL), |
| 309 |
): |
| 310 |
obj = apprise.Apprise.instantiate( |
| 311 |
f"pinglet://abc123@hostname/acme/deploys?priority={priority}" |
| 312 |
) |
| 313 |
assert isinstance(obj, NotifyPinglet) |
| 314 |
assert obj.priority == expected, f"priority={priority}" |