| 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 requests |
| 36 |
|
| 37 |
from apprise import Apprise, NotifyType |
| 38 |
from apprise.plugins.bulksms import NotifyBulkSMS |
| 39 |
|
| 40 |
logging.disable(logging.CRITICAL) |
| 41 |
|
| 42 |
|
| 43 |
apprise_url_tests = ( |
| 44 |
( |
| 45 |
"bulksms://", |
| 46 |
{ |
| 47 |
|
| 48 |
"instance": NotifyBulkSMS, |
| 49 |
|
| 50 |
"notify_response": False, |
| 51 |
}, |
| 52 |
), |
| 53 |
( |
| 54 |
"bulksms://:@/", |
| 55 |
{ |
| 56 |
|
| 57 |
"instance": NotifyBulkSMS, |
| 58 |
|
| 59 |
"notify_response": False, |
| 60 |
}, |
| 61 |
), |
| 62 |
( |
| 63 |
"bulksms://{}@12345678".format("a" * 10), |
| 64 |
{ |
| 65 |
|
| 66 |
"instance": NotifyBulkSMS, |
| 67 |
|
| 68 |
"notify_response": False, |
| 69 |
}, |
| 70 |
), |
| 71 |
( |
| 72 |
"bulksms://{}:{}@{}".format("a" * 10, "b" * 10, "3" * 5), |
| 73 |
{ |
| 74 |
|
| 75 |
"instance": NotifyBulkSMS, |
| 76 |
|
| 77 |
"notify_response": False, |
| 78 |
}, |
| 79 |
), |
| 80 |
( |
| 81 |
"bulksms://{}:{}@123/{}/abcd/".format("a" * 5, "b" * 10, "3" * 11), |
| 82 |
{ |
| 83 |
|
| 84 |
"instance": NotifyBulkSMS, |
| 85 |
"privacy_url": "bulksms://a...a:****@+33333333333/@abcd", |
| 86 |
}, |
| 87 |
), |
| 88 |
( |
| 89 |
"bulksms://{}:{}@{}?batch=y&unicode=n".format( |
| 90 |
"b" * 5, "c" * 10, "4" * 11 |
| 91 |
), |
| 92 |
{ |
| 93 |
"instance": NotifyBulkSMS, |
| 94 |
|
| 95 |
"privacy_url": "bulksms://b...b:****@+4444444444", |
| 96 |
}, |
| 97 |
), |
| 98 |
( |
| 99 |
"bulksms://{}:{}@123456/{}".format("a" * 10, "b" * 10, "4" * 11), |
| 100 |
{ |
| 101 |
|
| 102 |
"instance": NotifyBulkSMS, |
| 103 |
}, |
| 104 |
), |
| 105 |
( |
| 106 |
"bulksms://{}:{}@{}".format("a" * 10, "b" * 10, "5" * 11), |
| 107 |
{ |
| 108 |
|
| 109 |
|
| 110 |
"instance": NotifyBulkSMS, |
| 111 |
}, |
| 112 |
), |
| 113 |
|
| 114 |
( |
| 115 |
"bulksms://{}:{}@admin?route=premium".format("a" * 10, "b" * 10), |
| 116 |
{ |
| 117 |
"instance": NotifyBulkSMS, |
| 118 |
}, |
| 119 |
), |
| 120 |
( |
| 121 |
"bulksms://{}:{}@admin?route=invalid".format("a" * 10, "b" * 10), |
| 122 |
{ |
| 123 |
|
| 124 |
"instance": TypeError, |
| 125 |
}, |
| 126 |
), |
| 127 |
( |
| 128 |
"bulksms://_?user={}&password={}&from={}".format( |
| 129 |
"a" * 10, "b" * 10, "5" * 11 |
| 130 |
), |
| 131 |
{ |
| 132 |
|
| 133 |
"instance": NotifyBulkSMS, |
| 134 |
}, |
| 135 |
), |
| 136 |
( |
| 137 |
"bulksms://_?user={}&password={}&from={}".format( |
| 138 |
"a" * 10, "b" * 10, "5" * 3 |
| 139 |
), |
| 140 |
{ |
| 141 |
|
| 142 |
"instance": TypeError, |
| 143 |
}, |
| 144 |
), |
| 145 |
( |
| 146 |
"bulksms://_?user={}&password={}&from={}&to={}".format( |
| 147 |
"a" * 10, "b" * 10, "5" * 11, "7" * 13 |
| 148 |
), |
| 149 |
{ |
| 150 |
|
| 151 |
"instance": NotifyBulkSMS, |
| 152 |
}, |
| 153 |
), |
| 154 |
( |
| 155 |
"bulksms://{}:{}@{}".format("a" * 10, "b" * 10, "a" * 3), |
| 156 |
{ |
| 157 |
"instance": NotifyBulkSMS, |
| 158 |
|
| 159 |
"response": False, |
| 160 |
"requests_response_code": 999, |
| 161 |
}, |
| 162 |
), |
| 163 |
( |
| 164 |
"bulksms://{}:{}@{}".format("a" * 10, "b" * 10, "6" * 11), |
| 165 |
{ |
| 166 |
"instance": NotifyBulkSMS, |
| 167 |
|
| 168 |
|
| 169 |
"test_requests_exceptions": True, |
| 170 |
}, |
| 171 |
), |
| 172 |
) |
| 173 |
|
| 174 |
|
| 175 |
def test_plugin_bulksms_urls(): |
| 176 |
"""NotifyTemplate() Apprise URLs.""" |
| 177 |
|
| 178 |
|
| 179 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 180 |
|
| 181 |
|
| 182 |
@mock.patch("requests.post") |
| 183 |
def test_plugin_bulksms_edge_cases(mock_post): |
| 184 |
"""NotifyBulkSMS() Edge Cases.""" |
| 185 |
|
| 186 |
|
| 187 |
user = "abcd" |
| 188 |
pwd = "mypass123" |
| 189 |
targets = [ |
| 190 |
"+1(555) 123-1234", |
| 191 |
"1555 5555555", |
| 192 |
"group", |
| 193 |
|
| 194 |
"12", |
| 195 |
|
| 196 |
"@12", |
| 197 |
] |
| 198 |
|
| 199 |
|
| 200 |
response = requests.Request() |
| 201 |
response.status_code = requests.codes.ok |
| 202 |
|
| 203 |
|
| 204 |
mock_post.return_value = response |
| 205 |
|
| 206 |
|
| 207 |
obj = Apprise.instantiate( |
| 208 |
"bulksms://{}:{}@{}?batch=n".format(user, pwd, "/".join(targets)) |
| 209 |
) |
| 210 |
|
| 211 |
assert ( |
| 212 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 213 |
is True |
| 214 |
) |
| 215 |
|
| 216 |
|
| 217 |
assert len(obj) == 4 |
| 218 |
|
| 219 |
|
| 220 |
assert mock_post.call_count == 4 |
| 221 |
|
| 222 |
|
| 223 |
details = mock_post.call_args_list[0] |
| 224 |
payload = loads(details[1]["data"]) |
| 225 |
assert payload["to"] == "+15551231234" |
| 226 |
assert payload["body"] == "title\r\nbody" |
| 227 |
|
| 228 |
details = mock_post.call_args_list[1] |
| 229 |
payload = loads(details[1]["data"]) |
| 230 |
assert payload["to"] == "+15555555555" |
| 231 |
assert payload["body"] == "title\r\nbody" |
| 232 |
|
| 233 |
details = mock_post.call_args_list[2] |
| 234 |
payload = loads(details[1]["data"]) |
| 235 |
assert isinstance(payload["to"], dict) |
| 236 |
assert payload["to"]["name"] == "group" |
| 237 |
assert payload["body"] == "title\r\nbody" |
| 238 |
|
| 239 |
details = mock_post.call_args_list[3] |
| 240 |
payload = loads(details[1]["data"]) |
| 241 |
assert isinstance(payload["to"], dict) |
| 242 |
assert payload["to"]["name"] == "12" |
| 243 |
assert payload["body"] == "title\r\nbody" |
| 244 |
|
| 245 |
|
| 246 |
assert obj.url().startswith( |
| 247 |
"bulksms://{}:{}@{}".format( |
| 248 |
user, |
| 249 |
pwd, |
| 250 |
"/".join(["+15551231234", "+15555555555", "@group", "@12"]), |
| 251 |
) |
| 252 |
) |
| 253 |
|
| 254 |
assert "batch=no" in obj.url() |
| 255 |
|
| 256 |
|
| 257 |
obj = Apprise.instantiate( |
| 258 |
"bulksms://{}:{}@{}?batch=y".format(user, pwd, "/".join(targets)) |
| 259 |
) |
| 260 |
|
| 261 |
assert len(obj) == 3 |