| 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 |
|
| 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.plugins.smsmanager import NotifySMSManager |
| 37 |
|
| 38 |
logging.disable(logging.CRITICAL) |
| 39 |
|
| 40 |
|
| 41 |
apprise_url_tests = ( |
| 42 |
( |
| 43 |
"smsmgr://", |
| 44 |
{ |
| 45 |
|
| 46 |
"instance": TypeError, |
| 47 |
}, |
| 48 |
), |
| 49 |
( |
| 50 |
"smsmgr://:@/", |
| 51 |
{ |
| 52 |
|
| 53 |
"instance": TypeError |
| 54 |
}, |
| 55 |
), |
| 56 |
( |
| 57 |
"smsmgr://{}@{}".format("b" * 10, "3" * 5), |
| 58 |
{ |
| 59 |
|
| 60 |
"instance": NotifySMSManager, |
| 61 |
|
| 62 |
"notify_response": False, |
| 63 |
}, |
| 64 |
), |
| 65 |
( |
| 66 |
"smsmgr://{}@123/{}/abcd/+{}".format("z" * 10, "3" * 11, "4" * 11), |
| 67 |
{ |
| 68 |
|
| 69 |
"instance": NotifySMSManager, |
| 70 |
"privacy_url": "smsmgr://z...z@33333333333/+44444444444", |
| 71 |
}, |
| 72 |
), |
| 73 |
( |
| 74 |
"smsmgr://{}@{}?batch=y".format("b" * 5, "4" * 11), |
| 75 |
{ |
| 76 |
"instance": NotifySMSManager, |
| 77 |
|
| 78 |
"privacy_url": "smsmgr://b...b@44444444444", |
| 79 |
}, |
| 80 |
), |
| 81 |
|
| 82 |
( |
| 83 |
"smsmgr://{}@{}?gateway=low".format("a" * 10, "1" * 11), |
| 84 |
{ |
| 85 |
"instance": NotifySMSManager, |
| 86 |
}, |
| 87 |
), |
| 88 |
( |
| 89 |
"smsmgr://{}@{}?gateway=invalid".format("a" * 10, "1" * 11), |
| 90 |
{ |
| 91 |
|
| 92 |
"instance": TypeError, |
| 93 |
}, |
| 94 |
), |
| 95 |
( |
| 96 |
"smsmgr://{}?key={}&from=user".format("1" * 11, "a" * 10), |
| 97 |
{ |
| 98 |
|
| 99 |
"instance": NotifySMSManager, |
| 100 |
}, |
| 101 |
), |
| 102 |
( |
| 103 |
"smsmgr://_?to={},{}&key={}&sender={}".format( |
| 104 |
"1" * 11, "2" * 11, "b" * 10, "5" * 13 |
| 105 |
), |
| 106 |
{ |
| 107 |
|
| 108 |
"instance": NotifySMSManager, |
| 109 |
}, |
| 110 |
), |
| 111 |
( |
| 112 |
"smsmgr://{}@{}".format("a" * 10, "1" * 11), |
| 113 |
{ |
| 114 |
"instance": NotifySMSManager, |
| 115 |
|
| 116 |
"response": False, |
| 117 |
"requests_response_code": 999, |
| 118 |
}, |
| 119 |
), |
| 120 |
( |
| 121 |
"smsmgr://{}@{}".format("a" * 10, "1" * 11), |
| 122 |
{ |
| 123 |
"instance": NotifySMSManager, |
| 124 |
|
| 125 |
|
| 126 |
"test_requests_exceptions": True, |
| 127 |
}, |
| 128 |
), |
| 129 |
) |
| 130 |
|
| 131 |
|
| 132 |
def test_plugin_smsmgr_urls(): |
| 133 |
"""NotifyTemplate() Apprise URLs.""" |
| 134 |
|
| 135 |
|
| 136 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 137 |
|
| 138 |
|
| 139 |
@mock.patch("requests.get") |
| 140 |
def test_plugin_smsmgr_edge_cases(mock_get): |
| 141 |
"""NotifySMSManager() Edge Cases.""" |
| 142 |
|
| 143 |
|
| 144 |
apikey = "my-api-key" |
| 145 |
targets = [ |
| 146 |
"+1(555) 123-1234", |
| 147 |
"1555 5555555", |
| 148 |
|
| 149 |
"12", |
| 150 |
|
| 151 |
"@12", |
| 152 |
] |
| 153 |
|
| 154 |
|
| 155 |
response = requests.Request() |
| 156 |
response.status_code = requests.codes.ok |
| 157 |
|
| 158 |
|
| 159 |
mock_get.return_value = response |
| 160 |
|
| 161 |
|
| 162 |
obj = Apprise.instantiate( |
| 163 |
"smsmgr://{}@{}?batch=n".format(apikey, "/".join(targets)) |
| 164 |
) |
| 165 |
|
| 166 |
assert ( |
| 167 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 168 |
is True |
| 169 |
) |
| 170 |
|
| 171 |
|
| 172 |
assert len(obj) == 2 |
| 173 |
|
| 174 |
|
| 175 |
assert mock_get.call_count == 2 |
| 176 |
|
| 177 |
|
| 178 |
details = mock_get.call_args_list[0] |
| 179 |
payload = details[1]["params"] |
| 180 |
assert payload["apikey"] == apikey |
| 181 |
assert payload["gateway"] == "high" |
| 182 |
assert payload["number"] == "+15551231234" |
| 183 |
assert payload["message"] == "title\r\nbody" |
| 184 |
|
| 185 |
details = mock_get.call_args_list[1] |
| 186 |
payload = details[1]["params"] |
| 187 |
assert payload["apikey"] == apikey |
| 188 |
assert payload["gateway"] == "high" |
| 189 |
assert payload["number"] == "15555555555" |
| 190 |
assert payload["message"] == "title\r\nbody" |
| 191 |
|
| 192 |
|
| 193 |
assert obj.url().startswith( |
| 194 |
"smsmgr://{}@{}".format( |
| 195 |
apikey, "/".join(["+15551231234", "15555555555"]) |
| 196 |
) |
| 197 |
) |
| 198 |
|
| 199 |
assert "batch=no" in obj.url() |
| 200 |
|
| 201 |
|
| 202 |
mock_get.reset_mock() |
| 203 |
|
| 204 |
|
| 205 |
obj = Apprise.instantiate( |
| 206 |
"smsmgr://{}@{}?batch=y".format(apikey, "/".join(targets)) |
| 207 |
) |
| 208 |
|
| 209 |
|
| 210 |
assert len(obj) == 1 |
| 211 |
|
| 212 |
assert ( |
| 213 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 214 |
is True |
| 215 |
) |
| 216 |
|
| 217 |
|
| 218 |
assert mock_get.call_count == 1 |
| 219 |
|
| 220 |
details = mock_get.call_args_list[0] |
| 221 |
payload = details[1]["params"] |
| 222 |
assert payload["apikey"] == apikey |
| 223 |
assert payload["gateway"] == "high" |
| 224 |
assert payload["number"] == "+15551231234;15555555555" |
| 225 |
assert payload["message"] == "title\r\nbody" |