| 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 |
# Disable logging for a cleaner testing output |
| 29 |
import logging |
| 30 |
from unittest import mock |
| 31 |
|
| 32 |
from helpers import AppriseURLTester |
| 33 |
import requests |
| 34 |
|
| 35 |
from apprise import Apprise, NotifyType |
| 36 |
from apprise.exception import AppriseImproperlyConfigured |
| 37 |
from apprise.plugins.smsmanager import NotifySMSManager |
| 38 |
|
| 39 |
logging.disable(logging.CRITICAL) |
| 40 |
|
| 41 |
# Our Testing URLs |
| 42 |
apprise_url_tests = ( |
| 43 |
( |
| 44 |
"smsmgr://", |
| 45 |
{ |
| 46 |
# Instantiated but no auth, so no otification can happen |
| 47 |
"instance": AppriseImproperlyConfigured, |
| 48 |
}, |
| 49 |
), |
| 50 |
( |
| 51 |
"smsmgr://:@/", |
| 52 |
{ |
| 53 |
# invalid auth |
| 54 |
"instance": AppriseImproperlyConfigured |
| 55 |
}, |
| 56 |
), |
| 57 |
( |
| 58 |
"smsmgr://{}@{}".format("b" * 10, "3" * 5), |
| 59 |
{ |
| 60 |
# invalid nubmer provided |
| 61 |
"instance": NotifySMSManager, |
| 62 |
# Expected notify() response because we have no one to notify |
| 63 |
"notify_response": False, |
| 64 |
}, |
| 65 |
), |
| 66 |
( |
| 67 |
"smsmgr://{}@123/{}/abcd/+{}".format("z" * 10, "3" * 11, "4" * 11), |
| 68 |
{ |
| 69 |
# includes a few invalid bits of info |
| 70 |
"instance": NotifySMSManager, |
| 71 |
"privacy_url": "smsmgr://z...z@33333333333/+44444444444", |
| 72 |
}, |
| 73 |
), |
| 74 |
( |
| 75 |
"smsmgr://{}@{}?batch=y".format("b" * 5, "4" * 11), |
| 76 |
{ |
| 77 |
"instance": NotifySMSManager, |
| 78 |
# Our expected url(privacy=True) startswith() response: |
| 79 |
"privacy_url": "smsmgr://b...b@44444444444", |
| 80 |
}, |
| 81 |
), |
| 82 |
# Test gateway group |
| 83 |
( |
| 84 |
"smsmgr://{}@{}?gateway=low".format("a" * 10, "1" * 11), |
| 85 |
{ |
| 86 |
"instance": NotifySMSManager, |
| 87 |
}, |
| 88 |
), |
| 89 |
( |
| 90 |
"smsmgr://{}@{}?gateway=invalid".format("a" * 10, "1" * 11), |
| 91 |
{ |
| 92 |
# invalid gatewwway |
| 93 |
"instance": AppriseImproperlyConfigured, |
| 94 |
}, |
| 95 |
), |
| 96 |
( |
| 97 |
"smsmgr://{}?key={}&from=user".format("1" * 11, "a" * 10), |
| 98 |
{ |
| 99 |
# use get args to acomplish the same thing |
| 100 |
"instance": NotifySMSManager, |
| 101 |
}, |
| 102 |
), |
| 103 |
( |
| 104 |
"smsmgr://_?to={},{}&key={}&sender={}".format( |
| 105 |
"1" * 11, "2" * 11, "b" * 10, "5" * 13 |
| 106 |
), |
| 107 |
{ |
| 108 |
# use get args to acomplish the same thing |
| 109 |
"instance": NotifySMSManager, |
| 110 |
}, |
| 111 |
), |
| 112 |
( |
| 113 |
"smsmgr://{}@{}".format("a" * 10, "1" * 11), |
| 114 |
{ |
| 115 |
"instance": NotifySMSManager, |
| 116 |
# throw a bizarre code forcing us to fail to look it up |
| 117 |
"response": False, |
| 118 |
"requests_response_code": 999, |
| 119 |
}, |
| 120 |
), |
| 121 |
( |
| 122 |
"smsmgr://{}@{}".format("a" * 10, "1" * 11), |
| 123 |
{ |
| 124 |
"instance": NotifySMSManager, |
| 125 |
# Throws a series of i/o exceptions with this flag |
| 126 |
# is set and tests that we gracefully handle them |
| 127 |
"test_requests_exceptions": True, |
| 128 |
}, |
| 129 |
), |
| 130 |
) |
| 131 |
|
| 132 |
|
| 133 |
def test_plugin_smsmgr_urls(): |
| 134 |
"""NotifyTemplate() Apprise URLs.""" |
| 135 |
|
| 136 |
# Run our general tests |
| 137 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 138 |
|
| 139 |
|
| 140 |
@mock.patch("requests.get") |
| 141 |
def test_plugin_smsmgr_edge_cases(mock_get): |
| 142 |
"""NotifySMSManager() Edge Cases.""" |
| 143 |
|
| 144 |
# Initialize some generic (but valid) tokens |
| 145 |
apikey = "my-api-key" |
| 146 |
targets = [ |
| 147 |
"+1(555) 123-1234", |
| 148 |
"1555 5555555", |
| 149 |
# A garbage entry |
| 150 |
"12", |
| 151 |
# NOw a valid one because a group was implicit |
| 152 |
"@12", |
| 153 |
] |
| 154 |
|
| 155 |
# Prepare our response |
| 156 |
response = requests.Request() |
| 157 |
response.status_code = requests.codes.ok |
| 158 |
|
| 159 |
# Prepare Mock |
| 160 |
mock_get.return_value = response |
| 161 |
|
| 162 |
# Instantiate our object |
| 163 |
obj = Apprise.instantiate( |
| 164 |
"smsmgr://{}@{}?batch=n".format(apikey, "/".join(targets)) |
| 165 |
) |
| 166 |
|
| 167 |
assert ( |
| 168 |
bool( |
| 169 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 170 |
) |
| 171 |
is True |
| 172 |
) |
| 173 |
|
| 174 |
# We know there are 2 (valid) targets |
| 175 |
assert len(obj) == 2 |
| 176 |
|
| 177 |
# Test our call count |
| 178 |
assert mock_get.call_count == 2 |
| 179 |
|
| 180 |
# Test |
| 181 |
details = mock_get.call_args_list[0] |
| 182 |
payload = details[1]["params"] |
| 183 |
assert payload["apikey"] == apikey |
| 184 |
assert payload["gateway"] == "high" |
| 185 |
assert payload["number"] == "+15551231234" |
| 186 |
assert payload["message"] == "title\r\nbody" |
| 187 |
|
| 188 |
details = mock_get.call_args_list[1] |
| 189 |
payload = details[1]["params"] |
| 190 |
assert payload["apikey"] == apikey |
| 191 |
assert payload["gateway"] == "high" |
| 192 |
assert payload["number"] == "15555555555" |
| 193 |
assert payload["message"] == "title\r\nbody" |
| 194 |
|
| 195 |
# Verify our URL looks good |
| 196 |
assert obj.url().startswith( |
| 197 |
"smsmgr://{}@{}".format( |
| 198 |
apikey, "/".join(["+15551231234", "15555555555"]) |
| 199 |
) |
| 200 |
) |
| 201 |
|
| 202 |
assert "batch=no" in obj.url() |
| 203 |
|
| 204 |
# Reset our mock object |
| 205 |
mock_get.reset_mock() |
| 206 |
|
| 207 |
# With our batch in place, our calculations are different |
| 208 |
obj = Apprise.instantiate( |
| 209 |
"smsmgr://{}@{}?batch=y".format(apikey, "/".join(targets)) |
| 210 |
) |
| 211 |
|
| 212 |
# 2 phones were loaded but counted as 1 due to batch flag |
| 213 |
assert len(obj) == 1 |
| 214 |
|
| 215 |
assert ( |
| 216 |
bool( |
| 217 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 218 |
) |
| 219 |
is True |
| 220 |
) |
| 221 |
|
| 222 |
# Test our call count (batched into 1) |
| 223 |
assert mock_get.call_count == 1 |
| 224 |
|
| 225 |
details = mock_get.call_args_list[0] |
| 226 |
payload = details[1]["params"] |
| 227 |
assert payload["apikey"] == apikey |
| 228 |
assert payload["gateway"] == "high" |
| 229 |
assert payload["number"] == "+15551231234;15555555555" |
| 230 |
assert payload["message"] == "title\r\nbody" |