| 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 pytest |
| 34 |
import requests |
| 35 |
|
| 36 |
from apprise import Apprise, NotifyType |
| 37 |
from apprise.plugins.exotel import NotifyExotel |
| 38 |
|
| 39 |
logging.disable(logging.CRITICAL) |
| 40 |
|
| 41 |
|
| 42 |
apprise_url_tests = ( |
| 43 |
( |
| 44 |
"exotel://", |
| 45 |
{ |
| 46 |
|
| 47 |
"instance": TypeError, |
| 48 |
}, |
| 49 |
), |
| 50 |
( |
| 51 |
"exotel://:@/", |
| 52 |
{ |
| 53 |
|
| 54 |
"instance": TypeError, |
| 55 |
}, |
| 56 |
), |
| 57 |
( |
| 58 |
"exotel://{}@12345678".format("a" * 32), |
| 59 |
{ |
| 60 |
|
| 61 |
"instance": TypeError, |
| 62 |
}, |
| 63 |
), |
| 64 |
( |
| 65 |
"exotel://{}:{}@_".format("a" * 32, "b" * 32), |
| 66 |
{ |
| 67 |
|
| 68 |
"instance": TypeError, |
| 69 |
}, |
| 70 |
), |
| 71 |
( |
| 72 |
"exotel://{}:{}@/%20".format("a" * 32, "b" * 32), |
| 73 |
{ |
| 74 |
|
| 75 |
"instance": TypeError, |
| 76 |
}, |
| 77 |
), |
| 78 |
( |
| 79 |
"exotel://{}:{}@{}".format("a" * 32, "b" * 32, "3" * 8), |
| 80 |
{ |
| 81 |
|
| 82 |
"instance": TypeError, |
| 83 |
}, |
| 84 |
), |
| 85 |
( |
| 86 |
"exotel://{}:{}@{}".format("a" * 32, "b" * 32, "3" * 9), |
| 87 |
{ |
| 88 |
|
| 89 |
"instance": NotifyExotel, |
| 90 |
}, |
| 91 |
), |
| 92 |
( |
| 93 |
"exotel://{}:{}@EXOTEL/{}".format("a" * 32, "b" * 32, "3" * 9), |
| 94 |
{ |
| 95 |
|
| 96 |
"instance": NotifyExotel, |
| 97 |
}, |
| 98 |
), |
| 99 |
( |
| 100 |
"exotel://{}:{}@600123/{}".format("a" * 32, "b" * 32, "3" * 9), |
| 101 |
{ |
| 102 |
|
| 103 |
"instance": NotifyExotel, |
| 104 |
}, |
| 105 |
), |
| 106 |
( |
| 107 |
"exotel://{}:{}@{}/123/{}/abcd/".format( |
| 108 |
"a" * 32, "b" * 32, "3" * 11, "9" * 15 |
| 109 |
), |
| 110 |
{ |
| 111 |
|
| 112 |
"instance": NotifyExotel, |
| 113 |
|
| 114 |
"notify_response": False, |
| 115 |
}, |
| 116 |
), |
| 117 |
( |
| 118 |
"exotel://{}:{}@12345/{}".format("a" * 32, "b" * 32, "4" * 11), |
| 119 |
{ |
| 120 |
|
| 121 |
"instance": TypeError, |
| 122 |
}, |
| 123 |
), |
| 124 |
( |
| 125 |
"exotel://{}:{}@{}".format("a" * 32, "b" * 32, "5" * 11), |
| 126 |
{ |
| 127 |
|
| 128 |
|
| 129 |
"instance": NotifyExotel, |
| 130 |
|
| 131 |
"privacy_url": "exotel://****:b...b@55555555555/", |
| 132 |
}, |
| 133 |
), |
| 134 |
( |
| 135 |
"exotel://{}:{}@{}?batch=yes".format("a" * 32, "b" * 32, "5" * 11), |
| 136 |
{ |
| 137 |
|
| 138 |
"instance": NotifyExotel, |
| 139 |
}, |
| 140 |
), |
| 141 |
( |
| 142 |
"exotel://{}:{}@{}?batch=no".format("a" * 32, "b" * 32, "5" * 11), |
| 143 |
{ |
| 144 |
|
| 145 |
"instance": NotifyExotel, |
| 146 |
}, |
| 147 |
), |
| 148 |
( |
| 149 |
"exotel://_?sid={}&token={}&from={}".format( |
| 150 |
"a" * 32, "b" * 32, "5" * 11 |
| 151 |
), |
| 152 |
{ |
| 153 |
|
| 154 |
"instance": NotifyExotel, |
| 155 |
}, |
| 156 |
), |
| 157 |
( |
| 158 |
"exotel://_?sid={}&token={}&from={}&unicode=Yes".format( |
| 159 |
"a" * 32, "b" * 32, "5" * 11 |
| 160 |
), |
| 161 |
{ |
| 162 |
|
| 163 |
"instance": NotifyExotel, |
| 164 |
}, |
| 165 |
), |
| 166 |
( |
| 167 |
"exotel://_?sid={}&token={}&from={}&unicode=no".format( |
| 168 |
"a" * 32, "b" * 32, "5" * 11 |
| 169 |
), |
| 170 |
{ |
| 171 |
|
| 172 |
"instance": NotifyExotel, |
| 173 |
}, |
| 174 |
), |
| 175 |
( |
| 176 |
"exotel://_?sid={}&token={}&from={}®ion=us".format( |
| 177 |
"a" * 32, "b" * 32, "5" * 11 |
| 178 |
), |
| 179 |
{ |
| 180 |
|
| 181 |
"instance": NotifyExotel, |
| 182 |
}, |
| 183 |
), |
| 184 |
( |
| 185 |
"exotel://_?sid={}&token={}&from={}®ion=in".format( |
| 186 |
"a" * 32, "b" * 32, "5" * 11 |
| 187 |
), |
| 188 |
{ |
| 189 |
|
| 190 |
"instance": NotifyExotel, |
| 191 |
}, |
| 192 |
), |
| 193 |
( |
| 194 |
"exotel://_?sid={}&token={}&from={}®ion=invalid".format( |
| 195 |
"a" * 32, "b" * 32, "5" * 11 |
| 196 |
), |
| 197 |
{ |
| 198 |
|
| 199 |
"instance": TypeError, |
| 200 |
}, |
| 201 |
), |
| 202 |
( |
| 203 |
"exotel://_?sid={}&token={}&from={}&priority=normal".format( |
| 204 |
"a" * 32, "b" * 32, "5" * 11 |
| 205 |
), |
| 206 |
{ |
| 207 |
|
| 208 |
"instance": NotifyExotel, |
| 209 |
}, |
| 210 |
), |
| 211 |
( |
| 212 |
"exotel://_?sid={}&token={}&from={}&priority=high".format( |
| 213 |
"a" * 32, "b" * 32, "5" * 11 |
| 214 |
), |
| 215 |
{ |
| 216 |
|
| 217 |
"instance": NotifyExotel, |
| 218 |
}, |
| 219 |
), |
| 220 |
( |
| 221 |
"exotel://_?sid={}&token={}&from={}&priority=invalid".format( |
| 222 |
"a" * 32, "b" * 32, "5" * 11 |
| 223 |
), |
| 224 |
{ |
| 225 |
|
| 226 |
"instance": TypeError, |
| 227 |
}, |
| 228 |
), |
| 229 |
( |
| 230 |
"exotel://_?sid={}&token={}&source={}".format( |
| 231 |
"a" * 32, "b" * 32, "5" * 11 |
| 232 |
), |
| 233 |
{ |
| 234 |
|
| 235 |
|
| 236 |
"instance": NotifyExotel, |
| 237 |
}, |
| 238 |
), |
| 239 |
( |
| 240 |
"exotel://_?sid={}&token={}&from={}&to={}".format( |
| 241 |
"a" * 32, "b" * 32, "5" * 11, "7" * 13 |
| 242 |
), |
| 243 |
{ |
| 244 |
|
| 245 |
"instance": NotifyExotel, |
| 246 |
}, |
| 247 |
), |
| 248 |
( |
| 249 |
"exotel://{}:{}@{}".format("a" * 32, "b" * 32, "6" * 11), |
| 250 |
{ |
| 251 |
"instance": NotifyExotel, |
| 252 |
|
| 253 |
"response": False, |
| 254 |
"requests_response_code": 999, |
| 255 |
}, |
| 256 |
), |
| 257 |
( |
| 258 |
"exotel://{}:{}@{}".format("a" * 32, "b" * 32, "6" * 11), |
| 259 |
{ |
| 260 |
"instance": NotifyExotel, |
| 261 |
|
| 262 |
|
| 263 |
"test_requests_exceptions": True, |
| 264 |
}, |
| 265 |
), |
| 266 |
) |
| 267 |
|
| 268 |
|
| 269 |
def test_plugin_exotel_urls(): |
| 270 |
"""NotifyExotel() Apprise URLs.""" |
| 271 |
|
| 272 |
assert NotifyExotel.setup_url == "https://appriseit.com/services/exotel/" |
| 273 |
|
| 274 |
|
| 275 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 276 |
|
| 277 |
|
| 278 |
@mock.patch("requests.post") |
| 279 |
def test_plugin_exotel_edge_cases(mock_post): |
| 280 |
"""NotifyExotel() Edge Cases.""" |
| 281 |
|
| 282 |
sid = "a" * 32 |
| 283 |
apikey = "api-key" |
| 284 |
token = "b" * 32 |
| 285 |
source = "LM-EXOTEL" |
| 286 |
targets = ("6" * 11, "7" * 11) |
| 287 |
|
| 288 |
response = requests.Request() |
| 289 |
response.status_code = requests.codes.ok |
| 290 |
|
| 291 |
mock_post.return_value = response |
| 292 |
|
| 293 |
with pytest.raises(TypeError): |
| 294 |
NotifyExotel(sid=sid, token=token, source=" ") |
| 295 |
|
| 296 |
with pytest.raises(TypeError): |
| 297 |
NotifyExotel(sid=sid, token=token, source=source, apikey=" ") |
| 298 |
|
| 299 |
with pytest.raises(TypeError): |
| 300 |
NotifyExotel(sid=sid, token=token, source=source, region_name=" ") |
| 301 |
|
| 302 |
with pytest.raises(TypeError): |
| 303 |
NotifyExotel(sid=sid, token=token, source=source, priority=" ") |
| 304 |
|
| 305 |
|
| 306 |
obj = NotifyExotel(sid=sid, token=token, source="5" * 11, priority="+") |
| 307 |
assert obj.priority == "high" |
| 308 |
assert obj.targets == ["5" * 11] |
| 309 |
|
| 310 |
mock_post.reset_mock() |
| 311 |
assert obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 312 |
assert mock_post.call_count == 1 |
| 313 |
payload = mock_post.call_args_list[0][1]["data"] |
| 314 |
assert payload["From"] == "5" * 11 |
| 315 |
assert payload["To"] == "5" * 11 |
| 316 |
assert payload["Priority"] == "high" |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
obj = NotifyExotel(sid=sid, token=token, source=source) |
| 321 |
assert obj.targets == [source] |
| 322 |
assert len(obj) == 1 |
| 323 |
|
| 324 |
mock_post.reset_mock() |
| 325 |
assert obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 326 |
assert mock_post.call_count == 1 |
| 327 |
payload = mock_post.call_args_list[0][1]["data"] |
| 328 |
assert payload["From"] == source |
| 329 |
assert payload["To"] == source |
| 330 |
|
| 331 |
obj = Apprise.instantiate( |
| 332 |
"exotel://{}:{}@{}/{}?apikey={}®ion=in&unicode=no" |
| 333 |
"&priority=high".format(sid, token, source, "/".join(targets), apikey) |
| 334 |
) |
| 335 |
|
| 336 |
assert isinstance(obj, NotifyExotel) |
| 337 |
assert len(obj) == len(targets) |
| 338 |
assert set(obj.targets) == set(targets) |
| 339 |
|
| 340 |
mock_post.reset_mock() |
| 341 |
assert obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 342 |
|
| 343 |
assert mock_post.call_count == len(targets) |
| 344 |
|
| 345 |
first_call = mock_post.call_args_list[0] |
| 346 |
assert first_call[0][0] == ( |
| 347 |
"https://api.in.exotel.com/v1/Accounts/{}/Sms/send".format(sid) |
| 348 |
) |
| 349 |
assert first_call[1]["auth"] == (apikey, token) |
| 350 |
|
| 351 |
first_payload = first_call[1]["data"] |
| 352 |
assert first_payload["From"] == source |
| 353 |
assert first_payload["To"] == obj.targets[0] |
| 354 |
assert first_payload["Body"] == "title\r\nbody" |
| 355 |
assert first_payload["EncodingType"] == "plain" |
| 356 |
assert first_payload["Priority"] == "high" |
| 357 |
assert "StatusCallback" not in first_payload |
| 358 |
|
| 359 |
second_call = mock_post.call_args_list[1] |
| 360 |
assert second_call[0][0] == ( |
| 361 |
"https://api.in.exotel.com/v1/Accounts/{}/Sms/send".format(sid) |
| 362 |
) |
| 363 |
assert second_call[1]["auth"] == (apikey, token) |
| 364 |
|
| 365 |
second_payload = second_call[1]["data"] |
| 366 |
assert second_payload["From"] == source |
| 367 |
assert second_payload["To"] == obj.targets[1] |
| 368 |
assert second_payload["Body"] == "title\r\nbody" |
| 369 |
assert second_payload["EncodingType"] == "plain" |
| 370 |
assert second_payload["Priority"] == "high" |
| 371 |
assert "StatusCallback" not in second_payload |
| 372 |
|
| 373 |
obj = Apprise.instantiate( |
| 374 |
"exotel://{}:{}@{}/{}?apikey={}®ion=in&unicode=no" |
| 375 |
"&priority=high&batch=yes".format( |
| 376 |
sid, token, source, "/".join(targets), apikey |
| 377 |
) |
| 378 |
) |
| 379 |
assert isinstance(obj, NotifyExotel) |
| 380 |
assert len(obj) == 1 |
| 381 |
assert set(obj.targets) == set(targets) |
| 382 |
|
| 383 |
mock_post.reset_mock() |
| 384 |
assert obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 385 |
assert mock_post.call_count == 1 |
| 386 |
|
| 387 |
first_call = mock_post.call_args_list[0] |
| 388 |
assert first_call[0][0] == ( |
| 389 |
"https://api.in.exotel.com/v1/Accounts/{}/Sms/send".format(sid) |
| 390 |
) |
| 391 |
assert first_call[1]["auth"] == (apikey, token) |
| 392 |
|
| 393 |
payload = first_call[1]["data"] |
| 394 |
assert payload["From"] == source |
| 395 |
assert payload["To"] == obj.targets |
| 396 |
assert payload["Body"] == "title\r\nbody" |
| 397 |
assert payload["EncodingType"] == "plain" |
| 398 |
assert payload["Priority"] == "high" |
| 399 |
assert "StatusCallback" not in payload |
| 400 |
assert "batch=yes" in obj.url() |
| 401 |
|
| 402 |
batch_targets = tuple("6{:010d}".format(i) for i in range(101)) |
| 403 |
batch_obj = NotifyExotel( |
| 404 |
sid=sid, |
| 405 |
token=token, |
| 406 |
source=source, |
| 407 |
targets=batch_targets, |
| 408 |
batch=True, |
| 409 |
) |
| 410 |
assert len(batch_obj) == 2 |
| 411 |
|
| 412 |
mock_post.reset_mock() |
| 413 |
assert batch_obj.notify( |
| 414 |
body="body", title="title", notify_type=NotifyType.INFO |
| 415 |
) |
| 416 |
assert mock_post.call_count == 2 |
| 417 |
|
| 418 |
first_payload = mock_post.call_args_list[0][1]["data"] |
| 419 |
assert first_payload["To"] == list(batch_targets[:100]) |
| 420 |
|
| 421 |
second_payload = mock_post.call_args_list[1][1]["data"] |
| 422 |
assert second_payload["To"] == list(batch_targets[100:]) |
| 423 |
|
| 424 |
|
| 425 |
obj_cmp = Apprise.instantiate( |
| 426 |
"exotel://{}:{}@{}/{}?region=in".format(sid, token, source, "8" * 11) |
| 427 |
) |
| 428 |
assert obj.url_id() != obj_cmp.url_id() |
| 429 |
|
| 430 |
obj_cmp = Apprise.instantiate( |
| 431 |
"exotel://{}:{}@{}/{}?apikey={}®ion=in".format( |
| 432 |
sid, token, source, "8" * 11, apikey |
| 433 |
) |
| 434 |
) |
| 435 |
assert obj.url_id() == obj_cmp.url_id() |
| 436 |
|
| 437 |
obj_cmp = Apprise.instantiate( |
| 438 |
"exotel://{}:{}@{}/{}?key={}®ion=in".format( |
| 439 |
sid, token, source, "8" * 11, apikey |
| 440 |
) |
| 441 |
) |
| 442 |
assert obj.url_id() == obj_cmp.url_id() |
| 443 |
|
| 444 |
|
| 445 |
obj_cmp = Apprise.instantiate( |
| 446 |
"exotel://{}:{}@{}/{}?apikey={}®ion=us".format( |
| 447 |
sid, token, source, "8" * 11, apikey |
| 448 |
) |
| 449 |
) |
| 450 |
assert obj.url_id() != obj_cmp.url_id() |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
obj = Apprise.instantiate(f"exotel://{sid}:{token}@{source}/abcd") |
| 455 |
assert isinstance(obj, NotifyExotel) |
| 456 |
assert len(obj) == 1 |