Debian Codegraph @

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