init
This commit is contained in:
commit
fe636432d0
154 files changed
+30381
No files matched your search
@@ -0,0 +1,238 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
from app.config import get_settings
|
||||
from app.database import SessionLocal
|
||||
from app.models import DifySyncRecord, Document, DocumentVersion, Group, Job, User
|
||||
from app.services.storage_service import version_dir
|
||||
|
||||
|
||||
def _token(client, username: str = "admin", password: str = "admin123456"):
|
||||
response = client.post("/api/auth/login", json={"username": username, "password": password})
|
||||
return response.json()["access_token"]
|
||||
|
||||
|
||||
def _headers(client, username: str = "admin", password: str = "admin123456"):
|
||||
return {"Authorization": f"Bearer {_token(client, username=username, password=password)}"}
|
||||
|
||||
|
||||
def _seed_document_for_delete(*, with_remote_sync: bool) -> tuple[int, Path, Path, Path]:
|
||||
settings = get_settings()
|
||||
db = SessionLocal()
|
||||
try:
|
||||
admin = db.query(User).filter(User.username == "admin").first()
|
||||
assert admin is not None
|
||||
|
||||
group = Group(name=f"delete-{uuid.uuid4().hex[:8]}", description="Delete coverage", created_by_id=admin.id, department_id=1)
|
||||
db.add(group)
|
||||
db.commit()
|
||||
db.refresh(group)
|
||||
|
||||
document = Document(group_id=group.id, title="manual", created_by_id=admin.id, is_deleted=False)
|
||||
db.add(document)
|
||||
db.commit()
|
||||
db.refresh(document)
|
||||
|
||||
source_root = version_dir("original", group.id, document.id, 1)
|
||||
markdown_root = version_dir("markdown", group.id, document.id, 1)
|
||||
assets_root = version_dir("assets", group.id, document.id, 1)
|
||||
source_root.mkdir(parents=True, exist_ok=True)
|
||||
markdown_root.mkdir(parents=True, exist_ok=True)
|
||||
assets_root.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
source_path = source_root / "manual.docx"
|
||||
markdown_path = markdown_root / "document.md"
|
||||
asset_path = assets_root / "img_001.png"
|
||||
source_path.write_bytes(b"source")
|
||||
markdown_path.write_text("# Manual", encoding="utf-8")
|
||||
asset_path.write_bytes(b"png")
|
||||
|
||||
version = DocumentVersion(
|
||||
document_id=document.id,
|
||||
version_no=1,
|
||||
is_current=True,
|
||||
source_filename="manual.docx",
|
||||
source_ext=".docx",
|
||||
source_path=str(source_path),
|
||||
source_sha256=uuid.uuid4().hex,
|
||||
md_path=str(markdown_path),
|
||||
md_public_url="http://testserver/storage/markdown/document.md",
|
||||
asset_dir=str(assets_root.relative_to(settings.data_root)),
|
||||
file_size=source_path.stat().st_size,
|
||||
uploader_id=admin.id,
|
||||
convert_status="success",
|
||||
sync_status="synced" if with_remote_sync else "unsynced",
|
||||
)
|
||||
db.add(version)
|
||||
db.commit()
|
||||
db.refresh(version)
|
||||
|
||||
if with_remote_sync:
|
||||
db.add(
|
||||
DifySyncRecord(
|
||||
version_id=version.id,
|
||||
dataset_id="dataset-001",
|
||||
remote_document_id="remote-001",
|
||||
status="synced",
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
return document.id, source_root, markdown_root, assets_root
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def test_delete_file_marks_document_deleted_and_enqueues_cleanup(client, monkeypatch):
|
||||
document_id, source_root, markdown_root, assets_root = _seed_document_for_delete(with_remote_sync=False)
|
||||
enqueue_calls: list[tuple[str, tuple[object, ...]]] = []
|
||||
|
||||
class FakeQueue:
|
||||
def enqueue(self, task_name: str, *args, **kwargs):
|
||||
enqueue_calls.append((task_name, args))
|
||||
return SimpleNamespace(id="queued-delete-job")
|
||||
|
||||
monkeypatch.setattr("app.api.files.get_queue", lambda: FakeQueue())
|
||||
|
||||
response = client.delete(f"/api/files/{document_id}?delete_dify=false", headers=_headers(client))
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"deleted": True}
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
document = db.query(Document).filter(Document.id == document_id).first()
|
||||
job = db.query(Job).filter(Job.document_id == document_id, Job.job_type == "delete").first()
|
||||
assert document is not None
|
||||
assert job is not None
|
||||
assert document.is_deleted is True
|
||||
assert "__deleted__" in document.title
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
assert enqueue_calls
|
||||
assert enqueue_calls[0][0] == "app.tasks.run_delete_document_job"
|
||||
assert enqueue_calls[0][1][1] == document_id
|
||||
assert source_root.exists()
|
||||
assert markdown_root.exists()
|
||||
assert assets_root.exists()
|
||||
|
||||
|
||||
def test_batch_markdown_download_returns_zip_file(client):
|
||||
document_id, source_root, markdown_root, assets_root = _seed_document_for_delete(with_remote_sync=False)
|
||||
|
||||
response = client.post(
|
||||
"/api/files/download-batch",
|
||||
json={"document_ids": [document_id], "kind": "md"},
|
||||
headers=_headers(client),
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert "zip" in response.headers["content-type"]
|
||||
assert len(response.content) > 0
|
||||
|
||||
|
||||
def test_sync_endpoint_rejects_already_synced_current_version(client):
|
||||
settings = get_settings()
|
||||
db = SessionLocal()
|
||||
try:
|
||||
admin = db.query(User).filter(User.username == "admin").first()
|
||||
assert admin is not None
|
||||
group = Group(name=f"sync-{uuid.uuid4().hex[:8]}", description="Sync coverage", created_by_id=admin.id, department_id=1)
|
||||
db.add(group)
|
||||
db.commit()
|
||||
db.refresh(group)
|
||||
|
||||
document = Document(group_id=group.id, title="already-synced", created_by_id=admin.id, is_deleted=False)
|
||||
db.add(document)
|
||||
db.commit()
|
||||
db.refresh(document)
|
||||
|
||||
markdown_root = version_dir("markdown", group.id, document.id, 1)
|
||||
markdown_root.mkdir(parents=True, exist_ok=True)
|
||||
markdown_path = markdown_root / "document.md"
|
||||
markdown_path.write_text("# Synced", encoding="utf-8")
|
||||
|
||||
version = DocumentVersion(
|
||||
document_id=document.id,
|
||||
version_no=1,
|
||||
is_current=True,
|
||||
source_filename="already-synced.docx",
|
||||
source_ext=".docx",
|
||||
source_path=str(settings.data_root / "tmp" / "already-synced.docx"),
|
||||
source_sha256=uuid.uuid4().hex,
|
||||
md_path=str(markdown_path),
|
||||
file_size=1,
|
||||
uploader_id=admin.id,
|
||||
convert_status="success",
|
||||
sync_status="synced",
|
||||
)
|
||||
db.add(version)
|
||||
db.commit()
|
||||
db.refresh(version)
|
||||
|
||||
db.add(
|
||||
DifySyncRecord(
|
||||
version_id=version.id,
|
||||
dataset_id="dataset-001",
|
||||
remote_document_id="remote-001",
|
||||
status="synced",
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
file_id = document.id
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
response = client.post(f"/api/files/{file_id}/sync-dify", headers=_headers(client))
|
||||
assert response.status_code == 409
|
||||
assert response.json()["detail"] == "Current version is already synced to Dify."
|
||||
|
||||
|
||||
def test_sync_endpoint_rejects_current_version_already_syncing(client):
|
||||
settings = get_settings()
|
||||
db = SessionLocal()
|
||||
try:
|
||||
admin = db.query(User).filter(User.username == "admin").first()
|
||||
assert admin is not None
|
||||
group = Group(name=f"syncing-{uuid.uuid4().hex[:8]}", description="Sync coverage", created_by_id=admin.id, department_id=1)
|
||||
db.add(group)
|
||||
db.commit()
|
||||
db.refresh(group)
|
||||
|
||||
document = Document(group_id=group.id, title="already-syncing", created_by_id=admin.id, is_deleted=False)
|
||||
db.add(document)
|
||||
db.commit()
|
||||
db.refresh(document)
|
||||
|
||||
markdown_root = version_dir("markdown", group.id, document.id, 1)
|
||||
markdown_root.mkdir(parents=True, exist_ok=True)
|
||||
markdown_path = markdown_root / "document.md"
|
||||
markdown_path.write_text("# Syncing", encoding="utf-8")
|
||||
|
||||
version = DocumentVersion(
|
||||
document_id=document.id,
|
||||
version_no=1,
|
||||
is_current=True,
|
||||
source_filename="already-syncing.docx",
|
||||
source_ext=".docx",
|
||||
source_path=str(settings.data_root / "tmp" / "already-syncing.docx"),
|
||||
source_sha256=uuid.uuid4().hex,
|
||||
md_path=str(markdown_path),
|
||||
file_size=1,
|
||||
uploader_id=admin.id,
|
||||
convert_status="success",
|
||||
sync_status="syncing",
|
||||
)
|
||||
db.add(version)
|
||||
db.commit()
|
||||
db.refresh(version)
|
||||
file_id = document.id
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
response = client.post(f"/api/files/{file_id}/sync-dify", headers=_headers(client))
|
||||
assert response.status_code == 409
|
||||
assert response.json()["detail"] == "Current version is already syncing to Dify."
|
||||
Reference in new issue
Block a user