mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
refactor(plugins): migrate endpoint queries to generated contracts (#42685)
This commit is contained in:
26 files changed
+1240
-1689
No files matched your search
@@ -3,8 +3,10 @@ from datetime import UTC, datetime
|
||||
from types import FunctionType
|
||||
from unittest.mock import patch
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from flask_restx import Resource
|
||||
|
||||
from controllers.common.rbac import Workspace
|
||||
from controllers.console import console_ns
|
||||
@@ -22,13 +24,15 @@ from controllers.console.workspace.endpoint import (
|
||||
EndpointListForPluginQuery,
|
||||
EndpointListForSinglePluginApi,
|
||||
EndpointListQuery,
|
||||
EndpointMutationResponse,
|
||||
EndpointUpdatePayload,
|
||||
LegacyEndpointUpdatePayload,
|
||||
)
|
||||
from controllers.console.wraps import RBACPermission
|
||||
from core.entities.provider_entities import ProviderConfig, ProviderConfigType
|
||||
from core.plugin.entities.endpoint import EndpointEntityWithInstance, EndpointProviderDeclaration
|
||||
from core.plugin.impl.exc import PluginPermissionDeniedError
|
||||
from core.plugin.impl.exc import PluginDaemonInternalServerError, PluginPermissionDeniedError
|
||||
from libs.external_api import ExternalApi
|
||||
from tests.unit_tests.controllers.rbac_introspection import rbac_checks
|
||||
|
||||
|
||||
@@ -73,6 +77,36 @@ def test_endpoint_lists_require_management_permission(method: FunctionType) -> N
|
||||
|
||||
|
||||
class TestEndpointCollectionApi:
|
||||
@pytest.mark.parametrize("success", [True, False])
|
||||
def test_daemon_result_uses_http_success_or_error(self, success: bool) -> None:
|
||||
app = Flask(__name__)
|
||||
api = ExternalApi(app)
|
||||
method = inspect.unwrap(EndpointCollectionApi.post)
|
||||
payload = EndpointCreatePayload(plugin_unique_identifier="plugin-1", name="endpoint", settings={})
|
||||
|
||||
@api.route("/workspaces/current/endpoints")
|
||||
class EndpointMutationApi(Resource):
|
||||
def post(self) -> dict[str, object]:
|
||||
return method(EndpointCollectionApi(), payload, "t1", "u1")
|
||||
|
||||
with patch(
|
||||
"core.plugin.impl.base.BasePluginClient._request",
|
||||
return_value=httpx.Response(
|
||||
200,
|
||||
request=httpx.Request("POST", "https://daemon.test/endpoint/setup"),
|
||||
json={"code": 0, "message": "success", "data": success},
|
||||
),
|
||||
):
|
||||
response = app.test_client().post("/workspaces/current/endpoints")
|
||||
|
||||
if success:
|
||||
assert response.status_code == 200
|
||||
assert response.get_json() == {"success": True}
|
||||
else:
|
||||
assert response.status_code == 500
|
||||
assert response.get_json()["status"] == 500
|
||||
assert "success" not in response.get_json()
|
||||
|
||||
def test_create_success(self, app: Flask):
|
||||
api = EndpointCollectionApi()
|
||||
method = inspect.unwrap(api.post)
|
||||
@@ -253,11 +287,13 @@ class TestEndpointItemApi:
|
||||
|
||||
with (
|
||||
app.test_request_context("/", method="DELETE"),
|
||||
patch("controllers.console.workspace.endpoint.EndpointService.delete_endpoint", return_value=False),
|
||||
patch(
|
||||
"controllers.console.workspace.endpoint.EndpointService.delete_endpoint",
|
||||
side_effect=PluginDaemonInternalServerError("failed"),
|
||||
),
|
||||
):
|
||||
result = method(api, "t1", "u1", "e1")
|
||||
|
||||
assert result["success"] is False
|
||||
with pytest.raises(PluginDaemonInternalServerError):
|
||||
method(api, "t1", "u1", "e1")
|
||||
|
||||
def test_update_success(self, app: Flask):
|
||||
api = EndpointItemApi()
|
||||
@@ -311,11 +347,13 @@ class TestEndpointItemApi:
|
||||
|
||||
with (
|
||||
app.test_request_context("/", method="PATCH", json=payload),
|
||||
patch("controllers.console.workspace.endpoint.EndpointService.update_endpoint", return_value=False),
|
||||
patch(
|
||||
"controllers.console.workspace.endpoint.EndpointService.update_endpoint",
|
||||
side_effect=PluginDaemonInternalServerError("failed"),
|
||||
),
|
||||
):
|
||||
result = method(api, req_data, "t1", "u1", "e1")
|
||||
|
||||
assert result["success"] is False
|
||||
with pytest.raises(PluginDaemonInternalServerError):
|
||||
method(api, req_data, "t1", "u1", "e1")
|
||||
|
||||
|
||||
class TestDeprecatedEndpointDeleteApi:
|
||||
@@ -353,11 +391,13 @@ class TestDeprecatedEndpointDeleteApi:
|
||||
|
||||
with (
|
||||
app.test_request_context("/", json=payload),
|
||||
patch("controllers.console.workspace.endpoint.EndpointService.delete_endpoint", return_value=False),
|
||||
patch(
|
||||
"controllers.console.workspace.endpoint.EndpointService.delete_endpoint",
|
||||
side_effect=PluginDaemonInternalServerError("failed"),
|
||||
),
|
||||
):
|
||||
result = method(api, req_data, "t1", "u1")
|
||||
|
||||
assert result["success"] is False
|
||||
with pytest.raises(PluginDaemonInternalServerError):
|
||||
method(api, req_data, "t1", "u1")
|
||||
|
||||
|
||||
class TestDeprecatedEndpointUpdateApi:
|
||||
@@ -405,14 +445,22 @@ class TestDeprecatedEndpointUpdateApi:
|
||||
|
||||
with (
|
||||
app.test_request_context("/", json=payload),
|
||||
patch("controllers.console.workspace.endpoint.EndpointService.update_endpoint", return_value=False),
|
||||
patch(
|
||||
"controllers.console.workspace.endpoint.EndpointService.update_endpoint",
|
||||
side_effect=PluginDaemonInternalServerError("failed"),
|
||||
),
|
||||
):
|
||||
result = method(api, req_data, "t1", "u1")
|
||||
|
||||
assert result["success"] is False
|
||||
with pytest.raises(PluginDaemonInternalServerError):
|
||||
method(api, req_data, "t1", "u1")
|
||||
|
||||
|
||||
class TestEndpointRouteMetadata:
|
||||
def test_mutation_response_only_documents_success(self) -> None:
|
||||
success_schema = EndpointMutationResponse.model_json_schema()["properties"]["success"]
|
||||
|
||||
assert success_schema["const"] is True
|
||||
assert success_schema["type"] == "boolean"
|
||||
|
||||
def test_legacy_write_routes_are_marked_deprecated(self):
|
||||
assert DeprecatedEndpointCreateApi.post.__apidoc__["deprecated"] is True
|
||||
assert DeprecatedEndpointDeleteApi.post.__apidoc__["deprecated"] is True
|
||||
@@ -477,11 +525,13 @@ class TestEndpointEnableApi:
|
||||
|
||||
with (
|
||||
app.test_request_context("/", json=payload),
|
||||
patch("controllers.console.workspace.endpoint.EndpointService.enable_endpoint", return_value=False),
|
||||
patch(
|
||||
"controllers.console.workspace.endpoint.EndpointService.enable_endpoint",
|
||||
side_effect=PluginDaemonInternalServerError("failed"),
|
||||
),
|
||||
):
|
||||
result = method(api, req_data, "t1", "u1")
|
||||
|
||||
assert result["success"] is False
|
||||
with pytest.raises(PluginDaemonInternalServerError):
|
||||
method(api, req_data, "t1", "u1")
|
||||
|
||||
|
||||
class TestEndpointDisableApi:
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
from collections.abc import Callable
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
@@ -6,13 +9,46 @@ from core.plugin.impl.exc import PluginDaemonInternalServerError
|
||||
|
||||
|
||||
class TestPluginEndpointClientImpl:
|
||||
@pytest.mark.parametrize(
|
||||
"mutate",
|
||||
[
|
||||
pytest.param(
|
||||
lambda client: client.create_endpoint("tenant-1", "user-1", "org/plugin:1", "endpoint-a", {}),
|
||||
id="create",
|
||||
),
|
||||
pytest.param(
|
||||
lambda client: client.update_endpoint("tenant-1", "user-1", "endpoint-1", "renamed", {}),
|
||||
id="update",
|
||||
),
|
||||
pytest.param(lambda client: client.delete_endpoint("tenant-1", "user-1", "endpoint-1"), id="delete"),
|
||||
pytest.param(lambda client: client.enable_endpoint("tenant-1", "user-1", "endpoint-1"), id="enable"),
|
||||
pytest.param(lambda client: client.disable_endpoint("tenant-1", "user-1", "endpoint-1"), id="disable"),
|
||||
],
|
||||
)
|
||||
def test_mutation_rejects_false_daemon_result(
|
||||
self, mocker: MockerFixture, mutate: Callable[[PluginEndpointClient], bool]
|
||||
) -> None:
|
||||
client = PluginEndpointClient()
|
||||
mocker.patch.object(
|
||||
client,
|
||||
"_request",
|
||||
return_value=httpx.Response(
|
||||
200,
|
||||
request=httpx.Request("POST", "https://daemon.test/endpoint"),
|
||||
json={"code": 0, "message": "success", "data": False},
|
||||
),
|
||||
)
|
||||
|
||||
with pytest.raises(PluginDaemonInternalServerError, match="Plugin daemon failed to"):
|
||||
mutate(client)
|
||||
|
||||
def test_create_endpoint(self, mocker: MockerFixture):
|
||||
client = PluginEndpointClient()
|
||||
request_mock = mocker.patch.object(client, "_request_with_plugin_daemon_response", return_value=True)
|
||||
|
||||
result = client.create_endpoint("tenant-1", "user-1", "org/plugin:1", "endpoint-a", {"k": "v"})
|
||||
|
||||
assert result is True
|
||||
assert result
|
||||
assert request_mock.call_count == 1
|
||||
args = request_mock.call_args.args
|
||||
kwargs = request_mock.call_args.kwargs
|
||||
@@ -49,15 +85,15 @@ class TestPluginEndpointClientImpl:
|
||||
|
||||
result = client.update_endpoint("tenant-1", "user-1", "endpoint-1", "renamed", {"x": 1})
|
||||
|
||||
assert result is True
|
||||
assert result
|
||||
assert request_mock.call_args.args[:3] == ("POST", "plugin/tenant-1/endpoint/update", bool)
|
||||
|
||||
def test_enable_and_disable_endpoint(self, mocker: MockerFixture):
|
||||
client = PluginEndpointClient()
|
||||
request_mock = mocker.patch.object(client, "_request_with_plugin_daemon_response", return_value=True)
|
||||
|
||||
assert client.enable_endpoint("tenant-1", "user-1", "endpoint-1") is True
|
||||
assert client.disable_endpoint("tenant-1", "user-1", "endpoint-1") is True
|
||||
assert client.enable_endpoint("tenant-1", "user-1", "endpoint-1")
|
||||
assert client.disable_endpoint("tenant-1", "user-1", "endpoint-1")
|
||||
|
||||
calls = request_mock.call_args_list
|
||||
assert calls[0].args[1] == "plugin/tenant-1/endpoint/enable"
|
||||
@@ -68,7 +104,7 @@ class TestPluginEndpointClientImpl:
|
||||
request_mock = mocker.patch.object(client, "_request_with_plugin_daemon_response")
|
||||
|
||||
request_mock.side_effect = PluginDaemonInternalServerError("record not found")
|
||||
assert client.delete_endpoint("tenant-1", "user-1", "endpoint-1") is True
|
||||
assert client.delete_endpoint("tenant-1", "user-1", "endpoint-1")
|
||||
|
||||
request_mock.side_effect = PluginDaemonInternalServerError("permission denied")
|
||||
with pytest.raises(PluginDaemonInternalServerError) as exc_info:
|
||||
|
||||
Reference in new issue
Block a user