Debian Codegraph @

tests/test_plugin_octopush.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 import Apprise, NotifyType
37 from apprise.plugins.octopush import NotifyOctopush
38
39 logging.disable(logging.CRITICAL)
40
41 # Our Testing URLs
42 apprise_url_tests = (
43 (
44 "octopush://",
45 {
46 # No API Login or API Key specified
47 "instance": TypeError,
48 },
49 ),
50 (
51 "octopush://:@/",
52 {
53 # Invalid API Login
54 "instance": TypeError,
55 },
56 ),
57 (
58 "octopush://[email protected]",
59 {
60 # Valid API Login, but no API Key provided
61 "instance": TypeError,
62 },
63 ),
64 (
65 "octopush://_/apikey?login=invalid",
66 {
67 # Invalid login
68 "instance": TypeError,
69 },
70 ),
71 (
72 "octopush://[email protected]/%20",
73 {
74 # Valid API Login, but invalid API Key provided
75 "instance": TypeError,
76 },
77 ),
78 (
79 "octopush://%20:[email protected]/apikey",
80 {
81 # All valid entries, but invalid sender
82 "instance": TypeError,
83 },
84 ),
85 (
86 "octopush://[email protected]/apikey",
87 {
88 # All valid entries, but no target phone numbers defined
89 "instance": NotifyOctopush,
90 "response": False,
91 },
92 ),
93 (
94 "octopush://[email protected]/apikey/+0987654321",
95 {
96 # A valid url
97 "instance": NotifyOctopush,
98 "requests_response_code": requests.codes.created,
99 # Our expected url(privacy=True) startswith() response:
100 "privacy_url": "octopush://u...m/****/+0987654321",
101 },
102 ),
103 (
104 "octopush://sender:[email protected]/apikey/+1111111111",
105 {
106 # A valid url with sender
107 "instance": NotifyOctopush,
108 "requests_response_code": requests.codes.created,
109 # Our expected url(privacy=True) startswith() response:
110 "privacy_url": "octopush://sender:u...m/****/+1111111111",
111 },
112 ),
113 (
114 "octopush://[email protected]&key=key&to=9999999999"
115 "&purpose=wholesale",
116 {
117 # Testing valid purpose change
118 "instance": NotifyOctopush,
119 "requests_response_code": requests.codes.created,
120 },
121 ),
122 (
123 "octopush://[email protected]&key=key&to=9999999999"
124 "&purpose=invalid",
125 {
126 # Testing invalid purpose change
127 "instance": TypeError,
128 },
129 ),
130 (
131 "octopush://[email protected]&key=key&to=9999999999"
132 "&type=premium",
133 {
134 # Testing valid type change
135 "instance": NotifyOctopush,
136 "requests_response_code": requests.codes.created,
137 },
138 ),
139 (
140 "octopush://[email protected]&key=key&to=9999999999"
141 "&type=invalid",
142 {
143 # Testing invalid type change
144 "instance": TypeError,
145 },
146 ),
147 (
148 "octopush://[email protected]/apikey/+3333333333?replies=yes",
149 {
150 # Test replies
151 "instance": NotifyOctopush,
152 "requests_response_code": requests.codes.created,
153 },
154 ),
155 (
156 "octopush://sender:[email protected]/apikey/{}/{}/{}/?batch=yes".format(
157 "1" * 10, "2" * 3, "3" * 11
158 ),
159 {
160 # batch mode, 2 valid targets (1 is invalid and skipped)
161 "instance": NotifyOctopush,
162 "requests_response_code": requests.codes.created,
163 },
164 ),
165 (
166 "octopush://_?key=abc123&[email protected]&sender=abc"
167 "&to=2222222222",
168 {
169 # use get args to accomplish the same thing
170 "instance": NotifyOctopush,
171 "requests_response_code": requests.codes.created,
172 # Our expected url(privacy=True) startswith() response:
173 "privacy_url": "octopush://abc:u...m/****/+2222222222",
174 },
175 ),
176 (
177 "octopush://[email protected]/apikey/1234567890",
178 {
179 "instance": NotifyOctopush,
180 # throw a bizarre code forcing us to fail to look it up
181 "response": False,
182 "requests_response_code": 999,
183 },
184 ),
185 (
186 "octopush://[email protected]/apikey/1234567890",
187 {
188 "instance": NotifyOctopush,
189 # Throws a series of connection and transfer exceptions when this
190 # flag is set and tests that we gracefully handle them
191 "test_requests_exceptions": True,
192 },
193 ),
194 )
195
196
197 def test_plugin_octopush_urls():
198 """NotifyOctopush() Apprise URLs."""
199
200 assert (
201 NotifyOctopush.setup_url == "https://appriseit.com/services/octopush/"
202 )
203
204 # Run our general tests
205 AppriseURLTester(tests=apprise_url_tests).run_all()
206
207
208 def test_plugin_octopush_parse_url_and_validation():
209 """NotifyOctopush() parse_url() and validation coverage."""
210
211 assert NotifyOctopush.parse_url(None) is None
212
213 with pytest.raises(TypeError):
214 NotifyOctopush(
215 api_login="[email protected]",
216 api_key="apikey",
217 targets=("1234567890",),
218 purpose=" ",
219 )
220
221 results = NotifyOctopush.parse_url(
222 "octopush://sender:[email protected]/apikey/1234567890"
223 "?to=2345678901&type=sms_premium&purpose=alert&batch=yes"
224 "&replies=no"
225 )
226 assert isinstance(results, dict)
227 assert results["api_key"] == "apikey"
228 assert results["api_login"] == "[email protected]"
229 assert results["sender"] == "sender"
230 assert results["targets"] == ["1234567890", "2345678901"]
231 assert results["mtype"] == "sms_premium"
232 assert results["purpose"] == "alert"
233 assert results["batch"] is True
234 assert results["replies"] is False
235
236 results = NotifyOctopush.parse_url(
237 "octopush://[email protected]&key=apikey&sender=abc"
238 "&to=3456789012&type=sms_low_cost&purpose=wholesale"
239 "&batch=no&replies=yes"
240 )
241 assert isinstance(results, dict)
242 assert results["api_key"] == "apikey"
243 assert results["api_login"] == "[email protected]"
244 assert results["sender"] == "abc"
245 assert results["targets"] == ["3456789012"]
246 assert results["mtype"] == "sms_low_cost"
247 assert results["purpose"] == "wholesale"
248 assert results["batch"] is False
249 assert results["replies"] is True
250
251
252 @mock.patch("requests.post")
253 def test_plugin_octopush_edge_cases(mock_post):
254 """NotifyOctopush() Edge Cases."""
255
256 response = requests.Request()
257 response.status_code = requests.codes.created
258 mock_post.return_value = response
259
260 obj = Apprise.instantiate(
261 "octopush://sender:[email protected]/apikey/1234567890/2345678901"
262 "?batch=yes&replies=yes&type=low_cost&purpose=wholesale"
263 )
264
265 assert isinstance(obj, NotifyOctopush)
266 assert len(obj) == 1
267 assert obj.targets == ["+1234567890", "+2345678901"]
268 assert obj.mtype == "sms_low_cost"
269 assert obj.purpose == "wholesale"
270 assert obj.replies is True
271
272 assert obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
273 assert mock_post.call_count == 1
274
275 call = mock_post.call_args_list[0]
276 assert call[0][0] == "https://api.octopush.com/v1/public/sms-campaign/send"
277
278 headers = call[1]["headers"]
279 assert headers["api-login"] == "[email protected]"
280 assert headers["api-key"] == "apikey"
281 assert headers["Content-Type"] == "application/json; charset=utf-8"
282
283 payload = call[1]["data"]
284 assert '"type": "sms_low_cost"' in payload
285 assert '"purpose": "wholesale"' in payload
286 assert '"with_replies": true' in payload
287 assert '"phone_number": "+1234567890"' in payload
288 assert '"phone_number": "+2345678901"' in payload
289
290 obj = NotifyOctopush(
291 api_login="[email protected]",
292 api_key="apikey",
293 targets=tuple(str(1000000000 + i) for i in range(501)),
294 batch=True,
295 )
296 assert len(obj) == 2
297
298 mock_post.reset_mock()
299 assert obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
300 assert mock_post.call_count == 2
301
302 first_payload = mock_post.call_args_list[0][1]["data"]
303 assert first_payload.count('"phone_number"') == 500
304
305 second_payload = mock_post.call_args_list[1][1]["data"]
306 assert second_payload.count('"phone_number"') == 1