| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
from json import loads |
| 29 |
|
| 30 |
|
| 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.plugins.msg91 import NotifyMSG91 |
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
|
| 44 |
apprise_url_tests = ( |
| 45 |
( |
| 46 |
"msg91://", |
| 47 |
{ |
| 48 |
|
| 49 |
"instance": TypeError, |
| 50 |
}, |
| 51 |
), |
| 52 |
( |
| 53 |
"msg91://-", |
| 54 |
{ |
| 55 |
|
| 56 |
"instance": TypeError, |
| 57 |
}, |
| 58 |
), |
| 59 |
( |
| 60 |
"msg91://{}".format("a" * 23), |
| 61 |
{ |
| 62 |
|
| 63 |
"instance": TypeError, |
| 64 |
}, |
| 65 |
), |
| 66 |
( |
| 67 |
"msg91://{}@{}".format("t" * 20, "a" * 23), |
| 68 |
{ |
| 69 |
|
| 70 |
"instance": NotifyMSG91, |
| 71 |
|
| 72 |
|
| 73 |
"notify_response": False, |
| 74 |
}, |
| 75 |
), |
| 76 |
( |
| 77 |
"msg91://{}@{}/abcd".format("t" * 20, "a" * 23), |
| 78 |
{ |
| 79 |
|
| 80 |
"instance": NotifyMSG91, |
| 81 |
|
| 82 |
|
| 83 |
"notify_response": False, |
| 84 |
}, |
| 85 |
), |
| 86 |
( |
| 87 |
"msg91://{}@{}/15551232000".format("t" * 20, "a" * 23), |
| 88 |
{ |
| 89 |
|
| 90 |
"instance": NotifyMSG91, |
| 91 |
|
| 92 |
"privacy_url": "msg91://[email protected]/15551232000", |
| 93 |
}, |
| 94 |
), |
| 95 |
( |
| 96 |
"msg91://{}@{}/?to=15551232000&short_url=no".format( |
| 97 |
"t" * 20, "a" * 23 |
| 98 |
), |
| 99 |
{ |
| 100 |
|
| 101 |
"instance": NotifyMSG91, |
| 102 |
}, |
| 103 |
), |
| 104 |
( |
| 105 |
"msg91://{}@{}/15551232000?short_url=yes".format("t" * 20, "a" * 23), |
| 106 |
{ |
| 107 |
|
| 108 |
"instance": NotifyMSG91, |
| 109 |
}, |
| 110 |
), |
| 111 |
( |
| 112 |
"msg91://{}@{}/15551232000".format("t" * 20, "a" * 23), |
| 113 |
{ |
| 114 |
|
| 115 |
"instance": NotifyMSG91, |
| 116 |
}, |
| 117 |
), |
| 118 |
( |
| 119 |
"msg91://{}@{}/15551232000".format("t" * 20, "a" * 23), |
| 120 |
{ |
| 121 |
"instance": NotifyMSG91, |
| 122 |
|
| 123 |
"response": False, |
| 124 |
"requests_response_code": 999, |
| 125 |
}, |
| 126 |
), |
| 127 |
( |
| 128 |
"msg91://{}@{}/15551232000".format("t" * 20, "a" * 23), |
| 129 |
{ |
| 130 |
"instance": NotifyMSG91, |
| 131 |
|
| 132 |
|
| 133 |
"test_requests_exceptions": True, |
| 134 |
}, |
| 135 |
), |
| 136 |
) |
| 137 |
|
| 138 |
|
| 139 |
def test_plugin_msg91_urls(): |
| 140 |
"""NotifyMSG91() Apprise URLs.""" |
| 141 |
|
| 142 |
|
| 143 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 144 |
|
| 145 |
|
| 146 |
@mock.patch("requests.post") |
| 147 |
def test_plugin_msg91_edge_cases(mock_post): |
| 148 |
"""NotifyMSG91() Edge Cases.""" |
| 149 |
|
| 150 |
|
| 151 |
response = requests.Request() |
| 152 |
response.status_code = requests.codes.ok |
| 153 |
|
| 154 |
|
| 155 |
mock_post.return_value = response |
| 156 |
|
| 157 |
|
| 158 |
target = "+1 (555) 123-3456" |
| 159 |
|
| 160 |
|
| 161 |
with pytest.raises(TypeError): |
| 162 |
NotifyMSG91(template="1234", authkey=None, targets=target) |
| 163 |
with pytest.raises(TypeError): |
| 164 |
NotifyMSG91(template="1234", authkey=" ", targets=target) |
| 165 |
with pytest.raises(TypeError): |
| 166 |
NotifyMSG91(template=" ", authkey="a" * 23, targets=target) |
| 167 |
with pytest.raises(TypeError): |
| 168 |
NotifyMSG91(template=None, authkey="a" * 23, targets=target) |
| 169 |
|
| 170 |
|
| 171 |
@mock.patch("requests.post") |
| 172 |
def test_plugin_msg91_keywords(mock_post): |
| 173 |
"""NotifyMSG91() Templating.""" |
| 174 |
|
| 175 |
response = mock.Mock() |
| 176 |
response.content = "" |
| 177 |
response.status_code = requests.codes.ok |
| 178 |
|
| 179 |
|
| 180 |
mock_post.return_value = response |
| 181 |
|
| 182 |
target = "+1 (555) 123-3456" |
| 183 |
template = "12345" |
| 184 |
authkey = "{}".format("b" * 32) |
| 185 |
|
| 186 |
message_contents = "test" |
| 187 |
|
| 188 |
|
| 189 |
obj = Apprise.instantiate( |
| 190 |
f"msg91://{template}@{authkey}/{target}?:key=value&:mobiles=ignored" |
| 191 |
) |
| 192 |
assert isinstance(obj, NotifyMSG91) |
| 193 |
assert isinstance(obj.url(), str) |
| 194 |
|
| 195 |
|
| 196 |
assert obj.send(body=message_contents) is True |
| 197 |
|
| 198 |
|
| 199 |
assert mock_post.call_count == 1 |
| 200 |
first_call = mock_post.call_args_list[0] |
| 201 |
|
| 202 |
|
| 203 |
assert first_call[0][0] == "https://control.msg91.com/api/v5/flow/" |
| 204 |
response = loads(first_call[1]["data"]) |
| 205 |
assert response["template_id"] == template |
| 206 |
assert response["short_url"] == 0 |
| 207 |
assert len(response["recipients"]) == 1 |
| 208 |
|
| 209 |
assert response["recipients"][0]["mobiles"] == "15551233456" |
| 210 |
|
| 211 |
|
| 212 |
assert response["recipients"][0]["body"] == message_contents |
| 213 |
assert response["recipients"][0]["type"] == NotifyType.INFO.value |
| 214 |
assert response["recipients"][0]["key"] == "value" |
| 215 |
|
| 216 |
mock_post.reset_mock() |
| 217 |
|
| 218 |
|
| 219 |
obj = Apprise.instantiate( |
| 220 |
f"msg91://{template}@{authkey}/{target}?:body&:type=cat" |
| 221 |
) |
| 222 |
assert isinstance(obj, NotifyMSG91) |
| 223 |
assert isinstance(obj.url(), str) |
| 224 |
|
| 225 |
|
| 226 |
assert obj.send(body=message_contents) is True |
| 227 |
|
| 228 |
|
| 229 |
assert mock_post.call_count == 1 |
| 230 |
first_call = mock_post.call_args_list[0] |
| 231 |
assert "NotifyType." not in first_call[1]["data"] |
| 232 |
|
| 233 |
|
| 234 |
assert first_call[0][0] == "https://control.msg91.com/api/v5/flow/" |
| 235 |
response = loads(first_call[1]["data"]) |
| 236 |
assert response["template_id"] == template |
| 237 |
assert response["short_url"] == 0 |
| 238 |
assert len(response["recipients"]) == 1 |
| 239 |
assert response["recipients"][0]["mobiles"] == "15551233456" |
| 240 |
assert "body" not in response["recipients"][0] |
| 241 |
assert "type" not in response["recipients"][0] |
| 242 |
assert response["recipients"][0]["cat"] == "info" |