From b4c650b56e24a2c2261b55b3e9d1a8cce51b8344 Mon Sep 17 00:00:00 2001 From: huchenlei Date: Thu, 31 Oct 2024 17:22:02 -0400 Subject: [PATCH] Add tests --- app/user_manager.py | 5 +- .../prompt_server_test/user_manager_test.py | 61 ++++++++++++++++++- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/app/user_manager.py b/app/user_manager.py index f6c71e4c8..cf8eac979 100644 --- a/app/user_manager.py +++ b/app/user_manager.py @@ -22,7 +22,7 @@ class FileInfo(TypedDict): def get_file_info(path: str, relative_to: str) -> FileInfo: return { - "path": os.path.relpath(path, relative_to), + "path": os.path.relpath(path, relative_to).replace(os.sep, '/'), "size": os.path.getsize(path), "modified": os.path.getmtime(path) } @@ -178,11 +178,10 @@ class UserManager(): pattern = os.path.join(glob.escape(path), '*') def process_full_path(full_path: str) -> FileInfo | str | list[str]: - full_path = full_path.replace(os.sep, '/') if full_info: return get_file_info(full_path, path) - rel_path = os.path.relpath(full_path, path) + rel_path = os.path.relpath(full_path, path).replace(os.sep, '/') if split_path: return [rel_path] + rel_path.split('/') diff --git a/tests-unit/prompt_server_test/user_manager_test.py b/tests-unit/prompt_server_test/user_manager_test.py index 936c6bd27..91a427263 100644 --- a/tests-unit/prompt_server_test/user_manager_test.py +++ b/tests-unit/prompt_server_test/user_manager_test.py @@ -80,9 +80,7 @@ async def test_listuserdata_split_path(aiohttp_client, app, tmp_path): client = await aiohttp_client(app) resp = await client.get("/userdata?dir=test_dir&recurse=true&split=true") assert resp.status == 200 - assert await resp.json() == [ - ["subdir/file1.txt", "subdir", "file1.txt"] - ] + assert await resp.json() == [["subdir/file1.txt", "subdir", "file1.txt"]] async def test_listuserdata_invalid_directory(aiohttp_client, app): @@ -118,3 +116,60 @@ async def test_listuserdata_normalized_separator(aiohttp_client, app, tmp_path): assert "/" in result[0]["path"] # Ensure forward slash is used assert "\\" not in result[0]["path"] # Ensure backslash is not present assert result[0]["path"] == "subdir/file1.txt" + + +async def test_post_userdata_new_file(aiohttp_client, app, tmp_path): + client = await aiohttp_client(app) + content = b"test content" + resp = await client.post("/userdata/test.txt", data=content) + + assert resp.status == 200 + assert await resp.text() == '"test.txt"' + + # Verify file was created with correct content + with open(tmp_path / "test.txt", "rb") as f: + assert f.read() == content + + +async def test_post_userdata_overwrite_existing(aiohttp_client, app, tmp_path): + # Create initial file + with open(tmp_path / "test.txt", "w") as f: + f.write("initial content") + + client = await aiohttp_client(app) + new_content = b"updated content" + resp = await client.post("/userdata/test.txt", data=new_content) + + assert resp.status == 200 + assert await resp.text() == '"test.txt"' + + # Verify file was overwritten + with open(tmp_path / "test.txt", "rb") as f: + assert f.read() == new_content + + +async def test_post_userdata_no_overwrite(aiohttp_client, app, tmp_path): + # Create initial file + with open(tmp_path / "test.txt", "w") as f: + f.write("initial content") + + client = await aiohttp_client(app) + resp = await client.post("/userdata/test.txt?overwrite=false", data=b"new content") + + assert resp.status == 409 + + # Verify original content unchanged + with open(tmp_path / "test.txt", "r") as f: + assert f.read() == "initial content" + + +async def test_post_userdata_full_info(aiohttp_client, app, tmp_path): + client = await aiohttp_client(app) + content = b"test content" + resp = await client.post("/userdata/test.txt?full_info=true", data=content) + + assert resp.status == 200 + result = await resp.json() + assert result["path"] == "test.txt" + assert result["size"] == len(content) + assert "modified" in result