| 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 import Apprise |
| 39 |
from apprise.exception import AppriseImproperlyConfigured |
| 40 |
from apprise.plugins.twilio import NotifyTwilio, TwilioNotificationMethod |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
# Our Testing URLs |
| 45 |
apprise_url_tests = ( |
| 46 |
( |
| 47 |
"twilio://", |
| 48 |
{ |
| 49 |
# No Account SID specified |
| 50 |
"instance": AppriseImproperlyConfigured, |
| 51 |
}, |
| 52 |
), |
| 53 |
( |
| 54 |
"twilio://:@/", |
| 55 |
{ |
| 56 |
# invalid Auth token |
| 57 |
"instance": AppriseImproperlyConfigured, |
| 58 |
}, |
| 59 |
), |
| 60 |
( |
| 61 |
"twilio://AC{}@12345678".format("a" * 32), |
| 62 |
{ |
| 63 |
# Just sid provided |
| 64 |
"instance": AppriseImproperlyConfigured, |
| 65 |
}, |
| 66 |
), |
| 67 |
( |
| 68 |
"twilio://AC{}:{}@_".format("a" * 32, "b" * 32), |
| 69 |
{ |
| 70 |
# sid and token provided but invalid from |
| 71 |
"instance": AppriseImproperlyConfigured, |
| 72 |
}, |
| 73 |
), |
| 74 |
( |
| 75 |
"twilio://AC{}:{}@{}".format("a" * 32, "b" * 32, "3" * 5), |
| 76 |
{ |
| 77 |
# using short-code (5 characters) without a target |
| 78 |
# We can still instantiate ourselves with a valid short code |
| 79 |
"instance": NotifyTwilio, |
| 80 |
# Since there are no targets specified we expect a False return on |
| 81 |
# send() |
| 82 |
"notify_response": False, |
| 83 |
}, |
| 84 |
), |
| 85 |
( |
| 86 |
"twilio://AC{}:{}@{}".format("a" * 32, "b" * 32, "3" * 9), |
| 87 |
{ |
| 88 |
# sid and token provided and from but invalid from no |
| 89 |
"instance": AppriseImproperlyConfigured, |
| 90 |
}, |
| 91 |
), |
| 92 |
( |
| 93 |
"twilio://AC{}:{}@{}/123/{}/abcd/w:{}".format( |
| 94 |
"a" * 32, "b" * 32, "3" * 11, "9" * 15, 8 * 11 |
| 95 |
), |
| 96 |
{ |
| 97 |
# valid everything but target numbers |
| 98 |
"instance": NotifyTwilio, |
| 99 |
}, |
| 100 |
), |
| 101 |
( |
| 102 |
"twilio://AC{}:{}@12345/{}".format("a" * 32, "b" * 32, "4" * 11), |
| 103 |
{ |
| 104 |
# using short-code (5 characters) |
| 105 |
"instance": NotifyTwilio, |
| 106 |
# Our expected url(privacy=True) startswith() response: |
| 107 |
"privacy_url": "twilio://...aaaa:b...b@12345", |
| 108 |
}, |
| 109 |
), |
| 110 |
( |
| 111 |
"twilio://AC{}:{}@98765/{}/w:{}/".format( |
| 112 |
"a" * 32, "b" * 32, "4" * 11, "5" * 11 |
| 113 |
), |
| 114 |
{ |
| 115 |
# using short-code (5 characters) and 1 twillio address ignored |
| 116 |
# because source phone number can not be a short code |
| 117 |
"instance": NotifyTwilio, |
| 118 |
# Our expected url(privacy=True) startswith() response: |
| 119 |
"privacy_url": "twilio://...aaaa:b...b@98765", |
| 120 |
}, |
| 121 |
), |
| 122 |
( |
| 123 |
"twilio://AC{}:{}@w:12345/{}/{}".format( |
| 124 |
"a" * 32, "b" * 32, "4" * 11, "5" * 11 |
| 125 |
), |
| 126 |
{ |
| 127 |
# Invalid short-code |
| 128 |
"instance": AppriseImproperlyConfigured, |
| 129 |
}, |
| 130 |
), |
| 131 |
( |
| 132 |
"twilio://AC{}:{}@123456/{}".format("a" * 32, "b" * 32, "4" * 11), |
| 133 |
{ |
| 134 |
# using short-code (6 characters) |
| 135 |
"instance": NotifyTwilio, |
| 136 |
}, |
| 137 |
), |
| 138 |
( |
| 139 |
"twilio://AC{}:{}@{}".format("a" * 32, "b" * 32, "5" * 11), |
| 140 |
{ |
| 141 |
# using phone no with no target - we text ourselves in |
| 142 |
# this case |
| 143 |
"instance": NotifyTwilio, |
| 144 |
}, |
| 145 |
), |
| 146 |
( |
| 147 |
"twilio://AC{}:{}@{}?method=sms".format("a" * 32, "b" * 32, "5" * 11), |
| 148 |
{ |
| 149 |
# Specify explicitly notification method sms |
| 150 |
"instance": NotifyTwilio, |
| 151 |
}, |
| 152 |
), |
| 153 |
( |
| 154 |
"twilio://AC{}:{}@{}?method=mms".format("a" * 32, "b" * 32, "5" * 11), |
| 155 |
{ |
| 156 |
# Invalid notification method |
| 157 |
"instance": AppriseImproperlyConfigured, |
| 158 |
}, |
| 159 |
), |
| 160 |
( |
| 161 |
"twilio://AC{}:{}@{}?method=call".format( |
| 162 |
"a" * 32, "b" * 32, "w:" + "5" * 11 |
| 163 |
), |
| 164 |
{ |
| 165 |
# Incompatibility between Whatsapp mode and CALL method |
| 166 |
"instance": AppriseImproperlyConfigured, |
| 167 |
}, |
| 168 |
), |
| 169 |
( |
| 170 |
"twilio://_?sid=AC{}&token={}&from={}".format( |
| 171 |
"a" * 32, "b" * 32, "5" * 11 |
| 172 |
), |
| 173 |
{ |
| 174 |
# use get args to acomplish the same thing |
| 175 |
"instance": NotifyTwilio, |
| 176 |
}, |
| 177 |
), |
| 178 |
( |
| 179 |
"twilio://_?sid=AC{}&token={}&from={}&to=w:{}".format( |
| 180 |
"a" * 32, "b" * 32, "5" * 11, "6" * 11 |
| 181 |
), |
| 182 |
{ |
| 183 |
# Support whatsapp (w: before number) |
| 184 |
"instance": NotifyTwilio, |
| 185 |
}, |
| 186 |
), |
| 187 |
( |
| 188 |
"twilio://_?sid=AC{}&token={}&source={}".format( |
| 189 |
"a" * 32, "b" * 32, "5" * 11 |
| 190 |
), |
| 191 |
{ |
| 192 |
# use get args to acomplish the same thing (use source instead of |
| 193 |
# from) |
| 194 |
"instance": NotifyTwilio, |
| 195 |
}, |
| 196 |
), |
| 197 |
( |
| 198 |
"twilio://_?sid=AC{}&token={}&from={}&to={}".format( |
| 199 |
"a" * 32, "b" * 32, "5" * 11, "7" * 13 |
| 200 |
), |
| 201 |
{ |
| 202 |
# use to= |
| 203 |
"instance": NotifyTwilio, |
| 204 |
}, |
| 205 |
), |
| 206 |
( |
| 207 |
"twilio://_?sid=AC{}&token={}&from={}&to={}method=call".format( |
| 208 |
"a" * 32, "b" * 32, "5" * 11, "7" * 13 |
| 209 |
), |
| 210 |
{ |
| 211 |
# Specify notification method call |
| 212 |
"instance": NotifyTwilio, |
| 213 |
}, |
| 214 |
), |
| 215 |
( |
| 216 |
"twilio://AC{}:{}@{}".format("a" * 32, "b" * 32, "6" * 11), |
| 217 |
{ |
| 218 |
"instance": NotifyTwilio, |
| 219 |
# throw a bizarre code forcing us to fail to look it up |
| 220 |
"response": False, |
| 221 |
"requests_response_code": 999, |
| 222 |
}, |
| 223 |
), |
| 224 |
( |
| 225 |
"twilio://AC{}:{}@{}".format("a" * 32, "b" * 32, "6" * 11), |
| 226 |
{ |
| 227 |
"instance": NotifyTwilio, |
| 228 |
# Throws a series of i/o exceptions with this flag |
| 229 |
# is set and tests that we gracefully handle them |
| 230 |
"test_requests_exceptions": True, |
| 231 |
}, |
| 232 |
), |
| 233 |
) |
| 234 |
|
| 235 |
|
| 236 |
def test_plugin_twilio_urls(): |
| 237 |
"""NotifyTwilio() Apprise URLs.""" |
| 238 |
|
| 239 |
# Run our general tests |
| 240 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 241 |
|
| 242 |
|
| 243 |
@mock.patch("requests.post") |
| 244 |
def test_plugin_twilio_auth(mock_post): |
| 245 |
"""NotifyTwilio() Auth. |
| 246 |
|
| 247 |
- account-wide auth token |
| 248 |
- API key and its own auth token |
| 249 |
""" |
| 250 |
|
| 251 |
response = mock.Mock() |
| 252 |
response.content = "" |
| 253 |
response.status_code = requests.codes.ok |
| 254 |
|
| 255 |
# Prepare Mock |
| 256 |
mock_post.return_value = response |
| 257 |
|
| 258 |
# Initialize some generic (but valid) tokens |
| 259 |
account_sid = "AC{}".format("b" * 32) |
| 260 |
apikey = "SK{}".format("b" * 32) |
| 261 |
auth_token = "{}".format("b" * 32) |
| 262 |
source = "+1 (555) 123-3456" |
| 263 |
dest = "+1 (555) 987-6543" |
| 264 |
message_contents = "test" |
| 265 |
|
| 266 |
# Variation of initialization without API key |
| 267 |
obj = Apprise.instantiate( |
| 268 |
f"twilio://{account_sid}:{auth_token}@{source}/{dest}" |
| 269 |
) |
| 270 |
assert isinstance(obj, NotifyTwilio) |
| 271 |
assert isinstance(obj.url(), str) |
| 272 |
|
| 273 |
# Send Notification |
| 274 |
assert obj.send(body=message_contents) is True |
| 275 |
|
| 276 |
# Variation of initialization with API key |
| 277 |
obj = Apprise.instantiate( |
| 278 |
f"twilio://{account_sid}:{auth_token}@{source}/{dest}?apikey={apikey}" |
| 279 |
) |
| 280 |
assert isinstance(obj, NotifyTwilio) |
| 281 |
assert isinstance(obj.url(), str) |
| 282 |
|
| 283 |
# Send Notification |
| 284 |
assert obj.send(body=message_contents) is True |
| 285 |
|
| 286 |
# Variation of initialization with method call |
| 287 |
obj = Apprise.instantiate( |
| 288 |
f"twilio://{account_sid}:{auth_token}@{source}/{dest}?method=call" |
| 289 |
) |
| 290 |
assert isinstance(obj, NotifyTwilio) |
| 291 |
assert isinstance(obj.url(), str) |
| 292 |
|
| 293 |
# Send Notification |
| 294 |
assert obj.send(body=message_contents) is True |
| 295 |
|
| 296 |
# Validate expected call parameters |
| 297 |
assert mock_post.call_count == 3 |
| 298 |
first_call = mock_post.call_args_list[0] |
| 299 |
second_call = mock_post.call_args_list[1] |
| 300 |
third_call = mock_post.call_args_list[2] |
| 301 |
|
| 302 |
# URL and message parameters are the same for both calls |
| 303 |
assert ( |
| 304 |
first_call[0][0] |
| 305 |
== second_call[0][0] |
| 306 |
== ( |
| 307 |
"https://api.twilio.com/2010-04-01/Accounts" |
| 308 |
f"/{account_sid}/Messages.json" |
| 309 |
) |
| 310 |
) |
| 311 |
assert ( |
| 312 |
first_call[1]["data"]["Body"] |
| 313 |
== second_call[1]["data"]["Body"] |
| 314 |
== message_contents |
| 315 |
) |
| 316 |
assert third_call[0][0] == ( |
| 317 |
f"https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Calls.json" |
| 318 |
) |
| 319 |
assert third_call[1]["data"]["Twiml"] == message_contents |
| 320 |
assert ( |
| 321 |
first_call[1]["data"]["From"] |
| 322 |
== second_call[1]["data"]["From"] |
| 323 |
== third_call[1]["data"]["From"] |
| 324 |
== "+15551233456" |
| 325 |
) |
| 326 |
assert ( |
| 327 |
first_call[1]["data"]["To"] |
| 328 |
== second_call[1]["data"]["To"] |
| 329 |
== third_call[1]["data"]["To"] |
| 330 |
== "+15559876543" |
| 331 |
) |
| 332 |
|
| 333 |
# Auth differs depending on if API Key is used |
| 334 |
assert first_call[1]["auth"] == (account_sid, auth_token) |
| 335 |
assert second_call[1]["auth"] == (apikey, auth_token) |
| 336 |
assert third_call[1]["auth"] == (account_sid, auth_token) |
| 337 |
|
| 338 |
|
| 339 |
@mock.patch("requests.post") |
| 340 |
def test_plugin_twilio_edge_cases(mock_post): |
| 341 |
"""NotifyTwilio() Edge Cases.""" |
| 342 |
|
| 343 |
# Prepare our response |
| 344 |
response = requests.Request() |
| 345 |
response.status_code = requests.codes.ok |
| 346 |
|
| 347 |
# Prepare Mock |
| 348 |
mock_post.return_value = response |
| 349 |
|
| 350 |
# Initialize some generic (but valid) tokens |
| 351 |
account_sid = "AC{}".format("b" * 32) |
| 352 |
auth_token = "{}".format("b" * 32) |
| 353 |
source = "+1 (555) 123-3456" |
| 354 |
whatsapp_source = "w:" + "+1 (555) 123-3456" |
| 355 |
|
| 356 |
# No account_sid specified |
| 357 |
with pytest.raises(AppriseImproperlyConfigured): |
| 358 |
NotifyTwilio(account_sid=None, auth_token=auth_token, source=source) |
| 359 |
|
| 360 |
# No auth_token specified |
| 361 |
with pytest.raises(AppriseImproperlyConfigured): |
| 362 |
NotifyTwilio(account_sid=account_sid, auth_token=None, source=source) |
| 363 |
|
| 364 |
# Source is bad |
| 365 |
with pytest.raises(AppriseImproperlyConfigured): |
| 366 |
NotifyTwilio(account_sid=account_sid, auth_token=auth_token, source="") |
| 367 |
|
| 368 |
# Incompatibility between mode and method |
| 369 |
with pytest.raises(AppriseImproperlyConfigured): |
| 370 |
NotifyTwilio( |
| 371 |
account_sid=account_sid, |
| 372 |
auth_token=auth_token, |
| 373 |
source=whatsapp_source, |
| 374 |
method=TwilioNotificationMethod.CALL, |
| 375 |
) |
| 376 |
|
| 377 |
# a error response |
| 378 |
response.status_code = 400 |
| 379 |
response.content = dumps( |
| 380 |
{ |
| 381 |
"code": 21211, |
| 382 |
"message": "The 'To' number +1234567 is not a valid phone number.", |
| 383 |
} |
| 384 |
) |
| 385 |
mock_post.return_value = response |
| 386 |
|
| 387 |
# Initialize our object |
| 388 |
obj = NotifyTwilio( |
| 389 |
account_sid=account_sid, auth_token=auth_token, source=source |
| 390 |
) |
| 391 |
|
| 392 |
# We will fail with the above error code |
| 393 |
assert bool(obj.notify("title", "body", "info")) is False |