Debian Codegraph @

tests/test_plugin_d7networks.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 loads
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 requests
36
37 from apprise import Apprise, NotifyType
38 from apprise.exception import AppriseImproperlyConfigured
39 from apprise.plugins.d7networks import NotifyD7Networks
40
41 logging.disable(logging.CRITICAL)
42
43 # Our Testing URLs
44 apprise_url_tests = (
45 (
46 "d7sms://",
47 {
48 # We failed to identify any valid authentication
49 "instance": AppriseImproperlyConfigured,
50 },
51 ),
52 (
53 "d7sms://:@/",
54 {
55 # We failed to identify any valid authentication
56 "instance": AppriseImproperlyConfigured,
57 },
58 ),
59 (
60 "d7sms://token@{}/{}/{}".format("1" * 9, "2" * 15, "a" * 13),
61 {
62 # No valid targets to notify
63 "instance": NotifyD7Networks,
64 # Since there are no targets specified we expect a False return on
65 # send()
66 "notify_response": False,
67 },
68 ),
69 (
70 "d7sms://token1@{}?batch=yes".format("3" * 14),
71 {
72 # valid number
73 "instance": NotifyD7Networks,
74 # Our expected url(privacy=True) startswith() response:
75 "privacy_url": "d7sms://t...1@",
76 },
77 ),
78 (
79 "d7sms://token:colon2@{}?batch=yes".format("3" * 14),
80 {
81 # valid number - token containing a colon
82 "instance": NotifyD7Networks,
83 # Our expected url(privacy=True) startswith() response:
84 "privacy_url": "d7sms://t...2@",
85 },
86 ),
87 (
88 "d7sms://:token3@{}?batch=yes".format("3" * 14),
89 {
90 # valid number - token starting wit a colon
91 "instance": NotifyD7Networks,
92 # Our expected url(privacy=True) startswith() response:
93 "privacy_url": "d7sms://:...3@",
94 },
95 ),
96 (
97 "d7sms://{}?token=token6".format("3" * 14),
98 {
99 # valid number - token starting wit a colon
100 "instance": NotifyD7Networks,
101 # Our expected url(privacy=True) startswith() response:
102 "privacy_url": "d7sms://t...6@",
103 },
104 ),
105 (
106 "d7sms://token4@{}?unicode=no".format("3" * 14),
107 {
108 # valid number - test unicode
109 "instance": NotifyD7Networks,
110 # Our expected url(privacy=True) startswith() response:
111 "privacy_url": "d7sms://t...4@",
112 },
113 ),
114 (
115 "d7sms://token8@{}/{}/?unicode=yes".format("3" * 14, "4" * 14),
116 {
117 # valid number - test unicode
118 "instance": NotifyD7Networks,
119 # Our expected url(privacy=True) startswith() response:
120 "privacy_url": "d7sms://t...8@",
121 },
122 ),
123 (
124 "d7sms://token@{}?batch=yes&to={}".format("3" * 14, "6" * 14),
125 {
126 # valid number
127 "instance": NotifyD7Networks,
128 },
129 ),
130 (
131 "d7sms://token@{}?batch=yes&from=apprise".format("3" * 14),
132 {
133 # valid number, utilizing the optional from= variable
134 "instance": NotifyD7Networks,
135 },
136 ),
137 (
138 "d7sms://token@{}?batch=yes&source=apprise".format("3" * 14),
139 {
140 # valid number, utilizing the optional source= variable (same as
141 # from)
142 "instance": NotifyD7Networks,
143 },
144 ),
145 (
146 "d7sms://token@{}?batch=no".format("3" * 14),
147 {
148 # valid number - no batch
149 "instance": NotifyD7Networks,
150 },
151 ),
152 (
153 "d7sms://token@{}".format("3" * 14),
154 {
155 "instance": NotifyD7Networks,
156 # throw a bizarre code forcing us to fail to look it up
157 "response": False,
158 "requests_response_code": 999,
159 },
160 ),
161 (
162 "d7sms://token@{}".format("3" * 14),
163 {
164 "instance": NotifyD7Networks,
165 # Throws a series of i/o exceptions with this flag
166 # is set and tests that we gracefully handle them
167 "test_requests_exceptions": True,
168 },
169 ),
170 )
171
172
173 def test_plugin_d7networks_urls():
174 """NotifyD7Networks() Apprise URLs."""
175
176 # Run our general tests
177 AppriseURLTester(tests=apprise_url_tests).run_all()
178
179
180 @mock.patch("requests.post")
181 def test_plugin_d7networks_edge_cases(mock_post):
182 """NotifyD7Networks() Edge Cases tests."""
183
184 # Prepare Mock
185 request = mock.Mock()
186 request.content = "{}"
187 request.status_code = requests.codes.ok
188 mock_post.return_value = request
189
190 # Initializations
191 aobj = Apprise()
192 assert aobj.add("d7sms://Token@15551231234/15551231236")
193
194 body = "test message"
195
196 # Send our notification
197 assert aobj.notify(body=body, title="title", notify_type=NotifyType.INFO)
198
199 # Not set to batch, so we send 2 different messages
200 assert mock_post.call_count == 2
201 assert (
202 mock_post.call_args_list[0][0][0]
203 == "https://api.d7networks.com/messages/v1/send"
204 )
205 assert (
206 mock_post.call_args_list[1][0][0]
207 == "https://api.d7networks.com/messages/v1/send"
208 )
209
210 # our first post
211 data = loads(mock_post.call_args_list[0][1]["data"])
212 assert len(data["messages"]) == 1
213 message = data["messages"][0]
214 assert len(message["recipients"]) == 1
215 assert message["content"] == "title\r\ntest message"
216 assert message["data_coding"] == "auto"
217
218 # our second post
219 data = loads(mock_post.call_args_list[1][1]["data"])
220 assert len(data["messages"]) == 1
221 message = data["messages"][0]
222 assert len(message["recipients"]) == 1
223 assert message["content"] == "title\r\ntest message"
224 assert message["data_coding"] == "auto"
225
226 #
227 # Do a batch test now
228 #
229
230 mock_post.reset_mock()
231
232 # Initializations
233 aobj = Apprise()
234 assert aobj.add("d7sms://Token@15551231234/15551231236?batch=yes")
235
236 body = "test message"
237
238 # Send our notification
239 assert aobj.notify(body=body, title="title", notify_type=NotifyType.INFO)
240
241 # All notifications go through in a batch
242 assert mock_post.call_count == 1
243 assert (
244 mock_post.call_args_list[0][0][0]
245 == "https://api.d7networks.com/messages/v1/send"
246 )
247
248 data = loads(mock_post.call_args_list[0][1]["data"])
249 assert len(data["messages"]) == 1
250 message = data["messages"][0]
251 # All of our phone numbers were added here
252 assert len(message["recipients"]) == 2
253 assert "15551231234" in message["recipients"]
254 assert "15551231236" in message["recipients"]
255 assert message["content"] == "title\r\ntest message"
256 assert message["data_coding"] == "auto"