| 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 |
import sys |
| 28 |
|
| 29 |
from apprise.utils.xml import flatten_xml_response |
| 30 |
|
| 31 |
KEEP_MAP = { |
| 32 |
"RequestId": "request_id", |
| 33 |
"MessageId": "message_id", |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
def test_flatten_xml_response_extracts_mapped_tags(): |
| 38 |
"""Tags listed in keep_map are pulled out under their mapped key.""" |
| 39 |
response = flatten_xml_response( |
| 40 |
""" |
| 41 |
<PublishResponse> |
| 42 |
<PublishResult> |
| 43 |
<MessageId>abc-123</MessageId> |
| 44 |
</PublishResult> |
| 45 |
<ResponseMetadata> |
| 46 |
<RequestId>req-456</RequestId> |
| 47 |
</ResponseMetadata> |
| 48 |
</PublishResponse> |
| 49 |
""", |
| 50 |
KEEP_MAP, |
| 51 |
) |
| 52 |
|
| 53 |
assert response["type"] == "PublishResponse" |
| 54 |
assert response["message_id"] == "abc-123" |
| 55 |
assert response["request_id"] == "req-456" |
| 56 |
|
| 57 |
|
| 58 |
def test_flatten_xml_response_repeated_tag_keeps_last_value(): |
| 59 |
"""Repeated mapped tags retain the last value in document order.""" |
| 60 |
response = flatten_xml_response( |
| 61 |
"<Response>" |
| 62 |
"<RequestId>first</RequestId>" |
| 63 |
"<RequestId>last</RequestId>" |
| 64 |
"</Response>", |
| 65 |
KEEP_MAP, |
| 66 |
) |
| 67 |
|
| 68 |
assert response["request_id"] == "last" |
| 69 |
|
| 70 |
|
| 71 |
def test_flatten_xml_response_strips_root_namespace(): |
| 72 |
"""A namespace on the root tag doesn't stop tags from being matched.""" |
| 73 |
response = flatten_xml_response( |
| 74 |
'<PublishResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">' |
| 75 |
"<RequestId>req-456</RequestId>" |
| 76 |
"</PublishResponse>", |
| 77 |
KEEP_MAP, |
| 78 |
) |
| 79 |
|
| 80 |
assert response["type"] == "PublishResponse" |
| 81 |
assert response["request_id"] == "req-456" |
| 82 |
|
| 83 |
|
| 84 |
def test_flatten_xml_response_empty_known_tag_is_blank_not_a_crash(): |
| 85 |
"""A self-closed or empty known tag yields "" instead of raising. |
| 86 |
|
| 87 |
<Message/> has no .text at all (it's None), and the old |
| 88 |
implementation called .strip() on that None directly. |
| 89 |
""" |
| 90 |
response = flatten_xml_response( |
| 91 |
"<ErrorResponse><Message/></ErrorResponse>", |
| 92 |
{"Message": "error_message"}, |
| 93 |
) |
| 94 |
|
| 95 |
assert response["type"] == "ErrorResponse" |
| 96 |
assert response["error_message"] == "" |
| 97 |
|
| 98 |
|
| 99 |
def test_flatten_xml_response_whitespace_only_tag_is_blank(): |
| 100 |
"""A tag containing only whitespace is trimmed down to an empty |
| 101 |
string, same as an empty tag.""" |
| 102 |
response = flatten_xml_response( |
| 103 |
"<ErrorResponse><Message> </Message></ErrorResponse>", |
| 104 |
{"Message": "error_message"}, |
| 105 |
) |
| 106 |
|
| 107 |
assert response["error_message"] == "" |
| 108 |
|
| 109 |
|
| 110 |
def test_flatten_xml_response_survives_deeply_nested_xml(): |
| 111 |
"""A response nested far past Python's default recursion limit still |
| 112 |
parses instead of raising a RecursionError. |
| 113 |
|
| 114 |
The old implementation walked the tree with a recursive helper, so |
| 115 |
one recursive call was made per level of nesting. |
| 116 |
""" |
| 117 |
depth = sys.getrecursionlimit() + 1000 |
| 118 |
|
| 119 |
xml = ("<a>" * depth) + "leaf-value" + ("</a>" * depth) |
| 120 |
|
| 121 |
response = flatten_xml_response(xml, {"a": "value"}) |
| 122 |
|
| 123 |
assert response["type"] == "a" |
| 124 |
assert response["value"] == "leaf-value" |
| 125 |
|
| 126 |
|
| 127 |
def test_flatten_xml_response_invalid_xml_returns_defaults(): |
| 128 |
"""Malformed XML falls back to the defaults instead of raising.""" |
| 129 |
response = flatten_xml_response( |
| 130 |
"<Bad Response>", KEEP_MAP, defaults={"request_id": None} |
| 131 |
) |
| 132 |
|
| 133 |
assert response["type"] is None |
| 134 |
assert response["request_id"] is None |
| 135 |
|
| 136 |
|
| 137 |
def test_flatten_xml_response_non_string_input_returns_defaults(): |
| 138 |
"""A non-string response (e.g. None, after a connection failure) |
| 139 |
falls back to the defaults instead of raising.""" |
| 140 |
response = flatten_xml_response( |
| 141 |
None, KEEP_MAP, defaults={"request_id": None} |
| 142 |
) |
| 143 |
|
| 144 |
assert response["type"] is None |
| 145 |
assert response["request_id"] is None |
| 146 |
|
| 147 |
|
| 148 |
def test_flatten_xml_response_defaults_not_mutated(): |
| 149 |
"""The defaults dictionary passed in is never modified in place.""" |
| 150 |
defaults = {"request_id": None} |
| 151 |
|
| 152 |
response = flatten_xml_response( |
| 153 |
"<PublishResponse><RequestId>req-456</RequestId></PublishResponse>", |
| 154 |
KEEP_MAP, |
| 155 |
defaults=defaults, |
| 156 |
) |
| 157 |
|
| 158 |
assert defaults == {"request_id": None} |
| 159 |
assert response["request_id"] == "req-456" |
| 160 |
|
| 161 |
|
| 162 |
def test_flatten_xml_response_unmapped_tags_are_ignored(): |
| 163 |
"""Tags that aren't listed in keep_map are left out of the result.""" |
| 164 |
response = flatten_xml_response( |
| 165 |
"<PublishResponse><Extra>ignored</Extra></PublishResponse>", |
| 166 |
KEEP_MAP, |
| 167 |
) |
| 168 |
|
| 169 |
assert "Extra" not in response |
| 170 |
assert "extra" not in response |
| 171 |
|
| 172 |
|
| 173 |
def test_flatten_xml_response_rejects_a_dtd(): |
| 174 |
"""A response carrying a DTD is refused before entities expand.""" |
| 175 |
response = flatten_xml_response( |
| 176 |
"""<?xml version="1.0"?> |
| 177 |
<!DOCTYPE PublishResponse [ |
| 178 |
<!ENTITY a "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"> |
| 179 |
<!ENTITY b "&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;"> |
| 180 |
<!ENTITY c "&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;"> |
| 181 |
<!ENTITY d "&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;"> |
| 182 |
<!ENTITY e "&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;"> |
| 183 |
]> |
| 184 |
<PublishResponse><MessageId>&e;</MessageId></PublishResponse>""", |
| 185 |
KEEP_MAP, |
| 186 |
defaults={"message_id": None}, |
| 187 |
) |
| 188 |
|
| 189 |
assert response["type"] is None |
| 190 |
assert response["message_id"] is None |
| 191 |
|
| 192 |
|
| 193 |
def test_flatten_xml_response_truncated_xml_returns_defaults(): |
| 194 |
"""A response that ends early contributes nothing, not a partial read.""" |
| 195 |
response = flatten_xml_response( |
| 196 |
"<PublishResponse><RequestId>req-456</RequestId>", |
| 197 |
KEEP_MAP, |
| 198 |
defaults={"request_id": None}, |
| 199 |
) |
| 200 |
|
| 201 |
assert response["type"] is None |
| 202 |
assert response["request_id"] is None |