|
| 1 | +import pathlib |
| 2 | +import shutil |
| 3 | +import tempfile |
| 4 | +import unittest |
| 5 | + |
| 6 | +from fastapi.testclient import TestClient |
| 7 | +from sqlalchemy import create_engine |
| 8 | +from sqlalchemy.orm import sessionmaker |
| 9 | + |
| 10 | +from server_api.auth import database as auth_database |
| 11 | +from server_api.auth import models |
| 12 | +from server_api.main import app as server_api_app |
| 13 | + |
| 14 | + |
| 15 | +class FileWorkspaceRouteTests(unittest.TestCase): |
| 16 | + def setUp(self): |
| 17 | + self.temp_dir = tempfile.TemporaryDirectory() |
| 18 | + self.db_path = pathlib.Path(self.temp_dir.name) / "auth-test.db" |
| 19 | + self.engine = create_engine( |
| 20 | + f"sqlite:///{self.db_path}", connect_args={"check_same_thread": False} |
| 21 | + ) |
| 22 | + self.SessionLocal = sessionmaker( |
| 23 | + autocommit=False, autoflush=False, bind=self.engine |
| 24 | + ) |
| 25 | + models.Base.metadata.create_all(bind=self.engine) |
| 26 | + |
| 27 | + def override_get_db(): |
| 28 | + db = self.SessionLocal() |
| 29 | + try: |
| 30 | + yield db |
| 31 | + finally: |
| 32 | + db.close() |
| 33 | + |
| 34 | + server_api_app.dependency_overrides[auth_database.get_db] = override_get_db |
| 35 | + self.client = TestClient(server_api_app) |
| 36 | + |
| 37 | + with self.SessionLocal() as db: |
| 38 | + guest = models.User( |
| 39 | + username="guest", |
| 40 | + email=None, |
| 41 | + hashed_password="guest", |
| 42 | + ) |
| 43 | + db.add(guest) |
| 44 | + db.commit() |
| 45 | + db.refresh(guest) |
| 46 | + self.user_id = guest.id |
| 47 | + |
| 48 | + self.uploads_root = pathlib.Path("uploads") / str(self.user_id) |
| 49 | + if self.uploads_root.exists(): |
| 50 | + shutil.rmtree(self.uploads_root) |
| 51 | + self.uploads_root.mkdir(parents=True, exist_ok=True) |
| 52 | + |
| 53 | + def tearDown(self): |
| 54 | + server_api_app.dependency_overrides.clear() |
| 55 | + if self.uploads_root.exists(): |
| 56 | + shutil.rmtree(self.uploads_root, ignore_errors=True) |
| 57 | + self.engine.dispose() |
| 58 | + self.temp_dir.cleanup() |
| 59 | + |
| 60 | + def _create_file( |
| 61 | + self, |
| 62 | + *, |
| 63 | + name, |
| 64 | + path="root", |
| 65 | + is_folder=False, |
| 66 | + physical_path=None, |
| 67 | + size="1B", |
| 68 | + file_type="text/plain", |
| 69 | + ): |
| 70 | + with self.SessionLocal() as db: |
| 71 | + node = models.File( |
| 72 | + user_id=self.user_id, |
| 73 | + name=name, |
| 74 | + path=path, |
| 75 | + is_folder=is_folder, |
| 76 | + physical_path=physical_path, |
| 77 | + size=size, |
| 78 | + type=file_type, |
| 79 | + ) |
| 80 | + db.add(node) |
| 81 | + db.commit() |
| 82 | + db.refresh(node) |
| 83 | + return node.id |
| 84 | + |
| 85 | + def test_mount_directory_skips_os_metadata_files(self): |
| 86 | + mount_root = pathlib.Path(self.temp_dir.name) / "project" |
| 87 | + mount_root.mkdir() |
| 88 | + (mount_root / ".DS_Store").write_text("junk", encoding="utf-8") |
| 89 | + (mount_root / "volume.tif").write_text("data", encoding="utf-8") |
| 90 | + |
| 91 | + response = self.client.post( |
| 92 | + "/files/mount", |
| 93 | + json={ |
| 94 | + "directory_path": str(mount_root), |
| 95 | + "destination_path": "root", |
| 96 | + }, |
| 97 | + ) |
| 98 | + |
| 99 | + self.assertEqual(response.status_code, 200) |
| 100 | + self.assertEqual(response.json()["mounted_files"], 1) |
| 101 | + |
| 102 | + files_response = self.client.get("/files") |
| 103 | + self.assertEqual(files_response.status_code, 200) |
| 104 | + names = {item["name"] for item in files_response.json()} |
| 105 | + self.assertIn("project", names) |
| 106 | + self.assertIn("volume.tif", names) |
| 107 | + self.assertNotIn(".DS_Store", names) |
| 108 | + |
| 109 | + def test_reset_workspace_preserves_mounted_sources_and_clears_uploads(self): |
| 110 | + mount_root = pathlib.Path(self.temp_dir.name) / "mounted-project" |
| 111 | + mount_root.mkdir() |
| 112 | + mounted_file = mount_root / "volume.tif" |
| 113 | + mounted_file.write_text("data", encoding="utf-8") |
| 114 | + |
| 115 | + mount_response = self.client.post( |
| 116 | + "/files/mount", |
| 117 | + json={ |
| 118 | + "directory_path": str(mount_root), |
| 119 | + "destination_path": "root", |
| 120 | + }, |
| 121 | + ) |
| 122 | + self.assertEqual(mount_response.status_code, 200) |
| 123 | + |
| 124 | + upload_file = self.uploads_root / "uploaded.tif" |
| 125 | + upload_file.write_text("upload", encoding="utf-8") |
| 126 | + self._create_file( |
| 127 | + name="uploaded.tif", |
| 128 | + physical_path=str(upload_file), |
| 129 | + size="6B", |
| 130 | + ) |
| 131 | + |
| 132 | + response = self.client.delete("/files/workspace") |
| 133 | + |
| 134 | + self.assertEqual(response.status_code, 200) |
| 135 | + self.assertGreaterEqual(response.json()["deleted_count"], 3) |
| 136 | + self.assertEqual(response.json()["mounted_root_count"], 1) |
| 137 | + self.assertTrue(mount_root.exists()) |
| 138 | + self.assertTrue(mounted_file.exists()) |
| 139 | + self.assertFalse(upload_file.exists()) |
| 140 | + self.assertTrue(self.uploads_root.exists()) |
| 141 | + |
| 142 | + files_response = self.client.get("/files") |
| 143 | + self.assertEqual(files_response.status_code, 200) |
| 144 | + self.assertEqual(files_response.json(), []) |
| 145 | + |
| 146 | + |
| 147 | +if __name__ == "__main__": |
| 148 | + unittest.main() |
0 commit comments