| 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 loads |
| 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, NotifyType |
| 39 |
from apprise.exception import AppriseImproperlyConfigured |
| 40 |
from apprise.plugins.msg91 import NotifyMSG91 |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
# Our Testing URLs |
| 45 |
apprise_url_tests = ( |
| 46 |
( |
| 47 |
"msg91://", |
| 48 |
{ |
| 49 |
# No hostname/authkey specified |
| 50 |
"instance": AppriseImproperlyConfigured, |
| 51 |
}, |
| 52 |
), |
| 53 |
( |
| 54 |
"msg91://-", |
| 55 |
{ |
| 56 |
# Invalid AuthKey |
| 57 |
"instance": AppriseImproperlyConfigured, |
| 58 |
}, |
| 59 |
), |
| 60 |
( |
| 61 |
"msg91://{}".format("a" * 23), |
| 62 |
{ |
| 63 |
# valid AuthKey but no Template ID |
| 64 |
"instance": AppriseImproperlyConfigured, |
| 65 |
}, |
| 66 |
), |
| 67 |
( |
| 68 |
"msg91://{}@{}".format("t" * 20, "a" * 23), |
| 69 |
{ |
| 70 |
# Valid entry but no targets |
| 71 |
"instance": NotifyMSG91, |
| 72 |
# Since there are no targets specified we expect a False return on |
| 73 |
# send() |
| 74 |
"notify_response": False, |
| 75 |
}, |
| 76 |
), |
| 77 |
( |
| 78 |
"msg91://{}@{}/abcd".format("t" * 20, "a" * 23), |
| 79 |
{ |
| 80 |
# No number to notify |
| 81 |
"instance": NotifyMSG91, |
| 82 |
# Since there are no targets specified we expect a False return on |
| 83 |
# send() |
| 84 |
"notify_response": False, |
| 85 |
}, |
| 86 |
), |
| 87 |
( |
| 88 |
"msg91://{}@{}/15551232000".format("t" * 20, "a" * 23), |
| 89 |
{ |
| 90 |
# a valid message |
| 91 |
"instance": NotifyMSG91, |
| 92 |
# Our expected url(privacy=True) startswith() response: |
| 93 |
"privacy_url": "msg91://[email protected]/15551232000", |
| 94 |
}, |
| 95 |
), |
| 96 |
( |
| 97 |
"msg91://{}@{}/?to=15551232000&short_url=no".format( |
| 98 |
"t" * 20, "a" * 23 |
| 99 |
), |
| 100 |
{ |
| 101 |
# a valid message |
| 102 |
"instance": NotifyMSG91, |
| 103 |
}, |
| 104 |
), |
| 105 |
( |
| 106 |
"msg91://{}@{}/15551232000?short_url=yes".format("t" * 20, "a" * 23), |
| 107 |
{ |
| 108 |
# testing short_url |
| 109 |
"instance": NotifyMSG91, |
| 110 |
}, |
| 111 |
), |
| 112 |
( |
| 113 |
"msg91://{}@{}/15551232000".format("t" * 20, "a" * 23), |
| 114 |
{ |
| 115 |
# use get args to acomplish the same thing |
| 116 |
"instance": NotifyMSG91, |
| 117 |
}, |
| 118 |
), |
| 119 |
( |
| 120 |
"msg91://{}@{}/15551232000".format("t" * 20, "a" * 23), |
| 121 |
{ |
| 122 |
"instance": NotifyMSG91, |
| 123 |
# throw a bizarre code forcing us to fail to look it up |
| 124 |
"response": False, |
| 125 |
"requests_response_code": 999, |
| 126 |
}, |
| 127 |
), |
| 128 |
( |
| 129 |
"msg91://{}@{}/15551232000".format("t" * 20, "a" * 23), |
| 130 |
{ |
| 131 |
"instance": NotifyMSG91, |
| 132 |
# Throws a series of i/o exceptions with this flag |
| 133 |
# is set and tests that we gracefully handle them |
| 134 |
"test_requests_exceptions": True, |
| 135 |
}, |
| 136 |
), |
| 137 |
) |
| 138 |
|
| 139 |
|
| 140 |
def test_plugin_msg91_urls(): |
| 141 |
"""NotifyMSG91() Apprise URLs.""" |
| 142 |
|
| 143 |
# Run our general tests |
| 144 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 145 |
|
| 146 |
|
| 147 |
@mock.patch("requests.post") |
| 148 |
def test_plugin_msg91_edge_cases(mock_post): |
| 149 |
"""NotifyMSG91() Edge Cases.""" |
| 150 |
|
| 151 |
# Prepare our response |
| 152 |
response = requests.Request() |
| 153 |
response.status_code = requests.codes.ok |
| 154 |
|
| 155 |
# Prepare Mock |
| 156 |
mock_post.return_value = response |
| 157 |
|
| 158 |
# Initialize some generic (but valid) tokens |
| 159 |
target = "+1 (555) 123-3456" |
| 160 |
|
| 161 |
# No authkey specified |
| 162 |
with pytest.raises(AppriseImproperlyConfigured): |
| 163 |
NotifyMSG91(template="1234", authkey=None, targets=target) |
| 164 |
with pytest.raises(AppriseImproperlyConfigured): |
| 165 |
NotifyMSG91(template="1234", authkey=" ", targets=target) |
| 166 |
with pytest.raises(AppriseImproperlyConfigured): |
| 167 |
NotifyMSG91(template=" ", authkey="a" * 23, targets=target) |
| 168 |
with pytest.raises(AppriseImproperlyConfigured): |
| 169 |
NotifyMSG91(template=None, authkey="a" * 23, targets=target) |
| 170 |
|
| 171 |
|
| 172 |
@mock.patch("requests.post") |
| 173 |
def test_plugin_msg91_keywords(mock_post): |
| 174 |
"""NotifyMSG91() Templating.""" |
| 175 |
|
| 176 |
response = mock.Mock() |
| 177 |
response.content = "" |
| 178 |
response.status_code = requests.codes.ok |
| 179 |
|
| 180 |
# Prepare Mock |
| 181 |
mock_post.return_value = response |
| 182 |
|
| 183 |
target = "+1 (555) 123-3456" |
| 184 |
template = "12345" |
| 185 |
authkey = "{}".format("b" * 32) |
| 186 |
|
| 187 |
message_contents = "test" |
| 188 |
|
| 189 |
# Variation of initialization without API key |
| 190 |
obj = Apprise.instantiate( |
| 191 |
f"msg91://{template}@{authkey}/{target}?:key=value&:mobiles=ignored" |
| 192 |
) |
| 193 |
assert isinstance(obj, NotifyMSG91) |
| 194 |
assert isinstance(obj.url(), str) |
| 195 |
|
| 196 |
# Send Notification |
| 197 |
assert obj.send(body=message_contents) is True |
| 198 |
|
| 199 |
# Validate expected call parameters |
| 200 |
assert mock_post.call_count == 1 |
| 201 |
first_call = mock_post.call_args_list[0] |
| 202 |
|
| 203 |
# URL and message parameters are the same for both calls |
| 204 |
assert first_call[0][0] == "https://control.msg91.com/api/v5/flow/" |
| 205 |
response = loads(first_call[1]["data"]) |
| 206 |
assert response["template_id"] == template |
| 207 |
assert response["short_url"] == 0 |
| 208 |
assert len(response["recipients"]) == 1 |
| 209 |
# mobiles is not over-ridden as it is a special reserved token |
| 210 |
assert response["recipients"][0]["mobiles"] == "15551233456" |
| 211 |
|
| 212 |
# Our base tokens |
| 213 |
assert response["recipients"][0]["body"] == message_contents |
| 214 |
assert response["recipients"][0]["type"] == NotifyType.INFO.value |
| 215 |
assert response["recipients"][0]["key"] == "value" |
| 216 |
|
| 217 |
mock_post.reset_mock() |
| 218 |
|
| 219 |
# Play with mapping |
| 220 |
obj = Apprise.instantiate( |
| 221 |
f"msg91://{template}@{authkey}/{target}?:body&:type=cat" |
| 222 |
) |
| 223 |
assert isinstance(obj, NotifyMSG91) |
| 224 |
assert isinstance(obj.url(), str) |
| 225 |
|
| 226 |
# Send Notification |
| 227 |
assert obj.send(body=message_contents) is True |
| 228 |
|
| 229 |
# Validate expected call parameters |
| 230 |
assert mock_post.call_count == 1 |
| 231 |
first_call = mock_post.call_args_list[0] |
| 232 |
assert "NotifyType." not in first_call[1]["data"] |
| 233 |
|
| 234 |
# URL and message parameters are the same for both calls |
| 235 |
assert first_call[0][0] == "https://control.msg91.com/api/v5/flow/" |
| 236 |
response = loads(first_call[1]["data"]) |
| 237 |
assert response["template_id"] == template |
| 238 |
assert response["short_url"] == 0 |
| 239 |
assert len(response["recipients"]) == 1 |
| 240 |
assert response["recipients"][0]["mobiles"] == "15551233456" |
| 241 |
assert "body" not in response["recipients"][0] |
| 242 |
assert "type" not in response["recipients"][0] |
| 243 |
assert response["recipients"][0]["cat"] == "info" |