|
| 1 | +import pytest |
| 2 | +import os |
| 3 | +from aiohttp import web |
| 4 | +from app.user_manager import UserManager |
| 5 | + |
| 6 | +pytestmark = ( |
| 7 | + pytest.mark.asyncio |
| 8 | +) # This applies the asyncio mark to all test functions in the module |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def user_manager(tmp_path): |
| 13 | + um = UserManager() |
| 14 | + um.get_request_user_filepath = lambda req, file, **kwargs: os.path.join( |
| 15 | + tmp_path, file |
| 16 | + ) |
| 17 | + return um |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def app(user_manager): |
| 22 | + app = web.Application() |
| 23 | + routes = web.RouteTableDef() |
| 24 | + user_manager.add_routes(routes) |
| 25 | + app.add_routes(routes) |
| 26 | + return app |
| 27 | + |
| 28 | + |
| 29 | +async def test_listuserdata_empty_directory(aiohttp_client, app, tmp_path): |
| 30 | + client = await aiohttp_client(app) |
| 31 | + resp = await client.get("/userdata?dir=test_dir") |
| 32 | + assert resp.status == 404 |
| 33 | + |
| 34 | + |
| 35 | +async def test_listuserdata_with_files(aiohttp_client, app, tmp_path): |
| 36 | + os.makedirs(tmp_path / "test_dir") |
| 37 | + with open(tmp_path / "test_dir" / "file1.txt", "w") as f: |
| 38 | + f.write("test content") |
| 39 | + |
| 40 | + client = await aiohttp_client(app) |
| 41 | + resp = await client.get("/userdata?dir=test_dir") |
| 42 | + assert resp.status == 200 |
| 43 | + assert await resp.json() == ["file1.txt"] |
| 44 | + |
| 45 | + |
| 46 | +async def test_listuserdata_recursive(aiohttp_client, app, tmp_path): |
| 47 | + os.makedirs(tmp_path / "test_dir" / "subdir") |
| 48 | + with open(tmp_path / "test_dir" / "file1.txt", "w") as f: |
| 49 | + f.write("test content") |
| 50 | + with open(tmp_path / "test_dir" / "subdir" / "file2.txt", "w") as f: |
| 51 | + f.write("test content") |
| 52 | + |
| 53 | + client = await aiohttp_client(app) |
| 54 | + resp = await client.get("/userdata?dir=test_dir&recurse=true") |
| 55 | + assert resp.status == 200 |
| 56 | + assert set(await resp.json()) == {"file1.txt", os.path.join("subdir", "file2.txt")} |
| 57 | + |
| 58 | + |
| 59 | +async def test_listuserdata_full_info(aiohttp_client, app, tmp_path): |
| 60 | + os.makedirs(tmp_path / "test_dir") |
| 61 | + with open(tmp_path / "test_dir" / "file1.txt", "w") as f: |
| 62 | + f.write("test content") |
| 63 | + |
| 64 | + client = await aiohttp_client(app) |
| 65 | + resp = await client.get("/userdata?dir=test_dir&full_info=true") |
| 66 | + assert resp.status == 200 |
| 67 | + result = await resp.json() |
| 68 | + assert len(result) == 1 |
| 69 | + assert result[0]["path"] == "file1.txt" |
| 70 | + assert "size" in result[0] |
| 71 | + assert "modified" in result[0] |
| 72 | + |
| 73 | + |
| 74 | +async def test_listuserdata_split_path(aiohttp_client, app, tmp_path): |
| 75 | + os.makedirs(tmp_path / "test_dir" / "subdir") |
| 76 | + with open(tmp_path / "test_dir" / "subdir" / "file1.txt", "w") as f: |
| 77 | + f.write("test content") |
| 78 | + |
| 79 | + client = await aiohttp_client(app) |
| 80 | + resp = await client.get("/userdata?dir=test_dir&recurse=true&split=true") |
| 81 | + assert resp.status == 200 |
| 82 | + assert await resp.json() == [ |
| 83 | + [os.path.join("subdir", "file1.txt"), "subdir", "file1.txt"] |
| 84 | + ] |
| 85 | + |
| 86 | + |
| 87 | +async def test_listuserdata_invalid_directory(aiohttp_client, app): |
| 88 | + client = await aiohttp_client(app) |
| 89 | + resp = await client.get("/userdata?dir=") |
| 90 | + assert resp.status == 400 |
0 commit comments