Debian Codegraph @

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