Debian Codegraph @

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