| 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 |
from datetime import datetime, timedelta |
| 29 |
|
| 30 |
|
| 31 |
import logging |
| 32 |
from timeit import default_timer |
| 33 |
|
| 34 |
import pytest |
| 35 |
|
| 36 |
from apprise import AppriseAsset, NotifyImageSize, NotifyType |
| 37 |
from apprise.plugins import NotifyBase |
| 38 |
|
| 39 |
logging.disable(logging.CRITICAL) |
| 40 |
|
| 41 |
|
| 42 |
def test_notify_base(): |
| 43 |
""" |
| 44 |
API: NotifyBase() object |
| 45 |
|
| 46 |
""" |
| 47 |
|
| 48 |
|
| 49 |
with pytest.raises(TypeError): |
| 50 |
NotifyBase(**{"format": "invalid"}) |
| 51 |
|
| 52 |
|
| 53 |
with pytest.raises(TypeError): |
| 54 |
NotifyBase(**{"overflow": "invalid"}) |
| 55 |
|
| 56 |
|
| 57 |
nb = NotifyBase(port="invalid") |
| 58 |
assert nb.port is None |
| 59 |
|
| 60 |
nb = NotifyBase(port=10) |
| 61 |
assert nb.port == 10 |
| 62 |
|
| 63 |
assert isinstance(nb.url(), str) |
| 64 |
assert str(nb) == nb.url() |
| 65 |
|
| 66 |
with pytest.raises(NotImplementedError): |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
nb.send("test message") |
| 71 |
|
| 72 |
|
| 73 |
nb = NotifyBase() |
| 74 |
nb.request_rate_per_sec = 0.0 |
| 75 |
start_time = default_timer() |
| 76 |
nb.throttle() |
| 77 |
elapsed = default_timer() - start_time |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
assert elapsed < 0.5 |
| 82 |
|
| 83 |
|
| 84 |
start_time = default_timer() |
| 85 |
nb.throttle() |
| 86 |
elapsed = default_timer() - start_time |
| 87 |
assert elapsed < 0.5 |
| 88 |
|
| 89 |
nb = NotifyBase() |
| 90 |
nb.request_rate_per_sec = 1.0 |
| 91 |
|
| 92 |
|
| 93 |
start_time = default_timer() |
| 94 |
nb.throttle() |
| 95 |
elapsed = default_timer() - start_time |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
assert elapsed < 0.5 |
| 100 |
|
| 101 |
|
| 102 |
start_time = default_timer() |
| 103 |
nb.throttle(last_io=datetime.now()) |
| 104 |
elapsed = default_timer() - start_time |
| 105 |
assert elapsed > 0.48 and elapsed < 1.5 |
| 106 |
|
| 107 |
nb = NotifyBase() |
| 108 |
nb.request_rate_per_sec = 1.0 |
| 109 |
|
| 110 |
|
| 111 |
start_time = default_timer() |
| 112 |
nb.throttle(last_io=datetime.now()) |
| 113 |
elapsed = default_timer() - start_time |
| 114 |
|
| 115 |
|
| 116 |
assert elapsed > 0.48 and elapsed < 1.5 |
| 117 |
|
| 118 |
|
| 119 |
start_time = default_timer() |
| 120 |
nb.throttle(last_io=datetime.now()) |
| 121 |
elapsed = default_timer() - start_time |
| 122 |
assert elapsed > 0.48 and elapsed < 1.5 |
| 123 |
|
| 124 |
nb = NotifyBase() |
| 125 |
start_time = default_timer() |
| 126 |
nb.request_rate_per_sec = 1.0 |
| 127 |
|
| 128 |
nb.throttle(last_io=(datetime.now() - timedelta(seconds=20))) |
| 129 |
elapsed = default_timer() - start_time |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
assert elapsed < 0.5 |
| 134 |
|
| 135 |
|
| 136 |
start_time = default_timer() |
| 137 |
nb.throttle(wait=0.5) |
| 138 |
elapsed = default_timer() - start_time |
| 139 |
assert elapsed > 0.48 and elapsed < 1.5 |
| 140 |
|
| 141 |
|
| 142 |
assert nb.image_url(notify_type=NotifyType.INFO) is None |
| 143 |
assert nb.image_path(notify_type=NotifyType.INFO) is None |
| 144 |
assert nb.image_raw(notify_type=NotifyType.INFO) is None |
| 145 |
|
| 146 |
|
| 147 |
assert nb.color(notify_type="invalid") == AppriseAsset.default_html_color |
| 148 |
assert isinstance( |
| 149 |
nb.color(notify_type=NotifyType.INFO, color_type=None), str |
| 150 |
) |
| 151 |
assert isinstance( |
| 152 |
nb.color(notify_type=NotifyType.INFO, color_type=int), int |
| 153 |
) |
| 154 |
assert isinstance( |
| 155 |
nb.color(notify_type=NotifyType.INFO, color_type=tuple), tuple |
| 156 |
) |
| 157 |
|
| 158 |
|
| 159 |
assert nb.ascii(notify_type="invalid") == AppriseAsset.default_ascii_chars |
| 160 |
assert nb.ascii(NotifyType.INFO) == "[i]" |
| 161 |
assert nb.ascii(NotifyType.SUCCESS) == "[+]" |
| 162 |
assert nb.ascii(NotifyType.WARNING) == "[~]" |
| 163 |
assert nb.ascii(NotifyType.FAILURE) == "[!]" |
| 164 |
|
| 165 |
|
| 166 |
nb = NotifyBase() |
| 167 |
|
| 168 |
nb.image_size = NotifyImageSize.XY_256 |
| 169 |
|
| 170 |
|
| 171 |
assert nb.image_url(notify_type=NotifyType.INFO) is not None |
| 172 |
assert nb.image_path(notify_type=NotifyType.INFO) is not None |
| 173 |
assert nb.image_raw(notify_type=NotifyType.INFO) is not None |
| 174 |
|
| 175 |
|
| 176 |
assert ( |
| 177 |
NotifyBase.escape_html("<content>'\t \n</content>") |
| 178 |
== "<content>'  \n</content>" |
| 179 |
) |
| 180 |
|
| 181 |
assert ( |
| 182 |
NotifyBase.escape_html( |
| 183 |
"<content>'\t \n</content>", convert_new_lines=True |
| 184 |
) |
| 185 |
== "<content>'  <br/></content>" |
| 186 |
) |
| 187 |
|
| 188 |
|
| 189 |
assert NotifyBase.split_path(None) == [] |
| 190 |
assert NotifyBase.split_path(object()) == [] |
| 191 |
assert NotifyBase.split_path(42) == [] |
| 192 |
|
| 193 |
assert NotifyBase.split_path( |
| 194 |
"/path/?name=Dr%20Disrespect", unquote=False |
| 195 |
) == [ |
| 196 |
"path", |
| 197 |
"?name=Dr%20Disrespect", |
| 198 |
] |
| 199 |
|
| 200 |
assert NotifyBase.split_path( |
| 201 |
"/path/?name=Dr%20Disrespect", unquote=True |
| 202 |
) == [ |
| 203 |
"path", |
| 204 |
"?name=Dr Disrespect", |
| 205 |
] |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
assert NotifyBase.split_path( |
| 211 |
"/%2F///%2F%2F////%2F%2F%2F////", unquote=True |
| 212 |
) == [ |
| 213 |
"/", |
| 214 |
"//", |
| 215 |
"///", |
| 216 |
] |
| 217 |
|
| 218 |
|
| 219 |
assert NotifyBase.parse_list(None) == [] |
| 220 |
assert NotifyBase.parse_list(object()) == [] |
| 221 |
assert NotifyBase.parse_list(42) == [] |
| 222 |
|
| 223 |
result = NotifyBase.parse_list( |
| 224 |
",path,?name=Dr%20Disrespect", unquote=False |
| 225 |
) |
| 226 |
assert isinstance(result, list) |
| 227 |
assert len(result) == 2 |
| 228 |
assert "path" in result |
| 229 |
assert "?name=Dr%20Disrespect" in result |
| 230 |
|
| 231 |
result = NotifyBase.parse_list(",path,?name=Dr%20Disrespect", unquote=True) |
| 232 |
assert isinstance(result, list) |
| 233 |
assert len(result) == 2 |
| 234 |
assert "path" in result |
| 235 |
assert "?name=Dr Disrespect" in result |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
result = NotifyBase.parse_list( |
| 242 |
",%2F,%2F%2F, , , ,%2F%2F%2F, %2F", unquote=True |
| 243 |
) |
| 244 |
assert isinstance(result, list) |
| 245 |
assert len(result) == 3 |
| 246 |
assert "/" in result |
| 247 |
assert "//" in result |
| 248 |
assert "///" in result |
| 249 |
|
| 250 |
|
| 251 |
assert NotifyBase.parse_phone_no(None) == [] |
| 252 |
assert NotifyBase.parse_phone_no(object()) == [] |
| 253 |
assert NotifyBase.parse_phone_no(42) == [] |
| 254 |
|
| 255 |
result = NotifyBase.parse_phone_no( |
| 256 |
"+1-800-123-1234,(800) 123-4567", unquote=False |
| 257 |
) |
| 258 |
assert isinstance(result, list) |
| 259 |
assert len(result) == 2 |
| 260 |
assert "+1-800-123-1234" in result |
| 261 |
assert "(800) 123-4567" in result |
| 262 |
|
| 263 |
|
| 264 |
result = NotifyBase.parse_phone_no( |
| 265 |
"%2B1-800-123-1234,%2B1%20800%20123%204567", unquote=True |
| 266 |
) |
| 267 |
assert isinstance(result, list) |
| 268 |
assert len(result) == 2 |
| 269 |
assert "+1-800-123-1234" in result |
| 270 |
assert "+1 800 123 4567" in result |
| 271 |
|
| 272 |
|
| 273 |
assert NotifyBase.escape_html("") == "" |
| 274 |
assert NotifyBase.escape_html(None) == "" |
| 275 |
assert NotifyBase.escape_html(object()) == "" |
| 276 |
|
| 277 |
|
| 278 |
assert NotifyBase.unquote("%20") == " " |
| 279 |
assert NotifyBase.quote(" ") == "%20" |
| 280 |
assert NotifyBase.unquote(None) == "" |
| 281 |
assert NotifyBase.quote(None) == "" |
| 282 |
|
| 283 |
|
| 284 |
def test_notify_base_urls(): |
| 285 |
""" |
| 286 |
API: NotifyBase() URLs |
| 287 |
|
| 288 |
""" |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
results = NotifyBase.parse_url("https://localhost:8080/?verify=No") |
| 295 |
assert "verify" in results |
| 296 |
assert results["verify"] is False |
| 297 |
|
| 298 |
results = NotifyBase.parse_url("https://localhost:8080/?verify=Yes") |
| 299 |
assert "verify" in results |
| 300 |
assert results["verify"] is True |
| 301 |
|
| 302 |
|
| 303 |
results = NotifyBase.parse_url("https://localhost:8080") |
| 304 |
assert "verify" in results |
| 305 |
assert results["verify"] is True |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
results = NotifyBase.parse_url("https://user:pass@localhost") |
| 311 |
assert "password" in results |
| 312 |
assert results["password"] == "pass" |
| 313 |
|
| 314 |
|
| 315 |
results = NotifyBase.parse_url( |
| 316 |
"https://user:pass@localhost?pass=newpassword" |
| 317 |
) |
| 318 |
assert "password" in results |
| 319 |
assert results["password"] == "newpassword" |
| 320 |
|
| 321 |
|
| 322 |
results = NotifyBase.parse_url( |
| 323 |
"https://user:pass@localhost?password=passwd" |
| 324 |
) |
| 325 |
assert "password" in results |
| 326 |
assert results["password"] == "passwd" |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
results = NotifyBase.parse_url( |
| 331 |
"https://user:pass@localhost?pass=pw1&password=pw2" |
| 332 |
) |
| 333 |
assert "password" in results |
| 334 |
assert results["password"] == "pw1" |
| 335 |
|
| 336 |
|
| 337 |
results = NotifyBase.parse_url("https://localhost?format=invalid") |
| 338 |
assert "format" not in results |
| 339 |
results = NotifyBase.parse_url("https://localhost?format=text") |
| 340 |
assert "format" in results |
| 341 |
assert results["format"] == "text" |
| 342 |
results = NotifyBase.parse_url("https://localhost?format=markdown") |
| 343 |
assert "format" in results |
| 344 |
assert results["format"] == "markdown" |
| 345 |
results = NotifyBase.parse_url("https://localhost?format=html") |
| 346 |
assert "format" in results |
| 347 |
assert results["format"] == "html" |
| 348 |
|
| 349 |
results = NotifyBase.parse_url("https://localhost?overflow=invalid") |
| 350 |
assert "overflow" not in results |
| 351 |
results = NotifyBase.parse_url("https://localhost?overflow=upstream") |
| 352 |
assert "overflow" in results |
| 353 |
assert results["overflow"] == "upstream" |
| 354 |
results = NotifyBase.parse_url("https://localhost?overflow=split") |
| 355 |
assert "overflow" in results |
| 356 |
assert results["overflow"] == "split" |
| 357 |
results = NotifyBase.parse_url("https://localhost?overflow=truncate") |
| 358 |
assert "overflow" in results |
| 359 |
assert results["overflow"] == "truncate" |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
results = NotifyBase.parse_url("https://user:pass@localhost") |
| 365 |
assert "user" in results |
| 366 |
assert results["user"] == "user" |
| 367 |
|
| 368 |
|
| 369 |
results = NotifyBase.parse_url("https://user:pass@localhost?user=newuser") |
| 370 |
assert "user" in results |
| 371 |
assert results["user"] == "newuser" |
| 372 |
|
| 373 |
|
| 374 |
assert NotifyBase.parse_url("https://:@/") is None |
| 375 |
assert NotifyBase.parse_url("http://:@") is None |
| 376 |
assert NotifyBase.parse_url("http://@") is None |
| 377 |
assert NotifyBase.parse_url("http:///") is None |
| 378 |
assert NotifyBase.parse_url("http://:test/") is None |
| 379 |
assert NotifyBase.parse_url("http://pass:test/") is None |
| 380 |
|
| 381 |
|
| 382 |
def test_notify_base_runtime_deps(): |
| 383 |
"""NotifyBase.runtime_deps() returns an empty tuple by default.""" |
| 384 |
|
| 385 |
assert NotifyBase.runtime_deps() == () |
| 386 |
assert isinstance(NotifyBase.runtime_deps(), tuple) |
| 387 |
|
| 388 |
|
| 389 |
def test_notify_base_enable_disable(): |
| 390 |
"""NotifyBase.enable() and disable() toggle the enabled flag.""" |
| 391 |
|
| 392 |
class ToggleNotification(NotifyBase): |
| 393 |
protocol = "toggle" |
| 394 |
|
| 395 |
def notify(self, *args, **kwargs): |
| 396 |
return True |
| 397 |
|
| 398 |
def url(self, **kwargs): |
| 399 |
return "toggle://" |
| 400 |
|
| 401 |
|
| 402 |
assert ToggleNotification.enabled is True |
| 403 |
|
| 404 |
|
| 405 |
ToggleNotification.disable() |
| 406 |
assert ToggleNotification.enabled is False |
| 407 |
|
| 408 |
|
| 409 |
ToggleNotification.enable() |
| 410 |
assert ToggleNotification.enabled is True |
| 411 |
|
| 412 |
|
| 413 |
ToggleNotification.disable() |
| 414 |
ToggleNotification.disable() |
| 415 |
assert ToggleNotification.enabled is False |
| 416 |
|
| 417 |
|
| 418 |
ToggleNotification.enable() |
| 419 |
ToggleNotification.enable() |
| 420 |
assert ToggleNotification.enabled is True |
| 421 |
|
| 422 |
|
| 423 |
instance = ToggleNotification(host="localhost") |
| 424 |
ToggleNotification.disable() |
| 425 |
assert instance.enabled is False |
| 426 |
ToggleNotification.enable() |
| 427 |
assert instance.enabled is True |
| 428 |
|
| 429 |
|
| 430 |
def test_notify_base_runtime_deps_override(): |
| 431 |
"""Subclasses can override runtime_deps() to declare their dependencies.""" |
| 432 |
|
| 433 |
class DepFreePlugin(NotifyBase): |
| 434 |
protocol = "depfree" |
| 435 |
|
| 436 |
def notify(self, *args, **kwargs): |
| 437 |
return True |
| 438 |
|
| 439 |
def url(self, **kwargs): |
| 440 |
return "depfree://" |
| 441 |
|
| 442 |
class SingleDepPlugin(NotifyBase): |
| 443 |
protocol = "singledep" |
| 444 |
|
| 445 |
def notify(self, *args, **kwargs): |
| 446 |
return True |
| 447 |
|
| 448 |
def url(self, **kwargs): |
| 449 |
return "singledep://" |
| 450 |
|
| 451 |
@staticmethod |
| 452 |
def runtime_deps(): |
| 453 |
return ("fakelibA",) |
| 454 |
|
| 455 |
class MultiDepPlugin(NotifyBase): |
| 456 |
protocol = "multidep" |
| 457 |
|
| 458 |
def notify(self, *args, **kwargs): |
| 459 |
return True |
| 460 |
|
| 461 |
def url(self, **kwargs): |
| 462 |
return "multidep://" |
| 463 |
|
| 464 |
@staticmethod |
| 465 |
def runtime_deps(): |
| 466 |
return ("fakelibA", "fakelibB") |
| 467 |
|
| 468 |
|
| 469 |
assert DepFreePlugin.runtime_deps() == () |
| 470 |
|
| 471 |
|
| 472 |
assert SingleDepPlugin.runtime_deps() == ("fakelibA",) |
| 473 |
|
| 474 |
|
| 475 |
deps = MultiDepPlugin.runtime_deps() |
| 476 |
assert deps == ("fakelibA", "fakelibB") |
| 477 |
assert deps[0] == "fakelibA" |
| 478 |
assert deps[1] == "fakelibB" |