upload volledige repo
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,38 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
|
||||
|
||||
from backend.app.db.bookmark_repository import BookmarkRepository
|
||||
|
||||
|
||||
class BookmarkRepositoryTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.temp_dir = tempfile.TemporaryDirectory()
|
||||
self.repo = BookmarkRepository(str(Path(self.temp_dir.name) / "bookmarks.db"))
|
||||
|
||||
def tearDown(self) -> None:
|
||||
self.temp_dir.cleanup()
|
||||
|
||||
def test_duplicate_path_raises_integrity_error(self) -> None:
|
||||
self.repo.create_bookmark(path="storage1/a", label="A")
|
||||
with self.assertRaises(sqlite3.IntegrityError):
|
||||
self.repo.create_bookmark(path="storage1/a", label="Again")
|
||||
|
||||
def test_list_order_created_at_desc(self) -> None:
|
||||
first = self.repo.create_bookmark(path="storage1/a", label="A")
|
||||
second = self.repo.create_bookmark(path="storage1/b", label="B")
|
||||
|
||||
items = self.repo.list_bookmarks()
|
||||
|
||||
self.assertEqual(items[0]["id"], second["id"])
|
||||
self.assertEqual(items[1]["id"], first["id"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,57 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
|
||||
|
||||
from backend.app.api.errors import AppError
|
||||
from backend.app.security.path_guard import PathGuard
|
||||
|
||||
|
||||
class PathGuardTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.temp_dir = tempfile.TemporaryDirectory()
|
||||
self.root = Path(self.temp_dir.name) / "root"
|
||||
self.root.mkdir(parents=True, exist_ok=True)
|
||||
self.other = Path(self.temp_dir.name) / "other"
|
||||
self.other.mkdir(parents=True, exist_ok=True)
|
||||
self.guard = PathGuard({"storage1": str(self.root)})
|
||||
|
||||
def tearDown(self) -> None:
|
||||
self.temp_dir.cleanup()
|
||||
|
||||
def test_resolve_under_whitelisted_root(self) -> None:
|
||||
target = self.root / "series"
|
||||
target.mkdir()
|
||||
|
||||
resolved = self.guard.resolve_directory_path("storage1/series")
|
||||
|
||||
self.assertEqual(resolved.alias, "storage1")
|
||||
self.assertEqual(resolved.relative, "storage1/series")
|
||||
self.assertEqual(resolved.absolute, target.resolve())
|
||||
|
||||
def test_rejects_path_traversal(self) -> None:
|
||||
with self.assertRaises(AppError) as ctx:
|
||||
self.guard.resolve_path("storage1/../etc")
|
||||
|
||||
self.assertEqual(ctx.exception.code, "path_traversal_detected")
|
||||
self.assertEqual(ctx.exception.status_code, 403)
|
||||
|
||||
def test_rejects_symlink_escape(self) -> None:
|
||||
outside_dir = self.other / "escape"
|
||||
outside_dir.mkdir()
|
||||
symlink = self.root / "link"
|
||||
symlink.symlink_to(outside_dir, target_is_directory=True)
|
||||
|
||||
with self.assertRaises(AppError) as ctx:
|
||||
self.guard.resolve_directory_path("storage1/link")
|
||||
|
||||
self.assertEqual(ctx.exception.code, "path_outside_whitelist")
|
||||
self.assertEqual(ctx.exception.status_code, 403)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,63 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
|
||||
|
||||
from backend.app.db.task_repository import TaskRepository
|
||||
|
||||
|
||||
class TaskRepositoryTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.temp_dir = tempfile.TemporaryDirectory()
|
||||
self.db_path = str(Path(self.temp_dir.name) / "tasks.db")
|
||||
self.repo = TaskRepository(self.db_path)
|
||||
|
||||
def tearDown(self) -> None:
|
||||
self.temp_dir.cleanup()
|
||||
|
||||
def test_list_tasks_sorted_created_at_desc(self) -> None:
|
||||
self.repo.insert_task_for_testing(
|
||||
{
|
||||
"id": "task-old",
|
||||
"operation": "copy",
|
||||
"status": "queued",
|
||||
"source": "storage1/a",
|
||||
"destination": "storage2/a",
|
||||
"created_at": "2026-03-10T09:00:00Z",
|
||||
}
|
||||
)
|
||||
self.repo.insert_task_for_testing(
|
||||
{
|
||||
"id": "task-new",
|
||||
"operation": "move",
|
||||
"status": "queued",
|
||||
"source": "storage1/b",
|
||||
"destination": "storage2/b",
|
||||
"created_at": "2026-03-10T10:00:00Z",
|
||||
}
|
||||
)
|
||||
|
||||
tasks = self.repo.list_tasks()
|
||||
|
||||
self.assertEqual([task["id"] for task in tasks], ["task-new", "task-old"])
|
||||
|
||||
def test_insert_rejects_invalid_status(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
self.repo.insert_task_for_testing(
|
||||
{
|
||||
"id": "task-x",
|
||||
"operation": "copy",
|
||||
"status": "unknown",
|
||||
"source": "storage1/a",
|
||||
"destination": "storage2/a",
|
||||
"created_at": "2026-03-10T09:00:00Z",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user