Debian Codegraph @

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