71 行
2.5 KiB
Python
71 行
2.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
from backend.dto.common import ApiError, ErrorCode
|
||
|
|
from backend.routes.profile_routes import archive_profile, delete_profile
|
||
|
|
from backend.services.profile_service import ProfileService
|
||
|
|
from core.audit_service import AuditService
|
||
|
|
from core.identity import RequestContext
|
||
|
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||
|
|
from database.repositories.profile_repository import ProfileRepository
|
||
|
|
|
||
|
|
|
||
|
|
def _fixture(tmp_path) -> tuple[ProfileService, RequestContext]:
|
||
|
|
database = Database(
|
||
|
|
DatabaseSettings(
|
||
|
|
backend="sqlite",
|
||
|
|
sqlite=SqliteSettings(path=str(tmp_path / "profile-archive.sqlite3")),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
database.initialize()
|
||
|
|
service = ProfileService(
|
||
|
|
repository=ProfileRepository(database),
|
||
|
|
audit_service=AuditService(database),
|
||
|
|
)
|
||
|
|
ctx = RequestContext.system("profile-archive-test")
|
||
|
|
service.create_profile({"profile_id": "profile-archive", "name": "Archivable"}, ctx)
|
||
|
|
return service, ctx
|
||
|
|
|
||
|
|
|
||
|
|
def test_archive_route_marks_profile_archived_instead_of_deleting(tmp_path) -> None:
|
||
|
|
service, ctx = _fixture(tmp_path)
|
||
|
|
|
||
|
|
response = archive_profile(ctx, "profile-archive", service)
|
||
|
|
|
||
|
|
assert response["ok"] is True
|
||
|
|
assert response["data"]["status"] == "archived"
|
||
|
|
# An archived profile stays readable -- that is the whole difference from
|
||
|
|
# delete, and the point of offering a reversible action.
|
||
|
|
assert service.get_profile("profile-archive", ctx).status == "archived"
|
||
|
|
|
||
|
|
|
||
|
|
def test_archive_route_keeps_profile_restorable(tmp_path) -> None:
|
||
|
|
service, ctx = _fixture(tmp_path)
|
||
|
|
|
||
|
|
archive_profile(ctx, "profile-archive", service)
|
||
|
|
restored = service.update_profile("profile-archive", {"status": "active"}, ctx)
|
||
|
|
|
||
|
|
assert restored.status == "active"
|
||
|
|
|
||
|
|
|
||
|
|
def test_delete_route_still_soft_deletes(tmp_path) -> None:
|
||
|
|
service, ctx = _fixture(tmp_path)
|
||
|
|
|
||
|
|
response = delete_profile(ctx, "profile-archive", service)
|
||
|
|
|
||
|
|
assert response["ok"] is True
|
||
|
|
assert response["data"]["status"] == "deleted"
|
||
|
|
with pytest.raises(ApiError) as excinfo:
|
||
|
|
service.get_profile("profile-archive", ctx)
|
||
|
|
assert excinfo.value.error_code == ErrorCode.RESOURCE_NOT_FOUND
|
||
|
|
|
||
|
|
|
||
|
|
def test_archive_unknown_profile_reports_not_found(tmp_path) -> None:
|
||
|
|
service, ctx = _fixture(tmp_path)
|
||
|
|
|
||
|
|
response = archive_profile(ctx, "profile-missing", service)
|
||
|
|
|
||
|
|
assert response["ok"] is False
|
||
|
|
assert response["error_code"] == ErrorCode.RESOURCE_NOT_FOUND.value
|