import pytest import httpx import respx from app.ingest.webdav import download_file, WebDAVError @pytest.mark.asyncio async def test_download_file_returns_bytes(): base = "https://nc.example.com/remote.php/dav/files/u" file_path = "Documents/THB/Studium/2.Semester/Databases/x.pdf" expected_url = f"{base}/{file_path}" with respx.mock(base_url=base) as mock: mock.get(expected_url).mock(return_value=httpx.Response(200, content=b"PDFBYTES")) data = await download_file(base, "u", "pw", file_path) assert data == b"PDFBYTES" @pytest.mark.asyncio async def test_download_file_404_raises(): base = "https://nc.example.com/remote.php/dav/files/u" file_path = "missing.pdf" expected_url = f"{base}/{file_path}" with respx.mock(base_url=base) as mock: mock.get(expected_url).mock(return_value=httpx.Response(404)) with pytest.raises(WebDAVError): await download_file(base, "u", "pw", file_path) @pytest.mark.asyncio async def test_download_file_handles_url_encoding(): base = "https://nc.example.com/remote.php/dav/files/u" file_path = "Documents/Folder With Space/file.pdf" with respx.mock(base_url=base) as mock: # httpx will percent-encode the spaces route = mock.get(url__regex=r".*/Folder%20With%20Space/file\.pdf").mock( return_value=httpx.Response(200, content=b"OK") ) data = await download_file(base, "u", "pw", file_path) assert data == b"OK" assert route.called