Debian Codegraph @

tests/test_plugin_sinch.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 dumps
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 pytest
36 import requests
37
38 from apprise.plugins.sinch import NotifySinch
39
40 logging.disable(logging.CRITICAL)
41
42 # Our Testing URLs
43 apprise_url_tests = (
44 (
45 "sinch://",
46 {
47 # No Account SID specified
48 "instance": TypeError,
49 },
50 ),
51 (
52 "sinch://:@/",
53 {
54 # invalid Auth token
55 "instance": TypeError,
56 },
57 ),
58 (
59 "sinch://{}@12345678".format("a" * 32),
60 {
61 # Just spi provided
62 "instance": TypeError,
63 },
64 ),
65 (
66 "sinch://{}:{}@_".format("a" * 32, "b" * 32),
67 {
68 # spi and token provided but invalid from
69 "instance": TypeError,
70 },
71 ),
72 (
73 "sinch://{}:{}@{}".format("a" * 32, "b" * 32, "3" * 5),
74 {
75 # using short-code (5 characters) without a target
76 # We can still instantiate ourselves with a valid short code
77 "instance": NotifySinch,
78 # Expected notify() response because we have no one to notify
79 "notify_response": False,
80 },
81 ),
82 (
83 "sinch://{}:{}@{}".format("a" * 32, "b" * 32, "3" * 9),
84 {
85 # spi and token provided and from but invalid from no
86 "instance": TypeError,
87 },
88 ),
89 (
90 "sinch://{}:{}@{}/123/{}/abcd/".format(
91 "a" * 32, "b" * 32, "3" * 11, "9" * 15
92 ),
93 {
94 # valid everything but target numbers
95 "instance": NotifySinch,
96 },
97 ),
98 (
99 "sinch://{}:{}@12345/{}".format("a" * 32, "b" * 32, "4" * 11),
100 {
101 # using short-code (5 characters)
102 "instance": NotifySinch,
103 # Our expected url(privacy=True) startswith() response:
104 "privacy_url": "sinch://...aaaa:b...b@12345",
105 },
106 ),
107 (
108 "sinch://{}:{}@123456/{}".format("a" * 32, "b" * 32, "4" * 11),
109 {
110 # using short-code (6 characters)
111 "instance": NotifySinch,
112 },
113 ),
114 (
115 "sinch://{}:{}@{}".format("a" * 32, "b" * 32, "5" * 11),
116 {
117 # using phone no with no target - we text ourselves in
118 # this case
119 "instance": NotifySinch,
120 },
121 ),
122 (
123 "sinch://{}:{}@{}?region=eu".format("a" * 32, "b" * 32, "5" * 11),
124 {
125 # Specify a region
126 "instance": NotifySinch,
127 },
128 ),
129 (
130 "sinch://{}:{}@{}?region=invalid".format("a" * 32, "b" * 32, "5" * 11),
131 {
132 # Invalid region
133 "instance": TypeError,
134 },
135 ),
136 (
137 "sinch://_?spi={}&token={}&from={}".format(
138 "a" * 32, "b" * 32, "5" * 11
139 ),
140 {
141 # use get args to acomplish the same thing
142 "instance": NotifySinch,
143 },
144 ),
145 (
146 "sinch://_?spi={}&token={}&source={}".format(
147 "a" * 32, "b" * 32, "5" * 11
148 ),
149 {
150 # use get args to acomplish the same thing (use source instead of
151 # from)
152 "instance": NotifySinch,
153 },
154 ),
155 (
156 "sinch://_?spi={}&token={}&from={}&to={}".format(
157 "a" * 32, "b" * 32, "5" * 11, "7" * 13
158 ),
159 {
160 # use to=
161 "instance": NotifySinch,
162 },
163 ),
164 (
165 "sinch://{}:{}@{}".format("a" * 32, "b" * 32, "6" * 11),
166 {
167 "instance": NotifySinch,
168 # throw a bizarre code forcing us to fail to look it up
169 "response": False,
170 "requests_response_code": 999,
171 },
172 ),
173 (
174 "sinch://{}:{}@{}".format("a" * 32, "b" * 32, "6" * 11),
175 {
176 "instance": NotifySinch,
177 # Throws a series of i/o exceptions with this flag
178 # is set and tests that we gracefully handle them
179 "test_requests_exceptions": True,
180 },
181 ),
182 )
183
184
185 def test_plugin_sinch_urls():
186 """NotifyTemplate() Apprise URLs."""
187
188 # Run our general tests
189 AppriseURLTester(tests=apprise_url_tests).run_all()
190
191
192 @mock.patch("requests.post")
193 def test_plugin_sinch_edge_cases(mock_post):
194 """NotifySinch() Edge Cases."""
195
196 # Prepare our response
197 response = requests.Request()
198 response.status_code = requests.codes.ok
199
200 # Prepare Mock
201 mock_post.return_value = response
202
203 # Initialize some generic (but valid) tokens
204 service_plan_id = "{}".format("b" * 32)
205 api_token = "{}".format("b" * 32)
206 source = "+1 (555) 123-3456"
207
208 # No service_plan_id specified
209 with pytest.raises(TypeError):
210 NotifySinch(service_plan_id=None, api_token=api_token, source=source)
211
212 # No api_token specified
213 with pytest.raises(TypeError):
214 NotifySinch(
215 service_plan_id=service_plan_id, api_token=None, source=source
216 )
217
218 # a error response
219 response.status_code = 400
220 response.content = dumps(
221 {
222 "code": 21211,
223 "message": "The 'To' number +1234567 is not a valid phone number.",
224 }
225 )
226 mock_post.return_value = response
227
228 # Initialize our object
229 obj = NotifySinch(
230 service_plan_id=service_plan_id, api_token=api_token, source=source
231 )
232
233 # We will fail with the above error code
234 assert obj.notify("title", "body", "info") is False