| 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.voipms import NotifyVoipms |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
# Our Testing URLs |
| 45 |
apprise_url_tests = ( |
| 46 |
( |
| 47 |
"voipms://", |
| 48 |
{ |
| 49 |
# No email/password specified |
| 50 |
"instance": AppriseImproperlyConfigured, |
| 51 |
}, |
| 52 |
), |
| 53 |
( |
| 54 |
"voipms://@:", |
| 55 |
{ |
| 56 |
# Invalid url |
| 57 |
"instance": AppriseImproperlyConfigured, |
| 58 |
}, |
| 59 |
), |
| 60 |
( |
| 61 |
"voipms://{}/{}".format("[email protected]", "1" * 11), |
| 62 |
{ |
| 63 |
# No password specified |
| 64 |
"instance": AppriseImproperlyConfigured, |
| 65 |
}, |
| 66 |
), |
| 67 |
( |
| 68 |
"voipms://:{}".format("password"), |
| 69 |
{ |
| 70 |
# No email specified |
| 71 |
"instance": AppriseImproperlyConfigured, |
| 72 |
}, |
| 73 |
), |
| 74 |
( |
| 75 |
"voipms://{}:{}/{}".format("user@", "pass", "1" * 11), |
| 76 |
{ |
| 77 |
# Check valid email |
| 78 |
"instance": AppriseImproperlyConfigured, |
| 79 |
}, |
| 80 |
), |
| 81 |
( |
| 82 |
"voipms://{password}:{email}".format( |
| 83 |
email="[email protected]", password="password" |
| 84 |
), |
| 85 |
{ |
| 86 |
# No from_phone specified |
| 87 |
"instance": AppriseImproperlyConfigured, |
| 88 |
}, |
| 89 |
), |
| 90 |
# Invalid phone number test |
| 91 |
( |
| 92 |
"voipms://{password}:{email}/1613".format( |
| 93 |
email="[email protected]", password="password" |
| 94 |
), |
| 95 |
{ |
| 96 |
# Invalid phone number |
| 97 |
"instance": AppriseImproperlyConfigured, |
| 98 |
}, |
| 99 |
), |
| 100 |
# Invalid country code phone number test |
| 101 |
( |
| 102 |
"voipms://{password}:{email}/01133122446688".format( |
| 103 |
email="[email protected]", password="password" |
| 104 |
), |
| 105 |
{ |
| 106 |
# Non North American phone number |
| 107 |
"instance": AppriseImproperlyConfigured, |
| 108 |
}, |
| 109 |
), |
| 110 |
( |
| 111 |
"voipms://{password}:{email}/{from_phone}/{targets}/".format( |
| 112 |
email="[email protected]", |
| 113 |
password="password", |
| 114 |
from_phone="16134448888", |
| 115 |
targets="/".join(["26134442222"]), |
| 116 |
), |
| 117 |
{ |
| 118 |
# Invalid target phone number |
| 119 |
"instance": NotifyVoipms, |
| 120 |
"response": False, |
| 121 |
"requests_response_code": 999, |
| 122 |
}, |
| 123 |
), |
| 124 |
( |
| 125 |
"voipms://{password}:{email}/{from_phone}".format( |
| 126 |
email="[email protected]", |
| 127 |
password="password", |
| 128 |
from_phone="16138884444", |
| 129 |
), |
| 130 |
{ |
| 131 |
"instance": NotifyVoipms, |
| 132 |
# No targets specified |
| 133 |
"response": False, |
| 134 |
"requests_response_code": 999, |
| 135 |
}, |
| 136 |
), |
| 137 |
( |
| 138 |
"voipms://{password}:{email}/?from={from_phone}".format( |
| 139 |
email="[email protected]", |
| 140 |
password="password", |
| 141 |
from_phone="16138884444", |
| 142 |
), |
| 143 |
{ |
| 144 |
"instance": NotifyVoipms, |
| 145 |
# No targets specified |
| 146 |
"response": False, |
| 147 |
"requests_response_code": 999, |
| 148 |
}, |
| 149 |
), |
| 150 |
( |
| 151 |
"voipms://{password}:{email}/{from_phone}/{targets}/".format( |
| 152 |
email="[email protected]", |
| 153 |
password="password", |
| 154 |
from_phone="16138884444", |
| 155 |
targets="/".join(["16134442222"]), |
| 156 |
), |
| 157 |
{ |
| 158 |
# Valid |
| 159 |
"instance": NotifyVoipms, |
| 160 |
"response": True, |
| 161 |
"privacy_url": "voipms://p...d:[email protected]/16...4", |
| 162 |
}, |
| 163 |
), |
| 164 |
( |
| 165 |
"voipms://{password}:{email}/{from_phone}/{targets}/".format( |
| 166 |
email="[email protected]", |
| 167 |
password="password", |
| 168 |
from_phone="16138884444", |
| 169 |
targets="/".join(["16134442222", "16134443333"]), |
| 170 |
), |
| 171 |
{ |
| 172 |
# Valid multiple targets |
| 173 |
"instance": NotifyVoipms, |
| 174 |
"response": True, |
| 175 |
"privacy_url": "voipms://p...d:[email protected]/16...4", |
| 176 |
}, |
| 177 |
), |
| 178 |
( |
| 179 |
"voipms://{password}:{email}/?from={from_phone}&to={targets}".format( |
| 180 |
email="[email protected]", |
| 181 |
password="password", |
| 182 |
from_phone="16138884444", |
| 183 |
targets="16134448888", |
| 184 |
), |
| 185 |
{ |
| 186 |
# Valid |
| 187 |
"instance": NotifyVoipms, |
| 188 |
}, |
| 189 |
), |
| 190 |
( |
| 191 |
"voipms://{password}:{email}/{from_phone}/{targets}/".format( |
| 192 |
email="[email protected]", |
| 193 |
password="password", |
| 194 |
from_phone="16138884444", |
| 195 |
targets="16134442222", |
| 196 |
), |
| 197 |
{ |
| 198 |
"instance": NotifyVoipms, |
| 199 |
# Throws a series of errors |
| 200 |
"test_requests_exceptions": True, |
| 201 |
}, |
| 202 |
), |
| 203 |
) |
| 204 |
|
| 205 |
|
| 206 |
def test_plugin_voipms(): |
| 207 |
"""NotifyVoipms() Apprise URLs.""" |
| 208 |
|
| 209 |
# Run our general tests |
| 210 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 211 |
|
| 212 |
|
| 213 |
@mock.patch("requests.get") |
| 214 |
def test_plugin_voipms_edge_cases(mock_get): |
| 215 |
"""NotifyVoipms() Edge Cases.""" |
| 216 |
|
| 217 |
# Prepare our response |
| 218 |
response = requests.Request() |
| 219 |
response.status_code = requests.codes.ok |
| 220 |
|
| 221 |
# Prepare Mock |
| 222 |
mock_get.return_value = response |
| 223 |
|
| 224 |
# Initialize some generic (but valid) tokens |
| 225 |
email = "[email protected]" |
| 226 |
password = "password" |
| 227 |
source = "+1 (555) 123-3456" |
| 228 |
targets = "+1 (555) 123-9876" |
| 229 |
|
| 230 |
# No email specified |
| 231 |
with pytest.raises(AppriseImproperlyConfigured): |
| 232 |
NotifyVoipms(email=None, source=source) |
| 233 |
|
| 234 |
# a error response is returned |
| 235 |
response.status_code = 400 |
| 236 |
response.content = dumps( |
| 237 |
{ |
| 238 |
"code": 21211, |
| 239 |
"message": "Unable to process your request.", |
| 240 |
} |
| 241 |
) |
| 242 |
mock_get.return_value = response |
| 243 |
|
| 244 |
# Initialize our object |
| 245 |
obj = Apprise.instantiate( |
| 246 |
f"voipms://{password}:{email}/{source}/{targets}" |
| 247 |
) |
| 248 |
|
| 249 |
assert isinstance(obj, NotifyVoipms) |
| 250 |
|
| 251 |
# We will fail with the above error code |
| 252 |
assert bool(obj.notify("title", "body", "info")) is False |
| 253 |
|
| 254 |
|
| 255 |
@mock.patch("requests.get") |
| 256 |
def test_plugin_voipms_non_success_status(mock_get): |
| 257 |
"""NotifyVoipms() Non Success Status.""" |
| 258 |
|
| 259 |
# Prepare our response |
| 260 |
response = requests.Request() |
| 261 |
response.status_code = requests.codes.ok |
| 262 |
|
| 263 |
# Prepare Mock |
| 264 |
mock_get.return_value = response |
| 265 |
|
| 266 |
# A 200 response is returned but non-success message |
| 267 |
response.status_code = 200 |
| 268 |
response.content = dumps( |
| 269 |
{ |
| 270 |
"status": "invalid_credentials", |
| 271 |
"message": "Username or Password is incorrect", |
| 272 |
} |
| 273 |
) |
| 274 |
|
| 275 |
obj = Apprise.instantiate( |
| 276 |
"voipms://{password}:{email}/{source}/{targets}".format( |
| 277 |
email="[email protected]", |
| 278 |
password="badpassword", |
| 279 |
source="16134448888", |
| 280 |
targets="16134442222", |
| 281 |
) |
| 282 |
) |
| 283 |
|
| 284 |
assert isinstance(obj, NotifyVoipms) |
| 285 |
|
| 286 |
# We will fail with the above error code |
| 287 |
assert bool(obj.notify("title", "body", "info")) is False |
| 288 |
|
| 289 |
response.content = "{" |
| 290 |
assert obj.send("title", "body") is False |