Debian Codegraph @

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