Debian Codegraph @

tests/test_plugin_africas_talking.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 requests
34
35 from apprise import Apprise, NotifyType
36 from apprise.plugins.africas_talking import NotifyAfricasTalking
37
38 logging.disable(logging.CRITICAL)
39
40 # Our Testing URLs
41 apprise_url_tests = (
42 (
43 "atalk://",
44 {
45 # Instantiated but no auth, so no notification can happen
46 "instance": TypeError,
47 },
48 ),
49 (
50 "atalk://:@/",
51 {
52 # invalid auth
53 "instance": TypeError
54 },
55 ),
56 (
57 "atalk://user@^/",
58 {
59 # invalid apikey
60 "instance": TypeError
61 },
62 ),
63 (
64 "atalk://user@apikey/{}".format("3" * 5),
65 {
66 # invalid nubmer provided
67 "instance": NotifyAfricasTalking,
68 # Expected notify() response because we have no one to notify
69 "notify_response": False,
70 },
71 ),
72 (
73 "atalk://user@apikey/123/{}/abcd/+{}".format("3" * 11, "4" * 11),
74 {
75 # includes a few invalid bits of info
76 "instance": NotifyAfricasTalking,
77 "privacy_url": "atalk://[email protected]/33333333333/+44444444444",
78 },
79 ),
80 (
81 "atalk://user@apikey/+{}?batch=y".format("4" * 11),
82 {
83 "instance": NotifyAfricasTalking,
84 # Our expected url(privacy=True) startswith() response:
85 "privacy_url": "atalk://[email protected]/+44444444444",
86 },
87 ),
88 (
89 "atalk://user@apikey/+{}?mode=invalid".format("4" * 11),
90 {"instance": TypeError},
91 ),
92 (
93 "atalk://user@apikey/+{}?mode=s".format("4" * 11),
94 {
95 # S will match the sandbox
96 "instance": NotifyAfricasTalking,
97 },
98 ),
99 (
100 "atalk://user@apikey/+{}?mode=PREM".format("4" * 11),
101 {
102 # PREM will match premium (not case sensitive)
103 "instance": NotifyAfricasTalking,
104 },
105 ),
106 (
107 "atalk://{}?apikey=key&user=user&from=FROMUSER".format("1" * 11),
108 {
109 # use get args to acomplish the same thing
110 "instance": NotifyAfricasTalking,
111 },
112 ),
113 (
114 "atalk://_?user=user&to={},{}&key={}&from={}".format(
115 "1" * 11, "2" * 11, "b" * 10, "5" * 13
116 ),
117 {
118 # use get args to acomplish the same thing
119 "instance": NotifyAfricasTalking,
120 },
121 ),
122 (
123 "atalk://user@apikey/{}/".format("1" * 11),
124 {
125 "instance": NotifyAfricasTalking,
126 # throw a bizarre code forcing us to fail to look it up
127 "response": False,
128 "requests_response_code": 999,
129 },
130 ),
131 (
132 "atalk://user@apikey/{}/".format("1" * 11),
133 {
134 "instance": NotifyAfricasTalking,
135 # Throws a series of i/o exceptions with this flag
136 # is set and tests that we gracefully handle them
137 "test_requests_exceptions": True,
138 },
139 ),
140 )
141
142
143 def test_plugin_atalk_urls():
144 """NotifyTemplate() 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_atalk_edge_cases(mock_post):
152 """NotifyAfricasTalking() Edge Cases."""
153
154 # Initialize some generic (but valid) tokens
155 apikey = "my-api-key"
156 appuser = "my-app-user"
157 targets = [
158 "+1(555) 123-1234",
159 "1555 5555555",
160 # A garbage entry
161 "12",
162 ]
163
164 # Prepare our response
165 response = requests.Request()
166 response.status_code = requests.codes.ok
167
168 # Prepare Mock
169 mock_post.return_value = response
170
171 # Instantiate our object
172 obj = Apprise.instantiate(
173 "atalk://{}@{}/{}?batch=n".format(appuser, apikey, "/".join(targets))
174 )
175
176 assert (
177 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
178 is True
179 )
180
181 # We know there are 2 (valid) targets
182 assert len(obj) == 2
183
184 # Test our call count
185 assert mock_post.call_count == 2
186
187 # Test
188 details = mock_post.call_args_list[0]
189 headers = details[1]["headers"]
190 assert headers["apiKey"] == apikey
191 payload = details[1]["data"]
192 assert payload["username"] == appuser
193 assert payload["from"] == "AFRICASTKNG"
194 assert payload["to"] == "+15551231234"
195 assert payload["message"] == "title\r\nbody"
196
197 details = mock_post.call_args_list[1]
198 headers = details[1]["headers"]
199 assert headers["apiKey"] == apikey
200 payload = details[1]["data"]
201 assert payload["username"] == appuser
202 assert payload["from"] == "AFRICASTKNG"
203 assert payload["to"] == "15555555555"
204 assert payload["message"] == "title\r\nbody"
205
206 # Verify our URL looks good
207 assert obj.url().startswith(
208 "atalk://{}@{}/{}".format(
209 appuser, apikey, "/".join(["+15551231234", "15555555555"])
210 )
211 )
212
213 assert "batch=no" in obj.url()
214
215 # Reset our mock object
216 mock_post.reset_mock()
217
218 # With our batch in place, our calculations are different
219 # Testing URL restructuring here as well where phone # is found
220 # in host
221 obj = Apprise.instantiate(
222 "atalk://{}?user={}&apikey={}&batch=y&from=TEST".format(
223 "/".join(targets), appuser, apikey
224 )
225 )
226
227 # 2 phones were loaded but counted as 1 due to batch flag
228 assert len(obj) == 1
229
230 assert (
231 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
232 is True
233 )
234
235 # Test our call count (batched into 1)
236 assert mock_post.call_count == 1
237
238 details = mock_post.call_args_list[0]
239 headers = details[1]["headers"]
240 assert headers["apiKey"] == apikey
241 payload = details[1]["data"]
242 assert payload["username"] == appuser
243 assert payload["from"] == "TEST"
244 assert payload["to"] == "+15551231234,15555555555"
245 assert payload["message"] == "title\r\nbody"