Debian Codegraph @

tests/test_plugin_groupme.py

raw
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 # Disable logging for a cleaner testing output
29 import logging
30 import os
31 from unittest import mock
32 from urllib.parse import urlparse
33
34 from helpers import AppriseURLTester
35 import pytest
36 import requests
37
38 from apprise import Apprise, AppriseAttachment
39 from apprise.exception import AppriseImproperlyConfigured
40 from apprise.plugins.groupme import NotifyGroupMe
41
42 logging.disable(logging.CRITICAL)
43
44 # Attachment directory
45 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
46
47 # Realistic test credentials
48 TEST_BOT_ID = "68ca900a7d17f9b9891a73af2a"
49 TEST_TOKEN = "abc123def456gh789ijklmn0op"
50
51 # A valid image URL returned by the GroupMe image service
52 TEST_IMAGE_URL = "https://i.groupme.com/somethingsomething.large"
53
54 # Fake image-service response payload
55 IMAGE_UPLOAD_RESP = b'{"payload":{"url":"' + TEST_IMAGE_URL.encode() + b'"}}'
56
57
58 # Our Testing URLs
59 apprise_url_tests = (
60 (
61 # No bot_id
62 "groupme://",
63 {
64 "instance": AppriseImproperlyConfigured,
65 },
66 ),
67 (
68 # Empty URL
69 "groupme://:@/",
70 {
71 "instance": AppriseImproperlyConfigured,
72 },
73 ),
74 (
75 # Invalid bot_id containing non-hex characters
76 "groupme://not-a-valid-bot-id!",
77 {
78 "instance": AppriseImproperlyConfigured,
79 },
80 ),
81 (
82 # Valid bot_id (text only)
83 "groupme://{}".format(TEST_BOT_ID),
84 {
85 "instance": NotifyGroupMe,
86 "privacy_url": "groupme://6...a/",
87 },
88 ),
89 (
90 # bot_id via query string
91 "groupme://?bot_id={}".format(TEST_BOT_ID),
92 {
93 "instance": NotifyGroupMe,
94 },
95 ),
96 (
97 # Access token in the path (canonical form with attachments)
98 "groupme://{}/{}".format(TEST_BOT_ID, TEST_TOKEN),
99 {
100 "instance": NotifyGroupMe,
101 "privacy_url": "groupme://6...a/",
102 # Image upload fails under URL tester mock (no payload.url)
103 "attach_response": False,
104 },
105 ),
106 (
107 # Access token supplied via ?token= query parameter (alias form)
108 "groupme://{}?token={}".format(TEST_BOT_ID, TEST_TOKEN),
109 {
110 "instance": NotifyGroupMe,
111 # Image upload fails under URL tester mock (no payload.url)
112 "attach_response": False,
113 },
114 ),
115 (
116 # HTTP 500 response
117 "groupme://{}".format(TEST_BOT_ID),
118 {
119 "instance": NotifyGroupMe,
120 "response": False,
121 "requests_response_code": requests.codes.internal_server_error,
122 },
123 ),
124 (
125 # Unknown response code
126 "groupme://{}".format(TEST_BOT_ID),
127 {
128 "instance": NotifyGroupMe,
129 "response": False,
130 "requests_response_code": 999,
131 },
132 ),
133 (
134 # Request exception
135 "groupme://{}".format(TEST_BOT_ID),
136 {
137 "instance": NotifyGroupMe,
138 "test_requests_exceptions": True,
139 },
140 ),
141 )
142
143
144 def test_plugin_groupme_urls():
145 """NotifyGroupMe() Apprise URLs."""
146
147 # Run our general tests
148 AppriseURLTester(tests=apprise_url_tests).run_all()
149
150
151 @mock.patch("requests.post")
152 def test_plugin_groupme_init(mock_post):
153 """NotifyGroupMe() Initialization checks."""
154
155 mock_post.return_value = requests.Request()
156 mock_post.return_value.status_code = requests.codes.accepted
157
158 # A bot ID is required.
159 with pytest.raises(AppriseImproperlyConfigured):
160 NotifyGroupMe(bot_id=None)
161
162 # A blank bot ID is invalid.
163 with pytest.raises(AppriseImproperlyConfigured):
164 NotifyGroupMe(bot_id="")
165
166 # A bot ID cannot contain unsupported characters.
167 with pytest.raises(AppriseImproperlyConfigured):
168 NotifyGroupMe(bot_id="not-valid!")
169
170 # Valid bot_id instantiates without error
171 obj = NotifyGroupMe(bot_id=TEST_BOT_ID)
172 assert isinstance(obj, NotifyGroupMe)
173 assert obj.bot_id == TEST_BOT_ID
174 assert obj.token is None
175
176 # Valid bot_id + token
177 obj = NotifyGroupMe(bot_id=TEST_BOT_ID, token=TEST_TOKEN)
178 assert isinstance(obj, NotifyGroupMe)
179 assert obj.bot_id == TEST_BOT_ID
180 assert obj.token == TEST_TOKEN
181
182
183 @mock.patch("requests.post")
184 def test_plugin_groupme_send(mock_post):
185 """NotifyGroupMe() Send checks."""
186
187 def _mk_resp(code, content=b""):
188 """Build a mock response."""
189 resp = requests.Request()
190 resp.status_code = code
191 resp.content = content
192 return resp
193
194 # 202 Accepted is the expected success code
195 mock_post.return_value = _mk_resp(requests.codes.accepted)
196
197 obj = NotifyGroupMe(bot_id=TEST_BOT_ID)
198 assert isinstance(obj, NotifyGroupMe)
199
200 # Successful send
201 assert bool(obj.notify(body="Test message")) is True
202 assert mock_post.call_count == 1
203
204 # Verify the correct URL host was used
205 call_url = mock_post.call_args[0][0]
206 assert urlparse(call_url).hostname == "api.groupme.com"
207
208 mock_post.reset_mock()
209
210 # 200 OK also accepted as success
211 mock_post.return_value = _mk_resp(requests.codes.ok)
212 assert bool(obj.notify(body="Test message")) is True
213
214 mock_post.reset_mock()
215
216 # HTTP 400 returns False
217 mock_post.return_value = _mk_resp(requests.codes.bad_request)
218 assert bool(obj.notify(body="Test message")) is False
219
220 mock_post.reset_mock()
221
222 # HTTP 401 returns False
223 mock_post.return_value = _mk_resp(requests.codes.unauthorized)
224 assert bool(obj.notify(body="Test message")) is False
225
226 mock_post.reset_mock()
227
228 # HTTP 404 returns False
229 mock_post.return_value = _mk_resp(requests.codes.not_found)
230 assert bool(obj.notify(body="Test message")) is False
231
232 mock_post.reset_mock()
233
234 # HTTP 429 returns False
235 mock_post.return_value = _mk_resp(429)
236 assert bool(obj.notify(body="Test message")) is False
237
238 mock_post.reset_mock()
239
240 # HTTP 500 returns False
241 mock_post.return_value = _mk_resp(requests.codes.internal_server_error)
242 assert bool(obj.notify(body="Test message")) is False
243
244 mock_post.reset_mock()
245
246 # Unknown error code returns False
247 mock_post.return_value = _mk_resp(999)
248 assert bool(obj.notify(body="Test message")) is False
249
250 mock_post.reset_mock()
251
252 # RequestException returns False
253 mock_post.side_effect = requests.RequestException("Network error")
254 assert bool(obj.notify(body="Test message")) is False
255
256
257 @mock.patch("requests.post")
258 def test_plugin_groupme_attach_no_token(mock_post):
259 """NotifyGroupMe() warns but still sends text when token is absent."""
260
261 def _mk_resp(code, content=b""):
262 """Build a mock response."""
263 resp = requests.Request()
264 resp.status_code = code
265 resp.content = content
266 return resp
267
268 mock_post.return_value = _mk_resp(requests.codes.accepted)
269
270 obj = NotifyGroupMe(bot_id=TEST_BOT_ID)
271 assert obj.token is None
272
273 # Load a real attachment file
274 attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.png"))
275
276 # Attachment send without token -- text message still succeeds
277 assert bool(obj.notify(body="Test", attach=attach)) is True
278
279 # Only the bot POST is made (no upload call since no token)
280 assert mock_post.call_count == 1
281
282
283 @mock.patch("requests.post")
284 def test_plugin_groupme_attach_success(mock_post):
285 """NotifyGroupMe() image upload and attachment send succeeds."""
286
287 def _mk_resp(code, content=b""):
288 """Build a mock response."""
289 resp = requests.Request()
290 resp.status_code = code
291 resp.content = content
292 return resp
293
294 # First call: image upload returns 200 with URL
295 # Second call: bot post returns 202
296 mock_post.side_effect = [
297 _mk_resp(requests.codes.ok, IMAGE_UPLOAD_RESP),
298 _mk_resp(requests.codes.accepted),
299 ]
300
301 obj = NotifyGroupMe(bot_id=TEST_BOT_ID, token=TEST_TOKEN)
302
303 attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.png"))
304 assert bool(obj.notify(body="Test", attach=attach)) is True
305
306 # Two requests: upload + bot post
307 assert mock_post.call_count == 2
308
309 # Verify the image service URL host was called first
310 upload_url = mock_post.call_args_list[0][0][0]
311 assert urlparse(upload_url).hostname == "image.groupme.com"
312
313 # Verify the bot post URL host was called second
314 post_url = mock_post.call_args_list[1][0][0]
315 assert urlparse(post_url).hostname == "api.groupme.com"
316
317
318 @mock.patch("requests.post")
319 def test_plugin_groupme_attach_inaccessible(mock_post):
320 """NotifyGroupMe() aborts upload when attachment is inaccessible."""
321
322 def _mk_resp(code, content=b""):
323 """Build a mock response."""
324 resp = requests.Request()
325 resp.status_code = code
326 resp.content = content
327 return resp
328
329 mock_post.return_value = _mk_resp(requests.codes.ok, IMAGE_UPLOAD_RESP)
330
331 obj = NotifyGroupMe(bot_id=TEST_BOT_ID, token=TEST_TOKEN)
332
333 attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.png"))
334
335 # Simulate the file being inaccessible (isfile returns False)
336 with mock.patch("os.path.isfile", return_value=False):
337 assert bool(obj.notify(body="Test", attach=attach)) is False
338
339 # No HTTP calls should have been made
340 assert mock_post.call_count == 0
341
342
343 @mock.patch("requests.post")
344 def test_plugin_groupme_attach_oserror(mock_post):
345 """NotifyGroupMe() aborts upload when open() raises OSError."""
346
347 def _mk_resp(code, content=b""):
348 """Build a mock response."""
349 resp = requests.Request()
350 resp.status_code = code
351 resp.content = content
352 return resp
353
354 mock_post.return_value = _mk_resp(requests.codes.ok, IMAGE_UPLOAD_RESP)
355
356 obj = NotifyGroupMe(bot_id=TEST_BOT_ID, token=TEST_TOKEN)
357
358 attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.png"))
359
360 # Simulate an I/O error when opening the file
361 with mock.patch("builtins.open", side_effect=OSError("disk error")):
362 assert bool(obj.notify(body="Test", attach=attach)) is False
363
364 # No HTTP calls should have been made
365 assert mock_post.call_count == 0
366
367
368 @mock.patch("requests.post")
369 def test_plugin_groupme_attach_upload_http_error(mock_post):
370 """NotifyGroupMe() returns False when the image upload gets HTTP error."""
371
372 def _mk_resp(code, content=b""):
373 """Build a mock response."""
374 resp = requests.Request()
375 resp.status_code = code
376 resp.content = content
377 return resp
378
379 # Image upload returns 401
380 mock_post.return_value = _mk_resp(requests.codes.unauthorized)
381
382 obj = NotifyGroupMe(bot_id=TEST_BOT_ID, token=TEST_TOKEN)
383
384 attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.png"))
385 assert bool(obj.notify(body="Test", attach=attach)) is False
386
387 # Only the upload attempt was made (bot post never called)
388 assert mock_post.call_count == 1
389
390
391 @mock.patch("requests.post")
392 def test_plugin_groupme_attach_upload_request_exception(mock_post):
393 """NotifyGroupMe() returns False on RequestException during upload."""
394
395 obj = NotifyGroupMe(bot_id=TEST_BOT_ID, token=TEST_TOKEN)
396
397 attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.png"))
398
399 # Simulate a network error during the image upload
400 mock_post.side_effect = requests.RequestException("Network error")
401 assert bool(obj.notify(body="Test", attach=attach)) is False
402
403 # Only the upload attempt was made
404 assert mock_post.call_count == 1
405
406
407 @mock.patch("requests.post")
408 def test_plugin_groupme_attach_bad_response(mock_post):
409 """NotifyGroupMe() returns False when upload response has no URL."""
410
411 def _mk_resp(code, content=b""):
412 """Build a mock response."""
413 resp = requests.Request()
414 resp.status_code = code
415 resp.content = content
416 return resp
417
418 # Upload returns 200 but with a response missing the URL field
419 mock_post.return_value = _mk_resp(requests.codes.ok, b"{}")
420
421 obj = NotifyGroupMe(bot_id=TEST_BOT_ID, token=TEST_TOKEN)
422
423 attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.png"))
424 assert bool(obj.notify(body="Test", attach=attach)) is False
425
426 mock_post.reset_mock()
427
428 # Upload returns 200 but with invalid JSON
429 mock_post.return_value = _mk_resp(requests.codes.ok, b"not-json")
430 assert bool(obj.notify(body="Test", attach=attach)) is False
431
432
433 @mock.patch("requests.post")
434 def test_plugin_groupme_attach_non_image(mock_post):
435 """NotifyGroupMe() skips non-image attachments with a warning."""
436
437 def _mk_resp(code, content=b""):
438 """Build a mock response."""
439 resp = requests.Request()
440 resp.status_code = code
441 resp.content = content
442 return resp
443
444 mock_post.return_value = _mk_resp(requests.codes.accepted)
445
446 obj = NotifyGroupMe(bot_id=TEST_BOT_ID, token=TEST_TOKEN)
447
448 # apprise-archive.zip has MIME type application/zip (not an image)
449 attach = AppriseAttachment(
450 os.path.join(TEST_VAR_DIR, "apprise-archive.zip")
451 )
452
453 # Non-image attachment is skipped; text message still succeeds
454 assert bool(obj.notify(body="Test", attach=attach)) is True
455
456 # Only the bot POST is made (no upload attempted for non-image file)
457 assert mock_post.call_count == 1
458
459
460 @mock.patch("requests.post")
461 def test_plugin_groupme_attach_multi(mock_post):
462 """NotifyGroupMe() sends multiple attachments in one bot post."""
463
464 def _mk_resp(code, content=b""):
465 """Build a mock response."""
466 resp = requests.Request()
467 resp.status_code = code
468 resp.content = content
469 return resp
470
471 # Two image uploads + one bot post
472 mock_post.side_effect = [
473 _mk_resp(requests.codes.ok, IMAGE_UPLOAD_RESP),
474 _mk_resp(requests.codes.ok, IMAGE_UPLOAD_RESP),
475 _mk_resp(requests.codes.accepted),
476 ]
477
478 obj = NotifyGroupMe(bot_id=TEST_BOT_ID, token=TEST_TOKEN)
479
480 # Load two different attachment files
481 attach = AppriseAttachment()
482 attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png"))
483 attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.jpeg"))
484
485 assert bool(obj.notify(body="Test", attach=attach)) is True
486
487 # 2 uploads + 1 bot post
488 assert mock_post.call_count == 3
489
490
491 @mock.patch("requests.post")
492 def test_plugin_groupme_url_roundtrip(mock_post):
493 """NotifyGroupMe() URL round-trip invariant."""
494
495 mock_post.return_value = requests.Request()
496 mock_post.return_value.status_code = requests.codes.accepted
497
498 # Without token
499 obj1 = NotifyGroupMe(bot_id=TEST_BOT_ID)
500 results = NotifyGroupMe.parse_url(obj1.url())
501 assert results is not None
502 obj2 = NotifyGroupMe(**results)
503 assert obj1.url_identifier == obj2.url_identifier
504 assert obj1.bot_id == obj2.bot_id
505
506 # With token
507 obj3 = NotifyGroupMe(bot_id=TEST_BOT_ID, token=TEST_TOKEN)
508 results = NotifyGroupMe.parse_url(obj3.url())
509 assert results is not None
510 obj4 = NotifyGroupMe(**results)
511 assert obj3.url_identifier == obj4.url_identifier
512 assert obj3.bot_id == obj4.bot_id
513 assert obj3.token == obj4.token
514
515
516 @mock.patch("requests.post")
517 def test_plugin_groupme_url_parsing(mock_post):
518 """NotifyGroupMe() URL parsing checks."""
519
520 mock_post.return_value = requests.Request()
521 mock_post.return_value.status_code = requests.codes.accepted
522
523 # Standard URL (no token)
524 results = NotifyGroupMe.parse_url("groupme://{}".format(TEST_BOT_ID))
525 assert results is not None
526 assert results["bot_id"] == TEST_BOT_ID
527 assert not results.get("token")
528
529 # Token in path (canonical form)
530 results = NotifyGroupMe.parse_url(
531 "groupme://{}/{}".format(TEST_BOT_ID, TEST_TOKEN)
532 )
533 assert results is not None
534 assert results["bot_id"] == TEST_BOT_ID
535 assert results["token"] == TEST_TOKEN
536
537 # Token via ?token= query parameter (alias form)
538 results = NotifyGroupMe.parse_url(
539 "groupme://{}?token={}".format(TEST_BOT_ID, TEST_TOKEN)
540 )
541 assert results is not None
542 assert results["bot_id"] == TEST_BOT_ID
543 assert results["token"] == TEST_TOKEN
544
545 # ?token= overrides path-supplied token
546 other_token = "zzz999yyy888xxx777"
547 results = NotifyGroupMe.parse_url(
548 "groupme://{}/{}?token={}".format(TEST_BOT_ID, TEST_TOKEN, other_token)
549 )
550 assert results is not None
551 assert results["token"] == other_token
552
553 # ?bot_id= query override
554 results = NotifyGroupMe.parse_url(
555 "groupme://ignored?bot_id={}".format(TEST_BOT_ID)
556 )
557 assert results is not None
558 assert results["bot_id"] == TEST_BOT_ID
559
560 # None URL returns None
561 assert NotifyGroupMe.parse_url(None) is None
562
563
564 @mock.patch("requests.post")
565 def test_plugin_groupme_privacy_url(mock_post):
566 """NotifyGroupMe() privacy URL masks credentials."""
567
568 mock_post.return_value = requests.Request()
569 mock_post.return_value.status_code = requests.codes.accepted
570
571 obj = NotifyGroupMe(bot_id=TEST_BOT_ID, token=TEST_TOKEN)
572
573 privacy = obj.url(privacy=True)
574 assert TEST_BOT_ID not in privacy
575 assert TEST_TOKEN not in privacy
576 assert privacy.startswith("groupme://")
577
578
579 @mock.patch("requests.post")
580 def test_plugin_groupme_url_identifier(mock_post):
581 """NotifyGroupMe() url_identifier uniqueness."""
582
583 obj1 = NotifyGroupMe(bot_id=TEST_BOT_ID)
584 obj2 = NotifyGroupMe(bot_id=TEST_BOT_ID)
585 assert obj1.url_identifier == obj2.url_identifier
586
587 # Different bot_id produces a different identifier
588 obj3 = NotifyGroupMe(bot_id="aabbccddeeff00112233445566")
589 assert obj1.url_identifier != obj3.url_identifier
590
591 # token must not affect the identifier
592 obj4 = NotifyGroupMe(bot_id=TEST_BOT_ID, token=TEST_TOKEN)
593 assert obj1.url_identifier == obj4.url_identifier
594
595 # Schema is the first element
596 assert obj1.url_identifier[0] == NotifyGroupMe.secure_protocol
597 assert obj1.url_identifier[1] == TEST_BOT_ID
598
599
600 @mock.patch("requests.post")
601 def test_plugin_groupme_apprise_integration(mock_post):
602 """NotifyGroupMe() Apprise integration."""
603
604 def _mk_resp(code, content=b""):
605 """Build a mock response."""
606 resp = requests.Request()
607 resp.status_code = code
608 resp.content = content
609 return resp
610
611 mock_post.return_value = _mk_resp(requests.codes.accepted)
612
613 # Load via Apprise URL
614 a = Apprise()
615 assert a.add("groupme://{}".format(TEST_BOT_ID)) is True
616 assert len(a) == 1
617 assert isinstance(a[0], NotifyGroupMe)
618
619 assert bool(a.notify(body="Test", title="Title")) is True
620 assert mock_post.call_count == 1
621
622 mock_post.reset_mock()
623
624 # Load with token
625 a2 = Apprise()
626 assert (
627 a2.add("groupme://{}?token={}".format(TEST_BOT_ID, TEST_TOKEN)) is True
628 )
629 assert len(a2) == 1
630 assert isinstance(a2[0], NotifyGroupMe)
631
632 mock_post.return_value = _mk_resp(requests.codes.accepted)
633 assert bool(a2.notify(body="Test", title="Title")) is True
634 assert mock_post.call_count == 1
635
636 mock_post.reset_mock()
637
638 # Failure response
639 mock_post.return_value = _mk_resp(requests.codes.internal_server_error)
640 assert bool(a.notify(body="Test")) is False