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