Debian Codegraph @

tests/test_plugin_threema.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 from unittest import mock
31
32 from helpers import AppriseURLTester
33 import pytest
34 import requests
35
36 from apprise.exception import AppriseImproperlyConfigured
37 from apprise.plugins.threema import NotifyThreema
38
39 logging.disable(logging.CRITICAL)
40
41 # Our Testing URLs
42 apprise_url_tests = (
43 (
44 "threema://",
45 {
46 # No user/secret specified
47 "instance": AppriseImproperlyConfigured,
48 },
49 ),
50 (
51 "threema://@:",
52 {
53 # Invalid url
54 "instance": AppriseImproperlyConfigured,
55 },
56 ),
57 (
58 "threema://user@secret",
59 {
60 # gateway id must be 8 characters in len
61 "instance": AppriseImproperlyConfigured,
62 },
63 ),
64 (
65 "threema://*THEGWID@secret/{targets}/".format(
66 targets="/".join(["2222"])
67 ),
68 {
69 # Invalid target phone number
70 "instance": NotifyThreema,
71 "notify_response": False,
72 "privacy_url": "threema://%2ATHEGWID@****/2222",
73 },
74 ),
75 (
76 "threema://*THEGWID@secret/{targets}/".format(
77 targets="/".join(["16134442222"])
78 ),
79 {
80 # Valid
81 "instance": NotifyThreema,
82 "privacy_url": "threema://%2ATHEGWID@****/16134442222",
83 },
84 ),
85 (
86 "threema://*THEGWID@secret/{targets}/".format(
87 targets="/".join(["16134442222", "16134443333"])
88 ),
89 {
90 # Valid multiple targets
91 "instance": NotifyThreema,
92 "privacy_url": "threema://%2ATHEGWID@****/16134442222/16134443333",
93 },
94 ),
95 (
96 "threema:///?secret=secret&from=*THEGWID&to={targets}".format(
97 targets=",".join(["16134448888", "[email protected]", "abcd1234"])
98 ),
99 {
100 # Valid
101 "instance": NotifyThreema,
102 },
103 ),
104 (
105 "threema:///?secret=secret&gwid=*THEGWID&to={targets}".format(
106 targets=",".join(["16134448888", "[email protected]", "abcd1234"])
107 ),
108 {
109 # Valid
110 "instance": NotifyThreema,
111 },
112 ),
113 (
114 "threema://*THEGWID@secret",
115 {
116 "instance": NotifyThreema,
117 # No targets specified
118 "notify_response": False,
119 },
120 ),
121 (
122 "threema://*THEGWID@secret/16134443333",
123 {
124 "instance": NotifyThreema,
125 # throw a bizarre code forcing us to fail to look it up
126 "response": False,
127 "requests_response_code": 999,
128 },
129 ),
130 (
131 "threema://*THEGWID@secret/16134443333",
132 {
133 "instance": NotifyThreema,
134 # Throws a series of errors
135 "test_requests_exceptions": True,
136 },
137 ),
138 )
139
140
141 def test_plugin_threema():
142 """NotifyThreema() Apprise URLs."""
143
144 # Run our general tests
145 AppriseURLTester(tests=apprise_url_tests).run_all()
146
147
148 @mock.patch("requests.post")
149 def test_plugin_threema_edge_cases(mock_post):
150 """NotifyThreema() Edge Cases."""
151
152 # Prepare our response
153 response = requests.Request()
154 response.status_code = requests.codes.ok
155
156 # Prepare Mock
157 mock_post.return_value = response
158
159 # Initialize some generic (but valid) tokens
160 gwid = "*THEGWID"
161 secret = "mysecret"
162 targets = "+1 (555) 123-9876"
163
164 # No email specified
165 with pytest.raises(AppriseImproperlyConfigured):
166 NotifyThreema(user=gwid, secret=None, targets=targets)
167
168 results = NotifyThreema.parse_url(
169 f"threema://?gwid={gwid}&secret={secret}&to={targets}"
170 )
171
172 assert isinstance(results, dict)
173 assert results["user"] == gwid
174 assert results["secret"] == secret
175 assert results["password"] is None
176 assert results["port"] is None
177 assert results["host"] == ""
178 assert results["fullpath"] == "/"
179 assert results["path"] == "/"
180 assert results["query"] is None
181 assert results["schema"] == "threema"
182 assert results["url"] == "threema:///"
183 assert isinstance(results["targets"], list)
184 assert len(results["targets"]) == 1
185 assert results["targets"][0] == targets
186
187 instance = NotifyThreema(**results)
188 assert len(instance.targets) == 1
189 assert instance.targets[0] == ("phone", "15551239876")
190 assert isinstance(instance, NotifyThreema)
191
192 response = instance.send(title="title", body="body 😊")
193 assert response is True
194 assert mock_post.call_count == 1
195
196 details = mock_post.call_args_list[0]
197 assert details[0][0] == "https://msgapi.threema.ch/send_simple"
198 assert details[1]["headers"]["User-Agent"] == "Apprise"
199 assert details[1]["headers"]["Accept"] == "*/*"
200 assert (
201 details[1]["headers"]["Content-Type"]
202 == "application/x-www-form-urlencoded; charset=utf-8"
203 )
204 assert details[1]["params"]["secret"] == secret
205 assert details[1]["params"]["from"] == gwid
206 assert details[1]["params"]["phone"] == "15551239876"
207 assert details[1]["params"]["text"] == "body 😊".encode()