| 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 datetime import tzinfo |
| 29 |
from inspect import cleandoc |
| 30 |
|
| 31 |
# Disable logging for a cleaner testing output |
| 32 |
import logging |
| 33 |
import os |
| 34 |
import re |
| 35 |
import sys |
| 36 |
import time |
| 37 |
from unittest import mock |
| 38 |
from urllib.parse import unquote |
| 39 |
|
| 40 |
from apprise import NotificationManager, utils |
| 41 |
from apprise.tag import AppriseTag |
| 42 |
|
| 43 |
logging.disable(logging.CRITICAL) |
| 44 |
|
| 45 |
# Ensure we don't create .pyc files for these tests |
| 46 |
sys.dont_write_bytecode = True |
| 47 |
|
| 48 |
# Grant access to our Notification Manager Singleton |
| 49 |
N_MGR = NotificationManager() |
| 50 |
|
| 51 |
|
| 52 |
def test_parse_qsd(): |
| 53 |
"utils: parse_qsd() testing" |
| 54 |
|
| 55 |
result = utils.parse.parse_qsd("a=1&b=&c&d=abcd") |
| 56 |
assert isinstance(result, dict) |
| 57 |
assert len(result) == 4 |
| 58 |
assert "qsd" in result |
| 59 |
assert "qsd+" in result |
| 60 |
assert "qsd-" in result |
| 61 |
assert "qsd:" in result |
| 62 |
|
| 63 |
assert len(result["qsd"]) == 4 |
| 64 |
assert "a" in result["qsd"] |
| 65 |
assert "b" in result["qsd"] |
| 66 |
assert "c" in result["qsd"] |
| 67 |
assert "d" in result["qsd"] |
| 68 |
|
| 69 |
assert len(result["qsd-"]) == 0 |
| 70 |
assert len(result["qsd+"]) == 0 |
| 71 |
assert len(result["qsd:"]) == 0 |
| 72 |
|
| 73 |
|
| 74 |
def test_parse_url_general(): |
| 75 |
"utils: parse_url() testing" |
| 76 |
|
| 77 |
result = utils.parse.parse_url("http://hostname") |
| 78 |
assert result["schema"] == "http" |
| 79 |
assert result["host"] == "hostname" |
| 80 |
assert result["port"] is None |
| 81 |
assert result["user"] is None |
| 82 |
assert result["password"] is None |
| 83 |
assert result["fullpath"] is None |
| 84 |
assert result["path"] is None |
| 85 |
assert result["query"] is None |
| 86 |
assert result["url"] == "http://hostname" |
| 87 |
assert result["qsd"] == {} |
| 88 |
assert result["qsd-"] == {} |
| 89 |
assert result["qsd+"] == {} |
| 90 |
assert result["qsd:"] == {} |
| 91 |
|
| 92 |
# GitHub Ticket 1234 - Unparseable Hostname |
| 93 |
result = utils.parse.parse_url("http://5t4m59hl-34343.euw.devtunnels.ms") |
| 94 |
assert result["schema"] == "http" |
| 95 |
assert result["host"] == "5t4m59hl-34343.euw.devtunnels.ms" |
| 96 |
assert result["port"] is None |
| 97 |
assert result["user"] is None |
| 98 |
assert result["password"] is None |
| 99 |
assert result["fullpath"] is None |
| 100 |
assert result["path"] is None |
| 101 |
assert result["query"] is None |
| 102 |
assert result["url"] == "http://5t4m59hl-34343.euw.devtunnels.ms" |
| 103 |
assert result["qsd"] == {} |
| 104 |
assert result["qsd-"] == {} |
| 105 |
assert result["qsd+"] == {} |
| 106 |
assert result["qsd:"] == {} |
| 107 |
|
| 108 |
result = utils.parse.parse_url("http://hostname/") |
| 109 |
assert result["schema"] == "http" |
| 110 |
assert result["host"] == "hostname" |
| 111 |
assert result["port"] is None |
| 112 |
assert result["user"] is None |
| 113 |
assert result["password"] is None |
| 114 |
assert result["fullpath"] == "/" |
| 115 |
assert result["path"] == "/" |
| 116 |
assert result["query"] is None |
| 117 |
assert result["url"] == "http://hostname/" |
| 118 |
assert result["qsd"] == {} |
| 119 |
assert result["qsd-"] == {} |
| 120 |
assert result["qsd+"] == {} |
| 121 |
assert result["qsd:"] == {} |
| 122 |
|
| 123 |
# colon after hostname without port number is no good |
| 124 |
assert utils.parse.parse_url("http://hostname:") is None |
| 125 |
|
| 126 |
# An invalid port |
| 127 |
result = utils.parse.parse_url( |
| 128 |
"http://hostname:invalid", verify_host=False, strict_port=True |
| 129 |
) |
| 130 |
assert result["schema"] == "http" |
| 131 |
assert result["host"] == "hostname" |
| 132 |
assert result["port"] == "invalid" |
| 133 |
assert result["user"] is None |
| 134 |
assert result["password"] is None |
| 135 |
assert result["fullpath"] is None |
| 136 |
assert result["path"] is None |
| 137 |
assert result["query"] is None |
| 138 |
assert result["url"] == "http://hostname:invalid" |
| 139 |
assert result["qsd"] == {} |
| 140 |
assert result["qsd-"] == {} |
| 141 |
assert result["qsd+"] == {} |
| 142 |
assert result["qsd:"] == {} |
| 143 |
|
| 144 |
# However if we don't verify the host, it is okay |
| 145 |
result = utils.parse.parse_url("http://hostname:", verify_host=False) |
| 146 |
assert result["schema"] == "http" |
| 147 |
assert result["host"] == "hostname:" |
| 148 |
assert result["port"] is None |
| 149 |
assert result["user"] is None |
| 150 |
assert result["password"] is None |
| 151 |
assert result["fullpath"] is None |
| 152 |
assert result["path"] is None |
| 153 |
assert result["query"] is None |
| 154 |
assert result["url"] == "http://hostname:" |
| 155 |
assert result["qsd"] == {} |
| 156 |
assert result["qsd-"] == {} |
| 157 |
assert result["qsd+"] == {} |
| 158 |
assert result["qsd:"] == {} |
| 159 |
|
| 160 |
# A port of Zero is not valid with strict port checking |
| 161 |
assert utils.parse.parse_url("http://hostname:0", strict_port=True) is None |
| 162 |
|
| 163 |
# Without strict port checking however, it is okay |
| 164 |
result = utils.parse.parse_url("http://hostname:0", strict_port=False) |
| 165 |
assert result["schema"] == "http" |
| 166 |
assert result["host"] == "hostname" |
| 167 |
assert result["port"] == 0 |
| 168 |
assert result["user"] is None |
| 169 |
assert result["password"] is None |
| 170 |
assert result["fullpath"] is None |
| 171 |
assert result["path"] is None |
| 172 |
assert result["query"] is None |
| 173 |
assert result["url"] == "http://hostname:0" |
| 174 |
assert result["qsd"] == {} |
| 175 |
assert result["qsd-"] == {} |
| 176 |
assert result["qsd+"] == {} |
| 177 |
assert result["qsd:"] == {} |
| 178 |
|
| 179 |
# A negative port is not valid |
| 180 |
assert ( |
| 181 |
utils.parse.parse_url("http://hostname:-92", strict_port=True) is None |
| 182 |
) |
| 183 |
result = utils.parse.parse_url( |
| 184 |
"http://hostname:-92", verify_host=False, strict_port=True |
| 185 |
) |
| 186 |
assert result["schema"] == "http" |
| 187 |
assert result["host"] == "hostname" |
| 188 |
assert result["port"] == -92 |
| 189 |
assert result["user"] is None |
| 190 |
assert result["password"] is None |
| 191 |
assert result["fullpath"] is None |
| 192 |
assert result["path"] is None |
| 193 |
assert result["query"] is None |
| 194 |
assert result["url"] == "http://hostname:-92" |
| 195 |
assert result["qsd"] == {} |
| 196 |
assert result["qsd-"] == {} |
| 197 |
assert result["qsd+"] == {} |
| 198 |
assert result["qsd:"] == {} |
| 199 |
|
| 200 |
# A port that is too large is not valid |
| 201 |
assert ( |
| 202 |
utils.parse.parse_url("http://hostname:65536", strict_port=True) |
| 203 |
is None |
| 204 |
) |
| 205 |
|
| 206 |
# This is an accetable port (the maximum) |
| 207 |
result = utils.parse.parse_url("http://hostname:65535", strict_port=True) |
| 208 |
assert result["schema"] == "http" |
| 209 |
assert result["host"] == "hostname" |
| 210 |
assert result["port"] == 65535 |
| 211 |
assert result["user"] is None |
| 212 |
assert result["password"] is None |
| 213 |
assert result["fullpath"] is None |
| 214 |
assert result["path"] is None |
| 215 |
assert result["query"] is None |
| 216 |
assert result["url"] == "http://hostname:65535" |
| 217 |
assert result["qsd"] == {} |
| 218 |
assert result["qsd-"] == {} |
| 219 |
assert result["qsd+"] == {} |
| 220 |
assert result["qsd:"] == {} |
| 221 |
|
| 222 |
# This is an accetable port (the maximum) |
| 223 |
result = utils.parse.parse_url("http://hostname:1", strict_port=True) |
| 224 |
assert result["schema"] == "http" |
| 225 |
assert result["host"] == "hostname" |
| 226 |
assert result["port"] == 1 |
| 227 |
assert result["user"] is None |
| 228 |
assert result["password"] is None |
| 229 |
assert result["fullpath"] is None |
| 230 |
assert result["path"] is None |
| 231 |
assert result["query"] is None |
| 232 |
assert result["url"] == "http://hostname:1" |
| 233 |
assert result["qsd"] == {} |
| 234 |
assert result["qsd-"] == {} |
| 235 |
assert result["qsd+"] == {} |
| 236 |
assert result["qsd:"] == {} |
| 237 |
|
| 238 |
# A port that was identfied as a string is invalid |
| 239 |
assert ( |
| 240 |
utils.parse.parse_url("http://hostname:invalid", strict_port=True) |
| 241 |
is None |
| 242 |
) |
| 243 |
result = utils.parse.parse_url( |
| 244 |
"http://hostname:invalid", verify_host=False, strict_port=True |
| 245 |
) |
| 246 |
assert result["schema"] == "http" |
| 247 |
assert result["host"] == "hostname" |
| 248 |
assert result["port"] == "invalid" |
| 249 |
assert result["user"] is None |
| 250 |
assert result["password"] is None |
| 251 |
assert result["fullpath"] is None |
| 252 |
assert result["path"] is None |
| 253 |
assert result["query"] is None |
| 254 |
assert result["url"] == "http://hostname:invalid" |
| 255 |
assert result["qsd"] == {} |
| 256 |
assert result["qsd-"] == {} |
| 257 |
assert result["qsd+"] == {} |
| 258 |
assert result["qsd:"] == {} |
| 259 |
|
| 260 |
result = utils.parse.parse_url( |
| 261 |
"http://hostname:invalid", verify_host=False, strict_port=False |
| 262 |
) |
| 263 |
assert result["schema"] == "http" |
| 264 |
assert result["host"] == "hostname:invalid" |
| 265 |
assert result["port"] is None |
| 266 |
assert result["user"] is None |
| 267 |
assert result["password"] is None |
| 268 |
assert result["fullpath"] is None |
| 269 |
assert result["path"] is None |
| 270 |
assert result["query"] is None |
| 271 |
assert result["url"] == "http://hostname:invalid" |
| 272 |
assert result["qsd"] == {} |
| 273 |
assert result["qsd-"] == {} |
| 274 |
assert result["qsd+"] == {} |
| 275 |
assert result["qsd:"] == {} |
| 276 |
|
| 277 |
result = utils.parse.parse_url( |
| 278 |
"http://hostname:invalid?key=value&-minuskey=mvalue", |
| 279 |
verify_host=False, |
| 280 |
strict_port=False, |
| 281 |
) |
| 282 |
assert result["schema"] == "http" |
| 283 |
assert result["host"] == "hostname:invalid" |
| 284 |
assert result["port"] is None |
| 285 |
assert result["user"] is None |
| 286 |
assert result["password"] is None |
| 287 |
assert result["fullpath"] is None |
| 288 |
assert result["path"] is None |
| 289 |
assert result["query"] is None |
| 290 |
assert result["url"] == "http://hostname:invalid" |
| 291 |
assert unquote(result["qsd"]["-minuskey"]) == "mvalue" |
| 292 |
assert unquote(result["qsd"]["key"]) == "value" |
| 293 |
assert unquote(result["qsd-"]["minuskey"]) == "mvalue" |
| 294 |
assert result["qsd+"] == {} |
| 295 |
assert result["qsd:"] == {} |
| 296 |
|
| 297 |
# Handling of floats |
| 298 |
assert ( |
| 299 |
utils.parse.parse_url("http://hostname:4.2", strict_port=True) is None |
| 300 |
) |
| 301 |
result = utils.parse.parse_url( |
| 302 |
"http://hostname:4.2", verify_host=False, strict_port=True |
| 303 |
) |
| 304 |
assert result["schema"] == "http" |
| 305 |
assert result["host"] == "hostname" |
| 306 |
assert result["port"] == "4.2" |
| 307 |
assert result["user"] is None |
| 308 |
assert result["password"] is None |
| 309 |
assert result["fullpath"] is None |
| 310 |
assert result["path"] is None |
| 311 |
assert result["query"] is None |
| 312 |
assert result["url"] == "http://hostname:4.2" |
| 313 |
assert result["qsd"] == {} |
| 314 |
assert result["qsd-"] == {} |
| 315 |
assert result["qsd+"] == {} |
| 316 |
assert result["qsd:"] == {} |
| 317 |
|
| 318 |
# A Port of zero is not acceptable for a regular hostname |
| 319 |
assert utils.parse.parse_url("http://hostname:0", strict_port=True) is None |
| 320 |
|
| 321 |
# No host verification (zero is an acceptable port when this is the case |
| 322 |
result = utils.parse.parse_url("http://hostname:0", verify_host=False) |
| 323 |
assert result["schema"] == "http" |
| 324 |
assert result["host"] == "hostname" |
| 325 |
assert result["port"] == 0 |
| 326 |
assert result["user"] is None |
| 327 |
assert result["password"] is None |
| 328 |
assert result["fullpath"] is None |
| 329 |
assert result["path"] is None |
| 330 |
assert result["query"] is None |
| 331 |
assert result["url"] == "http://hostname:0" |
| 332 |
assert result["qsd"] == {} |
| 333 |
assert result["qsd-"] == {} |
| 334 |
assert result["qsd+"] == {} |
| 335 |
assert result["qsd:"] == {} |
| 336 |
|
| 337 |
result = utils.parse.parse_url( |
| 338 |
"http://[2001:db8:002a:3256:adfe:05c0:0003:0006]:8080", |
| 339 |
verify_host=False, |
| 340 |
) |
| 341 |
assert result["schema"] == "http" |
| 342 |
assert result["host"] == "[2001:db8:002a:3256:adfe:05c0:0003:0006]" |
| 343 |
assert result["port"] == 8080 |
| 344 |
assert result["user"] is None |
| 345 |
assert result["password"] is None |
| 346 |
assert result["fullpath"] is None |
| 347 |
assert result["path"] is None |
| 348 |
assert result["query"] is None |
| 349 |
assert ( |
| 350 |
result["url"] == "http://[2001:db8:002a:3256:adfe:05c0:0003:0006]:8080" |
| 351 |
) |
| 352 |
assert result["qsd"] == {} |
| 353 |
assert result["qsd-"] == {} |
| 354 |
assert result["qsd+"] == {} |
| 355 |
assert result["qsd:"] == {} |
| 356 |
|
| 357 |
result = utils.parse.parse_url( |
| 358 |
"http://hostname:0", verify_host=False, strict_port=True |
| 359 |
) |
| 360 |
assert result["schema"] == "http" |
| 361 |
assert result["host"] == "hostname" |
| 362 |
assert result["port"] == 0 |
| 363 |
assert result["user"] is None |
| 364 |
assert result["password"] is None |
| 365 |
assert result["fullpath"] is None |
| 366 |
assert result["path"] is None |
| 367 |
assert result["query"] is None |
| 368 |
assert result["url"] == "http://hostname:0" |
| 369 |
assert result["qsd"] == {} |
| 370 |
assert result["qsd-"] == {} |
| 371 |
assert result["qsd+"] == {} |
| 372 |
assert result["qsd:"] == {} |
| 373 |
|
| 374 |
result = utils.parse.parse_url("http://hostname/?-KeY=Value") |
| 375 |
assert result["schema"] == "http" |
| 376 |
assert result["host"] == "hostname" |
| 377 |
assert result["port"] is None |
| 378 |
assert result["user"] is None |
| 379 |
assert result["password"] is None |
| 380 |
assert result["fullpath"] == "/" |
| 381 |
assert result["path"] == "/" |
| 382 |
assert result["query"] is None |
| 383 |
assert result["url"] == "http://hostname/" |
| 384 |
assert "-key" in result["qsd"] |
| 385 |
assert unquote(result["qsd"]["-key"]) == "Value" |
| 386 |
assert "KeY" in result["qsd-"] |
| 387 |
assert unquote(result["qsd-"]["KeY"]) == "Value" |
| 388 |
assert result["qsd+"] == {} |
| 389 |
assert result["qsd:"] == {} |
| 390 |
|
| 391 |
result = utils.parse.parse_url("http://hostname/?+KeY=Value") |
| 392 |
assert result["schema"] == "http" |
| 393 |
assert result["host"] == "hostname" |
| 394 |
assert result["port"] is None |
| 395 |
assert result["user"] is None |
| 396 |
assert result["password"] is None |
| 397 |
assert result["fullpath"] == "/" |
| 398 |
assert result["path"] == "/" |
| 399 |
assert result["query"] is None |
| 400 |
assert result["url"] == "http://hostname/" |
| 401 |
assert "+key" in result["qsd"] |
| 402 |
assert "KeY" in result["qsd+"] |
| 403 |
assert result["qsd+"]["KeY"] == "Value" |
| 404 |
assert result["qsd-"] == {} |
| 405 |
assert result["qsd:"] == {} |
| 406 |
|
| 407 |
result = utils.parse.parse_url("http://hostname/?:kEy=vALUE") |
| 408 |
assert result["schema"] == "http" |
| 409 |
assert result["host"] == "hostname" |
| 410 |
assert result["port"] is None |
| 411 |
assert result["user"] is None |
| 412 |
assert result["password"] is None |
| 413 |
assert result["fullpath"] == "/" |
| 414 |
assert result["path"] == "/" |
| 415 |
assert result["query"] is None |
| 416 |
assert result["url"] == "http://hostname/" |
| 417 |
assert ":key" in result["qsd"] |
| 418 |
assert "kEy" in result["qsd:"] |
| 419 |
assert result["qsd:"]["kEy"] == "vALUE" |
| 420 |
assert result["qsd+"] == {} |
| 421 |
assert result["qsd-"] == {} |
| 422 |
|
| 423 |
result = utils.parse.parse_url( |
| 424 |
"http://hostname/?+KeY=ValueA&-kEy=ValueB&KEY=Value%20+C&:colon=y" |
| 425 |
) |
| 426 |
assert result["schema"] == "http" |
| 427 |
assert result["host"] == "hostname" |
| 428 |
assert result["port"] is None |
| 429 |
assert result["user"] is None |
| 430 |
assert result["password"] is None |
| 431 |
assert result["fullpath"] == "/" |
| 432 |
assert result["path"] == "/" |
| 433 |
assert result["query"] is None |
| 434 |
assert result["url"] == "http://hostname/" |
| 435 |
assert "+key" in result["qsd"] |
| 436 |
assert "-key" in result["qsd"] |
| 437 |
assert ":colon" in result["qsd"] |
| 438 |
assert result["qsd:"]["colon"] == "y" |
| 439 |
assert "key" in result["qsd"] |
| 440 |
assert "KeY" in result["qsd+"] |
| 441 |
assert result["qsd+"]["KeY"] == "ValueA" |
| 442 |
assert "kEy" in result["qsd-"] |
| 443 |
assert result["qsd-"]["kEy"] == "ValueB" |
| 444 |
assert result["qsd"]["key"] == "Value +C" |
| 445 |
assert result["qsd"]["+key"] == result["qsd+"]["KeY"] |
| 446 |
assert result["qsd"]["-key"] == result["qsd-"]["kEy"] |
| 447 |
|
| 448 |
result = utils.parse.parse_url( |
| 449 |
"http://hostname/?+KeY=ValueA&-kEy=ValueB&KEY=Value%20+C&:colon=y", |
| 450 |
plus_to_space=True, |
| 451 |
) |
| 452 |
assert result["schema"] == "http" |
| 453 |
assert result["host"] == "hostname" |
| 454 |
assert result["port"] is None |
| 455 |
assert result["user"] is None |
| 456 |
assert result["password"] is None |
| 457 |
assert result["fullpath"] == "/" |
| 458 |
assert result["path"] == "/" |
| 459 |
assert result["query"] is None |
| 460 |
assert result["url"] == "http://hostname/" |
| 461 |
assert "+key" in result["qsd"] |
| 462 |
assert "-key" in result["qsd"] |
| 463 |
assert ":colon" in result["qsd"] |
| 464 |
assert result["qsd:"]["colon"] == "y" |
| 465 |
assert "key" in result["qsd"] |
| 466 |
assert "KeY" in result["qsd+"] |
| 467 |
assert result["qsd+"]["KeY"] == "ValueA" |
| 468 |
assert "kEy" in result["qsd-"] |
| 469 |
assert result["qsd-"]["kEy"] == "ValueB" |
| 470 |
assert result["qsd"]["key"] == "Value C" |
| 471 |
assert result["qsd"]["+key"] == result["qsd+"]["KeY"] |
| 472 |
assert result["qsd"]["-key"] == result["qsd-"]["kEy"] |
| 473 |
|
| 474 |
result = utils.parse.parse_url("http://hostname////") |
| 475 |
assert result["schema"] == "http" |
| 476 |
assert result["host"] == "hostname" |
| 477 |
assert result["port"] is None |
| 478 |
assert result["user"] is None |
| 479 |
assert result["password"] is None |
| 480 |
assert result["fullpath"] == "/" |
| 481 |
assert result["path"] == "/" |
| 482 |
assert result["query"] is None |
| 483 |
assert result["url"] == "http://hostname/" |
| 484 |
assert result["qsd"] == {} |
| 485 |
assert result["qsd-"] == {} |
| 486 |
assert result["qsd+"] == {} |
| 487 |
assert result["qsd:"] == {} |
| 488 |
|
| 489 |
result = utils.parse.parse_url("http://hostname:40////") |
| 490 |
assert result["schema"] == "http" |
| 491 |
assert result["host"] == "hostname" |
| 492 |
assert result["port"] == 40 |
| 493 |
assert result["user"] is None |
| 494 |
assert result["password"] is None |
| 495 |
assert result["fullpath"] == "/" |
| 496 |
assert result["path"] == "/" |
| 497 |
assert result["query"] is None |
| 498 |
assert result["url"] == "http://hostname:40/" |
| 499 |
assert result["qsd"] == {} |
| 500 |
assert result["qsd-"] == {} |
| 501 |
assert result["qsd+"] == {} |
| 502 |
assert result["qsd:"] == {} |
| 503 |
|
| 504 |
result = utils.parse.parse_url("HTTP://HoStNaMe:40/test.php") |
| 505 |
assert result["schema"] == "http" |
| 506 |
assert result["host"] == "HoStNaMe" |
| 507 |
assert result["port"] == 40 |
| 508 |
assert result["user"] is None |
| 509 |
assert result["password"] is None |
| 510 |
assert result["fullpath"] == "/test.php" |
| 511 |
assert result["path"] == "/" |
| 512 |
assert result["query"] == "test.php" |
| 513 |
assert result["url"] == "http://HoStNaMe:40/test.php" |
| 514 |
assert result["qsd"] == {} |
| 515 |
assert result["qsd-"] == {} |
| 516 |
assert result["qsd+"] == {} |
| 517 |
assert result["qsd:"] == {} |
| 518 |
|
| 519 |
result = utils.parse.parse_url("HTTPS://user@hostname/test.py") |
| 520 |
assert result["schema"] == "https" |
| 521 |
assert result["host"] == "hostname" |
| 522 |
assert result["port"] is None |
| 523 |
assert result["user"] == "user" |
| 524 |
assert result["password"] is None |
| 525 |
assert result["fullpath"] == "/test.py" |
| 526 |
assert result["path"] == "/" |
| 527 |
assert result["query"] == "test.py" |
| 528 |
assert result["url"] == "https://user@hostname/test.py" |
| 529 |
assert result["qsd"] == {} |
| 530 |
assert result["qsd-"] == {} |
| 531 |
assert result["qsd+"] == {} |
| 532 |
assert result["qsd:"] == {} |
| 533 |
|
| 534 |
result = utils.parse.parse_url(" HTTPS://///user@@@hostname///test.py ") |
| 535 |
assert result["schema"] == "https" |
| 536 |
assert result["host"] == "hostname" |
| 537 |
assert result["port"] is None |
| 538 |
assert result["user"] == "user" |
| 539 |
assert result["password"] is None |
| 540 |
assert result["fullpath"] == "/test.py" |
| 541 |
assert result["path"] == "/" |
| 542 |
assert result["query"] == "test.py" |
| 543 |
assert result["url"] == "https://user@hostname/test.py" |
| 544 |
assert result["qsd"] == {} |
| 545 |
assert result["qsd-"] == {} |
| 546 |
assert result["qsd+"] == {} |
| 547 |
assert result["qsd:"] == {} |
| 548 |
|
| 549 |
result = utils.parse.parse_url( |
| 550 |
"HTTPS://user:password@otherHost/full///path/name/", |
| 551 |
) |
| 552 |
assert result["schema"] == "https" |
| 553 |
assert result["host"] == "otherHost" |
| 554 |
assert result["port"] is None |
| 555 |
assert result["user"] == "user" |
| 556 |
assert result["password"] == "password" |
| 557 |
assert result["fullpath"] == "/full/path/name/" |
| 558 |
assert result["path"] == "/full/path/name/" |
| 559 |
assert result["query"] is None |
| 560 |
assert result["url"] == "https://user:password@otherHost/full/path/name/" |
| 561 |
assert result["qsd"] == {} |
| 562 |
assert result["qsd-"] == {} |
| 563 |
assert result["qsd+"] == {} |
| 564 |
assert result["qsd:"] == {} |
| 565 |
|
| 566 |
result = utils.parse.parse_url( |
| 567 |
"HTTPS://hostname/a/path/ending/with/slash/?key=value", |
| 568 |
) |
| 569 |
assert result["schema"] == "https" |
| 570 |
assert result["host"] == "hostname" |
| 571 |
assert result["port"] is None |
| 572 |
assert result["user"] is None |
| 573 |
assert result["password"] is None |
| 574 |
assert result["fullpath"] == "/a/path/ending/with/slash/" |
| 575 |
assert result["path"] == "/a/path/ending/with/slash/" |
| 576 |
assert result["query"] is None |
| 577 |
assert result["url"] == "https://hostname/a/path/ending/with/slash/" |
| 578 |
assert result["qsd"] == {"key": "value"} |
| 579 |
assert result["qsd-"] == {} |
| 580 |
assert result["qsd+"] == {} |
| 581 |
assert result["qsd:"] == {} |
| 582 |
|
| 583 |
# Handle garbage |
| 584 |
assert utils.parse.parse_url(None) is None |
| 585 |
|
| 586 |
result = utils.parse.parse_url( |
| 587 |
"mailto://user:password@otherHost/[email protected]" |
| 588 |
+ "[email protected]&name=Chris%20Caron&format=text" |
| 589 |
) |
| 590 |
assert result["schema"] == "mailto" |
| 591 |
assert result["host"] == "otherHost" |
| 592 |
assert result["port"] is None |
| 593 |
assert result["user"] == "user" |
| 594 |
assert result["password"] == "password" |
| 595 |
assert unquote(result["fullpath"]) == "/[email protected]" |
| 596 |
assert result["path"] == "/" |
| 597 |
assert unquote(result["query"]) == "[email protected]" |
| 598 |
assert ( |
| 599 |
unquote(result["url"]) |
| 600 |
== "mailto://user:password@otherHost/[email protected]" |
| 601 |
) |
| 602 |
assert len(result["qsd"]) == 3 |
| 603 |
assert "name" in result["qsd"] |
| 604 |
assert unquote(result["qsd"]["name"]) == "Chris Caron" |
| 605 |
assert "from" in result["qsd"] |
| 606 |
assert unquote(result["qsd"]["from"]) == "[email protected]" |
| 607 |
assert "format" in result["qsd"] |
| 608 |
assert unquote(result["qsd"]["format"]) == "text" |
| 609 |
assert result["qsd-"] == {} |
| 610 |
assert result["qsd+"] == {} |
| 611 |
assert result["qsd:"] == {} |
| 612 |
|
| 613 |
# Test Passwords with question marks ?; not supported |
| 614 |
result = utils.parse.parse_url("http://user:pass.with.?question@host") |
| 615 |
assert result is None |
| 616 |
|
| 617 |
# just hostnames |
| 618 |
result = utils.parse.parse_url("nuxref.com") |
| 619 |
assert result["schema"] == "http" |
| 620 |
assert result["host"] == "nuxref.com" |
| 621 |
assert result["port"] is None |
| 622 |
assert result["user"] is None |
| 623 |
assert result["password"] is None |
| 624 |
assert result["fullpath"] is None |
| 625 |
assert result["path"] is None |
| 626 |
assert result["query"] is None |
| 627 |
assert result["url"] == "http://nuxref.com" |
| 628 |
assert result["qsd"] == {} |
| 629 |
assert result["qsd-"] == {} |
| 630 |
assert result["qsd+"] == {} |
| 631 |
assert result["qsd:"] == {} |
| 632 |
|
| 633 |
# just host and path |
| 634 |
result = utils.parse.parse_url("invalid/host") |
| 635 |
assert result["schema"] == "http" |
| 636 |
assert result["host"] == "invalid" |
| 637 |
assert result["port"] is None |
| 638 |
assert result["user"] is None |
| 639 |
assert result["password"] is None |
| 640 |
assert result["fullpath"] == "/host" |
| 641 |
assert result["path"] == "/" |
| 642 |
assert result["query"] == "host" |
| 643 |
assert result["url"] == "http://invalid/host" |
| 644 |
assert result["qsd"] == {} |
| 645 |
assert result["qsd-"] == {} |
| 646 |
assert result["qsd+"] == {} |
| 647 |
assert result["qsd:"] == {} |
| 648 |
|
| 649 |
# just all out invalid |
| 650 |
assert utils.parse.parse_url("?") is None |
| 651 |
assert utils.parse.parse_url("/") is None |
| 652 |
|
| 653 |
# Test some illegal strings |
| 654 |
result = utils.parse.parse_url(object, verify_host=False) |
| 655 |
assert result is None |
| 656 |
result = utils.parse.parse_url(None, verify_host=False) |
| 657 |
assert result is None |
| 658 |
|
| 659 |
# Just a schema; invalid host |
| 660 |
result = utils.parse.parse_url("test://") |
| 661 |
assert result is None |
| 662 |
|
| 663 |
# Do it again without host validation |
| 664 |
result = utils.parse.parse_url("test://", verify_host=False) |
| 665 |
assert result["schema"] == "test" |
| 666 |
# It's worth noting that the hostname is an empty string and is NEVER set |
| 667 |
# to None if it wasn't specified. |
| 668 |
assert result["host"] == "" |
| 669 |
assert result["port"] is None |
| 670 |
assert result["user"] is None |
| 671 |
assert result["password"] is None |
| 672 |
assert result["fullpath"] is None |
| 673 |
assert result["path"] is None |
| 674 |
assert result["query"] is None |
| 675 |
assert result["url"] == "test://" |
| 676 |
assert result["qsd"] == {} |
| 677 |
assert result["qsd-"] == {} |
| 678 |
assert result["qsd+"] == {} |
| 679 |
assert result["qsd:"] == {} |
| 680 |
|
| 681 |
result = utils.parse.parse_url("testhostname") |
| 682 |
assert result["schema"] == "http" |
| 683 |
assert result["host"] == "testhostname" |
| 684 |
assert result["port"] is None |
| 685 |
assert result["user"] is None |
| 686 |
assert result["password"] is None |
| 687 |
assert result["fullpath"] is None |
| 688 |
assert result["path"] is None |
| 689 |
assert result["query"] is None |
| 690 |
# The default_schema kicks in here |
| 691 |
assert result["url"] == "http://testhostname" |
| 692 |
assert result["qsd"] == {} |
| 693 |
assert result["qsd-"] == {} |
| 694 |
assert result["qsd+"] == {} |
| 695 |
assert result["qsd:"] == {} |
| 696 |
|
| 697 |
result = utils.parse.parse_url("example.com", default_schema="unknown") |
| 698 |
assert result["schema"] == "unknown" |
| 699 |
assert result["host"] == "example.com" |
| 700 |
assert result["port"] is None |
| 701 |
assert result["user"] is None |
| 702 |
assert result["password"] is None |
| 703 |
assert result["fullpath"] is None |
| 704 |
assert result["path"] is None |
| 705 |
assert result["query"] is None |
| 706 |
# The default_schema kicks in here |
| 707 |
assert result["url"] == "unknown://example.com" |
| 708 |
assert result["qsd"] == {} |
| 709 |
assert result["qsd-"] == {} |
| 710 |
assert result["qsd+"] == {} |
| 711 |
assert result["qsd:"] == {} |
| 712 |
|
| 713 |
# An empty string without a hostame is still valid if verify_host is set |
| 714 |
result = utils.parse.parse_url("", verify_host=False) |
| 715 |
assert result["schema"] == "http" |
| 716 |
assert result["host"] == "" |
| 717 |
assert result["port"] is None |
| 718 |
assert result["user"] is None |
| 719 |
assert result["password"] is None |
| 720 |
assert result["fullpath"] is None |
| 721 |
assert result["path"] is None |
| 722 |
assert result["query"] is None |
| 723 |
# The default_schema kicks in here |
| 724 |
assert result["url"] == "http://" |
| 725 |
assert result["qsd"] == {} |
| 726 |
assert result["qsd-"] == {} |
| 727 |
assert result["qsd+"] == {} |
| 728 |
assert result["qsd:"] == {} |
| 729 |
|
| 730 |
# A messed up URL |
| 731 |
result = utils.parse.parse_url("test://:@/", verify_host=False) |
| 732 |
assert result["schema"] == "test" |
| 733 |
assert result["host"] == "" |
| 734 |
assert result["port"] is None |
| 735 |
assert result["user"] == "" |
| 736 |
assert result["password"] == "" |
| 737 |
assert result["fullpath"] == "/" |
| 738 |
assert result["path"] == "/" |
| 739 |
assert result["query"] is None |
| 740 |
assert result["url"] == "test://:@/" |
| 741 |
assert result["qsd"] == {} |
| 742 |
assert result["qsd-"] == {} |
| 743 |
assert result["qsd+"] == {} |
| 744 |
assert result["qsd:"] == {} |
| 745 |
|
| 746 |
result = utils.parse.parse_url( |
| 747 |
"crazy://:@//_/@^&/jack.json", verify_host=False |
| 748 |
) |
| 749 |
assert result["schema"] == "crazy" |
| 750 |
assert result["host"] == "" |
| 751 |
assert result["port"] is None |
| 752 |
assert result["user"] == "" |
| 753 |
assert result["password"] == "" |
| 754 |
assert unquote(result["fullpath"]) == "/_/@^&/jack.json" |
| 755 |
assert unquote(result["path"]) == "/_/@^&/" |
| 756 |
assert result["query"] == "jack.json" |
| 757 |
assert unquote(result["url"]) == "crazy://:@/_/@^&/jack.json" |
| 758 |
assert result["qsd"] == {} |
| 759 |
assert result["qsd-"] == {} |
| 760 |
assert result["qsd+"] == {} |
| 761 |
assert result["qsd:"] == {} |
| 762 |
|
| 763 |
# Sanitizing |
| 764 |
result = utils.parse.parse_url( |
| 765 |
"hTTp://hostname/?+KeY=ValueA&-kEy=ValueB&KEY=Value%20+C&:cOlON=YeS", |
| 766 |
sanitize=False, |
| 767 |
) |
| 768 |
|
| 769 |
assert len(result["qsd-"]) == 1 |
| 770 |
assert len(result["qsd+"]) == 1 |
| 771 |
assert len(result["qsd"]) == 4 |
| 772 |
assert len(result["qsd:"]) == 1 |
| 773 |
|
| 774 |
assert result["schema"] == "http" |
| 775 |
assert result["host"] == "hostname" |
| 776 |
assert result["port"] is None |
| 777 |
assert result["user"] is None |
| 778 |
assert result["password"] is None |
| 779 |
assert result["fullpath"] == "/" |
| 780 |
assert result["path"] == "/" |
| 781 |
assert result["query"] is None |
| 782 |
assert result["url"] == "http://hostname/" |
| 783 |
assert "+KeY" in result["qsd"] |
| 784 |
assert "-kEy" in result["qsd"] |
| 785 |
assert ":cOlON" in result["qsd"] |
| 786 |
assert result["qsd:"]["cOlON"] == "YeS" |
| 787 |
assert "key" not in result["qsd"] |
| 788 |
assert "KeY" in result["qsd+"] |
| 789 |
assert result["qsd+"]["KeY"] == "ValueA" |
| 790 |
assert "kEy" in result["qsd-"] |
| 791 |
assert result["qsd-"]["kEy"] == "ValueB" |
| 792 |
assert result["qsd"]["KEY"] == "Value +C" |
| 793 |
assert result["qsd"]["+KeY"] == result["qsd+"]["KeY"] |
| 794 |
assert result["qsd"]["-kEy"] == result["qsd-"]["kEy"] |
| 795 |
|
| 796 |
# Testing Defect 1264 - whitespaces in url |
| 797 |
result = utils.parse.parse_url( |
| 798 |
"posts://example.com/my endpoint?-token=ab cdefg" |
| 799 |
) |
| 800 |
|
| 801 |
assert len(result["qsd-"]) == 1 |
| 802 |
assert len(result["qsd+"]) == 0 |
| 803 |
assert len(result["qsd"]) == 1 |
| 804 |
assert len(result["qsd:"]) == 0 |
| 805 |
|
| 806 |
assert result["schema"] == "posts" |
| 807 |
assert result["host"] == "example.com" |
| 808 |
assert result["port"] is None |
| 809 |
assert result["user"] is None |
| 810 |
assert result["password"] is None |
| 811 |
assert result["fullpath"] == "/my%20endpoint" |
| 812 |
assert result["path"] == "/" |
| 813 |
assert result["query"] == "my%20endpoint" |
| 814 |
assert result["url"] == "posts://example.com/my%20endpoint" |
| 815 |
assert "-token" in result["qsd"] |
| 816 |
assert result["qsd-"]["token"] == "ab cdefg" |
| 817 |
|
| 818 |
# Testing Defect 1672 - reserved characters in URL paths |
| 819 |
url = "https://cdn.jsdelivr.net/gh/Cleanuparr/Cleanuparr@main/Logo/256.png" |
| 820 |
result = utils.parse.parse_url(url) |
| 821 |
assert result["fullpath"] == "/gh/Cleanuparr/Cleanuparr@main/Logo/256.png" |
| 822 |
assert result["url"] == url |
| 823 |
|
| 824 |
# RFC 3986 path characters must survive a parse_url() round trip because |
| 825 |
# some services distinguish literal characters from encoded ones. |
| 826 |
url = "https://example.com/a:b@c!d$e&f'g(h)i*j+k=n/file.txt" |
| 827 |
result = utils.parse.parse_url(url) |
| 828 |
assert result["fullpath"] == "/a:b@c!d$e&f'g(h)i*j+k=n/file.txt" |
| 829 |
assert result["url"] == url |
| 830 |
|
| 831 |
# Keep commas and semicolons encoded because Apprise uses them as path/list |
| 832 |
# delimiters. Otherwise path data such as a Home Assistant target list |
| 833 |
# could become extra segments. Related regressions have dedicated plugin |
| 834 |
# tests. |
| 835 |
result = utils.parse.parse_url("https://example.com/a,b;c/file.txt") |
| 836 |
assert result["fullpath"] == "/a%2Cb%3Bc/file.txt" |
| 837 |
|
| 838 |
# Characters outside an RFC 3986 path segment must remain encoded when the |
| 839 |
# safe list is expanded. |
| 840 |
result = utils.parse.parse_url("https://example.com/a b/c[1]#/file.txt") |
| 841 |
assert "%20" in result["fullpath"] |
| 842 |
assert "[" not in result["fullpath"] |
| 843 |
assert "]" not in result["fullpath"] |
| 844 |
|
| 845 |
|
| 846 |
def test_parse_url_markdown_content(): |
| 847 |
""" |
| 848 |
utils: parse_url() with unmatched-bracket content (GH#1693) |
| 849 |
""" |
| 850 |
# Content whose host portion carries an unmatched '[' (for example from |
| 851 |
# Markdown link syntax) makes the underlying urlparse() raise |
| 852 |
# "Invalid IPv6 URL". parse_url() documents that it returns None when |
| 853 |
# content cannot be extracted, so that ValueError must not propagate. |
| 854 |
assert ( |
| 855 |
utils.parse.parse_url("**bold** and [link](https://example.com)") |
| 856 |
is None |
| 857 |
) |
| 858 |
assert utils.parse.parse_url("text with [unclosed bracket") is None |
| 859 |
|
| 860 |
|
| 861 |
def test_parse_url_path_no_redos(): |
| 862 |
"utils: parse_url() does not regress into slow/backtracking behavior" |
| 863 |
|
| 864 |
# Escaping uses linear-time table lookups. This guards against a future |
| 865 |
# regex-based implementation backtracking on untrusted paths. |
| 866 |
segment = "%40%3A%21%24%26%27%28%29%2A%2B%2C%3B%3D-a" * 5000 |
| 867 |
url = f"https://example.com/{segment}/file.txt?foo=bar" |
| 868 |
|
| 869 |
start = time.time() |
| 870 |
result = utils.parse.parse_url(url) |
| 871 |
elapsed = time.time() - start |
| 872 |
|
| 873 |
assert result is not None |
| 874 |
# The generous limit allows slower CI while catching nonlinear behavior. |
| 875 |
assert elapsed < 5.0 |
| 876 |
|
| 877 |
|
| 878 |
def test_parse_url_simple(): |
| 879 |
"utils: parse_url() testing" |
| 880 |
|
| 881 |
result = utils.parse.parse_url("http://hostname", simple=True) |
| 882 |
assert len(result) == 3 |
| 883 |
assert result["schema"] == "http" |
| 884 |
assert result["host"] == "hostname" |
| 885 |
assert result["url"] == "http://hostname" |
| 886 |
|
| 887 |
result = utils.parse.parse_url("http://hostname/", simple=True) |
| 888 |
assert len(result) == 5 |
| 889 |
assert result["schema"] == "http" |
| 890 |
assert result["host"] == "hostname" |
| 891 |
assert result["fullpath"] == "/" |
| 892 |
assert result["path"] == "/" |
| 893 |
assert result["url"] == "http://hostname/" |
| 894 |
|
| 895 |
# colon after hostname without port number is no good |
| 896 |
assert utils.parse.parse_url("http://hostname:", simple=True) is None |
| 897 |
|
| 898 |
# An invalid port |
| 899 |
result = utils.parse.parse_url( |
| 900 |
"http://hostname:invalid", |
| 901 |
verify_host=False, |
| 902 |
strict_port=True, |
| 903 |
simple=True, |
| 904 |
) |
| 905 |
assert len(result) == 4 |
| 906 |
assert result["schema"] == "http" |
| 907 |
assert result["host"] == "hostname" |
| 908 |
assert result["port"] == "invalid" |
| 909 |
assert result["url"] == "http://hostname:invalid" |
| 910 |
|
| 911 |
# However if we don't verify the host, it is okay |
| 912 |
result = utils.parse.parse_url( |
| 913 |
"http://hostname:", verify_host=False, simple=True |
| 914 |
) |
| 915 |
assert len(result) == 3 |
| 916 |
assert result["schema"] == "http" |
| 917 |
assert result["host"] == "hostname:" |
| 918 |
assert result["url"] == "http://hostname:" |
| 919 |
|
| 920 |
# A port of Zero is not valid with strict port checking |
| 921 |
assert ( |
| 922 |
utils.parse.parse_url( |
| 923 |
"http://hostname:0", strict_port=True, simple=True |
| 924 |
) |
| 925 |
is None |
| 926 |
) |
| 927 |
|
| 928 |
# Without strict port checking however, it is okay |
| 929 |
result = utils.parse.parse_url( |
| 930 |
"http://hostname:0", strict_port=False, simple=True |
| 931 |
) |
| 932 |
assert len(result) == 4 |
| 933 |
assert result["schema"] == "http" |
| 934 |
assert result["port"] == 0 |
| 935 |
assert result["host"] == "hostname" |
| 936 |
assert result["url"] == "http://hostname:0" |
| 937 |
|
| 938 |
# A negative port is not valid |
| 939 |
assert ( |
| 940 |
utils.parse.parse_url( |
| 941 |
"http://hostname:-92", strict_port=True, simple=True |
| 942 |
) |
| 943 |
is None |
| 944 |
) |
| 945 |
result = utils.parse.parse_url( |
| 946 |
"http://hostname:-92", verify_host=False, strict_port=True, simple=True |
| 947 |
) |
| 948 |
assert len(result) == 4 |
| 949 |
assert result["schema"] == "http" |
| 950 |
assert result["host"] == "hostname" |
| 951 |
assert result["port"] == -92 |
| 952 |
assert result["url"] == "http://hostname:-92" |
| 953 |
|
| 954 |
# A port that is too large is not valid |
| 955 |
assert ( |
| 956 |
utils.parse.parse_url( |
| 957 |
"http://hostname:65536", strict_port=True, simple=True |
| 958 |
) |
| 959 |
is None |
| 960 |
) |
| 961 |
|
| 962 |
# This is an accetable port (the maximum) |
| 963 |
result = utils.parse.parse_url( |
| 964 |
"http://hostname:65535", strict_port=True, simple=True |
| 965 |
) |
| 966 |
assert len(result) == 4 |
| 967 |
assert result["schema"] == "http" |
| 968 |
assert result["host"] == "hostname" |
| 969 |
assert result["port"] == 65535 |
| 970 |
assert result["url"] == "http://hostname:65535" |
| 971 |
|
| 972 |
# This is an accetable port (the maximum) |
| 973 |
result = utils.parse.parse_url( |
| 974 |
"http://hostname:1", strict_port=True, simple=True |
| 975 |
) |
| 976 |
assert len(result) == 4 |
| 977 |
assert result["schema"] == "http" |
| 978 |
assert result["host"] == "hostname" |
| 979 |
assert result["port"] == 1 |
| 980 |
assert result["url"] == "http://hostname:1" |
| 981 |
|
| 982 |
# A port that was identfied as a string is invalid |
| 983 |
assert ( |
| 984 |
utils.parse.parse_url( |
| 985 |
"http://hostname:invalid", strict_port=True, simple=True |
| 986 |
) |
| 987 |
is None |
| 988 |
) |
| 989 |
result = utils.parse.parse_url( |
| 990 |
"http://hostname:invalid", |
| 991 |
verify_host=False, |
| 992 |
strict_port=True, |
| 993 |
simple=True, |
| 994 |
) |
| 995 |
assert len(result) == 4 |
| 996 |
assert result["schema"] == "http" |
| 997 |
assert result["host"] == "hostname" |
| 998 |
assert result["port"] == "invalid" |
| 999 |
assert result["url"] == "http://hostname:invalid" |
| 1000 |
|
| 1001 |
result = utils.parse.parse_url( |
| 1002 |
"http://hostname:invalid", |
| 1003 |
verify_host=False, |
| 1004 |
strict_port=False, |
| 1005 |
simple=True, |
| 1006 |
) |
| 1007 |
assert len(result) == 3 |
| 1008 |
assert result["schema"] == "http" |
| 1009 |
assert result["host"] == "hostname:invalid" |
| 1010 |
assert result["url"] == "http://hostname:invalid" |
| 1011 |
|
| 1012 |
result = utils.parse.parse_url( |
| 1013 |
"http://hostname:invalid?key=value&-minuskey=mvalue", |
| 1014 |
verify_host=False, |
| 1015 |
strict_port=False, |
| 1016 |
simple=True, |
| 1017 |
) |
| 1018 |
assert len(result) == 4 |
| 1019 |
assert result["schema"] == "http" |
| 1020 |
assert result["host"] == "hostname:invalid" |
| 1021 |
assert result["url"] == "http://hostname:invalid" |
| 1022 |
assert isinstance(result["qsd"], dict) |
| 1023 |
assert len(result["qsd"]) == 2 |
| 1024 |
assert unquote(result["qsd"]["-minuskey"]) == "mvalue" |
| 1025 |
assert unquote(result["qsd"]["key"]) == "value" |
| 1026 |
|
| 1027 |
# Handling of floats |
| 1028 |
assert ( |
| 1029 |
utils.parse.parse_url( |
| 1030 |
"http://hostname:4.2", strict_port=True, simple=True |
| 1031 |
) |
| 1032 |
is None |
| 1033 |
) |
| 1034 |
result = utils.parse.parse_url( |
| 1035 |
"http://hostname:4.2", verify_host=False, strict_port=True, simple=True |
| 1036 |
) |
| 1037 |
assert len(result) == 4 |
| 1038 |
assert result["schema"] == "http" |
| 1039 |
assert result["host"] == "hostname" |
| 1040 |
assert result["port"] == "4.2" |
| 1041 |
assert result["url"] == "http://hostname:4.2" |
| 1042 |
|
| 1043 |
# A Port of zero is not acceptable for a regular hostname |
| 1044 |
assert ( |
| 1045 |
utils.parse.parse_url( |
| 1046 |
"http://hostname:0", strict_port=True, simple=True |
| 1047 |
) |
| 1048 |
is None |
| 1049 |
) |
| 1050 |
|
| 1051 |
# No host verification (zero is an acceptable port when this is the case |
| 1052 |
result = utils.parse.parse_url( |
| 1053 |
"http://hostname:0", verify_host=False, simple=True |
| 1054 |
) |
| 1055 |
assert len(result) == 4 |
| 1056 |
assert result["schema"] == "http" |
| 1057 |
assert result["host"] == "hostname" |
| 1058 |
assert result["port"] == 0 |
| 1059 |
assert result["url"] == "http://hostname:0" |
| 1060 |
|
| 1061 |
result = utils.parse.parse_url( |
| 1062 |
"http://[2001:db8:002a:3256:adfe:05c0:0003:0006]:8080", |
| 1063 |
verify_host=False, |
| 1064 |
simple=True, |
| 1065 |
) |
| 1066 |
assert len(result) == 4 |
| 1067 |
assert result["schema"] == "http" |
| 1068 |
assert result["host"] == "[2001:db8:002a:3256:adfe:05c0:0003:0006]" |
| 1069 |
assert result["port"] == 8080 |
| 1070 |
assert ( |
| 1071 |
result["url"] == "http://[2001:db8:002a:3256:adfe:05c0:0003:0006]:8080" |
| 1072 |
) |
| 1073 |
|
| 1074 |
result = utils.parse.parse_url( |
| 1075 |
"http://hostname:0", verify_host=False, strict_port=True, simple=True |
| 1076 |
) |
| 1077 |
assert len(result) == 4 |
| 1078 |
assert result["schema"] == "http" |
| 1079 |
assert result["host"] == "hostname" |
| 1080 |
assert result["port"] == 0 |
| 1081 |
assert result["url"] == "http://hostname:0" |
| 1082 |
|
| 1083 |
result = utils.parse.parse_url("http://hostname/?-KeY=Value", simple=True) |
| 1084 |
assert len(result) == 6 |
| 1085 |
assert result["schema"] == "http" |
| 1086 |
assert result["host"] == "hostname" |
| 1087 |
assert result["fullpath"] == "/" |
| 1088 |
assert result["path"] == "/" |
| 1089 |
assert result["url"] == "http://hostname/" |
| 1090 |
assert "-key" in result["qsd"] |
| 1091 |
assert unquote(result["qsd"]["-key"]) == "Value" |
| 1092 |
|
| 1093 |
result = utils.parse.parse_url("http://hostname/?+KeY=Value", simple=True) |
| 1094 |
assert len(result) == 6 |
| 1095 |
assert result["schema"] == "http" |
| 1096 |
assert result["host"] == "hostname" |
| 1097 |
assert result["fullpath"] == "/" |
| 1098 |
assert result["path"] == "/" |
| 1099 |
assert result["url"] == "http://hostname/" |
| 1100 |
assert "+key" in result["qsd"] |
| 1101 |
assert result["qsd"]["+key"] == "Value" |
| 1102 |
|
| 1103 |
result = utils.parse.parse_url("http://hostname/?:kEy=vALUE", simple=True) |
| 1104 |
assert len(result) == 6 |
| 1105 |
assert result["schema"] == "http" |
| 1106 |
assert result["host"] == "hostname" |
| 1107 |
assert result["fullpath"] == "/" |
| 1108 |
assert result["path"] == "/" |
| 1109 |
assert result["url"] == "http://hostname/" |
| 1110 |
assert ":key" in result["qsd"] |
| 1111 |
assert result["qsd"][":key"] == "vALUE" |
| 1112 |
|
| 1113 |
result = utils.parse.parse_url( |
| 1114 |
"http://hostname/?+KeY=ValueA&-kEy=ValueB&KEY=Value%20+C&:colon=y", |
| 1115 |
simple=True, |
| 1116 |
) |
| 1117 |
assert len(result) == 6 |
| 1118 |
assert result["schema"] == "http" |
| 1119 |
assert result["host"] == "hostname" |
| 1120 |
assert result["fullpath"] == "/" |
| 1121 |
assert result["path"] == "/" |
| 1122 |
assert result["url"] == "http://hostname/" |
| 1123 |
assert "+key" in result["qsd"] |
| 1124 |
assert "-key" in result["qsd"] |
| 1125 |
assert ":colon" in result["qsd"] |
| 1126 |
assert result["qsd"][":colon"] == "y" |
| 1127 |
assert result["qsd"]["key"] == "Value +C" |
| 1128 |
assert result["qsd"]["+key"] == "ValueA" |
| 1129 |
assert result["qsd"]["-key"] == "ValueB" |
| 1130 |
|
| 1131 |
result = utils.parse.parse_url("http://hostname////", simple=True) |
| 1132 |
assert len(result) == 5 |
| 1133 |
assert result["schema"] == "http" |
| 1134 |
assert result["host"] == "hostname" |
| 1135 |
assert result["fullpath"] == "/" |
| 1136 |
assert result["path"] == "/" |
| 1137 |
assert result["url"] == "http://hostname/" |
| 1138 |
|
| 1139 |
result = utils.parse.parse_url("http://hostname:40////", simple=True) |
| 1140 |
assert len(result) == 6 |
| 1141 |
assert result["schema"] == "http" |
| 1142 |
assert result["host"] == "hostname" |
| 1143 |
assert result["port"] == 40 |
| 1144 |
assert result["fullpath"] == "/" |
| 1145 |
assert result["path"] == "/" |
| 1146 |
assert result["url"] == "http://hostname:40/" |
| 1147 |
|
| 1148 |
result = utils.parse.parse_url("HTTP://HoStNaMe:40/test.php", simple=True) |
| 1149 |
assert len(result) == 7 |
| 1150 |
assert result["schema"] == "http" |
| 1151 |
assert result["host"] == "HoStNaMe" |
| 1152 |
assert result["port"] == 40 |
| 1153 |
assert result["fullpath"] == "/test.php" |
| 1154 |
assert result["path"] == "/" |
| 1155 |
assert result["query"] == "test.php" |
| 1156 |
assert result["url"] == "http://HoStNaMe:40/test.php" |
| 1157 |
|
| 1158 |
result = utils.parse.parse_url( |
| 1159 |
"HTTPS://user@hostname/test.py", simple=True |
| 1160 |
) |
| 1161 |
assert len(result) == 7 |
| 1162 |
assert result["schema"] == "https" |
| 1163 |
assert result["host"] == "hostname" |
| 1164 |
assert result["user"] == "user" |
| 1165 |
assert result["fullpath"] == "/test.py" |
| 1166 |
assert result["path"] == "/" |
| 1167 |
assert result["query"] == "test.py" |
| 1168 |
assert result["url"] == "https://user@hostname/test.py" |
| 1169 |
|
| 1170 |
result = utils.parse.parse_url( |
| 1171 |
" HTTPS://///user@@@hostname///test.py ", simple=True |
| 1172 |
) |
| 1173 |
assert len(result) == 7 |
| 1174 |
assert result["schema"] == "https" |
| 1175 |
assert result["host"] == "hostname" |
| 1176 |
assert result["user"] == "user" |
| 1177 |
assert result["fullpath"] == "/test.py" |
| 1178 |
assert result["path"] == "/" |
| 1179 |
assert result["query"] == "test.py" |
| 1180 |
assert result["url"] == "https://user@hostname/test.py" |
| 1181 |
|
| 1182 |
result = utils.parse.parse_url( |
| 1183 |
"HTTPS://user:password@otherHost/full///path/name/", |
| 1184 |
simple=True, |
| 1185 |
) |
| 1186 |
assert len(result) == 7 |
| 1187 |
assert result["schema"] == "https" |
| 1188 |
assert result["host"] == "otherHost" |
| 1189 |
assert result["user"] == "user" |
| 1190 |
assert result["password"] == "password" |
| 1191 |
assert result["fullpath"] == "/full/path/name/" |
| 1192 |
assert result["path"] == "/full/path/name/" |
| 1193 |
assert result["url"] == "https://user:password@otherHost/full/path/name/" |
| 1194 |
|
| 1195 |
# Handle garbage |
| 1196 |
assert utils.parse.parse_url(None) is None |
| 1197 |
|
| 1198 |
result = utils.parse.parse_url( |
| 1199 |
"mailto://user:password@otherHost/[email protected]" |
| 1200 |
+ "[email protected]&name=Chris%20Caron&format=text", |
| 1201 |
simple=True, |
| 1202 |
) |
| 1203 |
assert len(result) == 9 |
| 1204 |
assert result["schema"] == "mailto" |
| 1205 |
assert result["host"] == "otherHost" |
| 1206 |
assert result["user"] == "user" |
| 1207 |
assert result["password"] == "password" |
| 1208 |
assert unquote(result["fullpath"]) == "/[email protected]" |
| 1209 |
assert result["path"] == "/" |
| 1210 |
assert unquote(result["query"]) == "[email protected]" |
| 1211 |
assert ( |
| 1212 |
unquote(result["url"]) |
| 1213 |
== "mailto://user:password@otherHost/[email protected]" |
| 1214 |
) |
| 1215 |
assert len(result["qsd"]) == 3 |
| 1216 |
assert "name" in result["qsd"] |
| 1217 |
assert unquote(result["qsd"]["name"]) == "Chris Caron" |
| 1218 |
assert "from" in result["qsd"] |
| 1219 |
assert unquote(result["qsd"]["from"]) == "[email protected]" |
| 1220 |
assert "format" in result["qsd"] |
| 1221 |
assert unquote(result["qsd"]["format"]) == "text" |
| 1222 |
|
| 1223 |
# Test Passwords with question marks ?; not supported |
| 1224 |
result = utils.parse.parse_url( |
| 1225 |
"http://user:pass.with.?question@host", simple=True |
| 1226 |
) |
| 1227 |
assert result is None |
| 1228 |
|
| 1229 |
# just hostnames |
| 1230 |
result = utils.parse.parse_url( |
| 1231 |
"nuxref.com", |
| 1232 |
simple=True, |
| 1233 |
) |
| 1234 |
assert len(result) == 3 |
| 1235 |
assert result["schema"] == "http" |
| 1236 |
assert result["host"] == "nuxref.com" |
| 1237 |
assert result["url"] == "http://nuxref.com" |
| 1238 |
|
| 1239 |
# just host and path |
| 1240 |
result = utils.parse.parse_url("invalid/host", simple=True) |
| 1241 |
assert len(result) == 6 |
| 1242 |
assert result["schema"] == "http" |
| 1243 |
assert result["host"] == "invalid" |
| 1244 |
assert result["fullpath"] == "/host" |
| 1245 |
assert result["path"] == "/" |
| 1246 |
assert result["query"] == "host" |
| 1247 |
assert result["url"] == "http://invalid/host" |
| 1248 |
|
| 1249 |
# just all out invalid |
| 1250 |
assert utils.parse.parse_url("?", simple=True) is None |
| 1251 |
assert utils.parse.parse_url("/", simple=True) is None |
| 1252 |
|
| 1253 |
# Test some illegal strings |
| 1254 |
result = utils.parse.parse_url(object, verify_host=False, simple=True) |
| 1255 |
assert result is None |
| 1256 |
result = utils.parse.parse_url(None, verify_host=False, simple=True) |
| 1257 |
assert result is None |
| 1258 |
|
| 1259 |
# Just a schema; invalid host |
| 1260 |
result = utils.parse.parse_url("test://", simple=True) |
| 1261 |
assert result is None |
| 1262 |
|
| 1263 |
# Do it again without host validation |
| 1264 |
result = utils.parse.parse_url("test://", verify_host=False, simple=True) |
| 1265 |
assert len(result) == 2 |
| 1266 |
assert result["schema"] == "test" |
| 1267 |
assert result["url"] == "test://" |
| 1268 |
|
| 1269 |
result = utils.parse.parse_url("testhostname", simple=True) |
| 1270 |
assert len(result) == 3 |
| 1271 |
assert result["schema"] == "http" |
| 1272 |
assert result["host"] == "testhostname" |
| 1273 |
# The default_schema kicks in here |
| 1274 |
assert result["url"] == "http://testhostname" |
| 1275 |
|
| 1276 |
result = utils.parse.parse_url( |
| 1277 |
"example.com", default_schema="unknown", simple=True |
| 1278 |
) |
| 1279 |
assert len(result) == 3 |
| 1280 |
assert result["schema"] == "unknown" |
| 1281 |
assert result["host"] == "example.com" |
| 1282 |
# The default_schema kicks in here |
| 1283 |
assert result["url"] == "unknown://example.com" |
| 1284 |
|
| 1285 |
# An empty string without a hostame is still valid if verify_host is set |
| 1286 |
result = utils.parse.parse_url("", verify_host=False, simple=True) |
| 1287 |
assert len(result) == 2 |
| 1288 |
assert result["schema"] == "http" |
| 1289 |
# The default_schema kicks in here |
| 1290 |
assert result["url"] == "http://" |
| 1291 |
|
| 1292 |
# A messed up URL |
| 1293 |
result = utils.parse.parse_url( |
| 1294 |
"test://:@/", verify_host=False, simple=True |
| 1295 |
) |
| 1296 |
assert len(result) == 6 |
| 1297 |
assert result["schema"] == "test" |
| 1298 |
assert result["user"] == "" |
| 1299 |
assert result["password"] == "" |
| 1300 |
assert result["fullpath"] == "/" |
| 1301 |
assert result["path"] == "/" |
| 1302 |
assert result["url"] == "test://:@/" |
| 1303 |
|
| 1304 |
result = utils.parse.parse_url( |
| 1305 |
"crazy://:@//_/@^&/jack.json", verify_host=False, simple=True |
| 1306 |
) |
| 1307 |
assert len(result) == 7 |
| 1308 |
assert result["schema"] == "crazy" |
| 1309 |
assert result["user"] == "" |
| 1310 |
assert result["password"] == "" |
| 1311 |
assert unquote(result["fullpath"]) == "/_/@^&/jack.json" |
| 1312 |
assert unquote(result["path"]) == "/_/@^&/" |
| 1313 |
assert result["query"] == "jack.json" |
| 1314 |
assert unquote(result["url"]) == "crazy://:@/_/@^&/jack.json" |
| 1315 |
|
| 1316 |
|
| 1317 |
def test_url_assembly(): |
| 1318 |
""" "utils: url_assembly() testing.""" |
| 1319 |
|
| 1320 |
url = "schema://user:password@hostname:port/path/?key=value" |
| 1321 |
assert ( |
| 1322 |
utils.parse.url_assembly( |
| 1323 |
**utils.parse.parse_url(url, verify_host=False) |
| 1324 |
) |
| 1325 |
== url |
| 1326 |
) |
| 1327 |
|
| 1328 |
# Same URL without trailing slash after path |
| 1329 |
url = "schema://user:password@hostname:port/path?key=value" |
| 1330 |
assert ( |
| 1331 |
utils.parse.url_assembly( |
| 1332 |
**utils.parse.parse_url(url, verify_host=False) |
| 1333 |
) |
| 1334 |
== url |
| 1335 |
) |
| 1336 |
|
| 1337 |
url = "schema://user@hostname:port/path?key=value" |
| 1338 |
assert ( |
| 1339 |
utils.parse.url_assembly( |
| 1340 |
**utils.parse.parse_url(url, verify_host=False) |
| 1341 |
) |
| 1342 |
== url |
| 1343 |
) |
| 1344 |
|
| 1345 |
url = "schema://hostname:10/a/file.php" |
| 1346 |
assert ( |
| 1347 |
utils.parse.url_assembly( |
| 1348 |
**utils.parse.parse_url(url, verify_host=False) |
| 1349 |
) |
| 1350 |
== url |
| 1351 |
) |
| 1352 |
|
| 1353 |
# When spaces and special characters are introduced, the URL |
| 1354 |
# is hard to mimic what was entered. Instead it is normalized |
| 1355 |
url = ( |
| 1356 |
"schema://hostname:10/a space/file.php?" |
| 1357 |
"arg=a+space&arg2=a%20space&arg3=a space" |
| 1358 |
) |
| 1359 |
assert ( |
| 1360 |
utils.parse.url_assembly( |
| 1361 |
**utils.parse.parse_url(url, verify_host=False) |
| 1362 |
) |
| 1363 |
== "schema://hostname:10/a%20space/file.php?" |
| 1364 |
"arg=a%2Bspace&arg2=a%20space&arg3=a%20space" |
| 1365 |
) |
| 1366 |
|
| 1367 |
# encode=True should only be used if you're passing in un-assembled |
| 1368 |
# content... hence the following is likely not what is expected: |
| 1369 |
assert ( |
| 1370 |
utils.parse.url_assembly( |
| 1371 |
**utils.parse.parse_url(url, verify_host=False), encode=True |
| 1372 |
) |
| 1373 |
== "schema://hostname:10/a%2520space/file.php?" |
| 1374 |
"arg=a%2Bspace&arg2=a%20space&arg3=a%20space" |
| 1375 |
) |
| 1376 |
|
| 1377 |
# But the following utilizes the encode=True and produces the |
| 1378 |
# desired effects: |
| 1379 |
content = { |
| 1380 |
"host": "hostname", |
| 1381 |
# Note that fullpath requires escaping in this case |
| 1382 |
"fullpath": "/a space/file.php", |
| 1383 |
"path": "/a space/", |
| 1384 |
"query": "file.php", |
| 1385 |
"schema": "schema", |
| 1386 |
# our query arguments also require escaping as well |
| 1387 |
"qsd": {"arg": "a+space", "arg2": "a space", "arg3": "a space"}, |
| 1388 |
} |
| 1389 |
assert ( |
| 1390 |
utils.parse.url_assembly(**content, encode=True) |
| 1391 |
== "schema://hostname/a%20space/file.php?" |
| 1392 |
"arg=a%2Bspace&arg2=a%20space&arg3=a%20space" |
| 1393 |
) |
| 1394 |
|
| 1395 |
|
| 1396 |
def test_parse_bool(): |
| 1397 |
"utils: parse_bool() testing" |
| 1398 |
|
| 1399 |
assert utils.parse.parse_bool("Enabled", None) is True |
| 1400 |
assert utils.parse.parse_bool("Disabled", None) is False |
| 1401 |
assert utils.parse.parse_bool("Allow", None) is True |
| 1402 |
assert utils.parse.parse_bool("Deny", None) is False |
| 1403 |
assert utils.parse.parse_bool("Yes", None) is True |
| 1404 |
assert utils.parse.parse_bool("YES", None) is True |
| 1405 |
assert utils.parse.parse_bool("Always", None) is True |
| 1406 |
assert utils.parse.parse_bool("No", None) is False |
| 1407 |
assert utils.parse.parse_bool("NO", None) is False |
| 1408 |
assert utils.parse.parse_bool("NEVER", None) is False |
| 1409 |
assert utils.parse.parse_bool("TrUE", None) is True |
| 1410 |
assert utils.parse.parse_bool("tRUe", None) is True |
| 1411 |
assert utils.parse.parse_bool("FAlse", None) is False |
| 1412 |
assert utils.parse.parse_bool("F", None) is False |
| 1413 |
assert utils.parse.parse_bool("T", None) is True |
| 1414 |
assert utils.parse.parse_bool("0", None) is False |
| 1415 |
assert utils.parse.parse_bool("1", None) is True |
| 1416 |
assert utils.parse.parse_bool("True", None) is True |
| 1417 |
assert utils.parse.parse_bool("Yes", None) is True |
| 1418 |
assert utils.parse.parse_bool(1, None) is True |
| 1419 |
assert utils.parse.parse_bool(0, None) is False |
| 1420 |
assert utils.parse.parse_bool(True, None) is True |
| 1421 |
assert utils.parse.parse_bool(False, None) is False |
| 1422 |
|
| 1423 |
# only the int of 0 will return False since the function |
| 1424 |
# casts this to a boolean |
| 1425 |
assert utils.parse.parse_bool(2, None) is True |
| 1426 |
# An empty list is still false |
| 1427 |
assert utils.parse.parse_bool([], None) is False |
| 1428 |
# But a list that contains something is True |
| 1429 |
assert ( |
| 1430 |
utils.parse.parse_bool( |
| 1431 |
[ |
| 1432 |
"value", |
| 1433 |
], |
| 1434 |
None, |
| 1435 |
) |
| 1436 |
is True |
| 1437 |
) |
| 1438 |
|
| 1439 |
# Use Default (which is False) |
| 1440 |
assert utils.parse.parse_bool("OhYeah") is False |
| 1441 |
# Adjust Default and get a different result |
| 1442 |
assert utils.parse.parse_bool("OhYeah", True) is True |
| 1443 |
|
| 1444 |
|
| 1445 |
def test_is_uuid(): |
| 1446 |
""" |
| 1447 |
API: is_uuid() function |
| 1448 |
""" |
| 1449 |
# Invalid Entries |
| 1450 |
assert utils.parse.is_uuid("invalid") is False |
| 1451 |
assert utils.parse.is_uuid(None) is False |
| 1452 |
assert utils.parse.is_uuid(5) is False |
| 1453 |
assert utils.parse.is_uuid(object) is False |
| 1454 |
|
| 1455 |
# A slightly invalid uuid4 entry |
| 1456 |
assert utils.parse.is_uuid("591ed387-fa65-ac97-9712-b9d2a15e42a9") is False |
| 1457 |
assert utils.parse.is_uuid("591ed387-fa65-Jc97-9712-b9d2a15e42a9") is False |
| 1458 |
|
| 1459 |
# Valid UUID4 Entries |
| 1460 |
assert utils.parse.is_uuid("591ed387-fa65-4c97-9712-b9d2a15e42a9") is True |
| 1461 |
assert utils.parse.is_uuid("32b0b447-fe84-4df1-8368-81925e729265") is True |
| 1462 |
|
| 1463 |
|
| 1464 |
def test_is_hostname(): |
| 1465 |
""" |
| 1466 |
API: is_hostname() function |
| 1467 |
|
| 1468 |
""" |
| 1469 |
# Valid Hostnames |
| 1470 |
assert utils.parse.is_hostname("yahoo.ca") == "yahoo.ca" |
| 1471 |
assert utils.parse.is_hostname("yahoo.ca.") == "yahoo.ca" |
| 1472 |
assert ( |
| 1473 |
utils.parse.is_hostname("valid-dashes-in-host.ca") |
| 1474 |
== "valid-dashes-in-host.ca" |
| 1475 |
) |
| 1476 |
assert ( |
| 1477 |
utils.parse.is_hostname("valid-underscores_in_host.ca") |
| 1478 |
== "valid-underscores_in_host.ca" |
| 1479 |
) |
| 1480 |
|
| 1481 |
# Underscores are supported by default |
| 1482 |
assert ( |
| 1483 |
utils.parse.is_hostname("valid_dashes_in_host.ca") |
| 1484 |
== "valid_dashes_in_host.ca" |
| 1485 |
) |
| 1486 |
# However they are not if specified otherwise: |
| 1487 |
assert ( |
| 1488 |
utils.parse.is_hostname("valid_dashes_in_host.ca", underscore=False) |
| 1489 |
is False |
| 1490 |
) |
| 1491 |
|
| 1492 |
# Invalid Hostnames |
| 1493 |
assert ( |
| 1494 |
utils.parse.is_hostname("-hostname.that.starts.with.a.dash") is False |
| 1495 |
) |
| 1496 |
assert utils.parse.is_hostname("invalid-characters_#^.ca") is False |
| 1497 |
assert utils.parse.is_hostname(" spaces ") is False |
| 1498 |
assert utils.parse.is_hostname(" ") is False |
| 1499 |
assert utils.parse.is_hostname("") is False |
| 1500 |
|
| 1501 |
# Valid IPv4 Addresses |
| 1502 |
assert utils.parse.is_hostname("127.0.0.1") == "127.0.0.1" |
| 1503 |
assert utils.parse.is_hostname("0.0.0.0") == "0.0.0.0" |
| 1504 |
assert utils.parse.is_hostname("255.255.255.255") == "255.255.255.255" |
| 1505 |
|
| 1506 |
# But not if we're not checking for this: |
| 1507 |
assert utils.parse.is_hostname("127.0.0.1", ipv4=False) is False |
| 1508 |
assert utils.parse.is_hostname("0.0.0.0", ipv4=False) is False |
| 1509 |
assert utils.parse.is_hostname("255.255.255.255", ipv4=False) is False |
| 1510 |
|
| 1511 |
# Invalid IPv4 Addresses |
| 1512 |
assert utils.parse.is_hostname("1.2.3") is False |
| 1513 |
assert utils.parse.is_hostname("256.256.256.256") is False |
| 1514 |
assert utils.parse.is_hostname("999.0.0.0") is False |
| 1515 |
assert utils.parse.is_hostname("1.2.3.4.5") is False |
| 1516 |
assert utils.parse.is_hostname(" 127.0.0.1 ") is False |
| 1517 |
assert utils.parse.is_hostname(" ") is False |
| 1518 |
assert utils.parse.is_hostname("") is False |
| 1519 |
|
| 1520 |
# Valid IPv6 Addresses (square brakets supported for URL construction) |
| 1521 |
assert ( |
| 1522 |
utils.parse.is_hostname("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]") |
| 1523 |
== "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]" |
| 1524 |
) |
| 1525 |
assert ( |
| 1526 |
utils.parse.is_hostname("2001:0db8:85a3:0000:0000:8a2e:0370:7334") |
| 1527 |
== "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]" |
| 1528 |
) |
| 1529 |
assert ( |
| 1530 |
utils.parse.is_hostname("[2001:db8:002a:3256:adfe:05c0:0003:0006]") |
| 1531 |
== "[2001:db8:002a:3256:adfe:05c0:0003:0006]" |
| 1532 |
) |
| 1533 |
|
| 1534 |
# localhost |
| 1535 |
assert utils.parse.is_hostname("::1") == "[::1]" |
| 1536 |
assert utils.parse.is_hostname("0:0:0:0:0:0:0:1") == "[0:0:0:0:0:0:0:1]" |
| 1537 |
|
| 1538 |
# compressed addresses |
| 1539 |
assert utils.parse.is_hostname("2001:db8::1") == "[2001:db8::1]" |
| 1540 |
assert utils.parse.is_hostname("[2001:db8::1]") == "[2001:db8::1]" |
| 1541 |
assert utils.parse.is_hostname("fd00::5") == "[fd00::5]" |
| 1542 |
assert utils.parse.is_hostname("[fd00::5]") == "[fd00::5]" |
| 1543 |
|
| 1544 |
# A malformed address is refused rather than partially accepted |
| 1545 |
assert utils.parse.is_hostname("2001:db8::1::2") is False |
| 1546 |
assert utils.parse.is_hostname("[2001:db8::1") is False |
| 1547 |
|
| 1548 |
# But not if we're not checking for this: |
| 1549 |
assert ( |
| 1550 |
utils.parse.is_hostname( |
| 1551 |
"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", ipv6=False |
| 1552 |
) |
| 1553 |
is False |
| 1554 |
) |
| 1555 |
assert ( |
| 1556 |
utils.parse.is_hostname( |
| 1557 |
"2001:0db8:85a3:0000:0000:8a2e:0370:7334", ipv6=False |
| 1558 |
) |
| 1559 |
is False |
| 1560 |
) |
| 1561 |
|
| 1562 |
# Test hostnames with a single character hostname |
| 1563 |
assert ( |
| 1564 |
utils.parse.is_hostname("cloud.a.example.com", ipv4=False, ipv6=False) |
| 1565 |
== "cloud.a.example.com" |
| 1566 |
) |
| 1567 |
|
| 1568 |
|
| 1569 |
def test_is_ipaddr(): |
| 1570 |
""" |
| 1571 |
API: is_ipaddr() function |
| 1572 |
|
| 1573 |
""" |
| 1574 |
# Valid IPv4 Addresses |
| 1575 |
assert utils.parse.is_ipaddr("127.0.0.1") == "127.0.0.1" |
| 1576 |
assert utils.parse.is_ipaddr("0.0.0.0") == "0.0.0.0" |
| 1577 |
assert utils.parse.is_ipaddr("255.255.255.255") == "255.255.255.255" |
| 1578 |
|
| 1579 |
# Invalid IPv4 Addresses |
| 1580 |
assert utils.parse.is_ipaddr("1.2.3") is False |
| 1581 |
assert utils.parse.is_ipaddr("256.256.256.256") is False |
| 1582 |
assert utils.parse.is_ipaddr("999.0.0.0") is False |
| 1583 |
assert utils.parse.is_ipaddr("1.2.3.4.5") is False |
| 1584 |
assert utils.parse.is_ipaddr(" 127.0.0.1 ") is False |
| 1585 |
assert utils.parse.is_ipaddr(" ") is False |
| 1586 |
assert utils.parse.is_ipaddr("") is False |
| 1587 |
|
| 1588 |
# Valid IPv6 Addresses (square brakets supported for URL construction) |
| 1589 |
assert ( |
| 1590 |
utils.parse.is_ipaddr("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]") |
| 1591 |
== "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]" |
| 1592 |
) |
| 1593 |
assert ( |
| 1594 |
utils.parse.is_ipaddr("2001:0db8:85a3:0000:0000:8a2e:0370:7334") |
| 1595 |
== "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]" |
| 1596 |
) |
| 1597 |
assert ( |
| 1598 |
utils.parse.is_ipaddr("[2001:db8:002a:3256:adfe:05c0:0003:0006]") |
| 1599 |
== "[2001:db8:002a:3256:adfe:05c0:0003:0006]" |
| 1600 |
) |
| 1601 |
|
| 1602 |
# localhost |
| 1603 |
assert utils.parse.is_ipaddr("::1") == "[::1]" |
| 1604 |
assert utils.parse.is_ipaddr("0:0:0:0:0:0:0:1") == "[0:0:0:0:0:0:0:1]" |
| 1605 |
|
| 1606 |
# Compressed addresses |
| 1607 |
assert utils.parse.is_ipaddr("2001:db8::1") == "[2001:db8::1]" |
| 1608 |
assert utils.parse.is_ipaddr("[2001:db8::1]") == "[2001:db8::1]" |
| 1609 |
assert utils.parse.is_ipaddr("fd00::5") == "[fd00::5]" |
| 1610 |
assert utils.parse.is_ipaddr("fe80::1") == "[fe80::1]" |
| 1611 |
assert utils.parse.is_ipaddr("::") == "[::]" |
| 1612 |
assert utils.parse.is_ipaddr("1::") == "[1::]" |
| 1613 |
assert utils.parse.is_ipaddr("1::8") == "[1::8]" |
| 1614 |
assert utils.parse.is_ipaddr("2001:db8::8a2e:370:7334") == ( |
| 1615 |
"[2001:db8::8a2e:370:7334]" |
| 1616 |
) |
| 1617 |
assert utils.parse.is_ipaddr("2001:db8:85a3::8a2e:370:7334") == ( |
| 1618 |
"[2001:db8:85a3::8a2e:370:7334]" |
| 1619 |
) |
| 1620 |
assert utils.parse.is_ipaddr("2001:db8:0:0:1::1") == ( |
| 1621 |
"[2001:db8:0:0:1::1]" |
| 1622 |
) |
| 1623 |
|
| 1624 |
# IPv4 mapped and embedded forms |
| 1625 |
assert utils.parse.is_ipaddr("::ffff:192.0.2.1") == "[::ffff:192.0.2.1]" |
| 1626 |
assert utils.parse.is_ipaddr("::192.0.2.1") == "[::192.0.2.1]" |
| 1627 |
assert utils.parse.is_ipaddr("2001:db8::192.0.2.1") == ( |
| 1628 |
"[2001:db8::192.0.2.1]" |
| 1629 |
) |
| 1630 |
|
| 1631 |
# A link local address may carry a zone id |
| 1632 |
assert utils.parse.is_ipaddr("fe80::1%eth0") == "[fe80::1%eth0]" |
| 1633 |
assert utils.parse.is_ipaddr("[fe80::1%eth0]") == "[fe80::1%eth0]" |
| 1634 |
|
| 1635 |
# Invalid IPv6 Addresses; more than one :: is not allowed |
| 1636 |
assert utils.parse.is_ipaddr("2001:db8::1::2") is False |
| 1637 |
assert utils.parse.is_ipaddr("::1::") is False |
| 1638 |
|
| 1639 |
# Groups longer than four hex digits, or that are not hex at all |
| 1640 |
assert utils.parse.is_ipaddr("2001:db8::12345") is False |
| 1641 |
assert utils.parse.is_ipaddr("not:an:address") is False |
| 1642 |
assert utils.parse.is_ipaddr("2001:db8::xyz") is False |
| 1643 |
|
| 1644 |
# Too many groups |
| 1645 |
assert utils.parse.is_ipaddr("1:2:3:4:5:6:7:8:9") is False |
| 1646 |
|
| 1647 |
# Trailing and leading junk is not quietly dropped |
| 1648 |
assert utils.parse.is_ipaddr("2001:db8::1junk") is False |
| 1649 |
assert utils.parse.is_ipaddr("junk2001:db8::1") is False |
| 1650 |
assert utils.parse.is_ipaddr("2001:db8::1 ") is False |
| 1651 |
assert utils.parse.is_ipaddr(" 2001:db8::1") is False |
| 1652 |
|
| 1653 |
# Brackets have to come in pairs |
| 1654 |
assert utils.parse.is_ipaddr("[2001:db8::1") is False |
| 1655 |
assert utils.parse.is_ipaddr("2001:db8::1]") is False |
| 1656 |
assert utils.parse.is_ipaddr("[::1") is False |
| 1657 |
assert utils.parse.is_ipaddr("::1]") is False |
| 1658 |
|
| 1659 |
# An IPv6 address is not accepted when we are not looking for one |
| 1660 |
assert utils.parse.is_ipaddr("2001:db8::1", ipv6=False) is False |
| 1661 |
assert utils.parse.is_ipaddr("fd00::5", ipv6=False) is False |
| 1662 |
|
| 1663 |
# ...and likewise for IPv4 |
| 1664 |
assert utils.parse.is_ipaddr("127.0.0.1", ipv4=False) is False |
| 1665 |
|
| 1666 |
# An IPv4 address is not an IPv6 one, brackets or not |
| 1667 |
assert utils.parse.is_ipaddr("[127.0.0.1]") is False |
| 1668 |
|
| 1669 |
|
| 1670 |
def test_parse_url_ipv6_host(): |
| 1671 |
""" |
| 1672 |
API: parse_url() IPv6 host handling |
| 1673 |
|
| 1674 |
""" |
| 1675 |
# Compressed addresses |
| 1676 |
result = utils.parse.parse_url("https://[2001:db8::1]/path") |
| 1677 |
assert result["host"] == "[2001:db8::1]" |
| 1678 |
assert result["port"] is None |
| 1679 |
assert result["url"] == "https://[2001:db8::1]/path" |
| 1680 |
|
| 1681 |
# ...and the port is kept separate from the address |
| 1682 |
result = utils.parse.parse_url("https://[2001:db8::1]:8443/path") |
| 1683 |
assert result["host"] == "[2001:db8::1]" |
| 1684 |
assert result["port"] == 8443 |
| 1685 |
assert result["url"] == "https://[2001:db8::1]:8443/path" |
| 1686 |
|
| 1687 |
# An address written without brackets and without a port also works |
| 1688 |
result = utils.parse.parse_url("https://fd00::5/path") |
| 1689 |
assert result["host"] == "[fd00::5]" |
| 1690 |
assert result["port"] is None |
| 1691 |
assert result["url"] == "https://[fd00::5]/path" |
| 1692 |
|
| 1693 |
# A full length address is unchanged |
| 1694 |
result = utils.parse.parse_url( |
| 1695 |
"https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:443/x" |
| 1696 |
) |
| 1697 |
assert result["host"] == "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]" |
| 1698 |
assert result["port"] == 443 |
| 1699 |
|
| 1700 |
# Credentials sit alongside an IPv6 host without disturbing it |
| 1701 |
result = utils.parse.parse_url("https://user:pass@[fd00::5]:8443/path") |
| 1702 |
assert result["host"] == "[fd00::5]" |
| 1703 |
assert result["port"] == 8443 |
| 1704 |
assert result["user"] == "user" |
| 1705 |
assert result["password"] == "pass" |
| 1706 |
|
| 1707 |
# A malformed bracketed address is refused. |
| 1708 |
assert utils.parse.parse_url("https://[2001:db8::1::2]/x") is None |
| 1709 |
assert ( |
| 1710 |
utils.parse.parse_url("https://[2001:db8::1::2]/x", verify_host=False) |
| 1711 |
is None |
| 1712 |
) |
| 1713 |
|
| 1714 |
# An address written without brackets |
| 1715 |
assert utils.parse.parse_url("https://2001:db8::1::2/x") is None |
| 1716 |
|
| 1717 |
result = utils.parse.parse_url( |
| 1718 |
"https://2001:db8::1::2/x", verify_host=False |
| 1719 |
) |
| 1720 |
assert result["host"] == "2001:db8::1::2" |
| 1721 |
|
| 1722 |
|
| 1723 |
def test_parse_url_ipv6_round_trip(): |
| 1724 |
""" |
| 1725 |
API: parse_url() IPv6 addresses survive a round trip |
| 1726 |
|
| 1727 |
""" |
| 1728 |
# ipv6 host checks |
| 1729 |
for host in ( |
| 1730 |
"[2001:db8::1]", |
| 1731 |
"[fd00::5]", |
| 1732 |
"[::1]", |
| 1733 |
"[2001:db8:85a3::8a2e:370:7334]", |
| 1734 |
"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", |
| 1735 |
): |
| 1736 |
url = f"https://{host}:8443/path" |
| 1737 |
first = utils.parse.parse_url(url) |
| 1738 |
assert first["host"] == host |
| 1739 |
|
| 1740 |
second = utils.parse.parse_url(first["url"]) |
| 1741 |
assert second["host"] == host |
| 1742 |
assert second["url"] == first["url"] |
| 1743 |
|
| 1744 |
|
| 1745 |
def test_is_email(): |
| 1746 |
""" |
| 1747 |
API: is_email() function |
| 1748 |
|
| 1749 |
""" |
| 1750 |
# Valid Emails |
| 1751 |
results = utils.parse.is_email("[email protected]") |
| 1752 |
assert results["name"] == "" |
| 1753 |
assert results["email"] == "[email protected]" |
| 1754 |
assert results["full_email"] == "[email protected]" |
| 1755 |
assert results["domain"] == "gmail.com" |
| 1756 |
assert results["user"] == "test" |
| 1757 |
assert results["label"] == "" |
| 1758 |
|
| 1759 |
results = utils.parse.is_email("test@my-valid_host.com") |
| 1760 |
assert results["name"] == "" |
| 1761 |
assert results["email"] == "test@my-valid_host.com" |
| 1762 |
assert results["full_email"] == "test@my-valid_host.com" |
| 1763 |
assert results["domain"] == "my-valid_host.com" |
| 1764 |
assert results["user"] == "test" |
| 1765 |
assert results["label"] == "" |
| 1766 |
|
| 1767 |
results = utils.parse.is_email("[email protected]") |
| 1768 |
assert results["name"] == "" |
| 1769 |
assert results["email"] == "[email protected]" |
| 1770 |
assert results["full_email"] == "[email protected]" |
| 1771 |
assert results["domain"] == "gmail.com" |
| 1772 |
assert results["user"] == "test" |
| 1773 |
assert results["label"] == "tag" |
| 1774 |
|
| 1775 |
# Support Full Names as well |
| 1776 |
results = utils.parse.is_email("Bill Gates: [email protected]") |
| 1777 |
assert results["name"] == "Bill Gates" |
| 1778 |
assert results["email"] == "[email protected]" |
| 1779 |
assert results["full_email"] == "[email protected]" |
| 1780 |
assert results["domain"] == "microsoft.com" |
| 1781 |
assert results["user"] == "bgates" |
| 1782 |
assert results["label"] == "" |
| 1783 |
|
| 1784 |
results = utils.parse.is_email("Bill Gates <[email protected]>") |
| 1785 |
assert results["name"] == "Bill Gates" |
| 1786 |
assert results["email"] == "[email protected]" |
| 1787 |
assert results["full_email"] == "[email protected]" |
| 1788 |
assert results["domain"] == "microsoft.com" |
| 1789 |
assert results["user"] == "bgates" |
| 1790 |
assert results["label"] == "" |
| 1791 |
|
| 1792 |
results = utils.parse.is_email("Bill Gates: <[email protected]>") |
| 1793 |
assert results["name"] == "Bill Gates" |
| 1794 |
assert results["email"] == "[email protected]" |
| 1795 |
assert results["full_email"] == "[email protected]" |
| 1796 |
assert results["domain"] == "microsoft.com" |
| 1797 |
assert results["user"] == "bgates" |
| 1798 |
assert results["label"] == "" |
| 1799 |
|
| 1800 |
results = utils.parse.is_email("Sundar Pichai <[email protected]>") |
| 1801 |
assert results["name"] == "Sundar Pichai" |
| 1802 |
assert results["email"] == "[email protected]" |
| 1803 |
assert results["full_email"] == "[email protected]" |
| 1804 |
assert results["domain"] == "gmail.com" |
| 1805 |
assert results["user"] == "spichai" |
| 1806 |
assert results["label"] == "ceo" |
| 1807 |
|
| 1808 |
# Support Quotes |
| 1809 |
results = utils.parse.is_email('"Chris Hemsworth" <[email protected]>') |
| 1810 |
assert results["name"] == "Chris Hemsworth" |
| 1811 |
assert results["email"] == "[email protected]" |
| 1812 |
assert results["full_email"] == "[email protected]" |
| 1813 |
assert results["domain"] == "test.com" |
| 1814 |
assert results["user"] == "ch" |
| 1815 |
assert results["label"] == "" |
| 1816 |
|
| 1817 |
# An email without name, but contains delimiters |
| 1818 |
results = utils.parse.is_email(" <[email protected]>") |
| 1819 |
assert results["name"] == "" |
| 1820 |
assert results["email"] == "[email protected]" |
| 1821 |
assert results["full_email"] == "[email protected]" |
| 1822 |
assert results["domain"] == "gmail.com" |
| 1823 |
assert results["user"] == "spichai" |
| 1824 |
assert results["label"] == "" |
| 1825 |
|
| 1826 |
# a valid email not properly delimited with a colon or angle bracket |
| 1827 |
# We do a best guess and still parse it correctly |
| 1828 |
results = utils.parse.is_email("Name [email protected]") |
| 1829 |
assert results["name"] == "Name" |
| 1830 |
assert results["email"] == "[email protected]" |
| 1831 |
assert results["full_email"] == "[email protected]" |
| 1832 |
assert results["domain"] == "example.com" |
| 1833 |
assert results["user"] == "valid" |
| 1834 |
assert results["label"] == "" |
| 1835 |
|
| 1836 |
# a valid email not properly delimited with a colon or angle bracket |
| 1837 |
# We do a best guess and still parse it correctly |
| 1838 |
results = utils.parse.is_email("Руслан Эра [email protected]") |
| 1839 |
assert results["name"] == "Руслан Эра" |
| 1840 |
assert results["email"] == "[email protected]" |
| 1841 |
assert results["full_email"] == "[email protected]" |
| 1842 |
assert results["domain"] == "example.ru" |
| 1843 |
assert results["user"] == "russia" |
| 1844 |
assert results["label"] == "russian" |
| 1845 |
|
| 1846 |
# Invalid Emails |
| 1847 |
assert utils.parse.is_email("invalid.com") is False |
| 1848 |
assert utils.parse.is_email(object()) is False |
| 1849 |
assert utils.parse.is_email(None) is False |
| 1850 |
assert utils.parse.is_email("Just A Name") is False |
| 1851 |
assert utils.parse.is_email("Name <bademail>") is False |
| 1852 |
|
| 1853 |
# Extended valid emails |
| 1854 |
# |
| 1855 |
# The first + denotes our label, so this test really validates |
| 1856 |
# that there is a correct split and parsing of our email |
| 1857 |
results = utils.parse.is_email("a-z0-9_!#$%&*+/=?%`{|}~^[email protected]") |
| 1858 |
assert results["name"] == "" |
| 1859 |
assert results["label"] == "a-z0-9_!#$%&*" |
| 1860 |
assert results["email"] == "/=?%`{|}~^[email protected]" |
| 1861 |
assert results["full_email"] == "a-z0-9_!#$%&*+/=?%`{|}~^[email protected]" |
| 1862 |
assert results["domain"] == "gmail.com" |
| 1863 |
assert results["user"] == "/=?%`{|}~^.-" |
| 1864 |
|
| 1865 |
# A similar test without '+' (use of a label) |
| 1866 |
|
| 1867 |
# The first + denotes our label, so this test really validates |
| 1868 |
# that there is a correct split and parsing of our email |
| 1869 |
results = utils.parse.is_email("a-z0-9_!#$%&*/=?%`{|}~^[email protected]") |
| 1870 |
assert results["name"] == "" |
| 1871 |
assert results["label"] == "" |
| 1872 |
assert results["email"] == "a-z0-9_!#$%&*/=?%`{|}~^[email protected]" |
| 1873 |
assert results["full_email"] == "a-z0-9_!#$%&*/=?%`{|}~^[email protected]" |
| 1874 |
assert results["domain"] == "gmail.com" |
| 1875 |
assert results["user"] == "a-z0-9_!#$%&*/=?%`{|}~^.-" |
| 1876 |
|
| 1877 |
|
| 1878 |
def test_is_domain_service_target(): |
| 1879 |
""" |
| 1880 |
API: is_domain_service_target() function |
| 1881 |
|
| 1882 |
""" |
| 1883 |
# Invalid information |
| 1884 |
assert utils.parse.is_domain_service_target(None) is False |
| 1885 |
assert utils.parse.is_domain_service_target(42) is False |
| 1886 |
assert utils.parse.is_domain_service_target(object) is False |
| 1887 |
assert utils.parse.is_domain_service_target("") is False |
| 1888 |
assert utils.parse.is_domain_service_target("+()") is False |
| 1889 |
assert utils.parse.is_domain_service_target("+") is False |
| 1890 |
|
| 1891 |
# Valid entries |
| 1892 |
result = utils.parse.is_domain_service_target("service") |
| 1893 |
assert isinstance(result, dict) |
| 1894 |
assert result["service"] == "service" |
| 1895 |
# Default domain |
| 1896 |
assert result["domain"] == "notify" |
| 1897 |
assert isinstance(result["targets"], list) |
| 1898 |
assert len(result["targets"]) == 0 |
| 1899 |
|
| 1900 |
result = utils.parse.is_domain_service_target("domain.service") |
| 1901 |
assert isinstance(result, dict) |
| 1902 |
assert result["service"] == "service" |
| 1903 |
assert result["domain"] == "domain" |
| 1904 |
assert isinstance(result["targets"], list) |
| 1905 |
assert len(result["targets"]) == 0 |
| 1906 |
|
| 1907 |
result = utils.parse.is_domain_service_target("domain.service:target") |
| 1908 |
assert isinstance(result, dict) |
| 1909 |
assert result["service"] == "service" |
| 1910 |
assert result["domain"] == "domain" |
| 1911 |
assert isinstance(result["targets"], list) |
| 1912 |
assert len(result["targets"]) == 1 |
| 1913 |
assert result["targets"][0] == "target" |
| 1914 |
|
| 1915 |
result = utils.parse.is_domain_service_target("domain.service:t1,t2,t3") |
| 1916 |
assert isinstance(result, dict) |
| 1917 |
assert result["service"] == "service" |
| 1918 |
assert result["domain"] == "domain" |
| 1919 |
assert isinstance(result["targets"], list) |
| 1920 |
assert len(result["targets"]) == 3 |
| 1921 |
assert "t1" in result["targets"] |
| 1922 |
assert "t2" in result["targets"] |
| 1923 |
assert "t3" in result["targets"] |
| 1924 |
|
| 1925 |
result = utils.parse.is_domain_service_target( |
| 1926 |
"service:t1,t2,t3", domain="new_default" |
| 1927 |
) |
| 1928 |
assert isinstance(result, dict) |
| 1929 |
assert result["service"] == "service" |
| 1930 |
# Default domain |
| 1931 |
assert result["domain"] == "new_default" |
| 1932 |
assert isinstance(result["targets"], list) |
| 1933 |
assert len(result["targets"]) == 3 |
| 1934 |
assert "t1" in result["targets"] |
| 1935 |
assert "t2" in result["targets"] |
| 1936 |
assert "t3" in result["targets"] |
| 1937 |
|
| 1938 |
|
| 1939 |
def test_is_call_sign_no(): |
| 1940 |
""" |
| 1941 |
API: is_call_sign() function |
| 1942 |
|
| 1943 |
""" |
| 1944 |
# Invalid numbers |
| 1945 |
assert utils.parse.is_call_sign(None) is False |
| 1946 |
assert utils.parse.is_call_sign(42) is False |
| 1947 |
assert utils.parse.is_call_sign(object) is False |
| 1948 |
assert utils.parse.is_call_sign("") is False |
| 1949 |
assert utils.parse.is_call_sign("1") is False |
| 1950 |
assert utils.parse.is_call_sign("12") is False |
| 1951 |
assert utils.parse.is_call_sign("abc") is False |
| 1952 |
assert utils.parse.is_call_sign("+()") is False |
| 1953 |
assert utils.parse.is_call_sign("+") is False |
| 1954 |
assert utils.parse.is_call_sign(None) is False |
| 1955 |
assert utils.parse.is_call_sign(42) is False |
| 1956 |
|
| 1957 |
# To short or 2 long |
| 1958 |
assert utils.parse.is_call_sign("DF1ABCX") is False |
| 1959 |
assert utils.parse.is_call_sign("DF1ABCEFG") is False |
| 1960 |
assert utils.parse.is_call_sign("1ABCX") is False |
| 1961 |
# 4th character is not an number |
| 1962 |
assert utils.parse.is_call_sign("XXXXXX") is False |
| 1963 |
|
| 1964 |
# Some valid checks |
| 1965 |
# 1x2 |
| 1966 |
result = utils.parse.is_call_sign("A0AF") |
| 1967 |
assert isinstance(result, dict) |
| 1968 |
assert result["callsign"] == "A0AF" |
| 1969 |
assert result["ssid"] == "" |
| 1970 |
|
| 1971 |
# 2x1 |
| 1972 |
result = utils.parse.is_call_sign("AA0A") |
| 1973 |
assert isinstance(result, dict) |
| 1974 |
assert result["callsign"] == "AA0A" |
| 1975 |
assert result["ssid"] == "" |
| 1976 |
|
| 1977 |
# 2x2 |
| 1978 |
result = utils.parse.is_call_sign("AA0AF") |
| 1979 |
assert isinstance(result, dict) |
| 1980 |
assert result["callsign"] == "AA0AF" |
| 1981 |
assert result["ssid"] == "" |
| 1982 |
|
| 1983 |
result = utils.parse.is_call_sign("AA0AF-23") |
| 1984 |
assert isinstance(result, dict) |
| 1985 |
assert result["callsign"] == "AA0AF" |
| 1986 |
assert result["ssid"] == "23" |
| 1987 |
|
| 1988 |
# 1x3 |
| 1989 |
result = utils.parse.is_call_sign("K0ACL") |
| 1990 |
assert isinstance(result, dict) |
| 1991 |
assert result["callsign"] == "K0ACL" |
| 1992 |
assert result["ssid"] == "" |
| 1993 |
|
| 1994 |
# 2x3 |
| 1995 |
result = utils.parse.is_call_sign("DF1ABC") |
| 1996 |
assert isinstance(result, dict) |
| 1997 |
assert result["callsign"] == "DF1ABC" |
| 1998 |
assert result["ssid"] == "" |
| 1999 |
|
| 2000 |
# Get our SSID |
| 2001 |
result = utils.parse.is_call_sign("DF1ABC-14") |
| 2002 |
assert result["callsign"] == "DF1ABC" |
| 2003 |
assert result["ssid"] == "14" |
| 2004 |
|
| 2005 |
|
| 2006 |
def test_is_phone_no(): |
| 2007 |
""" |
| 2008 |
API: is_phone_no() function |
| 2009 |
|
| 2010 |
""" |
| 2011 |
# Invalid numbers |
| 2012 |
assert utils.parse.is_phone_no(None) is False |
| 2013 |
assert utils.parse.is_phone_no(42) is False |
| 2014 |
assert utils.parse.is_phone_no(object) is False |
| 2015 |
assert utils.parse.is_phone_no("") is False |
| 2016 |
assert utils.parse.is_phone_no("1") is False |
| 2017 |
assert utils.parse.is_phone_no("12") is False |
| 2018 |
assert utils.parse.is_phone_no("abc") is False |
| 2019 |
assert utils.parse.is_phone_no("+()") is False |
| 2020 |
assert utils.parse.is_phone_no("+") is False |
| 2021 |
assert utils.parse.is_phone_no(None) is False |
| 2022 |
assert utils.parse.is_phone_no(42) is False |
| 2023 |
assert utils.parse.is_phone_no(object, min_len=0) is False |
| 2024 |
assert utils.parse.is_phone_no("", min_len=1) is False |
| 2025 |
assert utils.parse.is_phone_no("abc", min_len=0) is False |
| 2026 |
assert utils.parse.is_phone_no("", min_len=0) is False |
| 2027 |
|
| 2028 |
# Ambigious, but will document it here in this test as such |
| 2029 |
results = utils.parse.is_phone_no("+((()))--+", min_len=0) |
| 2030 |
assert results["country"] == "" |
| 2031 |
assert results["area"] == "" |
| 2032 |
assert results["line"] == "" |
| 2033 |
assert results["pretty"] == "" |
| 2034 |
assert results["full"] == "" |
| 2035 |
|
| 2036 |
# Valid phone numbers |
| 2037 |
assert utils.parse.is_phone_no("+(0)") is False |
| 2038 |
results = utils.parse.is_phone_no("+(0)", min_len=1) |
| 2039 |
assert results["country"] == "" |
| 2040 |
assert results["area"] == "" |
| 2041 |
assert results["line"] == "0" |
| 2042 |
assert results["pretty"] == "0" |
| 2043 |
assert results["full"] == "0" |
| 2044 |
|
| 2045 |
assert utils.parse.is_phone_no("1") is False |
| 2046 |
results = utils.parse.is_phone_no("1", min_len=1) |
| 2047 |
assert results["country"] == "" |
| 2048 |
assert results["area"] == "" |
| 2049 |
assert results["line"] == "1" |
| 2050 |
assert results["pretty"] == "1" |
| 2051 |
assert results["full"] == "1" |
| 2052 |
|
| 2053 |
assert utils.parse.is_phone_no("12") is False |
| 2054 |
results = utils.parse.is_phone_no("12", min_len=2) |
| 2055 |
assert results["country"] == "" |
| 2056 |
assert results["area"] == "" |
| 2057 |
assert results["line"] == "12" |
| 2058 |
assert results["pretty"] == "12" |
| 2059 |
assert results["full"] == "12" |
| 2060 |
|
| 2061 |
assert utils.parse.is_phone_no("911") is False |
| 2062 |
results = utils.parse.is_phone_no("911", min_len=3) |
| 2063 |
assert isinstance(results, dict) |
| 2064 |
assert results["country"] == "" |
| 2065 |
assert results["area"] == "" |
| 2066 |
assert results["line"] == "911" |
| 2067 |
assert results["pretty"] == "911" |
| 2068 |
assert results["full"] == "911" |
| 2069 |
|
| 2070 |
assert utils.parse.is_phone_no("1234") is False |
| 2071 |
results = utils.parse.is_phone_no("1234", min_len=4) |
| 2072 |
assert isinstance(results, dict) |
| 2073 |
assert results["country"] == "" |
| 2074 |
assert results["area"] == "" |
| 2075 |
assert results["line"] == "1234" |
| 2076 |
assert results["pretty"] == "1234" |
| 2077 |
assert results["full"] == "1234" |
| 2078 |
|
| 2079 |
assert utils.parse.is_phone_no("12345") is False |
| 2080 |
results = utils.parse.is_phone_no("12345", min_len=5) |
| 2081 |
assert isinstance(results, dict) |
| 2082 |
assert results["country"] == "" |
| 2083 |
assert results["area"] == "" |
| 2084 |
assert results["line"] == "12345" |
| 2085 |
assert results["pretty"] == "12345" |
| 2086 |
assert results["full"] == "12345" |
| 2087 |
|
| 2088 |
assert utils.parse.is_phone_no("123456") is False |
| 2089 |
results = utils.parse.is_phone_no("123456", min_len=6) |
| 2090 |
assert isinstance(results, dict) |
| 2091 |
assert results["country"] == "" |
| 2092 |
assert results["area"] == "" |
| 2093 |
assert results["line"] == "123456" |
| 2094 |
assert results["pretty"] == "123456" |
| 2095 |
assert results["full"] == "123456" |
| 2096 |
|
| 2097 |
# at 7 digits, the format hyphenates in the `pretty` section |
| 2098 |
assert utils.parse.is_phone_no("1234567") is False |
| 2099 |
results = utils.parse.is_phone_no("1234567", min_len=7) |
| 2100 |
assert isinstance(results, dict) |
| 2101 |
assert results["country"] == "" |
| 2102 |
assert results["area"] == "" |
| 2103 |
assert results["line"] == "1234567" |
| 2104 |
assert results["pretty"] == "123-4567" |
| 2105 |
assert results["full"] == "1234567" |
| 2106 |
|
| 2107 |
results = utils.parse.is_phone_no("1(800) 123-4567") |
| 2108 |
assert isinstance(results, dict) |
| 2109 |
assert results["country"] == "1" |
| 2110 |
assert results["area"] == "800" |
| 2111 |
assert results["line"] == "1234567" |
| 2112 |
assert results["pretty"] == "+1 800-123-4567" |
| 2113 |
assert results["full"] == "18001234567" |
| 2114 |
|
| 2115 |
|
| 2116 |
def test_parse_call_sign(): |
| 2117 |
"""utils: parse_call_sign() testing""" |
| 2118 |
# A simple single array entry (As str) |
| 2119 |
results = utils.parse.parse_call_sign("") |
| 2120 |
assert isinstance(results, list) |
| 2121 |
assert len(results) == 0 |
| 2122 |
|
| 2123 |
# just delimeters |
| 2124 |
results = utils.parse.parse_call_sign(", ,, , ,,, ") |
| 2125 |
assert isinstance(results, list) |
| 2126 |
assert len(results) == 0 |
| 2127 |
|
| 2128 |
results = utils.parse.parse_call_sign(None) |
| 2129 |
assert isinstance(results, list) |
| 2130 |
assert len(results) == 0 |
| 2131 |
|
| 2132 |
results = utils.parse.parse_call_sign(42) |
| 2133 |
assert isinstance(results, list) |
| 2134 |
assert len(results) == 0 |
| 2135 |
|
| 2136 |
results = utils.parse.parse_call_sign( |
| 2137 |
"this is not a parseable call sign at all" |
| 2138 |
) |
| 2139 |
assert isinstance(results, list) |
| 2140 |
assert len(results) == 9 |
| 2141 |
|
| 2142 |
results = utils.parse.parse_call_sign( |
| 2143 |
"this is not a parseable call sign at all", store_unparseable=False |
| 2144 |
) |
| 2145 |
assert isinstance(results, list) |
| 2146 |
assert len(results) == 0 |
| 2147 |
|
| 2148 |
# Now test valid call signs |
| 2149 |
results = utils.parse.parse_call_sign("0A1DEF") |
| 2150 |
assert isinstance(results, list) |
| 2151 |
assert len(results) == 1 |
| 2152 |
assert "0A1DEF" in results |
| 2153 |
|
| 2154 |
results = utils.parse.parse_call_sign("0A1DEF, DF1ABC") |
| 2155 |
assert isinstance(results, list) |
| 2156 |
assert len(results) == 2 |
| 2157 |
assert "0A1DEF" in results |
| 2158 |
assert "DF1ABC" in results |
| 2159 |
|
| 2160 |
results = utils.parse.parse_call_sign("AA0A, A0AF-12, GARBAGE") |
| 2161 |
assert isinstance(results, list) |
| 2162 |
assert len(results) == 2 |
| 2163 |
assert "AA0A" in results |
| 2164 |
assert "A0AF-12" in results |
| 2165 |
|
| 2166 |
|
| 2167 |
def test_parse_domain_service_targets(): |
| 2168 |
"""utils: parse_domain_service_targets() testing""" |
| 2169 |
|
| 2170 |
results = utils.parse.parse_domain_service_targets("") |
| 2171 |
assert isinstance(results, list) |
| 2172 |
assert len(results) == 0 |
| 2173 |
|
| 2174 |
results = utils.parse.parse_domain_service_targets("service1 service2") |
| 2175 |
assert isinstance(results, list) |
| 2176 |
assert len(results) == 2 |
| 2177 |
assert "service1" in results |
| 2178 |
assert "service2" in results |
| 2179 |
|
| 2180 |
results = utils.parse.parse_domain_service_targets( |
| 2181 |
"service1:target1,target2" |
| 2182 |
) |
| 2183 |
assert isinstance(results, list) |
| 2184 |
assert len(results) == 1 |
| 2185 |
assert "service1:target1,target2" in results |
| 2186 |
|
| 2187 |
results = utils.parse.parse_domain_service_targets( |
| 2188 |
"service1:target1,target2 service2 domain.service3" |
| 2189 |
) |
| 2190 |
assert isinstance(results, list) |
| 2191 |
assert len(results) == 3 |
| 2192 |
assert "service1:target1,target2" in results |
| 2193 |
assert "service2" in results |
| 2194 |
assert "domain.service3" in results |
| 2195 |
|
| 2196 |
# Support a comma in the space between entries |
| 2197 |
results = utils.parse.parse_domain_service_targets( |
| 2198 |
"service1:target1,target2, service2 ,domain.service3, , , service4" |
| 2199 |
) |
| 2200 |
assert isinstance(results, list) |
| 2201 |
assert len(results) == 4 |
| 2202 |
assert "service1:target1,target2" in results |
| 2203 |
assert "service2" in results |
| 2204 |
assert "domain.service3" in results |
| 2205 |
assert "service4" in results |
| 2206 |
|
| 2207 |
results = utils.parse.parse_domain_service_targets( |
| 2208 |
"service:target1,target2" |
| 2209 |
) |
| 2210 |
assert isinstance(results, list) |
| 2211 |
assert len(results) == 1 |
| 2212 |
assert "service:target1,target2" in results |
| 2213 |
|
| 2214 |
# Handle unparseables |
| 2215 |
results = utils.parse.parse_domain_service_targets( |
| 2216 |
": %invalid ^entries%", store_unparseable=False |
| 2217 |
) |
| 2218 |
assert isinstance(results, list) |
| 2219 |
assert len(results) == 0 |
| 2220 |
|
| 2221 |
# Verify many separators complete in reasonable time. |
| 2222 |
t0 = time.monotonic() |
| 2223 |
utils.parse.parse_domain_service_targets("ha.smtp" + " , " * 30) |
| 2224 |
assert time.monotonic() - t0 < 5.0 |
| 2225 |
|
| 2226 |
|
| 2227 |
def test_parse_phone_no(): |
| 2228 |
"""utils: parse_phone_no() testing""" |
| 2229 |
# A simple single array entry (As str) |
| 2230 |
results = utils.parse.parse_phone_no("") |
| 2231 |
assert isinstance(results, list) |
| 2232 |
assert len(results) == 0 |
| 2233 |
|
| 2234 |
# just delimeters |
| 2235 |
results = utils.parse.parse_phone_no(", ,, , ,,, ") |
| 2236 |
assert isinstance(results, list) |
| 2237 |
assert len(results) == 0 |
| 2238 |
|
| 2239 |
results = utils.parse.parse_phone_no(",") |
| 2240 |
assert isinstance(results, list) |
| 2241 |
assert len(results) == 0 |
| 2242 |
|
| 2243 |
results = utils.parse.parse_phone_no(None) |
| 2244 |
assert isinstance(results, list) |
| 2245 |
assert len(results) == 0 |
| 2246 |
|
| 2247 |
results = utils.parse.parse_phone_no(42) |
| 2248 |
assert isinstance(results, list) |
| 2249 |
assert len(results) == 0 |
| 2250 |
|
| 2251 |
results = utils.parse.parse_phone_no( |
| 2252 |
"this is not a parseable phoneno at all" |
| 2253 |
) |
| 2254 |
assert isinstance(results, list) |
| 2255 |
assert len(results) == 8 |
| 2256 |
# Now we do it again with the store_unparsable flag set to False |
| 2257 |
results = utils.parse.parse_phone_no( |
| 2258 |
"this is not a parseable email at all", store_unparseable=False |
| 2259 |
) |
| 2260 |
assert isinstance(results, list) |
| 2261 |
assert len(results) == 0 |
| 2262 |
|
| 2263 |
results = utils.parse.parse_phone_no("+", store_unparseable=False) |
| 2264 |
assert isinstance(results, list) |
| 2265 |
assert len(results) == 0 |
| 2266 |
|
| 2267 |
results = utils.parse.parse_phone_no("(", store_unparseable=False) |
| 2268 |
assert isinstance(results, list) |
| 2269 |
assert len(results) == 0 |
| 2270 |
|
| 2271 |
# Number is too short |
| 2272 |
results = utils.parse.parse_phone_no("0", store_unparseable=False) |
| 2273 |
assert isinstance(results, list) |
| 2274 |
assert len(results) == 0 |
| 2275 |
|
| 2276 |
results = utils.parse.parse_phone_no("12", store_unparseable=False) |
| 2277 |
assert isinstance(results, list) |
| 2278 |
assert len(results) == 0 |
| 2279 |
|
| 2280 |
# Now test valid phone numbers |
| 2281 |
results = utils.parse.parse_phone_no("+1 (124) 245 2345") |
| 2282 |
assert isinstance(results, list) |
| 2283 |
assert len(results) == 1 |
| 2284 |
assert "+1 (124) 245 2345" in results |
| 2285 |
|
| 2286 |
results = utils.parse.parse_phone_no("911", store_unparseable=False) |
| 2287 |
assert isinstance(results, list) |
| 2288 |
assert len(results) == 1 |
| 2289 |
assert "911" in results |
| 2290 |
|
| 2291 |
results = utils.parse.parse_phone_no( |
| 2292 |
"911, 123-123-1234", store_unparseable=False |
| 2293 |
) |
| 2294 |
assert isinstance(results, list) |
| 2295 |
assert len(results) == 2 |
| 2296 |
assert "911" in results |
| 2297 |
assert "123-123-1234" in results |
| 2298 |
|
| 2299 |
# Space variations |
| 2300 |
results = utils.parse.parse_phone_no(" 911 , +1 (123) 123-1234") |
| 2301 |
assert isinstance(results, list) |
| 2302 |
assert len(results) == 2 |
| 2303 |
assert "911" in results |
| 2304 |
assert "+1 (123) 123-1234" in results |
| 2305 |
|
| 2306 |
results = utils.parse.parse_phone_no(" 911 , + 1 ( 123 ) 123-1234") |
| 2307 |
assert isinstance(results, list) |
| 2308 |
assert len(results) == 2 |
| 2309 |
assert "911" in results |
| 2310 |
assert "+ 1 ( 123 ) 123-1234" in results |
| 2311 |
|
| 2312 |
# Verify trailing-whitespace runs complete in reasonable time. |
| 2313 |
t0 = time.monotonic() |
| 2314 |
results = utils.parse.parse_phone_no("+12345678901" + " " * 1000) |
| 2315 |
assert time.monotonic() - t0 < 5.0 |
| 2316 |
assert results == ["+12345678901"] |
| 2317 |
|
| 2318 |
|
| 2319 |
def test_parse_emails(): |
| 2320 |
"""utils: parse_emails() testing""" |
| 2321 |
# A simple single array entry (As str) |
| 2322 |
results = utils.parse.parse_emails("") |
| 2323 |
assert isinstance(results, list) |
| 2324 |
assert len(results) == 0 |
| 2325 |
|
| 2326 |
# just delimeters |
| 2327 |
results = utils.parse.parse_emails(", ,, , ,,, ") |
| 2328 |
assert isinstance(results, list) |
| 2329 |
assert len(results) == 0 |
| 2330 |
|
| 2331 |
results = utils.parse.parse_emails(",") |
| 2332 |
assert isinstance(results, list) |
| 2333 |
assert len(results) == 0 |
| 2334 |
|
| 2335 |
results = utils.parse.parse_emails(None) |
| 2336 |
assert isinstance(results, list) |
| 2337 |
assert len(results) == 0 |
| 2338 |
|
| 2339 |
results = utils.parse.parse_emails(42) |
| 2340 |
assert isinstance(results, list) |
| 2341 |
assert len(results) == 0 |
| 2342 |
|
| 2343 |
results = utils.parse.parse_emails("this is not a parseable email at all") |
| 2344 |
assert isinstance(results, list) |
| 2345 |
assert len(results) == 8 |
| 2346 |
# Now we do it again with the store_unparsable flag set to False |
| 2347 |
results = utils.parse.parse_emails( |
| 2348 |
"this is not a parseable email at all", store_unparseable=False |
| 2349 |
) |
| 2350 |
assert isinstance(results, list) |
| 2351 |
assert len(results) == 0 |
| 2352 |
|
| 2353 |
# Now test valid emails |
| 2354 |
results = utils.parse.parse_emails("[email protected]") |
| 2355 |
assert isinstance(results, list) |
| 2356 |
assert len(results) == 1 |
| 2357 |
assert "[email protected]" in results |
| 2358 |
|
| 2359 |
results = utils.parse.parse_emails("a@") |
| 2360 |
assert isinstance(results, list) |
| 2361 |
assert len(results) == 1 |
| 2362 |
assert "a@" in results |
| 2363 |
|
| 2364 |
results = utils.parse.parse_emails("[email protected] [email protected]") |
| 2365 |
assert isinstance(results, list) |
| 2366 |
assert len(results) == 2 |
| 2367 |
assert "[email protected]" in results |
| 2368 |
assert "[email protected]" in results |
| 2369 |
|
| 2370 |
# Commas and spaces found inside URLs are ignored |
| 2371 |
emails = [ |
| 2372 |
"[email protected],", |
| 2373 |
"[email protected],,, [email protected]", |
| 2374 |
"Chuck Norris [email protected]", |
| 2375 |
"David Spade [email protected], Yours Truly [email protected]", |
| 2376 |
] |
| 2377 |
|
| 2378 |
results = utils.parse.parse_emails(", ".join(emails)) |
| 2379 |
assert isinstance(results, list) |
| 2380 |
assert len(results) == 6 |
| 2381 |
assert "[email protected]" in results |
| 2382 |
assert "[email protected]" in results |
| 2383 |
assert "[email protected]" in results |
| 2384 |
assert "Chuck Norris [email protected]" in results |
| 2385 |
assert "David Spade [email protected]" in results |
| 2386 |
assert "Yours Truly [email protected]" in results |
| 2387 |
|
| 2388 |
# Test triangle bracket parsing |
| 2389 |
# Commas and spaces found inside URLs are ignored |
| 2390 |
emails = [ |
| 2391 |
"User1 [email protected]", |
| 2392 |
"User 2 [email protected]", |
| 2393 |
"User Three <[email protected]>", |
| 2394 |
"The Forth User: <[email protected]>", |
| 2395 |
"5th User: [email protected]", |
| 2396 |
] |
| 2397 |
|
| 2398 |
results = utils.parse.parse_emails(", ".join(emails)) |
| 2399 |
assert isinstance(results, list) |
| 2400 |
assert len(results) == len(emails) |
| 2401 |
for email in emails: |
| 2402 |
assert email in results |
| 2403 |
is_email = utils.parse.is_email(email) |
| 2404 |
assert is_email |
| 2405 |
assert is_email.get("name") |
| 2406 |
|
| 2407 |
# pass the entries in as a list |
| 2408 |
results = utils.parse.parse_emails(emails) |
| 2409 |
assert isinstance(results, list) |
| 2410 |
assert len(results) == len(emails) |
| 2411 |
for email in emails: |
| 2412 |
assert email in results |
| 2413 |
|
| 2414 |
# Pass in some unparseables |
| 2415 |
results = utils.parse.parse_emails("garbage") |
| 2416 |
assert isinstance(results, list) |
| 2417 |
assert len(results) == 1 |
| 2418 |
|
| 2419 |
results = utils.parse.parse_emails("garbage", store_unparseable=False) |
| 2420 |
assert isinstance(results, list) |
| 2421 |
assert len(results) == 0 |
| 2422 |
|
| 2423 |
# Pass in garbage |
| 2424 |
results = utils.parse.parse_emails(object) |
| 2425 |
assert isinstance(results, list) |
| 2426 |
assert len(results) == 0 |
| 2427 |
|
| 2428 |
results = utils.parse.parse_emails(42) |
| 2429 |
assert isinstance(results, list) |
| 2430 |
assert len(results) == 0 |
| 2431 |
|
| 2432 |
results = utils.parse.parse_emails([None, object, 42]) |
| 2433 |
assert isinstance(results, list) |
| 2434 |
assert len(results) == 0 |
| 2435 |
|
| 2436 |
# Verify trailing-whitespace runs complete in reasonable time. |
| 2437 |
t0 = time.monotonic() |
| 2438 |
results = utils.parse.parse_emails("[email protected]" + " " * 400) |
| 2439 |
assert time.monotonic() - t0 < 5.0 |
| 2440 |
assert results == ["[email protected]"] |
| 2441 |
|
| 2442 |
# Quoted display names containing commas are kept as a single token. |
| 2443 |
results = utils.parse.parse_emails( |
| 2444 |
'"John, Jason & Mike" <[email protected]>' |
| 2445 |
) |
| 2446 |
assert results == ['"John, Jason & Mike" <[email protected]>'] |
| 2447 |
|
| 2448 |
results = utils.parse.parse_emails( |
| 2449 |
'"Smith, Jane" <[email protected]>, Bob Jones <[email protected]>' |
| 2450 |
) |
| 2451 |
assert len(results) == 2 |
| 2452 |
assert '"Smith, Jane" <[email protected]>' in results |
| 2453 |
assert "Bob Jones <[email protected]>" in results |
| 2454 |
|
| 2455 |
|
| 2456 |
def test_parse_urls(): |
| 2457 |
"""utils: parse_urls() testing""" |
| 2458 |
# A simple single array entry (As str) |
| 2459 |
results = utils.parse.parse_urls("") |
| 2460 |
assert isinstance(results, list) |
| 2461 |
assert len(results) == 0 |
| 2462 |
|
| 2463 |
# just delimeters |
| 2464 |
results = utils.parse.parse_urls(", ,, , ,,, ") |
| 2465 |
assert isinstance(results, list) |
| 2466 |
assert len(results) == 0 |
| 2467 |
|
| 2468 |
results = utils.parse.parse_urls(",") |
| 2469 |
assert isinstance(results, list) |
| 2470 |
assert len(results) == 0 |
| 2471 |
|
| 2472 |
results = utils.parse.parse_urls(None) |
| 2473 |
assert isinstance(results, list) |
| 2474 |
assert len(results) == 0 |
| 2475 |
|
| 2476 |
results = utils.parse.parse_urls(42) |
| 2477 |
assert isinstance(results, list) |
| 2478 |
assert len(results) == 0 |
| 2479 |
|
| 2480 |
results = utils.parse.parse_urls("this is not a parseable url at all") |
| 2481 |
assert isinstance(results, list) |
| 2482 |
# we still end up returning this |
| 2483 |
assert len(results) == 8 |
| 2484 |
|
| 2485 |
results = utils.parse.parse_urls( |
| 2486 |
"this is not a parseable url at all", store_unparseable=False |
| 2487 |
) |
| 2488 |
assert isinstance(results, list) |
| 2489 |
assert len(results) == 0 |
| 2490 |
|
| 2491 |
# Now test valid URLs |
| 2492 |
results = utils.parse.parse_urls("windows://") |
| 2493 |
assert isinstance(results, list) |
| 2494 |
assert len(results) == 1 |
| 2495 |
assert "windows://" in results |
| 2496 |
|
| 2497 |
results = utils.parse.parse_urls("windows:// gnome://") |
| 2498 |
assert isinstance(results, list) |
| 2499 |
assert len(results) == 2 |
| 2500 |
assert "windows://" in results |
| 2501 |
assert "gnome://" in results |
| 2502 |
|
| 2503 |
# We don't want to parse out URLs that are part of another URL's arguments |
| 2504 |
results = utils.parse.parse_urls("discord://host?url=https://localhost") |
| 2505 |
assert isinstance(results, list) |
| 2506 |
assert len(results) == 1 |
| 2507 |
assert "discord://host?url=https://localhost" in results |
| 2508 |
|
| 2509 |
# Commas and spaces found inside URLs are ignored |
| 2510 |
urls = [ |
| 2511 |
( |
| 2512 |
"mailgun://[email protected]/apikey/" |
| 2513 |
"[email protected],[email protected],," |
| 2514 |
" [email protected]" |
| 2515 |
), |
| 2516 |
( |
| 2517 |
"mailgun://[email protected]/apikey/" |
| 2518 |
"[email protected],,[email protected],, [email protected], ," |
| 2519 |
), |
| 2520 |
"windows://", |
| 2521 |
] |
| 2522 |
|
| 2523 |
# Since comma's and whitespace are the delimiters; they won't be |
| 2524 |
# present at the end of the URL; so we just need to write a special |
| 2525 |
# rstrip() as a regular exression to handle whitespace (\s) and comma |
| 2526 |
# delimiter |
| 2527 |
rstrip_re = re.compile(r"[\s,]+$") |
| 2528 |
|
| 2529 |
# Since a comma acts as a delimiter, we run a risk of a problem where the |
| 2530 |
# comma exists as part of the URL and is therefore lost if it was found |
| 2531 |
# at the end of it. |
| 2532 |
|
| 2533 |
results = utils.parse.parse_urls(", ".join(urls)) |
| 2534 |
assert isinstance(results, list) |
| 2535 |
assert len(results) == len(urls) |
| 2536 |
for url in urls: |
| 2537 |
assert rstrip_re.sub("", url) in results |
| 2538 |
|
| 2539 |
# However if a comma is found at the end of a single url without a new |
| 2540 |
# match to hit, it is saved and not lost |
| 2541 |
|
| 2542 |
# The comma at the end of the password will not be lost if we're |
| 2543 |
# dealing with a single entry: |
| 2544 |
url = "http://hostname?password=,abcd," |
| 2545 |
results = utils.parse.parse_urls(url) |
| 2546 |
assert isinstance(results, list) |
| 2547 |
assert len(results) == 1 |
| 2548 |
assert url in results |
| 2549 |
|
| 2550 |
# however if we have multiple entries, commas and spaces between |
| 2551 |
# URLs will be lost, however the last URL will not lose the comma |
| 2552 |
urls = [ |
| 2553 |
"schema1://hostname?password=,abcd,", |
| 2554 |
"schema2://hostname?password=,abcd,", |
| 2555 |
] |
| 2556 |
results = utils.parse.parse_urls(", ".join(urls)) |
| 2557 |
assert isinstance(results, list) |
| 2558 |
assert len(results) == len(urls) |
| 2559 |
|
| 2560 |
# No match because the comma is gone in the results entry |
| 2561 |
# schema1://hostname?password=,abcd |
| 2562 |
assert urls[0] not in results |
| 2563 |
assert urls[0][:-1] in results |
| 2564 |
|
| 2565 |
# However we wouldn't have lost the comma in the second one: |
| 2566 |
# schema2://hostname?password=,abcd, |
| 2567 |
assert urls[1] in results |
| 2568 |
|
| 2569 |
# Pass the list in (as a list); results are the same |
| 2570 |
results = utils.parse.parse_urls(urls) |
| 2571 |
assert isinstance(results, list) |
| 2572 |
assert len(results) == len(urls) |
| 2573 |
|
| 2574 |
# Pass in garbage |
| 2575 |
results = utils.parse.parse_urls(object) |
| 2576 |
assert isinstance(results, list) |
| 2577 |
assert len(results) == 0 |
| 2578 |
|
| 2579 |
results = utils.parse.parse_urls(42) |
| 2580 |
assert isinstance(results, list) |
| 2581 |
assert len(results) == 0 |
| 2582 |
|
| 2583 |
results = utils.parse.parse_urls([None, object, 42]) |
| 2584 |
assert isinstance(results, list) |
| 2585 |
assert len(results) == 0 |
| 2586 |
|
| 2587 |
# Verify trailing-whitespace runs complete in reasonable time. |
| 2588 |
t0 = time.monotonic() |
| 2589 |
results = utils.parse.parse_urls("https://example.com" + " " * 50000) |
| 2590 |
assert time.monotonic() - t0 < 5.0 |
| 2591 |
assert results == ["https://example.com"] |
| 2592 |
|
| 2593 |
|
| 2594 |
def test_dict_full_update(): |
| 2595 |
"""utils: dict_full_update() testing""" |
| 2596 |
dict_1 = { |
| 2597 |
"a": 1, |
| 2598 |
"b": 2, |
| 2599 |
"c": 3, |
| 2600 |
"d": { |
| 2601 |
"z": 27, |
| 2602 |
"y": 26, |
| 2603 |
"x": 25, |
| 2604 |
}, |
| 2605 |
} |
| 2606 |
|
| 2607 |
dict_2 = { |
| 2608 |
"d": { |
| 2609 |
"x": "updated", |
| 2610 |
"w": 24, |
| 2611 |
}, |
| 2612 |
"c": "updated", |
| 2613 |
"e": 5, |
| 2614 |
} |
| 2615 |
utils.logic.dict_full_update(dict_1, dict_2) |
| 2616 |
# Dictionary 2 is untouched |
| 2617 |
assert len(dict_2) == 3 |
| 2618 |
assert dict_2["c"] == "updated" |
| 2619 |
assert dict_2["d"]["w"] == 24 |
| 2620 |
assert dict_2["d"]["x"] == "updated" |
| 2621 |
assert dict_2["e"] == 5 |
| 2622 |
|
| 2623 |
# Dictionary 3 however has entries from Dict 2 applied |
| 2624 |
# without disrupting entries that were not matched. |
| 2625 |
assert len(dict_1) == 5 |
| 2626 |
assert dict_1["a"] == 1 |
| 2627 |
assert dict_1["b"] == 2 |
| 2628 |
assert dict_1["c"] == "updated" |
| 2629 |
assert dict_1["d"]["w"] == 24 |
| 2630 |
assert dict_1["d"]["x"] == "updated" |
| 2631 |
assert dict_1["d"]["y"] == 26 |
| 2632 |
assert dict_1["d"]["z"] == 27 |
| 2633 |
assert dict_1["e"] == 5 |
| 2634 |
|
| 2635 |
|
| 2636 |
def test_parse_list(): |
| 2637 |
"""utils: parse_list() testing""" |
| 2638 |
|
| 2639 |
# A simple single array entry (As str) |
| 2640 |
results = utils.parse.parse_list( |
| 2641 |
".mkv,.avi,.divx,.xvid,.mov,.wmv,.mp4,.mpg,.mpeg,.vob,.iso" |
| 2642 |
) |
| 2643 |
|
| 2644 |
assert results == sorted( |
| 2645 |
[ |
| 2646 |
".divx", |
| 2647 |
".iso", |
| 2648 |
".mkv", |
| 2649 |
".mov", |
| 2650 |
".mpg", |
| 2651 |
".avi", |
| 2652 |
".mpeg", |
| 2653 |
".vob", |
| 2654 |
".xvid", |
| 2655 |
".wmv", |
| 2656 |
".mp4", |
| 2657 |
] |
| 2658 |
) |
| 2659 |
|
| 2660 |
class StrangeObject: |
| 2661 |
def __str__(self): |
| 2662 |
return ".avi" |
| 2663 |
|
| 2664 |
# Now 2 lists with lots of duplicates and other delimiters |
| 2665 |
results = utils.parse.parse_list( |
| 2666 |
".mkv,.avi,.divx,.xvid,.mov,.wmv,.mp4,.mpg .mpeg,.vob,,; ;", |
| 2667 |
(".mkv,.avi,.divx,.xvid,.mov ", " .wmv,.mp4;.mpg,.mpeg,"), |
| 2668 |
".vob,.iso", |
| 2669 |
[ |
| 2670 |
".vob", |
| 2671 |
[ |
| 2672 |
".vob", |
| 2673 |
".mkv", |
| 2674 |
StrangeObject(), |
| 2675 |
], |
| 2676 |
], |
| 2677 |
StrangeObject(), |
| 2678 |
) |
| 2679 |
|
| 2680 |
assert results == sorted( |
| 2681 |
[ |
| 2682 |
".divx", |
| 2683 |
".iso", |
| 2684 |
".mkv", |
| 2685 |
".mov", |
| 2686 |
".mpg", |
| 2687 |
".avi", |
| 2688 |
".mpeg", |
| 2689 |
".vob", |
| 2690 |
".xvid", |
| 2691 |
".wmv", |
| 2692 |
".mp4", |
| 2693 |
] |
| 2694 |
) |
| 2695 |
|
| 2696 |
# Garbage in is removed |
| 2697 |
assert utils.parse.parse_list(object(), 42, None) == [] |
| 2698 |
|
| 2699 |
# Now a list with extras we want to add as strings |
| 2700 |
# empty entries are removed |
| 2701 |
results = utils.parse.parse_list( |
| 2702 |
[ |
| 2703 |
".divx", |
| 2704 |
".iso", |
| 2705 |
".mkv", |
| 2706 |
".mov", |
| 2707 |
"", |
| 2708 |
" ", |
| 2709 |
".avi", |
| 2710 |
".mpeg", |
| 2711 |
".vob", |
| 2712 |
".xvid", |
| 2713 |
".mp4", |
| 2714 |
], |
| 2715 |
".mov,.wmv,.mp4,.mpg", |
| 2716 |
) |
| 2717 |
|
| 2718 |
assert results == sorted( |
| 2719 |
[ |
| 2720 |
".divx", |
| 2721 |
".wmv", |
| 2722 |
".iso", |
| 2723 |
".mkv", |
| 2724 |
".mov", |
| 2725 |
".mpg", |
| 2726 |
".avi", |
| 2727 |
".vob", |
| 2728 |
".xvid", |
| 2729 |
".mpeg", |
| 2730 |
".mp4", |
| 2731 |
] |
| 2732 |
) |
| 2733 |
|
| 2734 |
|
| 2735 |
def test_import_module(tmpdir): |
| 2736 |
"""utils: import_module testing""" |
| 2737 |
# Prepare ourselves a file to work with |
| 2738 |
bad_file_base = tmpdir.mkdir("a") |
| 2739 |
bad_file = bad_file_base.join("README.md") |
| 2740 |
bad_file.write( |
| 2741 |
cleandoc(""" |
| 2742 |
I'm a README file, not a Python one. |
| 2743 |
|
| 2744 |
I can't be loaded |
| 2745 |
""") |
| 2746 |
) |
| 2747 |
assert utils.module.import_module(str(bad_file), "invalidfile1") is None |
| 2748 |
assert ( |
| 2749 |
utils.module.import_module(str(bad_file_base), "invalidfile2") is None |
| 2750 |
) |
| 2751 |
|
| 2752 |
|
| 2753 |
def test_module_detection(tmpdir): |
| 2754 |
"""utils: test_module_detection() testing""" |
| 2755 |
|
| 2756 |
# Clear our working variables so they don't obstruct with the tests here |
| 2757 |
N_MGR._paths_previously_scanned.clear() |
| 2758 |
N_MGR._custom_module_map.clear() |
| 2759 |
|
| 2760 |
# Test case where we load invalid data |
| 2761 |
N_MGR.module_detection(None) |
| 2762 |
|
| 2763 |
# Invalid data does not load anything |
| 2764 |
assert len(N_MGR._paths_previously_scanned) == 0 |
| 2765 |
assert len(N_MGR._custom_module_map) == 0 |
| 2766 |
|
| 2767 |
# Prepare ourselves a file to work with |
| 2768 |
notify_hook_a_base = tmpdir.mkdir("a") |
| 2769 |
notify_hook_a = notify_hook_a_base.join("hook.py") |
| 2770 |
notify_hook_a.write( |
| 2771 |
cleandoc(""" |
| 2772 |
from apprise.decorators import notify |
| 2773 |
|
| 2774 |
@notify(on="clihook") |
| 2775 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 2776 |
pass |
| 2777 |
""") |
| 2778 |
) |
| 2779 |
|
| 2780 |
notify_ignore = notify_hook_a_base.join("README.md") |
| 2781 |
notify_ignore.write( |
| 2782 |
cleandoc(""" |
| 2783 |
We're not a .py file, so this file gets gracefully skipped |
| 2784 |
""") |
| 2785 |
) |
| 2786 |
|
| 2787 |
# Not previously loaded |
| 2788 |
assert "clihook" not in N_MGR |
| 2789 |
|
| 2790 |
# load entry by string |
| 2791 |
N_MGR.module_detection(str(notify_hook_a)) |
| 2792 |
N_MGR.module_detection(str(notify_ignore)) |
| 2793 |
N_MGR.module_detection(str(notify_hook_a_base)) |
| 2794 |
|
| 2795 |
assert len(N_MGR._paths_previously_scanned) == 3 |
| 2796 |
assert len(N_MGR._custom_module_map) == 1 |
| 2797 |
|
| 2798 |
# Now loaded |
| 2799 |
assert "clihook" in N_MGR |
| 2800 |
|
| 2801 |
# load entry by array |
| 2802 |
N_MGR.module_detection([str(notify_hook_a)]) |
| 2803 |
|
| 2804 |
# No changes to our path |
| 2805 |
assert len(N_MGR._paths_previously_scanned) == 3 |
| 2806 |
assert len(N_MGR._custom_module_map) == 1 |
| 2807 |
|
| 2808 |
# Reset our variables for the next test |
| 2809 |
N_MGR._paths_previously_scanned.clear() |
| 2810 |
N_MGR._custom_module_map.clear() |
| 2811 |
|
| 2812 |
# Hidden files are ignored |
| 2813 |
notify_hook_b_base = tmpdir.mkdir("b") |
| 2814 |
notify_hook_b = notify_hook_b_base.join(".hook.py") |
| 2815 |
notify_hook_b.write( |
| 2816 |
cleandoc(""" |
| 2817 |
from apprise.decorators import notify |
| 2818 |
|
| 2819 |
# this is in a hidden file so it will not load |
| 2820 |
@notify(on="hidden") |
| 2821 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 2822 |
pass |
| 2823 |
""") |
| 2824 |
) |
| 2825 |
|
| 2826 |
assert "hidden" not in N_MGR |
| 2827 |
|
| 2828 |
N_MGR.module_detection([str(notify_hook_b)]) |
| 2829 |
|
| 2830 |
# Verify that it did not load |
| 2831 |
assert "hidden" not in N_MGR |
| 2832 |
|
| 2833 |
# Path was scanned; nothing loaded |
| 2834 |
assert len(N_MGR._paths_previously_scanned) == 1 |
| 2835 |
assert len(N_MGR._custom_module_map) == 0 |
| 2836 |
|
| 2837 |
# Reset our variables for the next test |
| 2838 |
N_MGR._paths_previously_scanned.clear() |
| 2839 |
N_MGR._custom_module_map.clear() |
| 2840 |
|
| 2841 |
# modules with no hooks found are ignored |
| 2842 |
notify_hook_c_base = tmpdir.mkdir("c") |
| 2843 |
notify_hook_c = notify_hook_c_base.join("empty.py") |
| 2844 |
notify_hook_c.write("") |
| 2845 |
|
| 2846 |
N_MGR.module_detection([str(notify_hook_c)]) |
| 2847 |
|
| 2848 |
# File was found, no custom modules |
| 2849 |
assert len(N_MGR._paths_previously_scanned) == 1 |
| 2850 |
assert len(N_MGR._custom_module_map) == 0 |
| 2851 |
|
| 2852 |
# A new path scanned |
| 2853 |
N_MGR.module_detection([str(notify_hook_c_base)]) |
| 2854 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2855 |
assert len(N_MGR._custom_module_map) == 0 |
| 2856 |
|
| 2857 |
def create_hook(tdir, cache=True, on="valid1"): |
| 2858 |
"""Just a temporary hook creation tool for writing a working notify |
| 2859 |
hook.""" |
| 2860 |
tdir.write( |
| 2861 |
cleandoc(f""" |
| 2862 |
from apprise.decorators import notify |
| 2863 |
|
| 2864 |
# this is a good hook but burried in hidden directory which won't |
| 2865 |
# be accessed unless the file is pointed to via absolute path |
| 2866 |
@notify(on="{on}") |
| 2867 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 2868 |
pass |
| 2869 |
""") |
| 2870 |
) |
| 2871 |
|
| 2872 |
N_MGR.module_detection([str(tdir)], cache=cache) |
| 2873 |
|
| 2874 |
create_hook(notify_hook_c, on="valid1") |
| 2875 |
assert "valid1" not in N_MGR |
| 2876 |
|
| 2877 |
# Even if we correct our empty file; the fact the directory has been |
| 2878 |
# scanned and failed to load (same with file), it won't be loaded |
| 2879 |
# a second time. This is intentional since module_detection() get's |
| 2880 |
# called for every AppriseAsset() object creation. This prevents us |
| 2881 |
# from reloading conent over and over again wasting resources |
| 2882 |
assert "valid1" not in N_MGR |
| 2883 |
N_MGR.module_detection([str(notify_hook_c)]) |
| 2884 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2885 |
assert len(N_MGR._custom_module_map) == 0 |
| 2886 |
|
| 2887 |
# Even by absolute path... |
| 2888 |
N_MGR.module_detection([str(notify_hook_c)]) |
| 2889 |
assert "valid1" not in N_MGR |
| 2890 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2891 |
assert len(N_MGR._custom_module_map) == 0 |
| 2892 |
|
| 2893 |
# However we can bypass the cache if we really want to |
| 2894 |
N_MGR.module_detection([str(notify_hook_c_base)], cache=False) |
| 2895 |
assert "valid1" in N_MGR |
| 2896 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2897 |
assert len(N_MGR._custom_module_map) == 1 |
| 2898 |
|
| 2899 |
# Bypassing it twice causes the module to load twice (not very efficient) |
| 2900 |
# However we can bypass the cache if we really want to |
| 2901 |
N_MGR.module_detection([str(notify_hook_c_base)], cache=False) |
| 2902 |
assert "valid1" in N_MGR |
| 2903 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2904 |
assert len(N_MGR._custom_module_map) == 1 |
| 2905 |
|
| 2906 |
# If you update the module (corrupting it in the process and reload) |
| 2907 |
notify_hook_c.write( |
| 2908 |
cleandoc(""" |
| 2909 |
raise ValueError |
| 2910 |
""") |
| 2911 |
) |
| 2912 |
|
| 2913 |
# Force no cache to cause the file to be replaced |
| 2914 |
N_MGR.module_detection([str(notify_hook_c_base)], cache=False) |
| 2915 |
|
| 2916 |
# Our valid entry is no longer loaded |
| 2917 |
assert "valid1" not in N_MGR |
| 2918 |
|
| 2919 |
# No change to scanned paths |
| 2920 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2921 |
# The previously loaded module is now gone |
| 2922 |
assert len(N_MGR._custom_module_map) == 0 |
| 2923 |
|
| 2924 |
# Reload our valid1 entry |
| 2925 |
create_hook(notify_hook_c, on="valid1", cache=False) |
| 2926 |
assert "valid1" in N_MGR |
| 2927 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2928 |
assert len(N_MGR._custom_module_map) == 1 |
| 2929 |
|
| 2930 |
# Prepare an empty file |
| 2931 |
notify_hook_c.write("") |
| 2932 |
N_MGR.module_detection([str(notify_hook_c_base)], cache=False) |
| 2933 |
|
| 2934 |
# Our valid entry is no longer loaded |
| 2935 |
assert "valid1" not in N_MGR |
| 2936 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2937 |
assert len(N_MGR._custom_module_map) == 0 |
| 2938 |
|
| 2939 |
# Now reload our module again (this time rather then an exception, the |
| 2940 |
# module is read back and swaps `valid1` for `valid2` |
| 2941 |
create_hook(notify_hook_c, on="valid1", cache=False) |
| 2942 |
assert "valid1" in N_MGR |
| 2943 |
assert "valid2" not in N_MGR |
| 2944 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2945 |
assert len(N_MGR._custom_module_map) == 1 |
| 2946 |
|
| 2947 |
create_hook(notify_hook_c, on="valid2", cache=False) |
| 2948 |
assert "valid1" not in N_MGR |
| 2949 |
assert "valid2" in N_MGR |
| 2950 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2951 |
assert len(N_MGR._custom_module_map) == 1 |
| 2952 |
|
| 2953 |
# Reset our variables for the next test |
| 2954 |
create_hook(notify_hook_c, on="valid1", cache=False) |
| 2955 |
del N_MGR["valid1"] |
| 2956 |
N_MGR._paths_previously_scanned.clear() |
| 2957 |
N_MGR._custom_module_map.clear() |
| 2958 |
|
| 2959 |
notify_hook_d = notify_hook_c_base.join(".ignore.py") |
| 2960 |
notify_hook_d.write("") |
| 2961 |
notify_hook_e_base = notify_hook_c_base.mkdir(".ignore") |
| 2962 |
notify_hook_e = notify_hook_e_base.join("__init__.py") |
| 2963 |
notify_hook_e.write( |
| 2964 |
cleandoc(""" |
| 2965 |
from apprise.decorators import notify |
| 2966 |
|
| 2967 |
# this is a good hook but burried in hidden directory which won't |
| 2968 |
# be accessed unless the file is pointed to via absolute path |
| 2969 |
@notify(on="valid2") |
| 2970 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 2971 |
pass |
| 2972 |
""") |
| 2973 |
) |
| 2974 |
|
| 2975 |
# Try to load our base directory again; this time we search by the |
| 2976 |
# directory; the only edge case we're testing here is it will not |
| 2977 |
# even look at the .ignore.py file found since it is invalid |
| 2978 |
N_MGR.module_detection([str(notify_hook_c_base)]) |
| 2979 |
assert "valid1" in N_MGR |
| 2980 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 2981 |
assert len(N_MGR._custom_module_map) == 1 |
| 2982 |
|
| 2983 |
# Reset our variables for the next test |
| 2984 |
del N_MGR._schema_map["valid1"] |
| 2985 |
N_MGR._paths_previously_scanned.clear() |
| 2986 |
N_MGR._custom_module_map.clear() |
| 2987 |
|
| 2988 |
# Try to load our base directory again |
| 2989 |
N_MGR.module_detection([str(notify_hook_c)]) |
| 2990 |
assert "valid1" in N_MGR |
| 2991 |
|
| 2992 |
# Hidden directories are not scanned |
| 2993 |
assert "valid2" not in N_MGR |
| 2994 |
|
| 2995 |
assert len(N_MGR._paths_previously_scanned) == 1 |
| 2996 |
assert str(notify_hook_c) in N_MGR._paths_previously_scanned |
| 2997 |
assert len(N_MGR._custom_module_map) == 1 |
| 2998 |
|
| 2999 |
# However a direct reference to the hidden directory is okay |
| 3000 |
N_MGR.module_detection([str(notify_hook_e_base)]) |
| 3001 |
|
| 3002 |
# We loaded our module |
| 3003 |
assert "valid2" in N_MGR |
| 3004 |
assert len(N_MGR._paths_previously_scanned) == 3 |
| 3005 |
assert str(notify_hook_c) in N_MGR._paths_previously_scanned |
| 3006 |
assert str(notify_hook_e) in N_MGR._paths_previously_scanned |
| 3007 |
assert str(notify_hook_e_base) in N_MGR._paths_previously_scanned |
| 3008 |
assert len(N_MGR._custom_module_map) == 2 |
| 3009 |
|
| 3010 |
# Reset our variables for the next test |
| 3011 |
del N_MGR._schema_map["valid1"] |
| 3012 |
del N_MGR._schema_map["valid2"] |
| 3013 |
N_MGR._paths_previously_scanned.clear() |
| 3014 |
N_MGR._custom_module_map.clear() |
| 3015 |
|
| 3016 |
# Load our file directly |
| 3017 |
assert "valid2" not in N_MGR |
| 3018 |
N_MGR.module_detection([str(notify_hook_e)]) |
| 3019 |
|
| 3020 |
# Now we have it loaded as expected |
| 3021 |
assert "valid2" in N_MGR |
| 3022 |
assert len(N_MGR._paths_previously_scanned) == 1 |
| 3023 |
assert str(notify_hook_e) in N_MGR._paths_previously_scanned |
| 3024 |
assert len(N_MGR._custom_module_map) == 1 |
| 3025 |
|
| 3026 |
# however if we try to load the base directory where the __init__.py |
| 3027 |
# was already loaded from, it will not change anything |
| 3028 |
N_MGR.module_detection([str(notify_hook_e_base)]) |
| 3029 |
assert "valid2" in N_MGR |
| 3030 |
assert len(N_MGR._paths_previously_scanned) == 2 |
| 3031 |
assert str(notify_hook_e) in N_MGR._paths_previously_scanned |
| 3032 |
assert str(notify_hook_e_base) in N_MGR._paths_previously_scanned |
| 3033 |
assert len(N_MGR._custom_module_map) == 1 |
| 3034 |
|
| 3035 |
# Tidy up for the next test |
| 3036 |
del N_MGR._schema_map["valid2"] |
| 3037 |
N_MGR._paths_previously_scanned.clear() |
| 3038 |
N_MGR._custom_module_map.clear() |
| 3039 |
|
| 3040 |
assert "valid1" not in N_MGR |
| 3041 |
assert "valid2" not in N_MGR |
| 3042 |
assert "valid3" not in N_MGR |
| 3043 |
notify_hook_f_base = tmpdir.mkdir("f") |
| 3044 |
notify_hook_f = notify_hook_f_base.join("invalid.py") |
| 3045 |
notify_hook_f.write( |
| 3046 |
cleandoc(""" |
| 3047 |
from apprise.decorators import notify |
| 3048 |
|
| 3049 |
# A very invalid hook type... on should not be None |
| 3050 |
@notify(on=None) |
| 3051 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 3052 |
pass |
| 3053 |
|
| 3054 |
# An invalid name |
| 3055 |
@notify(on='valid1', name=None) |
| 3056 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 3057 |
pass |
| 3058 |
|
| 3059 |
# Another invalid name (so it's ignored) |
| 3060 |
@notify(on='valid2', name=object) |
| 3061 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 3062 |
pass |
| 3063 |
|
| 3064 |
# Simply put... the name has to be a string to be referenced |
| 3065 |
# however this will still be loaded |
| 3066 |
@notify(on='valid3', name=4) |
| 3067 |
def mywrapper(body, title, notify_type, *args, **kwargs): |
| 3068 |
pass |
| 3069 |
|
| 3070 |
""") |
| 3071 |
) |
| 3072 |
|
| 3073 |
N_MGR.module_detection([str(notify_hook_f)]) |
| 3074 |
|
| 3075 |
assert len(N_MGR._paths_previously_scanned) == 1 |
| 3076 |
assert len(N_MGR._custom_module_map) == 1 |
| 3077 |
assert "valid1" in N_MGR |
| 3078 |
assert "valid2" in N_MGR |
| 3079 |
assert "valid3" in N_MGR |
| 3080 |
|
| 3081 |
# Reset our variables for the next test |
| 3082 |
del N_MGR._schema_map["valid1"] |
| 3083 |
del N_MGR._schema_map["valid2"] |
| 3084 |
del N_MGR._schema_map["valid3"] |
| 3085 |
N_MGR._paths_previously_scanned.clear() |
| 3086 |
N_MGR._custom_module_map.clear() |
| 3087 |
|
| 3088 |
# Now test the handling of just bad data entirely |
| 3089 |
notify_hook_g_base = tmpdir.mkdir("g") |
| 3090 |
notify_hook_g = notify_hook_g_base.join("binary.py") |
| 3091 |
with open(str(notify_hook_g), "wb") as fout: |
| 3092 |
fout.write(os.urandom(512)) |
| 3093 |
|
| 3094 |
N_MGR.module_detection([str(notify_hook_g)]) |
| 3095 |
assert len(N_MGR._paths_previously_scanned) == 1 |
| 3096 |
assert len(N_MGR._custom_module_map) == 0 |
| 3097 |
|
| 3098 |
# Reset our variables before we leave |
| 3099 |
N_MGR._paths_previously_scanned.clear() |
| 3100 |
N_MGR._custom_module_map.clear() |
| 3101 |
|
| 3102 |
|
| 3103 |
def test_exclusive_match(): |
| 3104 |
"""utils: is_exclusive_match() testing""" |
| 3105 |
|
| 3106 |
# No Logic always returns True if there is also no data |
| 3107 |
assert utils.logic.is_exclusive_match(data=None, logic=None) is True |
| 3108 |
assert utils.logic.is_exclusive_match(data=None, logic=set()) is True |
| 3109 |
assert utils.logic.is_exclusive_match(data="", logic=set()) is True |
| 3110 |
assert utils.logic.is_exclusive_match(data="", logic=set()) is True |
| 3111 |
|
| 3112 |
# however, once data is introduced, True is no longer returned |
| 3113 |
# if no logic has been specified |
| 3114 |
assert utils.logic.is_exclusive_match(data="check", logic=set()) is False |
| 3115 |
assert ( |
| 3116 |
utils.logic.is_exclusive_match(data=["check", "checkb"], logic=set()) |
| 3117 |
is False |
| 3118 |
) |
| 3119 |
|
| 3120 |
# String delimters are stripped out so that a list can be formed |
| 3121 |
# the below is just an empty token list |
| 3122 |
assert utils.logic.is_exclusive_match(data=set(), logic=",; ,") is True |
| 3123 |
|
| 3124 |
# garbage logic is never an exclusive match |
| 3125 |
assert utils.logic.is_exclusive_match(data=set(), logic=object()) is False |
| 3126 |
assert ( |
| 3127 |
utils.logic.is_exclusive_match( |
| 3128 |
data=set(), |
| 3129 |
logic=[ |
| 3130 |
object(), |
| 3131 |
], |
| 3132 |
) |
| 3133 |
is False |
| 3134 |
) |
| 3135 |
|
| 3136 |
# |
| 3137 |
# Test with logic: |
| 3138 |
# |
| 3139 |
data = {"abc"} |
| 3140 |
|
| 3141 |
# def in data |
| 3142 |
assert utils.logic.is_exclusive_match(logic="def", data=data) is False |
| 3143 |
# def in data |
| 3144 |
assert ( |
| 3145 |
utils.logic.is_exclusive_match( |
| 3146 |
logic=[ |
| 3147 |
"def", |
| 3148 |
], |
| 3149 |
data=data, |
| 3150 |
) |
| 3151 |
is False |
| 3152 |
) |
| 3153 |
# def in data |
| 3154 |
assert utils.logic.is_exclusive_match(logic=("def",), data=data) is False |
| 3155 |
# def in data |
| 3156 |
assert ( |
| 3157 |
utils.logic.is_exclusive_match( |
| 3158 |
logic={ |
| 3159 |
"def", |
| 3160 |
}, |
| 3161 |
data=data, |
| 3162 |
) |
| 3163 |
is False |
| 3164 |
) |
| 3165 |
# abc in data |
| 3166 |
assert ( |
| 3167 |
utils.logic.is_exclusive_match( |
| 3168 |
logic=[ |
| 3169 |
"abc", |
| 3170 |
], |
| 3171 |
data=data, |
| 3172 |
) |
| 3173 |
is True |
| 3174 |
) |
| 3175 |
# abc in data |
| 3176 |
assert utils.logic.is_exclusive_match(logic=("abc",), data=data) is True |
| 3177 |
# abc in data |
| 3178 |
assert ( |
| 3179 |
utils.logic.is_exclusive_match( |
| 3180 |
logic={ |
| 3181 |
"abc", |
| 3182 |
}, |
| 3183 |
data=data, |
| 3184 |
) |
| 3185 |
is True |
| 3186 |
) |
| 3187 |
# abc or def in data |
| 3188 |
assert utils.logic.is_exclusive_match(logic="abc, def", data=data) is True |
| 3189 |
|
| 3190 |
# |
| 3191 |
# Update our data set so we can do more advance checks |
| 3192 |
# |
| 3193 |
data = {"abc", "def", "efg", "xyz"} |
| 3194 |
|
| 3195 |
# match_all matches everything |
| 3196 |
assert utils.logic.is_exclusive_match(logic="all", data=data) is True |
| 3197 |
assert utils.logic.is_exclusive_match(logic=["all"], data=data) is True |
| 3198 |
|
| 3199 |
# def and abc in data |
| 3200 |
assert ( |
| 3201 |
utils.logic.is_exclusive_match(logic=[("abc", "def")], data=data) |
| 3202 |
is True |
| 3203 |
) |
| 3204 |
|
| 3205 |
# cba and abc in data |
| 3206 |
assert ( |
| 3207 |
utils.logic.is_exclusive_match(logic=[("cba", "abc")], data=data) |
| 3208 |
is False |
| 3209 |
) |
| 3210 |
|
| 3211 |
# www or zzz or abc and xyz |
| 3212 |
assert ( |
| 3213 |
utils.logic.is_exclusive_match( |
| 3214 |
logic=["www", "zzz", ("abc", "xyz")], data=data |
| 3215 |
) |
| 3216 |
is True |
| 3217 |
) |
| 3218 |
# www or zzz or abc and xyz (strings are valid too) |
| 3219 |
assert ( |
| 3220 |
utils.logic.is_exclusive_match( |
| 3221 |
logic=["www", "zzz", "abc, xyz"], data=data |
| 3222 |
) |
| 3223 |
is True |
| 3224 |
) |
| 3225 |
|
| 3226 |
# www or zzz or abc and jjj |
| 3227 |
assert ( |
| 3228 |
utils.logic.is_exclusive_match( |
| 3229 |
logic=["www", "zzz", ("abc", "jjj")], data=data |
| 3230 |
) |
| 3231 |
is False |
| 3232 |
) |
| 3233 |
|
| 3234 |
# |
| 3235 |
# Empty data set |
| 3236 |
# |
| 3237 |
data = set() |
| 3238 |
assert utils.logic.is_exclusive_match(logic=["www"], data=data) is False |
| 3239 |
assert utils.logic.is_exclusive_match(logic="all", data=data) is True |
| 3240 |
|
| 3241 |
# |
| 3242 |
# Update our data set so we can do more advance checks |
| 3243 |
# |
| 3244 |
data = {"always", "entry1"} |
| 3245 |
# We'll always match on the with keyword always |
| 3246 |
assert utils.logic.is_exclusive_match(logic="always", data=data) is True |
| 3247 |
assert utils.logic.is_exclusive_match(logic="garbage", data=data) is True |
| 3248 |
# However we will not match if we turn this feature off |
| 3249 |
assert ( |
| 3250 |
utils.logic.is_exclusive_match( |
| 3251 |
logic="garbage", data=data, match_always=False |
| 3252 |
) |
| 3253 |
is False |
| 3254 |
) |
| 3255 |
|
| 3256 |
# Change default value from 'all' to 'match_me'. Logic matches |
| 3257 |
# so we pass |
| 3258 |
assert ( |
| 3259 |
utils.logic.is_exclusive_match( |
| 3260 |
logic="match_me", data=data, match_all="match_me" |
| 3261 |
) |
| 3262 |
is True |
| 3263 |
) |
| 3264 |
|
| 3265 |
|
| 3266 |
def test_exclusive_match_priority_tokens(): |
| 3267 |
"""utils: is_exclusive_match() with priority-prefixed filter tokens""" |
| 3268 |
|
| 3269 |
# AppriseTag.__hash__ is name-only, so a set holds at most one entry per |
| 3270 |
# tag name -- matching real usage where each URL has one priority. |
| 3271 |
tag_ep3 = AppriseTag("endpoint", priority=3, has_priority=True) |
| 3272 |
tag_other = AppriseTag("other", priority=1, has_priority=True) |
| 3273 |
|
| 3274 |
data = {tag_ep3, tag_other} |
| 3275 |
|
| 3276 |
# Exact priority match |
| 3277 |
assert ( |
| 3278 |
utils.logic.is_exclusive_match(logic="3:endpoint", data=data) is True |
| 3279 |
) |
| 3280 |
|
| 3281 |
# Priority mismatch -- "2:endpoint" does not match priority-3 entry |
| 3282 |
assert ( |
| 3283 |
utils.logic.is_exclusive_match(logic="2:endpoint", data=data) is False |
| 3284 |
) |
| 3285 |
|
| 3286 |
# Name-only filter matches via O(1) hash path regardless of stored priority |
| 3287 |
assert utils.logic.is_exclusive_match(logic="endpoint", data=data) is True |
| 3288 |
|
| 3289 |
# Absent tag returns False |
| 3290 |
assert utils.logic.is_exclusive_match(logic="absent", data=data) is False |
| 3291 |
|
| 3292 |
# AND group: both tokens present with correct priorities |
| 3293 |
assert ( |
| 3294 |
utils.logic.is_exclusive_match( |
| 3295 |
logic=[("3:endpoint", "1:other")], data=data |
| 3296 |
) |
| 3297 |
is True |
| 3298 |
) |
| 3299 |
|
| 3300 |
# AND group: one priority mismatch fails the whole group |
| 3301 |
assert ( |
| 3302 |
utils.logic.is_exclusive_match( |
| 3303 |
logic=[("2:endpoint", "1:other")], data=data |
| 3304 |
) |
| 3305 |
is False |
| 3306 |
) |
| 3307 |
|
| 3308 |
# OR: failing AND group rescued by a matching single token |
| 3309 |
assert ( |
| 3310 |
utils.logic.is_exclusive_match( |
| 3311 |
logic=[("2:endpoint", "1:other"), "3:endpoint"], data=data |
| 3312 |
) |
| 3313 |
is True |
| 3314 |
) |
| 3315 |
|
| 3316 |
# OR: no term matches -- all fail |
| 3317 |
assert ( |
| 3318 |
utils.logic.is_exclusive_match( |
| 3319 |
logic=[("2:endpoint", "1:other"), "99:endpoint"], data=data |
| 3320 |
) |
| 3321 |
is False |
| 3322 |
) |
| 3323 |
|
| 3324 |
# Backward compat: plain strings in data matched by a priority-prefixed |
| 3325 |
# filter token (the for-loop fallback in _token_matches_data) |
| 3326 |
str_data = {"endpoint", "other"} |
| 3327 |
assert ( |
| 3328 |
utils.logic.is_exclusive_match(logic="7:endpoint", data=str_data) |
| 3329 |
is True |
| 3330 |
) |
| 3331 |
assert ( |
| 3332 |
utils.logic.is_exclusive_match(logic="7:absent", data=str_data) |
| 3333 |
is False |
| 3334 |
) |
| 3335 |
|
| 3336 |
# match_all keyword short-circuits even with priority data |
| 3337 |
assert utils.logic.is_exclusive_match(logic="all", data=data) is True |
| 3338 |
|
| 3339 |
|
| 3340 |
def test_apprise_validate_regex(): |
| 3341 |
""" |
| 3342 |
API: Apprise() Validate Regex tests |
| 3343 |
|
| 3344 |
""" |
| 3345 |
assert utils.parse.validate_regex(None) is None |
| 3346 |
assert utils.parse.validate_regex(object) is None |
| 3347 |
assert utils.parse.validate_regex(42) is None |
| 3348 |
assert utils.parse.validate_regex("") is None |
| 3349 |
assert utils.parse.validate_regex(" ") is None |
| 3350 |
assert utils.parse.validate_regex("abc") == "abc" |
| 3351 |
|
| 3352 |
# value is a keyword that is extracted (if found) |
| 3353 |
assert ( |
| 3354 |
utils.parse.validate_regex( |
| 3355 |
"- abcd -", r"-(?P<value>[^-]+)-", fmt="{value}" |
| 3356 |
) |
| 3357 |
== "abcd" |
| 3358 |
) |
| 3359 |
assert ( |
| 3360 |
utils.parse.validate_regex( |
| 3361 |
"- abcd -", r"-(?P<value>[^-]+)-", strip=False, fmt="{value}" |
| 3362 |
) |
| 3363 |
== " abcd " |
| 3364 |
) |
| 3365 |
|
| 3366 |
# String flags supported in addition to numeric |
| 3367 |
assert ( |
| 3368 |
utils.parse.validate_regex( |
| 3369 |
"- abcd -", r"-(?P<value>[^-]+)-", "i", fmt="{value}" |
| 3370 |
) |
| 3371 |
== "abcd" |
| 3372 |
) |
| 3373 |
assert ( |
| 3374 |
utils.parse.validate_regex( |
| 3375 |
"- abcd -", r"-(?P<value>[^-]+)-", re.I, fmt="{value}" |
| 3376 |
) |
| 3377 |
== "abcd" |
| 3378 |
) |
| 3379 |
|
| 3380 |
# Test multiple flag settings |
| 3381 |
assert ( |
| 3382 |
utils.parse.validate_regex( |
| 3383 |
"- abcd -", r"-(?P<value>[^-]+)-", "isax", fmt="{value}" |
| 3384 |
) |
| 3385 |
== "abcd" |
| 3386 |
) |
| 3387 |
|
| 3388 |
# Invalid flags are just ignored. The below fails to match |
| 3389 |
# because the default value of 'i' is over-ridden by what is |
| 3390 |
# identfied below, and no flag is set at the end of the day |
| 3391 |
assert ( |
| 3392 |
utils.parse.validate_regex( |
| 3393 |
"- abcd -", r"-(?P<value>[ABCD]+)-", "-%2gb", fmt="{value}" |
| 3394 |
) |
| 3395 |
is None |
| 3396 |
) |
| 3397 |
assert ( |
| 3398 |
utils.parse.validate_regex( |
| 3399 |
"- abcd -", r"-(?P<value>[ABCD]+)-", "", fmt="{value}" |
| 3400 |
) |
| 3401 |
is None |
| 3402 |
) |
| 3403 |
assert ( |
| 3404 |
utils.parse.validate_regex( |
| 3405 |
"- abcd -", r"-(?P<value>[ABCD]+)-", None, fmt="{value}" |
| 3406 |
) |
| 3407 |
is None |
| 3408 |
) |
| 3409 |
|
| 3410 |
|
| 3411 |
def test_apply_templating(): |
| 3412 |
"""utils: apply_template() testing""" |
| 3413 |
|
| 3414 |
template = "Hello {{fname}}, How are you {{whence}}?" |
| 3415 |
|
| 3416 |
result = utils.templates.apply_template( |
| 3417 |
template, **{"fname": "Chris", "whence": "this morning"} |
| 3418 |
) |
| 3419 |
assert isinstance(result, str) |
| 3420 |
assert result == "Hello Chris, How are you this morning?" |
| 3421 |
|
| 3422 |
# In this example 'whence' isn't provided, so it isn't swapped |
| 3423 |
result = utils.templates.apply_template(template, **{"fname": "Chris"}) |
| 3424 |
assert isinstance(result, str) |
| 3425 |
assert result == "Hello Chris, How are you {{whence}}?" |
| 3426 |
|
| 3427 |
# white space won't cause any ill affects: |
| 3428 |
template = "Hello {{ fname }}, How are you {{ whence}}?" |
| 3429 |
result = utils.templates.apply_template( |
| 3430 |
template, **{"fname": "Chris", "whence": "this morning"} |
| 3431 |
) |
| 3432 |
assert isinstance(result, str) |
| 3433 |
assert result == "Hello Chris, How are you this morning?" |
| 3434 |
|
| 3435 |
# No arguments won't cause any problems |
| 3436 |
template = "Hello {{fname}}, How are you {{whence}}?" |
| 3437 |
result = utils.templates.apply_template(template) |
| 3438 |
assert isinstance(result, str) |
| 3439 |
assert result == template |
| 3440 |
|
| 3441 |
# Wrong elements are simply ignored |
| 3442 |
result = utils.templates.apply_template( |
| 3443 |
template, **{"fname": "l2g", "whence": "this evening", "ignore": "me"} |
| 3444 |
) |
| 3445 |
assert isinstance(result, str) |
| 3446 |
assert result == "Hello l2g, How are you this evening?" |
| 3447 |
|
| 3448 |
# Empty template makes things easy |
| 3449 |
result = utils.templates.apply_template( |
| 3450 |
"", **{"fname": "l2g", "whence": "this evening"} |
| 3451 |
) |
| 3452 |
assert isinstance(result, str) |
| 3453 |
assert result == "" |
| 3454 |
|
| 3455 |
# Regular expressions are safely escapped and act as normal |
| 3456 |
# tokens: |
| 3457 |
template = "Hello {{.*}}, How are you {{[A-Z0-9]+}}?" |
| 3458 |
result = utils.templates.apply_template( |
| 3459 |
template, **{".*": "l2g", "[A-Z0-9]+": "this afternoon"} |
| 3460 |
) |
| 3461 |
assert result == "Hello l2g, How are you this afternoon?" |
| 3462 |
|
| 3463 |
# JSON is handled too such as escaping quotes |
| 3464 |
template = '{value: "{{ value }}"}' |
| 3465 |
result = utils.templates.apply_template( |
| 3466 |
template, |
| 3467 |
app_mode=utils.templates.TemplateType.JSON, |
| 3468 |
**{"value": '"quotes are escaped"'}, |
| 3469 |
) |
| 3470 |
assert result == '{value: "\\"quotes are escaped\\""}' |
| 3471 |
|
| 3472 |
|
| 3473 |
def test_cwe312_word(): |
| 3474 |
"""utils: cwe312_word() testing""" |
| 3475 |
assert utils.cwe312.cwe312_word(None) is None |
| 3476 |
assert utils.cwe312.cwe312_word(42) == 42 |
| 3477 |
assert utils.cwe312.cwe312_word("") == "" |
| 3478 |
assert utils.cwe312.cwe312_word(" ") == " " |
| 3479 |
assert utils.cwe312.cwe312_word("!") == "!" |
| 3480 |
|
| 3481 |
assert utils.cwe312.cwe312_word("a") == "a" |
| 3482 |
assert utils.cwe312.cwe312_word("ab") == "ab" |
| 3483 |
assert utils.cwe312.cwe312_word("abc") == "abc" |
| 3484 |
assert utils.cwe312.cwe312_word("abcd") == "abcd" |
| 3485 |
assert utils.cwe312.cwe312_word("abcd", force=True) == "a...d" |
| 3486 |
|
| 3487 |
assert utils.cwe312.cwe312_word("abc--d") == "abc--d" |
| 3488 |
assert utils.cwe312.cwe312_word("a-domain.ca") == "a...a" |
| 3489 |
|
| 3490 |
# Variances to still catch domain |
| 3491 |
assert ( |
| 3492 |
utils.cwe312.cwe312_word("a-domain.ca", advanced=False) |
| 3493 |
== "a-domain.ca" |
| 3494 |
) |
| 3495 |
assert ( |
| 3496 |
utils.cwe312.cwe312_word("a-domain.ca", threshold=6) == "a-domain.ca" |
| 3497 |
) |
| 3498 |
|
| 3499 |
|
| 3500 |
def test_cwe312_url(): |
| 3501 |
"""utils: cwe312_url() testing""" |
| 3502 |
assert utils.cwe312.cwe312_url(None) is None |
| 3503 |
assert utils.cwe312.cwe312_url(42) == 42 |
| 3504 |
assert utils.cwe312.cwe312_url("http://") == "http://" |
| 3505 |
assert utils.cwe312.cwe312_url("discord://") == "discord://" |
| 3506 |
assert utils.cwe312.cwe312_url("path") == "http://path" |
| 3507 |
assert utils.cwe312.cwe312_url("path/") == "http://path/" |
| 3508 |
|
| 3509 |
# Now test http:// private data |
| 3510 |
assert ( |
| 3511 |
utils.cwe312.cwe312_url("http://user:pass123@localhost") |
| 3512 |
== "http://user:p...3@localhost" |
| 3513 |
) |
| 3514 |
assert ( |
| 3515 |
utils.cwe312.cwe312_url("http://user@localhost") |
| 3516 |
== "http://user@localhost" |
| 3517 |
) |
| 3518 |
assert ( |
| 3519 |
utils.cwe312.cwe312_url("http://user@localhost?password=abc123") |
| 3520 |
== "http://user@localhost?password=a...3" |
| 3521 |
) |
| 3522 |
assert ( |
| 3523 |
utils.cwe312.cwe312_url("http://user@localhost?secret=secret-.12345") |
| 3524 |
== "http://user@localhost?secret=s...5" |
| 3525 |
) |
| 3526 |
|
| 3527 |
assert ( |
| 3528 |
utils.cwe312.cwe312_url( |
| 3529 |
"slack://mybot@xoxb-43598234231-3248932482278" |
| 3530 |
"-BZK5Wj15B9mPh1RkShJoCZ44" |
| 3531 |
"/[email protected]" |
| 3532 |
) |
| 3533 |
== "slack://[email protected]/l...m" |
| 3534 |
) |
| 3535 |
assert ( |
| 3536 |
utils.cwe312.cwe312_url( |
| 3537 |
"slack://test@B4QP3WWB4/J3QWT41JM/XIl2ffpqXkzkwMXrJdevi7W3/#random" |
| 3538 |
) |
| 3539 |
== "slack://[email protected]/J...M/X...3/" |
| 3540 |
) |
| 3541 |
|
| 3542 |
|
| 3543 |
def test_cwe312_loggable(): |
| 3544 |
"""utils: cwe312_loggable() testing""" |
| 3545 |
|
| 3546 |
# A url carrying credentials is masked |
| 3547 |
assert ( |
| 3548 |
utils.cwe312.cwe312_loggable("http://user:pass123@localhost") |
| 3549 |
== "http://user:p...3@localhost" |
| 3550 |
) |
| 3551 |
assert ( |
| 3552 |
utils.cwe312.cwe312_loggable( |
| 3553 |
"https://user:[email protected]/private_key.pem" |
| 3554 |
) |
| 3555 |
== "https://user:[email protected]/p...m" |
| 3556 |
) |
| 3557 |
|
| 3558 |
# Masking can be switched off, which is what secure_logging=False does |
| 3559 |
assert ( |
| 3560 |
utils.cwe312.cwe312_loggable( |
| 3561 |
"http://user:pass123@localhost", secure=False |
| 3562 |
) |
| 3563 |
== "http://user:pass123@localhost" |
| 3564 |
) |
| 3565 |
|
| 3566 |
# Keep local paths readable so failures identify the affected file. |
| 3567 |
for path in ( |
| 3568 |
"/etc/apprise/private_key.pem", |
| 3569 |
"~/keys/private_key.pem", |
| 3570 |
"./subscriptions.json", |
| 3571 |
"subscriptions.json", |
| 3572 |
"private_key.pem", |
| 3573 |
"C:\\keys\\private.pem", |
| 3574 |
): |
| 3575 |
assert utils.cwe312.cwe312_loggable(path) == path |
| 3576 |
assert utils.cwe312.cwe312_loggable(path, secure=False) == path |
| 3577 |
|
| 3578 |
# Nothing to print reads as a placeholder rather than 'None' |
| 3579 |
assert utils.cwe312.cwe312_loggable(None) == "(none)" |
| 3580 |
assert utils.cwe312.cwe312_loggable("") == "(none)" |
| 3581 |
assert utils.cwe312.cwe312_loggable(0) == "(none)" |
| 3582 |
assert utils.cwe312.cwe312_loggable(None, secure=False) == "(none)" |
| 3583 |
|
| 3584 |
# Anything that is not a string is turned into one rather than raising |
| 3585 |
assert utils.cwe312.cwe312_loggable(42) == "42" |
| 3586 |
assert utils.cwe312.cwe312_loggable(4.2) == "4.2" |
| 3587 |
|
| 3588 |
# Dictionaries remain available for administrator-facing diagnostics. |
| 3589 |
details = {"password": "pass123"} |
| 3590 |
assert utils.cwe312.cwe312_loggable(details) == str(details) |
| 3591 |
assert utils.cwe312.cwe312_loggable(details, secure=False) == str(details) |
| 3592 |
|
| 3593 |
# Mask by default when the caller omits the setting. |
| 3594 |
url = "http://user:pass123@localhost" |
| 3595 |
assert utils.cwe312.cwe312_loggable(url) != url |
| 3596 |
|
| 3597 |
# Malformed URLs fail closed instead of returning credentials unchanged. |
| 3598 |
for url in ( |
| 3599 |
"http://user:pass123@[", |
| 3600 |
"http://user:[email protected]:bad", |
| 3601 |
"http://user:[email protected]\nforged", |
| 3602 |
): |
| 3603 |
result = utils.cwe312.cwe312_loggable(url) |
| 3604 |
assert "pass123" not in result |
| 3605 |
assert "\n" not in result |
| 3606 |
|
| 3607 |
assert ( |
| 3608 |
utils.cwe312.cwe312_loggable("http://user:pass123@[") == "http://..." |
| 3609 |
) |
| 3610 |
assert ( |
| 3611 |
utils.cwe312.cwe312_loggable("http://user:[email protected]:bad") |
| 3612 |
== "http://..." |
| 3613 |
) |
| 3614 |
|
| 3615 |
|
| 3616 |
def test_base64_encode_decode(): |
| 3617 |
"""Utils:Base64:URLEncode & Decode.""" |
| 3618 |
assert utils.base64.base64_urlencode(None) is None |
| 3619 |
assert utils.base64.base64_urlencode(42) is None |
| 3620 |
assert utils.base64.base64_urlencode(object) is None |
| 3621 |
assert utils.base64.base64_urlencode({}) is None |
| 3622 |
assert utils.base64.base64_urlencode("") is None |
| 3623 |
assert utils.base64.base64_urlencode("abc") is None |
| 3624 |
assert utils.base64.base64_urlencode(b"") == "" |
| 3625 |
assert utils.base64.base64_urlencode(b"abc") == "YWJj" |
| 3626 |
|
| 3627 |
assert utils.base64.base64_urldecode(None) is None |
| 3628 |
assert utils.base64.base64_urldecode(42) is None |
| 3629 |
assert utils.base64.base64_urldecode(object) is None |
| 3630 |
assert utils.base64.base64_urldecode({}) is None |
| 3631 |
|
| 3632 |
assert utils.base64.base64_urldecode("abc") == b"i\xb7" |
| 3633 |
assert utils.base64.base64_urldecode("") == b"" |
| 3634 |
assert utils.base64.base64_urldecode("YWJj") == b"abc" |
| 3635 |
|
| 3636 |
|
| 3637 |
def test_dict_base64_codec(tmpdir): |
| 3638 |
"""Test encoding/decoding of base64 content.""" |
| 3639 |
original = { |
| 3640 |
"int": 1, |
| 3641 |
"float": 2.3, |
| 3642 |
} |
| 3643 |
|
| 3644 |
encoded, needs_decoding = utils.base64.encode_b64_dict(original) |
| 3645 |
assert encoded == {"int": "b64:MQ==", "float": "b64:Mi4z"} |
| 3646 |
assert needs_decoding is True |
| 3647 |
decoded = utils.base64.decode_b64_dict(encoded) |
| 3648 |
assert decoded == original |
| 3649 |
|
| 3650 |
with mock.patch("json.dumps", side_effect=TypeError()): |
| 3651 |
encoded, needs_decoding = utils.base64.encode_b64_dict(original) |
| 3652 |
# we failed |
| 3653 |
assert needs_decoding is False |
| 3654 |
assert encoded == { |
| 3655 |
"int": "1", |
| 3656 |
"float": "2.3", |
| 3657 |
} |
| 3658 |
|
| 3659 |
|
| 3660 |
def test_dir_size(tmpdir): |
| 3661 |
"""Test dir size tool.""" |
| 3662 |
|
| 3663 |
# Nothing to find/see |
| 3664 |
size, errors = utils.disk.dir_size(str(tmpdir)) |
| 3665 |
assert size == 0 |
| 3666 |
assert len(errors) == 0 |
| 3667 |
|
| 3668 |
# Write a file in our root directory |
| 3669 |
tmpdir.join("root.psdata").write("0" * 1024 * 1024) |
| 3670 |
|
| 3671 |
# Prepare some more directories |
| 3672 |
namespace_1 = tmpdir.mkdir("abcdefg") |
| 3673 |
namespace_2 = tmpdir.mkdir("defghij") |
| 3674 |
namespace_2.join("cache.psdata").write("0" * 1024 * 1024) |
| 3675 |
size, errors = utils.disk.dir_size(str(tmpdir)) |
| 3676 |
assert size == 1024 * 1024 * 2 |
| 3677 |
assert len(errors) == 0 |
| 3678 |
|
| 3679 |
# Write another file |
| 3680 |
namespace_1.join("cache.psdata").write("0" * 1024 * 1024) |
| 3681 |
size, errors = utils.disk.dir_size(str(tmpdir)) |
| 3682 |
assert size == 1024 * 1024 * 3 |
| 3683 |
assert len(errors) == 0 |
| 3684 |
|
| 3685 |
size, errors = utils.disk.dir_size(str(namespace_1)) |
| 3686 |
assert size == 1024 * 1024 |
| 3687 |
assert len(errors) == 0 |
| 3688 |
|
| 3689 |
# Create a directory insde one of our namespaces |
| 3690 |
subspace_1 = namespace_1.mkdir("zyx") |
| 3691 |
size, errors = utils.disk.dir_size(str(namespace_1)) |
| 3692 |
assert size == 1024 * 1024 |
| 3693 |
|
| 3694 |
subspace_1.join("cache.psdata").write("0" * 1024 * 1024) |
| 3695 |
size, errors = utils.disk.dir_size(str(tmpdir)) |
| 3696 |
assert size == 1024 * 1024 * 4 |
| 3697 |
assert len(errors) == 0 |
| 3698 |
|
| 3699 |
# Recursion limit reduced... no change at 2 as we can go 2 |
| 3700 |
# diretories deep no problem |
| 3701 |
size, errors = utils.disk.dir_size(str(tmpdir), max_depth=2) |
| 3702 |
assert size == 1024 * 1024 * 4 |
| 3703 |
assert len(errors) == 0 |
| 3704 |
|
| 3705 |
size, errors = utils.disk.dir_size(str(tmpdir), max_depth=1) |
| 3706 |
assert size == 1024 * 1024 * 3 |
| 3707 |
# we can't get into our subspace_1 |
| 3708 |
assert len(errors) == 1 |
| 3709 |
assert str(subspace_1) in errors |
| 3710 |
|
| 3711 |
size, errors = utils.disk.dir_size(str(tmpdir), max_depth=0) |
| 3712 |
assert size == 1024 * 1024 |
| 3713 |
# we can't get into our namespace directories |
| 3714 |
assert len(errors) == 2 |
| 3715 |
assert str(namespace_1) in errors |
| 3716 |
assert str(namespace_2) in errors |
| 3717 |
|
| 3718 |
# Let's cause problems now and test the output |
| 3719 |
size, errors = utils.disk.dir_size("invalid-directory", missing_okay=True) |
| 3720 |
assert size == 0 |
| 3721 |
assert len(errors) == 0 |
| 3722 |
|
| 3723 |
size, errors = utils.disk.dir_size("invalid-directory", missing_okay=False) |
| 3724 |
assert size == 0 |
| 3725 |
assert len(errors) == 1 |
| 3726 |
assert "invalid-directory" in errors |
| 3727 |
|
| 3728 |
with mock.patch("os.scandir", side_effect=OSError()): |
| 3729 |
size, errors = utils.disk.dir_size(str(tmpdir), missing_okay=True) |
| 3730 |
assert size == 0 |
| 3731 |
assert len(errors) == 1 |
| 3732 |
assert str(tmpdir) in errors |
| 3733 |
|
| 3734 |
with mock.patch("os.scandir") as mock_scandir: |
| 3735 |
mock_entry = mock.MagicMock() |
| 3736 |
mock_entry.is_file.side_effect = OSError() |
| 3737 |
mock_entry.path = "/test/path" |
| 3738 |
# Mock the scandir return value to yield the mock entry |
| 3739 |
mock_scandir.return_value.__enter__.return_value = [mock_entry] |
| 3740 |
|
| 3741 |
size, errors = utils.disk.dir_size(str(tmpdir)) |
| 3742 |
assert size == 0 |
| 3743 |
assert len(errors) == 1 |
| 3744 |
assert mock_entry.path in errors |
| 3745 |
|
| 3746 |
with mock.patch("os.scandir") as mock_scandir: |
| 3747 |
mock_entry = mock.MagicMock() |
| 3748 |
mock_entry.is_file.return_value = False |
| 3749 |
mock_entry.is_dir.side_effect = OSError() |
| 3750 |
mock_entry.path = "/test/path" |
| 3751 |
# Mock the scandir return value to yield the mock entry |
| 3752 |
mock_scandir.return_value.__enter__.return_value = [mock_entry] |
| 3753 |
size, errors = utils.disk.dir_size(str(tmpdir)) |
| 3754 |
assert len(errors) == 1 |
| 3755 |
assert mock_entry.path in errors |
| 3756 |
|
| 3757 |
with mock.patch("os.scandir") as mock_scandir: |
| 3758 |
mock_entry = mock.MagicMock() |
| 3759 |
mock_entry.is_file.return_value = False |
| 3760 |
mock_entry.is_dir.return_value = False |
| 3761 |
# Mock the scandir return value to yield the mock entry |
| 3762 |
mock_scandir.return_value.__enter__.return_value = [mock_entry] |
| 3763 |
size, errors = utils.disk.dir_size(str(tmpdir)) |
| 3764 |
assert size == 0 |
| 3765 |
assert len(errors) == 0 |
| 3766 |
|
| 3767 |
with mock.patch("os.scandir") as mock_scandir: |
| 3768 |
mock_entry = mock.MagicMock() |
| 3769 |
mock_entry.is_file.side_effect = FileNotFoundError() |
| 3770 |
mock_entry.path = "/test/path" |
| 3771 |
# Mock the scandir return value to yield the mock entry |
| 3772 |
mock_scandir.return_value.__enter__.return_value = [mock_entry] |
| 3773 |
|
| 3774 |
size, errors = utils.disk.dir_size(str(tmpdir)) |
| 3775 |
assert size == 0 |
| 3776 |
# No file isn't a problem, we're calculating disksize anyway, |
| 3777 |
# one less thing to calculate |
| 3778 |
assert len(errors) == 0 |
| 3779 |
|
| 3780 |
|
| 3781 |
def test_bytes_to_str(): |
| 3782 |
"""Test Bytes to String representation.""" |
| 3783 |
# Garbage Entry |
| 3784 |
assert utils.disk.bytes_to_str(None) is None |
| 3785 |
assert utils.disk.bytes_to_str("") is None |
| 3786 |
assert utils.disk.bytes_to_str("GARBAGE") is None |
| 3787 |
|
| 3788 |
# Good Entries |
| 3789 |
assert utils.disk.bytes_to_str(0) == "0.00B" |
| 3790 |
assert utils.disk.bytes_to_str(1) == "1.00B" |
| 3791 |
assert utils.disk.bytes_to_str(1.1) == "1.10B" |
| 3792 |
assert utils.disk.bytes_to_str(1024) == "1.00KB" |
| 3793 |
assert utils.disk.bytes_to_str(1024 * 1024) == "1.00MB" |
| 3794 |
assert utils.disk.bytes_to_str(1024 * 1024 * 1024) == "1.00GB" |
| 3795 |
assert utils.disk.bytes_to_str(1024 * 1024 * 1024 * 1024) == "1.00TB" |
| 3796 |
|
| 3797 |
# Support strings too |
| 3798 |
assert utils.disk.bytes_to_str("0") == "0.00B" |
| 3799 |
assert utils.disk.bytes_to_str("1024") == "1.00KB" |
| 3800 |
|
| 3801 |
|
| 3802 |
def test_time_zoneinfo(): |
| 3803 |
"""utils: zoneinfo() testing""" |
| 3804 |
|
| 3805 |
# Some valid strings |
| 3806 |
assert isinstance(utils.time.zoneinfo("UTC"), tzinfo) |
| 3807 |
assert isinstance(utils.time.zoneinfo("z"), tzinfo) |
| 3808 |
assert isinstance(utils.time.zoneinfo("gmt"), tzinfo) |
| 3809 |
assert isinstance(utils.time.zoneinfo("utc"), tzinfo) |
| 3810 |
assert isinstance(utils.time.zoneinfo("Toronto"), tzinfo) |
| 3811 |
assert isinstance(utils.time.zoneinfo("America/Toronto"), tzinfo) |
| 3812 |
assert isinstance(utils.time.zoneinfo("america/toronto"), tzinfo) |
| 3813 |
|
| 3814 |
# Edge Case with time also supported: |
| 3815 |
tz = utils.time.zoneinfo("America/Argentina/Cordoba") |
| 3816 |
isinstance(tz, tzinfo) |
| 3817 |
assert isinstance(utils.time.zoneinfo("Argentina/Cordoba"), tzinfo) |
| 3818 |
assert utils.time.zoneinfo("Argentina/Cordoba").key == tz.key |
| 3819 |
# "America/Cordoba" has been obsoleted by IANA in facor of |
| 3820 |
# "America/Argentina/Cordoba", however the IANA database |
| 3821 |
# instance used is system-dependent, so these tests have |
| 3822 |
# different results depending on the system running them |
| 3823 |
if utils.time.zoneinfo("Cordoba") is not None: |
| 3824 |
# the system has the obsolete "America/Cordoba" entry |
| 3825 |
assert isinstance(utils.time.zoneinfo("Cordoba"), tzinfo) |
| 3826 |
assert utils.time.zoneinfo("Cordoba").key == "America/Cordoba" |
| 3827 |
else: |
| 3828 |
assert utils.time.zoneinfo("Cordoba") is None |
| 3829 |
# the utils helper should still resolve this abbreviated (and |
| 3830 |
# lowercase) form |
| 3831 |
assert ( |
| 3832 |
utils.time.zoneinfo("argentina/cordoba").key |
| 3833 |
== "America/Argentina/Cordoba" |
| 3834 |
) |
| 3835 |
|
| 3836 |
# Too ambiguous |
| 3837 |
assert utils.time.zoneinfo("Argentina") is None |
| 3838 |
|
| 3839 |
# bad data |
| 3840 |
assert utils.time.zoneinfo(object) is None |
| 3841 |
assert utils.time.zoneinfo(None) is None |
| 3842 |
assert utils.time.zoneinfo(1) is None |
| 3843 |
assert utils.time.zoneinfo("") is None |
| 3844 |
assert utils.time.zoneinfo("invalid") is None |