| 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 |
from datetime import datetime, timezone |
| 29 |
from json import dumps, loads |
| 30 |
|
| 31 |
# Disable logging for a cleaner testing output |
| 32 |
import logging |
| 33 |
import os |
| 34 |
from unittest import mock |
| 35 |
|
| 36 |
from helpers import AppriseURLTester |
| 37 |
import requests |
| 38 |
|
| 39 |
from apprise import Apprise, AppriseAttachment, NotifyFormat, NotifyType |
| 40 |
from apprise.exception import AppriseImproperlyConfigured |
| 41 |
from apprise.plugins.mastodon import NotifyMastodon |
| 42 |
|
| 43 |
logging.disable(logging.CRITICAL) |
| 44 |
|
| 45 |
# Attachment Directory |
| 46 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 47 |
|
| 48 |
# Our Testing URLs |
| 49 |
apprise_url_tests = ( |
| 50 |
################################## |
| 51 |
# NotifyMastodon |
| 52 |
################################## |
| 53 |
( |
| 54 |
"mastodon://", |
| 55 |
{ |
| 56 |
# Missing Everything :) |
| 57 |
"instance": None, |
| 58 |
}, |
| 59 |
), |
| 60 |
( |
| 61 |
"mastodon://:@/", |
| 62 |
{ |
| 63 |
"instance": None, |
| 64 |
}, |
| 65 |
), |
| 66 |
( |
| 67 |
"mastodon://hostname", |
| 68 |
{ |
| 69 |
# Missing Access Token |
| 70 |
"instance": AppriseImproperlyConfigured, |
| 71 |
}, |
| 72 |
), |
| 73 |
( |
| 74 |
"toot://access_token@hostname", |
| 75 |
{ |
| 76 |
# We're good; it's a simple notification |
| 77 |
"instance": NotifyMastodon, |
| 78 |
}, |
| 79 |
), |
| 80 |
( |
| 81 |
"toots://access_token@hostname", |
| 82 |
{ |
| 83 |
# We're good; it's another simple notification |
| 84 |
"instance": NotifyMastodon, |
| 85 |
# Our expected url(privacy=True) startswith() response: |
| 86 |
"privacy_url": "mastodons://****@hostname/", |
| 87 |
}, |
| 88 |
), |
| 89 |
( |
| 90 |
"mastodon://access_token@hostname/@user/@user2", |
| 91 |
{ |
| 92 |
# We're good; it's another simple notification |
| 93 |
"instance": NotifyMastodon, |
| 94 |
# Our expected url(privacy=True) startswith() response: |
| 95 |
"privacy_url": "mastodon://****@hostname/@user/@user2", |
| 96 |
}, |
| 97 |
), |
| 98 |
( |
| 99 |
"mastodon://hostname/@user/@user2?token=abcd123", |
| 100 |
{ |
| 101 |
# Our access token can be provided as a token= variable |
| 102 |
"instance": NotifyMastodon, |
| 103 |
# Our expected url(privacy=True) startswith() response: |
| 104 |
"privacy_url": "mastodon://****@hostname/@user/@user2", |
| 105 |
}, |
| 106 |
), |
| 107 |
( |
| 108 |
"mastodon://access_token@hostname?to=@user, @user2", |
| 109 |
{ |
| 110 |
# We're good; it's another simple notification |
| 111 |
"instance": NotifyMastodon, |
| 112 |
# Our expected url(privacy=True) startswith() response: |
| 113 |
"privacy_url": "mastodon://****@hostname/@user/@user2", |
| 114 |
}, |
| 115 |
), |
| 116 |
( |
| 117 |
"mastodon://access_token@hostname/?cache=no", |
| 118 |
{ |
| 119 |
# disable cache as a test |
| 120 |
"instance": NotifyMastodon, |
| 121 |
}, |
| 122 |
), |
| 123 |
( |
| 124 |
"mastodon://access_token@hostname/?spoiler=spoiler%20text", |
| 125 |
{ |
| 126 |
# a public post |
| 127 |
"instance": NotifyMastodon, |
| 128 |
}, |
| 129 |
), |
| 130 |
( |
| 131 |
"mastodon://access_token@hostname/?language=en", |
| 132 |
{ |
| 133 |
# over-ride our language |
| 134 |
"instance": NotifyMastodon, |
| 135 |
}, |
| 136 |
), |
| 137 |
( |
| 138 |
"mastodons://access_token@hostname:8443", |
| 139 |
{ |
| 140 |
# A custom port specified |
| 141 |
"instance": NotifyMastodon, |
| 142 |
}, |
| 143 |
), |
| 144 |
( |
| 145 |
"mastodon://access_token@hostname/?key=My%20Idempotency%20Key", |
| 146 |
{ |
| 147 |
# Prevent duplicate submissions of the same status. Idempotency |
| 148 |
# keys are stored for up to 1 hour, and can be any arbitrary |
| 149 |
# string. Consider using a hash or UUID generated client-side. |
| 150 |
"instance": NotifyMastodon, |
| 151 |
}, |
| 152 |
), |
| 153 |
( |
| 154 |
"mastodon://access_token@hostname/-/%/", |
| 155 |
{ |
| 156 |
# Invalid users specified |
| 157 |
"instance": AppriseImproperlyConfigured, |
| 158 |
}, |
| 159 |
), |
| 160 |
( |
| 161 |
"mastodon://access_token@hostname?visibility=invalid", |
| 162 |
{ |
| 163 |
# An invalid visibility |
| 164 |
"instance": AppriseImproperlyConfigured, |
| 165 |
}, |
| 166 |
), |
| 167 |
( |
| 168 |
"mastodon://access_token@hostname?visibility=direct", |
| 169 |
{ |
| 170 |
# A direct message |
| 171 |
"instance": NotifyMastodon, |
| 172 |
# Expected notify() response False (because we won't |
| 173 |
# get the response we were expecting from the upstream |
| 174 |
# server |
| 175 |
"notify_response": False, |
| 176 |
}, |
| 177 |
), |
| 178 |
( |
| 179 |
"mastodon://access_token@hostname?visibility=direct", |
| 180 |
{ |
| 181 |
# A direct message |
| 182 |
"instance": NotifyMastodon, |
| 183 |
# Provide a response that allows us to look our content up |
| 184 |
"requests_response_text": { |
| 185 |
"id": "12345", |
| 186 |
"username": "test", |
| 187 |
}, |
| 188 |
}, |
| 189 |
), |
| 190 |
( |
| 191 |
"toots://access_token@hostname", |
| 192 |
{ |
| 193 |
"instance": NotifyMastodon, |
| 194 |
# Throws a series of i/o exceptions with this flag |
| 195 |
# is set and tests that we gracefully handle them |
| 196 |
"test_requests_exceptions": True, |
| 197 |
}, |
| 198 |
), |
| 199 |
) |
| 200 |
|
| 201 |
|
| 202 |
def test_plugin_mastodon_urls(): |
| 203 |
"""NotifyMastodon() Apprise URLs.""" |
| 204 |
|
| 205 |
# Run our general tests |
| 206 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 207 |
|
| 208 |
|
| 209 |
@mock.patch("requests.get") |
| 210 |
@mock.patch("requests.post") |
| 211 |
def test_plugin_mastodon_general(mock_post, mock_get): |
| 212 |
"""NotifyMastodon() General Tests.""" |
| 213 |
token = "access_key" |
| 214 |
host = "nuxref.com" |
| 215 |
|
| 216 |
response_obj = { |
| 217 |
"username": "caronc", |
| 218 |
"id": 1234, |
| 219 |
} |
| 220 |
|
| 221 |
# Epoch time: |
| 222 |
epoch = datetime.fromtimestamp(0, timezone.utc) |
| 223 |
|
| 224 |
request = mock.Mock() |
| 225 |
request.content = dumps(response_obj) |
| 226 |
request.status_code = requests.codes.ok |
| 227 |
request.headers = { |
| 228 |
"X-RateLimit-Limit": ( |
| 229 |
(datetime.now(timezone.utc) - epoch).total_seconds() |
| 230 |
), |
| 231 |
"X-RateLimit-Remaining": 1, |
| 232 |
} |
| 233 |
|
| 234 |
# Prepare Mock |
| 235 |
mock_get.return_value = request |
| 236 |
mock_post.return_value = request |
| 237 |
|
| 238 |
# Instantiate our object |
| 239 |
obj = NotifyMastodon(token=token, host=host) |
| 240 |
|
| 241 |
assert isinstance(obj, NotifyMastodon) |
| 242 |
assert isinstance(obj.url(), str) |
| 243 |
|
| 244 |
# apprise room was found |
| 245 |
assert obj.send(body="test") is True |
| 246 |
|
| 247 |
# Change our status code and try again |
| 248 |
request.status_code = 403 |
| 249 |
assert obj.send(body="test") is False |
| 250 |
assert obj.ratelimit_remaining == 1 |
| 251 |
|
| 252 |
# Return the status |
| 253 |
request.status_code = requests.codes.ok |
| 254 |
# Force a reset |
| 255 |
request.headers["X-RateLimit-Remaining"] = 0 |
| 256 |
# behind the scenes, it should cause us to update our rate limit |
| 257 |
assert obj.send(body="test") is True |
| 258 |
assert obj.ratelimit_remaining == 0 |
| 259 |
|
| 260 |
# This should cause us to block |
| 261 |
request.headers["X-RateLimit-Remaining"] = 10 |
| 262 |
assert obj.send(body="test") is True |
| 263 |
assert obj.ratelimit_remaining == 10 |
| 264 |
|
| 265 |
# Handle cases where we simply couldn't get this field |
| 266 |
del request.headers["X-RateLimit-Remaining"] |
| 267 |
assert obj.send(body="test") is True |
| 268 |
# It remains set to the last value |
| 269 |
assert obj.ratelimit_remaining == 10 |
| 270 |
|
| 271 |
# Reset our variable back to 1 |
| 272 |
request.headers["X-RateLimit-Remaining"] = 1 |
| 273 |
|
| 274 |
# Handle cases where our epoch time is wrong |
| 275 |
del request.headers["X-RateLimit-Limit"] |
| 276 |
assert obj.send(body="test") is True |
| 277 |
|
| 278 |
# Return our object, but place it in the future forcing us to block |
| 279 |
request.headers["X-RateLimit-Limit"] = ( |
| 280 |
datetime.now(timezone.utc) - epoch |
| 281 |
).total_seconds() + 1 |
| 282 |
request.headers["X-RateLimit-Remaining"] = 0 |
| 283 |
obj.ratelimit_remaining = 0 |
| 284 |
assert obj.send(body="test") is True |
| 285 |
|
| 286 |
# Return our object, but place it in the future forcing us to block |
| 287 |
request.headers["X-RateLimit-Limit"] = ( |
| 288 |
datetime.now(timezone.utc) - epoch |
| 289 |
).total_seconds() - 1 |
| 290 |
request.headers["X-RateLimit-Remaining"] = 0 |
| 291 |
obj.ratelimit_remaining = 0 |
| 292 |
assert obj.send(body="test") is True |
| 293 |
|
| 294 |
# Return our limits to always work |
| 295 |
request.headers["X-RateLimit-Limit"] = ( |
| 296 |
datetime.now(timezone.utc) - epoch |
| 297 |
).total_seconds() |
| 298 |
request.headers["X-RateLimit-Remaining"] = 1 |
| 299 |
obj.ratelimit_remaining = 1 |
| 300 |
|
| 301 |
# Alter pending targets |
| 302 |
obj.targets.append("usera") |
| 303 |
request.content = dumps(response_obj) |
| 304 |
response_obj = { |
| 305 |
"username": "usera", |
| 306 |
"id": 4321, |
| 307 |
} |
| 308 |
|
| 309 |
# Cause content response to be None |
| 310 |
request.content = None |
| 311 |
assert obj.send(body="test") is True |
| 312 |
|
| 313 |
# Invalid JSON |
| 314 |
request.content = "{" |
| 315 |
assert obj.send(body="test") is True |
| 316 |
|
| 317 |
# Return it to a parseable string |
| 318 |
request.content = "{}" |
| 319 |
|
| 320 |
results = NotifyMastodon.parse_url( |
| 321 |
f"mastodon://{token}@{host}/@user?visbility=direct" |
| 322 |
) |
| 323 |
assert isinstance(results, dict) |
| 324 |
assert "@user" in results["targets"] |
| 325 |
|
| 326 |
# cause a json parsing issue now |
| 327 |
response_obj = None |
| 328 |
assert obj.send(body="test") is True |
| 329 |
|
| 330 |
response_obj = "{" |
| 331 |
assert obj.send(body="test") is True |
| 332 |
|
| 333 |
mock_get.reset_mock() |
| 334 |
mock_post.reset_mock() |
| 335 |
|
| 336 |
# |
| 337 |
# Test our lazy lookups |
| 338 |
# |
| 339 |
|
| 340 |
# Prepare a good status response |
| 341 |
request = mock.Mock() |
| 342 |
request.content = dumps({"id": "1234", "username": "caronc"}) |
| 343 |
request.status_code = requests.codes.ok |
| 344 |
mock_get.return_value = request |
| 345 |
|
| 346 |
mastodon_url = "mastodons://key@host?visibility=direct" |
| 347 |
obj = Apprise.instantiate(mastodon_url) |
| 348 |
obj._whoami(lazy=True) |
| 349 |
assert mock_get.call_count == 1 |
| 350 |
assert ( |
| 351 |
mock_get.call_args_list[0][0][0] |
| 352 |
== "https://host/api/v1/accounts/verify_credentials" |
| 353 |
) |
| 354 |
|
| 355 |
mock_get.reset_mock() |
| 356 |
obj._whoami(lazy=True) |
| 357 |
assert mock_get.call_count == 0 |
| 358 |
|
| 359 |
mock_get.reset_mock() |
| 360 |
obj._whoami(lazy=False) |
| 361 |
assert mock_get.call_count == 1 |
| 362 |
assert ( |
| 363 |
mock_get.call_args_list[0][0][0] |
| 364 |
== "https://host/api/v1/accounts/verify_credentials" |
| 365 |
) |
| 366 |
|
| 367 |
|
| 368 |
@mock.patch("requests.post") |
| 369 |
def test_plugin_mastodon_pings(mock_post): |
| 370 |
"""NotifyMastodon() Ping Checks.""" |
| 371 |
token = "access_key" |
| 372 |
host = "nuxref.com" |
| 373 |
|
| 374 |
request = mock.Mock() |
| 375 |
request.content = dumps({}) |
| 376 |
request.status_code = requests.codes.ok |
| 377 |
request.headers = {} |
| 378 |
|
| 379 |
mock_post.return_value = request |
| 380 |
|
| 381 |
body = ( |
| 382 |
"Body **#déjà_vu** and @abc, plus #再会. " |
| 383 |
"Ignore ##bad, #bad%, #123, email@miss, and duplicate " |
| 384 |
"@abc #déjà_vu." |
| 385 |
) |
| 386 |
|
| 387 |
obj = NotifyMastodon( |
| 388 |
token=token, |
| 389 |
host=host, |
| 390 |
format=NotifyFormat.MARKDOWN, |
| 391 |
ping="@abc,#déjà_vu,#radarr,radarr,#123,bad%", |
| 392 |
) |
| 393 |
|
| 394 |
assert obj.send(body=body) is True |
| 395 |
assert mock_post.call_count == 1 |
| 396 |
|
| 397 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 398 |
|
| 399 |
# Mentions and hashtags are appended in a plain form Mastodon can resolve. |
| 400 |
assert payload["status"] == f"{body} @abc #déjà_vu #再会 #radarr" |
| 401 |
|
| 402 |
mock_post.reset_mock() |
| 403 |
|
| 404 |
body = "Body with #solo only." |
| 405 |
|
| 406 |
obj = NotifyMastodon( |
| 407 |
token=token, |
| 408 |
host=host, |
| 409 |
format=NotifyFormat.MARKDOWN, |
| 410 |
) |
| 411 |
|
| 412 |
assert obj.send(body=body) is True |
| 413 |
assert mock_post.call_count == 1 |
| 414 |
|
| 415 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 416 |
|
| 417 |
# Markdown statuses can contribute visible mention and hashtag tokens. |
| 418 |
assert payload["status"] == f"{body} #solo" |
| 419 |
|
| 420 |
mock_post.reset_mock() |
| 421 |
|
| 422 |
body = "<p>No tags in this formatted body.</p>" |
| 423 |
|
| 424 |
obj = NotifyMastodon( |
| 425 |
token=token, |
| 426 |
host=host, |
| 427 |
format=NotifyFormat.HTML, |
| 428 |
) |
| 429 |
|
| 430 |
assert obj.send(body=body) is True |
| 431 |
assert mock_post.call_count == 1 |
| 432 |
|
| 433 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 434 |
|
| 435 |
# When no valid tag tokens are found, the status is not decorated. |
| 436 |
assert payload["status"] == body |
| 437 |
|
| 438 |
mock_post.reset_mock() |
| 439 |
|
| 440 |
obj = NotifyMastodon( |
| 441 |
token=token, |
| 442 |
host=host, |
| 443 |
ping="@caronc,#apprise,#alerts", |
| 444 |
) |
| 445 |
|
| 446 |
assert isinstance(obj.url(), str) |
| 447 |
assert "ping=" in obj.url() |
| 448 |
|
| 449 |
results = NotifyMastodon.parse_url(obj.url()) |
| 450 |
assert results["ping"] == "#alerts,#apprise,@caronc" |
| 451 |
|
| 452 |
assert NotifyMastodon.normalize_ping_token(" ") is None |
| 453 |
assert NotifyMastodon.normalize_ping_token("alerts") is None |
| 454 |
assert NotifyMastodon.valid_hashtag("#bad%") is False |
| 455 |
assert obj.ping_tokens("bad%", normalize=True) == [] |
| 456 |
|
| 457 |
obj = NotifyMastodon( |
| 458 |
token=token, |
| 459 |
host=host, |
| 460 |
targets="#Apprise,#apprise", |
| 461 |
ping="#Media,#media", |
| 462 |
) |
| 463 |
assert obj.hashtags == ["#Apprise"] |
| 464 |
assert obj.ping == ["#Media"] |
| 465 |
|
| 466 |
mock_post.reset_mock() |
| 467 |
|
| 468 |
results = NotifyMastodon.parse_url( |
| 469 |
f"mastodon://{token}@{host}/#apprise?ping=#apprise,#media" |
| 470 |
) |
| 471 |
obj = NotifyMastodon(**results) |
| 472 |
|
| 473 |
assert obj.send(body="Body") is True |
| 474 |
assert mock_post.call_count == 1 |
| 475 |
|
| 476 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 477 |
|
| 478 |
# Hashtag path entries and configured pings share de-duplication. |
| 479 |
assert payload["status"] == "Body #apprise #media" |
| 480 |
|
| 481 |
rebuilt_url = obj.url() |
| 482 |
assert "/%23apprise" in rebuilt_url |
| 483 |
assert "ping=%23apprise%2C%23media" in rebuilt_url |
| 484 |
|
| 485 |
results = NotifyMastodon.parse_url(rebuilt_url) |
| 486 |
assert results["targets"] == ["#apprise"] |
| 487 |
assert results["ping"] == "#apprise,#media" |
| 488 |
|
| 489 |
mock_post.reset_mock() |
| 490 |
|
| 491 |
obj = NotifyMastodon(token=token, host=host, ping="#apprise") |
| 492 |
|
| 493 |
assert obj.send(body="a" * obj.body_maxlen) is True |
| 494 |
assert mock_post.call_count == 1 |
| 495 |
|
| 496 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 497 |
|
| 498 |
# The effective body limit reserves room for configured status tokens. |
| 499 |
assert obj.body_maxlen == obj.mastodon_body_maxlen - len(" #apprise") |
| 500 |
assert len(payload["status"]) == obj.mastodon_body_maxlen |
| 501 |
assert payload["status"].endswith(" #apprise") |
| 502 |
|
| 503 |
|
| 504 |
@mock.patch("requests.post") |
| 505 |
def test_plugin_mastodon_text(mock_post): |
| 506 |
"""NotifyMastodon() Text Checks.""" |
| 507 |
token = "access_key" |
| 508 |
host = "nuxref.com" |
| 509 |
|
| 510 |
request = mock.Mock() |
| 511 |
request.content = dumps({}) |
| 512 |
request.status_code = requests.codes.ok |
| 513 |
request.headers = {} |
| 514 |
|
| 515 |
mock_post.return_value = request |
| 516 |
|
| 517 |
body = "Plain text keeps #déjà_vu and @abc exactly where they are." |
| 518 |
|
| 519 |
obj = NotifyMastodon( |
| 520 |
token=token, |
| 521 |
host=host, |
| 522 |
format=NotifyFormat.TEXT, |
| 523 |
) |
| 524 |
|
| 525 |
assert obj.send(body=body) is True |
| 526 |
assert mock_post.call_count == 1 |
| 527 |
|
| 528 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 529 |
|
| 530 |
# Plain text is already suitable for Mastodon and is left untouched. |
| 531 |
assert payload["status"] == body |
| 532 |
|
| 533 |
mock_post.reset_mock() |
| 534 |
|
| 535 |
obj = NotifyMastodon( |
| 536 |
token=token, |
| 537 |
host=host, |
| 538 |
format=NotifyFormat.TEXT, |
| 539 |
targets="@media,#homeassistant", |
| 540 |
ping="radarr,@media,#homeassistant,#alerts,#123,bad%", |
| 541 |
) |
| 542 |
|
| 543 |
assert obj.send(body=body) is True |
| 544 |
assert mock_post.call_count == 1 |
| 545 |
|
| 546 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 547 |
|
| 548 |
# Explicit path entries and ping values are appended without scanning for |
| 549 |
# new plain text body tokens. |
| 550 |
assert payload["status"] == f"{body} #alerts #homeassistant @media" |
| 551 |
|
| 552 |
|
| 553 |
@mock.patch("requests.get") |
| 554 |
@mock.patch("requests.post") |
| 555 |
def test_plugin_mastodon_direct(mock_post, mock_get): |
| 556 |
"""NotifyMastodon() Direct Checks.""" |
| 557 |
token = "access_key" |
| 558 |
host = "nuxref.com" |
| 559 |
|
| 560 |
request = mock.Mock() |
| 561 |
request.content = dumps({}) |
| 562 |
request.status_code = requests.codes.ok |
| 563 |
request.headers = {} |
| 564 |
|
| 565 |
mock_post.return_value = request |
| 566 |
|
| 567 |
body = "Direct body already contains @target." |
| 568 |
|
| 569 |
obj = NotifyMastodon( |
| 570 |
token=token, |
| 571 |
host=host, |
| 572 |
targets="@target", |
| 573 |
visibility="direct", |
| 574 |
) |
| 575 |
|
| 576 |
assert obj.send(body=body) is True |
| 577 |
assert mock_post.call_count == 1 |
| 578 |
assert mock_get.call_count == 0 |
| 579 |
|
| 580 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 581 |
|
| 582 |
# Explicit direct targets do not require a lookup for the authenticated |
| 583 |
# account, and duplicate body mentions are not prepended. |
| 584 |
assert payload["status"] == body |
| 585 |
|
| 586 |
mock_post.reset_mock() |
| 587 |
|
| 588 |
body = "Direct body contains email@target only." |
| 589 |
|
| 590 |
obj = NotifyMastodon( |
| 591 |
token=token, |
| 592 |
host=host, |
| 593 |
targets="@target", |
| 594 |
visibility="direct", |
| 595 |
) |
| 596 |
|
| 597 |
assert obj.send(body=body) is True |
| 598 |
assert mock_post.call_count == 1 |
| 599 |
assert mock_get.call_count == 0 |
| 600 |
|
| 601 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 602 |
|
| 603 |
# Email-like text is not treated as a delivered direct-message mention. |
| 604 |
assert payload["status"] == f"@target {body}" |
| 605 |
|
| 606 |
|
| 607 |
@mock.patch("requests.post") |
| 608 |
@mock.patch("requests.get") |
| 609 |
def test_plugin_mastodon_attachments(mock_get, mock_post): |
| 610 |
"""NotifyMastodon() Toot Attachment Checks.""" |
| 611 |
akey = "access_key" |
| 612 |
host = "nuxref.com" |
| 613 |
username = "caronc" |
| 614 |
|
| 615 |
# Prepare a good status response |
| 616 |
good_response_obj = { |
| 617 |
"id": "1234", |
| 618 |
} |
| 619 |
|
| 620 |
good_response = mock.Mock() |
| 621 |
good_response.content = dumps(good_response_obj) |
| 622 |
good_response.status_code = requests.codes.ok |
| 623 |
|
| 624 |
# Prepare a good whoami response |
| 625 |
good_whoami_response_obj = { |
| 626 |
"username": username, |
| 627 |
"id": "9876", |
| 628 |
} |
| 629 |
|
| 630 |
good_whoami_response = mock.Mock() |
| 631 |
good_whoami_response.content = dumps(good_whoami_response_obj) |
| 632 |
good_whoami_response.status_code = requests.codes.ok |
| 633 |
|
| 634 |
# Prepare bad response |
| 635 |
bad_response = mock.Mock() |
| 636 |
bad_response.content = dumps({}) |
| 637 |
bad_response.status_code = requests.codes.internal_server_error |
| 638 |
|
| 639 |
# Prepare a good media response |
| 640 |
good_media_response = mock.Mock() |
| 641 |
good_media_response.content = dumps( |
| 642 |
{ |
| 643 |
"id": "710511363345354753", |
| 644 |
"file_mime": "image/jpeg", |
| 645 |
} |
| 646 |
) |
| 647 |
good_media_response.status_code = requests.codes.ok |
| 648 |
|
| 649 |
# |
| 650 |
# Start testing using fixtures above |
| 651 |
# |
| 652 |
mock_post.side_effect = [good_media_response, good_response] |
| 653 |
mock_get.return_value = good_whoami_response |
| 654 |
|
| 655 |
mastodon_url = f"mastodon://{akey}@{host}" |
| 656 |
|
| 657 |
# attach our content |
| 658 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 659 |
|
| 660 |
# instantiate our object |
| 661 |
obj = Apprise.instantiate(mastodon_url) |
| 662 |
|
| 663 |
# Send our notification |
| 664 |
assert ( |
| 665 |
bool( |
| 666 |
obj.notify( |
| 667 |
body="body", |
| 668 |
title="title", |
| 669 |
notify_type=NotifyType.INFO, |
| 670 |
attach=attach, |
| 671 |
) |
| 672 |
) |
| 673 |
is True |
| 674 |
) |
| 675 |
|
| 676 |
# Test our call count |
| 677 |
assert mock_get.call_count == 0 |
| 678 |
assert mock_post.call_count == 2 |
| 679 |
assert ( |
| 680 |
mock_post.call_args_list[0][0][0] == "http://nuxref.com/api/v1/media" |
| 681 |
) |
| 682 |
assert ( |
| 683 |
mock_post.call_args_list[1][0][0] |
| 684 |
== "http://nuxref.com/api/v1/statuses" |
| 685 |
) |
| 686 |
|
| 687 |
# Test our media payload |
| 688 |
assert "files" in mock_post.call_args_list[0][1] |
| 689 |
assert "file" in mock_post.call_args_list[0][1]["files"] |
| 690 |
assert ( |
| 691 |
mock_post.call_args_list[0][1]["files"]["file"][0] |
| 692 |
== "apprise-test.gif" |
| 693 |
) |
| 694 |
|
| 695 |
# Test our status payload |
| 696 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 697 |
assert "status" in payload |
| 698 |
assert payload["status"] == "title\r\nbody" |
| 699 |
assert "sensitive" in payload |
| 700 |
assert payload["sensitive"] is False |
| 701 |
assert "media_ids" in payload |
| 702 |
assert isinstance(payload["media_ids"], list) |
| 703 |
assert len(payload["media_ids"]) == 1 |
| 704 |
assert payload["media_ids"][0] == "710511363345354753" |
| 705 |
|
| 706 |
# Verify we don't set incorrect keys not otherwise specified |
| 707 |
assert "spoiler_text" not in payload |
| 708 |
|
| 709 |
mock_get.reset_mock() |
| 710 |
mock_post.reset_mock() |
| 711 |
|
| 712 |
# |
| 713 |
# Handle the query again, but this time perform a direct message |
| 714 |
# requiring us to look up who we are |
| 715 |
# |
| 716 |
mock_post.side_effect = [good_media_response, good_response] |
| 717 |
mock_get.return_value = good_whoami_response |
| 718 |
|
| 719 |
mastodon_url = f"mastodon://{akey}@{host}?visibility=direct" |
| 720 |
|
| 721 |
# attach our content |
| 722 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 723 |
|
| 724 |
# instantiate our object |
| 725 |
obj = Apprise.instantiate(mastodon_url) |
| 726 |
|
| 727 |
# Send our notification |
| 728 |
assert ( |
| 729 |
bool( |
| 730 |
obj.notify( |
| 731 |
body="body", |
| 732 |
title="title", |
| 733 |
notify_type=NotifyType.INFO, |
| 734 |
attach=attach, |
| 735 |
) |
| 736 |
) |
| 737 |
is True |
| 738 |
) |
| 739 |
|
| 740 |
# Test our call count |
| 741 |
assert mock_get.call_count == 1 |
| 742 |
assert mock_post.call_count == 2 |
| 743 |
assert ( |
| 744 |
mock_get.call_args_list[0][0][0] |
| 745 |
== "http://nuxref.com/api/v1/accounts/verify_credentials" |
| 746 |
) |
| 747 |
assert ( |
| 748 |
mock_post.call_args_list[0][0][0] == "http://nuxref.com/api/v1/media" |
| 749 |
) |
| 750 |
assert ( |
| 751 |
mock_post.call_args_list[1][0][0] |
| 752 |
== "http://nuxref.com/api/v1/statuses" |
| 753 |
) |
| 754 |
|
| 755 |
# Test our status payload |
| 756 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 757 |
assert "status" in payload |
| 758 |
# Our ID was added into the payload |
| 759 |
assert payload["status"] == "@caronc title\r\nbody" |
| 760 |
assert "sensitive" in payload |
| 761 |
assert payload["sensitive"] is False |
| 762 |
assert "media_ids" in payload |
| 763 |
assert isinstance(payload["media_ids"], list) |
| 764 |
assert len(payload["media_ids"]) == 1 |
| 765 |
assert payload["media_ids"][0] == "710511363345354753" |
| 766 |
|
| 767 |
mock_get.reset_mock() |
| 768 |
mock_post.reset_mock() |
| 769 |
|
| 770 |
# Store 3 attachments |
| 771 |
attach = ( |
| 772 |
AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")), |
| 773 |
AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.png")), |
| 774 |
AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.jpeg")), |
| 775 |
) |
| 776 |
|
| 777 |
# Prepare a good media response |
| 778 |
mr1 = mock.Mock() |
| 779 |
mr1.content = dumps( |
| 780 |
{ |
| 781 |
"id": "1", |
| 782 |
"file_mime": "image/gif", |
| 783 |
} |
| 784 |
) |
| 785 |
mr1.status_code = requests.codes.ok |
| 786 |
|
| 787 |
mr2 = mock.Mock() |
| 788 |
mr2.content = dumps( |
| 789 |
{ |
| 790 |
"id": "2", |
| 791 |
"file_mime": "image/png", |
| 792 |
} |
| 793 |
) |
| 794 |
mr2.status_code = requests.codes.ok |
| 795 |
|
| 796 |
mr3 = mock.Mock() |
| 797 |
mr3.content = dumps( |
| 798 |
{ |
| 799 |
"id": "3", |
| 800 |
"file_mime": "image/jpeg", |
| 801 |
} |
| 802 |
) |
| 803 |
mr3.status_code = requests.codes.ok |
| 804 |
|
| 805 |
# Return 3 good uploads and a good status response |
| 806 |
mock_post.side_effect = [mr1, mr2, mr3, good_response, good_response] |
| 807 |
mock_get.return_value = good_whoami_response |
| 808 |
|
| 809 |
# instantiate our object |
| 810 |
mastodon_url = ( |
| 811 |
f"mastodons://{akey}@{host}?visibility=direct&sensitive=yes&key=abcd" |
| 812 |
) |
| 813 |
obj = Apprise.instantiate(mastodon_url) |
| 814 |
|
| 815 |
# Send ourselves a direct message where our ID was already found |
| 816 |
# in the body. This smart detection method will prevent us from |
| 817 |
# adding the @caronc to the begining of the same message (since it's a |
| 818 |
# direct message) |
| 819 |
assert ( |
| 820 |
bool( |
| 821 |
obj.notify( |
| 822 |
body="Check this out @caronc", |
| 823 |
title="Apprise", |
| 824 |
notify_type=NotifyType.INFO, |
| 825 |
attach=attach, |
| 826 |
) |
| 827 |
) |
| 828 |
is True |
| 829 |
) |
| 830 |
|
| 831 |
# Test our call count |
| 832 |
assert mock_get.call_count == 1 |
| 833 |
assert mock_post.call_count == 5 |
| 834 |
assert ( |
| 835 |
mock_post.call_args_list[0][0][0] == "https://nuxref.com/api/v1/media" |
| 836 |
) |
| 837 |
assert ( |
| 838 |
mock_post.call_args_list[1][0][0] == "https://nuxref.com/api/v1/media" |
| 839 |
) |
| 840 |
assert ( |
| 841 |
mock_post.call_args_list[2][0][0] == "https://nuxref.com/api/v1/media" |
| 842 |
) |
| 843 |
# Our status's will batch up and send the last 2 images in one |
| 844 |
# and our animated gif in one. |
| 845 |
assert ( |
| 846 |
mock_post.call_args_list[3][0][0] |
| 847 |
== "https://nuxref.com/api/v1/statuses" |
| 848 |
) |
| 849 |
assert ( |
| 850 |
mock_post.call_args_list[4][0][0] |
| 851 |
== "https://nuxref.com/api/v1/statuses" |
| 852 |
) |
| 853 |
assert ( |
| 854 |
mock_get.call_args_list[0][0][0] |
| 855 |
== "https://nuxref.com/api/v1/accounts/verify_credentials" |
| 856 |
) |
| 857 |
|
| 858 |
# Test our status payload |
| 859 |
payload = loads(mock_post.call_args_list[3][1]["data"]) |
| 860 |
assert "status" in payload |
| 861 |
assert payload["status"] == "Apprise\r\nCheck this out @caronc" |
| 862 |
assert "sensitive" in payload |
| 863 |
assert payload["sensitive"] is True |
| 864 |
assert "language" not in payload |
| 865 |
assert "Idempotency-Key" in payload |
| 866 |
assert payload["Idempotency-Key"] == "abcd" |
| 867 |
assert "media_ids" in payload |
| 868 |
assert isinstance(payload["media_ids"], list) |
| 869 |
assert len(payload["media_ids"]) == 1 |
| 870 |
assert payload["media_ids"][0] == "1" |
| 871 |
|
| 872 |
payload = loads(mock_post.call_args_list[4][1]["data"]) |
| 873 |
assert "status" in payload |
| 874 |
assert payload["status"] == "02/02" |
| 875 |
assert "sensitive" in payload |
| 876 |
assert payload["sensitive"] is False |
| 877 |
assert "language" not in payload |
| 878 |
assert "Idempotency-Key" in payload |
| 879 |
assert payload["Idempotency-Key"] == "abcd-part01" |
| 880 |
assert "media_ids" in payload |
| 881 |
assert isinstance(payload["media_ids"], list) |
| 882 |
assert len(payload["media_ids"]) == 2 |
| 883 |
assert "2" in payload["media_ids"] |
| 884 |
assert "3" in payload["media_ids"] |
| 885 |
|
| 886 |
# A second call does not cause us to look up our ID as we already know it |
| 887 |
mock_get.reset_mock() |
| 888 |
mock_post.reset_mock() |
| 889 |
mock_post.side_effect = [mr1, mr2, mr3, good_response, good_response] |
| 890 |
mock_get.return_value = good_whoami_response |
| 891 |
assert ( |
| 892 |
bool( |
| 893 |
obj.notify( |
| 894 |
body="Check this out @caronc", |
| 895 |
title="Apprise", |
| 896 |
notify_type=NotifyType.INFO, |
| 897 |
attach=attach, |
| 898 |
) |
| 899 |
) |
| 900 |
is True |
| 901 |
) |
| 902 |
|
| 903 |
# Same number of posts |
| 904 |
assert mock_post.call_count == 5 |
| 905 |
# But no lookup was made |
| 906 |
assert mock_get.call_count == 0 |
| 907 |
|
| 908 |
mock_get.reset_mock() |
| 909 |
mock_post.reset_mock() |
| 910 |
|
| 911 |
# Prepare an attach list |
| 912 |
attach = ( |
| 913 |
os.path.join(TEST_VAR_DIR, "apprise-test.png"), |
| 914 |
os.path.join(TEST_VAR_DIR, "apprise-test.jpeg"), |
| 915 |
) |
| 916 |
|
| 917 |
mock_post.side_effect = [mr2, mr3, good_response, good_response] |
| 918 |
mock_get.return_value = good_whoami_response |
| 919 |
|
| 920 |
# instantiate our object (but turn off the batch mode) |
| 921 |
mastodon_url = f"mastodons://{akey}@{host}?batch=no" |
| 922 |
obj = Apprise.instantiate(mastodon_url) |
| 923 |
|
| 924 |
assert ( |
| 925 |
bool( |
| 926 |
obj.notify( |
| 927 |
body="Check this out @caronc", |
| 928 |
title="Apprise", |
| 929 |
notify_type=NotifyType.INFO, |
| 930 |
attach=attach, |
| 931 |
) |
| 932 |
) |
| 933 |
is True |
| 934 |
) |
| 935 |
|
| 936 |
# 2 attachments + 2 different status messages |
| 937 |
assert mock_post.call_count == 4 |
| 938 |
|
| 939 |
# But no lookup was made |
| 940 |
assert mock_get.call_count == 0 |
| 941 |
|
| 942 |
assert ( |
| 943 |
mock_post.call_args_list[0][0][0] == "https://nuxref.com/api/v1/media" |
| 944 |
) |
| 945 |
assert ( |
| 946 |
mock_post.call_args_list[1][0][0] == "https://nuxref.com/api/v1/media" |
| 947 |
) |
| 948 |
assert ( |
| 949 |
mock_post.call_args_list[2][0][0] |
| 950 |
== "https://nuxref.com/api/v1/statuses" |
| 951 |
) |
| 952 |
assert ( |
| 953 |
mock_post.call_args_list[3][0][0] |
| 954 |
== "https://nuxref.com/api/v1/statuses" |
| 955 |
) |
| 956 |
|
| 957 |
mock_get.reset_mock() |
| 958 |
mock_post.reset_mock() |
| 959 |
|
| 960 |
# Prepare a bad media response |
| 961 |
bad_response = mock.Mock() |
| 962 |
bad_response.status_code = requests.codes.internal_server_error |
| 963 |
|
| 964 |
bad_responses = ( |
| 965 |
dumps({"error": "authorized scopes"}), |
| 966 |
"", |
| 967 |
) |
| 968 |
|
| 969 |
# |
| 970 |
# Test our Media failures |
| 971 |
# |
| 972 |
|
| 973 |
# Try several bad responses so we can capture the block of code where |
| 974 |
# we try to help the end user to remind them what scope they're missing |
| 975 |
for response in bad_responses: |
| 976 |
mock_post.side_effect = [good_media_response, bad_response] |
| 977 |
bad_response.content = response |
| 978 |
|
| 979 |
# instantiate our object |
| 980 |
mastodon_url = ( |
| 981 |
f"mastodons://{akey}@{host}?visibility=public&spoiler=uhoh" |
| 982 |
) |
| 983 |
obj = Apprise.instantiate(mastodon_url) |
| 984 |
|
| 985 |
# Our notification will fail now since our toot will error out |
| 986 |
# This is the same test as above, except our error response isn't |
| 987 |
# parseable |
| 988 |
assert ( |
| 989 |
bool( |
| 990 |
obj.notify( |
| 991 |
body="body", |
| 992 |
title="title", |
| 993 |
notify_type=NotifyType.INFO, |
| 994 |
attach=attach, |
| 995 |
) |
| 996 |
) |
| 997 |
is False |
| 998 |
) |
| 999 |
|
| 1000 |
# Test our call count |
| 1001 |
assert mock_get.call_count == 0 |
| 1002 |
assert mock_post.call_count == 2 |
| 1003 |
assert ( |
| 1004 |
mock_post.call_args_list[0][0][0] |
| 1005 |
== "https://nuxref.com/api/v1/media" |
| 1006 |
) |
| 1007 |
assert ( |
| 1008 |
mock_post.call_args_list[1][0][0] |
| 1009 |
== "https://nuxref.com/api/v1/media" |
| 1010 |
) |
| 1011 |
|
| 1012 |
mock_get.reset_mock() |
| 1013 |
mock_post.reset_mock() |
| 1014 |
|
| 1015 |
# |
| 1016 |
# Test our Status failures |
| 1017 |
# |
| 1018 |
|
| 1019 |
# Try several bad responses so we can capture the block of code where |
| 1020 |
# we try to help the end user to remind them what scope they're missing |
| 1021 |
for response in bad_responses: |
| 1022 |
mock_post.side_effect = [bad_response] |
| 1023 |
bad_response.content = response |
| 1024 |
|
| 1025 |
# instantiate our object |
| 1026 |
mastodon_url = f"mastodons://{akey}@{host}" |
| 1027 |
obj = Apprise.instantiate(mastodon_url) |
| 1028 |
|
| 1029 |
# Our notification will fail now since our toot will error out |
| 1030 |
# This is the same test as above, except our error response isn't |
| 1031 |
# parseable |
| 1032 |
assert ( |
| 1033 |
bool( |
| 1034 |
obj.notify( |
| 1035 |
body="body", title="title", notify_type=NotifyType.INFO |
| 1036 |
) |
| 1037 |
) |
| 1038 |
is False |
| 1039 |
) |
| 1040 |
|
| 1041 |
# Test our call count |
| 1042 |
assert mock_get.call_count == 0 |
| 1043 |
assert mock_post.call_count == 1 |
| 1044 |
assert ( |
| 1045 |
mock_post.call_args_list[0][0][0] |
| 1046 |
== "https://nuxref.com/api/v1/statuses" |
| 1047 |
) |
| 1048 |
|
| 1049 |
mock_get.reset_mock() |
| 1050 |
mock_post.reset_mock() |
| 1051 |
|
| 1052 |
# |
| 1053 |
# Test our whoami failures |
| 1054 |
# |
| 1055 |
|
| 1056 |
# Try several bad responses so we can capture the block of code where |
| 1057 |
# we try to help the end user to remind them what scope they're missing |
| 1058 |
for response in bad_responses: |
| 1059 |
mock_get.side_effect = [bad_response] |
| 1060 |
bad_response.content = response |
| 1061 |
|
| 1062 |
# instantiate our object |
| 1063 |
mastodon_url = f"mastodons://{akey}@{host}?visibility=direct" |
| 1064 |
obj = Apprise.instantiate(mastodon_url) |
| 1065 |
|
| 1066 |
# Our notification will fail now since our toot will error out |
| 1067 |
# This is the same test as above, except our error response isn't |
| 1068 |
# parseable |
| 1069 |
assert ( |
| 1070 |
bool( |
| 1071 |
obj.notify( |
| 1072 |
body="body", title="title", notify_type=NotifyType.INFO |
| 1073 |
) |
| 1074 |
) |
| 1075 |
is False |
| 1076 |
) |
| 1077 |
|
| 1078 |
# Test our call count |
| 1079 |
assert mock_get.call_count == 1 |
| 1080 |
assert mock_post.call_count == 0 |
| 1081 |
assert ( |
| 1082 |
mock_get.call_args_list[0][0][0] |
| 1083 |
== "https://nuxref.com/api/v1/accounts/verify_credentials" |
| 1084 |
) |
| 1085 |
|
| 1086 |
mock_get.reset_mock() |
| 1087 |
mock_post.reset_mock() |
| 1088 |
|
| 1089 |
mock_post.side_effect = [mr1, mr2, mr3, good_response, good_response] |
| 1090 |
mock_get.return_value = None |
| 1091 |
|
| 1092 |
# instantiate our object |
| 1093 |
mastodon_url = f"mastodons://{akey}@{host}" |
| 1094 |
obj = Apprise.instantiate(mastodon_url) |
| 1095 |
|
| 1096 |
# An invalid attachment will cause a failure |
| 1097 |
path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") |
| 1098 |
assert ( |
| 1099 |
bool( |
| 1100 |
obj.notify( |
| 1101 |
body="body", |
| 1102 |
title="title", |
| 1103 |
notify_type=NotifyType.INFO, |
| 1104 |
attach=path, |
| 1105 |
) |
| 1106 |
) |
| 1107 |
is False |
| 1108 |
) |
| 1109 |
|
| 1110 |
# No get requests are made |
| 1111 |
assert mock_get.call_count == 0 |
| 1112 |
|
| 1113 |
# No post request as attachment is no good anyway |
| 1114 |
assert mock_post.call_count == 0 |
| 1115 |
|
| 1116 |
mock_get.reset_mock() |
| 1117 |
mock_post.reset_mock() |
| 1118 |
|
| 1119 |
# We have an OSError thrown in the middle of our preparation |
| 1120 |
mock_post.side_effect = [ |
| 1121 |
good_media_response, |
| 1122 |
OSError(), |
| 1123 |
good_media_response, |
| 1124 |
] |
| 1125 |
mock_get.return_value = good_response |
| 1126 |
|
| 1127 |
# 3 images are produced |
| 1128 |
attach = [ |
| 1129 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 1130 |
# This one is not supported, so it's ignored gracefully |
| 1131 |
os.path.join(TEST_VAR_DIR, "apprise-archive.zip"), |
| 1132 |
# A supported video file |
| 1133 |
os.path.join(TEST_VAR_DIR, "apprise-test.mp4"), |
| 1134 |
] |
| 1135 |
|
| 1136 |
# We'll fail to send this time |
| 1137 |
assert ( |
| 1138 |
bool( |
| 1139 |
obj.notify( |
| 1140 |
body="body", |
| 1141 |
title="title", |
| 1142 |
notify_type=NotifyType.INFO, |
| 1143 |
attach=attach, |
| 1144 |
) |
| 1145 |
) |
| 1146 |
is False |
| 1147 |
) |
| 1148 |
|
| 1149 |
assert mock_get.call_count == 0 |
| 1150 |
# No get request as cached response is used |
| 1151 |
assert mock_post.call_count == 2 |
| 1152 |
assert ( |
| 1153 |
mock_post.call_args_list[0][0][0] == "https://nuxref.com/api/v1/media" |
| 1154 |
) |
| 1155 |
assert ( |
| 1156 |
mock_post.call_args_list[1][0][0] == "https://nuxref.com/api/v1/media" |
| 1157 |
) |
| 1158 |
|
| 1159 |
|
| 1160 |
@mock.patch("requests.post") |
| 1161 |
def test_plugin_mastodon_apprise_tags(mock_post): |
| 1162 |
"""NotifyMastodon() - Apprise tag filter must not corrupt URLBase.tags. |
| 1163 |
|
| 1164 |
Regression test for a bug where self.hashtags = [] in __init__ was |
| 1165 |
mistakenly named self.tags, overwriting the set() that URLBase stores for |
| 1166 |
Apprise's own notification-tag system. Any call to Apprise.find() or a |
| 1167 |
tag-filtered Apprise.notify() would crash with: |
| 1168 |
AttributeError: 'list' object has no attribute 'union' |
| 1169 |
""" |
| 1170 |
akey = "access_key" |
| 1171 |
host = "mastodon.social" |
| 1172 |
|
| 1173 |
# Prepare a good response |
| 1174 |
good_response = mock.Mock() |
| 1175 |
good_response.content = b"{}" |
| 1176 |
good_response.status_code = requests.codes.ok |
| 1177 |
|
| 1178 |
mock_post.return_value = good_response |
| 1179 |
|
| 1180 |
# Load a Mastodon URL that carries Mastodon hashtag targets into an Apprise |
| 1181 |
# instance that also has Apprise notification tags assigned. The Mastodon |
| 1182 |
# #hashtag and the Apprise tag are distinct concepts and must not collide. |
| 1183 |
a = Apprise() |
| 1184 |
assert ( |
| 1185 |
a.add( |
| 1186 |
f"mastodon://{akey}@{host}/%23apprise", |
| 1187 |
tag="prod", |
| 1188 |
) |
| 1189 |
is True |
| 1190 |
) |
| 1191 |
|
| 1192 |
# Confirm the plugin loaded and its Apprise tag set is a proper set (not a |
| 1193 |
# list), which is what caused the AttributeError before the fix. |
| 1194 |
plugins = list(a) |
| 1195 |
assert len(plugins) == 1 |
| 1196 |
assert isinstance(plugins[0].tags, set) |
| 1197 |
assert "prod" in plugins[0].tags |
| 1198 |
|
| 1199 |
# Confirm the Mastodon hashtag target was stored under hashtags, not tags |
| 1200 |
assert plugins[0].hashtags == ["#apprise"] |
| 1201 |
|
| 1202 |
# Exercising Apprise.find() is the exact path that crashed before the fix; |
| 1203 |
# it calls is_exclusive_match(data=notification.tags, ...) which requires |
| 1204 |
# notification.tags to support .union(). |
| 1205 |
found = list(a.find("prod")) |
| 1206 |
assert len(found) == 1 |
| 1207 |
|
| 1208 |
# A tag-filtered notify() follows the same code path |
| 1209 |
assert bool(a.notify(body="test", tag="prod")) is True |
| 1210 |
assert mock_post.call_count == 1 |