chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,157 @@
|
||||
import inspect
|
||||
from collections.abc import Callable
|
||||
from typing import cast
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from controllers.inner_api.agent.files import (
|
||||
AgentFileDownloadRequestApi,
|
||||
AgentFileUploadRequestApi,
|
||||
AgentFileUploadRequestPayload,
|
||||
)
|
||||
from core.workflow.file_reference import build_file_reference
|
||||
from models.account import Account, Tenant
|
||||
from services.file_request_service import DownloadFileRequestResult
|
||||
from tests.helpers.unit.config_override import apply_config_overrides
|
||||
from tests.helpers.unit.model_factories import make_tenant
|
||||
|
||||
MODULE = "controllers.inner_api.agent.files"
|
||||
|
||||
|
||||
def _tenant() -> Tenant:
|
||||
return make_tenant(name="Test Workspace")
|
||||
|
||||
|
||||
def _raw[R](method: Callable[..., R]) -> Callable[..., R]:
|
||||
return cast(Callable[..., R], inspect.unwrap(method))
|
||||
|
||||
|
||||
def test_upload_request_returns_origin_free_uri(app: Flask, unbound_session: Session) -> None:
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "execution-user-1",
|
||||
"filename": "report.pdf",
|
||||
"mimetype": "application/pdf",
|
||||
"conversation_id": "conversation-1",
|
||||
"max_size": 64 * 1024 * 1024,
|
||||
}
|
||||
tenant = _tenant()
|
||||
user = Account(name="Canonical user", email="canonical@example.com")
|
||||
user.id = "canonical-end-user-1"
|
||||
session = unbound_session
|
||||
with app.test_request_context("/", method="POST", json=payload):
|
||||
with (
|
||||
patch(f"{MODULE}.application_services") as service_provider,
|
||||
patch(f"{MODULE}.get_user", return_value=user),
|
||||
patch(f"{MODULE}.get_signed_file_uri_for_plugin", return_value="/files/upload/for-plugin?sign=1") as sign,
|
||||
):
|
||||
tenant_service = service_provider.return_value.workspaces.management
|
||||
service_provider.return_value.workspaces.members = tenant_service
|
||||
tenant_service.get.return_value = tenant
|
||||
response = _raw(AgentFileUploadRequestApi.post)(AgentFileUploadRequestApi(), session)
|
||||
|
||||
assert response == {"upload_uri": "/files/upload/for-plugin?sign=1"}
|
||||
tenant_service.get.assert_called_once_with("tenant-1")
|
||||
sign.assert_called_once_with(
|
||||
filename="report.pdf",
|
||||
mimetype="application/pdf",
|
||||
tenant_id="tenant-1",
|
||||
user_id="canonical-end-user-1",
|
||||
conversation_id="conversation-1",
|
||||
user_from=None,
|
||||
max_size=64 * 1024 * 1024,
|
||||
)
|
||||
|
||||
|
||||
def test_upload_request_payload_requires_non_negative_max_size() -> None:
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "user-1",
|
||||
"filename": "report.pdf",
|
||||
"mimetype": "application/pdf",
|
||||
}
|
||||
|
||||
with pytest.raises(ValueError, match="1 validation error for AgentFileUploadRequestPayload\\\nmax_size"):
|
||||
AgentFileUploadRequestPayload.model_validate(payload)
|
||||
with pytest.raises(ValueError, match="1 validation error for AgentFileUploadRequestPayload\\\nmax_size"):
|
||||
AgentFileUploadRequestPayload.model_validate({**payload, "max_size": -1})
|
||||
|
||||
assert AgentFileUploadRequestPayload.model_validate({**payload, "max_size": 0}).max_size == 0
|
||||
|
||||
|
||||
def test_download_request_returns_origin_free_uri_for_sandbox(app: Flask, unbound_session: Session) -> None:
|
||||
reference = build_file_reference(record_id="tool-file-1")
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "user-1",
|
||||
"user_from": "account",
|
||||
"invoke_from": "debugger",
|
||||
"file": {"transfer_method": "tool_file", "reference": reference},
|
||||
"for_frontend": False,
|
||||
}
|
||||
session = unbound_session
|
||||
with app.test_request_context("/", method="POST", json=payload):
|
||||
with (
|
||||
patch(f"{MODULE}.application_services") as service_provider,
|
||||
patch(f"{MODULE}.FileRequestService") as service,
|
||||
):
|
||||
tenant_service = service_provider.return_value.workspaces.management
|
||||
service_provider.return_value.workspaces.members = tenant_service
|
||||
tenant_service.get.return_value = _tenant()
|
||||
service.return_value.request_download.return_value = DownloadFileRequestResult(
|
||||
filename="report.pdf",
|
||||
mime_type="application/pdf",
|
||||
size=123,
|
||||
download_uri="/files/tools/tool-file-1.pdf?sign=1",
|
||||
)
|
||||
response = _raw(AgentFileDownloadRequestApi.post)(AgentFileDownloadRequestApi(), session)
|
||||
|
||||
assert response == {
|
||||
"filename": "report.pdf",
|
||||
"mime_type": "application/pdf",
|
||||
"size": 123,
|
||||
"download_uri": "/files/tools/tool-file-1.pdf?sign=1",
|
||||
}
|
||||
service.return_value.request_download.assert_called_once_with(
|
||||
tenant_id="tenant-1",
|
||||
user_id="user-1",
|
||||
user_from="account",
|
||||
invoke_from="debugger",
|
||||
file_mapping={"transfer_method": "tool_file", "reference": reference},
|
||||
)
|
||||
|
||||
|
||||
def test_download_request_binds_frontend_url(
|
||||
app: Flask, monkeypatch: pytest.MonkeyPatch, unbound_session: Session
|
||||
) -> None:
|
||||
reference = build_file_reference(record_id="tool-file-1")
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "user-1",
|
||||
"user_from": "account",
|
||||
"invoke_from": "debugger",
|
||||
"file": {"transfer_method": "tool_file", "reference": reference},
|
||||
"for_frontend": True,
|
||||
}
|
||||
apply_config_overrides(monkeypatch, FILES_URL="https://files.example.com")
|
||||
session = unbound_session
|
||||
with app.test_request_context("/", method="POST", json=payload):
|
||||
with (
|
||||
patch(f"{MODULE}.application_services") as service_provider,
|
||||
patch(f"{MODULE}.FileRequestService") as service,
|
||||
):
|
||||
tenant_service = service_provider.return_value.workspaces.management
|
||||
service_provider.return_value.workspaces.members = tenant_service
|
||||
tenant_service.get.return_value = _tenant()
|
||||
service.return_value.request_download.return_value = DownloadFileRequestResult(
|
||||
filename="report.pdf",
|
||||
mime_type="application/pdf",
|
||||
size=123,
|
||||
download_uri="/files/tools/tool-file-1.pdf?sign=1",
|
||||
)
|
||||
response = _raw(AgentFileDownloadRequestApi.post)(AgentFileDownloadRequestApi(), session)
|
||||
|
||||
assert response["download_uri"] == "https://files.example.com/files/tools/tool-file-1.pdf?sign=1"
|
||||
@@ -0,0 +1,120 @@
|
||||
"""Queue admission and authorization boundaries for runtime quality reports."""
|
||||
|
||||
from collections.abc import Iterator
|
||||
from typing import TypedDict
|
||||
from unittest.mock import MagicMock, patch
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from dify_agent.protocol.knowledge_fs import KnowledgeFsError
|
||||
from flask import Flask
|
||||
from flask.testing import FlaskClient
|
||||
from flask_restx.swagger import Swagger
|
||||
|
||||
from controllers.inner_api import api as inner_api
|
||||
from controllers.inner_api import bp as inner_api_bp
|
||||
from tests.helpers.unit.config_override import config_overrides_context
|
||||
|
||||
PATH = "/inner/api/agent/knowledge/investigations"
|
||||
HEADERS = {"X-Inner-Api-Key": "inner-key"}
|
||||
|
||||
|
||||
class _Report(TypedDict):
|
||||
investigation_id: str
|
||||
execution_context: dict[str, str]
|
||||
bindings: list[dict[str, str]]
|
||||
query: str
|
||||
status: str
|
||||
attempts: list[dict[str, object]]
|
||||
|
||||
|
||||
def _report() -> _Report:
|
||||
spaces = [str(uuid4()), str(uuid4())]
|
||||
return {
|
||||
"investigation_id": str(uuid4()),
|
||||
"execution_context": {"tenant_id": "tenant", "agent_mode": "agent_app", "invoke_from": "published"},
|
||||
"bindings": [
|
||||
{"id": f"docs-{i}", "name": f"Docs {i}", "control_space_id": space} for i, space in enumerate(spaces)
|
||||
],
|
||||
"query": "refund and renewal pricing",
|
||||
"status": "completed",
|
||||
"attempts": [
|
||||
{
|
||||
"command_id": str(uuid4()),
|
||||
"control_space_id": space,
|
||||
"command": "search",
|
||||
"outcome": "empty",
|
||||
"started_at_ms": 1,
|
||||
"elapsed_ms": 1,
|
||||
"delivered": True,
|
||||
}
|
||||
for space in [*spaces, spaces[0]]
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def boundary() -> Iterator[tuple[FlaskClient, MagicMock, MagicMock]]:
|
||||
app = Flask(__name__)
|
||||
app.config["TESTING"] = True
|
||||
app.register_blueprint(inner_api_bp)
|
||||
with (
|
||||
config_overrides_context(PLUGIN_DAEMON_KEY="plugin-key", INNER_API_KEY_FOR_PLUGIN="inner-key"),
|
||||
patch("controllers.console.wraps._is_setup_completed", return_value=True),
|
||||
patch("controllers.inner_api.agent.knowledge.AgentKnowledgeGateway") as gateway,
|
||||
patch(
|
||||
"tasks.knowledge_fs_agent_investigation_tasks.capture_agent_knowledge_investigation_task.delay"
|
||||
) as enqueue,
|
||||
):
|
||||
yield app.test_client(), gateway.return_value, enqueue
|
||||
|
||||
|
||||
def test_queues_once_per_space_with_original_identity(boundary: tuple[FlaskClient, MagicMock, MagicMock]) -> None:
|
||||
client, gateway, enqueue = boundary
|
||||
payload = _report()
|
||||
response = client.post(PATH, json=payload, headers=HEADERS)
|
||||
|
||||
assert response.status_code == 202
|
||||
assert response.get_json() == {"accepted": True}
|
||||
gateway.authorize_investigation.assert_called_once()
|
||||
assert enqueue.call_count == 2
|
||||
assert {call.kwargs["control_space_id"] for call in enqueue.call_args_list} == {
|
||||
binding["control_space_id"] for binding in payload["bindings"]
|
||||
}
|
||||
assert all(
|
||||
call.kwargs["report"]["investigation_id"] == payload["investigation_id"] for call in enqueue.call_args_list
|
||||
)
|
||||
gateway.capture_investigation.assert_not_called()
|
||||
|
||||
|
||||
def test_revoked_scope_cannot_enqueue_a_report(boundary: tuple[FlaskClient, MagicMock, MagicMock]) -> None:
|
||||
client, gateway, enqueue = boundary
|
||||
gateway.authorize_investigation.side_effect = KnowledgeFsError("KNOWLEDGE_SCOPE_MISMATCH", "revoked", 403)
|
||||
assert client.post(PATH, json=_report(), headers=HEADERS).status_code == 403
|
||||
enqueue.assert_not_called()
|
||||
|
||||
|
||||
def test_broker_failure_is_not_acknowledged_as_accepted(boundary: tuple[FlaskClient, MagicMock, MagicMock]) -> None:
|
||||
client, _, enqueue = boundary
|
||||
enqueue.side_effect = OSError("broker offline")
|
||||
assert client.post(PATH, json=_report(), headers=HEADERS).status_code == 500
|
||||
|
||||
|
||||
def test_missing_inner_api_key_cannot_enqueue_a_report(boundary: tuple[FlaskClient, MagicMock, MagicMock]) -> None:
|
||||
client, gateway, enqueue = boundary
|
||||
assert client.post(PATH, json=_report()).status_code == 404
|
||||
gateway.authorize_investigation.assert_not_called()
|
||||
enqueue.assert_not_called()
|
||||
|
||||
|
||||
def test_swagger_documents_report_and_queue_admission(boundary: tuple[FlaskClient, MagicMock, MagicMock]) -> None:
|
||||
client, _, _ = boundary
|
||||
with client.application.test_request_context():
|
||||
schema = Swagger(inner_api).as_dict()
|
||||
route = schema["paths"]["/agent/knowledge/investigations"]["post"]
|
||||
assert route["requestBody"]["content"]["application/json"]["schema"]["$ref"] == (
|
||||
"#/components/schemas/KnowledgeInvestigationPayload"
|
||||
)
|
||||
assert route["responses"]["202"]["content"]["application/json"]["schema"]["$ref"] == (
|
||||
"#/components/schemas/KnowledgeInvestigationAcceptedResponse"
|
||||
)
|
||||
@@ -0,0 +1,318 @@
|
||||
"""Contract tests for the trusted Agent LLM streaming gateway."""
|
||||
|
||||
import json
|
||||
import warnings
|
||||
from collections.abc import Generator
|
||||
from contextlib import contextmanager
|
||||
from copy import deepcopy
|
||||
from unittest.mock import MagicMock, patch
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from pydantic import JsonValue
|
||||
|
||||
from controllers.inner_api import bp as inner_api_bp
|
||||
from core.errors.error import QuotaExceededError
|
||||
from graphon.model_runtime.entities.llm_entities import LLMResultChunk, LLMResultChunkDelta, LLMUsage
|
||||
from graphon.model_runtime.entities.message_entities import AssistantPromptMessage, PromptMessage, UserPromptMessage
|
||||
from graphon.model_runtime.model_providers.base.large_language_model import LargeLanguageModel
|
||||
from services.agent_llm_inner_service import (
|
||||
AgentLLMInnerService,
|
||||
AgentLLMInnerServiceError,
|
||||
PreparedAgentLLMInvocation,
|
||||
)
|
||||
from services.entities.agent_llm_inner import AgentLLMInvokeRequest
|
||||
from tests.helpers.unit.config_override import config_overrides_context
|
||||
from tests.helpers.unit.core.model_fixtures import make_model_instance
|
||||
|
||||
|
||||
def _payload() -> dict[str, object]:
|
||||
return {
|
||||
"caller": {
|
||||
"invocation_id": str(uuid4()),
|
||||
"agent_run_id": str(uuid4()),
|
||||
"call_index": 1,
|
||||
"tenant_id": str(uuid4()),
|
||||
"user_id": str(uuid4()),
|
||||
"user_from": "account",
|
||||
"app_id": str(uuid4()),
|
||||
"invoke_from": "debugger",
|
||||
"agent_mode": "workflow_run",
|
||||
"agent_config_version_kind": "draft",
|
||||
},
|
||||
"target": {
|
||||
"provider": "openai",
|
||||
"model": "gpt-test",
|
||||
"prompt_messages": [UserPromptMessage(content="hello").model_dump(mode="json")],
|
||||
"model_parameters": {"temperature": 0.2},
|
||||
"stream": True,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _agent_inner_auth() -> Generator[None]:
|
||||
with (
|
||||
config_overrides_context(PLUGIN_DAEMON_KEY="plugin-daemon-key", INNER_API_KEY_FOR_PLUGIN="inner-key"),
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
def _flask_app() -> Flask:
|
||||
app = Flask(__name__)
|
||||
app.config["TESTING"] = True
|
||||
app.register_blueprint(inner_api_bp)
|
||||
return app
|
||||
|
||||
|
||||
def test_post_streams_plugin_compatible_envelope() -> None:
|
||||
payload = _payload()
|
||||
request = AgentLLMInvokeRequest.model_validate(payload)
|
||||
prepared = PreparedAgentLLMInvocation(request=request, model_instance=MagicMock())
|
||||
chunk = LLMResultChunk(
|
||||
model="gpt-test",
|
||||
delta=LLMResultChunkDelta(
|
||||
index=0,
|
||||
message=AssistantPromptMessage(content="done", tool_calls=[]),
|
||||
),
|
||||
)
|
||||
|
||||
with (
|
||||
_agent_inner_auth(),
|
||||
patch("controllers.inner_api.agent.llm.AgentLLMInnerService.prepare", return_value=prepared) as prepare,
|
||||
patch("controllers.inner_api.agent.llm.AgentLLMInnerService.invoke", return_value=iter([chunk])),
|
||||
):
|
||||
response = (
|
||||
_flask_app()
|
||||
.test_client()
|
||||
.post(
|
||||
"/inner/api/agent/llm/invoke",
|
||||
json=payload,
|
||||
headers={"X-Inner-Api-Key": "inner-key"},
|
||||
)
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.content_type == "text/event-stream"
|
||||
data_line = response.get_data(as_text=True).strip().removeprefix("data: ")
|
||||
envelope = json.loads(data_line)
|
||||
assert envelope["code"] == 0
|
||||
assert envelope["data"]["delta"]["message"]["content"] == "done"
|
||||
prepare.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("stream_error", [False, True], ids=["completed", "provider-error"])
|
||||
def test_post_preserves_raw_prompts_without_serializing_them(
|
||||
monkeypatch: pytest.MonkeyPatch, stream_error: bool
|
||||
) -> None:
|
||||
payload = _payload()
|
||||
prompt_messages: list[dict[str, JsonValue]] = [
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "prior answer",
|
||||
"tool_calls": [],
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "",
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": "call-1",
|
||||
"type": "function",
|
||||
"function": {"name": "search", "arguments": '{"query":"Dify"}'},
|
||||
}
|
||||
],
|
||||
"provider_extension": {"thought_signature": "opaque"},
|
||||
},
|
||||
{
|
||||
"role": "tool",
|
||||
"content": "result",
|
||||
"name": "search",
|
||||
"tool_call_id": "call-1",
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{"type": "text", "data": "Inspect this image"},
|
||||
{
|
||||
"type": "image",
|
||||
"format": "png",
|
||||
"url": "https://example.com/image.png",
|
||||
"mime_type": "image/png",
|
||||
"detail": "high",
|
||||
"provider_extension": {"quality": "original"},
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
target = payload["target"]
|
||||
assert isinstance(target, dict)
|
||||
target["prompt_messages"] = prompt_messages
|
||||
original_prompt_messages = deepcopy(prompt_messages)
|
||||
model_instance = make_model_instance(provider="openai", model="gpt-test")
|
||||
assert isinstance(model_instance.model_type_instance, LargeLanguageModel)
|
||||
captured_requests: list[AgentLLMInvokeRequest] = []
|
||||
runtime_prompt_messages: list[list[PromptMessage]] = []
|
||||
usage = LLMUsage.from_metadata({"prompt_tokens": 11, "completion_tokens": 2, "total_price": "0.002"})
|
||||
chunks = [
|
||||
LLMResultChunk(
|
||||
model="gpt-test",
|
||||
system_fingerprint="fp-test",
|
||||
delta=LLMResultChunkDelta(index=index, message=AssistantPromptMessage(content=content)),
|
||||
)
|
||||
for index, content in enumerate(["Dify", " ready", ""])
|
||||
]
|
||||
chunks[1].delta.message.tool_calls = [
|
||||
AssistantPromptMessage.ToolCall(
|
||||
id="call-2",
|
||||
type="function",
|
||||
function=AssistantPromptMessage.ToolCall.ToolCallFunction(name="search", arguments='{"query":"Dify"}'),
|
||||
)
|
||||
]
|
||||
chunks[-1].delta.usage = usage
|
||||
chunks[-1].delta.finish_reason = "tool_calls"
|
||||
|
||||
def prepare(_service: AgentLLMInnerService, request: AgentLLMInvokeRequest) -> PreparedAgentLLMInvocation:
|
||||
captured_requests.append(request)
|
||||
return PreparedAgentLLMInvocation(request=request, model_instance=model_instance)
|
||||
|
||||
def provider_stream(*, prompt_messages: list[PromptMessage], **_kwargs: object) -> Generator[LLMResultChunk]:
|
||||
runtime_prompt_messages.append(prompt_messages)
|
||||
yield from chunks[:2]
|
||||
if stream_error:
|
||||
raise ValueError("provider stream failed")
|
||||
yield chunks[-1]
|
||||
|
||||
# Replace only preflight database resolution and the external provider boundary;
|
||||
# the service, model instance, and Graphon streaming wrapper execute normally.
|
||||
monkeypatch.setattr(AgentLLMInnerService, "prepare", prepare)
|
||||
monkeypatch.setattr(model_instance.model_type_instance.model_runtime, "invoke_llm", provider_stream)
|
||||
|
||||
with (
|
||||
_agent_inner_auth(),
|
||||
warnings.catch_warnings(record=True) as recorded_warnings,
|
||||
):
|
||||
warnings.simplefilter("always")
|
||||
response = (
|
||||
_flask_app()
|
||||
.test_client()
|
||||
.post(
|
||||
"/inner/api/agent/llm/invoke",
|
||||
json=payload,
|
||||
headers={"X-Inner-Api-Key": "inner-key"},
|
||||
)
|
||||
)
|
||||
envelopes = [
|
||||
json.loads(event.removeprefix("data: ")) for event in response.get_data(as_text=True).strip().split("\n\n")
|
||||
]
|
||||
|
||||
assert response.status_code == 200
|
||||
assert not any("PydanticSerializationUnexpectedValue" in str(warning.message) for warning in recorded_warnings)
|
||||
assert captured_requests[0].target.prompt_messages == original_prompt_messages
|
||||
assert runtime_prompt_messages == [original_prompt_messages]
|
||||
assert prompt_messages == original_prompt_messages
|
||||
|
||||
successful_envelopes = envelopes[:-1] if stream_error else envelopes
|
||||
expected_chunks = chunks[:2] if stream_error else chunks
|
||||
assert len(successful_envelopes) == len(expected_chunks)
|
||||
for envelope, chunk in zip(successful_envelopes, expected_chunks, strict=True):
|
||||
assert envelope["code"] == 0
|
||||
assert envelope["message"] == ""
|
||||
assert "prompt_messages" not in envelope["data"]
|
||||
assert envelope["data"]["delta"] == chunk.delta.model_dump(mode="json")
|
||||
assert envelope["data"]["system_fingerprint"] == "fp-test"
|
||||
assert LLMResultChunk.model_validate(envelope["data"]).model == "gpt-test"
|
||||
# Graphon must have restored the raw history before the controller serializes.
|
||||
assert chunk.prompt_messages == original_prompt_messages
|
||||
assert all(isinstance(message, dict) for message in chunk.prompt_messages)
|
||||
|
||||
if stream_error:
|
||||
assert envelopes[-1]["code"] == -500
|
||||
assert envelopes[-1]["data"] is None
|
||||
assert json.loads(envelopes[-1]["message"]) == {
|
||||
"error_type": "ValueError",
|
||||
"message": "provider stream failed",
|
||||
}
|
||||
else:
|
||||
assert envelopes[-1]["data"]["delta"]["usage"] == usage.model_dump(mode="json")
|
||||
assert envelopes[-1]["data"]["delta"]["finish_reason"] == "tool_calls"
|
||||
|
||||
|
||||
def test_post_rejects_invalid_body_before_model_resolution() -> None:
|
||||
with _agent_inner_auth():
|
||||
response = (
|
||||
_flask_app()
|
||||
.test_client()
|
||||
.post(
|
||||
"/inner/api/agent/llm/invoke",
|
||||
json={"caller": {}},
|
||||
headers={"X-Inner-Api-Key": "inner-key"},
|
||||
)
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.get_json()["code"] == "invalid_request"
|
||||
|
||||
|
||||
def test_post_preserves_preflight_quota_failure() -> None:
|
||||
with (
|
||||
_agent_inner_auth(),
|
||||
patch(
|
||||
"controllers.inner_api.agent.llm.AgentLLMInnerService.prepare",
|
||||
side_effect=AgentLLMInnerServiceError(
|
||||
"agent_llm_quota_exceeded",
|
||||
"Insufficient Message Credits.",
|
||||
status_code=429,
|
||||
),
|
||||
),
|
||||
):
|
||||
response = (
|
||||
_flask_app()
|
||||
.test_client()
|
||||
.post(
|
||||
"/inner/api/agent/llm/invoke",
|
||||
json=_payload(),
|
||||
headers={"X-Inner-Api-Key": "inner-key"},
|
||||
)
|
||||
)
|
||||
|
||||
assert response.status_code == 429
|
||||
assert response.get_json() == {
|
||||
"code": "agent_llm_quota_exceeded",
|
||||
"message": "Insufficient Message Credits.",
|
||||
"status": 429,
|
||||
}
|
||||
|
||||
|
||||
def test_post_preserves_stream_quota_failure() -> None:
|
||||
payload = _payload()
|
||||
prepared = PreparedAgentLLMInvocation(
|
||||
request=AgentLLMInvokeRequest.model_validate(payload),
|
||||
model_instance=MagicMock(),
|
||||
)
|
||||
with (
|
||||
_agent_inner_auth(),
|
||||
patch("controllers.inner_api.agent.llm.AgentLLMInnerService.prepare", return_value=prepared),
|
||||
patch(
|
||||
"controllers.inner_api.agent.llm.AgentLLMInnerService.invoke",
|
||||
side_effect=QuotaExceededError("Insufficient hosted model quota remaining."),
|
||||
),
|
||||
):
|
||||
response = (
|
||||
_flask_app()
|
||||
.test_client()
|
||||
.post(
|
||||
"/inner/api/agent/llm/invoke",
|
||||
json=payload,
|
||||
headers={"X-Inner-Api-Key": "inner-key"},
|
||||
)
|
||||
)
|
||||
|
||||
envelope = json.loads(response.get_data(as_text=True).strip().removeprefix("data: "))
|
||||
error = json.loads(envelope["message"])
|
||||
assert response.status_code == 200
|
||||
assert envelope["code"] == -429
|
||||
assert error == {
|
||||
"error_type": "AgentLLMQuotaExceededError",
|
||||
"message": "Insufficient hosted model quota remaining.",
|
||||
}
|
||||
+219
@@ -0,0 +1,219 @@
|
||||
"""Authenticated HTTP contract backed by the real accounting service and DB."""
|
||||
|
||||
from collections.abc import Iterator
|
||||
from typing import TypedDict
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
import sqlalchemy as sa
|
||||
from flask import Flask
|
||||
from flask.testing import FlaskClient
|
||||
from pydantic import JsonValue
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from controllers.inner_api import bp as inner_api_bp
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from models.agent_sandbox_usage import AgentSandboxUsageEvent
|
||||
from tests.helpers.unit.config_override import config_overrides_context
|
||||
|
||||
PROJECT = "431de237-596f-4d59-8a85-20a9846bf243"
|
||||
|
||||
|
||||
class _EventPayload(TypedDict):
|
||||
id: str
|
||||
source: str
|
||||
type: str
|
||||
timestamp: str
|
||||
payload: dict[str, JsonValue]
|
||||
|
||||
|
||||
class _BatchPayload(TypedDict):
|
||||
project_id: str
|
||||
events: list[_EventPayload]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client() -> Iterator[FlaskClient]:
|
||||
app = Flask(__name__)
|
||||
app.config["TESTING"] = True
|
||||
app.register_blueprint(inner_api_bp)
|
||||
with config_overrides_context(
|
||||
PLUGIN_DAEMON_KEY="test-daemon",
|
||||
INNER_API_KEY_FOR_PLUGIN="test-inner",
|
||||
AGENT_SANDBOX_METERING_ENABLED=True,
|
||||
AGENT_SANDBOX_METERING_PROJECT_ID=PROJECT,
|
||||
AGENT_SANDBOX_METERING_START_AT="2026-09-20T00:00:00Z",
|
||||
):
|
||||
yield app.test_client()
|
||||
|
||||
|
||||
def payload() -> _BatchPayload:
|
||||
event_id = str(uuid4())
|
||||
return {
|
||||
"project_id": PROJECT,
|
||||
"events": [
|
||||
{
|
||||
"id": event_id,
|
||||
"source": "provider",
|
||||
"type": "sandbox.lifecycle.paused",
|
||||
"timestamp": "2026-09-20T00:01:00Z",
|
||||
"payload": {
|
||||
"id": event_id,
|
||||
"version": "v2",
|
||||
"type": "sandbox.lifecycle.paused",
|
||||
"timestamp": "2026-09-20T00:01:00Z",
|
||||
"sandboxId": "sandbox-test",
|
||||
"sandboxExecutionId": "execution-test",
|
||||
"sandboxTeamId": PROJECT,
|
||||
"eventData": {
|
||||
"execution": {
|
||||
"started_at": "2026-09-20T00:00:02Z",
|
||||
"execution_time": 12345,
|
||||
"memory_mb": 1024,
|
||||
"vcpu_count": 2,
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def test_state_requires_inner_auth(client: FlaskClient) -> None:
|
||||
response = client.get(f"/inner/api/agent/sandbox-usage/state?project_id={PROJECT}")
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_state_exposes_persisted_scope_without_secret_or_environment(client: FlaskClient) -> None:
|
||||
response = client.get(
|
||||
f"/inner/api/agent/sandbox-usage/state?project_id={PROJECT}", headers={"X-Inner-Api-Key": "test-inner"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json == {
|
||||
"enabled": True,
|
||||
"project_id": PROJECT,
|
||||
"started_at": "2026-09-20T00:00:00Z",
|
||||
"checkpoint_at": None,
|
||||
"full_scan_at": None,
|
||||
"diagnostics": {
|
||||
"unresolved_events": 0,
|
||||
"conflict_events": 0,
|
||||
"open_executions": 0,
|
||||
"unattributed_executions": 0,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("query", ["", "?project_id=", f"?project_id={PROJECT}&unexpected=value"])
|
||||
def test_state_rejects_invalid_query_before_activation(
|
||||
client: FlaskClient, sqlite_session_factory: sessionmaker[Session], query: str
|
||||
) -> None:
|
||||
response = client.get(f"/inner/api/agent/sandbox-usage/state{query}", headers={"X-Inner-Api-Key": "test-inner"})
|
||||
assert response.status_code == 400
|
||||
body = response.get_json()
|
||||
assert isinstance(body, dict)
|
||||
assert body["code"] == "sandbox_usage_invalid_request"
|
||||
with sqlite_session_factory() as session:
|
||||
assert session.scalar(sa.select(sa.func.count()).select_from(AgentSandboxUsageEvent)) == 0
|
||||
|
||||
|
||||
def test_state_rejects_project_outside_allowlist_without_activation(
|
||||
client: FlaskClient, sqlite_session_factory: sessionmaker[Session]
|
||||
) -> None:
|
||||
response = client.get(
|
||||
"/inner/api/agent/sandbox-usage/state?project_id=other-project", headers={"X-Inner-Api-Key": "test-inner"}
|
||||
)
|
||||
assert response.status_code == 403
|
||||
body = response.get_json()
|
||||
assert isinstance(body, dict)
|
||||
assert body["code"] == "sandbox_metering_project_not_allowed"
|
||||
with sqlite_session_factory() as session:
|
||||
assert session.scalar(sa.select(sa.func.count()).select_from(AgentSandboxUsageEvent)) == 0
|
||||
|
||||
|
||||
def test_state_reports_start_change_conflict_and_preserves_activation(client: FlaskClient) -> None:
|
||||
url = f"/inner/api/agent/sandbox-usage/state?project_id={PROJECT}"
|
||||
headers = {"X-Inner-Api-Key": "test-inner"}
|
||||
original = client.get(url, headers=headers)
|
||||
assert original.status_code == 200
|
||||
with config_overrides_context(AGENT_SANDBOX_METERING_START_AT="2026-09-21T00:00:00Z"):
|
||||
changed = client.get(url, headers=headers)
|
||||
assert changed.status_code == 409
|
||||
body = changed.get_json()
|
||||
assert isinstance(body, dict)
|
||||
assert body["code"] == "sandbox_metering_start_is_immutable"
|
||||
restored = client.get(url, headers=headers)
|
||||
assert restored.status_code == 200
|
||||
assert restored.json == original.json
|
||||
|
||||
|
||||
def test_state_reports_invalid_start_as_configuration_error(client: FlaskClient) -> None:
|
||||
with config_overrides_context(AGENT_SANDBOX_METERING_START_AT="2026-09-20T00:00:00"):
|
||||
response = client.get(
|
||||
f"/inner/api/agent/sandbox-usage/state?project_id={PROJECT}", headers={"X-Inner-Api-Key": "test-inner"}
|
||||
)
|
||||
assert response.status_code == 503
|
||||
body = response.get_json()
|
||||
assert isinstance(body, dict)
|
||||
assert body["code"] == "sandbox_metering_start_not_configured"
|
||||
|
||||
|
||||
def test_events_ack_only_after_persisted_and_duplicate_is_idempotent(
|
||||
client: FlaskClient, sqlite_session_factory: sessionmaker[Session]
|
||||
) -> None:
|
||||
body = payload()
|
||||
for expected in (
|
||||
{"accepted": 1, "duplicates": 0, "conflicts": 0, "ignored": 0},
|
||||
{"accepted": 0, "duplicates": 1, "conflicts": 0, "ignored": 0},
|
||||
):
|
||||
response = client.post(
|
||||
"/inner/api/agent/sandbox-usage/events", json=body, headers={"X-Inner-Api-Key": "test-inner"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json == expected
|
||||
with sqlite_session_factory() as session:
|
||||
assert (
|
||||
session.scalar(
|
||||
sa.select(sa.func.count())
|
||||
.select_from(AgentSandboxUsageEvent)
|
||||
.where(AgentSandboxUsageEvent.source_event_id == body["events"][0]["id"])
|
||||
)
|
||||
== 1
|
||||
)
|
||||
|
||||
|
||||
def test_ingestion_rejects_other_project_and_oversized_batch(client: FlaskClient) -> None:
|
||||
body = payload()
|
||||
body["project_id"] = "other-project"
|
||||
response = client.post(
|
||||
"/inner/api/agent/sandbox-usage/events", json=body, headers={"X-Inner-Api-Key": "test-inner"}
|
||||
)
|
||||
assert response.status_code == 403
|
||||
body = payload()
|
||||
body["events"] *= 101
|
||||
response = client.post(
|
||||
"/inner/api/agent/sandbox-usage/events", json=body, headers={"X-Inner-Api-Key": "test-inner"}
|
||||
)
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_ingestion_payload_limit(client: FlaskClient) -> None:
|
||||
body = payload()
|
||||
body["events"][0]["payload"] = {"oversized": "x" * (1024 * 1024)}
|
||||
response = client.post(
|
||||
"/inner/api/agent/sandbox-usage/events", json=body, headers={"X-Inner-Api-Key": "test-inner"}
|
||||
)
|
||||
assert response.status_code == 413
|
||||
|
||||
|
||||
def test_schema_and_http_exclude_business_operation_envelope_fields(client: FlaskClient) -> None:
|
||||
schema = inner_api_ns.models["SandboxUsageEvent"].__schema__
|
||||
assert set(schema["properties"]) == {"id", "source", "type", "timestamp", "sandbox_id", "execution_id", "payload"}
|
||||
assert schema["additionalProperties"] is False
|
||||
body = payload()
|
||||
response = client.post(
|
||||
"/inner/api/agent/sandbox-usage/events",
|
||||
json={**body, "events": [{**body["events"][0], "allocation_id": str(uuid4())}]},
|
||||
headers={"X-Inner-Api-Key": "test-inner"},
|
||||
)
|
||||
assert response.status_code == 400
|
||||
@@ -0,0 +1,251 @@
|
||||
"""Unit tests for the Agent tool inner API controller."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from contextlib import contextmanager
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from redis.exceptions import LockNotOwnedError, RedisError
|
||||
from redis.lock import Lock
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from controllers.inner_api import bp as inner_api_bp
|
||||
from core.plugin.entities.plugin_daemon import CredentialType
|
||||
from core.tools.entities.tool_entities import ToolProviderEntityWithPlugin
|
||||
from core.tools.plugin_tool.provider import PluginToolProviderController
|
||||
from core.tools.tool_manager import ToolManager
|
||||
from extensions.application_services.tools import resolve_tool_credentials
|
||||
from extensions.ext_redis import RedisClientWrapper
|
||||
from models.model import App, AppMode
|
||||
from models.tools import BuiltinToolProvider
|
||||
from services.credentials.refresh import CredentialRefreshUnavailableError
|
||||
from services.credentials.tool_runtime import ToolCredentialRefreshError
|
||||
from services.credentials.tool_runtime_adapters import ProviderToolCredentialRuntime
|
||||
from services.entities.agent_tool_inner import AgentToolInvokeResponse
|
||||
from services.errors.agent_tool_inner import AgentToolInnerServiceError
|
||||
from tests.helpers.unit.config_override import config_overrides_context
|
||||
|
||||
|
||||
def _headers(api_key: str | None = "inner-key") -> dict[str, str]:
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if api_key is not None:
|
||||
headers["X-Inner-Api-Key"] = api_key
|
||||
return headers
|
||||
|
||||
|
||||
def _payload(*, provider_type: str = "plugin", credential_id: str | None = "credential-1") -> dict[str, object]:
|
||||
return {
|
||||
"caller": {
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "user-1",
|
||||
"user_from": "account",
|
||||
"app_id": "app-1",
|
||||
"invoke_from": "service-api",
|
||||
"conversation_id": "conversation-1",
|
||||
"workflow_id": "workflow-1",
|
||||
"workflow_run_id": "workflow-run-1",
|
||||
"node_id": "node-1",
|
||||
"node_execution_id": "node-exec-1",
|
||||
"agent_id": "agent-1",
|
||||
"agent_config_version_id": "snapshot-1",
|
||||
},
|
||||
"tool": {
|
||||
"provider_type": provider_type,
|
||||
"provider_id": "langgenius/search/search",
|
||||
"tool_name": "search",
|
||||
"credential_id": credential_id,
|
||||
"tool_parameters": {"query": "dify"},
|
||||
"runtime_parameters": {"region": "us"},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _agent_inner_auth() -> Generator[None]:
|
||||
with (
|
||||
config_overrides_context(PLUGIN_DAEMON_KEY="plugin-daemon-key", INNER_API_KEY_FOR_PLUGIN="inner-key"),
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
def test_post_returns_service_response() -> None:
|
||||
app = Flask(__name__)
|
||||
app.config["TESTING"] = True
|
||||
app.register_blueprint(inner_api_bp)
|
||||
|
||||
with (
|
||||
_agent_inner_auth(),
|
||||
patch("controllers.inner_api.agent.tools.AgentToolInnerService.invoke") as mock_invoke,
|
||||
):
|
||||
mock_invoke.return_value = AgentToolInvokeResponse(
|
||||
messages=[{"type": "text", "message": {"text": "ok"}}],
|
||||
observation="ok",
|
||||
metadata={"provider_type": "plugin", "tool_name": "search"},
|
||||
)
|
||||
|
||||
response = app.test_client().post(
|
||||
"/inner/api/agent/tools/invoke",
|
||||
json=_payload(),
|
||||
headers=_headers(),
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
body = response.get_json()
|
||||
assert body["observation"] == "ok"
|
||||
assert body["metadata"]["provider_type"] == "plugin"
|
||||
|
||||
|
||||
def test_post_returns_400_for_invalid_body() -> None:
|
||||
app = Flask(__name__)
|
||||
app.config["TESTING"] = True
|
||||
app.register_blueprint(inner_api_bp)
|
||||
|
||||
with _agent_inner_auth():
|
||||
response = app.test_client().post(
|
||||
"/inner/api/agent/tools/invoke",
|
||||
json={"caller": {}},
|
||||
headers=_headers(),
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
body = response.get_json()
|
||||
assert body["code"] == "invalid_request"
|
||||
|
||||
|
||||
def test_post_preserves_service_error_status_code_and_description() -> None:
|
||||
app = Flask(__name__)
|
||||
app.config["TESTING"] = True
|
||||
app.register_blueprint(inner_api_bp)
|
||||
|
||||
with (
|
||||
_agent_inner_auth(),
|
||||
patch("controllers.inner_api.agent.tools.AgentToolInnerService.invoke") as mock_invoke,
|
||||
):
|
||||
mock_invoke.side_effect = AgentToolInnerServiceError(
|
||||
error_code="app_tenant_mismatch",
|
||||
description="App does not belong to the caller tenant.",
|
||||
status_code=403,
|
||||
)
|
||||
response = app.test_client().post(
|
||||
"/inner/api/agent/tools/invoke",
|
||||
json=_payload(),
|
||||
headers=_headers(),
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
body = response.get_json()
|
||||
assert body["code"] == "app_tenant_mismatch"
|
||||
assert body["message"] == "App does not belong to the caller tenant."
|
||||
|
||||
|
||||
@pytest.mark.parametrize("credential_id", [None, "1a1db43c-e9d8-4ef2-af8f-38b25424a4ad"])
|
||||
@pytest.mark.parametrize("failure", ["busy", "redis", "lost", "rejected"])
|
||||
def test_post_preserves_refresh_error_classification_through_real_credential_resolution(
|
||||
sqlite_session: Session,
|
||||
redis_transport: tuple[RedisClientWrapper, MagicMock],
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
credential_id: str | None,
|
||||
failure: str,
|
||||
) -> None:
|
||||
provider_id = "langgenius/search/search"
|
||||
credential = BuiltinToolProvider(
|
||||
tenant_id="tenant-1",
|
||||
user_id="user-1",
|
||||
provider=provider_id,
|
||||
name="Search",
|
||||
credential_type=CredentialType.OAUTH2,
|
||||
encrypted_credentials="{}",
|
||||
expires_at=1,
|
||||
is_default=True,
|
||||
)
|
||||
credential.id = "1a1db43c-e9d8-4ef2-af8f-38b25424a4ad"
|
||||
sqlite_session.add_all(
|
||||
[
|
||||
App(
|
||||
id="app-1",
|
||||
tenant_id="tenant-1",
|
||||
name="Agent",
|
||||
mode=AppMode.AGENT_CHAT,
|
||||
enable_site=False,
|
||||
enable_api=False,
|
||||
max_active_requests=None,
|
||||
),
|
||||
credential,
|
||||
]
|
||||
)
|
||||
sqlite_session.commit()
|
||||
controller = PluginToolProviderController(
|
||||
entity=ToolProviderEntityWithPlugin.model_validate(
|
||||
{
|
||||
"identity": {
|
||||
"author": "langgenius",
|
||||
"name": "search",
|
||||
"description": {"en_US": "Search"},
|
||||
"icon": "search.svg",
|
||||
"label": {"en_US": "Search"},
|
||||
},
|
||||
"oauth_schema": {
|
||||
"client_schema": [],
|
||||
"credentials_schema": [
|
||||
{"name": "access_token", "type": "secret-input", "label": {"en_US": "Access token"}}
|
||||
],
|
||||
},
|
||||
"tools": [
|
||||
{
|
||||
"identity": {
|
||||
"author": "langgenius",
|
||||
"name": "search",
|
||||
"provider": "search",
|
||||
"label": {"en_US": "Search"},
|
||||
},
|
||||
"description": {"human": {"en_US": "Search"}, "llm": "Search"},
|
||||
"parameters": [],
|
||||
}
|
||||
],
|
||||
}
|
||||
),
|
||||
plugin_id="langgenius/search",
|
||||
plugin_unique_identifier="langgenius/search:1.0.0@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
tenant_id="tenant-1",
|
||||
)
|
||||
redis, _ = redis_transport
|
||||
monkeypatch.setattr("extensions.application_services.tools.redis_client", redis)
|
||||
monkeypatch.setattr("core.helper.provider_cache.redis_client", redis)
|
||||
monkeypatch.setattr(ToolManager, "_credential_resolver", resolve_tool_credentials)
|
||||
app = Flask(__name__)
|
||||
app.config["TESTING"] = True
|
||||
app.register_blueprint(inner_api_bp)
|
||||
rejected = ToolCredentialRefreshError("OAuth credential was rejected. Reauthorize the provider.")
|
||||
|
||||
with (
|
||||
_agent_inner_auth(),
|
||||
patch.object(ToolManager, "get_builtin_provider", return_value=controller),
|
||||
patch("core.helper.credential_utils.check_credential_policy_compliance"),
|
||||
patch.object(
|
||||
Lock,
|
||||
"acquire",
|
||||
return_value=failure != "busy",
|
||||
side_effect=RedisError("unavailable") if failure == "redis" else None,
|
||||
),
|
||||
patch.object(Lock, "reacquire", side_effect=LockNotOwnedError("expired") if failure == "lost" else None),
|
||||
patch.object(Lock, "release"),
|
||||
patch.object(ProviderToolCredentialRuntime, "refresh", side_effect=rejected) as refresh,
|
||||
):
|
||||
response = app.test_client().post(
|
||||
"/inner/api/agent/tools/invoke",
|
||||
json=_payload(provider_type="builtin", credential_id=credential_id),
|
||||
headers=_headers(),
|
||||
)
|
||||
|
||||
expected_status = 422 if failure == "rejected" else 503
|
||||
assert response.status_code == expected_status
|
||||
assert response.get_json() == {
|
||||
"code": "agent_tool_credential_invalid" if failure == "rejected" else "service_unavailable",
|
||||
"message": str(rejected) if failure == "rejected" else str(CredentialRefreshUnavailableError()),
|
||||
"status": expected_status,
|
||||
}
|
||||
assert refresh.call_count == (1 if failure == "rejected" else 0)
|
||||
sqlite_session.refresh(credential)
|
||||
assert credential.encrypted_credentials == "{}"
|
||||
assert credential.expires_at == 1
|
||||
@@ -0,0 +1,188 @@
|
||||
"""HTTP authentication and acknowledgement backed by the real SQLite collector."""
|
||||
|
||||
from collections.abc import Iterator
|
||||
from dataclasses import replace
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from dify_trace_sdk.testing import RecordingEgress
|
||||
from flask import Flask
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from controllers.inner_api import bp
|
||||
from extensions.ext_application_services import ApplicationServices
|
||||
from models.application_trace import ApplicationTraceBatch
|
||||
from repositories.factory import DifyAPIRepositoryFactory
|
||||
from repositories.trace_record_repository import TraceRecordRepository
|
||||
from services.tracing.canonical import CanonicalTraceBuilder
|
||||
from services.tracing.collection import AgentTraceSettings
|
||||
from services.tracing.collection_ports import ScheduleTraceExport
|
||||
from services.tracing.providers import ProviderConfigCodec
|
||||
from services.tracing.service import ApplicationTracingService
|
||||
from tests.helpers.unit.config_override import config_overrides_context
|
||||
from tests.helpers.unit.services.tracing_ports import (
|
||||
AGENT_TRACE_SETTINGS,
|
||||
InstalledTraceProviders,
|
||||
WorkspaceBoundCipher,
|
||||
)
|
||||
from tests.unit_tests.repositories.test_application_trace_repository import Collection, batch_for, seed
|
||||
|
||||
BATCHES_URL = "/inner/api/agent/tracing/batches"
|
||||
INNER_API_HEADERS = {"X-Inner-Api-Key": "inner"}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def inner_app() -> Iterator[Flask]:
|
||||
app = Flask(__name__)
|
||||
app.config["TESTING"] = True
|
||||
app.register_blueprint(bp)
|
||||
with config_overrides_context(PLUGIN_DAEMON_KEY="daemon", INNER_API_KEY_FOR_PLUGIN="inner"):
|
||||
yield app
|
||||
|
||||
|
||||
def bind_collector(
|
||||
app: Flask,
|
||||
services: ApplicationServices,
|
||||
collection: Collection,
|
||||
*,
|
||||
settings: AgentTraceSettings,
|
||||
schedule_export: ScheduleTraceExport,
|
||||
) -> None:
|
||||
"""Make the app's composition root hand the ingestion handler a collector over the seeded ledger."""
|
||||
collector = ApplicationTracingService(
|
||||
runs=collection.repository,
|
||||
run_query=collection.repository,
|
||||
sources=collection.repository,
|
||||
records=TraceRecordRepository(
|
||||
collection.sessions,
|
||||
workflow_runs=DifyAPIRepositoryFactory.create_api_workflow_run_repository(collection.sessions),
|
||||
node_executions=DifyAPIRepositoryFactory.create_api_workflow_node_execution_repository(collection.sessions),
|
||||
files_url="https://files.test",
|
||||
enterprise_details=False,
|
||||
),
|
||||
builder=CanonicalTraceBuilder(),
|
||||
providers=InstalledTraceProviders(),
|
||||
codec=ProviderConfigCodec(WorkspaceBoundCipher()),
|
||||
# Ingesting batches never exports; an unexpected request fails the test.
|
||||
egress=RecordingEgress([], grpc_target=None),
|
||||
schedule_export=schedule_export,
|
||||
settings=settings,
|
||||
)
|
||||
app.extensions["application_services"] = replace(
|
||||
services, tracing=replace(services.tracing, agent_traces=collector)
|
||||
)
|
||||
|
||||
|
||||
def stored_batches(sessions: sessionmaker[Session]) -> int | None:
|
||||
with sessions() as session:
|
||||
return session.scalar(select(func.count()).select_from(ApplicationTraceBatch))
|
||||
|
||||
|
||||
def test_trace_http_ack_follows_commit_and_replay_is_idempotent(
|
||||
inner_app: Flask,
|
||||
account_application_services: ApplicationServices,
|
||||
sqlite_session_factory: sessionmaker[Session],
|
||||
) -> None:
|
||||
collection = seed(sqlite_session_factory, "http")
|
||||
payload = batch_for(collection.run, collection.invocation()).model_dump(mode="json")
|
||||
scheduled: list[str] = []
|
||||
|
||||
def schedule(run_id: str) -> None:
|
||||
# A fresh transaction can see the batch before the caller gets an ACK.
|
||||
assert stored_batches(sqlite_session_factory) == 1
|
||||
scheduled.append(run_id)
|
||||
|
||||
bind_collector(
|
||||
inner_app, account_application_services, collection, settings=AGENT_TRACE_SETTINGS, schedule_export=schedule
|
||||
)
|
||||
client = inner_app.test_client()
|
||||
for _ in range(2):
|
||||
response = client.post(BATCHES_URL, json=payload, headers=INNER_API_HEADERS)
|
||||
assert response.status_code == 200
|
||||
assert response.get_json() == {"result": "success", "batch_id": payload["batch_id"], "accepted_spans": 1}
|
||||
assert scheduled == [collection.run.id, collection.run.id]
|
||||
|
||||
payload["spans"][0]["name"] = "conflicting replay"
|
||||
response = client.post(BATCHES_URL, json=payload, headers=INNER_API_HEADERS)
|
||||
assert response.status_code == 409
|
||||
assert len(scheduled) == 2
|
||||
|
||||
|
||||
@pytest.mark.parametrize("header", [{}, {"X-Inner-Api-Key": "wrong"}])
|
||||
def test_trace_http_requires_internal_auth(inner_app: Flask, header: dict[str, str]) -> None:
|
||||
response = inner_app.test_client().post(BATCHES_URL, json={}, headers=header)
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_trace_http_rejects_invalid_span_and_unknown_owner(
|
||||
inner_app: Flask,
|
||||
account_application_services: ApplicationServices,
|
||||
sqlite_session_factory: sessionmaker[Session],
|
||||
) -> None:
|
||||
collection = seed(sqlite_session_factory, "http-validation")
|
||||
payload = batch_for(collection.run, collection.invocation()).model_dump(mode="json")
|
||||
scheduled: list[str] = []
|
||||
bind_collector(
|
||||
inner_app,
|
||||
account_application_services,
|
||||
collection,
|
||||
settings=AGENT_TRACE_SETTINGS,
|
||||
schedule_export=scheduled.append,
|
||||
)
|
||||
client = inner_app.test_client()
|
||||
payload["spans"][0]["end_time_ns"] = 0
|
||||
response = client.post(BATCHES_URL, json=payload, headers=INNER_API_HEADERS)
|
||||
assert response.status_code == 400
|
||||
payload["spans"][0]["end_time_ns"] = 2_000_000_000
|
||||
payload["invocation_id"] = str(uuid4())
|
||||
response = client.post(BATCHES_URL, json=payload, headers=INNER_API_HEADERS)
|
||||
assert response.status_code == 404
|
||||
assert scheduled == []
|
||||
assert stored_batches(sqlite_session_factory) == 0
|
||||
|
||||
|
||||
def test_trace_http_rejects_batches_beyond_collection_limits(
|
||||
inner_app: Flask,
|
||||
account_application_services: ApplicationServices,
|
||||
sqlite_session_factory: sessionmaker[Session],
|
||||
) -> None:
|
||||
collection = seed(sqlite_session_factory, "http-limits")
|
||||
payload = batch_for(collection.run, collection.invocation()).model_dump(mode="json")
|
||||
scheduled: list[str] = []
|
||||
bind_collector(
|
||||
inner_app,
|
||||
account_application_services,
|
||||
collection,
|
||||
settings=replace(AGENT_TRACE_SETTINGS, max_spans=0),
|
||||
schedule_export=scheduled.append,
|
||||
)
|
||||
response = inner_app.test_client().post(BATCHES_URL, json=payload, headers=INNER_API_HEADERS)
|
||||
assert response.status_code == 413
|
||||
body = response.get_json()
|
||||
assert body is not None
|
||||
assert body["message"] == "tracing_payload_limit"
|
||||
assert scheduled == []
|
||||
assert stored_batches(sqlite_session_factory) == 0
|
||||
|
||||
|
||||
def test_trace_http_enforces_body_limit_before_ingestion(
|
||||
inner_app: Flask,
|
||||
account_application_services: ApplicationServices,
|
||||
sqlite_session_factory: sessionmaker[Session],
|
||||
) -> None:
|
||||
collection = seed(sqlite_session_factory, "http-body-limit")
|
||||
payload = batch_for(collection.run, collection.invocation()).model_dump(mode="json")
|
||||
scheduled: list[str] = []
|
||||
bind_collector(
|
||||
inner_app,
|
||||
account_application_services,
|
||||
collection,
|
||||
settings=AGENT_TRACE_SETTINGS,
|
||||
schedule_export=scheduled.append,
|
||||
)
|
||||
with config_overrides_context(OPS_AGENT_TRACE_BATCH_MAX_BYTES=64):
|
||||
response = inner_app.test_client().post(BATCHES_URL, json=payload, headers=INNER_API_HEADERS)
|
||||
assert response.status_code == 413
|
||||
assert scheduled == []
|
||||
assert stored_batches(sqlite_session_factory) == 0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,749 @@
|
||||
"""Unit tests for the inner_api enterprise app list and detail endpoints.
|
||||
|
||||
Auth/setup decorators are tested in test_auth_wraps.py; handler tests use inspect.unwrap() to bypass them.
|
||||
"""
|
||||
|
||||
import inspect
|
||||
import json
|
||||
from collections.abc import Mapping, Sequence
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta
|
||||
from decimal import Decimal
|
||||
from typing import Protocol, cast
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
from werkzeug.exceptions import BadRequest
|
||||
|
||||
from controllers.inner_api.app.app import (
|
||||
EnterpriseAppDetail,
|
||||
EnterpriseAppDetailResponse,
|
||||
EnterpriseAppList,
|
||||
EnterpriseAppPaginationResponse,
|
||||
)
|
||||
from graphon.enums import WorkflowExecutionStatus
|
||||
from models import Account, App, Tenant
|
||||
from models.account import AccountStatus
|
||||
from models.agent import (
|
||||
Agent,
|
||||
AgentConfigRevision,
|
||||
AgentConfigRevisionOperation,
|
||||
AgentConfigSnapshot,
|
||||
AgentScope,
|
||||
AgentSource,
|
||||
AgentStatus,
|
||||
)
|
||||
from models.enums import (
|
||||
ApiTokenType,
|
||||
AppTriggerStatus,
|
||||
AppTriggerType,
|
||||
ConversationFromSource,
|
||||
CreatorUserRole,
|
||||
CustomizeTokenStrategy,
|
||||
InvokeFrom,
|
||||
MessageStatus,
|
||||
WorkflowRunTriggeredFrom,
|
||||
)
|
||||
from models.model import ApiToken, AppMCPServer, AppMode, Message, Site
|
||||
from models.trigger import AppTrigger, WorkflowWebhookTrigger
|
||||
from models.workflow import Workflow, WorkflowRun, WorkflowType
|
||||
from repositories.api_workflow_run_repository import APIWorkflowRunRepository, AppWorkflowRunStats
|
||||
from repositories.app.enterprise_app_repository import EnterpriseAppRepository
|
||||
from repositories.sqlalchemy_api_workflow_run_repository import DifyAPISQLAlchemyWorkflowRunRepository
|
||||
from services.app.enterprise_app_service import EnterpriseAppService
|
||||
|
||||
WS_A = "11111111-1111-1111-1111-111111111111"
|
||||
WS_B = "22222222-2222-2222-2222-222222222222"
|
||||
NOW = datetime(2026, 10, 14, 12)
|
||||
|
||||
|
||||
class DocumentedView(Protocol):
|
||||
__apidoc__: dict[str, dict[str, dict[str, object]]]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _Apps:
|
||||
enterprise: EnterpriseAppService
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _Services:
|
||||
apps: _Apps
|
||||
|
||||
|
||||
def _install_service(
|
||||
app: Flask,
|
||||
session_factory: sessionmaker[Session],
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
*,
|
||||
app_web_url: str = "https://apps.example.com/",
|
||||
service_api_url: str = "https://api.example.com",
|
||||
) -> None:
|
||||
service = EnterpriseAppService(
|
||||
apps=EnterpriseAppRepository(
|
||||
session_factory=session_factory, app_web_url=app_web_url, service_api_url=service_api_url
|
||||
),
|
||||
workflow_runs=DifyAPISQLAlchemyWorkflowRunRepository(session_factory),
|
||||
now=lambda: NOW,
|
||||
)
|
||||
monkeypatch.setitem(app.extensions, "application_services", _Services(apps=_Apps(service)))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def enterprise_apps(app: Flask, sqlite_session_factory: sessionmaker[Session], monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
_install_service(app, sqlite_session_factory, monkeypatch)
|
||||
|
||||
|
||||
def _app(tenant_id: str, name: str, *, mode: AppMode = AppMode.WORKFLOW, created_at: datetime, **kwargs: object) -> App:
|
||||
app = App(
|
||||
tenant_id=tenant_id,
|
||||
name=name,
|
||||
mode=mode,
|
||||
enable_site=False,
|
||||
enable_api=False,
|
||||
)
|
||||
for name, value in kwargs.items():
|
||||
setattr(app, name, value)
|
||||
app.created_at = created_at
|
||||
app.updated_at = created_at
|
||||
app.id = str(uuid4())
|
||||
return app
|
||||
|
||||
|
||||
def _agent(
|
||||
tenant_id: str,
|
||||
*,
|
||||
scope: AgentScope,
|
||||
status: AgentStatus = AgentStatus.ACTIVE,
|
||||
app_id: str | None = None,
|
||||
backing_app_id: str | None = None,
|
||||
) -> Agent:
|
||||
"""Create a test Agent. None leaves the corresponding App association unset."""
|
||||
return Agent(
|
||||
tenant_id=tenant_id,
|
||||
name=f"agent-{uuid4()}",
|
||||
scope=scope,
|
||||
source=AgentSource.AGENT_APP if scope == AgentScope.ROSTER else AgentSource.WORKFLOW,
|
||||
status=status,
|
||||
app_id=app_id,
|
||||
backing_app_id=backing_app_id,
|
||||
)
|
||||
|
||||
|
||||
def _workflow(app: App, *, version: str, created_by: str, entry_node_type: str = "start", **kwargs: object) -> Workflow:
|
||||
workflow = Workflow(
|
||||
tenant_id=app.tenant_id,
|
||||
app_id=app.id,
|
||||
type=WorkflowType.WORKFLOW,
|
||||
version=version,
|
||||
graph=json.dumps({"nodes": [{"id": "entry", "data": {"type": entry_node_type}}], "edges": []}),
|
||||
_features="{}",
|
||||
_environment_variables=[],
|
||||
_conversation_variables=[],
|
||||
created_by=created_by,
|
||||
)
|
||||
for name, value in kwargs.items():
|
||||
setattr(workflow, name, value)
|
||||
return workflow
|
||||
|
||||
|
||||
def _snapshot(
|
||||
agent: Agent,
|
||||
version: int,
|
||||
created_at: datetime,
|
||||
*,
|
||||
version_note: str | None = None,
|
||||
created_by: str | None = None,
|
||||
) -> AgentConfigSnapshot:
|
||||
"""Create a test snapshot. None leaves the version note or creator unspecified."""
|
||||
return AgentConfigSnapshot(
|
||||
tenant_id=agent.tenant_id,
|
||||
agent_id=agent.id,
|
||||
version=version,
|
||||
config_snapshot={},
|
||||
created_at=created_at,
|
||||
version_note=version_note,
|
||||
created_by=created_by,
|
||||
)
|
||||
|
||||
|
||||
def _revision(
|
||||
snapshot: AgentConfigSnapshot, revision: int, operation: AgentConfigRevisionOperation, created_at: datetime
|
||||
) -> AgentConfigRevision:
|
||||
return AgentConfigRevision(
|
||||
tenant_id=snapshot.tenant_id,
|
||||
agent_id=snapshot.agent_id,
|
||||
current_snapshot_id=snapshot.id,
|
||||
revision=revision,
|
||||
operation=operation,
|
||||
created_at=created_at,
|
||||
)
|
||||
|
||||
|
||||
def _publish_agent(session: Session, app_id: str) -> Agent:
|
||||
"""Publish one snapshot for the roster Agent bound to the App."""
|
||||
agent = session.scalar(select(Agent).where(Agent.app_id == app_id))
|
||||
assert agent is not None
|
||||
snapshot = _snapshot(agent, 1, datetime(2026, 2, 1))
|
||||
session.add(snapshot)
|
||||
session.flush()
|
||||
session.add(_revision(snapshot, 1, AgentConfigRevisionOperation.PUBLISH_DRAFT, datetime(2026, 2, 1)))
|
||||
agent.active_config_snapshot_id = snapshot.id
|
||||
agent.active_config_is_published = True
|
||||
session.flush()
|
||||
return agent
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def seeded(sqlite_session: Session) -> dict[str, str]:
|
||||
creator = Account(name="Creator", email="creator@example.com", status=AccountStatus.ACTIVE)
|
||||
ws_a, ws_b = Tenant(name="Support"), Tenant(name="Sales")
|
||||
ws_a.id, ws_b.id = WS_A, WS_B
|
||||
sqlite_session.add_all([creator, ws_a, ws_b])
|
||||
sqlite_session.flush()
|
||||
|
||||
alpha = _app(WS_A, "Alpha 100%", created_at=datetime(2026, 1, 1), created_by=creator.id, description="d")
|
||||
beta = _app(WS_A, "beta", mode=AppMode.ADVANCED_CHAT, created_at=datetime(2026, 1, 3))
|
||||
gamma = _app(WS_B, "Gamma", mode=AppMode.CHAT, created_at=datetime(2026, 1, 2), created_by=str(uuid4()))
|
||||
roster = _app(WS_B, "Roster Agent", mode=AppMode.AGENT, created_at=datetime(2026, 1, 4))
|
||||
archived = _app(WS_B, "Archived Agent", mode=AppMode.AGENT, created_at=datetime(2026, 1, 5))
|
||||
backing = _app(WS_B, "Backing", mode=AppMode.AGENT, created_at=datetime(2026, 1, 6))
|
||||
hidden = [
|
||||
archived,
|
||||
backing,
|
||||
_app(WS_A, "Pipeline", mode=AppMode.RAG_PIPELINE, created_at=datetime(2026, 1, 7)),
|
||||
_app(WS_A, "Channel", mode=AppMode.CHANNEL, created_at=datetime(2026, 1, 8)),
|
||||
_app(WS_A, "Universal", created_at=datetime(2026, 1, 9), is_universal=True),
|
||||
]
|
||||
sqlite_session.add_all([alpha, beta, gamma, roster, *hidden])
|
||||
sqlite_session.add_all(
|
||||
[
|
||||
_agent(WS_B, scope=AgentScope.ROSTER, app_id=roster.id),
|
||||
_agent(WS_B, scope=AgentScope.ROSTER, status=AgentStatus.ARCHIVED, app_id=archived.id),
|
||||
_agent(WS_B, scope=AgentScope.WORKFLOW_ONLY, backing_app_id=backing.id),
|
||||
]
|
||||
)
|
||||
sqlite_session.commit()
|
||||
return {"alpha": alpha.id, "beta": beta.id, "gamma": gamma.id, "roster": roster.id, "creator": creator.id}
|
||||
|
||||
|
||||
def _list(app: Flask, query: str = "") -> EnterpriseAppPaginationResponse:
|
||||
with app.test_request_context(f"/enterprise/apps{query}"):
|
||||
body = inspect.unwrap(EnterpriseAppList.get)(EnterpriseAppList())
|
||||
return EnterpriseAppPaginationResponse.model_validate(body)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enterprise_apps")
|
||||
class TestEnterpriseAppList:
|
||||
@pytest.mark.usefixtures("seeded")
|
||||
def test_lists_studio_visible_apps_newest_first(self, app: Flask) -> None:
|
||||
body = _list(app)
|
||||
|
||||
assert [item.name for item in body.data] == ["Roster Agent", "beta", "Gamma", "Alpha 100%"]
|
||||
assert (body.page, body.limit, body.total, body.has_more) == (1, 20, 4, False)
|
||||
|
||||
def test_item_shape(self, app: Flask, seeded: dict[str, str]) -> None:
|
||||
items = {item.name: item for item in _list(app).data}
|
||||
|
||||
assert items["Alpha 100%"].model_dump(mode="json") == {
|
||||
"id": seeded["alpha"],
|
||||
"name": "Alpha 100%",
|
||||
"description": "d",
|
||||
"mode": "workflow",
|
||||
"icon_type": None,
|
||||
"icon": None,
|
||||
"icon_background": None,
|
||||
"workspace_id": WS_A,
|
||||
"workspace_name": "Support",
|
||||
"created_by": {"id": seeded["creator"], "name": "Creator", "email": "creator@example.com"},
|
||||
"created_at": int(datetime(2026, 1, 1).timestamp()),
|
||||
"updated_at": int(datetime(2026, 1, 1).timestamp()),
|
||||
"published": False,
|
||||
"invocation_count_24h": 0,
|
||||
}
|
||||
# A creator whose account no longer exists is reported as missing.
|
||||
assert items["Gamma"].created_by is None
|
||||
|
||||
def test_item_publish_and_invocation_summary(
|
||||
self, app: Flask, sqlite_session: Session, seeded: dict[str, str]
|
||||
) -> None:
|
||||
workflow_app = sqlite_session.get(App, seeded["alpha"])
|
||||
assert workflow_app is not None
|
||||
published = _workflow(workflow_app, version="2026-01-02 00:00:00", created_by=seeded["creator"])
|
||||
sqlite_session.add(published)
|
||||
sqlite_session.flush()
|
||||
workflow_app.workflow_id = published.id
|
||||
# beta points at a workflow that no longer exists, so it is not published.
|
||||
beta = sqlite_session.get(App, seeded["beta"])
|
||||
assert beta is not None
|
||||
beta.workflow_id = str(uuid4())
|
||||
_publish_agent(sqlite_session, seeded["roster"])
|
||||
sqlite_session.add_all(
|
||||
[
|
||||
_run(workflow_app, WorkflowExecutionStatus.SUCCEEDED, NOW - timedelta(hours=1)),
|
||||
_run(workflow_app, WorkflowExecutionStatus.FAILED, NOW - timedelta(hours=2)),
|
||||
_run(workflow_app, WorkflowExecutionStatus.SUCCEEDED, NOW - timedelta(hours=25)),
|
||||
_run(
|
||||
workflow_app,
|
||||
WorkflowExecutionStatus.SUCCEEDED,
|
||||
NOW - timedelta(minutes=1),
|
||||
triggered_from=WorkflowRunTriggeredFrom.DEBUGGING,
|
||||
),
|
||||
_message(seeded["gamma"], MessageStatus.NORMAL, NOW - timedelta(hours=1), InvokeFrom.WEB_APP),
|
||||
_message(seeded["gamma"], MessageStatus.NORMAL, NOW - timedelta(hours=1), InvokeFrom.DEBUGGER),
|
||||
_message(seeded["roster"], MessageStatus.ERROR, NOW - timedelta(hours=3), InvokeFrom.SERVICE_API),
|
||||
]
|
||||
)
|
||||
sqlite_session.commit()
|
||||
|
||||
items = _list(app).data
|
||||
|
||||
assert {item.name: (item.published, item.invocation_count_24h) for item in items} == {
|
||||
"Alpha 100%": (True, 2),
|
||||
"beta": (False, 0),
|
||||
"Gamma": (False, 1),
|
||||
"Roster Agent": (True, 1),
|
||||
}
|
||||
for item in items:
|
||||
detail = _detail(app, item.id)
|
||||
assert item.published == (detail.published_version is not None)
|
||||
assert item.invocation_count_24h == detail.invocation_count_24h
|
||||
|
||||
def test_agent_draft_edits_keep_live_publication(
|
||||
self, app: Flask, sqlite_session: Session, seeded: dict[str, str]
|
||||
) -> None:
|
||||
agent = _publish_agent(sqlite_session, seeded["roster"])
|
||||
# Saving draft edits after publishing marks the draft dirty, but the
|
||||
# active snapshot keeps serving public requests.
|
||||
agent.active_config_is_published = False
|
||||
sqlite_session.commit()
|
||||
|
||||
items = {item.name: item for item in _list(app).data}
|
||||
detail = _detail(app, seeded["roster"])
|
||||
|
||||
assert items["Roster Agent"].published is True
|
||||
assert detail.published_version is not None
|
||||
assert detail.published_version.id == agent.active_config_snapshot_id
|
||||
|
||||
def test_agent_seed_snapshot_is_not_published(
|
||||
self, app: Flask, sqlite_session: Session, seeded: dict[str, str]
|
||||
) -> None:
|
||||
agent = sqlite_session.scalar(select(Agent).where(Agent.app_id == seeded["roster"]))
|
||||
assert agent is not None
|
||||
seed = _snapshot(agent, 1, datetime(2026, 1, 15))
|
||||
sqlite_session.add(seed)
|
||||
sqlite_session.flush()
|
||||
sqlite_session.add(_revision(seed, 1, AgentConfigRevisionOperation.CREATE_VERSION, datetime(2026, 1, 15)))
|
||||
# The seed matches the draft, so the flag alone would report it published.
|
||||
agent.active_config_snapshot_id = seed.id
|
||||
agent.active_config_is_published = True
|
||||
sqlite_session.commit()
|
||||
|
||||
items = {item.name: item for item in _list(app).data}
|
||||
|
||||
assert items["Roster Agent"].published is False
|
||||
assert _detail(app, seeded["roster"]).published_version is None
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("query", "expected"),
|
||||
[
|
||||
(f"?workspace_ids={WS_B}", ["Roster Agent", "Gamma"]),
|
||||
(f"?workspace_ids={WS_A}&workspace_ids={WS_B}", ["Roster Agent", "beta", "Gamma", "Alpha 100%"]),
|
||||
("?modes=workflow&modes=chat", ["Gamma", "Alpha 100%"]),
|
||||
("?modes=agent", ["Roster Agent"]),
|
||||
("?name=A", ["Roster Agent", "beta", "Gamma", "Alpha 100%"]),
|
||||
("?name=100%25", ["Alpha 100%"]),
|
||||
("?name=%25", ["Alpha 100%"]),
|
||||
("?name=%20%20", ["Roster Agent", "beta", "Gamma", "Alpha 100%"]),
|
||||
("?sort_by=name&sort_order=asc", ["Alpha 100%", "beta", "Gamma", "Roster Agent"]),
|
||||
("?sort_by=created_at&sort_order=asc", ["Alpha 100%", "Gamma", "beta", "Roster Agent"]),
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("seeded")
|
||||
def test_filters_and_sorting(self, app: Flask, query: str, expected: list[str]) -> None:
|
||||
assert [item.name for item in _list(app, query).data] == expected
|
||||
|
||||
@pytest.mark.usefixtures("seeded")
|
||||
def test_pagination(self, app: Flask) -> None:
|
||||
first = _list(app, "?limit=3")
|
||||
second = _list(app, "?limit=3&page=2")
|
||||
|
||||
assert (first.total, first.has_more, len(first.data)) == (4, True, 3)
|
||||
assert (second.total, second.has_more, [item.name for item in second.data]) == (
|
||||
4,
|
||||
False,
|
||||
["Alpha 100%"],
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"query", ["?workspace_ids=not-a-uuid", "?modes=rag-pipeline", "?limit=0", "?limit=101", "?sort_by=calls"]
|
||||
)
|
||||
def test_invalid_query_is_rejected(self, app: Flask, query: str) -> None:
|
||||
with pytest.raises(BadRequest):
|
||||
_list(app, query)
|
||||
|
||||
def test_documents_query_parameters(self) -> None:
|
||||
params = cast(DocumentedView, EnterpriseAppList.get).__apidoc__["params"]
|
||||
for name in ("page", "limit", "workspace_ids", "modes", "name", "sort_by", "sort_order"):
|
||||
assert params[name]["in"] == "query"
|
||||
|
||||
|
||||
def _detail_raw(app: Flask, app_id: str) -> object:
|
||||
with app.test_request_context(f"/enterprise/apps/{app_id}"):
|
||||
return inspect.unwrap(EnterpriseAppDetail.get)(EnterpriseAppDetail(), app_id=UUID(app_id))
|
||||
|
||||
|
||||
def _detail(app: Flask, app_id: str) -> EnterpriseAppDetailResponse:
|
||||
return EnterpriseAppDetailResponse.model_validate(_detail_raw(app, app_id))
|
||||
|
||||
|
||||
def _run(
|
||||
app: App,
|
||||
status: WorkflowExecutionStatus,
|
||||
created_at: datetime,
|
||||
*,
|
||||
triggered_from: WorkflowRunTriggeredFrom = WorkflowRunTriggeredFrom.APP_RUN,
|
||||
) -> WorkflowRun:
|
||||
run = WorkflowRun(
|
||||
tenant_id=app.tenant_id,
|
||||
app_id=app.id,
|
||||
workflow_id=app.workflow_id or str(uuid4()),
|
||||
type=WorkflowType.WORKFLOW,
|
||||
triggered_from=triggered_from,
|
||||
version="v",
|
||||
graph="{}",
|
||||
inputs="{}",
|
||||
status=status,
|
||||
created_by_role=CreatorUserRole.END_USER,
|
||||
created_by=str(uuid4()),
|
||||
)
|
||||
run.created_at = created_at
|
||||
return run
|
||||
|
||||
|
||||
def _message(app_id: str, status: MessageStatus, created_at: datetime, invoke_from: InvokeFrom) -> Message:
|
||||
message = Message(
|
||||
app_id=app_id,
|
||||
conversation_id=str(uuid4()),
|
||||
_inputs={},
|
||||
query="q",
|
||||
message={},
|
||||
message_unit_price=Decimal(0),
|
||||
answer="a",
|
||||
answer_unit_price=Decimal(0),
|
||||
currency="USD",
|
||||
from_source=ConversationFromSource.API,
|
||||
status=status,
|
||||
invoke_from=invoke_from,
|
||||
)
|
||||
message.created_at = created_at
|
||||
return message
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enterprise_apps")
|
||||
class TestEnterpriseAppDetail:
|
||||
def test_workflow_app_state(self, app: Flask, sqlite_session: Session, seeded: dict[str, str]) -> None:
|
||||
workflow_app = sqlite_session.get(App, seeded["alpha"])
|
||||
assert workflow_app is not None
|
||||
published = _workflow(
|
||||
workflow_app,
|
||||
version="2026-01-02 00:00:00",
|
||||
version_number=3,
|
||||
marked_name="Launch",
|
||||
marked_comment="First release",
|
||||
created_by=seeded["creator"],
|
||||
created_at=datetime(2026, 1, 2),
|
||||
)
|
||||
sqlite_session.add_all(
|
||||
[
|
||||
published,
|
||||
_workflow(workflow_app, version="2026-01-03 00:00:00", version_number=4, created_by=seeded["creator"]),
|
||||
_workflow(workflow_app, version=Workflow.VERSION_DRAFT, created_by=seeded["creator"]),
|
||||
]
|
||||
)
|
||||
sqlite_session.flush()
|
||||
workflow_app.workflow_id = published.id
|
||||
workflow_app.enable_site = True
|
||||
webhook = AppTrigger(
|
||||
tenant_id=WS_A,
|
||||
app_id=workflow_app.id,
|
||||
node_id="hook",
|
||||
trigger_type=AppTriggerType.TRIGGER_WEBHOOK,
|
||||
title="Hook",
|
||||
)
|
||||
schedule = AppTrigger(
|
||||
tenant_id=WS_A,
|
||||
app_id=workflow_app.id,
|
||||
node_id="cron",
|
||||
trigger_type=AppTriggerType.TRIGGER_SCHEDULE,
|
||||
title="Cron",
|
||||
status=AppTriggerStatus.DISABLED,
|
||||
)
|
||||
sqlite_session.add_all(
|
||||
[
|
||||
Site(
|
||||
app_id=workflow_app.id,
|
||||
title="t",
|
||||
default_language="en-US",
|
||||
customize_token_strategy=CustomizeTokenStrategy.NOT_ALLOW,
|
||||
code="site-code",
|
||||
),
|
||||
ApiToken(app_id=workflow_app.id, tenant_id=WS_A, type=ApiTokenType.APP, token="app-1"),
|
||||
ApiToken(app_id=workflow_app.id, tenant_id=WS_A, type=ApiTokenType.APP, token="app-2"),
|
||||
AppMCPServer(
|
||||
tenant_id=WS_A,
|
||||
app_id=workflow_app.id,
|
||||
name="n",
|
||||
description="d",
|
||||
server_code="mcp-code",
|
||||
status="active",
|
||||
parameters="{}",
|
||||
),
|
||||
webhook,
|
||||
schedule,
|
||||
WorkflowWebhookTrigger(
|
||||
app_id=workflow_app.id,
|
||||
node_id="hook",
|
||||
tenant_id=WS_A,
|
||||
webhook_id="whk",
|
||||
created_by=seeded["creator"],
|
||||
),
|
||||
_run(workflow_app, WorkflowExecutionStatus.SUCCEEDED, NOW - timedelta(hours=1)),
|
||||
_run(workflow_app, WorkflowExecutionStatus.FAILED, NOW - timedelta(hours=2)),
|
||||
_run(workflow_app, WorkflowExecutionStatus.PARTIAL_SUCCEEDED, NOW - timedelta(hours=3)),
|
||||
_run(workflow_app, WorkflowExecutionStatus.STOPPED, NOW - timedelta(hours=4)),
|
||||
# Outside the 24h window.
|
||||
_run(workflow_app, WorkflowExecutionStatus.FAILED, NOW - timedelta(hours=25)),
|
||||
# Debugger runs are not invocations, even when newest.
|
||||
_run(
|
||||
workflow_app,
|
||||
WorkflowExecutionStatus.FAILED,
|
||||
NOW - timedelta(minutes=1),
|
||||
triggered_from=WorkflowRunTriggeredFrom.DEBUGGING,
|
||||
),
|
||||
]
|
||||
)
|
||||
sqlite_session.commit()
|
||||
|
||||
body = _detail(app, seeded["alpha"])
|
||||
|
||||
assert (body.id, body.workspace_name) == (seeded["alpha"], "Support")
|
||||
detail = body.model_dump(mode="json")
|
||||
assert detail["published_version"] == {
|
||||
"id": published.id,
|
||||
"version": "2026-01-02 00:00:00",
|
||||
"version_number": 3,
|
||||
"marked_name": "Launch",
|
||||
"marked_comment": "First release",
|
||||
"created_by": {"id": seeded["creator"], "name": "Creator", "email": "creator@example.com"},
|
||||
"published_at": int(datetime(2026, 1, 2).timestamp()),
|
||||
}
|
||||
assert detail["published_version_count"] == 2
|
||||
assert detail["site"] == {"enabled": True, "code": "site-code", "app_base_url": "https://apps.example.com"}
|
||||
assert detail["api"] == {"enabled": False, "base_url": "https://api.example.com/v1", "api_key_count": 2}
|
||||
assert detail["mcp_server"] == {"status": "active", "server_code": "mcp-code"}
|
||||
triggers = {trigger.node_id: trigger for trigger in body.triggers}
|
||||
assert (triggers["hook"].webhook_url or "").endswith("/triggers/webhook/whk")
|
||||
assert triggers["cron"].model_dump(mode="json") | {"id": None} == {
|
||||
"id": None,
|
||||
"node_id": "cron",
|
||||
"title": "Cron",
|
||||
"trigger_type": "trigger-schedule",
|
||||
"provider_name": "",
|
||||
"status": "disabled",
|
||||
"webhook_url": None,
|
||||
}
|
||||
assert detail["last_invoked_at"] == int((NOW - timedelta(hours=1)).timestamp())
|
||||
assert (detail["invocation_count_24h"], detail["failed_invocation_count_24h"]) == (4, 2)
|
||||
|
||||
def test_chat_app_counts_messages_and_has_no_version(
|
||||
self, app: Flask, sqlite_session: Session, seeded: dict[str, str]
|
||||
) -> None:
|
||||
sqlite_session.add_all(
|
||||
[
|
||||
_message(seeded["gamma"], MessageStatus.NORMAL, NOW - timedelta(hours=1), InvokeFrom.WEB_APP),
|
||||
_message(seeded["gamma"], MessageStatus.ERROR, NOW - timedelta(hours=2), InvokeFrom.SERVICE_API),
|
||||
_message(seeded["gamma"], MessageStatus.ERROR, NOW - timedelta(minutes=1), InvokeFrom.DEBUGGER),
|
||||
_message(seeded["gamma"], MessageStatus.NORMAL, NOW - timedelta(hours=30), InvokeFrom.WEB_APP),
|
||||
]
|
||||
)
|
||||
sqlite_session.commit()
|
||||
|
||||
detail = _detail(app, seeded["gamma"]).model_dump(mode="json")
|
||||
|
||||
assert (detail["published_version"], detail["published_version_count"]) == (None, 0)
|
||||
assert detail["site"] == {"enabled": False, "code": None, "app_base_url": "https://apps.example.com"}
|
||||
assert detail["mcp_server"] is None
|
||||
assert detail["triggers"] == []
|
||||
assert detail["last_invoked_at"] == int((NOW - timedelta(hours=1)).timestamp())
|
||||
assert (detail["invocation_count_24h"], detail["failed_invocation_count_24h"]) == (2, 1)
|
||||
|
||||
@pytest.mark.usefixtures("seeded")
|
||||
def test_app_without_invocations(self, app: Flask, seeded: dict[str, str]) -> None:
|
||||
detail = _detail(app, seeded["beta"])
|
||||
|
||||
assert detail.published_version is None
|
||||
assert (detail.last_invoked_at, detail.invocation_count_24h) == (None, 0)
|
||||
|
||||
def test_agent_app_published_version_uses_latest_revision(
|
||||
self, app: Flask, sqlite_session: Session, seeded: dict[str, str]
|
||||
) -> None:
|
||||
agent = sqlite_session.scalar(select(Agent).where(Agent.app_id == seeded["roster"]))
|
||||
assert agent is not None
|
||||
# The initial snapshot is not in an Agent App's version history.
|
||||
initial = _snapshot(agent, 1, datetime(2026, 1, 15))
|
||||
served = _snapshot(agent, 2, datetime(2026, 2, 1), version_note="Tuned prompt", created_by=seeded["creator"])
|
||||
newer = _snapshot(agent, 3, datetime(2026, 2, 15))
|
||||
sqlite_session.add_all([initial, served, newer])
|
||||
sqlite_session.flush()
|
||||
agent.active_config_snapshot_id = served.id
|
||||
agent.active_config_is_published = True
|
||||
sqlite_session.add_all(
|
||||
[
|
||||
_revision(initial, 1, AgentConfigRevisionOperation.CREATE_VERSION, datetime(2026, 1, 15)),
|
||||
_revision(served, 2, AgentConfigRevisionOperation.PUBLISH_DRAFT, datetime(2026, 2, 1)),
|
||||
_revision(newer, 3, AgentConfigRevisionOperation.PUBLISH_DRAFT, datetime(2026, 2, 15)),
|
||||
_revision(served, 4, AgentConfigRevisionOperation.RESTORE_VERSION, datetime(2026, 3, 1)),
|
||||
]
|
||||
)
|
||||
sqlite_session.commit()
|
||||
|
||||
detail = _detail(app, seeded["roster"]).model_dump(mode="json")
|
||||
|
||||
assert detail["published_version"] == {
|
||||
"id": served.id,
|
||||
"version": None,
|
||||
"version_number": 1,
|
||||
"marked_name": None,
|
||||
"marked_comment": "Tuned prompt",
|
||||
"created_by": {"id": seeded["creator"], "name": "Creator", "email": "creator@example.com"},
|
||||
"published_at": int(datetime(2026, 3, 1).timestamp()),
|
||||
}
|
||||
assert detail["published_version_count"] == 2
|
||||
|
||||
@pytest.mark.usefixtures("seeded")
|
||||
def test_missing_or_hidden_app_returns_404(self, app: Flask, sqlite_session: Session) -> None:
|
||||
hidden_ids = sqlite_session.scalars(select(App.id).where(App.name.in_(["Archived Agent", "Universal"]))).all()
|
||||
assert len(hidden_ids) == 2
|
||||
listed_ids = {item.id for item in _list(app).data}
|
||||
for app_id in (str(uuid4()), *hidden_ids):
|
||||
assert app_id not in listed_ids
|
||||
assert _detail_raw(app, app_id) == ({"message": "app not found"}, 404)
|
||||
|
||||
def test_trigger_entry_workflow_has_no_manual_access_points(
|
||||
self, app: Flask, sqlite_session: Session, seeded: dict[str, str]
|
||||
) -> None:
|
||||
workflow_app = sqlite_session.get(App, seeded["alpha"])
|
||||
assert workflow_app is not None
|
||||
published = _workflow(
|
||||
workflow_app, version="2026-01-02 00:00:00", created_by=seeded["creator"], entry_node_type="trigger-webhook"
|
||||
)
|
||||
sqlite_session.add(published)
|
||||
sqlite_session.flush()
|
||||
workflow_app.workflow_id = published.id
|
||||
workflow_app.enable_site = True
|
||||
workflow_app.enable_api = True
|
||||
sqlite_session.add_all(
|
||||
[
|
||||
AppTrigger(
|
||||
tenant_id=WS_A,
|
||||
app_id=workflow_app.id,
|
||||
node_id="entry",
|
||||
trigger_type=AppTriggerType.TRIGGER_WEBHOOK,
|
||||
title="Hook",
|
||||
),
|
||||
AppMCPServer(
|
||||
tenant_id=WS_A,
|
||||
app_id=workflow_app.id,
|
||||
name="n",
|
||||
description="d",
|
||||
server_code="mcp-code",
|
||||
status="active",
|
||||
parameters="{}",
|
||||
),
|
||||
]
|
||||
)
|
||||
sqlite_session.commit()
|
||||
|
||||
detail = _detail(app, seeded["alpha"])
|
||||
|
||||
assert (detail.site, detail.api, detail.mcp_server) == (None, None, None)
|
||||
assert [trigger.node_id for trigger in detail.triggers] == ["entry"]
|
||||
assert detail.published_version is not None
|
||||
|
||||
def test_base_urls_are_null_when_not_configured(
|
||||
self,
|
||||
app: Flask,
|
||||
sqlite_session_factory: sessionmaker[Session],
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
seeded: dict[str, str],
|
||||
) -> None:
|
||||
_install_service(app, sqlite_session_factory, monkeypatch, app_web_url="", service_api_url="")
|
||||
|
||||
detail = _detail(app, seeded["alpha"])
|
||||
|
||||
assert detail.site is not None
|
||||
assert detail.api is not None
|
||||
assert detail.site.app_base_url is None
|
||||
assert detail.api.base_url is None
|
||||
|
||||
|
||||
class _StubWorkflowRuns:
|
||||
"""Stands in for a workflow run store outside the database, such as LogStore."""
|
||||
|
||||
def __init__(self, app_id: str) -> None:
|
||||
self.app_id = app_id
|
||||
self.calls: list[tuple[dict[str, str], list[WorkflowRunTriggeredFrom]]] = []
|
||||
|
||||
def get_stats_by_app_ids(
|
||||
self,
|
||||
tenant_id_by_app_id: Mapping[str, str],
|
||||
triggered_from: Sequence[WorkflowRunTriggeredFrom],
|
||||
created_since: datetime,
|
||||
) -> dict[str, AppWorkflowRunStats]:
|
||||
assert created_since == NOW - timedelta(hours=24)
|
||||
self.calls.append((dict(tenant_id_by_app_id), list(triggered_from)))
|
||||
return {
|
||||
self.app_id: AppWorkflowRunStats(
|
||||
status_counts={
|
||||
WorkflowExecutionStatus.SUCCEEDED: 3,
|
||||
WorkflowExecutionStatus.RUNNING: 1,
|
||||
WorkflowExecutionStatus.STOPPED: 1,
|
||||
WorkflowExecutionStatus.FAILED: 2,
|
||||
WorkflowExecutionStatus.PARTIAL_SUCCEEDED: 1,
|
||||
},
|
||||
last_created_at=NOW - timedelta(minutes=5),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
def test_workflow_invocations_come_from_the_workflow_run_repository(
|
||||
app: Flask, sqlite_session_factory: sessionmaker[Session], monkeypatch: pytest.MonkeyPatch, seeded: dict[str, str]
|
||||
) -> None:
|
||||
runs = _StubWorkflowRuns(seeded["alpha"])
|
||||
service = EnterpriseAppService(
|
||||
apps=EnterpriseAppRepository(session_factory=sqlite_session_factory, app_web_url="", service_api_url=""),
|
||||
workflow_runs=cast(APIWorkflowRunRepository, runs),
|
||||
now=lambda: NOW,
|
||||
)
|
||||
monkeypatch.setitem(app.extensions, "application_services", _Services(apps=_Apps(service)))
|
||||
|
||||
items = {item.name: item for item in _list(app).data}
|
||||
detail = _detail(app, seeded["alpha"])
|
||||
|
||||
assert items["Alpha 100%"].invocation_count_24h == 8
|
||||
assert (detail.invocation_count_24h, detail.failed_invocation_count_24h) == (8, 3)
|
||||
assert detail.last_invoked_at == int((NOW - timedelta(minutes=5)).timestamp())
|
||||
sources = [
|
||||
WorkflowRunTriggeredFrom.APP_RUN,
|
||||
WorkflowRunTriggeredFrom.WEBHOOK,
|
||||
WorkflowRunTriggeredFrom.SCHEDULE,
|
||||
WorkflowRunTriggeredFrom.PLUGIN,
|
||||
]
|
||||
assert runs.calls == [
|
||||
({seeded["beta"]: WS_A, seeded["alpha"]: WS_A}, sources),
|
||||
({seeded["alpha"]: WS_A}, sources),
|
||||
]
|
||||
@@ -0,0 +1,340 @@
|
||||
"""Unit tests for inner_api app DSL import/export endpoints.
|
||||
|
||||
Tests Pydantic model validation and endpoint handler logic. Auth/setup decorators are tested separately
|
||||
in test_auth_wraps.py; handler tests use inspect.unwrap() to bypass them.
|
||||
"""
|
||||
|
||||
import inspect
|
||||
from typing import Protocol, cast
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from pydantic import ValidationError
|
||||
from sqlalchemy import event
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.orm import Session
|
||||
from werkzeug.exceptions import UnprocessableEntity
|
||||
|
||||
from controllers.inner_api.app.dsl import (
|
||||
EnterpriseAppDSLExport,
|
||||
EnterpriseAppDSLImport,
|
||||
InnerAppDSLImportPayload,
|
||||
)
|
||||
from controllers.inner_api.wraps import InnerApiUnauthorizedError
|
||||
from models import Account, App, Tenant, TenantAccountJoin
|
||||
from models.account import AccountStatus, TenantAccountRole
|
||||
from models.model import AppMode
|
||||
from services.app_dsl_service import AppDslService
|
||||
from services.entities.app_entities import AppExportOptions
|
||||
from services.entities.dsl_entities import Import, ImportStatus
|
||||
from services.errors.app import AppDiscoveryNotFoundError, IsDraftWorkflowError, WorkflowNotFoundError
|
||||
from tests.helpers.unit.config_override import config_overrides_context
|
||||
from tests.unit_tests.controllers.conftest import ControllerTestServices
|
||||
|
||||
|
||||
class DocumentedView(Protocol):
|
||||
__apidoc__: dict[str, dict[str, dict[str, object]]]
|
||||
|
||||
|
||||
def _persist_account(session: Session, *, workspace_id: str = "ws-123") -> Account:
|
||||
account = Account(name="DSL Creator", email="user@example.com", status=AccountStatus.ACTIVE)
|
||||
tenant = Tenant(name="DSL Workspace")
|
||||
tenant.id = workspace_id
|
||||
session.add_all([account, tenant])
|
||||
session.flush()
|
||||
session.add(
|
||||
TenantAccountJoin(
|
||||
tenant_id=tenant.id,
|
||||
account_id=account.id,
|
||||
current=True,
|
||||
role=TenantAccountRole.OWNER,
|
||||
)
|
||||
)
|
||||
session.commit()
|
||||
return account
|
||||
|
||||
|
||||
class TestInnerAppDSLImportPayload:
|
||||
"""Test InnerAppDSLImportPayload Pydantic model validation."""
|
||||
|
||||
def test_valid_payload_all_fields(self) -> None:
|
||||
data = {
|
||||
"yaml_content": "version: 0.6.0\nkind: app\n",
|
||||
"creator_email": "user@example.com",
|
||||
"name": "My App",
|
||||
"description": "A test app",
|
||||
}
|
||||
payload = InnerAppDSLImportPayload.model_validate(data)
|
||||
assert payload.yaml_content == data["yaml_content"]
|
||||
assert payload.creator_email == "user@example.com"
|
||||
assert payload.name == "My App"
|
||||
assert payload.description == "A test app"
|
||||
|
||||
def test_valid_payload_optional_fields_omitted(self) -> None:
|
||||
data = {
|
||||
"yaml_content": "version: 0.6.0\n",
|
||||
"creator_email": "user@example.com",
|
||||
}
|
||||
payload = InnerAppDSLImportPayload.model_validate(data)
|
||||
assert payload.name is None
|
||||
assert payload.description is None
|
||||
|
||||
def test_missing_yaml_content_fails(self) -> None:
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
InnerAppDSLImportPayload.model_validate({"creator_email": "a@b.com"})
|
||||
assert "yaml_content" in str(exc_info.value)
|
||||
|
||||
def test_missing_creator_email_fails(self) -> None:
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
InnerAppDSLImportPayload.model_validate({"yaml_content": "test"})
|
||||
assert "creator_email" in str(exc_info.value)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("app_query_services")
|
||||
class TestEnterpriseAppDSLImport:
|
||||
"""Exercise the composed import path with real account and membership reads."""
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("import_status", "http_status", "transaction"),
|
||||
[
|
||||
(ImportStatus.COMPLETED, 200, "commit"),
|
||||
(ImportStatus.PENDING, 202, "commit"),
|
||||
(ImportStatus.FAILED, 400, "rollback"),
|
||||
],
|
||||
)
|
||||
def test_import_releases_lookup_connections_and_finishes_transaction(
|
||||
self,
|
||||
app: Flask,
|
||||
sqlite_session: Session,
|
||||
sqlite_engine: Engine,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
import_status: ImportStatus,
|
||||
http_status: int,
|
||||
transaction: str,
|
||||
) -> None:
|
||||
account = _persist_account(sqlite_session)
|
||||
account_id = account.id
|
||||
sqlite_session.close()
|
||||
connections: set[object] = set()
|
||||
transactions: list[str] = []
|
||||
|
||||
def checkout(connection: object, *_args: object) -> None:
|
||||
connections.add(connection)
|
||||
|
||||
def checkin(connection: object, *_args: object) -> None:
|
||||
connections.discard(connection)
|
||||
|
||||
def committed(session: Session) -> None:
|
||||
if session.get_bind() is sqlite_engine:
|
||||
transactions.append("commit")
|
||||
|
||||
def rolled_back(session: Session) -> None:
|
||||
if session.get_bind() is sqlite_engine:
|
||||
transactions.append("rollback")
|
||||
|
||||
def import_app(service: AppDslService, *, account: Account, **kwargs: object) -> Import:
|
||||
assert not connections
|
||||
assert account.id == account_id
|
||||
assert account.current_tenant_id == "ws-123"
|
||||
assert kwargs["yaml_content"] == "version: 0.6.0\n"
|
||||
# Open a real write transaction to verify the adapter's completion policy.
|
||||
service._session.add(
|
||||
App(tenant_id="ws-123", name="imported", mode=AppMode.WORKFLOW, enable_site=False, enable_api=False)
|
||||
)
|
||||
service._session.flush()
|
||||
return Import(id="import-id", status=import_status, app_id="app-123", app_mode="workflow")
|
||||
|
||||
monkeypatch.setattr(AppDslService, "import_app", import_app)
|
||||
listeners = [
|
||||
(sqlite_engine, "checkout", checkout),
|
||||
(sqlite_engine, "checkin", checkin),
|
||||
(Session, "after_commit", committed),
|
||||
(Session, "after_rollback", rolled_back),
|
||||
]
|
||||
for target, name, callback in listeners:
|
||||
event.listen(target, name, callback)
|
||||
try:
|
||||
payload = InnerAppDSLImportPayload(yaml_content="version: 0.6.0\n", creator_email="user@example.com")
|
||||
with app.test_request_context(method="POST", json=payload.model_dump()):
|
||||
body, status = inspect.unwrap(EnterpriseAppDSLImport.post)(
|
||||
EnterpriseAppDSLImport(), workspace_id="ws-123"
|
||||
)
|
||||
assert status == http_status
|
||||
assert body["status"] == import_status
|
||||
assert transactions == [transaction]
|
||||
assert not connections
|
||||
finally:
|
||||
for target, name, callback in listeners:
|
||||
event.remove(target, name, callback)
|
||||
|
||||
@pytest.mark.parametrize("creator_email", ["missing@example.com", "banned@example.com", "USER@example.com"])
|
||||
def test_missing_or_inactive_creator_returns_404(
|
||||
self,
|
||||
app: Flask,
|
||||
sqlite_session: Session,
|
||||
creator_email: str,
|
||||
) -> None:
|
||||
sqlite_session.add_all(
|
||||
[
|
||||
Account(name="Banned", email="banned@example.com", status=AccountStatus.BANNED),
|
||||
Account(name="Active", email="user@example.com", status=AccountStatus.ACTIVE),
|
||||
]
|
||||
)
|
||||
sqlite_session.commit()
|
||||
payload = InnerAppDSLImportPayload(yaml_content="test", creator_email=creator_email)
|
||||
with app.test_request_context(method="POST", json=payload.model_dump()):
|
||||
body, status = inspect.unwrap(EnterpriseAppDSLImport.post)(EnterpriseAppDSLImport(), workspace_id="ws-123")
|
||||
assert status == 404
|
||||
assert body == {"message": f"account '{creator_email}' not found or inactive"}
|
||||
|
||||
|
||||
class TestEnterpriseAppDSLExport:
|
||||
def test_export_documents_query_parameters(self) -> None:
|
||||
params = cast(DocumentedView, EnterpriseAppDSLExport.get).__apidoc__["params"]
|
||||
assert params["include_secret"]["in"] == "query"
|
||||
assert params["workflow_id"]["in"] == "query"
|
||||
assert params["workflow_id"]["format"] == "uuid"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("query", "secret", "workflow_id"),
|
||||
[
|
||||
("", False, None),
|
||||
("?include_secret=TRUE", True, None),
|
||||
("?include_secret=1", False, None),
|
||||
("?workflow_id=F1FD7266-56FC-45C7-9D81-A72CD5A1B4F6", False, "f1fd7266-56fc-45c7-9d81-a72cd5a1b4f6"),
|
||||
],
|
||||
)
|
||||
def test_export_parses_options(
|
||||
self,
|
||||
app: Flask,
|
||||
app_query_services: ControllerTestServices,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
query: str,
|
||||
secret: bool,
|
||||
workflow_id: str | None,
|
||||
) -> None:
|
||||
calls: list[tuple[str, AppExportOptions]] = []
|
||||
|
||||
def export(app_id: str, options: AppExportOptions) -> str:
|
||||
calls.append((app_id, options))
|
||||
return "yaml-data"
|
||||
|
||||
monkeypatch.setattr(app_query_services.apps.exports, "export_for_inner", export)
|
||||
with app.test_request_context(query):
|
||||
result = inspect.unwrap(EnterpriseAppDSLExport.get)(EnterpriseAppDSLExport(), app_id="app-id")
|
||||
assert result == ({"data": "yaml-data"}, 200)
|
||||
assert calls == [("app-id", AppExportOptions(include_secret=secret, workflow_id=workflow_id))]
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error", "query", "expected"),
|
||||
[
|
||||
(AppDiscoveryNotFoundError(), "", ({"message": "app not found"}, 404)),
|
||||
(
|
||||
WorkflowNotFoundError("missing"),
|
||||
"?workflow_id=f1fd7266-56fc-45c7-9d81-a72cd5a1b4f6",
|
||||
({"code": "workflow_version_not_found", "message": "missing", "status": 404}, 404),
|
||||
),
|
||||
(
|
||||
IsDraftWorkflowError("draft"),
|
||||
"?workflow_id=f1fd7266-56fc-45c7-9d81-a72cd5a1b4f6",
|
||||
({"code": "workflow_version_not_published", "message": "draft", "status": 400}, 400),
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_export_maps_domain_errors(
|
||||
self,
|
||||
app: Flask,
|
||||
app_query_services: ControllerTestServices,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
error: Exception,
|
||||
query: str,
|
||||
expected: tuple[dict[str, object], int],
|
||||
) -> None:
|
||||
def export(*_args: object) -> str:
|
||||
raise error
|
||||
|
||||
monkeypatch.setattr(app_query_services.apps.exports, "export_for_inner", export)
|
||||
with app.test_request_context(query):
|
||||
assert inspect.unwrap(EnterpriseAppDSLExport.get)(EnterpriseAppDSLExport(), app_id="app-id") == expected
|
||||
|
||||
def test_invalid_workflow_id_is_rejected_before_service_lookup(self, app: Flask) -> None:
|
||||
with app.test_request_context("?workflow_id=invalid"):
|
||||
result = inspect.unwrap(EnterpriseAppDSLExport.get)(EnterpriseAppDSLExport(), app_id="app-id")
|
||||
assert result == (
|
||||
{"code": "invalid_workflow_id", "message": "workflow_id must be a valid UUID", "status": 400},
|
||||
400,
|
||||
)
|
||||
|
||||
def test_default_draft_error_is_propagated(
|
||||
self,
|
||||
app: Flask,
|
||||
app_query_services: ControllerTestServices,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
def export(*_args: object) -> str:
|
||||
raise WorkflowNotFoundError("Missing draft workflow configuration")
|
||||
|
||||
monkeypatch.setattr(app_query_services.apps.exports, "export_for_inner", export)
|
||||
with app.test_request_context(), pytest.raises(WorkflowNotFoundError, match="Missing draft"):
|
||||
inspect.unwrap(EnterpriseAppDSLExport.get)(EnterpriseAppDSLExport(), app_id="app-id")
|
||||
|
||||
|
||||
class TestInnerAppAdmission:
|
||||
"""Exercise admission and payload validation through the decorated entry point."""
|
||||
|
||||
def test_invalid_body_is_rejected_before_the_handler_runs(self, app: Flask) -> None:
|
||||
api_instance = EnterpriseAppDSLImport()
|
||||
|
||||
with (
|
||||
patch("controllers.console.wraps._is_setup_completed", return_value=True),
|
||||
config_overrides_context(INNER_API=True, INNER_API_KEY="inner-api-key"),
|
||||
app.test_request_context(
|
||||
method="POST",
|
||||
json={},
|
||||
headers={"X-Inner-Api-Key": "inner-api-key"},
|
||||
),
|
||||
pytest.raises(UnprocessableEntity),
|
||||
):
|
||||
api_instance.post(workspace_id="ws-123")
|
||||
|
||||
def test_bad_key_is_rejected_before_body_validation(self, app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr("controllers.console.wraps._is_setup_completed", lambda: True)
|
||||
with (
|
||||
config_overrides_context(INNER_API=True, INNER_API_KEY="inner-api-key"),
|
||||
app.test_request_context(method="POST", json={}, headers={"X-Inner-Api-Key": "wrong-key"}),
|
||||
pytest.raises(InnerApiUnauthorizedError),
|
||||
):
|
||||
EnterpriseAppDSLImport().post(workspace_id="ws-123")
|
||||
|
||||
def test_admitted_import_passes_stable_metadata(
|
||||
self,
|
||||
app: Flask,
|
||||
app_query_services: ControllerTestServices,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
calls: list[dict[str, object]] = []
|
||||
|
||||
def import_as_creator(**kwargs: object) -> Import:
|
||||
calls.append(kwargs)
|
||||
return Import(id="import-id", status=ImportStatus.COMPLETED)
|
||||
|
||||
monkeypatch.setattr(app_query_services.apps.imports, "import_as_creator", import_as_creator)
|
||||
monkeypatch.setattr("controllers.console.wraps._is_setup_completed", lambda: True)
|
||||
monkeypatch.setattr("controllers.inner_api.app.dsl.get_request_id", lambda: "request-id")
|
||||
monkeypatch.setattr("controllers.inner_api.app.dsl.get_trace_id", lambda: "trace-id")
|
||||
with (
|
||||
config_overrides_context(INNER_API=True, INNER_API_KEY="inner-api-key"),
|
||||
app.test_request_context(
|
||||
method="POST",
|
||||
json={"yaml_content": "app: {}", "creator_email": "creator@example.com"},
|
||||
headers={"X-Inner-Api-Key": "inner-api-key"},
|
||||
),
|
||||
):
|
||||
body, status = EnterpriseAppDSLImport().post(workspace_id="ws-123")
|
||||
assert status == 200
|
||||
assert body["status"] == ImportStatus.COMPLETED
|
||||
assert calls[0]["request_id"] == "request-id"
|
||||
assert calls[0]["trace_id"] == "trace-id"
|
||||
assert calls[0]["workspace_id"] == "ws-123"
|
||||
assert calls[0]["creator_email"] == "creator@example.com"
|
||||
@@ -0,0 +1,499 @@
|
||||
"""Tests for the AppDeploy file grant minting endpoint."""
|
||||
|
||||
import inspect
|
||||
import os
|
||||
import time
|
||||
from collections.abc import Callable, Iterator
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
import jwt
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from controllers.files.wraps import GrantedFileNotFoundError
|
||||
from controllers.inner_api.app.file_grants import (
|
||||
EnterpriseFileGrantApi,
|
||||
GrantAppNotFoundError,
|
||||
GrantTtlTooLongError,
|
||||
InvalidGrantRequestError,
|
||||
InvalidSubjectError,
|
||||
)
|
||||
from extensions.ext_application_services import _build_file_grant_service
|
||||
from extensions.storage.storage_type import StorageType
|
||||
from libs.datetime_utils import naive_utc_now
|
||||
from models.enums import CreatorUserRole, EndUserType
|
||||
from models.model import App, AppMode, EndUser, UploadFile
|
||||
from models.tools import ToolFile
|
||||
from repositories.app_scoped_end_user_repository import AppScopedEndUserRepo
|
||||
from services.app_scoped_end_user_service import AppScopedEndUserService
|
||||
from services.file_grant_gateways import FILE_GRANT_AUDIENCE
|
||||
from services.file_grant_service import (
|
||||
MAX_RUN_GRANT_TTL_SECONDS,
|
||||
MAX_SESSION_GRANT_TTL_SECONDS,
|
||||
MAX_WORKFLOW_EXECUTION_SECONDS,
|
||||
RUN_GRANT_EXPIRY_GRACE_SECONDS,
|
||||
FileGrantService,
|
||||
)
|
||||
|
||||
CONTROLLER_MODULE = "controllers.inner_api.app.file_grants"
|
||||
|
||||
SECRET_KEY = "file-grant-test-secret-long-enough-for-hs256"
|
||||
TENANT_ID = "11111111-1111-4111-8111-111111111111"
|
||||
APP_ID = "22222222-2222-4222-8222-222222222222"
|
||||
SUBJECT = "adp1.dGVzdC1zdWJqZWN0"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def granted_config(config_overrides: Callable[..., None]) -> None:
|
||||
config_overrides(
|
||||
SECRET_KEY=SECRET_KEY,
|
||||
FILES_URL="https://files.example.com",
|
||||
INTERNAL_FILES_URL="http://dify-api.dify.svc:5001",
|
||||
FILES_ACCESS_TIMEOUT=300,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def seeded_app(sqlite_session: Session) -> App:
|
||||
app_model = App(
|
||||
id=APP_ID,
|
||||
tenant_id=TENANT_ID,
|
||||
name="deployed app",
|
||||
mode=AppMode.WORKFLOW,
|
||||
enable_site=True,
|
||||
enable_api=True,
|
||||
)
|
||||
sqlite_session.add(app_model)
|
||||
sqlite_session.commit()
|
||||
return app_model
|
||||
|
||||
|
||||
def _mint(app: Flask, payload: dict[str, object]) -> dict[str, object]:
|
||||
handler = EnterpriseFileGrantApi()
|
||||
with app.test_request_context("/", method="POST", json=payload):
|
||||
with patch(f"{CONTROLLER_MODULE}.inner_api_ns") as mock_ns:
|
||||
mock_ns.payload = payload
|
||||
return inspect.unwrap(EnterpriseFileGrantApi.post)(handler)
|
||||
|
||||
|
||||
def _subject_of(response: dict[str, object]) -> str:
|
||||
grant = response["grant"]
|
||||
assert isinstance(grant, str)
|
||||
return str(jwt.decode(grant, SECRET_KEY, algorithms=["HS256"], audience=FILE_GRANT_AUDIENCE)["sub"])
|
||||
|
||||
|
||||
def _payload(**overrides: object) -> dict[str, object]:
|
||||
return {
|
||||
"tenant_id": TENANT_ID,
|
||||
"app_id": APP_ID,
|
||||
"subject": SUBJECT,
|
||||
"is_anonymous": True,
|
||||
"scopes": ["upload"],
|
||||
"ttl_seconds": 600,
|
||||
} | overrides
|
||||
|
||||
|
||||
def _persist_upload_file(session: Session, *, owner_id: str, tenant_id: str = TENANT_ID) -> UploadFile:
|
||||
upload_file = UploadFile(
|
||||
tenant_id=tenant_id,
|
||||
storage_type=StorageType.OPENDAL,
|
||||
key="upload_files/report.pdf",
|
||||
name="report.pdf",
|
||||
size=2048,
|
||||
extension="pdf",
|
||||
mime_type="application/pdf",
|
||||
created_by=owner_id,
|
||||
created_by_role=CreatorUserRole.END_USER,
|
||||
created_at=naive_utc_now(),
|
||||
used=False,
|
||||
)
|
||||
session.add(upload_file)
|
||||
session.commit()
|
||||
return upload_file
|
||||
|
||||
|
||||
def _persist_tool_file(session: Session, *, owner_id: str, tenant_id: str = TENANT_ID) -> ToolFile:
|
||||
tool_file = ToolFile(
|
||||
user_id=owner_id,
|
||||
tenant_id=tenant_id,
|
||||
conversation_id=None,
|
||||
file_key="tools/chart.png",
|
||||
mimetype="image/png",
|
||||
name="chart.png",
|
||||
size=64,
|
||||
)
|
||||
session.add(tool_file)
|
||||
session.commit()
|
||||
return tool_file
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sqlite_db(sqlite_engine: Engine, granted_config: None) -> Iterator[None]:
|
||||
del granted_config
|
||||
service = _build_file_grant_service(database_client=sessionmaker(bind=sqlite_engine, expire_on_commit=False))
|
||||
services = SimpleNamespace(file_grants=service)
|
||||
with patch(f"{CONTROLLER_MODULE}.application_services", return_value=services):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_creates_exactly_one_end_user_and_reuses_it(app: Flask, sqlite_session: Session) -> None:
|
||||
first = _mint(app, _payload())
|
||||
second = _mint(app, _payload(ttl_seconds=900))
|
||||
|
||||
end_users = list(sqlite_session.scalars(select(EndUser).where(EndUser.tenant_id == TENANT_ID)).all())
|
||||
assert len(end_users) == 1
|
||||
assert end_users[0].type == EndUserType.APP_DEPLOY
|
||||
assert end_users[0].session_id == FileGrantService.session_id_for_subject(SUBJECT)
|
||||
assert end_users[0].external_user_id == SUBJECT
|
||||
|
||||
first_grant = first["grant"]
|
||||
second_grant = second["grant"]
|
||||
assert isinstance(first_grant, str)
|
||||
assert isinstance(second_grant, str)
|
||||
first_claims = jwt.decode(first_grant, SECRET_KEY, algorithms=["HS256"], audience=FILE_GRANT_AUDIENCE)
|
||||
second_claims = jwt.decode(second_grant, SECRET_KEY, algorithms=["HS256"], audience=FILE_GRANT_AUDIENCE)
|
||||
assert first_claims["sub"] == second_claims["sub"] == end_users[0].id
|
||||
assert first_claims["tenant_id"] == TENANT_ID
|
||||
assert first_claims["app_id"] == APP_ID
|
||||
assert first_claims["scopes"] == ["upload"]
|
||||
first_expires_at = first["expires_at"]
|
||||
second_expires_at = second["expires_at"]
|
||||
assert isinstance(first_expires_at, int)
|
||||
assert isinstance(second_expires_at, int)
|
||||
assert second_expires_at > first_expires_at
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_returns_dify_upload_limits(app: Flask, config_overrides: Callable[..., None]) -> None:
|
||||
config_overrides(
|
||||
UPLOAD_FILE_SIZE_LIMIT=15,
|
||||
UPLOAD_IMAGE_FILE_SIZE_LIMIT=10,
|
||||
UPLOAD_AUDIO_FILE_SIZE_LIMIT=50,
|
||||
UPLOAD_VIDEO_FILE_SIZE_LIMIT=100,
|
||||
WORKFLOW_FILE_UPLOAD_LIMIT=10,
|
||||
UPLOAD_FILE_BATCH_LIMIT=5,
|
||||
)
|
||||
|
||||
response = _mint(app, _payload())
|
||||
|
||||
assert response["limits"] == {
|
||||
"file_size_limit": 15,
|
||||
"image_file_size_limit": 10,
|
||||
"audio_file_size_limit": 50,
|
||||
"video_file_size_limit": 100,
|
||||
"workflow_file_upload_limit": 10,
|
||||
"batch_count_limit": 5,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_rejects_ttl_over_the_cap_before_touching_identity(app: Flask, sqlite_session: Session) -> None:
|
||||
with pytest.raises(GrantTtlTooLongError):
|
||||
_mint(app, _payload(ttl_seconds=MAX_SESSION_GRANT_TTL_SECONDS + 1))
|
||||
|
||||
assert sqlite_session.scalars(select(EndUser)).all() == []
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_accepts_a_session_ttl_exactly_at_the_cap(app: Flask) -> None:
|
||||
before = int(time.time())
|
||||
|
||||
response = _mint(app, _payload(ttl_seconds=MAX_SESSION_GRANT_TTL_SECONDS))
|
||||
|
||||
expires_at = response["expires_at"]
|
||||
assert isinstance(expires_at, int)
|
||||
assert MAX_SESSION_GRANT_TTL_SECONDS <= expires_at - before <= MAX_SESSION_GRANT_TTL_SECONDS + 5
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_accepts_a_run_ttl_until_deadline_plus_grace(app: Flask) -> None:
|
||||
now = int(time.time())
|
||||
run_duration = MAX_RUN_GRANT_TTL_SECONDS - RUN_GRANT_EXPIRY_GRACE_SECONDS
|
||||
|
||||
response = _mint(
|
||||
app,
|
||||
_payload(
|
||||
scopes=["resolve", "produce"],
|
||||
ttl_seconds=MAX_RUN_GRANT_TTL_SECONDS,
|
||||
run_deadline=now + run_duration,
|
||||
),
|
||||
)
|
||||
|
||||
expires_at = response["expires_at"]
|
||||
assert isinstance(expires_at, int)
|
||||
assert MAX_RUN_GRANT_TTL_SECONDS <= expires_at - now <= MAX_RUN_GRANT_TTL_SECONDS + 5
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_rejects_a_run_ttl_past_deadline_grace(app: Flask) -> None:
|
||||
now = int(time.time())
|
||||
|
||||
with pytest.raises(GrantTtlTooLongError):
|
||||
_mint(
|
||||
app,
|
||||
_payload(
|
||||
scopes=["resolve", "produce"],
|
||||
ttl_seconds=MAX_RUN_GRANT_TTL_SECONDS + 1,
|
||||
run_deadline=now + MAX_WORKFLOW_EXECUTION_SECONDS,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_rejects_an_expired_run_deadline(app: Flask) -> None:
|
||||
with pytest.raises(InvalidGrantRequestError):
|
||||
_mint(
|
||||
app,
|
||||
_payload(scopes=["resolve", "produce"], ttl_seconds=1, run_deadline=int(time.time()) - 1),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_rejects_a_run_deadline_without_produce_scope(app: Flask) -> None:
|
||||
with pytest.raises(InvalidGrantRequestError):
|
||||
_mint(app, _payload(run_deadline=int(time.time()) + 60))
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_rejects_a_run_deadline_beyond_the_workflow_limit(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
now = 1_700_000_000
|
||||
# Keep the one-second boundary fixed between payload construction and validation.
|
||||
monkeypatch.setattr("extensions.ext_application_services.time", SimpleNamespace(time=lambda: now))
|
||||
|
||||
with pytest.raises(InvalidGrantRequestError):
|
||||
_mint(
|
||||
app,
|
||||
_payload(
|
||||
scopes=["resolve", "produce"],
|
||||
run_deadline=now + MAX_WORKFLOW_EXECUTION_SECONDS + 1,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_caps_a_run_grant_at_deadline_plus_grace(app: Flask) -> None:
|
||||
now = int(time.time())
|
||||
run_deadline = now + 60
|
||||
|
||||
response = _mint(
|
||||
app,
|
||||
_payload(
|
||||
scopes=["resolve", "produce"],
|
||||
ttl_seconds=1200,
|
||||
run_deadline=run_deadline,
|
||||
),
|
||||
)
|
||||
|
||||
expires_at = response["expires_at"]
|
||||
assert isinstance(expires_at, int)
|
||||
assert (
|
||||
run_deadline + RUN_GRANT_EXPIRY_GRACE_SECONDS
|
||||
<= expires_at
|
||||
<= (run_deadline + RUN_GRANT_EXPIRY_GRACE_SECONDS + 1)
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
@pytest.mark.parametrize("ttl_seconds", [0, -1])
|
||||
def test_mint_rejects_a_non_positive_ttl(app: Flask, ttl_seconds: int) -> None:
|
||||
with pytest.raises(InvalidGrantRequestError):
|
||||
_mint(app, _payload(ttl_seconds=ttl_seconds))
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
@pytest.mark.parametrize("subject", ["", " ", "\t\n", "adp1.with\x00nul", "\x00"])
|
||||
def test_mint_rejects_an_unusable_subject(app: Flask, subject: str, sqlite_session: Session) -> None:
|
||||
"""A NUL would reach ``external_user_id`` verbatim and blow up on PostgreSQL."""
|
||||
|
||||
with pytest.raises(InvalidSubjectError):
|
||||
_mint(app, _payload(subject=subject))
|
||||
|
||||
assert sqlite_session.scalars(select(EndUser)).all() == []
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_folds_an_oversized_subject_into_one_identity(app: Flask, sqlite_session: Session) -> None:
|
||||
"""``external_user_id`` truncates at 255, so ``session_id`` is what keeps them apart."""
|
||||
|
||||
long_subject = "adp1." + "s" * 4000
|
||||
sibling = long_subject[:-1] + "t"
|
||||
|
||||
first = _mint(app, _payload(subject=long_subject))
|
||||
again = _mint(app, _payload(subject=long_subject))
|
||||
other = _mint(app, _payload(subject=sibling))
|
||||
|
||||
end_users = sqlite_session.scalars(select(EndUser).order_by(EndUser.created_at)).all()
|
||||
assert len(end_users) == 2
|
||||
assert all(
|
||||
end_user.external_user_id is not None and len(end_user.external_user_id) == 255 for end_user in end_users
|
||||
)
|
||||
assert _subject_of(first) == _subject_of(again) != _subject_of(other)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "sqlite_db")
|
||||
def test_mint_rejects_unknown_app(app: Flask) -> None:
|
||||
with pytest.raises(GrantAppNotFoundError):
|
||||
_mint(app, _payload())
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_returns_strict_metadata_without_urls(app: Flask, sqlite_session: Session) -> None:
|
||||
owner_id = _subject_of(_mint(app, _payload()))
|
||||
upload_file = _persist_upload_file(sqlite_session, owner_id=owner_id)
|
||||
tool_file = _persist_tool_file(sqlite_session, owner_id=owner_id)
|
||||
|
||||
response = _mint(
|
||||
app,
|
||||
_payload(file_ids=[{"id": upload_file.id, "kind": "upload"}, {"id": tool_file.id, "kind": "tool"}]),
|
||||
)
|
||||
|
||||
assert response["files"] == [
|
||||
{
|
||||
"id": upload_file.id,
|
||||
"kind": "upload",
|
||||
"name": "report.pdf",
|
||||
"size": 2048,
|
||||
"extension": "pdf",
|
||||
"mime_type": "application/pdf",
|
||||
},
|
||||
{
|
||||
"id": tool_file.id,
|
||||
"kind": "tool",
|
||||
"name": "chart.png",
|
||||
"size": 64,
|
||||
"extension": "png",
|
||||
"mime_type": "image/png",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_fails_the_whole_strict_batch_on_one_miss(app: Flask, sqlite_session: Session) -> None:
|
||||
owner_id = _subject_of(_mint(app, _payload()))
|
||||
upload_file = _persist_upload_file(sqlite_session, owner_id=owner_id)
|
||||
|
||||
with pytest.raises(GrantedFileNotFoundError):
|
||||
_mint(
|
||||
app,
|
||||
_payload(
|
||||
file_ids=[
|
||||
{"id": upload_file.id, "kind": "upload"},
|
||||
{"id": "33333333-3333-4333-8333-333333333333", "kind": "upload"},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_hides_files_owned_by_another_subject(app: Flask, sqlite_session: Session) -> None:
|
||||
other_owner = _subject_of(_mint(app, _payload(subject="adp1.other")))
|
||||
foreign_file = _persist_upload_file(sqlite_session, owner_id=other_owner)
|
||||
|
||||
with pytest.raises(GrantedFileNotFoundError):
|
||||
_mint(app, _payload(file_ids=[{"id": foreign_file.id, "kind": "upload"}]))
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db")
|
||||
def test_mint_reports_optional_files_item_by_item(app: Flask, sqlite_session: Session) -> None:
|
||||
owner_id = _subject_of(_mint(app, _payload()))
|
||||
upload_file = _persist_upload_file(sqlite_session, owner_id=owner_id)
|
||||
missing_id = "44444444-4444-4444-8444-444444444444"
|
||||
|
||||
response = _mint(
|
||||
app,
|
||||
_payload(
|
||||
optional_file_ids=[
|
||||
{"id": upload_file.id, "kind": "upload"},
|
||||
{"id": missing_id, "kind": "tool"},
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
optional_files = response["optional_files"]
|
||||
assert isinstance(optional_files, list)
|
||||
present, absent = optional_files
|
||||
assert present["ok"] is True
|
||||
assert present["name"] == "report.pdf"
|
||||
assert present["url"].startswith(f"https://files.example.com/files/appdeploy/{upload_file.id}/content?token=")
|
||||
assert present["internal_url"].startswith(
|
||||
f"http://dify-api.dify.svc:5001/files/appdeploy/{upload_file.id}/content?token="
|
||||
)
|
||||
assert absent == {
|
||||
"id": missing_id,
|
||||
"ok": False,
|
||||
"kind": None,
|
||||
"name": None,
|
||||
"size": None,
|
||||
"extension": None,
|
||||
"mime_type": None,
|
||||
"url": None,
|
||||
"internal_url": None,
|
||||
"error": "not_found",
|
||||
}
|
||||
assert response["files"] == []
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("seeded_app")
|
||||
def test_end_user_service_never_retypes_an_app_deploy_row(
|
||||
sqlite_session: Session,
|
||||
sqlite_session_factory: sessionmaker[Session],
|
||||
) -> None:
|
||||
"""Retyping would hide the row from the grant read and strand its files."""
|
||||
|
||||
subject = "subject-that-also-reaches-the-service-api"
|
||||
session_id = FileGrantService.session_id_for_subject(subject)
|
||||
owner = EndUser(
|
||||
tenant_id=TENANT_ID,
|
||||
app_id=APP_ID,
|
||||
type=EndUserType.APP_DEPLOY,
|
||||
_is_anonymous=True,
|
||||
session_id=session_id,
|
||||
external_user_id=session_id,
|
||||
)
|
||||
sqlite_session.add(owner)
|
||||
sqlite_session.commit()
|
||||
owner_id = owner.id
|
||||
|
||||
AppScopedEndUserService(
|
||||
end_users=AppScopedEndUserRepo(session_factory=sqlite_session_factory)
|
||||
).get_or_create_end_user_by_type(EndUserType.SERVICE_API, TENANT_ID, APP_ID, session_id)
|
||||
|
||||
sqlite_session.expire_all()
|
||||
persisted_owner = sqlite_session.get(EndUser, owner_id)
|
||||
assert persisted_owner is not None
|
||||
assert persisted_owner.type == EndUserType.APP_DEPLOY
|
||||
|
||||
|
||||
SKIPPED_DIRECTORY_NAMES = frozenset({".git", ".venv", "__pycache__", "migrations", "node_modules", "tests"})
|
||||
|
||||
|
||||
def test_app_deploy_end_users_have_exactly_one_writer() -> None:
|
||||
"""``end_users`` has no unique constraint, so a second writer would fork identities.
|
||||
|
||||
``app_scoped_end_user_service`` names the type only to exclude it from the legacy retype;
|
||||
the behavioural guard above is what holds that exclusion in place.
|
||||
"""
|
||||
|
||||
api_root = Path(__file__).resolve().parents[5]
|
||||
assert api_root.name == "api"
|
||||
|
||||
referencing_modules: set[str] = set()
|
||||
for directory, subdirectories, filenames in os.walk(api_root):
|
||||
subdirectories[:] = [name for name in subdirectories if name not in SKIPPED_DIRECTORY_NAMES]
|
||||
for filename in filenames:
|
||||
if not filename.endswith(".py"):
|
||||
continue
|
||||
path = Path(directory) / filename
|
||||
if "EndUserType.APP_DEPLOY" in path.read_text(encoding="utf-8"):
|
||||
referencing_modules.add(path.relative_to(api_root).as_posix())
|
||||
|
||||
assert referencing_modules == {
|
||||
"repositories/file_grant_repository.py",
|
||||
"services/app_scoped_end_user_service.py",
|
||||
}
|
||||
+242
@@ -0,0 +1,242 @@
|
||||
"""Unit tests for the plugin inner knowledge retrieval controller."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from contextlib import contextmanager
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
|
||||
from controllers.inner_api import bp as inner_api_bp
|
||||
from core.workflow.nodes.knowledge_retrieval.exc import RateLimitExceededError
|
||||
from core.workflow.nodes.knowledge_retrieval.retrieval import Source, SourceMetadata
|
||||
from services.entities.knowledge_retrieval_inner import InnerKnowledgeRetrieveResponse, InnerKnowledgeRetrieveUsage
|
||||
from services.errors.knowledge_retrieval import (
|
||||
ExternalKnowledgeRetrievalError,
|
||||
InnerKnowledgeRetrieveAppNotFoundError,
|
||||
InnerKnowledgeRetrieveDatasetTenantMismatchError,
|
||||
)
|
||||
from tests.helpers.unit.config_override import config_overrides_context
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def inner_api_app() -> Flask:
|
||||
app = Flask(__name__)
|
||||
app.config["TESTING"] = True
|
||||
app.register_blueprint(inner_api_bp)
|
||||
return app
|
||||
|
||||
|
||||
def _headers(api_key: str | None = "inner-key") -> dict[str, str]:
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if api_key is not None:
|
||||
headers["X-Inner-Api-Key"] = api_key
|
||||
return headers
|
||||
|
||||
|
||||
def _payload() -> dict[str, object]:
|
||||
return {
|
||||
"caller": {
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "user-1",
|
||||
"app_id": "app-1",
|
||||
"user_from": "account",
|
||||
"invoke_from": "workflow",
|
||||
},
|
||||
"dataset_ids": ["dataset-1"],
|
||||
"query": "reset password",
|
||||
"retrieval": {
|
||||
"mode": "multiple",
|
||||
"top_k": 4,
|
||||
},
|
||||
"metadata_filtering": {
|
||||
"mode": "disabled",
|
||||
},
|
||||
"attachment_ids": [],
|
||||
}
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _plugin_inner_auth() -> Generator[None]:
|
||||
with (
|
||||
config_overrides_context(PLUGIN_DAEMON_KEY="plugin-daemon-key", INNER_API_KEY_FOR_PLUGIN="inner-key"),
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
class TestInnerKnowledgeRetrieveApi:
|
||||
def test_post_returns_404_when_api_key_missing(self, inner_api_app: Flask):
|
||||
with _plugin_inner_auth():
|
||||
response = inner_api_app.test_client().post(
|
||||
"/inner/api/knowledge/retrieve",
|
||||
json=_payload(),
|
||||
headers=_headers(api_key=None),
|
||||
)
|
||||
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_post_returns_404_when_api_key_invalid(self, inner_api_app: Flask):
|
||||
with _plugin_inner_auth():
|
||||
response = inner_api_app.test_client().post(
|
||||
"/inner/api/knowledge/retrieve",
|
||||
json=_payload(),
|
||||
headers=_headers(api_key="wrong-key"),
|
||||
)
|
||||
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_post_returns_400_for_invalid_body(self, inner_api_app: Flask):
|
||||
with _plugin_inner_auth():
|
||||
response = inner_api_app.test_client().post(
|
||||
"/inner/api/knowledge/retrieve",
|
||||
json={"caller": {"tenant_id": "tenant-1"}},
|
||||
headers=_headers(),
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.get_json()["code"] == "invalid_request"
|
||||
|
||||
@patch("controllers.inner_api.knowledge.retrieval.InnerKnowledgeRetrievalService.retrieve")
|
||||
def test_post_returns_404_for_service_not_found_error(self, mock_retrieve, inner_api_app: Flask):
|
||||
mock_retrieve.side_effect = InnerKnowledgeRetrieveAppNotFoundError("app missing")
|
||||
|
||||
with _plugin_inner_auth():
|
||||
response = inner_api_app.test_client().post(
|
||||
"/inner/api/knowledge/retrieve",
|
||||
json=_payload(),
|
||||
headers=_headers(),
|
||||
)
|
||||
|
||||
assert response.status_code == 404
|
||||
assert response.get_json()["code"] == "app_not_found"
|
||||
|
||||
@patch("controllers.inner_api.knowledge.retrieval.InnerKnowledgeRetrievalService.retrieve")
|
||||
def test_post_returns_403_for_service_forbidden_error(self, mock_retrieve, inner_api_app: Flask):
|
||||
mock_retrieve.side_effect = InnerKnowledgeRetrieveDatasetTenantMismatchError("wrong tenant")
|
||||
|
||||
with _plugin_inner_auth():
|
||||
response = inner_api_app.test_client().post(
|
||||
"/inner/api/knowledge/retrieve",
|
||||
json=_payload(),
|
||||
headers=_headers(),
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
assert response.get_json()["code"] == "dataset_tenant_mismatch"
|
||||
|
||||
@patch("controllers.inner_api.knowledge.retrieval.InnerKnowledgeRetrievalService.retrieve")
|
||||
def test_post_returns_422_for_retrieval_config_value_error(self, mock_retrieve, inner_api_app: Flask):
|
||||
mock_retrieve.side_effect = ValueError("invalid reranking config")
|
||||
|
||||
with _plugin_inner_auth():
|
||||
response = inner_api_app.test_client().post(
|
||||
"/inner/api/knowledge/retrieve",
|
||||
json=_payload(),
|
||||
headers=_headers(),
|
||||
)
|
||||
|
||||
assert response.status_code == 422
|
||||
assert response.get_json()["code"] == "retrieval_config_invalid"
|
||||
|
||||
@patch("controllers.inner_api.knowledge.retrieval.InnerKnowledgeRetrievalService.retrieve")
|
||||
def test_post_returns_429_for_rate_limit_error(self, mock_retrieve, inner_api_app: Flask):
|
||||
mock_retrieve.side_effect = RateLimitExceededError("knowledge rate limited")
|
||||
|
||||
with _plugin_inner_auth():
|
||||
response = inner_api_app.test_client().post(
|
||||
"/inner/api/knowledge/retrieve",
|
||||
json=_payload(),
|
||||
headers=_headers(),
|
||||
)
|
||||
|
||||
assert response.status_code == 429
|
||||
assert response.get_json()["code"] == "knowledge_rate_limited"
|
||||
|
||||
def test_post_returns_400_for_manual_metadata_without_conditions(self, inner_api_app: Flask):
|
||||
payload = _payload()
|
||||
payload["metadata_filtering"] = {"mode": "manual"}
|
||||
|
||||
with _plugin_inner_auth():
|
||||
response = inner_api_app.test_client().post(
|
||||
"/inner/api/knowledge/retrieve",
|
||||
json=payload,
|
||||
headers=_headers(),
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.get_json()["code"] == "invalid_request"
|
||||
|
||||
def test_post_returns_400_for_automatic_metadata_without_model_config(self, inner_api_app: Flask):
|
||||
payload = _payload()
|
||||
payload["metadata_filtering"] = {"mode": "automatic"}
|
||||
|
||||
with _plugin_inner_auth():
|
||||
response = inner_api_app.test_client().post(
|
||||
"/inner/api/knowledge/retrieve",
|
||||
json=payload,
|
||||
headers=_headers(),
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.get_json()["code"] == "invalid_request"
|
||||
|
||||
@patch("controllers.inner_api.knowledge.retrieval.InnerKnowledgeRetrievalService.retrieve")
|
||||
def test_post_returns_502_for_external_knowledge_failure(self, mock_retrieve, inner_api_app: Flask):
|
||||
mock_retrieve.side_effect = ExternalKnowledgeRetrievalError("upstream failed")
|
||||
|
||||
with _plugin_inner_auth():
|
||||
response = inner_api_app.test_client().post(
|
||||
"/inner/api/knowledge/retrieve",
|
||||
json=_payload(),
|
||||
headers=_headers(),
|
||||
)
|
||||
|
||||
assert response.status_code == 502
|
||||
assert response.get_json()["code"] == "external_knowledge_failed"
|
||||
|
||||
@patch("controllers.inner_api.knowledge.retrieval.InnerKnowledgeRetrievalService.retrieve")
|
||||
def test_post_returns_service_response(self, mock_retrieve, inner_api_app: Flask):
|
||||
mock_retrieve.return_value = InnerKnowledgeRetrieveResponse(
|
||||
results=[
|
||||
Source(
|
||||
metadata=SourceMetadata(
|
||||
dataset_id="dataset-1",
|
||||
dataset_name="Docs",
|
||||
document_id="document-1",
|
||||
document_name="FAQ.md",
|
||||
data_source_type="upload_file",
|
||||
),
|
||||
title="FAQ.md",
|
||||
files=[],
|
||||
content="Reset your password from settings.",
|
||||
summary=None,
|
||||
)
|
||||
],
|
||||
usage=InnerKnowledgeRetrieveUsage(
|
||||
prompt_tokens=0,
|
||||
completion_tokens=0,
|
||||
total_tokens=0,
|
||||
prompt_unit_price="0",
|
||||
completion_unit_price="0",
|
||||
prompt_price_unit="0.001",
|
||||
completion_price_unit="0.001",
|
||||
prompt_price="0",
|
||||
completion_price="0",
|
||||
total_price="0",
|
||||
currency="USD",
|
||||
latency=0,
|
||||
),
|
||||
)
|
||||
|
||||
with _plugin_inner_auth():
|
||||
response = inner_api_app.test_client().post(
|
||||
"/inner/api/knowledge/retrieve",
|
||||
json=_payload(),
|
||||
headers=_headers(),
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.get_json()
|
||||
assert data["results"][0]["metadata"]["_source"] == "knowledge"
|
||||
assert data["results"][0]["title"] == "FAQ.md"
|
||||
assert data["usage"]["total_tokens"] == 0
|
||||
@@ -0,0 +1 @@
|
||||
"""Unit tests for KnowledgeFS inner API controllers."""
|
||||
@@ -0,0 +1,16 @@
|
||||
"""Enable KnowledgeFS for tests of its trusted inner API routes."""
|
||||
|
||||
from collections.abc import Callable
|
||||
|
||||
import pytest
|
||||
|
||||
from enums import DeploymentEdition
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _enabled_knowledge_fs(config_overrides: Callable[..., None]) -> None:
|
||||
config_overrides(
|
||||
DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY,
|
||||
KNOWLEDGE_FS_ENABLED=True,
|
||||
KNOWLEDGE_FS_COMMUNITY_DEV_ENABLED=True,
|
||||
)
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
import inspect
|
||||
from collections.abc import Callable
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from werkzeug.exceptions import BadRequest, RequestEntityTooLarge, ServiceUnavailable
|
||||
|
||||
from controllers.inner_api.knowledge_fs.background import KnowledgeFSBackgroundHealthApi, KnowledgeFSBackgroundJobApi
|
||||
from services.knowledge_fs.background_contract import BackgroundJobPayload
|
||||
|
||||
JOB = {
|
||||
"id": "018f0d60-7a49-7cc2-9c1b-5b36f18f8a01",
|
||||
"type": "document.compile",
|
||||
"payload": {"attemptId": "018f0d60-7a49-7cc2-9c1b-5b36f18f8a02"},
|
||||
}
|
||||
|
||||
|
||||
def test_publication_returns_202_only_after_broker_accepts(app: Flask) -> None:
|
||||
handler = KnowledgeFSBackgroundJobApi()
|
||||
with (
|
||||
app.test_request_context(json=JOB),
|
||||
patch("controllers.inner_api.knowledge_fs.background.publish_background_job") as publish,
|
||||
):
|
||||
response = inspect.unwrap(handler.post)(handler)
|
||||
assert response.status_code == 202
|
||||
assert response.get_data() == b""
|
||||
publish.assert_called_once()
|
||||
|
||||
|
||||
def test_priority_is_documented_and_consumed_only_by_the_publisher(app: Flask) -> None:
|
||||
schema = BackgroundJobPayload.model_json_schema()
|
||||
assert schema["properties"]["priority"]["enum"] == ["normal", "high"]
|
||||
assert schema["properties"]["priority"]["default"] == "normal"
|
||||
handler = KnowledgeFSBackgroundJobApi()
|
||||
with (
|
||||
app.test_request_context(json={**JOB, "priority": "high"}),
|
||||
patch("controllers.inner_api.knowledge_fs.background.publish_background_job") as publish,
|
||||
):
|
||||
assert inspect.unwrap(handler.post)(handler).status_code == 202
|
||||
job = publish.call_args.args[0]
|
||||
assert job.priority == "high"
|
||||
assert "priority" not in job.model_dump()
|
||||
|
||||
|
||||
def test_publication_failure_is_retryable_and_does_not_report_success(app: Flask) -> None:
|
||||
handler = KnowledgeFSBackgroundJobApi()
|
||||
with (
|
||||
app.test_request_context(json=JOB),
|
||||
patch(
|
||||
"controllers.inner_api.knowledge_fs.background.publish_background_job",
|
||||
side_effect=RuntimeError("broker unavailable"),
|
||||
),
|
||||
):
|
||||
with pytest.raises(ServiceUnavailable):
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
|
||||
|
||||
def test_unknown_fields_never_reach_the_broker(app: Flask) -> None:
|
||||
handler = KnowledgeFSBackgroundJobApi()
|
||||
with (
|
||||
app.test_request_context(json={**JOB, "command": "anything"}),
|
||||
patch("controllers.inner_api.knowledge_fs.background.publish_background_job") as publish,
|
||||
):
|
||||
with pytest.raises(BadRequest):
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
publish.assert_not_called()
|
||||
|
||||
|
||||
def test_publisher_readiness_is_closed_before_rollout(config_overrides: Callable[..., None]) -> None:
|
||||
handler = KnowledgeFSBackgroundHealthApi()
|
||||
config_overrides(KNOWLEDGE_FS_BACKGROUND_WORKER_ENABLED=False)
|
||||
with pytest.raises(ServiceUnavailable):
|
||||
inspect.unwrap(handler.get)(handler)
|
||||
config_overrides(KNOWLEDGE_FS_BACKGROUND_WORKER_ENABLED=True)
|
||||
with patch("controllers.inner_api.knowledge_fs.background.check_background_publisher_ready"):
|
||||
assert inspect.unwrap(handler.get)(handler).status_code == 204
|
||||
|
||||
|
||||
def test_oversized_locator_is_rejected_before_json_parsing(app: Flask) -> None:
|
||||
handler = KnowledgeFSBackgroundJobApi()
|
||||
with app.test_request_context(json={"body": "x" * 17000}):
|
||||
with pytest.raises(RequestEntityTooLarge):
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
|
||||
|
||||
def test_broker_outage_is_not_reported_as_ready() -> None:
|
||||
handler = KnowledgeFSBackgroundHealthApi()
|
||||
with patch(
|
||||
"controllers.inner_api.knowledge_fs.background.check_background_publisher_ready", side_effect=OSError("offline")
|
||||
):
|
||||
with pytest.raises(ServiceUnavailable):
|
||||
inspect.unwrap(handler.get)(handler)
|
||||
+300
@@ -0,0 +1,300 @@
|
||||
"""Unit tests for the KnowledgeFS unified object-storage inner API."""
|
||||
|
||||
import inspect
|
||||
import json
|
||||
from base64 import urlsafe_b64encode
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask, Response
|
||||
|
||||
from controllers.inner_api.knowledge_fs.storage import (
|
||||
KnowledgeFSObjectApi,
|
||||
KnowledgeFSObjectHealthApi,
|
||||
KnowledgeFSObjectListApi,
|
||||
KnowledgeFSObjectMetadataApi,
|
||||
KnowledgeFSObjectStorageHttpError,
|
||||
KnowledgeFSQueryImageApi,
|
||||
KnowledgeFSRemoteImageApi,
|
||||
)
|
||||
from services.knowledge_fs.object_storage import (
|
||||
KnowledgeFSObjectList,
|
||||
KnowledgeFSObjectMetadata,
|
||||
KnowledgeFSObjectStorageChecksumError,
|
||||
KnowledgeFSObjectStorageUnavailableError,
|
||||
)
|
||||
from services.knowledge_fs.query_images import KnowledgeFSResolvedQueryImage
|
||||
from services.knowledge_fs.remote_images import KnowledgeFSRemoteImageError, KnowledgeFSResolvedRemoteImage
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def object_metadata() -> KnowledgeFSObjectMetadata:
|
||||
return KnowledgeFSObjectMetadata(
|
||||
checksum_sha256_base64="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
|
||||
content_type="text/plain",
|
||||
key="tenant-1/spaces/space-1/file.txt",
|
||||
metadata={"tenantId": "tenant-1"},
|
||||
size_bytes=4,
|
||||
)
|
||||
|
||||
|
||||
def _metadata_header(metadata: dict[str, str]) -> str:
|
||||
return urlsafe_b64encode(json.dumps(metadata).encode()).decode().rstrip("=")
|
||||
|
||||
|
||||
@patch("controllers.inner_api.knowledge_fs.storage.load_remote_image")
|
||||
def test_remote_image_endpoint_returns_only_sniffed_bounded_image_bytes(
|
||||
load_remote_image: MagicMock,
|
||||
app: Flask,
|
||||
) -> None:
|
||||
body = b"\x89PNG\r\n\x1a\nremote"
|
||||
load_remote_image.return_value = KnowledgeFSResolvedRemoteImage(
|
||||
byte_size=len(body),
|
||||
mime_type="image/png",
|
||||
body=body,
|
||||
sha256="a" * 64,
|
||||
)
|
||||
handler = KnowledgeFSRemoteImageApi()
|
||||
|
||||
with app.test_request_context("/?url=https%3A%2F%2Fcdn.example.test%2Fimage.png"):
|
||||
result = inspect.unwrap(handler.get)(handler)
|
||||
|
||||
assert result.get_data() == body
|
||||
assert result.content_type == "image/png"
|
||||
assert result.headers["X-Knowledge-FS-Remote-Image-Sha256"] == "a" * 64
|
||||
load_remote_image.assert_called_once_with("https://cdn.example.test/image.png")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error", "expected_status"),
|
||||
[
|
||||
(KnowledgeFSRemoteImageError("REMOTE_IMAGE_BLOCKED", "blocked", retryable=False), 403),
|
||||
(KnowledgeFSRemoteImageError("REMOTE_IMAGE_HOST_NOT_FOUND", "unresolvable", retryable=False), 404),
|
||||
(KnowledgeFSRemoteImageError("REMOTE_IMAGE_NOT_FOUND", "missing", retryable=False), 404),
|
||||
(KnowledgeFSRemoteImageError("REMOTE_IMAGE_TOO_LARGE", "large", retryable=False), 413),
|
||||
(
|
||||
KnowledgeFSRemoteImageError("REMOTE_IMAGE_CONTENT_UNSUPPORTED", "unsupported", retryable=False),
|
||||
415,
|
||||
),
|
||||
(KnowledgeFSRemoteImageError("REMOTE_IMAGE_RATE_LIMITED", "limited", retryable=True), 429),
|
||||
(KnowledgeFSRemoteImageError("REMOTE_IMAGE_UPSTREAM_UNAVAILABLE", "offline", retryable=True), 502),
|
||||
],
|
||||
)
|
||||
@patch("controllers.inner_api.knowledge_fs.storage.load_remote_image")
|
||||
def test_remote_image_endpoint_maps_safe_failure_statuses(
|
||||
load_remote_image: MagicMock,
|
||||
error: KnowledgeFSRemoteImageError,
|
||||
expected_status: int,
|
||||
app: Flask,
|
||||
) -> None:
|
||||
load_remote_image.side_effect = error
|
||||
handler = KnowledgeFSRemoteImageApi()
|
||||
|
||||
with app.test_request_context("/?url=https%3A%2F%2Fcdn.example.test%2Fimage.png"):
|
||||
with pytest.raises(KnowledgeFSObjectStorageHttpError) as exc_info:
|
||||
inspect.unwrap(handler.get)(handler)
|
||||
|
||||
assert exc_info.value.code == expected_status
|
||||
|
||||
|
||||
@patch("controllers.inner_api.knowledge_fs.storage.load_query_image")
|
||||
def test_query_image_endpoint_returns_only_actor_owned_bounded_bytes(
|
||||
load_query_image: MagicMock,
|
||||
app: Flask,
|
||||
) -> None:
|
||||
upload_file_id = "00000000-0000-4000-8000-000000000001"
|
||||
body = b"\x89PNG\r\n\x1a\nquery"
|
||||
load_query_image.return_value = KnowledgeFSResolvedQueryImage(
|
||||
upload_file_id=upload_file_id,
|
||||
byte_size=len(body),
|
||||
mime_type="image/png",
|
||||
body=body,
|
||||
sha256="a" * 64,
|
||||
)
|
||||
handler = KnowledgeFSQueryImageApi()
|
||||
|
||||
with app.test_request_context(
|
||||
f"/?uploadFileId={upload_file_id}&tenantId=tenant-1&subjectId=dify-account:account-1"
|
||||
):
|
||||
result = inspect.unwrap(handler.get)(handler)
|
||||
|
||||
assert result.get_data() == body
|
||||
assert result.content_type == "image/png"
|
||||
assert result.headers["X-Query-Image-Sha256"] == "a" * 64
|
||||
assert result.headers["X-Query-Image-Upload-File-Id"] == upload_file_id
|
||||
load_query_image.assert_called_once_with(
|
||||
tenant_id="tenant-1",
|
||||
account_id="account-1",
|
||||
access_grant=None,
|
||||
subject_id="dify-account:account-1",
|
||||
upload_file_id=upload_file_id,
|
||||
)
|
||||
|
||||
|
||||
@patch("controllers.inner_api.knowledge_fs.storage.load_query_image")
|
||||
def test_query_image_endpoint_forwards_workflow_grant_without_requiring_an_account_subject(
|
||||
load_query_image: MagicMock,
|
||||
app: Flask,
|
||||
) -> None:
|
||||
upload_file_id = "00000000-0000-4000-8000-000000000001"
|
||||
body = b"\x89PNG\r\n\x1a\nquery"
|
||||
load_query_image.return_value = KnowledgeFSResolvedQueryImage(
|
||||
upload_file_id=upload_file_id,
|
||||
byte_size=len(body),
|
||||
mime_type="image/png",
|
||||
body=body,
|
||||
sha256="a" * 64,
|
||||
)
|
||||
handler = KnowledgeFSQueryImageApi()
|
||||
|
||||
with app.test_request_context(
|
||||
f"/?uploadFileId={upload_file_id}&tenantId=tenant-1&subjectId=dify-app:app-1",
|
||||
headers={"X-Knowledge-FS-Query-Image-Grant": "short-lived-grant"},
|
||||
):
|
||||
result = inspect.unwrap(handler.get)(handler)
|
||||
|
||||
assert result.get_data() == body
|
||||
load_query_image.assert_called_once_with(
|
||||
tenant_id="tenant-1",
|
||||
account_id=None,
|
||||
access_grant="short-lived-grant",
|
||||
subject_id="dify-app:app-1",
|
||||
upload_file_id=upload_file_id,
|
||||
)
|
||||
|
||||
|
||||
@patch("controllers.inner_api.knowledge_fs.storage.KnowledgeFSObjectStorageService")
|
||||
def test_put_decodes_portable_metadata_and_returns_camel_case(
|
||||
service_cls: MagicMock,
|
||||
app: Flask,
|
||||
object_metadata: KnowledgeFSObjectMetadata,
|
||||
) -> None:
|
||||
service_cls.return_value.put_object.return_value = object_metadata
|
||||
handler = KnowledgeFSObjectApi()
|
||||
|
||||
with app.test_request_context(
|
||||
f"/?key={object_metadata.key}",
|
||||
method="PUT",
|
||||
data=b"data",
|
||||
headers={
|
||||
"Content-Type": "application/octet-stream",
|
||||
"X-Knowledge-FS-Checksum-Sha256": object_metadata.checksum_sha256_base64,
|
||||
"X-Knowledge-FS-Content-Type": "text/plain",
|
||||
"X-Knowledge-FS-Metadata": _metadata_header({"tenantId": "tenant-1"}),
|
||||
},
|
||||
):
|
||||
result = inspect.unwrap(handler.put)(handler)
|
||||
|
||||
assert result == {
|
||||
"checksumSha256Base64": object_metadata.checksum_sha256_base64,
|
||||
"contentType": "text/plain",
|
||||
"key": object_metadata.key,
|
||||
"metadata": {"tenantId": "tenant-1"},
|
||||
"sizeBytes": 4,
|
||||
}
|
||||
service_cls.return_value.put_object.assert_called_once_with(
|
||||
body=b"data",
|
||||
checksum_sha256_base64=object_metadata.checksum_sha256_base64,
|
||||
content_type="text/plain",
|
||||
key=object_metadata.key,
|
||||
metadata={"tenantId": "tenant-1"},
|
||||
)
|
||||
|
||||
|
||||
@patch("controllers.inner_api.knowledge_fs.storage.KnowledgeFSObjectStorageService")
|
||||
def test_get_streams_object_with_portable_headers(
|
||||
service_cls: MagicMock,
|
||||
app: Flask,
|
||||
object_metadata: KnowledgeFSObjectMetadata,
|
||||
) -> None:
|
||||
service_cls.return_value.head_object.return_value = object_metadata
|
||||
service_cls.return_value.load_stream.return_value = iter((b"da", b"ta"))
|
||||
handler = KnowledgeFSObjectApi()
|
||||
|
||||
with app.test_request_context(f"/?key={object_metadata.key}"):
|
||||
result = inspect.unwrap(handler.get)(handler)
|
||||
assert isinstance(result, Response)
|
||||
assert result.get_data() == b"data"
|
||||
|
||||
assert result.content_length == 4
|
||||
assert result.content_type == "text/plain"
|
||||
assert result.headers["X-Knowledge-FS-Checksum-Sha256"] == object_metadata.checksum_sha256_base64
|
||||
|
||||
|
||||
@patch("controllers.inner_api.knowledge_fs.storage.KnowledgeFSObjectStorageService")
|
||||
def test_head_list_delete_and_health_handlers(
|
||||
service_cls: MagicMock,
|
||||
app: Flask,
|
||||
object_metadata: KnowledgeFSObjectMetadata,
|
||||
) -> None:
|
||||
service = service_cls.return_value
|
||||
service.head_object.return_value = object_metadata
|
||||
service.list_objects.return_value = KnowledgeFSObjectList(
|
||||
objects=(object_metadata,),
|
||||
next_cursor=object_metadata.key,
|
||||
)
|
||||
service.health.return_value = True
|
||||
|
||||
with app.test_request_context(f"/?key={object_metadata.key}"):
|
||||
metadata_result = inspect.unwrap(KnowledgeFSObjectMetadataApi().get)(KnowledgeFSObjectMetadataApi())
|
||||
with app.test_request_context(f"/?prefix=tenant-1/spaces/&cursor={object_metadata.key}&limit=1"):
|
||||
list_result = inspect.unwrap(KnowledgeFSObjectListApi().get)(KnowledgeFSObjectListApi())
|
||||
with app.test_request_context(f"/?key={object_metadata.key}", method="DELETE"):
|
||||
delete_result = inspect.unwrap(KnowledgeFSObjectApi().delete)(KnowledgeFSObjectApi())
|
||||
with app.test_request_context():
|
||||
health_result = inspect.unwrap(KnowledgeFSObjectHealthApi().get)(KnowledgeFSObjectHealthApi())
|
||||
|
||||
assert metadata_result["key"] == object_metadata.key
|
||||
assert list_result == {
|
||||
"nextCursor": object_metadata.key,
|
||||
"objects": [metadata_result],
|
||||
}
|
||||
assert delete_result == ("", 204)
|
||||
assert health_result == {"ok": True}
|
||||
service.delete_object.assert_called_once_with(key=object_metadata.key)
|
||||
service.list_objects.assert_called_once_with(
|
||||
cursor=object_metadata.key,
|
||||
limit=1,
|
||||
prefix="tenant-1/spaces/",
|
||||
)
|
||||
|
||||
|
||||
@patch("controllers.inner_api.knowledge_fs.storage.KnowledgeFSObjectStorageService")
|
||||
def test_missing_object_returns_404(service_cls: MagicMock, app: Flask) -> None:
|
||||
service_cls.return_value.head_object.return_value = None
|
||||
handler = KnowledgeFSObjectMetadataApi()
|
||||
|
||||
with app.test_request_context("/?key=tenant-1/missing"):
|
||||
with pytest.raises(KnowledgeFSObjectStorageHttpError) as exc_info:
|
||||
inspect.unwrap(handler.get)(handler)
|
||||
|
||||
assert exc_info.value.code == 404
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error", "expected_status"),
|
||||
[
|
||||
(KnowledgeFSObjectStorageChecksumError("mismatch"), 422),
|
||||
(KnowledgeFSObjectStorageUnavailableError("unavailable"), 503),
|
||||
],
|
||||
)
|
||||
@patch("controllers.inner_api.knowledge_fs.storage.KnowledgeFSObjectStorageService")
|
||||
def test_storage_errors_are_mapped_to_safe_http_statuses(
|
||||
service_cls: MagicMock,
|
||||
error: Exception,
|
||||
expected_status: int,
|
||||
app: Flask,
|
||||
) -> None:
|
||||
service_cls.return_value.put_object.side_effect = error
|
||||
handler = KnowledgeFSObjectApi()
|
||||
|
||||
with app.test_request_context(
|
||||
"/?key=tenant-1/file.txt",
|
||||
method="PUT",
|
||||
data=b"data",
|
||||
headers={"X-Knowledge-FS-Metadata": _metadata_header({})},
|
||||
):
|
||||
with pytest.raises(KnowledgeFSObjectStorageHttpError) as exc_info:
|
||||
inspect.unwrap(handler.put)(handler)
|
||||
|
||||
assert exc_info.value.code == expected_status
|
||||
@@ -0,0 +1,468 @@
|
||||
import hashlib
|
||||
import inspect
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
from collections.abc import Callable, Generator
|
||||
from contextlib import contextmanager
|
||||
from unittest.mock import call, patch
|
||||
from uuid import UUID
|
||||
|
||||
import gevent
|
||||
import pymysql
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from flask.testing import FlaskClient
|
||||
from werkzeug.exceptions import BadRequest, NotFound, ServiceUnavailable
|
||||
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.knowledge_fs.texts import KnowledgeFSTextApi
|
||||
from libs.external_api import ExternalApi
|
||||
from models.dataset import TidbAuthBinding
|
||||
from models.enums import TidbAuthBindingStatus
|
||||
from services.knowledge_fs.text_store import TextIndexPendingError
|
||||
from services.knowledge_fs.vector_store import VectorResult
|
||||
from services.tidb_binding_service import TidbBindingPendingError
|
||||
|
||||
"""Exercise provider failures through the real HTTP bridge and TiDB store."""
|
||||
|
||||
|
||||
REQUEST_ID = "d4103b40-0744-458f-a740-d8047d1cbe8d"
|
||||
HEADERS = {"X-Inner-Api-Key": "trusted-key", "X-Knowledge-FS-Request-Id": REQUEST_ID}
|
||||
_TEXT_DIAGNOSTICS_PAYLOAD = {
|
||||
"operation": "search",
|
||||
"scope": {
|
||||
"tenant_id": "018f0d60-7a49-7cc2-9c1b-5b36f18f0001",
|
||||
"knowledge_space_id": "018f0d60-7a49-7cc2-9c1b-5b36f18f0002",
|
||||
"index_version": "private-index-version",
|
||||
},
|
||||
"ids": ["018f0d60-7a49-7cc2-9c1b-5b36f18f0004"],
|
||||
"query": "private-search-content",
|
||||
}
|
||||
|
||||
|
||||
class TestTextDiagnosticsCases:
|
||||
@staticmethod
|
||||
@pytest.fixture
|
||||
def http_client(config_overrides: Callable[..., None]) -> FlaskClient:
|
||||
config_overrides(
|
||||
VECTOR_STORE="tidb_on_qdrant", PLUGIN_DAEMON_KEY="enabled", INNER_API_KEY_FOR_PLUGIN="trusted-key"
|
||||
)
|
||||
app = Flask(__name__)
|
||||
api = ExternalApi(app)
|
||||
api.models.update(inner_api_ns.models)
|
||||
api.add_resource(KnowledgeFSTextApi, "/texts")
|
||||
return app.test_client()
|
||||
|
||||
@staticmethod
|
||||
@pytest.mark.parametrize("stage", ["connect", "index_check", "replica_readiness", "search"])
|
||||
def test_database_failure_retains_code_stage_and_correlation_without_secrets(
|
||||
http_client: FlaskClient, caplog: pytest.LogCaptureFixture, stage: str
|
||||
) -> None:
|
||||
error = pymysql.err.OperationalError(
|
||||
1815, "private-password private-search-content SELECT text FROM private-table at private-host"
|
||||
)
|
||||
binding = TidbAuthBinding(
|
||||
tenant_id=_TEXT_DIAGNOSTICS_PAYLOAD["scope"]["tenant_id"],
|
||||
cluster_id="private-cluster",
|
||||
cluster_name="private-cluster-name",
|
||||
account="private-account",
|
||||
password="private-password",
|
||||
active=True,
|
||||
status=TidbAuthBindingStatus.ACTIVE,
|
||||
)
|
||||
with (
|
||||
patch("services.tidb_binding_service.resolve_tidb_auth_binding", return_value=binding),
|
||||
patch("services.knowledge_fs.text_store_tidb._sql_endpoint", return_value=("private-host", 4000)),
|
||||
patch("pymysql.connect") as connect,
|
||||
caplog.at_level(logging.WARNING),
|
||||
):
|
||||
connection = connect.return_value
|
||||
cursor = connection.cursor.return_value.__enter__.return_value
|
||||
cursor.fetchone.return_value = (1,)
|
||||
|
||||
def execute(sql: str, *_args: object) -> None:
|
||||
if (
|
||||
(stage == "index_check" and "information_schema.TABLES" in sql)
|
||||
or (stage == "replica_readiness" and "information_schema.TIFLASH_REPLICA" in sql)
|
||||
or (stage == "search" and "fts_match_word" in sql)
|
||||
):
|
||||
raise error
|
||||
|
||||
cursor.execute.side_effect = execute
|
||||
if stage == "connect":
|
||||
connect.side_effect = error
|
||||
if stage == "search":
|
||||
# Cleanup must not replace the SQL failure with an unrelated secondary error.
|
||||
connection.close.side_effect = OSError("private-cleanup-error")
|
||||
response = http_client.post("/texts", json=_TEXT_DIAGNOSTICS_PAYLOAD, headers=HEADERS)
|
||||
|
||||
assert response.status_code == 503
|
||||
assert response.get_json() == {
|
||||
"code": "service_unavailable",
|
||||
"message": "KnowledgeFS full-text backend is unavailable",
|
||||
"status": 503,
|
||||
}
|
||||
assert response.headers["X-Knowledge-FS-Request-Id"] == REQUEST_ID
|
||||
records = [r for r in caplog.records if "knowledge_fs.text_backend_failure" in r.getMessage()]
|
||||
assert len(records) == 1
|
||||
record = records[0]
|
||||
attributes = record.__dict__["attributes"]
|
||||
assert attributes["request_id"] == REQUEST_ID
|
||||
assert attributes["tenant_id"] == _TEXT_DIAGNOSTICS_PAYLOAD["scope"]["tenant_id"]
|
||||
assert attributes["knowledge_space_id"] == _TEXT_DIAGNOSTICS_PAYLOAD["scope"]["knowledge_space_id"]
|
||||
assert attributes["provider"] == "tidb_on_qdrant"
|
||||
assert attributes["operation"] == "search"
|
||||
assert attributes["stage"] == stage
|
||||
assert attributes["elapsed_ms"] >= attributes["stage_elapsed_ms"] >= 0
|
||||
assert attributes["errors"][0]["type"] == "pymysql.err.OperationalError"
|
||||
assert attributes["errors"][0]["db_error_code"] == 1815
|
||||
assert attributes["errors"][0]["db_error_name"] == "INTERNAL_ERROR"
|
||||
assert attributes["errors"][0]["frames"]
|
||||
# Both text and structured JSON logs must contain diagnostics, never raw provider data.
|
||||
assert "1815" in record.getMessage()
|
||||
assert stage in record.getMessage()
|
||||
assert record.exc_info is None
|
||||
serialized = caplog.text + json.dumps(attributes) + response.get_data(as_text=True)
|
||||
for secret in (
|
||||
"private-password",
|
||||
"private-search-content",
|
||||
"private-table",
|
||||
"private-host",
|
||||
"private-account",
|
||||
"private-cluster",
|
||||
"private-index-version",
|
||||
"private-cleanup-error",
|
||||
):
|
||||
assert secret not in serialized
|
||||
|
||||
@staticmethod
|
||||
def test_deadline_failure_is_diagnosable_before_connect(
|
||||
http_client: FlaskClient, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
response = http_client.post(
|
||||
"/texts",
|
||||
json=_TEXT_DIAGNOSTICS_PAYLOAD,
|
||||
headers={**HEADERS, "X-Knowledge-FS-Deadline-Ms": str(int(time.time() * 1000) - 1)},
|
||||
)
|
||||
assert response.status_code == 503
|
||||
attributes = next(r.__dict__["attributes"] for r in caplog.records if "text_backend_failure" in r.getMessage())
|
||||
assert attributes["stage"] == "deadline"
|
||||
assert attributes["errors"][0]["type"].endswith("TextSearchTimeoutError")
|
||||
|
||||
@staticmethod
|
||||
def test_pending_index_keeps_retry_header_and_warning_diagnostic(
|
||||
http_client: FlaskClient, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
with patch(
|
||||
"controllers.inner_api.knowledge_fs.texts.configured_text_client",
|
||||
side_effect=TextIndexPendingError("private-pending-message"),
|
||||
):
|
||||
response = http_client.post("/texts", json=_TEXT_DIAGNOSTICS_PAYLOAD, headers=HEADERS)
|
||||
assert response.status_code == 503
|
||||
assert response.headers["Retry-After"] == "5"
|
||||
record = next(r for r in caplog.records if "text_backend_failure" in r.getMessage())
|
||||
assert record.levelno == logging.WARNING
|
||||
assert record.__dict__["attributes"]["outcome"] == "pending"
|
||||
assert "private-pending-message" not in caplog.text
|
||||
|
||||
@staticmethod
|
||||
@pytest.mark.parametrize("request_id", [None, "invalid sensitive value", "a" * 500, REQUEST_ID])
|
||||
def test_correlation_header_is_bounded_and_does_not_change_success(
|
||||
http_client: FlaskClient, request_id: str | None, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
headers = {"X-Inner-Api-Key": "trusted-key"}
|
||||
if request_id is not None:
|
||||
headers["X-Knowledge-FS-Request-Id"] = request_id
|
||||
with (
|
||||
patch("controllers.inner_api.knowledge_fs.texts.configured_text_client"),
|
||||
patch(
|
||||
"controllers.inner_api.knowledge_fs.texts.execute_text_request",
|
||||
return_value={"points": [], "matches": []},
|
||||
),
|
||||
):
|
||||
response = http_client.post("/texts", json=_TEXT_DIAGNOSTICS_PAYLOAD, headers=headers)
|
||||
assert response.status_code == 200
|
||||
assert response.get_json() == {"points": [], "matches": []}
|
||||
correlation_id = response.headers["X-Knowledge-FS-Request-Id"]
|
||||
assert str(UUID(correlation_id)) == correlation_id
|
||||
if request_id == REQUEST_ID:
|
||||
assert correlation_id == request_id
|
||||
assert not [r for r in caplog.records if "text_backend_failure" in r.getMessage()]
|
||||
|
||||
|
||||
_TEXTS_PAYLOAD = {
|
||||
"operation": "get",
|
||||
"scope": {
|
||||
"tenant_id": "018f0d60-7a49-7cc2-9c1b-5b36f18f0001",
|
||||
"knowledge_space_id": "018f0d60-7a49-7cc2-9c1b-5b36f18f0002",
|
||||
"index_version": "fts-v1",
|
||||
},
|
||||
"ids": ["018f0d60-7a49-7cc2-9c1b-5b36f18f0004"],
|
||||
}
|
||||
|
||||
|
||||
class TestTextsCases:
|
||||
@staticmethod
|
||||
@pytest.mark.parametrize("deadline", ["invalid", "NaN", "1.5", "-1", "9" * 30])
|
||||
def test_search_rejects_invalid_deadline_before_provider_io(app: Flask, deadline: str) -> None:
|
||||
handler = KnowledgeFSTextApi()
|
||||
with (
|
||||
app.test_request_context(
|
||||
json={**_TEXTS_PAYLOAD, "operation": "search", "query": "refund"},
|
||||
headers={"X-Knowledge-FS-Deadline-Ms": deadline},
|
||||
),
|
||||
patch("controllers.inner_api.knowledge_fs.texts.configured_text_client") as client,
|
||||
):
|
||||
with pytest.raises(BadRequest):
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
client.assert_not_called()
|
||||
|
||||
@staticmethod
|
||||
def test_search_rejects_expired_deadline_before_provider_io(app: Flask) -> None:
|
||||
handler = KnowledgeFSTextApi()
|
||||
with (
|
||||
app.test_request_context(
|
||||
json={**_TEXTS_PAYLOAD, "operation": "search", "query": "refund"},
|
||||
headers={"X-Knowledge-FS-Deadline-Ms": str(int(time.time() * 1000) - 1)},
|
||||
),
|
||||
patch("controllers.inner_api.knowledge_fs.texts.configured_text_client") as client,
|
||||
):
|
||||
with pytest.raises(ServiceUnavailable):
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
client.assert_not_called()
|
||||
|
||||
@staticmethod
|
||||
def test_legacy_search_without_deadline_is_still_bounded(app: Flask) -> None:
|
||||
handler = KnowledgeFSTextApi()
|
||||
with (
|
||||
app.test_request_context(json={**_TEXTS_PAYLOAD, "operation": "search", "query": "refund"}),
|
||||
patch("controllers.inner_api.knowledge_fs.texts.configured_text_client") as client,
|
||||
patch(
|
||||
"controllers.inner_api.knowledge_fs.texts.execute_text_request",
|
||||
return_value={"points": [], "matches": []},
|
||||
),
|
||||
):
|
||||
assert inspect.unwrap(handler.post)(handler) == {"points": [], "matches": []}
|
||||
assert 49 < client.call_args.kwargs["deadline"].remaining() <= 50
|
||||
|
||||
@staticmethod
|
||||
@pytest.mark.parametrize("stage", ["connect", "query"])
|
||||
def test_search_deadline_interrupts_stalled_backend_and_runs_cleanup(app: Flask, stage: str) -> None:
|
||||
cleaned_up = False
|
||||
|
||||
@contextmanager
|
||||
def stalled_client(*_args: object, **_kwargs: object) -> Generator[object]:
|
||||
nonlocal cleaned_up
|
||||
try:
|
||||
if stage == "connect":
|
||||
gevent.sleep(1)
|
||||
yield object()
|
||||
finally:
|
||||
cleaned_up = True
|
||||
|
||||
def execute(*_args: object) -> VectorResult:
|
||||
gevent.sleep(1)
|
||||
return {"points": [], "matches": []}
|
||||
|
||||
handler = KnowledgeFSTextApi()
|
||||
started = time.monotonic()
|
||||
with (
|
||||
app.test_request_context(
|
||||
json={**_TEXTS_PAYLOAD, "operation": "search", "query": "refund"},
|
||||
headers={"X-Knowledge-FS-Deadline-Ms": str(int(time.time() * 1000) + 50)},
|
||||
),
|
||||
patch("controllers.inner_api.knowledge_fs.texts.configured_text_client", stalled_client),
|
||||
patch("controllers.inner_api.knowledge_fs.texts.execute_text_request", execute),
|
||||
):
|
||||
with pytest.raises(ServiceUnavailable):
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
assert cleaned_up
|
||||
assert time.monotonic() - started < 0.5
|
||||
|
||||
@staticmethod
|
||||
def test_text_controller_rejects_unauthenticated_requests(app: Flask) -> None:
|
||||
with app.test_request_context(json=_TEXTS_PAYLOAD):
|
||||
with pytest.raises(NotFound):
|
||||
KnowledgeFSTextApi().post()
|
||||
|
||||
@staticmethod
|
||||
def test_text_controller_validates_before_opening_backend(app: Flask) -> None:
|
||||
handler = KnowledgeFSTextApi()
|
||||
with (
|
||||
app.test_request_context(json={**_TEXTS_PAYLOAD, "endpoint": "http://untrusted"}),
|
||||
patch("controllers.inner_api.knowledge_fs.texts.configured_text_client") as client,
|
||||
):
|
||||
with pytest.raises(BadRequest):
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
client.assert_not_called()
|
||||
|
||||
@staticmethod
|
||||
def test_text_controller_hides_provider_credentials(app: Flask) -> None:
|
||||
handler = KnowledgeFSTextApi()
|
||||
with (
|
||||
app.test_request_context(json=_TEXTS_PAYLOAD),
|
||||
patch(
|
||||
"controllers.inner_api.knowledge_fs.texts.configured_text_client",
|
||||
side_effect=RuntimeError("private-api-key"),
|
||||
),
|
||||
):
|
||||
with pytest.raises(ServiceUnavailable) as error:
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
assert "private-api-key" not in str(error.value)
|
||||
assert error.value.__suppress_context__
|
||||
|
||||
@staticmethod
|
||||
def test_text_controller_serializes_successful_response(app: Flask) -> None:
|
||||
handler = KnowledgeFSTextApi()
|
||||
result: dict[str, list[dict[str, object]]] = {
|
||||
"points": [],
|
||||
"matches": [{"id": _TEXTS_PAYLOAD["ids"][0], "score": 0.75}],
|
||||
}
|
||||
with (
|
||||
app.test_request_context(json=_TEXTS_PAYLOAD),
|
||||
patch("controllers.inner_api.knowledge_fs.texts.configured_text_client"),
|
||||
patch("controllers.inner_api.knowledge_fs.texts.execute_text_request", return_value=result),
|
||||
):
|
||||
assert inspect.unwrap(handler.post)(handler) == result
|
||||
|
||||
@staticmethod
|
||||
def test_text_controller_accepts_only_configured_inner_key(
|
||||
app: Flask, config_overrides: Callable[..., None]
|
||||
) -> None:
|
||||
config_overrides(PLUGIN_DAEMON_KEY="enabled", INNER_API_KEY_FOR_PLUGIN="trusted-key")
|
||||
with (
|
||||
patch("controllers.inner_api.knowledge_fs.texts.configured_text_client") as client,
|
||||
patch(
|
||||
"controllers.inner_api.knowledge_fs.texts.execute_text_request",
|
||||
return_value={"points": [], "matches": []},
|
||||
),
|
||||
):
|
||||
for key in [None, "wrong-key"]:
|
||||
with app.test_request_context(json=_TEXTS_PAYLOAD, headers={"X-Inner-Api-Key": key} if key else {}):
|
||||
with pytest.raises(NotFound):
|
||||
KnowledgeFSTextApi().post()
|
||||
client.assert_not_called()
|
||||
with app.test_request_context(json=_TEXTS_PAYLOAD, headers={"X-Inner-Api-Key": "trusted-key"}):
|
||||
assert KnowledgeFSTextApi().post() == {"points": [], "matches": []}
|
||||
client.assert_called_once_with(_TEXTS_PAYLOAD["scope"]["tenant_id"], allow_create=False)
|
||||
|
||||
@staticmethod
|
||||
def test_text_controller_reports_pending_index_without_leaking_credentials(app: Flask) -> None:
|
||||
handler = KnowledgeFSTextApi()
|
||||
with (
|
||||
app.test_request_context(json=_TEXTS_PAYLOAD),
|
||||
patch(
|
||||
"controllers.inner_api.knowledge_fs.texts.configured_text_client",
|
||||
side_effect=TextIndexPendingError("secret"),
|
||||
),
|
||||
):
|
||||
with pytest.raises(ServiceUnavailable) as error:
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
assert error.value.retry_after == 5
|
||||
assert "secret" not in str(error.value)
|
||||
|
||||
@staticmethod
|
||||
@pytest.mark.parametrize("pending_error", [TidbBindingPendingError, TextIndexPendingError])
|
||||
def test_http_pending_response_preserves_retry_after_header(
|
||||
config_overrides: Callable[..., None], pending_error: type[Exception]
|
||||
) -> None:
|
||||
config_overrides(PLUGIN_DAEMON_KEY="enabled", INNER_API_KEY_FOR_PLUGIN="trusted-key")
|
||||
http_app = Flask(__name__)
|
||||
api = ExternalApi(http_app)
|
||||
api.models.update(inner_api_ns.models)
|
||||
api.add_resource(KnowledgeFSTextApi, "/texts")
|
||||
with patch(
|
||||
"controllers.inner_api.knowledge_fs.texts.configured_text_client",
|
||||
side_effect=pending_error("private endpoint"),
|
||||
):
|
||||
response = http_app.test_client().post(
|
||||
"/texts", json=_TEXTS_PAYLOAD, headers={"X-Inner-Api-Key": "trusted-key"}
|
||||
)
|
||||
|
||||
assert response.status_code == 503
|
||||
assert response.headers["Retry-After"] == "5"
|
||||
assert response.mimetype == "application/json"
|
||||
assert response.get_json() == {
|
||||
"code": "service_unavailable",
|
||||
"message": "KnowledgeFS full-text index is being initialized",
|
||||
"status": 503,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def test_http_cleanup_after_interrupted_first_write_does_not_initialize_database(
|
||||
config_overrides: Callable[..., None],
|
||||
) -> None:
|
||||
config_overrides(
|
||||
VECTOR_STORE="tidb_on_qdrant", PLUGIN_DAEMON_KEY="enabled", INNER_API_KEY_FOR_PLUGIN="trusted-key"
|
||||
)
|
||||
http_app = Flask(__name__)
|
||||
api = ExternalApi(http_app)
|
||||
api.models.update(inner_api_ns.models)
|
||||
api.add_resource(KnowledgeFSTextApi, "/texts")
|
||||
binding = TidbAuthBinding(
|
||||
tenant_id=_TEXTS_PAYLOAD["scope"]["tenant_id"],
|
||||
cluster_id="created-cluster",
|
||||
cluster_name="tenant-cluster",
|
||||
account="tenant.root",
|
||||
password="test-password",
|
||||
active=True,
|
||||
status=TidbAuthBindingStatus.ACTIVE,
|
||||
)
|
||||
text = "refund policy"
|
||||
upsert = {
|
||||
"operation": "upsert",
|
||||
"scope": _TEXTS_PAYLOAD["scope"],
|
||||
"points": [
|
||||
{
|
||||
"id": _TEXTS_PAYLOAD["ids"][0],
|
||||
"generation_id": "018f0d60-7a49-7cc2-9c1b-5b36f18f0003",
|
||||
"content_hash": hashlib.sha256(text.encode()).hexdigest(),
|
||||
"text": text,
|
||||
}
|
||||
],
|
||||
}
|
||||
with (
|
||||
patch(
|
||||
"services.tidb_binding_service.resolve_tidb_auth_binding",
|
||||
side_effect=[TidbBindingPendingError(), binding, binding, binding, binding],
|
||||
) as resolver,
|
||||
patch("services.knowledge_fs.text_store_tidb._sql_endpoint", return_value=("tenant.test", 4000)),
|
||||
patch("pymysql.connect") as connect,
|
||||
):
|
||||
connection = connect.return_value
|
||||
|
||||
def open_connection(**kwargs: object) -> object:
|
||||
if kwargs.get("database") == "ai":
|
||||
raise pymysql.err.OperationalError(1049, "Unknown database 'ai'")
|
||||
return connection
|
||||
|
||||
def read_only_sql(sql: str, *_args: object) -> None:
|
||||
assert sql.lstrip().upper().startswith("SELECT "), sql
|
||||
|
||||
connect.side_effect = open_connection
|
||||
cursor = connection.cursor.return_value.__enter__.return_value
|
||||
cursor.execute.side_effect = read_only_sql
|
||||
cursor.fetchone.return_value = None
|
||||
client = http_app.test_client()
|
||||
headers = {"X-Inner-Api-Key": "trusted-key"}
|
||||
|
||||
# Provisioning was committed, but the writer stopped before initializing SQL storage.
|
||||
response = client.post("/texts", json=upsert, headers=headers)
|
||||
assert response.status_code == 503
|
||||
assert response.headers["Retry-After"] == "5"
|
||||
connect.assert_not_called()
|
||||
|
||||
for operation in ("delete", "get", "delete", "get"):
|
||||
response = client.post("/texts", json={**_TEXTS_PAYLOAD, "operation": operation}, headers=headers)
|
||||
assert response.status_code == 200
|
||||
assert response.get_json() == {"points": [], "matches": []}
|
||||
assert "Retry-After" not in response.headers
|
||||
|
||||
assert resolver.call_args_list == [
|
||||
call(_TEXTS_PAYLOAD["scope"]["tenant_id"], allow_create=True),
|
||||
*[call(_TEXTS_PAYLOAD["scope"]["tenant_id"], allow_create=False)] * 4,
|
||||
]
|
||||
assert connect.call_count == 4
|
||||
connection.select_db.assert_not_called()
|
||||
assert connection.close.call_count == 4
|
||||
+223
@@ -0,0 +1,223 @@
|
||||
import inspect
|
||||
from collections.abc import Callable
|
||||
from contextlib import AbstractContextManager, nullcontext
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from werkzeug.exceptions import BadRequest, NotFound, ServiceUnavailable
|
||||
|
||||
from controllers.inner_api.knowledge_fs.vectors import KnowledgeFSVectorApi, KnowledgeFSVectorQuotaExceededError
|
||||
from services.vector_space_admission_service import VectorSpaceAdmissionError, VectorSpaceAdmissionUnavailableError
|
||||
|
||||
PAYLOAD = {
|
||||
"operation": "get",
|
||||
"scope": {
|
||||
"tenant_id": "018f0d60-7a49-7cc2-9c1b-5b36f18f0001",
|
||||
"knowledge_space_id": "018f0d60-7a49-7cc2-9c1b-5b36f18f0002",
|
||||
"vector_space_id": "embedding-v1",
|
||||
"kind": "dense",
|
||||
"dimension": 2,
|
||||
},
|
||||
"ids": ["018f0d60-7a49-7cc2-9c1b-5b36f18f0004"],
|
||||
}
|
||||
|
||||
|
||||
def test_vector_controller_rejects_unauthenticated_requests(app: Flask) -> None:
|
||||
with app.test_request_context(json=PAYLOAD):
|
||||
with pytest.raises(NotFound):
|
||||
KnowledgeFSVectorApi().post()
|
||||
|
||||
|
||||
def test_vector_controller_validates_before_opening_backend(app: Flask) -> None:
|
||||
handler = KnowledgeFSVectorApi()
|
||||
with (
|
||||
app.test_request_context(json={**PAYLOAD, "endpoint": "http://untrusted"}),
|
||||
patch("controllers.inner_api.knowledge_fs.vectors.configured_vector_client") as client,
|
||||
):
|
||||
with pytest.raises(BadRequest):
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
client.assert_not_called()
|
||||
|
||||
|
||||
def test_vector_controller_hides_provider_credentials(app: Flask) -> None:
|
||||
handler = KnowledgeFSVectorApi()
|
||||
with (
|
||||
app.test_request_context(json=PAYLOAD),
|
||||
patch(
|
||||
"controllers.inner_api.knowledge_fs.vectors.configured_vector_client",
|
||||
side_effect=RuntimeError("private-api-key"),
|
||||
),
|
||||
):
|
||||
with pytest.raises(ServiceUnavailable) as error:
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
assert "private-api-key" not in str(error.value)
|
||||
assert error.value.__suppress_context__
|
||||
|
||||
|
||||
def test_vector_controller_serializes_successful_response(app: Flask) -> None:
|
||||
handler = KnowledgeFSVectorApi()
|
||||
result: dict[str, list[dict[str, object]]] = {"points": [], "matches": [{"id": PAYLOAD["ids"][0], "score": 0.75}]}
|
||||
with (
|
||||
app.test_request_context(json=PAYLOAD),
|
||||
patch("controllers.inner_api.knowledge_fs.vectors.configured_vector_client"),
|
||||
patch("controllers.inner_api.knowledge_fs.vectors.execute_vector_request", return_value=result),
|
||||
):
|
||||
assert inspect.unwrap(handler.post)(handler) == result
|
||||
|
||||
|
||||
def test_vector_controller_accepts_only_configured_inner_key(app: Flask, config_overrides: Callable[..., None]) -> None:
|
||||
config_overrides(PLUGIN_DAEMON_KEY="enabled", INNER_API_KEY_FOR_PLUGIN="trusted-key")
|
||||
with (
|
||||
patch("controllers.inner_api.knowledge_fs.vectors.configured_vector_client") as client,
|
||||
patch(
|
||||
"controllers.inner_api.knowledge_fs.vectors.execute_vector_request",
|
||||
return_value={"points": [], "matches": []},
|
||||
),
|
||||
):
|
||||
for key in [None, "wrong-key"]:
|
||||
with app.test_request_context(json=PAYLOAD, headers={"X-Inner-Api-Key": key} if key else {}):
|
||||
with pytest.raises(NotFound):
|
||||
KnowledgeFSVectorApi().post()
|
||||
client.assert_not_called()
|
||||
with app.test_request_context(json=PAYLOAD, headers={"X-Inner-Api-Key": "trusted-key"}):
|
||||
assert KnowledgeFSVectorApi().post() == {"points": [], "matches": []}
|
||||
client.assert_called_once_with(PAYLOAD["scope"]["tenant_id"], allow_create=False)
|
||||
|
||||
|
||||
def test_vector_controller_reports_quota_failure(app: Flask) -> None:
|
||||
handler = KnowledgeFSVectorApi()
|
||||
with (
|
||||
app.test_request_context(json=PAYLOAD),
|
||||
patch("controllers.inner_api.knowledge_fs.vectors.configured_vector_client"),
|
||||
patch(
|
||||
"controllers.inner_api.knowledge_fs.vectors.execute_vector_request",
|
||||
side_effect=VectorSpaceAdmissionError("vector quota exceeded"),
|
||||
),
|
||||
):
|
||||
with pytest.raises(KnowledgeFSVectorQuotaExceededError, match="vector quota exceeded"):
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("operation", ["get", "search", "delete", "upsert"])
|
||||
def test_only_admitted_writes_can_provision(app: Flask, operation: str) -> None:
|
||||
handler = KnowledgeFSVectorApi()
|
||||
payload: dict[str, object] = {**PAYLOAD, "operation": operation}
|
||||
if operation == "search":
|
||||
payload["query_vector"] = [1, 0]
|
||||
if operation == "upsert":
|
||||
payload.pop("ids")
|
||||
payload["points"] = [
|
||||
{"id": PAYLOAD["ids"][0], "generation_id": PAYLOAD["ids"][0], "content_hash": "a" * 64, "vector": [1, 0]}
|
||||
]
|
||||
with (
|
||||
app.test_request_context(json=payload),
|
||||
patch("controllers.inner_api.knowledge_fs.vectors.admit_vector_request") as admit,
|
||||
patch("controllers.inner_api.knowledge_fs.vectors.configured_vector_client") as client,
|
||||
patch(
|
||||
"controllers.inner_api.knowledge_fs.vectors.execute_vector_request",
|
||||
return_value={"points": [], "matches": []},
|
||||
) as execute,
|
||||
):
|
||||
|
||||
def open_client(*_args: object, **_kwargs: object) -> AbstractContextManager[object]:
|
||||
admit.assert_called_once()
|
||||
return nullcontext(object())
|
||||
|
||||
client.side_effect = open_client
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
assert client.call_args.kwargs["allow_create"] is (operation == "upsert")
|
||||
assert execute.call_args.kwargs["check_admission"] is False
|
||||
|
||||
|
||||
def test_quota_rejection_cannot_allocate_a_cluster(app: Flask) -> None:
|
||||
handler = KnowledgeFSVectorApi()
|
||||
with (
|
||||
app.test_request_context(json=PAYLOAD),
|
||||
patch(
|
||||
"controllers.inner_api.knowledge_fs.vectors.admit_vector_request",
|
||||
side_effect=VectorSpaceAdmissionError("quota"),
|
||||
),
|
||||
patch("controllers.inner_api.knowledge_fs.vectors.configured_vector_client") as client,
|
||||
):
|
||||
with pytest.raises(KnowledgeFSVectorQuotaExceededError):
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
client.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("admission_error", "status_code", "error_code"),
|
||||
[
|
||||
(VectorSpaceAdmissionError("vector quota exceeded"), 413, "vector_space_quota_exceeded"),
|
||||
(VectorSpaceAdmissionUnavailableError("vector usage is unavailable"), 503, "vector_space_quota_unavailable"),
|
||||
],
|
||||
)
|
||||
def test_http_quota_failure_cannot_allocate_a_cluster(
|
||||
config_overrides: Callable[..., None], admission_error: Exception, status_code: int, error_code: str
|
||||
) -> None:
|
||||
from flask_restx import Api
|
||||
|
||||
config_overrides(PLUGIN_DAEMON_KEY="enabled", INNER_API_KEY_FOR_PLUGIN="trusted-key")
|
||||
http_app = Flask(__name__)
|
||||
Api(http_app).add_resource(KnowledgeFSVectorApi, "/vectors")
|
||||
payload = {
|
||||
"operation": "upsert",
|
||||
"scope": PAYLOAD["scope"],
|
||||
"points": [
|
||||
{"id": PAYLOAD["ids"][0], "generation_id": PAYLOAD["ids"][0], "content_hash": "a" * 64, "vector": [1, 0]}
|
||||
],
|
||||
}
|
||||
with (
|
||||
patch("controllers.inner_api.knowledge_fs.vectors.admit_vector_request", side_effect=admission_error),
|
||||
patch("controllers.inner_api.knowledge_fs.vectors.configured_vector_client") as client,
|
||||
patch("controllers.inner_api.knowledge_fs.vectors.execute_vector_request") as execute,
|
||||
):
|
||||
response = http_app.test_client().post("/vectors", json=payload, headers={"X-Inner-Api-Key": "trusted-key"})
|
||||
assert response.status_code == status_code
|
||||
assert response.get_json() == {"code": error_code, "message": str(admission_error), "status": status_code}
|
||||
assert "Retry-After" not in response.headers
|
||||
client.assert_not_called()
|
||||
execute.assert_not_called()
|
||||
|
||||
|
||||
def test_pending_cluster_returns_explicit_retry_after_without_secrets(app: Flask) -> None:
|
||||
from services.tidb_binding_service import TidbBindingPendingError
|
||||
|
||||
handler = KnowledgeFSVectorApi()
|
||||
with (
|
||||
app.test_request_context(json=PAYLOAD),
|
||||
patch(
|
||||
"controllers.inner_api.knowledge_fs.vectors.configured_vector_client",
|
||||
side_effect=TidbBindingPendingError("private endpoint"),
|
||||
),
|
||||
):
|
||||
with pytest.raises(ServiceUnavailable) as error:
|
||||
inspect.unwrap(handler.post)(handler)
|
||||
assert ("Retry-After", "5") in error.value.get_headers()
|
||||
assert "private" not in str(error.value)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("pending_kind", ["binding", "collection"])
|
||||
def test_http_pending_response_preserves_retry_after_header(
|
||||
config_overrides: Callable[..., None], pending_kind: str
|
||||
) -> None:
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from libs.external_api import ExternalApi
|
||||
from services.knowledge_fs.vector_store import VectorCollectionPendingError
|
||||
from services.tidb_binding_service import TidbBindingPendingError
|
||||
|
||||
config_overrides(PLUGIN_DAEMON_KEY="enabled", INNER_API_KEY_FOR_PLUGIN="trusted-key")
|
||||
http_app = Flask(__name__)
|
||||
api = ExternalApi(http_app)
|
||||
api.models.update(inner_api_ns.models)
|
||||
api.add_resource(KnowledgeFSVectorApi, "/vectors")
|
||||
with patch(
|
||||
"controllers.inner_api.knowledge_fs.vectors.configured_vector_client",
|
||||
side_effect=(TidbBindingPendingError if pending_kind == "binding" else VectorCollectionPendingError)(
|
||||
"private endpoint"
|
||||
),
|
||||
):
|
||||
response = http_app.test_client().post("/vectors", json=PAYLOAD, headers={"X-Inner-Api-Key": "trusted-key"})
|
||||
assert response.status_code == 503
|
||||
assert response.headers["Retry-After"] == "5"
|
||||
assert "private" not in response.get_data(as_text=True)
|
||||
Whitespace-only changes.
+398
@@ -0,0 +1,398 @@
|
||||
"""Unit tests for the Agent config inner-API controller."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
from collections.abc import Callable
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
|
||||
from controllers.inner_api.plugin.agent_config import (
|
||||
AgentConfigDownloadRequestApi,
|
||||
AgentConfigEnvApi,
|
||||
AgentConfigManifestApi,
|
||||
AgentConfigNoteApi,
|
||||
AgentConfigPromptApi,
|
||||
AgentConfigPushApi,
|
||||
AgentConfigSkillInspectApi,
|
||||
)
|
||||
from libs.exception import RelayedHTTPError
|
||||
from services.agent_config_service import AgentConfigServiceError
|
||||
|
||||
MODULE = "controllers.inner_api.plugin.agent_config"
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
def _raw(method):
|
||||
return inspect.unwrap(method)
|
||||
|
||||
|
||||
def test_manifest_happy_path_calls_service() -> None:
|
||||
raw = _raw(AgentConfigManifestApi.get)
|
||||
|
||||
with app.test_request_context(
|
||||
"/?tenant_id=tenant-1&user_id=user-1&config_version_id=cfg-1&config_version_kind=build_draft"
|
||||
):
|
||||
with patch(f"{MODULE}.AgentConfigService") as service:
|
||||
service.return_value.manifest.return_value = {
|
||||
"agent_id": "agent-1",
|
||||
"config_version": {"id": "cfg-1", "kind": "build_draft", "writable": True},
|
||||
"skills": {
|
||||
"items": [
|
||||
{
|
||||
"id": "alpha",
|
||||
"name": "alpha",
|
||||
"file_id": "tool-file-1",
|
||||
"description": "Alpha skill",
|
||||
"size": 123,
|
||||
"hash": "sha256:abc",
|
||||
"mime_type": "application/zip",
|
||||
}
|
||||
]
|
||||
},
|
||||
"files": {
|
||||
"items": [
|
||||
{
|
||||
"id": "guide.txt",
|
||||
"name": "guide.txt",
|
||||
"file_id": "upload-file-1",
|
||||
"size": 7,
|
||||
"hash": "sha256:def",
|
||||
"mime_type": "text/plain",
|
||||
}
|
||||
]
|
||||
},
|
||||
"env_keys": ["KEY"],
|
||||
"note": "hello",
|
||||
}
|
||||
body = raw(AgentConfigManifestApi(), "agent-1")
|
||||
|
||||
assert body == {
|
||||
"agent_id": "agent-1",
|
||||
"config_version": {"id": "cfg-1", "kind": "build_draft", "writable": True},
|
||||
"skills": {
|
||||
"items": [
|
||||
{
|
||||
"id": "alpha",
|
||||
"name": "alpha",
|
||||
"file_id": "tool-file-1",
|
||||
"description": "Alpha skill",
|
||||
"size": 123,
|
||||
"hash": "sha256:abc",
|
||||
"mime_type": "application/zip",
|
||||
}
|
||||
]
|
||||
},
|
||||
"files": {
|
||||
"items": [
|
||||
{
|
||||
"id": "guide.txt",
|
||||
"name": "guide.txt",
|
||||
"file_id": "upload-file-1",
|
||||
"size": 7,
|
||||
"hash": "sha256:def",
|
||||
"mime_type": "text/plain",
|
||||
}
|
||||
]
|
||||
},
|
||||
"env_keys": ["KEY"],
|
||||
"note": "hello",
|
||||
}
|
||||
assert service.return_value.manifest.call_args.kwargs == {
|
||||
"tenant_id": "tenant-1",
|
||||
"agent_id": "agent-1",
|
||||
"user_id": "user-1",
|
||||
"config_version_id": "cfg-1",
|
||||
"config_version_kind": service.return_value.manifest.call_args.kwargs["config_version_kind"],
|
||||
}
|
||||
assert service.return_value.manifest.call_args.kwargs["config_version_kind"].value == "build_draft"
|
||||
|
||||
|
||||
def test_download_request_returns_origin_free_metadata() -> None:
|
||||
raw = _raw(AgentConfigDownloadRequestApi.post)
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "user-1",
|
||||
"config_version_id": "cfg-1",
|
||||
"config_version_kind": "build_draft",
|
||||
"config": {"kind": "skill", "name": "alpha"},
|
||||
}
|
||||
|
||||
with app.test_request_context("/", method="POST", json=payload):
|
||||
with patch(f"{MODULE}.AgentConfigService") as service:
|
||||
service.return_value.request_download.return_value.filename = "alpha.zip"
|
||||
service.return_value.request_download.return_value.mime_type = "application/zip"
|
||||
service.return_value.request_download.return_value.size = 123
|
||||
service.return_value.request_download.return_value.download_uri = "/files/tools/skill.zip?sign=1"
|
||||
body = raw(AgentConfigDownloadRequestApi(), "agent-1")
|
||||
|
||||
assert body == {
|
||||
"filename": "alpha.zip",
|
||||
"mime_type": "application/zip",
|
||||
"size": 123,
|
||||
"download_uri": "/files/tools/skill.zip?sign=1",
|
||||
}
|
||||
assert service.return_value.request_download.call_args.kwargs == {
|
||||
"tenant_id": "tenant-1",
|
||||
"agent_id": "agent-1",
|
||||
"user_id": "user-1",
|
||||
"config_version_id": "cfg-1",
|
||||
"config_version_kind": service.return_value.request_download.call_args.kwargs["config_version_kind"],
|
||||
"kind": "skill",
|
||||
"name": "alpha",
|
||||
}
|
||||
assert service.return_value.request_download.call_args.kwargs["config_version_kind"].value == "build_draft"
|
||||
|
||||
|
||||
def test_skill_inspect_happy_path_returns_service_payload() -> None:
|
||||
raw = _raw(AgentConfigSkillInspectApi.get)
|
||||
|
||||
with app.test_request_context(
|
||||
"/?tenant_id=tenant-1&user_id=user-1&config_version_id=cfg-1&config_version_kind=build_draft"
|
||||
):
|
||||
with patch(f"{MODULE}.AgentConfigService") as service:
|
||||
service.return_value.inspect_skill.return_value = {"name": "alpha", "files": ["SKILL.md"]}
|
||||
body = raw(AgentConfigSkillInspectApi(), "agent-1", "alpha")
|
||||
|
||||
assert body == {"name": "alpha", "files": ["SKILL.md"]}
|
||||
assert service.return_value.inspect_skill.call_args.kwargs["user_id"] == "user-1"
|
||||
|
||||
|
||||
def test_push_happy_path_validates_body_and_preserves_execution_user() -> None:
|
||||
raw = _raw(AgentConfigPushApi.post)
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "account-user-1",
|
||||
"config_version_id": "cfg-1",
|
||||
"config_version_kind": "build_draft",
|
||||
"files": [{"name": "guide.txt", "file_ref": {"kind": "tool_file", "id": "tool-file-1"}}],
|
||||
"skills": [],
|
||||
"env_text": "KEY=value\n",
|
||||
"note": "hello",
|
||||
}
|
||||
|
||||
with app.test_request_context("/", method="POST", json=payload):
|
||||
with patch(f"{MODULE}.AgentConfigService") as service:
|
||||
service.return_value.push.return_value = {
|
||||
"agent_id": "agent-1",
|
||||
"config_version": {"id": "cfg-1", "kind": "build_draft", "writable": True},
|
||||
"skills": {"items": []},
|
||||
"files": {"items": []},
|
||||
"env_keys": ["KEY"],
|
||||
"note": "hello",
|
||||
}
|
||||
body = raw(AgentConfigPushApi(), "agent-1")
|
||||
|
||||
assert body == {
|
||||
"agent_id": "agent-1",
|
||||
"config_version": {"id": "cfg-1", "kind": "build_draft", "writable": True},
|
||||
"skills": {"items": []},
|
||||
"files": {"items": []},
|
||||
"env_keys": ["KEY"],
|
||||
"note": "hello",
|
||||
}
|
||||
assert service.return_value.push.call_args.kwargs["user_id"] == "account-user-1"
|
||||
assert service.return_value.push.call_args.kwargs["config_version_kind"].value == "build_draft"
|
||||
|
||||
|
||||
def test_env_happy_path_calls_service() -> None:
|
||||
raw = _raw(AgentConfigEnvApi.patch)
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "account-user-1",
|
||||
"config_version_id": "cfg-1",
|
||||
"config_version_kind": "build_draft",
|
||||
"env_text": "KEY=value\n",
|
||||
}
|
||||
|
||||
with app.test_request_context("/", method="PATCH", json=payload):
|
||||
with patch(f"{MODULE}.AgentConfigService") as service:
|
||||
service.return_value.update_env.return_value = {"env_keys": ["KEY"]}
|
||||
body = raw(AgentConfigEnvApi(), "agent-1")
|
||||
|
||||
assert body == {"env_keys": ["KEY"]}
|
||||
assert service.return_value.update_env.call_args.kwargs["user_id"] == "account-user-1"
|
||||
assert service.return_value.update_env.call_args.kwargs["env_text"] == "KEY=value\n"
|
||||
|
||||
|
||||
def test_note_happy_path_calls_service() -> None:
|
||||
raw = _raw(AgentConfigNoteApi.put)
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "account-user-1",
|
||||
"config_version_id": "cfg-1",
|
||||
"config_version_kind": "build_draft",
|
||||
"note": "hello",
|
||||
}
|
||||
|
||||
with app.test_request_context("/", method="PUT", json=payload):
|
||||
with patch(f"{MODULE}.AgentConfigService") as service:
|
||||
service.return_value.update_note.return_value = {"note": "hello"}
|
||||
body = raw(AgentConfigNoteApi(), "agent-1")
|
||||
|
||||
assert body == {"note": "hello"}
|
||||
assert service.return_value.update_note.call_args.kwargs["user_id"] == "account-user-1"
|
||||
assert service.return_value.update_note.call_args.kwargs["note"] == "hello"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("prompt", [" 原文 [§file:guide.md:Guide§]\r\n\n", ""])
|
||||
def test_prompt_reads_and_replaces_raw_text(prompt: str) -> None:
|
||||
target = {
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "account-user-1",
|
||||
"config_version_id": "cfg-1",
|
||||
"config_version_kind": "build_draft",
|
||||
}
|
||||
with patch(f"{MODULE}.AgentConfigService") as service:
|
||||
service.return_value.get_prompt.return_value = {"prompt": prompt}
|
||||
service.return_value.update_prompt.return_value = {"prompt": prompt}
|
||||
with app.test_request_context("/", query_string=target):
|
||||
assert _raw(AgentConfigPromptApi.get)(AgentConfigPromptApi(), "agent-1") == {"prompt": prompt}
|
||||
with app.test_request_context("/", method="PUT", json={**target, "prompt": prompt}):
|
||||
assert _raw(AgentConfigPromptApi.put)(AgentConfigPromptApi(), "agent-1") == {"prompt": prompt}
|
||||
|
||||
assert service.return_value.get_prompt.call_args.kwargs == {**target, "agent_id": "agent-1"}
|
||||
assert service.return_value.update_prompt.call_args.kwargs == {**target, "agent_id": "agent-1", "prompt": prompt}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("prompt_body", [{}, {"prompt": None}, {"prompt": 123}])
|
||||
def test_prompt_rejects_missing_null_or_non_string_text(prompt_body: dict[str, object]) -> None:
|
||||
with (
|
||||
app.test_request_context(
|
||||
"/",
|
||||
method="PUT",
|
||||
json={
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "user-1",
|
||||
"config_version_id": "cfg-1",
|
||||
"config_version_kind": "build_draft",
|
||||
**prompt_body,
|
||||
},
|
||||
),
|
||||
patch(f"{MODULE}.AgentConfigService") as service,
|
||||
):
|
||||
with pytest.raises(RelayedHTTPError) as exc_info:
|
||||
_raw(AgentConfigPromptApi.put)(AgentConfigPromptApi(), "agent-1")
|
||||
assert exc_info.value.code == 400
|
||||
assert exc_info.value.error_code == "invalid_request"
|
||||
service.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("method", "error_code", "status"), [("get", "config_version_not_found", 404), ("put", "config_not_writable", 403)]
|
||||
)
|
||||
def test_prompt_http_routes_require_inner_key_and_serialize_errors(
|
||||
method: str, error_code: str, status: int, monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
|
||||
) -> None:
|
||||
from controllers.inner_api import bp
|
||||
|
||||
config_overrides(PLUGIN_DAEMON_KEY="enabled", INNER_API_KEY_FOR_PLUGIN="prompt-secret")
|
||||
monkeypatch.setattr("controllers.console.wraps._is_setup_completed", lambda: True)
|
||||
served_app = Flask(__name__)
|
||||
served_app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False)
|
||||
served_app.register_blueprint(bp)
|
||||
client = served_app.test_client()
|
||||
path = "/inner/api/agent-config/agent-1/prompt"
|
||||
target = {
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "user-1",
|
||||
"config_version_id": "cfg-1",
|
||||
"config_version_kind": "build_draft",
|
||||
}
|
||||
body = {**target, "prompt": "raw [§skill:research:Research§]\n"}
|
||||
with patch(f"{MODULE}.AgentConfigService") as service:
|
||||
service.return_value.get_prompt.return_value = {"prompt": body["prompt"]}
|
||||
service.return_value.update_prompt.return_value = {"prompt": body["prompt"]}
|
||||
unauthorized_headers: list[dict[str, str]] = [{}, {"X-Inner-Api-Key": "wrong"}]
|
||||
for headers in unauthorized_headers:
|
||||
response = client.open(path, method=method.upper(), headers=headers, query_string=target, json=body)
|
||||
assert response.status_code == 404
|
||||
service.assert_not_called()
|
||||
headers = {"X-Inner-Api-Key": "prompt-secret"}
|
||||
response = client.open(path, method=method.upper(), headers=headers, query_string=target, json=body)
|
||||
assert response.status_code == 200
|
||||
assert response.json == {"prompt": body["prompt"]}
|
||||
error = AgentConfigServiceError(error_code, "config unavailable", status_code=status)
|
||||
service.return_value.get_prompt.side_effect = error
|
||||
service.return_value.update_prompt.side_effect = error
|
||||
response = client.open(path, method=method.upper(), headers=headers, query_string=target, json=body)
|
||||
assert response.status_code == status
|
||||
assert response.json == {"code": error_code, "message": "config unavailable", "status": status}
|
||||
|
||||
|
||||
def test_manifest_invalid_query_returns_400() -> None:
|
||||
raw = _raw(AgentConfigManifestApi.get)
|
||||
|
||||
with app.test_request_context("/?tenant_id=tenant-1"):
|
||||
body, status = raw(AgentConfigManifestApi(), "agent-1")
|
||||
|
||||
assert status == 400
|
||||
assert body["code"] == "invalid_request"
|
||||
|
||||
|
||||
def test_download_request_rejects_invalid_config_name() -> None:
|
||||
raw = _raw(AgentConfigDownloadRequestApi.post)
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"config_version_id": "cfg-1",
|
||||
"config_version_kind": "draft",
|
||||
"config": {"kind": "file", "name": "../guide.txt"},
|
||||
}
|
||||
|
||||
with app.test_request_context("/", method="POST", json=payload):
|
||||
body, status = raw(AgentConfigDownloadRequestApi(), "agent-1")
|
||||
|
||||
assert status == 400
|
||||
assert body["code"] == "invalid_request"
|
||||
|
||||
|
||||
def test_push_invalid_body_returns_400() -> None:
|
||||
raw = _raw(AgentConfigPushApi.post)
|
||||
|
||||
with app.test_request_context("/", method="POST", json={"tenant_id": "tenant-1"}):
|
||||
body, status = raw(AgentConfigPushApi(), "agent-1")
|
||||
|
||||
assert status == 400
|
||||
assert body["code"] == "invalid_request"
|
||||
|
||||
|
||||
def test_manifest_maps_service_errors() -> None:
|
||||
raw = _raw(AgentConfigManifestApi.get)
|
||||
|
||||
with app.test_request_context("/?tenant_id=tenant-1&config_version_id=cfg-1&config_version_kind=build_draft"):
|
||||
with patch(f"{MODULE}.AgentConfigService") as service:
|
||||
service.return_value.manifest.side_effect = AgentConfigServiceError(
|
||||
"config_version_not_found",
|
||||
"missing",
|
||||
status_code=404,
|
||||
)
|
||||
body, status = raw(AgentConfigManifestApi(), "agent-1")
|
||||
|
||||
assert status == 404
|
||||
assert body == {"code": "config_version_not_found", "message": "missing"}
|
||||
|
||||
|
||||
def test_push_maps_service_errors() -> None:
|
||||
raw = _raw(AgentConfigPushApi.post)
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"user_id": "account-user-1",
|
||||
"config_version_id": "cfg-1",
|
||||
"config_version_kind": "build_draft",
|
||||
"files": [],
|
||||
"skills": [],
|
||||
}
|
||||
|
||||
with app.test_request_context("/", method="POST", json=payload):
|
||||
with patch(f"{MODULE}.AgentConfigService") as service:
|
||||
service.return_value.push.side_effect = AgentConfigServiceError(
|
||||
"config_not_writable",
|
||||
"denied",
|
||||
status_code=403,
|
||||
)
|
||||
body, status = raw(AgentConfigPushApi(), "agent-1")
|
||||
|
||||
assert status == 403
|
||||
assert body == {"code": "config_not_writable", "message": "denied"}
|
||||
@@ -0,0 +1,553 @@
|
||||
"""
|
||||
Unit tests for inner_api plugin endpoints
|
||||
|
||||
Tests endpoint structure (method existence) for all plugin APIs, plus
|
||||
handler-level logic tests for representative non-streaming endpoints.
|
||||
Auth/setup decorators are tested separately in test_auth_wraps.py;
|
||||
handler tests use inspect.unwrap() to bypass them.
|
||||
"""
|
||||
|
||||
import inspect
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from controllers.inner_api.plugin import plugin as plugin_module
|
||||
from controllers.inner_api.plugin.plugin import (
|
||||
PluginDownloadFileRequestApi,
|
||||
PluginFetchAppInfoApi,
|
||||
PluginInvokeAppApi,
|
||||
PluginInvokeDatasourceApi,
|
||||
PluginInvokeEncryptApi,
|
||||
PluginInvokeLLMApi,
|
||||
PluginInvokeLLMWithStructuredOutputApi,
|
||||
PluginInvokeModerationApi,
|
||||
PluginInvokeMultimodalEmbeddingApi,
|
||||
PluginInvokeParameterExtractorNodeApi,
|
||||
PluginInvokeQuestionClassifierNodeApi,
|
||||
PluginInvokeRerankApi,
|
||||
PluginInvokeSpeech2TextApi,
|
||||
PluginInvokeSummaryApi,
|
||||
PluginInvokeTextEmbeddingApi,
|
||||
PluginInvokeToolApi,
|
||||
PluginInvokeTTSApi,
|
||||
PluginModelCatalogApi,
|
||||
PluginUploadFileRequestApi,
|
||||
)
|
||||
from core.datasource.entities.datasource_entities import DatasourceProviderType
|
||||
from core.plugin.entities.request import RequestInvokeDatasource, RequestInvokeTextEmbedding, RequestListModels
|
||||
from core.workflow.file_reference import build_file_reference
|
||||
from graphon.model_runtime.entities.model_entities import ModelType
|
||||
from models import Account, Tenant
|
||||
from tests.helpers.unit.config_override import apply_config_overrides
|
||||
from tests.helpers.unit.model_factories import make_tenant
|
||||
|
||||
|
||||
def _tenant() -> Tenant:
|
||||
return make_tenant(tenant_id="tenant-id", name="Test Tenant")
|
||||
|
||||
|
||||
def _user() -> Account:
|
||||
user = Account(name="Test User", email="user@example.com")
|
||||
user.id = "user-id"
|
||||
return user
|
||||
|
||||
|
||||
def _text_embedding_payload() -> RequestInvokeTextEmbedding:
|
||||
return RequestInvokeTextEmbedding(provider="openai", model="text-embedding-3-small", texts=["hello"])
|
||||
|
||||
|
||||
def _extract_raw_post(cls):
|
||||
"""Extract the raw post() method from a plugin endpoint class.
|
||||
|
||||
Plugin endpoint methods are wrapped by several decorators (get_user_tenant,
|
||||
setup_required, plugin_inner_api_only, plugin_data). These decorators
|
||||
use @wraps where possible. This helper ensures we retrieve the original
|
||||
post(self, user_model, tenant_model, payload) function by unwrapping
|
||||
and, if necessary, walking the closure of the innermost wrapper.
|
||||
"""
|
||||
bottom = inspect.unwrap(cls.post)
|
||||
|
||||
# If unwrap() didn't get us to the raw function (e.g. if a decorator
|
||||
# missed @wraps), try to extract it from the closure if it looks like
|
||||
# a plugin_data or similar wrapper that closes over 'view_func'.
|
||||
if hasattr(bottom, "__code__") and "view_func" in bottom.__code__.co_freevars:
|
||||
try:
|
||||
idx = bottom.__code__.co_freevars.index("view_func")
|
||||
return bottom.__closure__[idx].cell_contents
|
||||
except (AttributeError, TypeError, IndexError):
|
||||
pass
|
||||
|
||||
return bottom
|
||||
|
||||
|
||||
class TestPluginInvokeLLMApi:
|
||||
"""Test PluginInvokeLLMApi endpoint structure"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeLLMApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
"""Test that endpoint has post method"""
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
|
||||
class TestPluginInvokeLLMWithStructuredOutputApi:
|
||||
"""Test PluginInvokeLLMWithStructuredOutputApi endpoint"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeLLMWithStructuredOutputApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
|
||||
class TestPluginInvokeTextEmbeddingApi:
|
||||
"""Test PluginInvokeTextEmbeddingApi endpoint"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeTextEmbeddingApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
@patch("controllers.inner_api.plugin.plugin.PluginModelBackwardsInvocation")
|
||||
def test_post_classifies_rate_limit_for_internal_consumers(self, mock_invocation, api_instance):
|
||||
mock_invocation.invoke_text_embedding.side_effect = RuntimeError(
|
||||
"Connection Error, 429 RESOURCE_EXHAUSTED. Please try again later."
|
||||
)
|
||||
raw_post = _extract_raw_post(PluginInvokeTextEmbeddingApi)
|
||||
|
||||
result = raw_post(
|
||||
api_instance,
|
||||
user_model=_user(),
|
||||
tenant_model=_tenant(),
|
||||
payload=_text_embedding_payload(),
|
||||
)
|
||||
|
||||
assert result == {
|
||||
"data": None,
|
||||
"error": "Connection Error, 429 RESOURCE_EXHAUSTED. Please try again later.",
|
||||
"error_code": "rate_limited",
|
||||
"retryable": True,
|
||||
"status": 429,
|
||||
}
|
||||
|
||||
@patch("controllers.inner_api.plugin.plugin.PluginModelBackwardsInvocation")
|
||||
def test_post_keeps_unknown_failures_non_retryable(self, mock_invocation, api_instance):
|
||||
mock_invocation.invoke_text_embedding.side_effect = ValueError("invalid model input")
|
||||
raw_post = _extract_raw_post(PluginInvokeTextEmbeddingApi)
|
||||
|
||||
result = raw_post(
|
||||
api_instance,
|
||||
user_model=_user(),
|
||||
tenant_model=_tenant(),
|
||||
payload=_text_embedding_payload(),
|
||||
)
|
||||
|
||||
assert result == {
|
||||
"data": None,
|
||||
"error": "invalid model input",
|
||||
"error_code": "invocation_failed",
|
||||
"retryable": False,
|
||||
}
|
||||
|
||||
|
||||
class TestPluginInvokeMultimodalEmbeddingApi:
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeMultimodalEmbeddingApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
|
||||
class TestPluginInvokeRerankApi:
|
||||
"""Test PluginInvokeRerankApi endpoint"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeRerankApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
|
||||
class TestPluginModelCatalogApi:
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginModelCatalogApi()
|
||||
|
||||
@patch("controllers.inner_api.plugin.plugin.PluginModelBackwardsInvocation")
|
||||
def test_post_lists_models_in_the_resolved_tenant_scope(self, mock_invocation, api_instance, app: Flask):
|
||||
mock_invocation.list_models.return_value = {"items": [], "next_offset": None}
|
||||
mock_tenant = _tenant()
|
||||
mock_user = _user()
|
||||
mock_payload = RequestListModels(model_type=ModelType.LLM)
|
||||
|
||||
raw_post = _extract_raw_post(PluginModelCatalogApi)
|
||||
result = raw_post(
|
||||
api_instance,
|
||||
user_model=mock_user,
|
||||
tenant_model=mock_tenant,
|
||||
payload=mock_payload,
|
||||
)
|
||||
|
||||
mock_invocation.list_models.assert_called_once_with(
|
||||
tenant_id="tenant-id",
|
||||
user_id="user-id",
|
||||
payload=mock_payload,
|
||||
)
|
||||
assert result == {"data": {"items": [], "next_offset": None}, "error": ""}
|
||||
|
||||
|
||||
class TestPluginInvokeDatasourceApi:
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeDatasourceApi()
|
||||
|
||||
@patch("controllers.inner_api.plugin.plugin.length_prefixed_response")
|
||||
@patch("controllers.inner_api.plugin.plugin.PluginDatasourceBackwardsInvocation")
|
||||
def test_post_delegates_to_dify_datasource_runtime(
|
||||
self,
|
||||
mock_invocation,
|
||||
mock_length_prefixed_response,
|
||||
api_instance,
|
||||
app: Flask,
|
||||
):
|
||||
mock_tenant = _tenant()
|
||||
mock_user = _user()
|
||||
mock_payload = RequestInvokeDatasource(
|
||||
tenant_id="tenant-id",
|
||||
user_id="user-id",
|
||||
provider="langgenius/notion_datasource/notion_datasource",
|
||||
datasource="notion_datasource",
|
||||
datasource_type=DatasourceProviderType.ONLINE_DOCUMENT,
|
||||
credential_id="credential-id",
|
||||
operation="get_online_document_pages",
|
||||
)
|
||||
invocation = object()
|
||||
stream = object()
|
||||
mock_invocation.invoke.return_value = invocation
|
||||
mock_invocation.convert_to_event_stream.return_value = stream
|
||||
mock_length_prefixed_response.return_value = "response"
|
||||
|
||||
raw_post = _extract_raw_post(PluginInvokeDatasourceApi)
|
||||
result = raw_post(
|
||||
api_instance,
|
||||
user_model=mock_user,
|
||||
tenant_model=mock_tenant,
|
||||
payload=mock_payload,
|
||||
)
|
||||
|
||||
assert result == "response"
|
||||
mock_invocation.invoke.assert_called_once_with(
|
||||
user_id="user-id",
|
||||
tenant=mock_tenant,
|
||||
payload=mock_payload,
|
||||
)
|
||||
mock_invocation.convert_to_event_stream.assert_called_once_with(invocation)
|
||||
mock_length_prefixed_response.assert_called_once_with(0xF, stream)
|
||||
|
||||
|
||||
class TestPluginInvokeTTSApi:
|
||||
"""Test PluginInvokeTTSApi endpoint"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeTTSApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
|
||||
class TestPluginInvokeSpeech2TextApi:
|
||||
"""Test PluginInvokeSpeech2TextApi endpoint"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeSpeech2TextApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
|
||||
class TestPluginInvokeModerationApi:
|
||||
"""Test PluginInvokeModerationApi endpoint"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeModerationApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
|
||||
class TestPluginInvokeToolApi:
|
||||
"""Test PluginInvokeToolApi endpoint"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeToolApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
|
||||
class TestPluginInvokeParameterExtractorNodeApi:
|
||||
"""Test PluginInvokeParameterExtractorNodeApi endpoint"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeParameterExtractorNodeApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
|
||||
class TestPluginInvokeQuestionClassifierNodeApi:
|
||||
"""Test PluginInvokeQuestionClassifierNodeApi endpoint"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeQuestionClassifierNodeApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
|
||||
class TestPluginInvokeAppApi:
|
||||
"""Test PluginInvokeAppApi endpoint"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeAppApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
|
||||
class TestPluginInvokeEncryptApi:
|
||||
"""Test PluginInvokeEncryptApi endpoint structure and handler logic"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeEncryptApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
@patch("controllers.inner_api.plugin.plugin.PluginEncrypter")
|
||||
def test_post_returns_encrypted_data(self, mock_encrypter, api_instance, app: Flask):
|
||||
"""Test that post() delegates to PluginEncrypter and returns model_dump output"""
|
||||
# Arrange
|
||||
mock_encrypter.invoke_encrypt.return_value = {"encrypted": "data"}
|
||||
tenant = _tenant()
|
||||
user = _user()
|
||||
mock_payload = MagicMock()
|
||||
|
||||
# Act — extract raw post() bypassing all decorators including plugin_data
|
||||
raw_post = _extract_raw_post(PluginInvokeEncryptApi)
|
||||
result = raw_post(api_instance, user_model=user, tenant_model=tenant, payload=mock_payload)
|
||||
|
||||
# Assert
|
||||
mock_encrypter.invoke_encrypt.assert_called_once_with(tenant, mock_payload)
|
||||
assert result["data"] == {"encrypted": "data"}
|
||||
assert result.get("error") == ""
|
||||
|
||||
@patch("controllers.inner_api.plugin.plugin.PluginEncrypter")
|
||||
def test_post_returns_error_on_exception(self, mock_encrypter, api_instance, app: Flask):
|
||||
"""Test that post() catches exceptions and returns error response"""
|
||||
# Arrange
|
||||
mock_encrypter.invoke_encrypt.side_effect = RuntimeError("encrypt failed")
|
||||
tenant = _tenant()
|
||||
user = _user()
|
||||
mock_payload = MagicMock()
|
||||
|
||||
# Act
|
||||
raw_post = _extract_raw_post(PluginInvokeEncryptApi)
|
||||
result = raw_post(api_instance, user_model=user, tenant_model=tenant, payload=mock_payload)
|
||||
|
||||
# Assert
|
||||
assert "encrypt failed" in result["error"]
|
||||
|
||||
|
||||
class TestPluginInvokeSummaryApi:
|
||||
"""Test PluginInvokeSummaryApi endpoint"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginInvokeSummaryApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
|
||||
class TestPluginUploadFileRequestApi:
|
||||
"""Test PluginUploadFileRequestApi endpoint structure and handler logic"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginUploadFileRequestApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
@patch("controllers.inner_api.plugin.plugin.get_signed_file_uri_for_plugin")
|
||||
def test_post_returns_signed_url(self, mock_get_uri, api_instance, app: Flask, monkeypatch: pytest.MonkeyPatch):
|
||||
"""Test that post() generates a signed URL and returns it"""
|
||||
# Arrange
|
||||
mock_get_uri.return_value = "/files/upload/for-plugin?sign=1"
|
||||
apply_config_overrides(monkeypatch, INTERNAL_FILES_URL="http://api:5001")
|
||||
tenant = _tenant()
|
||||
user = _user()
|
||||
mock_payload = MagicMock()
|
||||
mock_payload.filename = "test.pdf"
|
||||
mock_payload.mimetype = "application/pdf"
|
||||
mock_payload.conversation_id = "conversation-id"
|
||||
|
||||
# Act
|
||||
raw_post = _extract_raw_post(PluginUploadFileRequestApi)
|
||||
result = raw_post(api_instance, user_model=user, tenant_model=tenant, payload=mock_payload)
|
||||
|
||||
# Assert
|
||||
mock_get_uri.assert_called_once_with(
|
||||
filename="test.pdf",
|
||||
mimetype="application/pdf",
|
||||
tenant_id="tenant-id",
|
||||
user_id="user-id",
|
||||
conversation_id="conversation-id",
|
||||
)
|
||||
assert result["data"]["url"] == "http://api:5001/files/upload/for-plugin?sign=1"
|
||||
|
||||
|
||||
class TestPluginDownloadFileRequestApi:
|
||||
"""Test PluginDownloadFileRequestApi endpoint structure and handler logic"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginDownloadFileRequestApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
@pytest.mark.parametrize("sqlite_session", [(Tenant,)], indirect=True)
|
||||
@pytest.mark.parametrize(
|
||||
("for_external", "expected_url"),
|
||||
[
|
||||
(True, "https://files.example.com/files/tools/report.pdf?sign=1"),
|
||||
(False, "http://api:5001/files/tools/report.pdf?sign=1"),
|
||||
],
|
||||
)
|
||||
@patch("controllers.inner_api.plugin.plugin.FileRequestService")
|
||||
def test_post_returns_signed_download_url(
|
||||
self,
|
||||
mock_service_cls,
|
||||
api_instance,
|
||||
app: Flask,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
sqlite_session: Session,
|
||||
for_external: bool,
|
||||
expected_url: str,
|
||||
):
|
||||
tenant = Tenant(
|
||||
name="Plugin Tenant",
|
||||
encrypt_public_key=None,
|
||||
plan="basic",
|
||||
custom_config=None,
|
||||
)
|
||||
tenant.id = "49a99e46-bc2c-4885-91fa-47615f6192b5"
|
||||
sqlite_session.add(tenant)
|
||||
sqlite_session.commit()
|
||||
monkeypatch.setattr(plugin_module.db, "session", sqlite_session)
|
||||
mock_service = mock_service_cls.return_value
|
||||
mock_service.request_download.return_value = MagicMock(
|
||||
filename="report.pdf",
|
||||
mime_type="application/pdf",
|
||||
size=123,
|
||||
download_uri="/files/tools/report.pdf?sign=1",
|
||||
)
|
||||
apply_config_overrides(
|
||||
monkeypatch,
|
||||
FILES_URL="https://files.example.com",
|
||||
INTERNAL_FILES_URL="http://api:5001",
|
||||
)
|
||||
mock_payload = MagicMock()
|
||||
mock_payload.tenant_id = tenant.id
|
||||
mock_payload.user_id = "user-id"
|
||||
mock_payload.user_from = "account"
|
||||
mock_payload.invoke_from = "debugger"
|
||||
mock_payload.for_external = for_external
|
||||
reference = build_file_reference(record_id="tool-file-1")
|
||||
mock_payload.file.model_dump.return_value = {
|
||||
"transfer_method": "tool_file",
|
||||
"reference": reference,
|
||||
}
|
||||
|
||||
raw_post = _extract_raw_post(PluginDownloadFileRequestApi)
|
||||
result = raw_post(api_instance, payload=mock_payload)
|
||||
|
||||
mock_service.request_download.assert_called_once_with(
|
||||
tenant_id=tenant.id,
|
||||
user_id="user-id",
|
||||
user_from="account",
|
||||
invoke_from="debugger",
|
||||
file_mapping={"transfer_method": "tool_file", "reference": reference},
|
||||
)
|
||||
assert result["data"] == {
|
||||
"filename": "report.pdf",
|
||||
"mime_type": "application/pdf",
|
||||
"size": 123,
|
||||
"download_url": expected_url,
|
||||
}
|
||||
|
||||
|
||||
class TestPluginFetchAppInfoApi:
|
||||
"""Test PluginFetchAppInfoApi endpoint structure and handler logic"""
|
||||
|
||||
@pytest.fixture
|
||||
def api_instance(self):
|
||||
return PluginFetchAppInfoApi()
|
||||
|
||||
def test_has_post_method(self, api_instance):
|
||||
assert hasattr(api_instance, "post")
|
||||
assert callable(api_instance.post)
|
||||
|
||||
@patch("controllers.inner_api.plugin.plugin.PluginAppBackwardsInvocation")
|
||||
def test_post_returns_app_info(self, mock_invocation, api_instance, app: Flask):
|
||||
"""Test that post() fetches app info and returns it"""
|
||||
# Arrange
|
||||
mock_invocation.fetch_app_info.return_value = {"app_name": "My App", "mode": "chat"}
|
||||
tenant = _tenant()
|
||||
user = _user()
|
||||
mock_payload = MagicMock()
|
||||
mock_payload.app_id = "app-123"
|
||||
|
||||
# Act
|
||||
raw_post = _extract_raw_post(PluginFetchAppInfoApi)
|
||||
result = raw_post(api_instance, user_model=user, tenant_model=tenant, payload=mock_payload)
|
||||
|
||||
# Assert
|
||||
mock_invocation.fetch_app_info.assert_called_once_with("app-123", "tenant-id")
|
||||
assert result["data"] == {"app_name": "My App", "mode": "chat"}
|
||||
@@ -0,0 +1,432 @@
|
||||
"""
|
||||
Unit tests for inner_api plugin decorators
|
||||
"""
|
||||
|
||||
from collections.abc import Iterator
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from pydantic import ValidationError
|
||||
from sqlalchemy import Engine, event, select
|
||||
from sqlalchemy.orm import Session, scoped_session, sessionmaker
|
||||
|
||||
from controllers.inner_api.plugin import wraps as wraps_module
|
||||
from controllers.inner_api.plugin.wraps import (
|
||||
TenantUserPayload,
|
||||
get_user,
|
||||
get_user_tenant,
|
||||
plugin_data,
|
||||
)
|
||||
from models.account import Tenant
|
||||
from models.base import TypeBase
|
||||
from models.enums import DEFAULT_END_USER_SESSION_ID, EndUserType
|
||||
from models.model import EndUser
|
||||
|
||||
_USER_UUID = "00000000-0000-4000-8000-000000000001"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sqlite_plugin_engine(
|
||||
sqlite_engine: Engine,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> Iterator[Engine]:
|
||||
tables = [TypeBase.metadata.tables[model.__tablename__] for model in (Tenant, EndUser)]
|
||||
TypeBase.metadata.create_all(sqlite_engine, tables=tables)
|
||||
session_registry = scoped_session(sessionmaker(bind=sqlite_engine, expire_on_commit=False))
|
||||
monkeypatch.setattr(
|
||||
wraps_module,
|
||||
"db",
|
||||
SimpleNamespace(engine=sqlite_engine, session=session_registry),
|
||||
)
|
||||
try:
|
||||
yield sqlite_engine
|
||||
finally:
|
||||
session_registry.remove()
|
||||
|
||||
|
||||
def _persist_tenant(sqlite_engine: Engine, *, tenant_id: str = "tenant123") -> Tenant:
|
||||
tenant = Tenant(name=f"Tenant {tenant_id}")
|
||||
tenant.id = tenant_id
|
||||
with Session(sqlite_engine) as session, session.begin():
|
||||
session.add(tenant)
|
||||
return tenant
|
||||
|
||||
|
||||
def _persist_end_user(
|
||||
sqlite_engine: Engine,
|
||||
*,
|
||||
tenant_id: str = "tenant123",
|
||||
user_id: str,
|
||||
session_id: str,
|
||||
is_anonymous: bool = False,
|
||||
) -> EndUser:
|
||||
user = EndUser(
|
||||
id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
type=EndUserType.SERVICE_API,
|
||||
_is_anonymous=is_anonymous,
|
||||
session_id=session_id,
|
||||
)
|
||||
with Session(sqlite_engine) as session, session.begin():
|
||||
session.add(user)
|
||||
return user
|
||||
|
||||
|
||||
class TestTenantUserPayload:
|
||||
"""Test TenantUserPayload Pydantic model"""
|
||||
|
||||
def test_valid_payload(self):
|
||||
"""Test valid payload passes validation"""
|
||||
data = {"tenant_id": "tenant123", "user_id": "user456"}
|
||||
payload = TenantUserPayload.model_validate(data)
|
||||
assert payload.tenant_id == "tenant123"
|
||||
assert payload.user_id == "user456"
|
||||
|
||||
def test_missing_tenant_id(self):
|
||||
"""Test missing tenant_id raises ValidationError"""
|
||||
with pytest.raises(ValidationError):
|
||||
TenantUserPayload.model_validate({"user_id": "user456"})
|
||||
|
||||
def test_missing_user_id(self):
|
||||
"""Test missing user_id raises ValidationError"""
|
||||
with pytest.raises(ValidationError):
|
||||
TenantUserPayload.model_validate({"tenant_id": "tenant123"})
|
||||
|
||||
|
||||
class TestGetUser:
|
||||
"""Test get_user function"""
|
||||
|
||||
def test_should_return_existing_user_by_id(self, sqlite_plugin_engine: Engine, app: Flask):
|
||||
"""Test returning existing user when found by ID"""
|
||||
_persist_end_user(
|
||||
sqlite_plugin_engine,
|
||||
user_id=_USER_UUID,
|
||||
session_id="existing-session",
|
||||
)
|
||||
|
||||
with app.app_context():
|
||||
result = get_user("tenant123", _USER_UUID)
|
||||
|
||||
assert result.id == _USER_UUID
|
||||
assert result.tenant_id == "tenant123"
|
||||
|
||||
def test_should_not_resolve_non_anonymous_users_across_tenants(
|
||||
self,
|
||||
sqlite_plugin_engine: Engine,
|
||||
app: Flask,
|
||||
):
|
||||
"""Test that explicit user IDs remain scoped to the current tenant."""
|
||||
_persist_end_user(
|
||||
sqlite_plugin_engine,
|
||||
tenant_id="tenant-foreign",
|
||||
user_id="foreign-user-id",
|
||||
session_id="foreign-session",
|
||||
)
|
||||
|
||||
with app.app_context():
|
||||
result = get_user("tenant-current", "foreign-user-id")
|
||||
|
||||
assert result.id != "foreign-user-id"
|
||||
assert result.tenant_id == "tenant-current"
|
||||
assert result.session_id == "foreign-user-id"
|
||||
with Session(sqlite_plugin_engine) as session:
|
||||
current_tenant_users = session.scalars(select(EndUser).where(EndUser.tenant_id == "tenant-current")).all()
|
||||
assert [user.id for user in current_tenant_users] == [result.id]
|
||||
|
||||
def test_should_return_existing_user_by_session_id_fallback_for_non_anonymous(
|
||||
self,
|
||||
sqlite_plugin_engine: Engine,
|
||||
app: Flask,
|
||||
):
|
||||
"""Non-anonymous user_id misses on EndUser.id but hits on
|
||||
EndUser.session_id — this is the plugin-daemon Reverse Invocation
|
||||
case where the daemon sends a stable session-derived UUID that
|
||||
was written into session_id on the first call. See #36736.
|
||||
"""
|
||||
_persist_end_user(
|
||||
sqlite_plugin_engine,
|
||||
user_id="persisted-user-id",
|
||||
session_id="daemon-session-uuid",
|
||||
)
|
||||
|
||||
with app.app_context():
|
||||
result = get_user("tenant123", "daemon-session-uuid")
|
||||
|
||||
assert result.id == "persisted-user-id"
|
||||
with Session(sqlite_plugin_engine) as session:
|
||||
users = session.scalars(select(EndUser)).all()
|
||||
assert [user.id for user in users] == ["persisted-user-id"]
|
||||
|
||||
def test_should_skip_uuid_id_lookup_for_text_session_id(
|
||||
self,
|
||||
sqlite_plugin_engine: Engine,
|
||||
app: Flask,
|
||||
):
|
||||
"""Service actor names must not be compared with the UUID primary key."""
|
||||
_persist_end_user(
|
||||
sqlite_plugin_engine,
|
||||
user_id="persisted-user-id",
|
||||
session_id="knowledge-fs",
|
||||
)
|
||||
statements: list[str] = []
|
||||
|
||||
def _record_statement(
|
||||
_connection: object,
|
||||
_cursor: object,
|
||||
statement: str,
|
||||
_parameters: object,
|
||||
_context: object,
|
||||
_executemany: bool,
|
||||
) -> None:
|
||||
statements.append(statement)
|
||||
|
||||
event.listen(sqlite_plugin_engine, "before_cursor_execute", _record_statement)
|
||||
try:
|
||||
with app.app_context():
|
||||
result = get_user("tenant123", "knowledge-fs")
|
||||
finally:
|
||||
event.remove(sqlite_plugin_engine, "before_cursor_execute", _record_statement)
|
||||
|
||||
assert result.id == "persisted-user-id"
|
||||
assert not any("WHERE end_users.id =" in statement for statement in statements)
|
||||
|
||||
def test_should_return_existing_anonymous_user_by_session_id(
|
||||
self,
|
||||
sqlite_plugin_engine: Engine,
|
||||
app: Flask,
|
||||
):
|
||||
"""Test returning existing anonymous user by session_id"""
|
||||
_persist_end_user(
|
||||
sqlite_plugin_engine,
|
||||
user_id="anonymous-user-id",
|
||||
session_id="anonymous_session",
|
||||
is_anonymous=True,
|
||||
)
|
||||
|
||||
with app.app_context():
|
||||
result = get_user("tenant123", "anonymous_session")
|
||||
|
||||
assert result.id == "anonymous-user-id"
|
||||
|
||||
def test_should_create_new_user_when_not_found(
|
||||
self,
|
||||
sqlite_plugin_engine: Engine,
|
||||
app: Flask,
|
||||
):
|
||||
"""Test creating new user when not found in database"""
|
||||
with app.app_context():
|
||||
result = get_user("tenant123", "user123")
|
||||
|
||||
assert result.tenant_id == "tenant123"
|
||||
assert result.session_id == "user123"
|
||||
with Session(sqlite_plugin_engine) as session:
|
||||
persisted_user = session.get(EndUser, result.id)
|
||||
assert persisted_user is not None
|
||||
assert persisted_user.session_id == "user123"
|
||||
|
||||
def test_should_use_default_session_id_when_user_id_none(
|
||||
self,
|
||||
sqlite_plugin_engine: Engine,
|
||||
app: Flask,
|
||||
):
|
||||
"""Test using default session ID when user_id is None"""
|
||||
_persist_end_user(
|
||||
sqlite_plugin_engine,
|
||||
user_id="default-user-id",
|
||||
session_id=DEFAULT_END_USER_SESSION_ID,
|
||||
is_anonymous=True,
|
||||
)
|
||||
|
||||
with app.app_context():
|
||||
result = get_user("tenant123", None)
|
||||
|
||||
assert result.id == "default-user-id"
|
||||
assert result.session_id == DEFAULT_END_USER_SESSION_ID
|
||||
|
||||
def test_should_raise_error_on_database_exception(self, sqlite_plugin_engine: Engine, app: Flask):
|
||||
"""Test raising ValueError when database operation fails"""
|
||||
|
||||
def _raise_database_error(*_args, **_kwargs):
|
||||
raise RuntimeError("Database error")
|
||||
|
||||
event.listen(sqlite_plugin_engine, "before_cursor_execute", _raise_database_error)
|
||||
try:
|
||||
with app.app_context(), pytest.raises(ValueError, match="user not found") as exc_info:
|
||||
get_user("tenant123", "user123")
|
||||
# PEP 3134: the re-raised ValueError must chain the underlying database error.
|
||||
assert isinstance(exc_info.value.__cause__, RuntimeError)
|
||||
assert str(exc_info.value.__cause__) == "Database error"
|
||||
finally:
|
||||
event.remove(sqlite_plugin_engine, "before_cursor_execute", _raise_database_error)
|
||||
|
||||
|
||||
class TestGetUserTenant:
|
||||
"""Test get_user_tenant decorator"""
|
||||
|
||||
def test_should_inject_tenant_and_user_models(
|
||||
self,
|
||||
sqlite_plugin_engine: Engine,
|
||||
app: Flask,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
):
|
||||
"""Test that decorator injects tenant_model and user_model into kwargs"""
|
||||
|
||||
# Arrange
|
||||
@get_user_tenant
|
||||
def protected_view(tenant_model, user_model, **kwargs):
|
||||
return {"tenant": tenant_model, "user": user_model}
|
||||
|
||||
_persist_tenant(sqlite_plugin_engine)
|
||||
_persist_end_user(
|
||||
sqlite_plugin_engine,
|
||||
user_id=_USER_UUID,
|
||||
session_id="user-session",
|
||||
)
|
||||
|
||||
with app.test_request_context(json={"tenant_id": "tenant123", "user_id": _USER_UUID}):
|
||||
monkeypatch.setattr(app, "login_manager", MagicMock(), raising=False)
|
||||
with patch("controllers.inner_api.plugin.wraps.user_logged_in"):
|
||||
result = protected_view()
|
||||
|
||||
assert result["tenant"].id == "tenant123"
|
||||
assert result["user"].id == _USER_UUID
|
||||
|
||||
def test_should_raise_error_when_tenant_id_missing(self, app: Flask):
|
||||
"""Test that Pydantic ValidationError is raised when tenant_id is missing from payload"""
|
||||
|
||||
# Arrange
|
||||
@get_user_tenant
|
||||
def protected_view(tenant_model, user_model, **kwargs):
|
||||
return "success"
|
||||
|
||||
# Act & Assert - Pydantic validates payload before manual check
|
||||
with app.test_request_context(json={"user_id": "user456"}):
|
||||
with pytest.raises(ValidationError):
|
||||
protected_view()
|
||||
|
||||
def test_should_raise_error_when_tenant_not_found(self, sqlite_plugin_engine: Engine, app: Flask):
|
||||
"""Test that ValueError is raised when tenant is not found"""
|
||||
|
||||
# Arrange
|
||||
@get_user_tenant
|
||||
def protected_view(tenant_model, user_model, **kwargs):
|
||||
return "success"
|
||||
|
||||
with app.test_request_context(json={"tenant_id": "nonexistent", "user_id": "user456"}):
|
||||
with pytest.raises(ValueError, match="tenant not found"):
|
||||
protected_view()
|
||||
|
||||
def test_should_use_default_session_id_when_user_id_empty(
|
||||
self,
|
||||
sqlite_plugin_engine: Engine,
|
||||
app: Flask,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
):
|
||||
"""Test that default session ID is used when user_id is empty string"""
|
||||
|
||||
# Arrange
|
||||
@get_user_tenant
|
||||
def protected_view(tenant_model, user_model, **kwargs):
|
||||
return {"tenant": tenant_model, "user": user_model}
|
||||
|
||||
_persist_tenant(sqlite_plugin_engine)
|
||||
_persist_end_user(
|
||||
sqlite_plugin_engine,
|
||||
user_id="default-user-id",
|
||||
session_id=DEFAULT_END_USER_SESSION_ID,
|
||||
is_anonymous=True,
|
||||
)
|
||||
|
||||
with app.test_request_context(json={"tenant_id": "tenant123", "user_id": ""}):
|
||||
monkeypatch.setattr(app, "login_manager", MagicMock(), raising=False)
|
||||
with patch("controllers.inner_api.plugin.wraps.user_logged_in"):
|
||||
result = protected_view()
|
||||
|
||||
assert result["tenant"].id == "tenant123"
|
||||
assert result["user"].id == "default-user-id"
|
||||
assert result["user"].session_id == DEFAULT_END_USER_SESSION_ID
|
||||
|
||||
|
||||
class PluginTestPayload:
|
||||
"""Simple test payload class"""
|
||||
|
||||
def __init__(self, data: dict[str, Any]):
|
||||
self.value = data.get("value")
|
||||
|
||||
@classmethod
|
||||
def model_validate(cls, data: dict[str, Any]):
|
||||
return cls(data)
|
||||
|
||||
|
||||
class TestPluginData:
|
||||
"""Test plugin_data decorator"""
|
||||
|
||||
def test_should_inject_valid_payload(self, app: Flask):
|
||||
"""Test that valid payload is injected into kwargs"""
|
||||
|
||||
# Arrange
|
||||
@plugin_data(payload_type=PluginTestPayload)
|
||||
def protected_view(payload, **kwargs):
|
||||
return payload
|
||||
|
||||
# Act
|
||||
with app.test_request_context(json={"value": "test_data"}):
|
||||
result = protected_view()
|
||||
|
||||
# Assert
|
||||
assert result.value == "test_data"
|
||||
|
||||
def test_should_raise_error_on_invalid_json(self, app: Flask):
|
||||
"""Test that ValueError is raised when JSON parsing fails"""
|
||||
|
||||
# Arrange
|
||||
@plugin_data(payload_type=PluginTestPayload)
|
||||
def protected_view(payload, **kwargs):
|
||||
return payload
|
||||
|
||||
# Act & Assert - Malformed JSON triggers ValueError
|
||||
with app.test_request_context(data="not valid json", content_type="application/json"):
|
||||
with pytest.raises(ValueError, match="invalid json") as exc_info:
|
||||
protected_view()
|
||||
# PEP 3134: the re-raised ValueError must chain the underlying JSON parse failure.
|
||||
assert exc_info.value.__cause__ is not None
|
||||
assert isinstance(exc_info.value.__cause__, Exception)
|
||||
|
||||
def test_should_raise_error_on_invalid_payload(self, app: Flask):
|
||||
"""Test that ValueError is raised when payload validation fails"""
|
||||
|
||||
# Arrange
|
||||
class InvalidPayload:
|
||||
@classmethod
|
||||
def model_validate(cls, data: dict[str, Any]):
|
||||
raise Exception("Validation failed")
|
||||
|
||||
@plugin_data(payload_type=InvalidPayload)
|
||||
def protected_view(payload, **kwargs):
|
||||
return payload
|
||||
|
||||
# Act & Assert
|
||||
with app.test_request_context(json={"data": "test"}):
|
||||
with pytest.raises(ValueError, match="invalid payload") as exc_info:
|
||||
protected_view()
|
||||
# PEP 3134: the re-raised ValueError must chain the original Exception from model_validate.
|
||||
assert exc_info.value.__cause__ is not None
|
||||
assert str(exc_info.value.__cause__) == "Validation failed"
|
||||
|
||||
def test_should_work_as_parameterized_decorator(self, app: Flask):
|
||||
"""Test that decorator works when used with parentheses"""
|
||||
|
||||
# Arrange
|
||||
@plugin_data(payload_type=PluginTestPayload)
|
||||
def protected_view(payload, **kwargs):
|
||||
return payload
|
||||
|
||||
# Act
|
||||
with app.test_request_context(json={"value": "parameterized"}):
|
||||
result = protected_view()
|
||||
|
||||
# Assert
|
||||
assert result.value == "parameterized"
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
from inspect import unwrap
|
||||
|
||||
import pytest
|
||||
|
||||
from controllers.inner_api import datasource_credentials as controller
|
||||
from controllers.inner_api.datasource_credentials import (
|
||||
InnerDatasourceDefaultCredentialApi,
|
||||
InnerDatasourceDefaultCredentialPayload,
|
||||
)
|
||||
from services.credentials.query import DatasourceCredentialListItem
|
||||
from tests.helpers.unit.credential_query_fakes import CredentialServices, DatasourceCredentialQuery
|
||||
|
||||
|
||||
def test_resolves_default_datasource_credential_metadata(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
query = DatasourceCredentialQuery(
|
||||
credentials=[
|
||||
DatasourceCredentialListItem(
|
||||
id="credential-default",
|
||||
name="Workspace default",
|
||||
auth_type="oauth2",
|
||||
encrypted_credentials={"access_token": "must-not-be-returned"},
|
||||
avatar_url=None,
|
||||
is_default=True,
|
||||
)
|
||||
]
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
controller,
|
||||
"application_services",
|
||||
lambda: CredentialServices(credential_queries=query),
|
||||
)
|
||||
payload = InnerDatasourceDefaultCredentialPayload(
|
||||
tenant_id="tenant-1",
|
||||
plugin_id="langgenius/notion_datasource",
|
||||
provider="notion_datasource",
|
||||
)
|
||||
|
||||
response = unwrap(InnerDatasourceDefaultCredentialApi.post)(InnerDatasourceDefaultCredentialApi(), payload)
|
||||
|
||||
assert response == {
|
||||
"credential": {
|
||||
"id": "credential-default",
|
||||
"name": "Workspace default",
|
||||
"type": "oauth2",
|
||||
"is_default": True,
|
||||
}
|
||||
}
|
||||
assert "access_token" not in str(response)
|
||||
|
||||
|
||||
def test_returns_a_null_credential_when_no_default_is_available(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
query = DatasourceCredentialQuery(credentials=[])
|
||||
monkeypatch.setattr(
|
||||
controller,
|
||||
"application_services",
|
||||
lambda: CredentialServices(credential_queries=query),
|
||||
)
|
||||
payload = InnerDatasourceDefaultCredentialPayload(
|
||||
tenant_id="tenant-1",
|
||||
plugin_id="langgenius/notion_datasource",
|
||||
provider="notion_datasource",
|
||||
)
|
||||
|
||||
response = unwrap(InnerDatasourceDefaultCredentialApi.post)(InnerDatasourceDefaultCredentialApi(), payload)
|
||||
|
||||
assert response == {"credential": None}
|
||||
@@ -0,0 +1,145 @@
|
||||
"""Unit tests for the thin inner-mail Flask adapter and its admission boundary."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from inspect import unwrap
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from pydantic import ValidationError
|
||||
from werkzeug.exceptions import NotFound, UnprocessableEntity
|
||||
|
||||
from controllers.inner_api.mail import BaseMail, BillingMail, EnterpriseMail, InnerMailPayload
|
||||
from controllers.inner_api.wraps import InnerApiUnauthorizedError
|
||||
from services.entities.mail_entities import InnerMailMessage
|
||||
|
||||
|
||||
class TestInnerMailPayload:
|
||||
def test_valid_payload_with_all_fields(self) -> None:
|
||||
payload = InnerMailPayload.model_validate(
|
||||
{
|
||||
"to": ["test@example.com"],
|
||||
"subject": "Test Subject",
|
||||
"body": "Test Body",
|
||||
"substitutions": {"key": "value"},
|
||||
}
|
||||
)
|
||||
assert payload.to == ["test@example.com"]
|
||||
assert payload.substitutions == {"key": "value"}
|
||||
|
||||
def test_valid_payload_without_substitutions(self) -> None:
|
||||
payload = InnerMailPayload.model_validate(
|
||||
{"to": ["test@example.com"], "subject": "Test Subject", "body": "Test Body"}
|
||||
)
|
||||
assert payload.substitutions is None
|
||||
|
||||
def test_valid_payload_with_null_substitutions(self) -> None:
|
||||
payload = InnerMailPayload.model_validate(
|
||||
{
|
||||
"to": ["test@example.com"],
|
||||
"subject": "Test Subject",
|
||||
"body": "Test Body",
|
||||
"substitutions": None,
|
||||
}
|
||||
)
|
||||
assert payload.substitutions is None
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"payload",
|
||||
[
|
||||
{"to": [], "subject": "Subject", "body": "Body"},
|
||||
{"subject": "Subject", "body": "Body"},
|
||||
{"to": ["test@example.com"], "body": "Body"},
|
||||
{"to": ["test@example.com"], "subject": "Subject"},
|
||||
],
|
||||
)
|
||||
def test_invalid_payload(self, payload: dict[str, object]) -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
InnerMailPayload.model_validate(payload)
|
||||
|
||||
|
||||
class TestBaseMail:
|
||||
@pytest.mark.parametrize(
|
||||
("resource_type", "payload", "expected"),
|
||||
[
|
||||
(
|
||||
EnterpriseMail,
|
||||
{"to": ["test@example.com"], "subject": "Subject", "body": "Body"},
|
||||
InnerMailMessage(recipients=("test@example.com",), subject="Subject", body="Body", substitutions=None),
|
||||
),
|
||||
(
|
||||
BillingMail,
|
||||
{
|
||||
"to": ["one@example.com", "two@example.com"],
|
||||
"subject": "Hello {{name}}",
|
||||
"body": "Welcome {{name}}!",
|
||||
"substitutions": {"name": "John"},
|
||||
},
|
||||
InnerMailMessage(
|
||||
recipients=("one@example.com", "two@example.com"),
|
||||
subject="Hello {{name}}",
|
||||
body="Welcome {{name}}!",
|
||||
substitutions={"name": "John"},
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_post_delegates_to_application_service(
|
||||
self,
|
||||
resource_type: type[BaseMail],
|
||||
payload: dict[str, object],
|
||||
expected: InnerMailMessage,
|
||||
app: Flask,
|
||||
) -> None:
|
||||
mail_service = MagicMock()
|
||||
services = SimpleNamespace(inner_mail=mail_service)
|
||||
|
||||
with (
|
||||
app.test_request_context(method="POST", json=payload),
|
||||
patch("controllers.inner_api.mail.application_services", return_value=services),
|
||||
):
|
||||
result = unwrap(resource_type.post)(resource_type())
|
||||
|
||||
assert result == ({"message": "success"}, 200)
|
||||
mail_service.send.assert_called_once_with(expected)
|
||||
|
||||
@pytest.mark.parametrize("resource_type", [EnterpriseMail, BillingMail])
|
||||
def test_invalid_body_is_rejected_before_the_application_service_runs(
|
||||
self, resource_type: type[BaseMail], app: Flask
|
||||
) -> None:
|
||||
"""`super().post()` relies on the decorator to supply the payload, so this covers it."""
|
||||
mail_service = MagicMock()
|
||||
services = SimpleNamespace(inner_mail=mail_service)
|
||||
|
||||
with (
|
||||
app.test_request_context(method="POST", json={}),
|
||||
patch("controllers.inner_api.mail.application_services", return_value=services),
|
||||
pytest.raises(UnprocessableEntity),
|
||||
):
|
||||
unwrap(resource_type.post)(resource_type())
|
||||
|
||||
mail_service.send.assert_not_called()
|
||||
|
||||
|
||||
def test_disabled_inner_api_returns_not_found_before_setup(app: Flask, config_overrides: Callable[..., None]) -> None:
|
||||
config_overrides(INNER_API=False)
|
||||
with patch(
|
||||
"controllers.console.wraps._is_setup_completed",
|
||||
side_effect=AssertionError("setup must not run before Inner API authentication"),
|
||||
):
|
||||
with app.test_request_context(), pytest.raises(NotFound):
|
||||
EnterpriseMail().post()
|
||||
|
||||
|
||||
def test_invalid_inner_api_key_is_rejected_before_setup(app: Flask, config_overrides: Callable[..., None]) -> None:
|
||||
config_overrides(INNER_API=True, INNER_API_KEY="valid-key")
|
||||
with patch(
|
||||
"controllers.console.wraps._is_setup_completed",
|
||||
side_effect=AssertionError("setup must not run before Inner API authentication"),
|
||||
):
|
||||
with (
|
||||
app.test_request_context(headers={"X-Inner-Api-Key": "invalid-key"}),
|
||||
pytest.raises(InnerApiUnauthorizedError),
|
||||
):
|
||||
EnterpriseMail().post()
|
||||
+242
@@ -0,0 +1,242 @@
|
||||
"""Unit tests for runtime credential inner API."""
|
||||
|
||||
import inspect
|
||||
import json
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.orm import Session
|
||||
from werkzeug.exceptions import UnprocessableEntity
|
||||
|
||||
from controllers.inner_api.runtime_credentials import (
|
||||
EnterpriseRuntimeCredentialsResolve,
|
||||
InnerRuntimeCredentialsResolvePayload,
|
||||
)
|
||||
from models.provider import ProviderCredential
|
||||
from models.tools import BuiltinToolProvider
|
||||
from tests.helpers.unit.config_override import config_overrides_context
|
||||
|
||||
|
||||
def test_runtime_credentials_payload_accepts_items():
|
||||
payload = InnerRuntimeCredentialsResolvePayload.model_validate(
|
||||
{
|
||||
"tenant_id": "tenant-1",
|
||||
"credentials": [
|
||||
{
|
||||
"credential_id": "credential-1",
|
||||
"provider": "langgenius/openai/openai",
|
||||
"kind": "model",
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
assert payload.tenant_id == "tenant-1"
|
||||
assert payload.credentials[0].provider == "langgenius/openai/openai"
|
||||
assert payload.credentials[0].kind == "model"
|
||||
|
||||
|
||||
@patch("controllers.inner_api.runtime_credentials.encrypter.decrypt_token")
|
||||
@patch("controllers.inner_api.runtime_credentials.db")
|
||||
@patch("controllers.inner_api.runtime_credentials.create_plugin_provider_manager")
|
||||
@pytest.mark.parametrize("sqlite_session", [(ProviderCredential,)], indirect=True)
|
||||
def test_runtime_model_credentials_resolve_returns_decrypted_values(
|
||||
mock_provider_manager_factory,
|
||||
mock_db,
|
||||
mock_decrypt_token,
|
||||
app: Flask,
|
||||
sqlite_engine: Engine,
|
||||
sqlite_session: Session,
|
||||
):
|
||||
provider_configuration = MagicMock()
|
||||
provider_configuration.provider.provider_credential_schema.credential_form_schemas = []
|
||||
provider_configuration.extract_secret_variables.return_value = ["openai_api_key"]
|
||||
provider_configuration._get_provider_names.return_value = ["langgenius/openai/openai", "openai"]
|
||||
|
||||
provider_configurations = MagicMock()
|
||||
provider_configurations.get.return_value = provider_configuration
|
||||
provider_manager = MagicMock()
|
||||
provider_manager.get_configurations.return_value = provider_configurations
|
||||
mock_provider_manager_factory.return_value = provider_manager
|
||||
|
||||
credential = ProviderCredential(
|
||||
tenant_id="tenant-1",
|
||||
provider_name="langgenius/openai/openai",
|
||||
credential_name="OpenAI",
|
||||
encrypted_config='{"openai_api_key":"encrypted","api_base":"https://api.openai.com/v1"}',
|
||||
)
|
||||
credential.id = "credential-1"
|
||||
sqlite_session.add(credential)
|
||||
sqlite_session.commit()
|
||||
mock_db.engine = sqlite_engine
|
||||
mock_decrypt_token.return_value = "sk-test"
|
||||
|
||||
handler = EnterpriseRuntimeCredentialsResolve()
|
||||
unwrapped = inspect.unwrap(handler.post)
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"credentials": [
|
||||
{
|
||||
"credential_id": "credential-1",
|
||||
"provider": "langgenius/openai/openai",
|
||||
"kind": "model",
|
||||
}
|
||||
],
|
||||
}
|
||||
with app.test_request_context(json=payload):
|
||||
body, status_code = unwrapped(handler, InnerRuntimeCredentialsResolvePayload.model_validate(payload))
|
||||
|
||||
assert status_code == 200
|
||||
assert body["credentials"][0]["kind"] == "model"
|
||||
assert body["credentials"][0]["values"]["openai_api_key"] == "sk-test"
|
||||
assert body["credentials"][0]["values"]["api_base"] == "https://api.openai.com/v1"
|
||||
mock_decrypt_token.assert_called_once_with(tenant_id="tenant-1", token="encrypted")
|
||||
|
||||
|
||||
@patch("controllers.inner_api.runtime_credentials.create_plugin_provider_manager")
|
||||
def test_runtime_model_credentials_resolve_rejects_unknown_provider(mock_provider_manager_factory, app: Flask):
|
||||
provider_configurations = MagicMock()
|
||||
provider_configurations.get.return_value = None
|
||||
provider_manager = MagicMock()
|
||||
provider_manager.get_configurations.return_value = provider_configurations
|
||||
mock_provider_manager_factory.return_value = provider_manager
|
||||
|
||||
handler = EnterpriseRuntimeCredentialsResolve()
|
||||
unwrapped = inspect.unwrap(handler.post)
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"credentials": [{"credential_id": "credential-1", "provider": "missing", "kind": "model"}],
|
||||
}
|
||||
with app.test_request_context(json=payload):
|
||||
body, status_code = unwrapped(handler, InnerRuntimeCredentialsResolvePayload.model_validate(payload))
|
||||
|
||||
assert status_code == 404
|
||||
assert "provider" in body["message"]
|
||||
|
||||
|
||||
@patch("controllers.inner_api.runtime_credentials.create_provider_encrypter")
|
||||
@patch("controllers.inner_api.runtime_credentials.ToolProviderCredentialsCache")
|
||||
@patch("controllers.inner_api.runtime_credentials.db")
|
||||
@patch("controllers.inner_api.runtime_credentials.ToolManager")
|
||||
@pytest.mark.parametrize("sqlite_session", [(BuiltinToolProvider,)], indirect=True)
|
||||
def test_runtime_tool_credentials_resolve_returns_decrypted_values(
|
||||
mock_tool_manager,
|
||||
mock_db,
|
||||
mock_cache_cls,
|
||||
mock_create_encrypter,
|
||||
app: Flask,
|
||||
sqlite_engine: Engine,
|
||||
sqlite_session: Session,
|
||||
):
|
||||
provider_controller = MagicMock()
|
||||
provider_controller.get_credentials_schema_by_type.return_value = []
|
||||
mock_tool_manager.get_builtin_provider.return_value = provider_controller
|
||||
|
||||
builtin_provider = BuiltinToolProvider(
|
||||
tenant_id="tenant-1",
|
||||
user_id="user-1",
|
||||
provider="langgenius/tavily/tavily",
|
||||
name="Tavily",
|
||||
encrypted_credentials=json.dumps({"tavily_api_key": "encrypted"}),
|
||||
)
|
||||
builtin_provider.id = "credential-1"
|
||||
sqlite_session.add(builtin_provider)
|
||||
sqlite_session.commit()
|
||||
mock_db.engine = sqlite_engine
|
||||
|
||||
provider_encrypter = MagicMock()
|
||||
provider_encrypter.decrypt.return_value = {"tavily_api_key": "tvly-secret"}
|
||||
mock_create_encrypter.return_value = (provider_encrypter, MagicMock())
|
||||
|
||||
handler = EnterpriseRuntimeCredentialsResolve()
|
||||
unwrapped = inspect.unwrap(handler.post)
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"credentials": [
|
||||
{
|
||||
"credential_id": "credential-1",
|
||||
"provider": "langgenius/tavily/tavily",
|
||||
"kind": "tool",
|
||||
}
|
||||
],
|
||||
}
|
||||
with app.test_request_context(json=payload):
|
||||
body, status_code = unwrapped(handler, InnerRuntimeCredentialsResolvePayload.model_validate(payload))
|
||||
|
||||
assert status_code == 200
|
||||
assert body["credentials"][0]["kind"] == "tool"
|
||||
assert body["credentials"][0]["provider"] == "langgenius/tavily/tavily"
|
||||
assert body["credentials"][0]["values"]["tavily_api_key"] == "tvly-secret"
|
||||
provider_encrypter.decrypt.assert_called_once_with({"tavily_api_key": "encrypted"})
|
||||
|
||||
|
||||
@patch("controllers.inner_api.runtime_credentials.db")
|
||||
@patch("controllers.inner_api.runtime_credentials.ToolManager")
|
||||
@pytest.mark.parametrize("sqlite_session", [(BuiltinToolProvider,)], indirect=True)
|
||||
def test_runtime_tool_credentials_resolve_rejects_unknown_credential(
|
||||
mock_tool_manager,
|
||||
mock_db,
|
||||
app: Flask,
|
||||
sqlite_engine: Engine,
|
||||
sqlite_session: Session,
|
||||
):
|
||||
mock_tool_manager.get_builtin_provider.return_value = MagicMock()
|
||||
|
||||
# The requested id exists for another tenant, proving the resolver does not
|
||||
# expose a credential across workspace boundaries.
|
||||
builtin_provider = BuiltinToolProvider(
|
||||
tenant_id="tenant-2",
|
||||
user_id="user-2",
|
||||
provider="langgenius/tavily/tavily",
|
||||
name="Other workspace Tavily",
|
||||
encrypted_credentials=json.dumps({"tavily_api_key": "encrypted"}),
|
||||
)
|
||||
builtin_provider.id = "missing"
|
||||
sqlite_session.add(builtin_provider)
|
||||
sqlite_session.commit()
|
||||
mock_db.engine = sqlite_engine
|
||||
|
||||
handler = EnterpriseRuntimeCredentialsResolve()
|
||||
unwrapped = inspect.unwrap(handler.post)
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"credentials": [{"credential_id": "missing", "provider": "langgenius/tavily/tavily", "kind": "tool"}],
|
||||
}
|
||||
with app.test_request_context(json=payload):
|
||||
body, status_code = unwrapped(handler, InnerRuntimeCredentialsResolvePayload.model_validate(payload))
|
||||
|
||||
assert status_code == 404
|
||||
assert "credential" in body["message"]
|
||||
|
||||
|
||||
def test_runtime_credentials_resolve_rejects_unknown_kind(app: Flask):
|
||||
handler = EnterpriseRuntimeCredentialsResolve()
|
||||
unwrapped = inspect.unwrap(handler.post)
|
||||
payload = {
|
||||
"tenant_id": "tenant-1",
|
||||
"credentials": [{"credential_id": "credential-1", "provider": "x", "kind": "secret"}],
|
||||
}
|
||||
with app.test_request_context(json=payload):
|
||||
body, status_code = unwrapped(handler, InnerRuntimeCredentialsResolvePayload.model_validate(payload))
|
||||
|
||||
assert status_code == 400
|
||||
assert "kind" in body["message"]
|
||||
|
||||
|
||||
def test_invalid_body_is_rejected_before_the_handler_runs(app: Flask) -> None:
|
||||
"""The tests above unwrap the view, so this is what covers the decorator."""
|
||||
handler = EnterpriseRuntimeCredentialsResolve()
|
||||
|
||||
with (
|
||||
patch("controllers.console.wraps._is_setup_completed", return_value=True),
|
||||
config_overrides_context(INNER_API=True, INNER_API_KEY="inner-api-key"),
|
||||
app.test_request_context(
|
||||
method="POST",
|
||||
json={},
|
||||
headers={"X-Inner-Api-Key": "inner-api-key"},
|
||||
),
|
||||
pytest.raises(UnprocessableEntity),
|
||||
):
|
||||
handler.post()
|
||||
@@ -0,0 +1,351 @@
|
||||
"""
|
||||
Unit tests for inner_api auth decorators
|
||||
"""
|
||||
|
||||
from collections.abc import Callable
|
||||
from uuid import NAMESPACE_URL, uuid5
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from sqlalchemy import Engine, event
|
||||
from sqlalchemy.orm import Session
|
||||
from werkzeug.exceptions import HTTPException, NotFound
|
||||
|
||||
from controllers.inner_api.wraps import (
|
||||
enterprise_inner_api_only,
|
||||
enterprise_inner_api_user_auth,
|
||||
inner_api_only,
|
||||
knowledge_fs_inner_api_only,
|
||||
plugin_inner_api_only,
|
||||
)
|
||||
from enums import DeploymentEdition
|
||||
from models.enums import EndUserType
|
||||
from models.model import EndUser
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _inner_api_config(config_overrides: Callable[..., None]) -> None:
|
||||
config_overrides(
|
||||
INNER_API=True,
|
||||
INNER_API_KEY="valid_key",
|
||||
PLUGIN_DAEMON_KEY="plugin_key",
|
||||
INNER_API_KEY_FOR_PLUGIN="valid_plugin_key",
|
||||
)
|
||||
|
||||
|
||||
def _stable_uuid(value: str) -> str:
|
||||
return str(uuid5(NAMESPACE_URL, value))
|
||||
|
||||
|
||||
class TestEnterpriseInnerApiOnly:
|
||||
"""Test enterprise_inner_api_only decorator"""
|
||||
|
||||
def test_should_allow_when_inner_api_enabled_and_valid_key(self, app: Flask) -> None:
|
||||
"""Test that valid API key allows access when INNER_API is enabled"""
|
||||
|
||||
# Arrange
|
||||
@enterprise_inner_api_only
|
||||
def protected_view() -> str:
|
||||
return "success"
|
||||
|
||||
# Act
|
||||
with app.test_request_context(headers={"X-Inner-Api-Key": "valid_key"}):
|
||||
result = protected_view()
|
||||
|
||||
# Assert
|
||||
assert result == "success"
|
||||
|
||||
def test_should_return_404_when_inner_api_disabled(self, app: Flask, config_overrides: Callable[..., None]) -> None:
|
||||
"""Test that 404 is returned when INNER_API is disabled"""
|
||||
|
||||
# Arrange
|
||||
@enterprise_inner_api_only
|
||||
def protected_view() -> str:
|
||||
return "success"
|
||||
|
||||
# Act & Assert
|
||||
config_overrides(INNER_API=False)
|
||||
with app.test_request_context():
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
protected_view()
|
||||
assert exc_info.value.code == 404
|
||||
|
||||
def test_should_return_401_when_api_key_missing(self, app: Flask) -> None:
|
||||
"""Test that 401 is returned when X-Inner-Api-Key header is missing"""
|
||||
|
||||
# Arrange
|
||||
@enterprise_inner_api_only
|
||||
def protected_view() -> str:
|
||||
return "success"
|
||||
|
||||
# Act & Assert
|
||||
with app.test_request_context(headers={}):
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
protected_view()
|
||||
assert exc_info.value.code == 401
|
||||
|
||||
def test_should_return_401_when_api_key_invalid(self, app: Flask) -> None:
|
||||
"""Test that 401 is returned when X-Inner-Api-Key header is invalid"""
|
||||
|
||||
# Arrange
|
||||
@enterprise_inner_api_only
|
||||
def protected_view() -> str:
|
||||
return "success"
|
||||
|
||||
# Act & Assert
|
||||
with app.test_request_context(headers={"X-Inner-Api-Key": "invalid_key"}):
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
protected_view()
|
||||
assert exc_info.value.code == 401
|
||||
|
||||
|
||||
class TestInnerApiOnly:
|
||||
"""Test inner_api_only decorator."""
|
||||
|
||||
def test_should_allow_when_inner_api_enabled_and_valid_key(self, app: Flask) -> None:
|
||||
@inner_api_only
|
||||
def protected_view() -> str:
|
||||
return "success"
|
||||
|
||||
with app.test_request_context(headers={"X-Inner-Api-Key": "valid_key"}):
|
||||
result = protected_view()
|
||||
|
||||
assert result == "success"
|
||||
|
||||
def test_should_return_404_when_inner_api_disabled(self, app: Flask, config_overrides: Callable[..., None]) -> None:
|
||||
@inner_api_only
|
||||
def protected_view() -> str:
|
||||
return "success"
|
||||
|
||||
config_overrides(INNER_API=False)
|
||||
with app.test_request_context():
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
protected_view()
|
||||
assert exc_info.value.code == 404
|
||||
|
||||
def test_should_return_401_when_api_key_missing(self, app: Flask) -> None:
|
||||
@inner_api_only
|
||||
def protected_view() -> str:
|
||||
return "success"
|
||||
|
||||
with app.test_request_context(headers={}):
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
protected_view()
|
||||
assert exc_info.value.code == 401
|
||||
|
||||
def test_should_return_401_when_api_key_invalid(self, app: Flask) -> None:
|
||||
@inner_api_only
|
||||
def protected_view() -> str:
|
||||
return "success"
|
||||
|
||||
with app.test_request_context(headers={"X-Inner-Api-Key": "invalid_key"}):
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
protected_view()
|
||||
assert exc_info.value.code == 401
|
||||
|
||||
|
||||
class TestEnterpriseInnerApiUserAuth:
|
||||
"""Test enterprise_inner_api_user_auth decorator for HMAC-based user authentication"""
|
||||
|
||||
def test_should_pass_through_when_inner_api_disabled(
|
||||
self, app: Flask, config_overrides: Callable[..., None]
|
||||
) -> None:
|
||||
"""Test that request passes through when INNER_API is disabled"""
|
||||
|
||||
# Arrange
|
||||
@enterprise_inner_api_user_auth
|
||||
def protected_view(**kwargs: object) -> object:
|
||||
return kwargs.get("user", "no_user")
|
||||
|
||||
# Act
|
||||
config_overrides(INNER_API=False)
|
||||
with app.test_request_context():
|
||||
result = protected_view()
|
||||
|
||||
# Assert
|
||||
assert result == "no_user"
|
||||
|
||||
def test_should_pass_through_when_authorization_header_missing(self, app: Flask) -> None:
|
||||
"""Test that request passes through when Authorization header is missing"""
|
||||
|
||||
# Arrange
|
||||
@enterprise_inner_api_user_auth
|
||||
def protected_view(**kwargs: object) -> object:
|
||||
return kwargs.get("user", "no_user")
|
||||
|
||||
# Act
|
||||
with app.test_request_context(headers={}):
|
||||
result = protected_view()
|
||||
|
||||
# Assert
|
||||
assert result == "no_user"
|
||||
|
||||
def test_should_pass_through_when_authorization_format_invalid(self, app: Flask) -> None:
|
||||
"""Test that request passes through when Authorization format is invalid (no colon)"""
|
||||
|
||||
# Arrange
|
||||
@enterprise_inner_api_user_auth
|
||||
def protected_view(**kwargs: object) -> object:
|
||||
return kwargs.get("user", "no_user")
|
||||
|
||||
# Act
|
||||
with app.test_request_context(headers={"Authorization": "invalid_format"}):
|
||||
result = protected_view()
|
||||
|
||||
# Assert
|
||||
assert result == "no_user"
|
||||
|
||||
def test_should_pass_through_when_hmac_signature_invalid(self, app: Flask, sqlite_engine: Engine) -> None:
|
||||
"""Invalid HMAC auth passes through without opening a database session."""
|
||||
|
||||
# Arrange
|
||||
@enterprise_inner_api_user_auth
|
||||
def protected_view(**kwargs: object) -> object:
|
||||
return kwargs.get("user", "no_user")
|
||||
|
||||
def fail_on_query(*_args: object, **_kwargs: object) -> None:
|
||||
pytest.fail("invalid HMAC must not access the database")
|
||||
|
||||
event.listen(sqlite_engine, "before_cursor_execute", fail_on_query)
|
||||
try:
|
||||
with app.test_request_context(
|
||||
headers={"Authorization": "Bearer user123:wrong_signature", "X-Inner-Api-Key": "valid_key"}
|
||||
):
|
||||
result = protected_view()
|
||||
finally:
|
||||
event.remove(sqlite_engine, "before_cursor_execute", fail_on_query)
|
||||
|
||||
assert result == "no_user"
|
||||
|
||||
@pytest.mark.parametrize("sqlite_session", [(EndUser,)], indirect=True)
|
||||
def test_should_inject_user_when_hmac_signature_valid(self, app: Flask, sqlite_session: Session) -> None:
|
||||
"""Test that user is injected when HMAC signature is valid"""
|
||||
# Arrange
|
||||
from base64 import b64encode
|
||||
from hashlib import sha1
|
||||
from hmac import new as hmac_new
|
||||
|
||||
@enterprise_inner_api_user_auth
|
||||
def protected_view(**kwargs: object) -> object:
|
||||
return kwargs.get("user")
|
||||
|
||||
# Calculate valid HMAC signature
|
||||
user_id = _stable_uuid("end-user:user123")
|
||||
inner_api_key = "valid_key"
|
||||
data_to_sign = f"DIFY {user_id}"
|
||||
signature = hmac_new(inner_api_key.encode("utf-8"), data_to_sign.encode("utf-8"), sha1)
|
||||
valid_signature = b64encode(signature.digest()).decode("utf-8")
|
||||
|
||||
end_user = EndUser(
|
||||
id=user_id,
|
||||
tenant_id=_stable_uuid("tenant:inner-api"),
|
||||
type=EndUserType.BROWSER,
|
||||
name="Inner API User",
|
||||
session_id="inner-api-session",
|
||||
)
|
||||
sqlite_session.add(end_user)
|
||||
sqlite_session.commit()
|
||||
|
||||
# Act
|
||||
with app.test_request_context(
|
||||
headers={"Authorization": f"Bearer {user_id}:{valid_signature}", "X-Inner-Api-Key": inner_api_key}
|
||||
):
|
||||
result = protected_view()
|
||||
|
||||
# Assert
|
||||
assert isinstance(result, EndUser)
|
||||
assert result.id == end_user.id
|
||||
assert result.tenant_id == end_user.tenant_id
|
||||
assert result.session_id == "inner-api-session"
|
||||
|
||||
|
||||
class TestPluginInnerApiOnly:
|
||||
"""Test plugin_inner_api_only decorator"""
|
||||
|
||||
def test_should_allow_when_plugin_daemon_key_set_and_valid_key(self, app: Flask) -> None:
|
||||
"""Test that valid API key allows access when PLUGIN_DAEMON_KEY is set"""
|
||||
|
||||
# Arrange
|
||||
@plugin_inner_api_only
|
||||
def protected_view() -> str:
|
||||
return "success"
|
||||
|
||||
# Act
|
||||
with app.test_request_context(headers={"X-Inner-Api-Key": "valid_plugin_key"}):
|
||||
result = protected_view()
|
||||
|
||||
# Assert
|
||||
assert result == "success"
|
||||
|
||||
def test_should_return_404_when_plugin_daemon_key_not_set(
|
||||
self, app: Flask, config_overrides: Callable[..., None]
|
||||
) -> None:
|
||||
"""Test that 404 is returned when PLUGIN_DAEMON_KEY is not set"""
|
||||
|
||||
# Arrange
|
||||
@plugin_inner_api_only
|
||||
def protected_view() -> str:
|
||||
return "success"
|
||||
|
||||
# Act & Assert
|
||||
config_overrides(PLUGIN_DAEMON_KEY="")
|
||||
with app.test_request_context():
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
protected_view()
|
||||
assert exc_info.value.code == 404
|
||||
|
||||
def test_should_return_404_when_api_key_invalid(self, app: Flask) -> None:
|
||||
"""Test that 404 is returned when X-Inner-Api-Key header is invalid (note: returns 404, not 401)"""
|
||||
|
||||
# Arrange
|
||||
@plugin_inner_api_only
|
||||
def protected_view() -> str:
|
||||
return "success"
|
||||
|
||||
# Act & Assert
|
||||
with app.test_request_context(headers={"X-Inner-Api-Key": "invalid_key"}):
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
protected_view()
|
||||
assert exc_info.value.code == 404
|
||||
|
||||
|
||||
class TestKnowledgeFSInnerApiOnly:
|
||||
"""KnowledgeFS uses the same trusted transport key without exposing plugin semantics."""
|
||||
|
||||
def test_should_allow_valid_shared_inner_key(self, app: Flask, config_overrides: Callable[..., None]) -> None:
|
||||
@knowledge_fs_inner_api_only
|
||||
def protected_view() -> str:
|
||||
return "success"
|
||||
|
||||
config_overrides(
|
||||
PLUGIN_DAEMON_KEY="plugin_key",
|
||||
INNER_API_KEY_FOR_PLUGIN="valid_plugin_key",
|
||||
DEPLOYMENT_EDITION=DeploymentEdition.CLOUD,
|
||||
KNOWLEDGE_FS_ENABLED=True,
|
||||
)
|
||||
with app.test_request_context(headers={"X-Inner-Api-Key": "valid_plugin_key"}):
|
||||
assert protected_view() == "success"
|
||||
|
||||
def test_should_hide_endpoint_when_knowledge_fs_is_disabled(
|
||||
self, app: Flask, config_overrides: Callable[..., None]
|
||||
) -> None:
|
||||
@knowledge_fs_inner_api_only
|
||||
def protected_view() -> str:
|
||||
pytest.fail("disabled KnowledgeFS must not enter the handler")
|
||||
|
||||
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.CLOUD, KNOWLEDGE_FS_ENABLED=False)
|
||||
with app.test_request_context(headers={"X-Inner-Api-Key": "valid_plugin_key"}):
|
||||
with pytest.raises(NotFound):
|
||||
protected_view()
|
||||
|
||||
def test_should_hide_endpoint_for_invalid_key(self, app: Flask, config_overrides: Callable[..., None]) -> None:
|
||||
@knowledge_fs_inner_api_only
|
||||
def protected_view() -> str:
|
||||
return "success"
|
||||
|
||||
config_overrides(PLUGIN_DAEMON_KEY="plugin_key", INNER_API_KEY_FOR_PLUGIN="valid_plugin_key")
|
||||
with app.test_request_context(headers={"X-Inner-Api-Key": "invalid"}):
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
protected_view()
|
||||
|
||||
assert exc_info.value.code == 404
|
||||
Whitespace-only changes.
+252
@@ -0,0 +1,252 @@
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from inspect import unwrap
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock, create_autospec
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from flask.typing import ResponseReturnValue
|
||||
from flask_restx import Api, Resource
|
||||
from werkzeug.exceptions import UnprocessableEntity
|
||||
|
||||
from controllers.inner_api.workspace import workspace as controller
|
||||
from controllers.inner_api.wraps import InnerApiUnauthorizedError
|
||||
from extensions.application_services.workspace import WorkspaceServices
|
||||
from libs.external_api import register_external_error_handlers
|
||||
from services.account_errors import AccountNotFoundError
|
||||
from services.errors.workspace import (
|
||||
InvalidWorkspaceMemberRoleError,
|
||||
WorkspaceNotFoundError,
|
||||
WorkspaceOwnerNotFoundError,
|
||||
WorkspacesLimitExceededError,
|
||||
)
|
||||
from services.workspace.contracts import CreatedWorkspace, WorkspaceMembership
|
||||
from services.workspace.provisioning_service import WorkspaceProvisioningService
|
||||
from services.workspace.service import WorkspaceQueryService, WorkspaceService
|
||||
|
||||
WORKSPACE_ID = UUID("0dd1d570-cf7b-4bab-9c7f-84ebe6c36985")
|
||||
|
||||
|
||||
class TestResourceDeletionCases:
|
||||
@staticmethod
|
||||
def test_delete_calls_cleanup_with_the_workspace_id(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
service = Mock()
|
||||
monkeypatch.setattr(
|
||||
controller,
|
||||
"application_services",
|
||||
lambda: SimpleNamespace(workspaces=SimpleNamespace(resource_cleanup=service)),
|
||||
)
|
||||
with app.test_request_context(method="DELETE"):
|
||||
result = unwrap(controller.EnterpriseWorkspaceResourceDeletion.delete)(
|
||||
controller.EnterpriseWorkspaceResourceDeletion(), WORKSPACE_ID
|
||||
)
|
||||
assert result == ({"message": "workspace resource cleanup accepted."}, 202)
|
||||
service.cleanup.assert_called_once_with(str(WORKSPACE_ID))
|
||||
|
||||
@staticmethod
|
||||
def test_delete_requires_inner_api_key(
|
||||
app: Flask, monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
|
||||
) -> None:
|
||||
config_overrides(INNER_API=True, INNER_API_KEY="secret", DEPLOYMENT_EDITION="CLOUD")
|
||||
service = Mock()
|
||||
monkeypatch.setattr(
|
||||
controller,
|
||||
"application_services",
|
||||
lambda: SimpleNamespace(workspaces=SimpleNamespace(resource_cleanup=service)),
|
||||
)
|
||||
with app.test_request_context(method="DELETE"):
|
||||
with pytest.raises(InnerApiUnauthorizedError):
|
||||
controller.EnterpriseWorkspaceResourceDeletion().delete(WORKSPACE_ID)
|
||||
service.cleanup.assert_not_called()
|
||||
|
||||
@staticmethod
|
||||
def test_cleanup_failure_does_not_return_accepted(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
service = Mock()
|
||||
service.cleanup.side_effect = RuntimeError("broker unavailable")
|
||||
monkeypatch.setattr(
|
||||
controller,
|
||||
"application_services",
|
||||
lambda: SimpleNamespace(workspaces=SimpleNamespace(resource_cleanup=service)),
|
||||
)
|
||||
with app.test_request_context(method="DELETE"), pytest.raises(RuntimeError, match="broker unavailable"):
|
||||
unwrap(controller.EnterpriseWorkspaceResourceDeletion.delete)(
|
||||
controller.EnterpriseWorkspaceResourceDeletion(), WORKSPACE_ID
|
||||
)
|
||||
|
||||
|
||||
"""Internal workspace transport behavior and admission."""
|
||||
|
||||
|
||||
@dataclass
|
||||
class Services:
|
||||
workspaces: WorkspaceServices
|
||||
|
||||
|
||||
MEMBER = {"workspace_id": "w1", "account_id": "a1", "email": "member@example.com", "current": True}
|
||||
|
||||
|
||||
class TestWorkspaceCases:
|
||||
@staticmethod
|
||||
@pytest.fixture
|
||||
def provisioning(monkeypatch: pytest.MonkeyPatch) -> Mock:
|
||||
provisioning = create_autospec(WorkspaceProvisioningService, instance=True)
|
||||
services = WorkspaceServices(
|
||||
queries=create_autospec(WorkspaceQueryService, instance=True),
|
||||
management=create_autospec(WorkspaceService, instance=True),
|
||||
provisioning=provisioning,
|
||||
members=Mock(),
|
||||
member_queries=Mock(),
|
||||
owner_transfer=Mock(),
|
||||
invitations=Mock(),
|
||||
identity=Mock(),
|
||||
resource_cleanup=Mock(),
|
||||
)
|
||||
monkeypatch.setattr(controller, "application_services", lambda: Services(services))
|
||||
return provisioning
|
||||
|
||||
@staticmethod
|
||||
@pytest.mark.parametrize(
|
||||
("resource", "payload", "ownerless"),
|
||||
[
|
||||
(controller.EnterpriseWorkspace, {"name": "Test", "owner_email": "owner@example.com"}, False),
|
||||
(controller.EnterpriseWorkspaceNoOwnerEmail, {"name": "Test"}, True),
|
||||
],
|
||||
)
|
||||
def test_create_serialization(
|
||||
app: Flask,
|
||||
provisioning: Mock,
|
||||
resource: type[
|
||||
controller.EnterpriseWorkspace
|
||||
| controller.EnterpriseWorkspaceNoOwnerEmail
|
||||
| controller.EnterpriseWorkspaceMember
|
||||
],
|
||||
payload: dict[str, object],
|
||||
ownerless: bool,
|
||||
) -> None:
|
||||
provisioning.create.return_value = CreatedWorkspace(
|
||||
"w1", "Test", "sandbox", "normal", datetime(2026, 1, 1), None, "public-key", {"remove_webapp_brand": True}
|
||||
)
|
||||
with app.test_request_context(method="POST", json=payload):
|
||||
body = unwrap(resource.post)(resource())
|
||||
assert body["message"] == "enterprise workspace created."
|
||||
assert body["tenant"]["created_at"] == "2026-01-01T00:00:00Z"
|
||||
assert body["tenant"]["updated_at"] is None
|
||||
if ownerless:
|
||||
provisioning.create.assert_called_once_with(name="Test")
|
||||
assert body["tenant"]["encrypt_public_key"] == "public-key"
|
||||
assert body["tenant"]["custom_config"] == {"remove_webapp_brand": True}
|
||||
else:
|
||||
provisioning.create.assert_called_once_with(name="Test", owner_email="owner@example.com")
|
||||
assert "encrypt_public_key" not in body["tenant"]
|
||||
assert "custom_config" not in body["tenant"]
|
||||
|
||||
@staticmethod
|
||||
def test_ownerless_create_keeps_empty_custom_config_object(app: Flask, provisioning: Mock) -> None:
|
||||
provisioning.create.return_value = CreatedWorkspace(
|
||||
"w1", "Test", "sandbox", "normal", datetime(2026, 1, 1), None, "public-key", {}
|
||||
)
|
||||
with app.test_request_context(method="POST", json={"name": "Test"}):
|
||||
body = unwrap(controller.EnterpriseWorkspaceNoOwnerEmail.post)(controller.EnterpriseWorkspaceNoOwnerEmail())
|
||||
assert body["tenant"]["custom_config"] == {}
|
||||
|
||||
@staticmethod
|
||||
def test_owner_missing(app: Flask, provisioning: Mock) -> None:
|
||||
provisioning.create.side_effect = WorkspaceOwnerNotFoundError()
|
||||
with app.test_request_context(method="POST", json={"name": "Test", "owner_email": "absent@example.com"}):
|
||||
result = unwrap(controller.EnterpriseWorkspace.post)(controller.EnterpriseWorkspace())
|
||||
assert result == ({"message": "owner account not found."}, 404)
|
||||
|
||||
@staticmethod
|
||||
def test_workspace_limit_remains_a_client_error(provisioning: Mock) -> None:
|
||||
provisioning.create.side_effect = WorkspacesLimitExceededError("Workspace limit reached")
|
||||
http_app = Flask(__name__)
|
||||
http_api = Api(http_app, doc=False)
|
||||
register_external_error_handlers(http_api)
|
||||
|
||||
class Endpoint(Resource):
|
||||
def post(self) -> ResponseReturnValue:
|
||||
return unwrap(controller.EnterpriseWorkspace.post)(controller.EnterpriseWorkspace())
|
||||
|
||||
http_api.add_resource(Endpoint, "/workspaces")
|
||||
response = http_app.test_client().post("/workspaces", json={"name": "Test", "owner_email": "owner@example.com"})
|
||||
assert response.status_code == 400
|
||||
assert response.json == {"code": "invalid_param", "message": "Workspace limit reached", "status": 400}
|
||||
|
||||
@staticmethod
|
||||
def test_join_member(app: Flask, provisioning: Mock) -> None:
|
||||
provisioning.join_member.return_value = WorkspaceMembership("w1", "a1", "normal")
|
||||
with app.test_request_context(method="POST", json=MEMBER):
|
||||
body = unwrap(controller.EnterpriseWorkspaceMember.post)(controller.EnterpriseWorkspaceMember())
|
||||
assert body["member"] == {"workspace_id": "w1", "account_id": "a1", "role": "normal"}
|
||||
provisioning.join_member.assert_called_once_with(
|
||||
workspace_id="w1", account_id="a1", email="member@example.com", role="normal", operator_account_id=None
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
@pytest.mark.parametrize(
|
||||
("error", "message", "status"),
|
||||
[
|
||||
(AccountNotFoundError(), "account not found.", 404),
|
||||
(WorkspaceNotFoundError(), "workspace not found.", 404),
|
||||
(InvalidWorkspaceMemberRoleError("invalid workspace member role."), "invalid workspace member role.", 400),
|
||||
(
|
||||
InvalidWorkspaceMemberRoleError("cannot join workspace as owner."),
|
||||
"cannot join workspace as owner.",
|
||||
400,
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_join_errors(app: Flask, provisioning: Mock, error: Exception, message: str, status: int) -> None:
|
||||
provisioning.join_member.side_effect = error
|
||||
with app.test_request_context(method="POST", json=MEMBER):
|
||||
result = unwrap(controller.EnterpriseWorkspaceMember.post)(controller.EnterpriseWorkspaceMember())
|
||||
assert result == ({"message": message}, status)
|
||||
|
||||
@staticmethod
|
||||
@pytest.mark.parametrize(
|
||||
"resource",
|
||||
[
|
||||
controller.EnterpriseWorkspace,
|
||||
controller.EnterpriseWorkspaceNoOwnerEmail,
|
||||
controller.EnterpriseWorkspaceMember,
|
||||
],
|
||||
)
|
||||
def test_invalid_payload_after_admission(
|
||||
app: Flask,
|
||||
provisioning: Mock,
|
||||
config_overrides: Callable[..., None],
|
||||
resource: type[
|
||||
controller.EnterpriseWorkspace
|
||||
| controller.EnterpriseWorkspaceNoOwnerEmail
|
||||
| controller.EnterpriseWorkspaceMember
|
||||
],
|
||||
) -> None:
|
||||
config_overrides(INNER_API=True, INNER_API_KEY="secret", DEPLOYMENT_EDITION="CLOUD")
|
||||
with app.test_request_context(method="POST", json={}, headers={"X-Inner-Api-Key": "secret"}):
|
||||
with pytest.raises(UnprocessableEntity):
|
||||
resource().post()
|
||||
provisioning.create.assert_not_called()
|
||||
provisioning.join_member.assert_not_called()
|
||||
|
||||
@staticmethod
|
||||
def test_inner_api_auth_precedes_payload_parsing(
|
||||
app: Flask, provisioning: Mock, config_overrides: Callable[..., None]
|
||||
) -> None:
|
||||
config_overrides(INNER_API=True, INNER_API_KEY="secret", DEPLOYMENT_EDITION="CLOUD")
|
||||
with app.test_request_context(method="POST", json={}):
|
||||
with pytest.raises(InnerApiUnauthorizedError):
|
||||
controller.EnterpriseWorkspace().post()
|
||||
provisioning.create.assert_not_called()
|
||||
|
||||
@staticmethod
|
||||
def test_admitted_request_delegates_to_service(
|
||||
app: Flask, provisioning: Mock, config_overrides: Callable[..., None]
|
||||
) -> None:
|
||||
config_overrides(INNER_API=True, INNER_API_KEY="secret", DEPLOYMENT_EDITION="CLOUD")
|
||||
provisioning.create.return_value = CreatedWorkspace("w", "Test", "sandbox", "normal", None, None, None, {})
|
||||
with app.test_request_context(method="POST", json={"name": "Test"}, headers={"X-Inner-Api-Key": "secret"}):
|
||||
controller.EnterpriseWorkspaceNoOwnerEmail().post()
|
||||
provisioning.create.assert_called_once_with(name="Test")
|
||||
Reference in new issue
Block a user