Include all year folders from 2025 onwards (2026, 2027, …) and skip or mark anything older.
93 lines
3.0 KiB
Python
93 lines
3.0 KiB
Python
"""Tests for WebDAV client helpers and PROPFIND parsing."""
|
|
|
|
from forwarder.webdav.client import (
|
|
WebdavConfig,
|
|
_parse_propfind_response,
|
|
build_webdav_root,
|
|
get_webdav_message_key,
|
|
normalize_webdav_folder_path,
|
|
parse_webdav_message_key,
|
|
webdav_path_matches_min_year,
|
|
)
|
|
|
|
PROPFIND_SAMPLE = b"""<?xml version="1.0" encoding="utf-8"?>
|
|
<D:multistatus xmlns:D="DAV:">
|
|
<D:response>
|
|
<D:href>/remote.php/dav/files/user/Faturas/</D:href>
|
|
<D:propstat>
|
|
<D:prop>
|
|
<D:resourcetype><D:collection/></D:resourcetype>
|
|
</D:prop>
|
|
</D:propstat>
|
|
</D:response>
|
|
<D:response>
|
|
<D:href>/remote.php/dav/files/user/Faturas/invoice.pdf</D:href>
|
|
<D:propstat>
|
|
<D:prop>
|
|
<D:getetag>"abc123"</D:getetag>
|
|
<D:getlastmodified>Mon, 01 Jan 2024 10:00:00 GMT</D:getlastmodified>
|
|
<D:getcontentlength>12345</D:getcontentlength>
|
|
<D:resourcetype/>
|
|
</D:prop>
|
|
</D:propstat>
|
|
</D:response>
|
|
<D:response>
|
|
<D:href>/remote.php/dav/files/user/Faturas/sub/photo.jpg</D:href>
|
|
<D:propstat>
|
|
<D:prop>
|
|
<D:getetag>"def456"</D:getetag>
|
|
<D:getcontentlength>999</D:getcontentlength>
|
|
<D:resourcetype/>
|
|
</D:prop>
|
|
</D:propstat>
|
|
</D:response>
|
|
</D:multistatus>
|
|
"""
|
|
|
|
|
|
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 test_webdav_path_matches_min_year():
|
|
assert webdav_path_matches_min_year("2025/invoice.pdf", 2025)
|
|
assert webdav_path_matches_min_year("2026/a.pdf", 2025)
|
|
assert webdav_path_matches_min_year("2027/future.pdf", 2025)
|
|
assert not webdav_path_matches_min_year("2024/old.pdf", 2025)
|
|
assert not webdav_path_matches_min_year("misc/file.pdf", 2025)
|
|
assert webdav_path_matches_min_year("2024/old.pdf", None)
|
|
|
|
|
|
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
|