Debian Codegraph @

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