"""Tests for WebDAV client helpers and PROPFIND parsing.""" from forwarder.webdav.client import ( WebdavConfig, WebdavFile, _parse_propfind_response, build_webdav_root, get_webdav_message_key, normalize_webdav_folder_path, parse_webdav_message_key, 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, 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 == 2024 assert not 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_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)