| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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.plugins.pinglet import NotifyPinglet, PingletPriority |
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
|
| 44 |
apprise_url_tests = ( |
| 45 |
( |
| 46 |
"pinglet://", |
| 47 |
{ |
| 48 |
"instance": None, |
| 49 |
}, |
| 50 |
), |
| 51 |
|
| 52 |
( |
| 53 |
"pinglet://:@/", |
| 54 |
{ |
| 55 |
"instance": None, |
| 56 |
}, |
| 57 |
), |
| 58 |
|
| 59 |
( |
| 60 |
"pinglet://hostname/acme/deploys", |
| 61 |
{ |
| 62 |
"instance": TypeError, |
| 63 |
}, |
| 64 |
), |
| 65 |
|
| 66 |
( |
| 67 |
"pinglet://token@hostname", |
| 68 |
{ |
| 69 |
"instance": TypeError, |
| 70 |
}, |
| 71 |
), |
| 72 |
( |
| 73 |
"pinglet://token@hostname/deploys", |
| 74 |
{ |
| 75 |
"instance": TypeError, |
| 76 |
}, |
| 77 |
), |
| 78 |
|
| 79 |
( |
| 80 |
"pinglet://abc123@hostname/acme/deploys", |
| 81 |
{ |
| 82 |
"instance": NotifyPinglet, |
| 83 |
|
| 84 |
"privacy_url": "pinglet://****@hostname/acme/deploys", |
| 85 |
}, |
| 86 |
), |
| 87 |
|
| 88 |
( |
| 89 |
"pinglets://abc123@hostname/acme/deploys", |
| 90 |
{ |
| 91 |
"instance": NotifyPinglet, |
| 92 |
"privacy_url": "pinglets://****@hostname/acme/deploys", |
| 93 |
}, |
| 94 |
), |
| 95 |
|
| 96 |
( |
| 97 |
"pinglets://hostname/acme/deploys?token=abc123", |
| 98 |
{ |
| 99 |
"instance": NotifyPinglet, |
| 100 |
"privacy_url": "pinglets://****@hostname/acme/deploys", |
| 101 |
}, |
| 102 |
), |
| 103 |
|
| 104 |
( |
| 105 |
"pinglet://abc123@hostname/prefix/acme/deploys", |
| 106 |
{ |
| 107 |
"instance": NotifyPinglet, |
| 108 |
"privacy_url": "pinglet://****@hostname/prefix/acme/deploys", |
| 109 |
}, |
| 110 |
), |
| 111 |
|
| 112 |
( |
| 113 |
"pinglet://abc123@hostname:8080/acme/deploys", |
| 114 |
{ |
| 115 |
"instance": NotifyPinglet, |
| 116 |
"privacy_url": "pinglet://****@hostname:8080/acme/deploys", |
| 117 |
}, |
| 118 |
), |
| 119 |
|
| 120 |
( |
| 121 |
"pinglet://abc123@hostname/acme/deploys?priority=urgent", |
| 122 |
{ |
| 123 |
"instance": NotifyPinglet, |
| 124 |
}, |
| 125 |
), |
| 126 |
|
| 127 |
( |
| 128 |
"pinglet://abc123@hostname/acme/deploys?priority=s", |
| 129 |
{ |
| 130 |
"instance": NotifyPinglet, |
| 131 |
}, |
| 132 |
), |
| 133 |
|
| 134 |
( |
| 135 |
"pinglet://abc123@hostname/acme/deploys?priority=invalid", |
| 136 |
{ |
| 137 |
"instance": NotifyPinglet, |
| 138 |
}, |
| 139 |
), |
| 140 |
|
| 141 |
( |
| 142 |
"pinglet://abc123@hostname/acme/deploys?:CPU=95%25&+region=eu-west", |
| 143 |
{ |
| 144 |
"instance": NotifyPinglet, |
| 145 |
}, |
| 146 |
), |
| 147 |
( |
| 148 |
"pinglet://abc123@hostname/acme/deploys", |
| 149 |
{ |
| 150 |
"instance": NotifyPinglet, |
| 151 |
|
| 152 |
"response": False, |
| 153 |
"requests_response_code": requests.codes.internal_server_error, |
| 154 |
}, |
| 155 |
), |
| 156 |
( |
| 157 |
"pinglets://abc123@localhost/acme/deploys", |
| 158 |
{ |
| 159 |
"instance": NotifyPinglet, |
| 160 |
|
| 161 |
"response": False, |
| 162 |
"requests_response_code": 999, |
| 163 |
}, |
| 164 |
), |
| 165 |
( |
| 166 |
"pinglet://abc123@localhost/acme/deploys", |
| 167 |
{ |
| 168 |
"instance": NotifyPinglet, |
| 169 |
|
| 170 |
|
| 171 |
"test_requests_exceptions": True, |
| 172 |
}, |
| 173 |
), |
| 174 |
) |
| 175 |
|
| 176 |
|
| 177 |
def test_plugin_pinglet_urls(): |
| 178 |
"""NotifyPinglet() Apprise URLs.""" |
| 179 |
|
| 180 |
|
| 181 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 182 |
|
| 183 |
|
| 184 |
def test_plugin_pinglet_edge_cases(): |
| 185 |
"""NotifyPinglet() Edge Cases.""" |
| 186 |
|
| 187 |
with pytest.raises(TypeError): |
| 188 |
NotifyPinglet(token=None, namespace="acme", topic="deploys") |
| 189 |
|
| 190 |
with pytest.raises(TypeError): |
| 191 |
NotifyPinglet(token=" ", namespace="acme", topic="deploys") |
| 192 |
|
| 193 |
|
| 194 |
with pytest.raises(TypeError): |
| 195 |
NotifyPinglet(token="abc123", namespace=None, topic="deploys") |
| 196 |
with pytest.raises(TypeError): |
| 197 |
NotifyPinglet(token="abc123", namespace="acme", topic=None) |
| 198 |
|
| 199 |
|
| 200 |
obj = NotifyPinglet(token="abc123", namespace="acme", topic="deploys") |
| 201 |
assert obj.fullpath == "/" |
| 202 |
|
| 203 |
|
| 204 |
@mock.patch("requests.post") |
| 205 |
def test_plugin_pinglet_payload(mock_post): |
| 206 |
"""NotifyPinglet() Payload Construction.""" |
| 207 |
|
| 208 |
|
| 209 |
mock_post.return_value = requests.Request() |
| 210 |
mock_post.return_value.status_code = requests.codes.ok |
| 211 |
|
| 212 |
aobj = apprise.Apprise() |
| 213 |
assert aobj.add( |
| 214 |
"pinglets://[email protected]/acme/deploys" |
| 215 |
"?priority=urgent&:CPU=95%25&:Host=web-1&+region=eu-west" |
| 216 |
) |
| 217 |
|
| 218 |
assert ( |
| 219 |
aobj.notify( |
| 220 |
title="Hello", |
| 221 |
body="It works", |
| 222 |
notify_type=NotifyType.FAILURE, |
| 223 |
) |
| 224 |
is True |
| 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 |
|
| 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 |
|
| 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 aobj.notify(body="It works") is True |
| 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 |
|
| 264 |
assert "title" not in payload |
| 265 |
|
| 266 |
assert "badges" not in payload |
| 267 |
assert "data" not in payload |
| 268 |
assert payload["priority"] == PingletPriority.NORMAL |
| 269 |
|
| 270 |
assert payload["level"] == "info" |
| 271 |
|
| 272 |
|
| 273 |
def test_plugin_pinglet_limits(): |
| 274 |
"""NotifyPinglet() Badge and Metadata Limits.""" |
| 275 |
|
| 276 |
|
| 277 |
|
| 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 |
|
| 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 |
|
| 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}" |