| 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, timezone |
| 29 |
import json |
| 30 |
import logging |
| 31 |
import os |
| 32 |
from unittest.mock import Mock, patch |
| 33 |
|
| 34 |
from helpers import AppriseURLTester |
| 35 |
import pytest |
| 36 |
import requests |
| 37 |
|
| 38 |
from apprise import Apprise, AppriseAttachment, NotifyType |
| 39 |
from apprise.plugins.bluesky import NotifyBlueSky |
| 40 |
|
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
|
| 45 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 46 |
|
| 47 |
TWITTER_SCREEN_NAME = "apprise" |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
apprise_url_tests = ( |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
( |
| 56 |
"bluesky://", |
| 57 |
{ |
| 58 |
|
| 59 |
"instance": TypeError, |
| 60 |
}, |
| 61 |
), |
| 62 |
( |
| 63 |
"bluesky://:@/", |
| 64 |
{ |
| 65 |
"instance": TypeError, |
| 66 |
}, |
| 67 |
), |
| 68 |
( |
| 69 |
"bluesky://app-pw", |
| 70 |
{ |
| 71 |
|
| 72 |
"instance": TypeError, |
| 73 |
}, |
| 74 |
), |
| 75 |
( |
| 76 |
"bluesky://user@app-pw", |
| 77 |
{ |
| 78 |
"instance": NotifyBlueSky, |
| 79 |
|
| 80 |
|
| 81 |
"notify_response": False, |
| 82 |
|
| 83 |
"privacy_url": "bsky://user@****", |
| 84 |
}, |
| 85 |
), |
| 86 |
( |
| 87 |
"bluesky://user@app-pw1?cache=no", |
| 88 |
{ |
| 89 |
"instance": NotifyBlueSky, |
| 90 |
|
| 91 |
"requests_response_text": { |
| 92 |
"accessJwt": "abcd", |
| 93 |
"refreshJwt": "abcd", |
| 94 |
}, |
| 95 |
"notify_response": False, |
| 96 |
}, |
| 97 |
), |
| 98 |
( |
| 99 |
"bluesky://user@app-pw2?cache=no", |
| 100 |
{ |
| 101 |
"instance": NotifyBlueSky, |
| 102 |
|
| 103 |
"requests_response_text": { |
| 104 |
"accessJwt": "abcd", |
| 105 |
"refreshJwt": "abcd", |
| 106 |
"did": "did:plc:1234", |
| 107 |
|
| 108 |
"service": [ |
| 109 |
{ |
| 110 |
"type": "AtprotoPersonalDataServer", |
| 111 |
"serviceEndpoint": "https://example.pds.io", |
| 112 |
} |
| 113 |
], |
| 114 |
}, |
| 115 |
}, |
| 116 |
), |
| 117 |
( |
| 118 |
"bluesky://user@app-pw3", |
| 119 |
{ |
| 120 |
|
| 121 |
"instance": NotifyBlueSky, |
| 122 |
|
| 123 |
"requests_response_text": { |
| 124 |
"accessJwt": "abcd", |
| 125 |
"refreshJwt": "abcd", |
| 126 |
"did": "did:plc:1234", |
| 127 |
|
| 128 |
"blob": "content", |
| 129 |
|
| 130 |
"service": [ |
| 131 |
{ |
| 132 |
"type": "AtprotoPersonalDataServer", |
| 133 |
"serviceEndpoint": "https://example.pds.io", |
| 134 |
} |
| 135 |
], |
| 136 |
}, |
| 137 |
}, |
| 138 |
), |
| 139 |
( |
| 140 |
"bluesky://user.example.ca@app-pw3", |
| 141 |
{ |
| 142 |
|
| 143 |
"instance": NotifyBlueSky, |
| 144 |
|
| 145 |
"requests_response_text": { |
| 146 |
"accessJwt": "abcd", |
| 147 |
"refreshJwt": "abcd", |
| 148 |
"did": "did:plc:1234", |
| 149 |
|
| 150 |
"blob": "content", |
| 151 |
|
| 152 |
"service": [ |
| 153 |
{ |
| 154 |
"type": "AtprotoPersonalDataServer", |
| 155 |
"serviceEndpoint": "https://example.pds.io", |
| 156 |
} |
| 157 |
], |
| 158 |
}, |
| 159 |
}, |
| 160 |
), |
| 161 |
|
| 162 |
( |
| 163 |
"bluesky://user@app-pw3", |
| 164 |
{ |
| 165 |
|
| 166 |
"instance": NotifyBlueSky, |
| 167 |
|
| 168 |
"requests_response_text": { |
| 169 |
"accessJwt": "abcd", |
| 170 |
"refreshJwt": "abcd", |
| 171 |
"did": "did:plc:1234", |
| 172 |
|
| 173 |
"blob": "content", |
| 174 |
|
| 175 |
"service": [ |
| 176 |
{ |
| 177 |
"type": "AtprotoPersonalDataServer", |
| 178 |
"serviceEndpoint": "https://example.pds.io", |
| 179 |
} |
| 180 |
], |
| 181 |
}, |
| 182 |
}, |
| 183 |
), |
| 184 |
( |
| 185 |
"bluesky://user@app-pw", |
| 186 |
{ |
| 187 |
"instance": NotifyBlueSky, |
| 188 |
|
| 189 |
"response": False, |
| 190 |
"requests_response_code": 999, |
| 191 |
"requests_response_text": { |
| 192 |
"accessJwt": "abcd", |
| 193 |
"refreshJwt": "abcd", |
| 194 |
"did": "did:plc:1234", |
| 195 |
|
| 196 |
"service": [ |
| 197 |
{ |
| 198 |
"type": "AtprotoPersonalDataServer", |
| 199 |
"serviceEndpoint": "https://example.pds.io", |
| 200 |
} |
| 201 |
], |
| 202 |
}, |
| 203 |
}, |
| 204 |
), |
| 205 |
( |
| 206 |
"bluesky://user@app-pw", |
| 207 |
{ |
| 208 |
"instance": NotifyBlueSky, |
| 209 |
|
| 210 |
|
| 211 |
"test_requests_exceptions": True, |
| 212 |
"requests_response_text": { |
| 213 |
"accessJwt": "abcd", |
| 214 |
"refreshJwt": "abcd", |
| 215 |
"did": "did:plc:1234", |
| 216 |
|
| 217 |
"service": [ |
| 218 |
{ |
| 219 |
"type": "AtprotoPersonalDataServer", |
| 220 |
"serviceEndpoint": "https://example.pds.io", |
| 221 |
} |
| 222 |
], |
| 223 |
}, |
| 224 |
}, |
| 225 |
), |
| 226 |
) |
| 227 |
|
| 228 |
|
| 229 |
def good_response(data=None): |
| 230 |
"""Prepare a good response.""" |
| 231 |
response = Mock() |
| 232 |
response.content = json.dumps( |
| 233 |
{ |
| 234 |
"accessJwt": "abcd", |
| 235 |
"refreshJwt": "abcd", |
| 236 |
"did": "did:plc:1234", |
| 237 |
|
| 238 |
"service": [ |
| 239 |
{ |
| 240 |
"type": "AtprotoPersonalDataServer", |
| 241 |
"serviceEndpoint": "https://example.pds.io", |
| 242 |
} |
| 243 |
], |
| 244 |
"ratelimit-reset": str( |
| 245 |
int(datetime.now(timezone.utc).timestamp()) + 3600 |
| 246 |
), |
| 247 |
"ratelimit-remaining": "10", |
| 248 |
} |
| 249 |
if data is None |
| 250 |
else data |
| 251 |
) |
| 252 |
|
| 253 |
response.status_code = requests.codes.ok |
| 254 |
|
| 255 |
|
| 256 |
epoch = datetime.fromtimestamp(0, timezone.utc) |
| 257 |
|
| 258 |
|
| 259 |
response.headers = { |
| 260 |
"ratelimit-reset": ( |
| 261 |
(datetime.now(timezone.utc) - epoch).total_seconds() + 86400 |
| 262 |
), |
| 263 |
"ratelimit-remaining": "1000", |
| 264 |
} |
| 265 |
|
| 266 |
return response |
| 267 |
|
| 268 |
|
| 269 |
def bad_response(data=None): |
| 270 |
"""Prepare a bad response.""" |
| 271 |
response = Mock() |
| 272 |
response.content = json.dumps( |
| 273 |
{ |
| 274 |
"error": "InvalidRequest", |
| 275 |
"message": "Something failed", |
| 276 |
} |
| 277 |
if data is None |
| 278 |
else data |
| 279 |
) |
| 280 |
response.headers = {} |
| 281 |
response.status_code = requests.codes.internal_server_error |
| 282 |
return response |
| 283 |
|
| 284 |
|
| 285 |
@pytest.fixture |
| 286 |
def bluesky_url(): |
| 287 |
url = "bluesky://user@app-key" |
| 288 |
return url |
| 289 |
|
| 290 |
|
| 291 |
@pytest.fixture |
| 292 |
def good_message_response(): |
| 293 |
"""Prepare a good response.""" |
| 294 |
response = good_response() |
| 295 |
return response |
| 296 |
|
| 297 |
|
| 298 |
@pytest.fixture |
| 299 |
def bad_message_response(): |
| 300 |
"""Prepare a bad message response.""" |
| 301 |
response = bad_response() |
| 302 |
return response |
| 303 |
|
| 304 |
|
| 305 |
@pytest.fixture |
| 306 |
def good_media_response(): |
| 307 |
"""Prepare a good media response.""" |
| 308 |
response = Mock() |
| 309 |
response.content = json.dumps( |
| 310 |
{ |
| 311 |
"blob": { |
| 312 |
"$type": "blob", |
| 313 |
"mimeType": "image/jpeg", |
| 314 |
"ref": {"$link": "baf124idksduabcjkaa3iey4bfyq"}, |
| 315 |
"size": 73667, |
| 316 |
} |
| 317 |
} |
| 318 |
) |
| 319 |
response.headers = {} |
| 320 |
response.status_code = requests.codes.ok |
| 321 |
return response |
| 322 |
|
| 323 |
|
| 324 |
def test_plugin_bluesky_urls(): |
| 325 |
"""NotifyBlueSky() Apprise URLs.""" |
| 326 |
|
| 327 |
|
| 328 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 329 |
|
| 330 |
|
| 331 |
def test_plugin_bluesky_general(mocker): |
| 332 |
"""NotifyBlueSky() General Tests.""" |
| 333 |
|
| 334 |
mock_get = mocker.patch("requests.get") |
| 335 |
mock_post = mocker.patch("requests.post") |
| 336 |
|
| 337 |
|
| 338 |
epoch = datetime.fromtimestamp(0, timezone.utc) |
| 339 |
|
| 340 |
request = good_response() |
| 341 |
request.headers = { |
| 342 |
"ratelimit-reset": ( |
| 343 |
(datetime.now(timezone.utc) - epoch).total_seconds() |
| 344 |
), |
| 345 |
"ratelimit-remaining": "1", |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
mock_get.return_value = request |
| 350 |
mock_post.return_value = request |
| 351 |
|
| 352 |
|
| 353 |
obj = NotifyBlueSky(user="handle", password="app-password") |
| 354 |
|
| 355 |
assert isinstance(obj, NotifyBlueSky) is True |
| 356 |
assert isinstance(obj.url(), str) is True |
| 357 |
|
| 358 |
|
| 359 |
assert obj.send(body="test") is True |
| 360 |
|
| 361 |
|
| 362 |
request.status_code = 403 |
| 363 |
assert obj.send(body="test") is False |
| 364 |
assert obj.ratelimit_remaining == 1 |
| 365 |
|
| 366 |
|
| 367 |
request.status_code = requests.codes.ok |
| 368 |
|
| 369 |
request.headers["ratelimit-remaining"] = 0 |
| 370 |
|
| 371 |
assert obj.send(body="test") is True |
| 372 |
assert obj.ratelimit_remaining == 0 |
| 373 |
|
| 374 |
|
| 375 |
request.headers["ratelimit-remaining"] = 10 |
| 376 |
assert obj.send(body="test") is True |
| 377 |
assert obj.ratelimit_remaining == 10 |
| 378 |
|
| 379 |
|
| 380 |
del request.headers["ratelimit-remaining"] |
| 381 |
assert obj.send(body="test") is True |
| 382 |
|
| 383 |
assert obj.ratelimit_remaining == 10 |
| 384 |
|
| 385 |
|
| 386 |
request.headers["ratelimit-remaining"] = 1 |
| 387 |
|
| 388 |
|
| 389 |
del request.headers["ratelimit-reset"] |
| 390 |
assert obj.send(body="test") is True |
| 391 |
|
| 392 |
|
| 393 |
request.headers["ratelimit-reset"] = ( |
| 394 |
datetime.now(timezone.utc) - epoch |
| 395 |
).total_seconds() + 1 |
| 396 |
request.headers["ratelimit-remaining"] = 0 |
| 397 |
obj.ratelimit_remaining = 0 |
| 398 |
assert obj.send(body="test") is True |
| 399 |
|
| 400 |
|
| 401 |
request.headers["ratelimit-reset"] = ( |
| 402 |
datetime.now(timezone.utc) - epoch |
| 403 |
).total_seconds() - 1 |
| 404 |
request.headers["ratelimit-remaining"] = 0 |
| 405 |
obj.ratelimit_remaining = 0 |
| 406 |
assert obj.send(body="test") is True |
| 407 |
|
| 408 |
|
| 409 |
request.headers["ratelimit-reset"] = ( |
| 410 |
datetime.now(timezone.utc) - epoch |
| 411 |
).total_seconds() |
| 412 |
request.headers["ratelimit-remaining"] = 1 |
| 413 |
obj.ratelimit_remaining = 1 |
| 414 |
|
| 415 |
assert obj.send(body="test") is True |
| 416 |
|
| 417 |
|
| 418 |
NotifyBlueSky._user_cache = {} |
| 419 |
assert obj.send(body="test") is True |
| 420 |
|
| 421 |
|
| 422 |
request.content = None |
| 423 |
assert obj.send(body="test") is True |
| 424 |
|
| 425 |
|
| 426 |
request.content = "{" |
| 427 |
assert obj.send(body="test") is True |
| 428 |
|
| 429 |
|
| 430 |
request.content = "{}" |
| 431 |
|
| 432 |
results = NotifyBlueSky.parse_url("bluesky://handle@app-pass-word") |
| 433 |
assert isinstance(results, dict) is True |
| 434 |
|
| 435 |
|
| 436 |
response_obj = None |
| 437 |
assert obj.send(body="test") is True |
| 438 |
|
| 439 |
response_obj = "{" |
| 440 |
assert obj.send(body="test") is True |
| 441 |
|
| 442 |
|
| 443 |
NotifyBlueSky._user_cache = {} |
| 444 |
|
| 445 |
response_obj = { |
| 446 |
"accessJwt": "abcd", |
| 447 |
"refreshJwt": "abcd", |
| 448 |
"did": "did:plc:1234", |
| 449 |
|
| 450 |
"service": [ |
| 451 |
{ |
| 452 |
"type": "AtprotoPersonalDataServer", |
| 453 |
"serviceEndpoint": "https://example.pds.io", |
| 454 |
} |
| 455 |
], |
| 456 |
} |
| 457 |
request.content = json.dumps(response_obj) |
| 458 |
|
| 459 |
obj = NotifyBlueSky(user="handle", password="app-pass-word") |
| 460 |
assert obj.send(body="test") is True |
| 461 |
|
| 462 |
|
| 463 |
NotifyBlueSky._user_cache = {} |
| 464 |
NotifyBlueSky._whoami_cache = None |
| 465 |
obj.ckey = "different.then.it.was" |
| 466 |
assert obj.send(body="test") is True |
| 467 |
|
| 468 |
NotifyBlueSky._whoami_cache = None |
| 469 |
obj.ckey = "different.again" |
| 470 |
assert obj.send(body="test") is True |
| 471 |
|
| 472 |
|
| 473 |
def test_plugin_bluesky_edge_cases(): |
| 474 |
"""NotifyBlueSky() Edge Cases.""" |
| 475 |
|
| 476 |
with pytest.raises(TypeError): |
| 477 |
NotifyBlueSky() |
| 478 |
|
| 479 |
|
| 480 |
@patch("requests.post") |
| 481 |
@patch("requests.get") |
| 482 |
def test_plugin_bluesky_attachments_basic( |
| 483 |
mock_get, |
| 484 |
mock_post, |
| 485 |
bluesky_url, |
| 486 |
good_message_response, |
| 487 |
good_media_response, |
| 488 |
): |
| 489 |
""" |
| 490 |
NotifyBlueSky() Attachment Checks - Basic |
| 491 |
""" |
| 492 |
|
| 493 |
mock_get.return_value = good_message_response |
| 494 |
mock_post.side_effect = [ |
| 495 |
good_message_response, |
| 496 |
good_media_response, |
| 497 |
good_message_response, |
| 498 |
] |
| 499 |
|
| 500 |
|
| 501 |
obj = Apprise.instantiate(bluesky_url) |
| 502 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 503 |
|
| 504 |
|
| 505 |
assert ( |
| 506 |
obj.notify( |
| 507 |
body="body", |
| 508 |
title="title", |
| 509 |
notify_type=NotifyType.INFO, |
| 510 |
attach=attach, |
| 511 |
) |
| 512 |
is True |
| 513 |
) |
| 514 |
|
| 515 |
|
| 516 |
assert mock_get.call_count == 2 |
| 517 |
assert ( |
| 518 |
mock_get.call_args_list[0][0][0] |
| 519 |
== "https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle" |
| 520 |
) |
| 521 |
assert ( |
| 522 |
mock_get.call_args_list[1][0][0] |
| 523 |
== "https://plc.directory/did:plc:1234" |
| 524 |
) |
| 525 |
|
| 526 |
assert mock_post.call_count == 3 |
| 527 |
assert ( |
| 528 |
mock_post.call_args_list[0][0][0] |
| 529 |
== "https://example.pds.io/xrpc/com.atproto.server.createSession" |
| 530 |
) |
| 531 |
assert ( |
| 532 |
mock_post.call_args_list[1][0][0] |
| 533 |
== "https://example.pds.io/xrpc/com.atproto.repo.uploadBlob" |
| 534 |
) |
| 535 |
assert ( |
| 536 |
mock_post.call_args_list[2][0][0] |
| 537 |
== "https://example.pds.io/xrpc/com.atproto.repo.createRecord" |
| 538 |
) |
| 539 |
|
| 540 |
|
| 541 |
@patch("requests.post") |
| 542 |
@patch("requests.get") |
| 543 |
def test_plugin_bluesky_attachments_bad_message_response( |
| 544 |
mock_get, |
| 545 |
mock_post, |
| 546 |
bluesky_url, |
| 547 |
good_media_response, |
| 548 |
good_message_response, |
| 549 |
bad_message_response, |
| 550 |
): |
| 551 |
|
| 552 |
mock_get.return_value = good_message_response |
| 553 |
mock_post.side_effect = [ |
| 554 |
good_message_response, |
| 555 |
bad_message_response, |
| 556 |
good_message_response, |
| 557 |
] |
| 558 |
|
| 559 |
|
| 560 |
obj = Apprise.instantiate(bluesky_url) |
| 561 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 562 |
|
| 563 |
|
| 564 |
assert ( |
| 565 |
obj.notify( |
| 566 |
body="body", |
| 567 |
title="title", |
| 568 |
notify_type=NotifyType.INFO, |
| 569 |
attach=attach, |
| 570 |
) |
| 571 |
is False |
| 572 |
) |
| 573 |
|
| 574 |
|
| 575 |
assert mock_get.call_count == 2 |
| 576 |
assert ( |
| 577 |
mock_get.call_args_list[0][0][0] |
| 578 |
== "https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle" |
| 579 |
) |
| 580 |
assert ( |
| 581 |
mock_get.call_args_list[1][0][0] |
| 582 |
== "https://plc.directory/did:plc:1234" |
| 583 |
) |
| 584 |
|
| 585 |
assert mock_post.call_count == 2 |
| 586 |
assert ( |
| 587 |
mock_post.call_args_list[0][0][0] |
| 588 |
== "https://example.pds.io/xrpc/com.atproto.server.createSession" |
| 589 |
) |
| 590 |
assert ( |
| 591 |
mock_post.call_args_list[1][0][0] |
| 592 |
== "https://example.pds.io/xrpc/com.atproto.repo.uploadBlob" |
| 593 |
) |
| 594 |
|
| 595 |
|
| 596 |
@patch("requests.post") |
| 597 |
@patch("requests.get") |
| 598 |
def test_plugin_bluesky_attachments_upload_fails( |
| 599 |
mock_get, |
| 600 |
mock_post, |
| 601 |
bluesky_url, |
| 602 |
good_media_response, |
| 603 |
good_message_response, |
| 604 |
): |
| 605 |
|
| 606 |
|
| 607 |
mock_get.return_value = good_message_response |
| 608 |
mock_post.side_effect = [good_message_response, OSError] |
| 609 |
|
| 610 |
|
| 611 |
obj = Apprise.instantiate(bluesky_url) |
| 612 |
attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 613 |
|
| 614 |
|
| 615 |
assert ( |
| 616 |
obj.notify( |
| 617 |
body="body", |
| 618 |
title="title", |
| 619 |
notify_type=NotifyType.INFO, |
| 620 |
attach=attach, |
| 621 |
) |
| 622 |
is False |
| 623 |
) |
| 624 |
|
| 625 |
|
| 626 |
assert mock_get.call_count == 2 |
| 627 |
assert ( |
| 628 |
mock_get.call_args_list[0][0][0] |
| 629 |
== "https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle" |
| 630 |
) |
| 631 |
assert ( |
| 632 |
mock_get.call_args_list[1][0][0] |
| 633 |
== "https://plc.directory/did:plc:1234" |
| 634 |
) |
| 635 |
|
| 636 |
assert mock_post.call_count == 2 |
| 637 |
assert ( |
| 638 |
mock_post.call_args_list[0][0][0] |
| 639 |
== "https://example.pds.io/xrpc/com.atproto.server.createSession" |
| 640 |
) |
| 641 |
assert ( |
| 642 |
mock_post.call_args_list[1][0][0] |
| 643 |
== "https://example.pds.io/xrpc/com.atproto.repo.uploadBlob" |
| 644 |
) |
| 645 |
|
| 646 |
|
| 647 |
@patch("requests.post") |
| 648 |
@patch("requests.get") |
| 649 |
def test_plugin_bluesky_attachments_invalid_attachment( |
| 650 |
mock_get, |
| 651 |
mock_post, |
| 652 |
bluesky_url, |
| 653 |
good_message_response, |
| 654 |
good_media_response, |
| 655 |
): |
| 656 |
|
| 657 |
mock_get.return_value = good_message_response |
| 658 |
mock_post.side_effect = [good_message_response, good_media_response] |
| 659 |
|
| 660 |
|
| 661 |
obj = Apprise.instantiate(bluesky_url) |
| 662 |
attach = AppriseAttachment( |
| 663 |
os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") |
| 664 |
) |
| 665 |
|
| 666 |
|
| 667 |
assert ( |
| 668 |
obj.notify( |
| 669 |
body="body", |
| 670 |
title="title", |
| 671 |
notify_type=NotifyType.INFO, |
| 672 |
attach=attach, |
| 673 |
) |
| 674 |
is False |
| 675 |
) |
| 676 |
|
| 677 |
|
| 678 |
assert mock_get.call_count == 2 |
| 679 |
assert ( |
| 680 |
mock_get.call_args_list[0][0][0] |
| 681 |
== "https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle" |
| 682 |
) |
| 683 |
assert ( |
| 684 |
mock_get.call_args_list[1][0][0] |
| 685 |
== "https://plc.directory/did:plc:1234" |
| 686 |
) |
| 687 |
|
| 688 |
|
| 689 |
assert mock_post.call_count == 1 |
| 690 |
assert ( |
| 691 |
mock_post.call_args_list[0][0][0] |
| 692 |
== "https://example.pds.io/xrpc/com.atproto.server.createSession" |
| 693 |
) |
| 694 |
|
| 695 |
|
| 696 |
@patch("requests.post") |
| 697 |
@patch("requests.get") |
| 698 |
def test_plugin_bluesky_attachments_multiple_batch( |
| 699 |
mock_get, |
| 700 |
mock_post, |
| 701 |
bluesky_url, |
| 702 |
good_message_response, |
| 703 |
good_media_response, |
| 704 |
): |
| 705 |
|
| 706 |
mock_get.return_value = good_message_response |
| 707 |
mock_post.side_effect = [ |
| 708 |
good_message_response, |
| 709 |
good_media_response, |
| 710 |
good_media_response, |
| 711 |
good_media_response, |
| 712 |
good_media_response, |
| 713 |
good_message_response, |
| 714 |
good_message_response, |
| 715 |
good_message_response, |
| 716 |
good_message_response, |
| 717 |
] |
| 718 |
|
| 719 |
|
| 720 |
obj = Apprise.instantiate(bluesky_url) |
| 721 |
|
| 722 |
|
| 723 |
attach = [ |
| 724 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 725 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 726 |
os.path.join(TEST_VAR_DIR, "apprise-test.jpeg"), |
| 727 |
os.path.join(TEST_VAR_DIR, "apprise-test.png"), |
| 728 |
|
| 729 |
os.path.join(TEST_VAR_DIR, "apprise-test.mp4"), |
| 730 |
] |
| 731 |
|
| 732 |
assert ( |
| 733 |
obj.notify( |
| 734 |
body="body", |
| 735 |
title="title", |
| 736 |
notify_type=NotifyType.INFO, |
| 737 |
attach=attach, |
| 738 |
) |
| 739 |
is True |
| 740 |
) |
| 741 |
|
| 742 |
|
| 743 |
assert mock_get.call_count == 2 |
| 744 |
assert ( |
| 745 |
mock_get.call_args_list[0][0][0] |
| 746 |
== "https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle" |
| 747 |
) |
| 748 |
assert ( |
| 749 |
mock_get.call_args_list[1][0][0] |
| 750 |
== "https://plc.directory/did:plc:1234" |
| 751 |
) |
| 752 |
assert mock_post.call_count == 9 |
| 753 |
assert ( |
| 754 |
mock_post.call_args_list[0][0][0] |
| 755 |
== "https://example.pds.io/xrpc/com.atproto.server.createSession" |
| 756 |
) |
| 757 |
assert ( |
| 758 |
mock_post.call_args_list[1][0][0] |
| 759 |
== "https://example.pds.io/xrpc/com.atproto.repo.uploadBlob" |
| 760 |
) |
| 761 |
assert ( |
| 762 |
mock_post.call_args_list[2][0][0] |
| 763 |
== "https://example.pds.io/xrpc/com.atproto.repo.uploadBlob" |
| 764 |
) |
| 765 |
assert ( |
| 766 |
mock_post.call_args_list[3][0][0] |
| 767 |
== "https://example.pds.io/xrpc/com.atproto.repo.uploadBlob" |
| 768 |
) |
| 769 |
assert ( |
| 770 |
mock_post.call_args_list[4][0][0] |
| 771 |
== "https://example.pds.io/xrpc/com.atproto.repo.uploadBlob" |
| 772 |
) |
| 773 |
assert ( |
| 774 |
mock_post.call_args_list[5][0][0] |
| 775 |
== "https://example.pds.io/xrpc/com.atproto.repo.createRecord" |
| 776 |
) |
| 777 |
assert ( |
| 778 |
mock_post.call_args_list[6][0][0] |
| 779 |
== "https://example.pds.io/xrpc/com.atproto.repo.createRecord" |
| 780 |
) |
| 781 |
assert ( |
| 782 |
mock_post.call_args_list[7][0][0] |
| 783 |
== "https://example.pds.io/xrpc/com.atproto.repo.createRecord" |
| 784 |
) |
| 785 |
assert ( |
| 786 |
mock_post.call_args_list[8][0][0] |
| 787 |
== "https://example.pds.io/xrpc/com.atproto.repo.createRecord" |
| 788 |
) |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
mock_get.reset_mock() |
| 794 |
mock_post.reset_mock() |
| 795 |
|
| 796 |
mock_get.return_value = good_message_response |
| 797 |
mock_post.side_effect = [ |
| 798 |
good_media_response, |
| 799 |
good_media_response, |
| 800 |
good_media_response, |
| 801 |
good_media_response, |
| 802 |
good_message_response, |
| 803 |
good_message_response, |
| 804 |
good_message_response, |
| 805 |
good_message_response, |
| 806 |
] |
| 807 |
|
| 808 |
assert ( |
| 809 |
obj.notify( |
| 810 |
body="body", |
| 811 |
title="title", |
| 812 |
notify_type=NotifyType.INFO, |
| 813 |
attach=attach, |
| 814 |
) |
| 815 |
is True |
| 816 |
) |
| 817 |
|
| 818 |
|
| 819 |
assert mock_get.call_count == 0 |
| 820 |
assert mock_post.call_count == 8 |
| 821 |
assert ( |
| 822 |
mock_post.call_args_list[0][0][0] |
| 823 |
== "https://example.pds.io/xrpc/com.atproto.repo.uploadBlob" |
| 824 |
) |
| 825 |
assert ( |
| 826 |
mock_post.call_args_list[1][0][0] |
| 827 |
== "https://example.pds.io/xrpc/com.atproto.repo.uploadBlob" |
| 828 |
) |
| 829 |
assert ( |
| 830 |
mock_post.call_args_list[2][0][0] |
| 831 |
== "https://example.pds.io/xrpc/com.atproto.repo.uploadBlob" |
| 832 |
) |
| 833 |
assert ( |
| 834 |
mock_post.call_args_list[3][0][0] |
| 835 |
== "https://example.pds.io/xrpc/com.atproto.repo.uploadBlob" |
| 836 |
) |
| 837 |
assert ( |
| 838 |
mock_post.call_args_list[4][0][0] |
| 839 |
== "https://example.pds.io/xrpc/com.atproto.repo.createRecord" |
| 840 |
) |
| 841 |
assert ( |
| 842 |
mock_post.call_args_list[5][0][0] |
| 843 |
== "https://example.pds.io/xrpc/com.atproto.repo.createRecord" |
| 844 |
) |
| 845 |
assert ( |
| 846 |
mock_post.call_args_list[6][0][0] |
| 847 |
== "https://example.pds.io/xrpc/com.atproto.repo.createRecord" |
| 848 |
) |
| 849 |
assert ( |
| 850 |
mock_post.call_args_list[7][0][0] |
| 851 |
== "https://example.pds.io/xrpc/com.atproto.repo.createRecord" |
| 852 |
) |
| 853 |
|
| 854 |
|
| 855 |
@patch("requests.post") |
| 856 |
@patch("requests.get") |
| 857 |
def test_plugin_bluesky_auth_failure( |
| 858 |
mock_get, |
| 859 |
mock_post, |
| 860 |
bluesky_url, |
| 861 |
good_message_response, |
| 862 |
bad_message_response, |
| 863 |
): |
| 864 |
|
| 865 |
mock_get.return_value = good_message_response |
| 866 |
mock_post.return_value = bad_message_response |
| 867 |
|
| 868 |
|
| 869 |
obj = Apprise.instantiate(bluesky_url) |
| 870 |
|
| 871 |
assert ( |
| 872 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 873 |
is False |
| 874 |
) |
| 875 |
|
| 876 |
|
| 877 |
assert mock_get.call_count == 2 |
| 878 |
assert ( |
| 879 |
mock_get.call_args_list[0][0][0] |
| 880 |
== "https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle" |
| 881 |
) |
| 882 |
assert ( |
| 883 |
mock_get.call_args_list[1][0][0] |
| 884 |
== "https://plc.directory/did:plc:1234" |
| 885 |
) |
| 886 |
assert mock_post.call_count == 1 |
| 887 |
assert ( |
| 888 |
mock_post.call_args_list[0][0][0] |
| 889 |
== "https://example.pds.io/xrpc/com.atproto.server.createSession" |
| 890 |
) |
| 891 |
|
| 892 |
|
| 893 |
@patch("requests.post") |
| 894 |
@patch("requests.get") |
| 895 |
def test_plugin_bluesky_did_web_and_plc_resolution( |
| 896 |
mock_get, mock_post, bluesky_url, good_message_response |
| 897 |
): |
| 898 |
""" |
| 899 |
NotifyBlueSky() - Full coverage of did:web and did:plc path |
| 900 |
""" |
| 901 |
|
| 902 |
|
| 903 |
identity_response = good_response({"did": "did:plc:abcdefg1234567"}) |
| 904 |
|
| 905 |
|
| 906 |
plc_response = good_response( |
| 907 |
{ |
| 908 |
"service": [ |
| 909 |
{ |
| 910 |
"type": "AtprotoPersonalDataServer", |
| 911 |
"serviceEndpoint": "https://example.pds.io", |
| 912 |
} |
| 913 |
] |
| 914 |
} |
| 915 |
) |
| 916 |
|
| 917 |
|
| 918 |
session_response = good_response() |
| 919 |
|
| 920 |
|
| 921 |
post_response = good_response() |
| 922 |
|
| 923 |
mock_get.side_effect = [identity_response, plc_response] |
| 924 |
mock_post.side_effect = [session_response, post_response] |
| 925 |
|
| 926 |
obj = Apprise.instantiate(bluesky_url) |
| 927 |
assert obj.notify(body="Resolved PLC Flow") is True |
| 928 |
|
| 929 |
|
| 930 |
identity_response = good_response({"did": "did:web:example.com"}) |
| 931 |
|
| 932 |
web_did_response = good_response( |
| 933 |
{ |
| 934 |
"service": [ |
| 935 |
{ |
| 936 |
"type": "AtprotoPersonalDataServer", |
| 937 |
"serviceEndpoint": "https://example.com", |
| 938 |
} |
| 939 |
] |
| 940 |
} |
| 941 |
) |
| 942 |
|
| 943 |
mock_get.side_effect = [identity_response, web_did_response] |
| 944 |
mock_post.side_effect = [session_response, post_response] |
| 945 |
|
| 946 |
obj = Apprise.instantiate(bluesky_url) |
| 947 |
assert obj.notify(body="Resolved WEB Flow") is True |
| 948 |
|
| 949 |
|
| 950 |
bad_did_response = good_response({"did": "did:unsupported:scheme"}) |
| 951 |
|
| 952 |
mock_get.side_effect = [bad_did_response] |
| 953 |
obj = Apprise.instantiate(bluesky_url) |
| 954 |
assert obj.notify(body="fail due to bad scheme") is False |
| 955 |
|
| 956 |
|
| 957 |
@patch("requests.get") |
| 958 |
def test_plugin_bluesky_pds_resolution_failures(mock_get): |
| 959 |
""" |
| 960 |
NotifyBlueSky() - Missing service field or invalid service endpoint |
| 961 |
""" |
| 962 |
identity_response = good_response({"did": "did:plc:missing-service"}) |
| 963 |
plc_no_service = good_response({"foo": "bar"}) |
| 964 |
|
| 965 |
mock_get.side_effect = [identity_response, plc_no_service] |
| 966 |
obj = NotifyBlueSky(user="handle", password="pass") |
| 967 |
did, endpoint = obj.get_identifier() |
| 968 |
assert (did, endpoint) == (False, False) |
| 969 |
|
| 970 |
identity_response = good_response({"did": "did:web:example.com"}) |
| 971 |
web_did_no_service = good_response({"foo": "bar"}) |
| 972 |
|
| 973 |
mock_get.side_effect = [identity_response, web_did_no_service] |
| 974 |
obj = NotifyBlueSky(user="handle", password="pass") |
| 975 |
did, endpoint = obj.get_identifier() |
| 976 |
assert (did, endpoint) == (False, False) |
| 977 |
|
| 978 |
|
| 979 |
@patch("requests.get") |
| 980 |
def test_plugin_bluesky_missing_pds_endpoint(mock_get): |
| 981 |
""" |
| 982 |
NotifyBlueSky() - test case where endpoint is missing from DID document |
| 983 |
""" |
| 984 |
|
| 985 |
identity_response = good_response({"did": "did:plc:abcdefg1234567"}) |
| 986 |
|
| 987 |
|
| 988 |
incomplete_pds_response = good_response( |
| 989 |
{ |
| 990 |
"service": [ |
| 991 |
{ |
| 992 |
"type": "SomeOtherService", |
| 993 |
"serviceEndpoint": "https://unrelated.example.com", |
| 994 |
} |
| 995 |
] |
| 996 |
} |
| 997 |
) |
| 998 |
|
| 999 |
mock_get.side_effect = [identity_response, incomplete_pds_response] |
| 1000 |
obj = NotifyBlueSky(user="handle", password="app-pw") |
| 1001 |
assert obj.get_identifier() == (False, False) |