Debian Codegraph @

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