"""Tests for WebDAV client helpers and PROPFIND parsing.""" from forwarder.webdav.client import ( MIN_WEBDAV_FILE_BYTES, WebdavConfig, WebdavFile, _parse_propfind_collections, _parse_propfind_response, build_webdav_root, get_webdav_message_key, list_files, normalize_webdav_folder_path, parse_webdav_message_key, webdav_bytes_below_min_size, webdav_file_below_min_size, webdav_file_matches_min_year, webdav_file_reference_date, ) PROPFIND_SAMPLE = b""" /remote.php/dav/files/user/Faturas/ /remote.php/dav/files/user/Faturas/invoice.pdf "abc123" Mon, 01 Jan 2024 10:00:00 GMT 2024-01-01T10:00:00Z 12345 /remote.php/dav/files/user/Faturas/sub/photo.jpg "def456" Mon, 15 Jun 2025 12:00:00 GMT 999 """ def test_normalize_webdav_folder_path(): assert normalize_webdav_folder_path(None) == "/" assert normalize_webdav_folder_path("") == "/" assert normalize_webdav_folder_path("Faturas") == "/Faturas" assert normalize_webdav_folder_path("/Faturas/") == "/Faturas" def test_build_webdav_root(): config = WebdavConfig( base_url="https://cloud.example.com/remote.php/dav/files/user", folder_path="/Faturas", user="user", password="secret", ) assert ( build_webdav_root(config) == "https://cloud.example.com/remote.php/dav/files/user/Faturas" ) def test_webdav_message_key_roundtrip(): key = get_webdav_message_key("Faturas/invoice.pdf") assert key == "webdav:Faturas/invoice.pdf" assert parse_webdav_message_key(key) == "Faturas/invoice.pdf" assert parse_webdav_message_key("imap:123") is None def _sample_file(*, modified: str = "", created: str = "", path: str = "a.pdf") -> WebdavFile: return WebdavFile( relative_path=path, name="a.pdf", href="/a.pdf", etag="1", modified=modified, created=created, oc_creation_time="", nc_creation_time="", size=100, ) def test_webdav_file_matches_min_year_by_creation_date(): old = _sample_file(created="2024-06-01T10:00:00Z", modified="Mon, 01 Jan 2026 10:00:00 GMT") assert webdav_file_reference_date(old).year == 2026 assert webdav_file_matches_min_year(old, 2025) recent = _sample_file(modified="Mon, 15 Jun 2025 12:00:00 GMT") assert webdav_file_matches_min_year(recent, 2025) def test_webdav_file_reference_date_prefers_modified_over_epoch_creation(): nextcloud_import = WebdavFile( relative_path="Faturas/2025/01_Janeiro/invoice.pdf", name="invoice.pdf", href="/invoice.pdf", etag="1", modified="Mon, 06 Jan 2025 13:38:04 GMT", created="1970-01-01T00:00:00+00:00", oc_creation_time="", nc_creation_time="", size=50000, ) assert webdav_file_reference_date(nextcloud_import).year == 2025 assert webdav_file_matches_min_year(nextcloud_import, 2025) def test_webdav_file_matches_min_year_by_folder_when_epoch_date(): epoch_in_2025_folder = WebdavFile( relative_path="Faturas/2025/01_Janeiro/invoice.pdf", name="invoice.pdf", href="/invoice.pdf", etag="1", modified="", created="1970-01-01T00:00:00+00:00", oc_creation_time="", nc_creation_time="", size=50000, ) assert webdav_file_matches_min_year(epoch_in_2025_folder, 2025) epoch_in_2024_folder = WebdavFile( relative_path="Faturas/2024/invoice.pdf", name="invoice.pdf", href="/invoice.pdf", etag="1", modified="", created="1970-01-01T00:00:00+00:00", oc_creation_time="", nc_creation_time="", size=50000, ) assert not webdav_file_matches_min_year(epoch_in_2024_folder, 2025) def test_webdav_min_file_size(): assert MIN_WEBDAV_FILE_BYTES == 4096 small = WebdavFile( relative_path="stub.pdf", name="stub.pdf", href="/stub.pdf", etag="1", modified="", created="", oc_creation_time="", nc_creation_time="", size=1997, ) assert webdav_file_below_min_size(small) unknown = WebdavFile( relative_path="x.pdf", name="x.pdf", href="/x.pdf", etag="1", modified="", created="", oc_creation_time="", nc_creation_time="", size=0, ) assert not webdav_file_below_min_size(unknown) large = WebdavFile( relative_path="invoice.pdf", name="invoice.pdf", href="/invoice.pdf", etag="1", modified="", created="", oc_creation_time="", nc_creation_time="", size=5000, ) assert not webdav_file_below_min_size(large) assert webdav_bytes_below_min_size(b"x" * 100) assert not webdav_bytes_below_min_size(b"x" * MIN_WEBDAV_FILE_BYTES) PROPFIND_MULTI_PROPSTAT = b""" /remote.php/dav/files/user/Faturas/doc.pdf HTTP/1.1 404 Not Found Mon, 15 Jun 2025 12:00:00 GMT 1970-01-01T00:00:00+00:00 5000 "x" HTTP/1.1 200 OK """ def test_parse_propfind_uses_ok_propstat(): root = "https://cloud.example.com/remote.php/dav/files/user/Faturas/" files = _parse_propfind_response(root, PROPFIND_MULTI_PROPSTAT) assert len(files) == 1 assert files[0].modified == "Mon, 15 Jun 2025 12:00:00 GMT" assert webdav_file_reference_date(files[0]).year == 2025 def test_parse_propfind_response(): root = "https://cloud.example.com/remote.php/dav/files/user/Faturas/" files = _parse_propfind_response(root, PROPFIND_SAMPLE) names = sorted(item.name for item in files) assert names == ["invoice.pdf", "photo.jpg"] invoice = next(item for item in files if item.name == "invoice.pdf") assert invoice.relative_path == "invoice.pdf" assert invoice.etag == "abc123" assert invoice.size == 12345 assert invoice.created == "2024-01-01T10:00:00Z" assert not webdav_file_matches_min_year(invoice, 2025) photo = next(item for item in files if item.name == "photo.jpg") assert webdav_file_matches_min_year(photo, 2025) def _propfind_response(folder_href: str, entries: list[dict]) -> bytes: """Build a minimal DAV multistatus body for tests.""" parts = [ b'', b'', ] for entry in entries: is_collection = entry.get("collection", False) href = entry["href"].encode() parts.append(b" ") parts.append(b" " + href + b"") parts.append(b" ") if is_collection: parts.append(b" ") else: parts.append(b" ") parts.append(b" " + entry.get("etag", b'"x"') + b"") parts.append( b" " + entry.get("modified", b"Mon, 01 Jan 2024 10:00:00 GMT") + b"" ) parts.append( b" " + str(entry.get("size", 5000)).encode() + b"" ) parts.append(b" ") parts.append(b" ") parts.append(b"") return b"\n".join(parts) ROOT_URL = "https://cloud.example.com/remote.php/dav/files/user/Faturas" def _make_config() -> WebdavConfig: return WebdavConfig( base_url="https://cloud.example.com/remote.php/dav/files/user", folder_path="/Faturas", user="user", password="secret", verify_ssl=False, ) def test_parse_propfind_collections_skips_root(): body = _propfind_response( ROOT_URL + "/", [ {"href": ROOT_URL + "/", "collection": True}, {"href": ROOT_URL + "/2025/", "collection": True}, {"href": ROOT_URL + "/invoice.pdf"}, ], ) children = _parse_propfind_collections(ROOT_URL, body) assert children == [ROOT_URL + "/2025/"] def test_list_files_falls_back_to_depth_one_on_http_500(monkeypatch): """Depth:infinity returning HTTP 500 triggers recursive Depth:1 traversal.""" import httpx import forwarder.webdav.client as webdav_client root_body = _propfind_response( ROOT_URL + "/", [ {"href": ROOT_URL + "/", "collection": True}, {"href": ROOT_URL + "/2025/", "collection": True}, {"href": ROOT_URL + "/invoice.pdf", "etag": b'"abc"'}, ], ) sub_body = _propfind_response( ROOT_URL + "/2025/", [ {"href": ROOT_URL + "/2025/", "collection": True}, {"href": ROOT_URL + "/2025/receipt.pdf", "etag": b'"def"', "size": 9000}, ], ) def handler(request: httpx.Request) -> httpx.Response: depth = request.headers.get("Depth") url = str(request.url).rstrip("/") if depth == "infinity": return httpx.Response(500, text="infinite depth disabled") if url == ROOT_URL and depth == "1": return httpx.Response(207, content=root_body) if url == ROOT_URL + "/2025" and depth == "1": return httpx.Response(207, content=sub_body) return httpx.Response(404) transport = httpx.MockTransport(handler) def _fake_client(config, *, timeout=60.0): return httpx.Client( transport=transport, auth=(config.user, config.password), headers={"User-Agent": "Bizdocs-Forwarder/1.0"}, ) monkeypatch.setattr(webdav_client, "_client", _fake_client) files = list_files(_make_config()) paths = sorted(item.relative_path for item in files) assert paths == ["2025/receipt.pdf", "invoice.pdf"] receipt = next(item for item in files if item.name == "receipt.pdf") assert receipt.size == 9000 assert webdav_file_matches_min_year(receipt, 2025) def test_list_files_still_raises_on_persistent_500(monkeypatch): """A 500 that is not resolved by Depth:1 fallback surfaces as WebdavError.""" import httpx import forwarder.webdav.client as webdav_client def handler(request: httpx.Request) -> httpx.Response: return httpx.Response(500, text="server error") transport = httpx.MockTransport(handler) def _fake_client(config, *, timeout=60.0): return httpx.Client( transport=transport, auth=(config.user, config.password), headers={"User-Agent": "Bizdocs-Forwarder/1.0"}, ) monkeypatch.setattr(webdav_client, "_client", _fake_client) import pytest with pytest.raises(webdav_client.WebdavError): list_files(_make_config())