Debian Codegraph @

tests/test_plugin_bulkvs.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.bulkvs import NotifyBulkVS
40
41 logging.disable(logging.CRITICAL)
42
43 # Our Testing URLs
44 apprise_url_tests = (
45 (
46 "bulkvs://",
47 {
48 # Instantiated but no auth, so no otification can happen
49 "instance": AppriseImproperlyConfigured,
50 },
51 ),
52 (
53 "bulkvs://:@/",
54 {
55 # invalid auth
56 "instance": AppriseImproperlyConfigured,
57 },
58 ),
59 (
60 "bulkvs://{}@9876543210/".format("a" * 10),
61 {
62 # Just user provided (no password)
63 "instance": AppriseImproperlyConfigured,
64 },
65 ),
66 (
67 "bulkvs://{}:{}@{}".format("u" * 10, "p" * 10, "3" * 5),
68 {
69 # invalid source number provided
70 "instance": AppriseImproperlyConfigured,
71 },
72 ),
73 (
74 "bulkvs://{}:{}@{}/{}".format("u" * 10, "p" * 10, "1" * 10, 2 * "5"),
75 {
76 # invalid target number provided
77 "instance": NotifyBulkVS,
78 # Expected notify() response because we have no one to notify
79 "notify_response": False,
80 },
81 ),
82 (
83 "bulkvs://{}:{}@{}".format("u" * 10, "p" * 10, "2" * 10),
84 {
85 # default to ourselves
86 "instance": NotifyBulkVS,
87 },
88 ),
89 (
90 "bulkvs://{}:{}@9876543210/{}/abcd/".format(
91 "a" * 5, "b" * 10, "3" * 11
92 ),
93 {
94 # included phone, short number (123) and garbage string (abcd)
95 # dropped
96 "instance": NotifyBulkVS,
97 "privacy_url": "bulkvs://a...a:****@9876543210/33333333333",
98 },
99 ),
100 (
101 "bulkvs://{}:{}@{}?batch=y".format("b" * 5, "c" * 10, "4" * 11),
102 {
103 "instance": NotifyBulkVS,
104 # Our expected url(privacy=True) startswith() response:
105 "privacy_url": "bulkvs://b...b:****@44444444444",
106 },
107 ),
108 (
109 "bulkvs://{}:{}@{}".format("a" * 10, "b" * 10, "5" * 11),
110 {
111 # using phone no with no target - we text ourselves in
112 # this case
113 "instance": NotifyBulkVS,
114 },
115 ),
116 (
117 "bulkvs://?user={}&password={}&from={}".format(
118 "z" * 10, "y" * 10, "5" * 11
119 ),
120 {
121 # use get args to acomplish the same thing
122 "instance": NotifyBulkVS,
123 },
124 ),
125 (
126 "bulkvs://?user={}&password={}&from={}&to={}".format(
127 "a" * 10, "b" * 10, "5" * 11, "7" * 13
128 ),
129 {
130 # use to=
131 "instance": NotifyBulkVS,
132 },
133 ),
134 (
135 "bulkvs://{}:{}@{}".format("a" * 10, "b" * 10, "5" * 11),
136 {
137 "instance": NotifyBulkVS,
138 # throw a bizarre code forcing us to fail to look it up
139 "response": False,
140 "requests_response_code": 999,
141 },
142 ),
143 (
144 "bulkvs://{}:{}@{}".format("a" * 10, "b" * 10, "5" * 11),
145 {
146 "instance": NotifyBulkVS,
147 # Throws a series of i/o exceptions with this flag
148 # is set and tests that we gracefully handle them
149 "test_requests_exceptions": True,
150 },
151 ),
152 )
153
154
155 def test_plugin_bulkvs_urls():
156 """NotifyTemplate() Apprise URLs."""
157
158 # Run our general tests
159 AppriseURLTester(tests=apprise_url_tests).run_all()
160
161
162 @mock.patch("requests.post")
163 def test_plugin_bulkvs_edge_cases(mock_post):
164 """NotifyBulkVS() Edge Cases."""
165
166 # Initialize some generic (but valid) tokens
167 user = "abcd"
168 pwd = "mypass123"
169 source = "1 (405) 123 1234"
170 targets = [
171 "+1(555) 123-1234",
172 "1555 5555555",
173 # A garbage entry
174 "12",
175 # NOw a valid one because a group was implicit
176 "@12",
177 ]
178
179 # Prepare our response
180 response = requests.Request()
181 response.status_code = requests.codes.ok
182
183 # Prepare Mock
184 mock_post.return_value = response
185
186 # Instantiate our object
187 obj = Apprise.instantiate(
188 "bulkvs://{}:{}@{}/{}?batch=n".format(
189 user, pwd, source, "/".join(targets)
190 )
191 )
192
193 assert (
194 bool(
195 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
196 )
197 is True
198 )
199
200 # We know there are 2 targets
201 assert len(obj) == 2
202
203 # Test our call count
204 assert mock_post.call_count == 2
205
206 # Test
207 details = mock_post.call_args_list[0]
208 payload = loads(details[1]["data"])
209 assert payload["From"] == "14051231234"
210 assert payload["To"] == "15551231234"
211 assert payload["Message"] == "title\r\nbody"
212
213 details = mock_post.call_args_list[1]
214 payload = loads(details[1]["data"])
215 assert payload["From"] == "14051231234"
216 assert payload["To"] == "15555555555"
217 assert payload["Message"] == "title\r\nbody"
218
219 # Verify our URL looks good
220 assert obj.url().startswith(
221 "bulkvs://abcd:mypass123@14051231234/15551231234/15555555555"
222 )
223
224 assert "batch=no" in obj.url()
225
226 # With our batch in place, our calculations are different
227 obj = Apprise.instantiate(
228 "bulkvs://{}:{}@{}?batch=y".format(user, pwd, "/".join(targets))
229 )
230 # 2 phones are lumped together
231 assert len(obj) == 1