|
26 | 26 | # POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
|
28 | 28 | # Disable logging for a cleaner testing output |
| 29 | +from json import loads |
29 | 30 | import logging |
30 | 31 | import os |
31 | 32 | from unittest import mock |
|
273 | 274 | ) |
274 | 275 |
|
275 | 276 |
|
276 | | -def test_plugin_apprise_urls(): |
| 277 | +def test_plugin_apprise_api_urls(): |
277 | 278 | """NotifyAppriseAPI() General Checks.""" |
278 | 279 |
|
279 | 280 | # Run our general tests |
280 | 281 | AppriseURLTester(tests=apprise_url_tests).run_all() |
281 | 282 |
|
282 | 283 |
|
| 284 | + |
| 285 | +@mock.patch("requests.post") |
| 286 | +def test_notify_apprise_api_payload_check(mock_post): |
| 287 | + """NotifyAppriseAPI() payload checks""" |
| 288 | + |
| 289 | + okay_response = requests.Request() |
| 290 | + |
| 291 | + okay_response.status_code = requests.codes.ok |
| 292 | + okay_response.content = "" |
| 293 | + |
| 294 | + # Assign our mock object our return value |
| 295 | + mock_post.return_value = okay_response |
| 296 | + |
| 297 | + obj = Apprise.instantiate( |
| 298 | + "apprise://user@localhost/mytoken1/?method=form" |
| 299 | + ) |
| 300 | + assert isinstance(obj, NotifyAppriseAPI) |
| 301 | + |
| 302 | + # Test URL with Attachment |
| 303 | + path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 304 | + attach = AppriseAttachment(path) |
| 305 | + assert ( |
| 306 | + obj.notify( |
| 307 | + body="body", |
| 308 | + title="title", |
| 309 | + notify_type=NotifyType.INFO, |
| 310 | + attach=attach, |
| 311 | + ) |
| 312 | + is True |
| 313 | + ) |
| 314 | + |
| 315 | + details = mock_post.call_args_list[0] |
| 316 | + assert details[0][0] == "http://localhost/notify/mytoken1" |
| 317 | + assert details[1]["data"] == { |
| 318 | + "title": "title", |
| 319 | + "body": "body", |
| 320 | + "type": "info", |
| 321 | + "format": "text", |
| 322 | + } |
| 323 | + assert "X-Apprise-ID" in details[1]["headers"] |
| 324 | + assert details[1]["headers"].get("User-Agent") == "Apprise" |
| 325 | + assert details[1]["headers"].get("Accept") == "application/json" |
| 326 | + assert details[1]["headers"].get("X-Apprise-Recursion-Count") == "1" |
| 327 | + |
| 328 | + mock_post.reset_mock() |
| 329 | + |
| 330 | + obj = Apprise.instantiate( |
| 331 | + "apprise://user@localhost/mytoken1/?method=json" |
| 332 | + ) |
| 333 | + assert isinstance(obj, NotifyAppriseAPI) |
| 334 | + |
| 335 | + assert ( |
| 336 | + obj.notify( |
| 337 | + body="body", |
| 338 | + title="title", |
| 339 | + notify_type=NotifyType.INFO, |
| 340 | + attach=attach, |
| 341 | + ) |
| 342 | + is True |
| 343 | + ) |
| 344 | + |
| 345 | + details = mock_post.call_args_list[0] |
| 346 | + assert details[0][0] == "http://localhost/notify/mytoken1" |
| 347 | + data = loads(details[1]["data"]) |
| 348 | + assert "attachments" in data |
| 349 | + assert isinstance(data["attachments"], list) |
| 350 | + assert len(data["attachments"]) == 1 |
| 351 | + |
| 352 | + # Remove our attachment to make the next assert easier |
| 353 | + del data["attachments"] |
| 354 | + assert data == { |
| 355 | + "title": "title", |
| 356 | + "body": "body", |
| 357 | + "type": "info", |
| 358 | + "format": "text", |
| 359 | + } |
| 360 | + assert "X-Apprise-ID" in details[1]["headers"] |
| 361 | + assert details[1]["headers"].get("User-Agent") == "Apprise" |
| 362 | + assert details[1]["headers"].get("Accept") == "application/json" |
| 363 | + assert details[1]["headers"].get("X-Apprise-Recursion-Count") == "1" |
| 364 | + |
| 365 | + |
283 | 366 | @mock.patch("requests.post") |
284 | 367 | def test_notify_apprise_api_attachments(mock_post): |
285 | 368 | """NotifyAppriseAPI() Attachments.""" |
|
0 commit comments