| 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 |
"""Tests for the small YAML/template inspection boundary.""" |
| 29 |
|
| 30 |
import pytest |
| 31 |
|
| 32 |
from apprise.utils.parse import QSD_FULL_MODE_KEYS |
| 33 |
from apprise.utils.template import TemplatePlaceholderMap, TemplateSchema |
| 34 |
from apprise.utils.yaml import ( |
| 35 |
dropped_template, |
| 36 |
template_in_schema, |
| 37 |
template_references, |
| 38 |
templated_tag, |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
def placeholders(*names): |
| 43 |
"""Create the same placeholder map used while parsing YAML URLs.""" |
| 44 |
return TemplatePlaceholderMap(TemplateSchema.parse(list(names)), "") |
| 45 |
|
| 46 |
|
| 47 |
def test_yaml_template_references_walks_values_and_url_keys_once(): |
| 48 |
"""Scan supported containers without treating names as values.""" |
| 49 |
loop = [] |
| 50 |
loop.append(loop) |
| 51 |
shared = ("${TUPLE}",) |
| 52 |
value = [ |
| 53 |
None, |
| 54 |
7, |
| 55 |
"${PLAIN} and malformed ${NO-DASH}", |
| 56 |
{ |
| 57 |
"json://${HOST}/": { |
| 58 |
"+${LITERAL_KEY}": "${VALUE}", |
| 59 |
"nested": shared, |
| 60 |
}, |
| 61 |
"ordinary": shared, |
| 62 |
}, |
| 63 |
{"${SET}"}, |
| 64 |
loop, |
| 65 |
] |
| 66 |
|
| 67 |
assert template_references(value) == { |
| 68 |
"plain", |
| 69 |
"host", |
| 70 |
"value", |
| 71 |
"tuple", |
| 72 |
"set", |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.parametrize( |
| 77 |
("url", "declared", "expected"), |
| 78 |
[ |
| 79 |
(None, {"t"}, None), |
| 80 |
("json://host/${T}", {"t"}, None), |
| 81 |
("${OTHER}://host/", {"t"}, None), |
| 82 |
("${T}://host/", {"t"}, "t"), |
| 83 |
("json${T}://host/", ("t",), "t"), |
| 84 |
("${T}user:pass@host/", {"t"}, "t"), |
| 85 |
("${T}/path://later", {"t"}, "t"), |
| 86 |
("${T}localhost/?url=http://bad.actor.com", {"t"}, "t"), |
| 87 |
(r"abc${T}s\://localhost", {"t"}, "t"), |
| 88 |
("json://host/${T}://later", {"t"}, None), |
| 89 |
], |
| 90 |
) |
| 91 |
def test_yaml_template_schema_boundary(url, declared, expected): |
| 92 |
"""Only declared markers at or before the schema boundary are refused.""" |
| 93 |
assert template_in_schema(url, declared) == expected |
| 94 |
|
| 95 |
|
| 96 |
def test_yaml_dropped_template_reports_stable_missing_name(): |
| 97 |
"""Parser loss is detected without considering the saved source URL.""" |
| 98 |
mapping = placeholders("a", "b") |
| 99 |
url = mapping.encode_url("json://user:${B}@host/${A}") |
| 100 |
|
| 101 |
assert dropped_template(url, {}, None) is None |
| 102 |
assert dropped_template(url, {"path": url}, mapping) is None |
| 103 |
assert ( |
| 104 |
dropped_template( |
| 105 |
url, |
| 106 |
{ |
| 107 |
"url": url, |
| 108 |
"password": mapping.encode("${B}"), |
| 109 |
}, |
| 110 |
mapping, |
| 111 |
) |
| 112 |
== "a" |
| 113 |
) |
| 114 |
|
| 115 |
|
| 116 |
@pytest.mark.parametrize("bucket", ("qsd", *QSD_FULL_MODE_KEYS)) |
| 117 |
def test_yaml_templated_tag_checks_every_query_bucket(bucket): |
| 118 |
"""Every query modifier uses the same fixed-routing rule.""" |
| 119 |
mapping = placeholders("t") |
| 120 |
assert templated_tag({bucket: {"tag": mapping.encode("${T}")}}, mapping) |
| 121 |
|
| 122 |
|
| 123 |
def test_yaml_templated_tag_handles_encoded_and_global_values(): |
| 124 |
"""Per-service placeholders and raw global tags are both detected.""" |
| 125 |
mapping = placeholders("t") |
| 126 |
|
| 127 |
assert templated_tag({"tag": {mapping.encode("${T}")}}, mapping) |
| 128 |
assert templated_tag({"tag": {"${T}"}}, mapping) |
| 129 |
assert not templated_tag({"qsd": None, "tag": {"${OTHER}"}}, mapping) |
| 130 |
assert not templated_tag([], mapping) |
| 131 |
assert not templated_tag({"tag": set()}, None) |