Debian Codegraph @

tests/test_plugin_burstsms.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 from json import dumps
29
30 # Disable logging for a cleaner testing output
31 import logging
32 from unittest import mock
33
34 from helpers import AppriseURLTester
35 import pytest
36 import requests
37
38 from apprise.exception import AppriseImproperlyConfigured
39 from apprise.plugins.burstsms import NotifyBurstSMS
40
41 logging.disable(logging.CRITICAL)
42
43 # Our Testing URLs
44 apprise_url_tests = (
45 (
46 "burstsms://",
47 {
48 # No API Key specified
49 "instance": AppriseImproperlyConfigured,
50 },
51 ),
52 (
53 "burstsms://:@/",
54 {
55 # invalid Auth key
56 "instance": AppriseImproperlyConfigured,
57 },
58 ),
59 (
60 "burstsms://{}@12345678".format("a" * 8),
61 {
62 # Just a key provided
63 "instance": AppriseImproperlyConfigured,
64 },
65 ),
66 (
67 "burstsms://{}:{}@%20".format("d" * 8, "e" * 16),
68 {
69 # Invalid source number
70 "instance": AppriseImproperlyConfigured,
71 },
72 ),
73 (
74 "burstsms://{}:{}@{}/123/{}/abcd/".format(
75 "f" * 8, "g" * 16, "3" * 11, "9" * 15
76 ),
77 {
78 # valid everything but target numbers
79 "instance": NotifyBurstSMS,
80 # Expected notify() response because not all targets are valid
81 "notify_response": False,
82 # Our expected url(privacy=True) startswith() response:
83 "privacy_url": "burstsms://f...f:****@",
84 },
85 ),
86 (
87 "burstsms://{}:{}@{}".format("h" * 8, "i" * 16, "5" * 11),
88 {
89 "instance": NotifyBurstSMS,
90 # Expected notify() response because no targets are defined
91 "notify_response": False,
92 },
93 ),
94 (
95 "burstsms://_?key={}&secret={}&from={}&to={}".format(
96 "a" * 8, "b" * 16, "5" * 11, "6" * 11
97 ),
98 {
99 # use get args to acomplish the same thing
100 "instance": NotifyBurstSMS,
101 },
102 ),
103 (
104 "burstsms://_?key={}&secret={}&from={}&to={}&batch=y".format(
105 "a" * 8, "b" * 16, "5" * 11, "6" * 11
106 ),
107 {
108 # batch flag set
109 "instance": NotifyBurstSMS,
110 },
111 ),
112 # Test our country
113 (
114 "burstsms://_?key={}&secret={}&source={}&to={}&country=us".format(
115 "a" * 8, "b" * 16, "5" * 11, "6" * 11
116 ),
117 {
118 "instance": NotifyBurstSMS,
119 },
120 ),
121 # Test an invalid country
122 (
123 "burstsms://_?key={}&secret={}&source={}&to={}&country=invalid".format(
124 "a" * 8, "b" * 16, "5" * 11, "6" * 11
125 ),
126 {
127 "instance": AppriseImproperlyConfigured,
128 },
129 ),
130 # Test our validity
131 (
132 "burstsms://_?key={}&secret={}&source={}&to={}&validity=10".format(
133 "a" * 8, "b" * 16, "5" * 11, "6" * 11
134 ),
135 {
136 "instance": NotifyBurstSMS,
137 },
138 ),
139 # Test an invalid country
140 (
141 "burstsms://_?key={}&secret={}&source={}&to={}&validity=invalid".format(
142 "a" * 8, "b" * 16, "5" * 11, "6" * 11
143 ),
144 {
145 "instance": AppriseImproperlyConfigured,
146 },
147 ),
148 (
149 "burstsms://_?key={}&secret={}&from={}&to={}".format(
150 "a" * 8, "b" * 16, "5" * 11, "7" * 11
151 ),
152 {
153 # use to=
154 "instance": NotifyBurstSMS,
155 },
156 ),
157 (
158 "burstsms://{}:{}@{}/{}".format("a" * 8, "b" * 16, "6" * 11, "7" * 11),
159 {
160 "instance": NotifyBurstSMS,
161 # throw a bizarre code forcing us to fail to look it up
162 "response": False,
163 "requests_response_code": 999,
164 },
165 ),
166 (
167 "burstsms://{}:{}@{}/{}".format("a" * 8, "b" * 16, "6" * 11, "7" * 11),
168 {
169 "instance": NotifyBurstSMS,
170 # Throws a series of i/o exceptions with this flag
171 # is set and tests that we gracefully handle them
172 "test_requests_exceptions": True,
173 },
174 ),
175 )
176
177
178 def test_plugin_burstsms_urls():
179 """NotifyBurstSMS() Apprise URLs."""
180
181 # Run our general tests
182 AppriseURLTester(tests=apprise_url_tests).run_all()
183
184
185 @mock.patch("requests.post")
186 def test_plugin_burstsms_edge_cases(mock_post):
187 """NotifyBurstSMS() Edge Cases."""
188
189 # Prepare our response
190 response = requests.Request()
191 response.status_code = requests.codes.ok
192
193 # Prepare Mock
194 mock_post.return_value = response
195
196 # Initialize some generic (but valid) tokens
197 apikey = "{}".format("b" * 8)
198 secret = "{}".format("b" * 16)
199 source = "+1 (555) 123-3456"
200
201 # No apikey specified
202 with pytest.raises(AppriseImproperlyConfigured):
203 NotifyBurstSMS(apikey=None, secret=secret, source=source)
204
205 with pytest.raises(AppriseImproperlyConfigured):
206 NotifyBurstSMS(apikey=" ", secret=secret, source=source)
207
208 # No secret specified
209 with pytest.raises(AppriseImproperlyConfigured):
210 NotifyBurstSMS(apikey=apikey, secret=None, source=source)
211
212 with pytest.raises(AppriseImproperlyConfigured):
213 NotifyBurstSMS(apikey=apikey, secret=" ", source=source)
214
215 # a error response
216 response.status_code = 400
217 response.content = dumps(
218 {
219 "error": {
220 "code": "FIELD_INVALID",
221 "description": (
222 "Sender ID must be one of the numbers that are currently"
223 " leased."
224 ),
225 },
226 }
227 )
228 mock_post.return_value = response
229
230 # Initialize our object
231 obj = NotifyBurstSMS(apikey=apikey, secret=secret, source=source)
232
233 # We will fail with the above error code
234 assert bool(obj.notify("title", "body", "info")) is False