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