Debian Codegraph @

tests/test_plugin_pushbullet.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 json import dumps
29
30 # Disable logging for a cleaner testing output
31 import logging
32 import os
33 from unittest import mock
34
35 from helpers import AppriseURLTester
36 import pytest
37 import requests
38
39 from apprise import Apprise, AppriseAttachment
40 from apprise.plugins.pushbullet import NotifyPushBullet
41
42 logging.disable(logging.CRITICAL)
43
44 # Attachment Directory
45 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
46
47 # Our Testing URLs
48 apprise_url_tests = (
49 (
50 "pbul://",
51 {
52 "instance": TypeError,
53 },
54 ),
55 (
56 "pbul://:@/",
57 {
58 "instance": TypeError,
59 },
60 ),
61 # APIkey
62 (
63 "pbul://%s" % ("a" * 32),
64 {
65 "instance": NotifyPushBullet,
66 "check_attachments": False,
67 },
68 ),
69 # APIkey; but support attachment response
70 (
71 "pbul://%s" % ("a" * 32),
72 {
73 "instance": NotifyPushBullet,
74 # Test what happens if a batch send fails to return a messageCount
75 "requests_response_text": {
76 "file_name": "cat.jpeg",
77 "file_type": "image/jpeg",
78 "file_url": "http://file_url",
79 "upload_url": "http://upload_url",
80 },
81 },
82 ),
83 # APIkey; attachment testing that isn't an image type
84 (
85 "pbul://%s" % ("a" * 32),
86 {
87 "instance": NotifyPushBullet,
88 # Test what happens if a batch send fails to return a messageCount
89 "requests_response_text": {
90 "file_name": "test.pdf",
91 "file_type": "application/pdf",
92 "file_url": "http://file_url",
93 "upload_url": "http://upload_url",
94 },
95 },
96 ),
97 # APIkey; attachment testing were expected entry in payload is missing
98 (
99 "pbul://%s" % ("a" * 32),
100 {
101 "instance": NotifyPushBullet,
102 # Test what happens if a batch send fails to return a messageCount
103 "requests_response_text": {
104 "file_name": "test.pdf",
105 "file_type": "application/pdf",
106 "file_url": "http://file_url",
107 # upload_url missing
108 },
109 # Our Notification calls associated with attachments will fail:
110 "attach_response": False,
111 },
112 ),
113 # API Key + channel
114 (
115 "pbul://%s/#channel/" % ("a" * 32),
116 {
117 "instance": NotifyPushBullet,
118 "check_attachments": False,
119 },
120 ),
121 # API Key + channel (via to=
122 (
123 "pbul://%s/?to=#channel" % ("a" * 32),
124 {
125 "instance": NotifyPushBullet,
126 "check_attachments": False,
127 },
128 ),
129 # API Key + 2 channels
130 (
131 "pbul://%s/#channel1/#channel2" % ("a" * 32),
132 {
133 "instance": NotifyPushBullet,
134 # Our expected url(privacy=True) startswith() response:
135 "privacy_url": "pbul://a...a/",
136 "check_attachments": False,
137 },
138 ),
139 # API Key + device
140 (
141 "pbul://%s/device/" % ("a" * 32),
142 {
143 "instance": NotifyPushBullet,
144 "check_attachments": False,
145 },
146 ),
147 # API Key + 2 devices
148 (
149 "pbul://%s/device1/device2/" % ("a" * 32),
150 {
151 "instance": NotifyPushBullet,
152 "check_attachments": False,
153 },
154 ),
155 # API Key + email
156 (
157 "pbul://%s/[email protected]/" % ("a" * 32),
158 {
159 "instance": NotifyPushBullet,
160 "check_attachments": False,
161 },
162 ),
163 # API Key + 2 emails
164 (
165 "pbul://%s/[email protected]/[email protected]/" % ("a" * 32),
166 {
167 "instance": NotifyPushBullet,
168 "check_attachments": False,
169 },
170 ),
171 # API Key + Combo
172 (
173 "pbul://%s/device/#channel/[email protected]/" % ("a" * 32),
174 {
175 "instance": NotifyPushBullet,
176 "check_attachments": False,
177 },
178 ),
179 # ,
180 (
181 "pbul://%s" % ("a" * 32),
182 {
183 "instance": NotifyPushBullet,
184 # force a failure
185 "response": False,
186 "requests_response_code": requests.codes.internal_server_error,
187 "check_attachments": False,
188 },
189 ),
190 (
191 "pbul://%s" % ("a" * 32),
192 {
193 "instance": NotifyPushBullet,
194 # throw a bizarre code forcing us to fail to look it up
195 "response": False,
196 "requests_response_code": 999,
197 "check_attachments": False,
198 },
199 ),
200 (
201 "pbul://%s" % ("a" * 32),
202 {
203 "instance": NotifyPushBullet,
204 # Throws a series of i/o exceptions with this flag
205 # is set and tests that we gracefully handle them
206 "test_requests_exceptions": True,
207 "check_attachments": False,
208 },
209 ),
210 (
211 "pbul://%s" % ("a" * 32),
212 {
213 "instance": NotifyPushBullet,
214 # force a failure
215 "response": False,
216 "requests_response_code": requests.codes.internal_server_error,
217 "check_attachments": False,
218 },
219 ),
220 (
221 "pbul://%s" % ("a" * 32),
222 {
223 "instance": NotifyPushBullet,
224 # throw a bizarre code forcing us to fail to look it up
225 "response": False,
226 "requests_response_code": 999,
227 "check_attachments": False,
228 },
229 ),
230 (
231 "pbul://%s" % ("a" * 32),
232 {
233 "instance": NotifyPushBullet,
234 # Throws a series of i/o exceptions with this flag
235 # is set and tests that we gracefully handle them
236 "test_requests_exceptions": True,
237 "check_attachments": False,
238 },
239 ),
240 )
241
242
243 def test_plugin_pushbullet_urls():
244 """NotifyPushBullet() Apprise URLs."""
245
246 # Run our general tests
247 AppriseURLTester(tests=apprise_url_tests).run_all()
248
249
250 @mock.patch("requests.post")
251 def test_plugin_pushbullet_attachments(mock_post):
252 """NotifyPushBullet() Attachment Checks."""
253
254 # Initialize some generic (but valid) tokens
255 access_token = "t" * 32
256
257 # Prepare Mock return object
258 response = mock.Mock()
259 response.content = dumps(
260 {
261 "file_name": "cat.jpg",
262 "file_type": "image/jpeg",
263 "file_url": "https://dl.pushb.com/abc/cat.jpg",
264 "upload_url": "https://upload.pushbullet.com/abcd123",
265 }
266 ).encode("utf-8")
267 response.status_code = requests.codes.ok
268 mock_post.return_value = response
269
270 # prepare our attachment
271 attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif"))
272
273 # Test our markdown
274 obj = Apprise.instantiate(f"pbul://{access_token}/?format=markdown")
275
276 # Send a good attachment
277 assert obj.notify(body="test", attach=attach) is True
278
279 # Test our call count
280 assert mock_post.call_count == 4
281 # Image Prep
282 assert (
283 mock_post.call_args_list[0][0][0]
284 == "https://api.pushbullet.com/v2/upload-request"
285 )
286 assert (
287 mock_post.call_args_list[1][0][0]
288 == "https://upload.pushbullet.com/abcd123"
289 )
290 # Message
291 assert (
292 mock_post.call_args_list[2][0][0]
293 == "https://api.pushbullet.com/v2/pushes"
294 )
295 # Image Send
296 assert (
297 mock_post.call_args_list[3][0][0]
298 == "https://api.pushbullet.com/v2/pushes"
299 )
300
301 # Reset our mock object
302 mock_post.reset_mock()
303
304 # Add another attachment so we drop into the area of the PushBullet code
305 # that sends remaining attachments (if more detected)
306 attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.gif"))
307
308 # Send our attachments
309 assert obj.notify(body="test", attach=attach) is True
310
311 # Test our call count
312 assert mock_post.call_count == 7
313 # Image Prep
314 assert (
315 mock_post.call_args_list[0][0][0]
316 == "https://api.pushbullet.com/v2/upload-request"
317 )
318 assert (
319 mock_post.call_args_list[1][0][0]
320 == "https://upload.pushbullet.com/abcd123"
321 )
322 assert (
323 mock_post.call_args_list[2][0][0]
324 == "https://api.pushbullet.com/v2/upload-request"
325 )
326 assert (
327 mock_post.call_args_list[3][0][0]
328 == "https://upload.pushbullet.com/abcd123"
329 )
330 # Message
331 assert (
332 mock_post.call_args_list[4][0][0]
333 == "https://api.pushbullet.com/v2/pushes"
334 )
335 # Image Send
336 assert (
337 mock_post.call_args_list[5][0][0]
338 == "https://api.pushbullet.com/v2/pushes"
339 )
340 assert (
341 mock_post.call_args_list[6][0][0]
342 == "https://api.pushbullet.com/v2/pushes"
343 )
344
345 # Reset our mock object
346 mock_post.reset_mock()
347
348 # An invalid attachment will cause a failure
349 path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg")
350 attach = AppriseAttachment(path)
351 assert obj.notify(body="test", attach=attach) is False
352
353 # Test our call count
354 assert mock_post.call_count == 0
355
356 # prepare our attachment
357 attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif"))
358
359 # Prepare a bad response
360 bad_response = mock.Mock()
361 bad_response.content = dumps(
362 {
363 "file_name": "cat.jpg",
364 "file_type": "image/jpeg",
365 "file_url": "https://dl.pushb.com/abc/cat.jpg",
366 "upload_url": "https://upload.pushbullet.com/abcd123",
367 }
368 ).encode("utf-8")
369 bad_response.status_code = requests.codes.internal_server_error
370 bad_response.headers = {}
371
372 # Prepare a bad response
373 bad_json_response = mock.Mock()
374 bad_json_response.content = b"}"
375 bad_json_response.status_code = requests.codes.ok
376 bad_json_response.headers = {}
377
378 # Throw an exception on the first call to requests.post()
379 for side_effect in (
380 requests.RequestException(),
381 OSError(),
382 [bad_response],
383 ):
384 mock_post.reset_mock()
385 mock_post.side_effect = side_effect
386
387 # We'll fail now because of our error handling
388 assert obj.send(body="test", attach=attach) is False
389
390 # Throw an exception on the second call to requests.post()
391 for side_effect in (requests.RequestException(), OSError(), bad_response):
392 mock_post.reset_mock()
393 mock_post.side_effect = [response, side_effect]
394
395 # We'll fail now because of our error handling
396 assert obj.send(body="test", attach=attach) is False
397
398 # Throw an exception on the third call to requests.post()
399 for side_effect in (requests.RequestException(), OSError(), bad_response):
400 mock_post.reset_mock()
401 mock_post.side_effect = [response, response, side_effect]
402
403 # We'll fail now because of our error handling
404 assert obj.send(body="test", attach=attach) is False
405
406 # Throw an exception on the forth call to requests.post()
407 for side_effect in (requests.RequestException(), OSError(), bad_response):
408 mock_post.reset_mock()
409 mock_post.side_effect = [response, response, response, side_effect]
410
411 # We'll fail now because of our error handling
412 assert obj.send(body="test", attach=attach) is False
413
414 # Test case where we don't get a valid response back
415 mock_post.side_effect = None
416 mock_post.return_value = bad_json_response
417
418 # We'll fail because of an invalid json object
419 assert obj.send(body="test", attach=attach) is False
420
421
422 @mock.patch("requests.get")
423 @mock.patch("requests.post")
424 def test_plugin_pushbullet_edge_cases(mock_post, mock_get):
425 """NotifyPushBullet() Edge Cases."""
426
427 # Initialize some generic (but valid) tokens
428 accesstoken = "a" * 32
429
430 # Support strings
431 recipients = "#chan1,#chan2,device,[email protected],,,"
432
433 # Prepare Mock
434 mock_get.return_value = requests.Request()
435 mock_post.return_value = requests.Request()
436 mock_post.content = b""
437 mock_post.return_value.status_code = requests.codes.ok
438 mock_get.return_value.status_code = requests.codes.ok
439 mock_get.content = b""
440
441 # Invalid Access Token
442 with pytest.raises(TypeError):
443 NotifyPushBullet(accesstoken=None)
444 with pytest.raises(TypeError):
445 NotifyPushBullet(accesstoken=" ")
446
447 obj = NotifyPushBullet(accesstoken=accesstoken, targets=recipients)
448 assert isinstance(obj, NotifyPushBullet) is True
449 assert len(obj.targets) == 4
450
451 obj = NotifyPushBullet(accesstoken=accesstoken)
452 assert isinstance(obj, NotifyPushBullet) is True
453 # Default is to send to all devices, so there will be a
454 # recipient here
455 assert len(obj.targets) == 1
456
457 obj = NotifyPushBullet(accesstoken=accesstoken, targets=set())
458 assert isinstance(obj, NotifyPushBullet) is True
459 # Default is to send to all devices, so there will be a
460 # recipient here
461 assert len(obj.targets) == 1