Debian Codegraph @

tests/test_plugin_bluesky.py

raw symbols SCIP
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 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 # Disable logging for a cleaner testing output
42 logging.disable(logging.CRITICAL)
43
44 # Attachment Directory
45 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
46
47 TWITTER_SCREEN_NAME = "apprise"
48
49
50 # Our Testing URLs
51 apprise_url_tests = (
52 ##################################
53 # NotifyBlueSky
54 ##################################
55 (
56 "bluesky://",
57 {
58 # Missing user and app_pass
59 "instance": TypeError,
60 },
61 ),
62 (
63 "bluesky://:@/",
64 {
65 "instance": TypeError,
66 },
67 ),
68 (
69 "bluesky://app-pw",
70 {
71 # Missing User
72 "instance": TypeError,
73 },
74 ),
75 (
76 "bluesky://user@app-pw",
77 {
78 "instance": NotifyBlueSky,
79 # Expected notify() response False (because we won't be able
80 # to detect our user)
81 "notify_response": False,
82 # Our expected url(privacy=True) startswith() response:
83 "privacy_url": "bsky://user@****",
84 },
85 ),
86 (
87 "bluesky://user@app-pw1?cache=no",
88 {
89 "instance": NotifyBlueSky,
90 # At minimum we need an access token and did; below has no did
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 # valid payload
103 "requests_response_text": {
104 "accessJwt": "abcd",
105 "refreshJwt": "abcd",
106 "did": "did:plc:1234",
107 # Support plc response
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 # no cache; so we store our results
121 "instance": NotifyBlueSky,
122 # valid payload
123 "requests_response_text": {
124 "accessJwt": "abcd",
125 "refreshJwt": "abcd",
126 "did": "did:plc:1234",
127 # For handling attachments
128 "blob": "content",
129 # Support plc response
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 # no cache; so we store our results
143 "instance": NotifyBlueSky,
144 # valid payload
145 "requests_response_text": {
146 "accessJwt": "abcd",
147 "refreshJwt": "abcd",
148 "did": "did:plc:1234",
149 # For handling attachments
150 "blob": "content",
151 # Support plc response
152 "service": [
153 {
154 "type": "AtprotoPersonalDataServer",
155 "serviceEndpoint": "https://example.pds.io",
156 }
157 ],
158 },
159 },
160 ),
161 # A duplicate of the entry above, this will cause cache to be referenced
162 (
163 "bluesky://user@app-pw3",
164 {
165 # no cache; so we store our results
166 "instance": NotifyBlueSky,
167 # valid payload
168 "requests_response_text": {
169 "accessJwt": "abcd",
170 "refreshJwt": "abcd",
171 "did": "did:plc:1234",
172 # For handling attachments
173 "blob": "content",
174 # Support plc response
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 # throw a bizarre code forcing us to fail to look it up
189 "response": False,
190 "requests_response_code": 999,
191 "requests_response_text": {
192 "accessJwt": "abcd",
193 "refreshJwt": "abcd",
194 "did": "did:plc:1234",
195 # Support plc response
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 # Throws a series of i/o exceptions with this flag
210 # is set and tests that we gracefully handle them
211 "test_requests_exceptions": True,
212 "requests_response_text": {
213 "accessJwt": "abcd",
214 "refreshJwt": "abcd",
215 "did": "did:plc:1234",
216 # Support plc response
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 # Support plc response
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 # Epoch time:
256 epoch = datetime.fromtimestamp(0, timezone.utc)
257
258 # Generate a very large rate-limit header window
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 # Run our general tests
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 # Epoch time:
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 # Prepare Mock
349 mock_get.return_value = request
350 mock_post.return_value = request
351
352 # Variation Initializations
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 # apprise room was found
359 assert obj.send(body="test") is True
360
361 # Change our status code and try again
362 request.status_code = 403
363 assert obj.send(body="test") is False
364 assert obj.ratelimit_remaining == 1
365
366 # Return the status
367 request.status_code = requests.codes.ok
368 # Force a reset
369 request.headers["ratelimit-remaining"] = 0
370 # behind the scenes, it should cause us to update our rate limit
371 assert obj.send(body="test") is True
372 assert obj.ratelimit_remaining == 0
373
374 # This should cause us to block
375 request.headers["ratelimit-remaining"] = 10
376 assert obj.send(body="test") is True
377 assert obj.ratelimit_remaining == 10
378
379 # Handle cases where we simply couldn't get this field
380 del request.headers["ratelimit-remaining"]
381 assert obj.send(body="test") is True
382 # It remains set to the last value
383 assert obj.ratelimit_remaining == 10
384
385 # Reset our variable back to 1
386 request.headers["ratelimit-remaining"] = 1
387
388 # Handle cases where our epoch time is wrong
389 del request.headers["ratelimit-reset"]
390 assert obj.send(body="test") is True
391
392 # Return our object, but place it in the future forcing us to block
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 # Return our object, but place it in the future forcing us to block
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 # Return our limits to always work
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 # Flush our cache forcing it is re-creating
418 NotifyBlueSky._user_cache = {}
419 assert obj.send(body="test") is True
420
421 # Cause content response to be None
422 request.content = None
423 assert obj.send(body="test") is True
424
425 # Invalid JSON
426 request.content = "{"
427 assert obj.send(body="test") is True
428
429 # Return it to a parseable string
430 request.content = "{}"
431
432 results = NotifyBlueSky.parse_url("bluesky://handle@app-pass-word")
433 assert isinstance(results, dict) is True
434
435 # cause a json parsing issue now
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 # Flush out our cache
443 NotifyBlueSky._user_cache = {}
444
445 response_obj = {
446 "accessJwt": "abcd",
447 "refreshJwt": "abcd",
448 "did": "did:plc:1234",
449 # Support plc response
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 # Alter the key forcing us to look up a new value of ourselves again
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 # Create application objects.
501 obj = Apprise.instantiate(bluesky_url)
502 attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif"))
503
504 # Send our notification
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 # Verify API calls.
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 # Create application objects.
560 obj = Apprise.instantiate(bluesky_url)
561 attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif"))
562
563 # Our notification will fail now since our message will error out.
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 # Verify API calls.
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 # Test case where upload fails.
607 mock_get.return_value = good_message_response
608 mock_post.side_effect = [good_message_response, OSError]
609
610 # Create application objects.
611 obj = Apprise.instantiate(bluesky_url)
612 attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif"))
613
614 # Send our notification; it will fail because of the message response.
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 # Verify API calls.
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 # Create application objects.
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 # An invalid attachment will cause a failure.
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 # Verify API calls.
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 # No post request as attachment is not good.
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 # instantiate our object
720 obj = Apprise.instantiate(bluesky_url)
721
722 # 4 images are produced
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 # This one is not supported, so it's ignored gracefully
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 # Verify API calls.
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 # If we call the functions again, the only difference is
791 # we no longer need to resolve the handle or create a session
792 # as the previous one is fine.
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 # Verify API calls.
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 # instantiate our object
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 # Verify API calls.
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 # Step 1: Identity resolution response (public.api.bsky.app)
903 identity_response = good_response({"did": "did:plc:abcdefg1234567"})
904
905 # Step 2: PLC Directory lookup
906 plc_response = good_response(
907 {
908 "service": [
909 {
910 "type": "AtprotoPersonalDataServer",
911 "serviceEndpoint": "https://example.pds.io",
912 }
913 ]
914 }
915 )
916
917 # Step 3: Auth session
918 session_response = good_response()
919
920 # Step 4: Create post
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 # Reset for did:web test
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 # Invalid DID scheme
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 # Return a valid DID resolution
985 identity_response = good_response({"did": "did:plc:abcdefg1234567"})
986
987 # Return DID document with a service list, but no matching PDS type
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)