| 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 json import loads |
| 29 |
|
| 30 |
|
| 31 |
import logging |
| 32 |
from unittest import mock |
| 33 |
|
| 34 |
from helpers import AppriseURLTester |
| 35 |
import pytest |
| 36 |
import requests |
| 37 |
|
| 38 |
from apprise import Apprise |
| 39 |
from apprise.plugins.one_signal import NotifyOneSignal |
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
|
| 44 |
apprise_url_tests = ( |
| 45 |
( |
| 46 |
"onesignal://", |
| 47 |
{ |
| 48 |
|
| 49 |
"instance": TypeError, |
| 50 |
}, |
| 51 |
), |
| 52 |
( |
| 53 |
"onesignal://:@/", |
| 54 |
{ |
| 55 |
|
| 56 |
"instance": TypeError, |
| 57 |
}, |
| 58 |
), |
| 59 |
( |
| 60 |
"onesignal://apikey/", |
| 61 |
{ |
| 62 |
|
| 63 |
"instance": TypeError, |
| 64 |
}, |
| 65 |
), |
| 66 |
( |
| 67 |
"onesignal://appid@%20%20/", |
| 68 |
{ |
| 69 |
|
| 70 |
"instance": TypeError, |
| 71 |
}, |
| 72 |
), |
| 73 |
( |
| 74 |
"onesignal://appid@apikey/playerid/?lang=X", |
| 75 |
{ |
| 76 |
|
| 77 |
"instance": TypeError, |
| 78 |
}, |
| 79 |
), |
| 80 |
( |
| 81 |
"onesignal://appid@apikey/", |
| 82 |
{ |
| 83 |
|
| 84 |
"instance": NotifyOneSignal, |
| 85 |
"notify_response": False, |
| 86 |
}, |
| 87 |
), |
| 88 |
( |
| 89 |
"onesignal://appid@apikey/playerid", |
| 90 |
{ |
| 91 |
|
| 92 |
"instance": NotifyOneSignal, |
| 93 |
"privacy_url": "onesignal://[email protected]/playerid", |
| 94 |
}, |
| 95 |
), |
| 96 |
( |
| 97 |
"onesignal://appid@apikey/player", |
| 98 |
{ |
| 99 |
|
| 100 |
"instance": NotifyOneSignal, |
| 101 |
|
| 102 |
"include_image": False, |
| 103 |
}, |
| 104 |
), |
| 105 |
( |
| 106 |
"onesignal://appid@apikey/@user?image=no", |
| 107 |
{ |
| 108 |
|
| 109 |
"instance": NotifyOneSignal, |
| 110 |
}, |
| 111 |
), |
| 112 |
( |
| 113 |
"onesignal://appid@apikey/[email protected]/#seg/player/@user/%20/a", |
| 114 |
{ |
| 115 |
|
| 116 |
|
| 117 |
"instance": NotifyOneSignal, |
| 118 |
}, |
| 119 |
), |
| 120 |
( |
| 121 |
"onesignal://appid@apikey?to=#segment,playerid", |
| 122 |
{ |
| 123 |
|
| 124 |
"instance": NotifyOneSignal, |
| 125 |
}, |
| 126 |
), |
| 127 |
( |
| 128 |
"onesignal://appid@apikey/#segment/@user/?batch=yes", |
| 129 |
{ |
| 130 |
|
| 131 |
"instance": NotifyOneSignal, |
| 132 |
}, |
| 133 |
), |
| 134 |
( |
| 135 |
"onesignal://appid@apikey/#segment/@user/?batch=no", |
| 136 |
{ |
| 137 |
|
| 138 |
"instance": NotifyOneSignal, |
| 139 |
}, |
| 140 |
), |
| 141 |
( |
| 142 |
"onesignal://templateid:appid@apikey/playerid", |
| 143 |
{ |
| 144 |
|
| 145 |
"instance": NotifyOneSignal, |
| 146 |
}, |
| 147 |
), |
| 148 |
( |
| 149 |
"onesignal://appid@apikey/playerid/?lang=es&subtitle=Sub", |
| 150 |
{ |
| 151 |
|
| 152 |
"instance": NotifyOneSignal, |
| 153 |
}, |
| 154 |
), |
| 155 |
( |
| 156 |
"onesignal://?apikey=abc&template=tp&app=123&to=playerid", |
| 157 |
{ |
| 158 |
|
| 159 |
"instance": NotifyOneSignal, |
| 160 |
}, |
| 161 |
), |
| 162 |
( |
| 163 |
( |
| 164 |
"onesignal://?apikey=abc&template=tp&app=123&to=playerid&body=no" |
| 165 |
"&:key1=val1&:key2=val2" |
| 166 |
), |
| 167 |
{ |
| 168 |
|
| 169 |
"instance": NotifyOneSignal, |
| 170 |
}, |
| 171 |
), |
| 172 |
( |
| 173 |
( |
| 174 |
"onesignal://?apikey=abc&template=tp&app=123&to=playerid&body=no" |
| 175 |
"&+key1=val1&+key2=val2" |
| 176 |
), |
| 177 |
{ |
| 178 |
|
| 179 |
"instance": NotifyOneSignal, |
| 180 |
}, |
| 181 |
), |
| 182 |
( |
| 183 |
"onesignal://appid@apikey/#segment/playerid/", |
| 184 |
{ |
| 185 |
"instance": NotifyOneSignal, |
| 186 |
|
| 187 |
"response": False, |
| 188 |
"requests_response_code": 999, |
| 189 |
}, |
| 190 |
), |
| 191 |
( |
| 192 |
"onesignal://appid@apikey/#segment/playerid/", |
| 193 |
{ |
| 194 |
"instance": NotifyOneSignal, |
| 195 |
|
| 196 |
|
| 197 |
"test_requests_exceptions": True, |
| 198 |
}, |
| 199 |
), |
| 200 |
) |
| 201 |
|
| 202 |
|
| 203 |
def test_plugin_onesignal_urls(): |
| 204 |
"""NotifyOneSignal() Apprise URLs.""" |
| 205 |
|
| 206 |
|
| 207 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 208 |
|
| 209 |
|
| 210 |
def test_plugin_onesignal_edge_cases(): |
| 211 |
"""NotifyOneSignal() Batch Validation.""" |
| 212 |
obj = Apprise.instantiate( |
| 213 |
"onesignal://appid@apikey/#segment/@user/playerid/[email protected]" |
| 214 |
"/?batch=yes" |
| 215 |
) |
| 216 |
|
| 217 |
assert isinstance(obj, NotifyOneSignal) |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
assert len(obj) == 4 |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
obj = Apprise.instantiate( |
| 227 |
"onesignal://appid@apikey/@user1/@user2/@user3/@user4/?batch=yes" |
| 228 |
) |
| 229 |
assert isinstance(obj, NotifyOneSignal) |
| 230 |
|
| 231 |
|
| 232 |
assert len(obj) == 1 |
| 233 |
|
| 234 |
|
| 235 |
obj = Apprise.instantiate( |
| 236 |
"onesignal://appid@apikey/@user1/@user2/@user3/@user4/?batch=no" |
| 237 |
) |
| 238 |
assert isinstance(obj, NotifyOneSignal) |
| 239 |
|
| 240 |
|
| 241 |
assert len(obj) == 4 |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
obj = Apprise.instantiate( |
| 247 |
"onesignal://appid@apikey/#segment1/#seg2/#seg3/#seg4/?batch=yes" |
| 248 |
) |
| 249 |
assert isinstance(obj, NotifyOneSignal) |
| 250 |
|
| 251 |
|
| 252 |
assert len(obj) == 1 |
| 253 |
|
| 254 |
|
| 255 |
obj = Apprise.instantiate( |
| 256 |
"onesignal://appid@apikey/#segment1/#seg2/#seg3/#seg4/?batch=no" |
| 257 |
) |
| 258 |
assert isinstance(obj, NotifyOneSignal) |
| 259 |
|
| 260 |
|
| 261 |
assert len(obj) == 4 |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
obj = Apprise.instantiate( |
| 267 |
"onesignal://appid@apikey/pid1/pid2/pid3/pid4/?batch=yes" |
| 268 |
) |
| 269 |
assert isinstance(obj, NotifyOneSignal) |
| 270 |
|
| 271 |
|
| 272 |
assert len(obj) == 1 |
| 273 |
|
| 274 |
|
| 275 |
obj = Apprise.instantiate( |
| 276 |
"onesignal://appid@apikey/pid1/pid2/pid3/pid4/?batch=no" |
| 277 |
) |
| 278 |
assert isinstance(obj, NotifyOneSignal) |
| 279 |
|
| 280 |
|
| 281 |
assert len(obj) == 4 |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
emails = ("[email protected]", "[email protected]", "[email protected]", "[email protected]") |
| 287 |
obj = Apprise.instantiate( |
| 288 |
"onesignal://appid@apikey/{}/?batch=yes".format("/".join(emails)) |
| 289 |
) |
| 290 |
assert isinstance(obj, NotifyOneSignal) |
| 291 |
|
| 292 |
|
| 293 |
assert len(obj) == 1 |
| 294 |
|
| 295 |
|
| 296 |
obj = Apprise.instantiate( |
| 297 |
"onesignal://appid@apikey/{}/?batch=no".format("/".join(emails)) |
| 298 |
) |
| 299 |
assert isinstance(obj, NotifyOneSignal) |
| 300 |
|
| 301 |
|
| 302 |
assert len(obj) == 4 |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
emails = ("[email protected]", "[email protected]", "[email protected]", "[email protected]") |
| 308 |
users = ("@user1", "@user2", "@user3", "@user4") |
| 309 |
players = ("player1", "player2", "player3", "player4") |
| 310 |
segments = ("#seg1", "#seg2", "#seg3", "#seg4") |
| 311 |
|
| 312 |
path = "{}/{}/{}/{}".format( |
| 313 |
"/".join(emails), |
| 314 |
"/".join(users), |
| 315 |
"/".join(players), |
| 316 |
"/".join(segments), |
| 317 |
) |
| 318 |
|
| 319 |
obj = Apprise.instantiate(f"onesignal://appid@apikey/{path}/?batch=yes") |
| 320 |
assert isinstance(obj, NotifyOneSignal) |
| 321 |
|
| 322 |
|
| 323 |
assert len(obj) == 4 |
| 324 |
|
| 325 |
|
| 326 |
obj = Apprise.instantiate(f"onesignal://appid@apikey/{path}/?batch=no") |
| 327 |
assert isinstance(obj, NotifyOneSignal) |
| 328 |
|
| 329 |
|
| 330 |
assert len(obj) == 16 |
| 331 |
|
| 332 |
|
| 333 |
with pytest.raises(TypeError): |
| 334 |
NotifyOneSignal( |
| 335 |
app="appid", apikey="key", targets=["@user"], custom="not-a-dict" |
| 336 |
) |
| 337 |
|
| 338 |
|
| 339 |
with pytest.raises(TypeError): |
| 340 |
NotifyOneSignal( |
| 341 |
app="appid", |
| 342 |
apikey="key", |
| 343 |
targets=["@user"], |
| 344 |
custom=[], |
| 345 |
postback="not-a-dict", |
| 346 |
) |
| 347 |
|
| 348 |
|
| 349 |
def test_plugin_onesignal_url_round_trip(): |
| 350 |
"""NotifyOneSignal() url() is reloadable without mutating targets.""" |
| 351 |
obj = Apprise.instantiate( |
| 352 |
"onesignal://appid@apikey/@user/#segment/player/[email protected]" |
| 353 |
) |
| 354 |
assert isinstance(obj, NotifyOneSignal) |
| 355 |
|
| 356 |
|
| 357 |
assert obj.targets["include_external_user_ids"] == ["@user"] |
| 358 |
assert obj.targets["included_segments"] == ["#segment"] |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
url = obj.url() |
| 363 |
reloaded = Apprise.instantiate(url) |
| 364 |
assert isinstance(reloaded, NotifyOneSignal) |
| 365 |
|
| 366 |
assert reloaded.targets["include_external_user_ids"] == ["@user"] |
| 367 |
assert reloaded.targets["included_segments"] == ["#segment"] |
| 368 |
assert reloaded.url() == url |
| 369 |
|
| 370 |
|
| 371 |
@mock.patch("requests.post") |
| 372 |
def test_plugin_onesignal_notifications(mock_post): |
| 373 |
"""OneSignal() Notifications Support.""" |
| 374 |
|
| 375 |
mock_post.return_value = requests.Request() |
| 376 |
mock_post.return_value.status_code = requests.codes.ok |
| 377 |
|
| 378 |
|
| 379 |
instance = Apprise.instantiate( |
| 380 |
"onesignal://templateid:appid@apikey/@user/?:key1=value1&+key3=value3" |
| 381 |
) |
| 382 |
|
| 383 |
|
| 384 |
assert isinstance(instance, NotifyOneSignal) |
| 385 |
|
| 386 |
response = instance.notify("hello world") |
| 387 |
assert response is True |
| 388 |
assert mock_post.call_count == 1 |
| 389 |
assert ( |
| 390 |
mock_post.call_args_list[0][0][0] |
| 391 |
== "https://api.onesignal.com/notifications" |
| 392 |
) |
| 393 |
|
| 394 |
details = mock_post.call_args_list[0] |
| 395 |
payload = loads(details[1]["data"]) |
| 396 |
|
| 397 |
assert payload == { |
| 398 |
"app_id": "appid", |
| 399 |
"contents": {"en": "hello world"}, |
| 400 |
"content_available": True, |
| 401 |
"template_id": "templateid", |
| 402 |
"custom_data": {"key1": "value1"}, |
| 403 |
"data": {"key3": "value3"}, |
| 404 |
"large_icon": ( |
| 405 |
"https://github.com/caronc/apprise" |
| 406 |
"/raw/master/apprise/assets/themes/default/apprise-info-72x72.png" |
| 407 |
), |
| 408 |
"small_icon": ( |
| 409 |
"https://github.com/caronc/apprise" |
| 410 |
"/raw/master/apprise/assets/themes/default/apprise-info-32x32.png" |
| 411 |
), |
| 412 |
"include_external_user_ids": ["@user"], |
| 413 |
} |
| 414 |
|
| 415 |
mock_post.reset_mock() |
| 416 |
|
| 417 |
|
| 418 |
instance = Apprise.instantiate( |
| 419 |
"onesignal://templateid:appid@apikey/@user/?contents=no" |
| 420 |
) |
| 421 |
|
| 422 |
|
| 423 |
assert isinstance(instance, NotifyOneSignal) |
| 424 |
|
| 425 |
response = instance.notify("hello world") |
| 426 |
assert response is True |
| 427 |
assert mock_post.call_count == 1 |
| 428 |
assert ( |
| 429 |
mock_post.call_args_list[0][0][0] |
| 430 |
== "https://api.onesignal.com/notifications" |
| 431 |
) |
| 432 |
|
| 433 |
details = mock_post.call_args_list[0] |
| 434 |
payload = loads(details[1]["data"]) |
| 435 |
|
| 436 |
assert payload == { |
| 437 |
"app_id": "appid", |
| 438 |
"content_available": True, |
| 439 |
"template_id": "templateid", |
| 440 |
"large_icon": ( |
| 441 |
"https://github.com/caronc/apprise" |
| 442 |
"/raw/master/apprise/assets/themes/default/apprise-info-72x72.png" |
| 443 |
), |
| 444 |
"small_icon": ( |
| 445 |
"https://github.com/caronc/apprise" |
| 446 |
"/raw/master/apprise/assets/themes/default/apprise-info-32x32.png" |
| 447 |
), |
| 448 |
"include_external_user_ids": ["@user"], |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
mock_post.reset_mock() |
| 453 |
|
| 454 |
response = instance.notify("hello world", title="mytitle") |
| 455 |
|
| 456 |
assert response is True |
| 457 |
assert mock_post.call_count == 1 |
| 458 |
assert ( |
| 459 |
mock_post.call_args_list[0][0][0] |
| 460 |
== "https://api.onesignal.com/notifications" |
| 461 |
) |
| 462 |
|
| 463 |
details = mock_post.call_args_list[0] |
| 464 |
payload = loads(details[1]["data"]) |
| 465 |
|
| 466 |
assert payload == { |
| 467 |
"app_id": "appid", |
| 468 |
"headings": {"en": "mytitle"}, |
| 469 |
"content_available": True, |
| 470 |
"template_id": "templateid", |
| 471 |
"large_icon": ( |
| 472 |
"https://github.com/caronc/apprise" |
| 473 |
"/raw/master/apprise/assets/themes/default/apprise-info-72x72.png" |
| 474 |
), |
| 475 |
"small_icon": ( |
| 476 |
"https://github.com/caronc/apprise" |
| 477 |
"/raw/master/apprise/assets/themes/default/apprise-info-32x32.png" |
| 478 |
), |
| 479 |
"include_external_user_ids": ["@user"], |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
instance = Apprise.instantiate( |
| 484 |
"onesignal://templateid:appid@apikey/@user/" |
| 485 |
"?:par=b64:eyJhIjoxLCJiIjoyfQ==&decode=no" |
| 486 |
) |
| 487 |
assert isinstance(instance, NotifyOneSignal) and instance.custom_data == { |
| 488 |
"par": "b64:eyJhIjoxLCJiIjoyfQ==" |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
instance = Apprise.instantiate( |
| 493 |
"onesignal://templateid:appid@apikey/@user/" |
| 494 |
"?:par=b64:eyJhIjoxLCJiIjoyfQ==&decode=yes" |
| 495 |
) |
| 496 |
assert isinstance(instance, NotifyOneSignal) and instance.custom_data == { |
| 497 |
"par": {"a": 1, "b": 2} |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
instance = Apprise.instantiate( |
| 502 |
"onesignal://templateid:appid@apikey/@user/?:par=garbage1&decode=yes" |
| 503 |
) |
| 504 |
assert isinstance(instance, NotifyOneSignal) and instance.custom_data == { |
| 505 |
"par": "garbage1" |
| 506 |
} |
| 507 |
|
| 508 |
instance = Apprise.instantiate( |
| 509 |
"onesignal://templateid:appid@apikey/@user/" |
| 510 |
"?:par=b64:garbage2&decode=yes" |
| 511 |
) |
| 512 |
assert isinstance(instance, NotifyOneSignal) and instance.custom_data == { |
| 513 |
"par": "b64:garbage2" |
| 514 |
} |
| 515 |
|
| 516 |
instance = Apprise.instantiate( |
| 517 |
"onesignal://templateid:appid@apikey/@user/" |
| 518 |
"?:par=b64:garbage3==&decode=yes" |
| 519 |
) |
| 520 |
assert isinstance(instance, NotifyOneSignal) and instance.custom_data == { |
| 521 |
"par": "b64:garbage3==" |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
instance = Apprise.instantiate( |
| 526 |
"onesignal://templateid:appid@apikey/@user/" |
| 527 |
"?:par=eyJhIjoxLCJiIjoyfQ==&:par2=123&decode=yes" |
| 528 |
) |
| 529 |
assert isinstance(instance, NotifyOneSignal) and instance.custom_data == { |
| 530 |
"par": "eyJhIjoxLCJiIjoyfQ==", |
| 531 |
"par2": "123", |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
url = ( |
| 536 |
"onesignal://templateid:appid@apikey/@user/" |
| 537 |
"?:par=b64:1234=&:par2=b64:eyJhIjoxLCJiIjoyfQ&" |
| 538 |
":par3=b64:eyJhIjoxLCJiIjoyfQ==&decode=yes" |
| 539 |
) |
| 540 |
instance = Apprise.instantiate(url) |
| 541 |
assert isinstance(instance, NotifyOneSignal) and instance.custom_data == { |
| 542 |
"par": "b64:1234=", |
| 543 |
"par2": "b64:eyJhIjoxLCJiIjoyfQ", |
| 544 |
"par3": {"a": 1, "b": 2}, |
| 545 |
} |