Debian Codegraph @

tests/test_parser.py

raw
1 """Tests for the parser module."""
2
3 from lxml import etree
4 import pytest
5
6 from aiowebdav2.exceptions import MethodNotSupportedError, RemoteResourceNotFoundError
7 from aiowebdav2.models import QuotaInfo
8 from aiowebdav2.parser import WebDavXmlUtils
9
10
11 def test_parse_get_list_info_response_xml_error() -> None:
12 """Test parse get list info response xml error."""
13 assert not WebDavXmlUtils.parse_get_list_info_response(b"<not-xml")
14
15
16 def test_parse_get_list_property_response_xml_error() -> None:
17 """Test parse get list property response xml error."""
18 assert not (
19 WebDavXmlUtils.parse_get_list_property_response(
20 b"<not-xml", properties=[], hostname="https://example.com"
21 )
22 )
23
24
25 def test_parse_get_list_response_xml_error() -> None:
26 """Test parse get list response xml error."""
27 assert not WebDavXmlUtils.parse_get_list_response(b"<not-xml")
28
29
30 def test_parse_free_space_response_xml_error() -> None:
31 """Test parse free space response xml error."""
32 assert (
33 WebDavXmlUtils.parse_free_space_response(
34 b"<not-xml", hostname="https://example.com"
35 )
36 is None
37 )
38
39
40 def test_extract_response_for_path_xml_error() -> None:
41 """Test extract response for path xml error."""
42 with pytest.raises(MethodNotSupportedError):
43 WebDavXmlUtils.extract_response_for_path(
44 content=b"<not-xml",
45 path="/test_dir/",
46 hostname="https://example.com",
47 )
48
49
50 def test_extract_response_for_path_missing_response() -> None:
51 """Test extract response for path missing response."""
52 root = etree.Element("multistatus", xmlns="DAV:")
53 content = etree.tostring(etree.ElementTree(root))
54 with pytest.raises(RemoteResourceNotFoundError):
55 WebDavXmlUtils.extract_response_for_path(
56 content=content, path="/missing", hostname="https://example.com"
57 )
58
59
60 def test_extract_response_for_path_skips_none_href() -> None:
61 """Test extract_response_for_path skips responses with missing href."""
62 root = etree.Element("multistatus", xmlns="DAV:")
63 # Response without href - should be skipped
64 resp_no_href = etree.SubElement(root, "response")
65 etree.SubElement(resp_no_href, "propstat")
66 # Response with valid href
67 resp_valid = etree.SubElement(root, "response")
68 href = etree.SubElement(resp_valid, "href")
69 href.text = "/test_dir/"
70 content = etree.tostring(etree.ElementTree(root))
71 result = WebDavXmlUtils.extract_response_for_path(
72 content=content, path="/test_dir/", hostname="https://example.com"
73 )
74 assert result is not None
75
76
77 def test_parse_quota_response_both_values() -> None:
78 """Test parse_quota_response extracts both available and used bytes."""
79 content = (
80 b'<?xml version="1.0"?>'
81 b'<d:multistatus xmlns:d="DAV:">'
82 b"<d:response><d:propstat><d:prop>"
83 b"<d:quota-available-bytes>500</d:quota-available-bytes>"
84 b"<d:quota-used-bytes>200</d:quota-used-bytes>"
85 b"</d:prop></d:propstat></d:response>"
86 b"</d:multistatus>"
87 )
88 result = WebDavXmlUtils.parse_quota_response(content, "https://example.com")
89 assert result == QuotaInfo(available_bytes=500, used_bytes=200)
90
91
92 def test_parse_quota_response_not_supported() -> None:
93 """Test parse_quota_response raises when no quota properties present."""
94 content = (
95 b'<?xml version="1.0"?>'
96 b'<d:multistatus xmlns:d="DAV:">'
97 b"<d:response><d:propstat><d:prop/>"
98 b"</d:propstat></d:response>"
99 b"</d:multistatus>"
100 )
101 with pytest.raises(MethodNotSupportedError):
102 WebDavXmlUtils.parse_quota_response(content, "https://example.com")
103
104
105 def test_parse_quota_response_partial() -> None:
106 """Test parse_quota_response with only used_bytes present."""
107 content = (
108 b'<?xml version="1.0"?>'
109 b'<d:multistatus xmlns:d="DAV:">'
110 b"<d:response><d:propstat><d:prop>"
111 b"<d:quota-used-bytes>42</d:quota-used-bytes>"
112 b"</d:prop></d:propstat></d:response>"
113 b"</d:multistatus>"
114 )
115 result = WebDavXmlUtils.parse_quota_response(content, "https://example.com")
116 assert result == QuotaInfo(available_bytes=None, used_bytes=42)
117
118
119 def test_parse_quota_response_xml_error() -> None:
120 """Test parse_quota_response handles invalid XML gracefully."""
121 result = WebDavXmlUtils.parse_quota_response(
122 b"<not-xml", hostname="https://example.com"
123 )
124 assert result == QuotaInfo(available_bytes=None, used_bytes=None)
125
126
127 def test_parse_quota_response_non_numeric() -> None:
128 """Test parse_quota_response raises on non-numeric quota values."""
129 content = (
130 b'<?xml version="1.0"?>'
131 b'<d:multistatus xmlns:d="DAV:">'
132 b"<d:response><d:propstat><d:prop>"
133 b"<d:quota-available-bytes>abc</d:quota-available-bytes>"
134 b"</d:prop></d:propstat></d:response>"
135 b"</d:multistatus>"
136 )
137 with pytest.raises(MethodNotSupportedError):
138 WebDavXmlUtils.parse_quota_response(content, "https://example.com")