feat: 完善知识库预览功能与配置管理优化
This commit is contained in:
60
server/tests/test_config_settings_reload.py
Normal file
60
server/tests/test_config_settings_reload.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
from app.core.config import Settings, get_settings
|
||||
|
||||
|
||||
def test_get_settings_refreshes_when_env_file_changes(tmp_path, monkeypatch) -> None:
|
||||
env_file = tmp_path / ".env"
|
||||
env_file.write_text("ONLYOFFICE_ENABLED=false\n", encoding="utf-8")
|
||||
|
||||
original_env_file = Settings.model_config.get("env_file")
|
||||
monkeypatch.setitem(Settings.model_config, "env_file", (env_file,))
|
||||
get_settings.cache_clear()
|
||||
|
||||
try:
|
||||
first = get_settings()
|
||||
assert first.onlyoffice_enabled is False
|
||||
|
||||
env_file.write_text("ONLYOFFICE_ENABLED=true\n", encoding="utf-8")
|
||||
os.utime(env_file, None)
|
||||
|
||||
second = get_settings()
|
||||
assert second.onlyoffice_enabled is True
|
||||
finally:
|
||||
monkeypatch.setitem(Settings.model_config, "env_file", original_env_file)
|
||||
get_settings.cache_clear()
|
||||
|
||||
|
||||
def test_onlyoffice_values_prefer_env_file_over_inherited_environment(tmp_path, monkeypatch) -> None:
|
||||
env_file = tmp_path / ".env"
|
||||
env_file.write_text(
|
||||
"\n".join(
|
||||
[
|
||||
"ONLYOFFICE_ENABLED=true",
|
||||
"ONLYOFFICE_PUBLIC_URL=http://10.10.10.122:8082",
|
||||
"ONLYOFFICE_BACKEND_URL=http://main:8000",
|
||||
"ONLYOFFICE_JWT_SECRET=change-me-onlyoffice",
|
||||
]
|
||||
)
|
||||
+ "\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
original_env_file = Settings.model_config.get("env_file")
|
||||
monkeypatch.setitem(Settings.model_config, "env_file", (env_file,))
|
||||
monkeypatch.setenv("ONLYOFFICE_ENABLED", "false")
|
||||
monkeypatch.setenv("ONLYOFFICE_PUBLIC_URL", "http://127.0.0.1:8082")
|
||||
get_settings.cache_clear()
|
||||
|
||||
try:
|
||||
settings = get_settings()
|
||||
|
||||
assert settings.onlyoffice_enabled is True
|
||||
assert settings.onlyoffice_public_url == "http://10.10.10.122:8082"
|
||||
assert settings.onlyoffice_backend_url == "http://main:8000"
|
||||
assert settings.onlyoffice_jwt_secret == "change-me-onlyoffice"
|
||||
finally:
|
||||
monkeypatch.setitem(Settings.model_config, "env_file", original_env_file)
|
||||
get_settings.cache_clear()
|
||||
81
server/tests/test_env_file_precedence.py
Normal file
81
server/tests/test_env_file_precedence.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
|
||||
def _run_script_prefix(
|
||||
tmp_path: Path,
|
||||
relative_script_path: str,
|
||||
env_file_content: str,
|
||||
env: dict[str, str],
|
||||
output_vars: list[str],
|
||||
) -> subprocess.CompletedProcess[str]:
|
||||
project_dir = tmp_path / "project"
|
||||
script_source = Path(__file__).resolve().parents[2] / relative_script_path
|
||||
script_copy = project_dir / relative_script_path
|
||||
script_copy.parent.mkdir(parents=True, exist_ok=True)
|
||||
script_copy.write_text(script_source.read_text(encoding="utf-8"), encoding="utf-8")
|
||||
(project_dir / ".env").write_text(env_file_content, encoding="utf-8")
|
||||
|
||||
script_prefix = script_copy.read_text(encoding="utf-8").split('case "$MODE" in', 1)[0]
|
||||
print_lines = "\n".join(f'printf "{name}=%s\\n" "${{{name}:-}}"' for name in output_vars)
|
||||
command = f"""{script_prefix}
|
||||
{print_lines}
|
||||
"""
|
||||
|
||||
return subprocess.run(
|
||||
["bash", "-c", command, str(script_copy)],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env={**os.environ, **env, "MODE": "test"},
|
||||
cwd=script_copy.parent,
|
||||
check=False,
|
||||
)
|
||||
|
||||
|
||||
def test_server_start_can_prefer_env_file_over_inherited_onlyoffice_values(tmp_path: Path) -> None:
|
||||
result = _run_script_prefix(
|
||||
tmp_path,
|
||||
"server/server_start.sh",
|
||||
env_file_content=(
|
||||
"ONLYOFFICE_ENABLED=true\n"
|
||||
"ONLYOFFICE_PUBLIC_URL=http://10.10.10.122:8082\n"
|
||||
"ONLYOFFICE_BACKEND_URL=http://main:8000\n"
|
||||
"ONLYOFFICE_JWT_SECRET=change-me-onlyoffice\n"
|
||||
),
|
||||
env={
|
||||
"ONLYOFFICE_ENABLED": "false",
|
||||
"ONLYOFFICE_PUBLIC_URL": "http://127.0.0.1:8082",
|
||||
"X_FINANCIAL_PREFER_ENV_FILE": "true",
|
||||
},
|
||||
output_vars=["ONLYOFFICE_ENABLED", "ONLYOFFICE_PUBLIC_URL"],
|
||||
)
|
||||
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert "ONLYOFFICE_ENABLED=true" in result.stdout
|
||||
assert "ONLYOFFICE_PUBLIC_URL=http://10.10.10.122:8082" in result.stdout
|
||||
|
||||
|
||||
def test_root_start_can_prefer_env_file_over_inherited_onlyoffice_values(tmp_path: Path) -> None:
|
||||
result = _run_script_prefix(
|
||||
tmp_path,
|
||||
"start.sh",
|
||||
env_file_content=(
|
||||
"ONLYOFFICE_ENABLED=true\n"
|
||||
"ONLYOFFICE_PUBLIC_URL=http://10.10.10.122:8082\n"
|
||||
"ONLYOFFICE_BACKEND_URL=http://main:8000\n"
|
||||
"ONLYOFFICE_JWT_SECRET=change-me-onlyoffice\n"
|
||||
),
|
||||
env={
|
||||
"ONLYOFFICE_ENABLED": "false",
|
||||
"ONLYOFFICE_PUBLIC_URL": "http://127.0.0.1:8082",
|
||||
"X_FINANCIAL_PREFER_ENV_FILE": "true",
|
||||
},
|
||||
output_vars=["ONLYOFFICE_ENABLED", "ONLYOFFICE_PUBLIC_URL"],
|
||||
)
|
||||
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert "ONLYOFFICE_ENABLED=true" in result.stdout
|
||||
assert "ONLYOFFICE_PUBLIC_URL=http://10.10.10.122:8082" in result.stdout
|
||||
55
server/tests/test_knowledge_onlyoffice_config.py
Normal file
55
server/tests/test_knowledge_onlyoffice_config.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.api.deps import CurrentUserContext
|
||||
from app.core.config import Settings, get_settings
|
||||
from app.services.knowledge import KnowledgeService
|
||||
|
||||
|
||||
def test_onlyoffice_config_is_read_only_for_admin_users(tmp_path, monkeypatch) -> None:
|
||||
env_file = tmp_path / ".env"
|
||||
env_file.write_text(
|
||||
"\n".join(
|
||||
[
|
||||
"ONLYOFFICE_ENABLED=true",
|
||||
"ONLYOFFICE_PUBLIC_URL=http://10.10.10.122:8082",
|
||||
"ONLYOFFICE_BACKEND_URL=http://main:8000",
|
||||
"ONLYOFFICE_JWT_SECRET=change-me-onlyoffice",
|
||||
]
|
||||
)
|
||||
+ "\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
original_env_file = Settings.model_config.get("env_file")
|
||||
monkeypatch.setitem(Settings.model_config, "env_file", (env_file,))
|
||||
get_settings.cache_clear()
|
||||
|
||||
try:
|
||||
service = KnowledgeService(storage_root=tmp_path)
|
||||
service.ensure_library_ready()
|
||||
|
||||
document_id = "readonly-docx"
|
||||
folder = "制度政策"
|
||||
stored_name = f"{document_id}__制度预览.docx"
|
||||
target_path = tmp_path / "knowledge" / folder / stored_name
|
||||
target_path.write_bytes(b"fake-docx-content")
|
||||
|
||||
current_user = CurrentUserContext(
|
||||
username="admin",
|
||||
name="管理员",
|
||||
role_codes=["manager"],
|
||||
is_admin=True,
|
||||
)
|
||||
|
||||
config = service.build_onlyoffice_config(document_id, current_user)
|
||||
permissions = config.config["document"]["permissions"]
|
||||
customization = config.config["editorConfig"]["customization"]
|
||||
|
||||
assert config.documentServerUrl == "http://10.10.10.122:8082"
|
||||
assert config.config["editorConfig"]["mode"] == "view"
|
||||
assert permissions["edit"] is False
|
||||
assert permissions["download"] is True
|
||||
assert customization["autosave"] is False
|
||||
assert customization["forcesave"] is False
|
||||
finally:
|
||||
monkeypatch.setitem(Settings.model_config, "env_file", original_env_file)
|
||||
get_settings.cache_clear()
|
||||
Reference in New Issue
Block a user