Debian Codegraph @

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