Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

20.2 KB
Raw Blame
import inspect
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
from controllers.console.workspace.endpoint import (
    DeprecatedEndpointCreateApi,
    DeprecatedEndpointDeleteApi,
    DeprecatedEndpointUpdateApi,
    EndpointCollectionApi,
    EndpointCreatePayload,
    EndpointDisableApi,
    EndpointEnableApi,
    EndpointIdPayload,
    EndpointItemApi,
    EndpointListApi,
    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 PluginDaemonInternalServerError, PluginPermissionDeniedError
from libs.external_api import ExternalApi
from tests.helpers.unit.controllers.rbac_introspection import rbac_checks


def _endpoint_entity() -> EndpointEntityWithInstance:
    now = datetime(2026, 1, 1, tzinfo=UTC)
    return EndpointEntityWithInstance(
        id="e1",
        created_at=now,
        updated_at=now,
        tenant_id="t1",
        plugin_id="p1",
        settings={
            "api_key": "pl********et",
            "enabled": True,
            "ids": ["a", "b"],
            "nested": {"limit": 3},
        },
        expired_at=now,
        declaration=EndpointProviderDeclaration(
            settings=[
                ProviderConfig(type=ProviderConfigType.SECRET_INPUT, name="api_key"),
                ProviderConfig(type=ProviderConfigType.BOOLEAN, name="enabled"),
            ]
        ),
        name="endpoint",
        enabled=True,
        url="https://example.test/hook-1",
        hook_id="hook-1",
    )


@pytest.mark.parametrize("method", [EndpointListApi.get, EndpointListForSinglePluginApi.get])
def test_endpoint_lists_require_management_permission(method: FunctionType) -> None:
    legacy_wrapper = inspect.unwrap(
        method, stop=lambda wrapper: "is_admin_or_owner_required" in wrapper.__code__.co_qualname
    )
    assert "is_admin_or_owner_required" in legacy_wrapper.__code__.co_qualname

    [check] = rbac_checks(method)
    assert check.scene == RBACPermission.PLUGIN_MODEL_CONFIG
    assert isinstance(check.locator, Workspace)


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)

        payload = {
            "plugin_unique_identifier": "plugin-1",
            "name": "endpoint",
            "settings": {"a": 1},
        }
        req_data = EndpointCreatePayload(**payload)

        with (
            app.test_request_context("/", json=payload),
            patch("controllers.console.workspace.endpoint.EndpointService.create_endpoint", return_value=True),
        ):
            result = method(api, req_data, "t1", "u1")

        assert result["success"] is True

    def test_create_permission_denied(self, app: Flask):
        api = EndpointCollectionApi()
        method = inspect.unwrap(api.post)

        payload = {
            "plugin_unique_identifier": "plugin-1",
            "name": "endpoint",
            "settings": {},
        }
        req_data = EndpointCreatePayload(**payload)

        with (
            app.test_request_context("/", json=payload),
            patch(
                "controllers.console.workspace.endpoint.EndpointService.create_endpoint",
                side_effect=PluginPermissionDeniedError("denied"),
            ),
        ):
            with pytest.raises(ValueError, match="denied"):
                method(api, req_data, "t1", "u1")

    def test_create_validation_error(self, app: Flask):
        api = EndpointCollectionApi()
        method = inspect.unwrap(api.post)

        payload = {
            "plugin_unique_identifier": "p1",
            "name": "",
            "settings": {},
        }

        with (
            app.test_request_context("/", json=payload),
        ):
            with pytest.raises(ValueError, match="1 validation error for EndpointCreatePayload\\\nname"):
                method(api, EndpointCreatePayload(**payload), "t1", "u1")


class TestDeprecatedEndpointCreateApi:
    def test_create_success(self, app: Flask):
        api = DeprecatedEndpointCreateApi()
        method = inspect.unwrap(api.post)

        payload = {
            "plugin_unique_identifier": "plugin-1",
            "name": "endpoint",
            "settings": {"a": 1},
        }
        req_data = EndpointCreatePayload(**payload)

        with (
            app.test_request_context("/", json=payload),
            patch("controllers.console.workspace.endpoint.EndpointService.create_endpoint", return_value=True),
        ):
            result = method(api, req_data, "t1", "u1")

        assert result["success"] is True


class TestEndpointListApi:
    def test_list_success(self, app: Flask):
        api = EndpointListApi()
        method = inspect.unwrap(api.get)
        endpoint_entity = _endpoint_entity()

        with (
            app.test_request_context("/?page=1&page_size=10"),
            patch(
                "controllers.console.workspace.endpoint.EndpointService.list_endpoints",
                return_value=[endpoint_entity],
            ),
        ):
            result = method(api, EndpointListQuery(page=1, page_size=10), "t1", "u1")

        endpoint = result["endpoints"][0]
        assert endpoint["id"] == "e1"
        assert endpoint["created_at"] == "2026-01-01T00:00:00Z"
        assert endpoint["updated_at"] == "2026-01-01T00:00:00Z"
        assert endpoint["settings"] == {
            "api_key": "pl********et",
            "enabled": True,
            "ids": ["a", "b"],
            "nested": {"limit": 3},
        }
        assert endpoint["tenant_id"] == "t1"
        assert endpoint["plugin_id"] == "p1"
        assert endpoint["expired_at"] == "2026-01-01T00:00:00Z"
        assert endpoint["declaration"]["settings"][0]["type"] == "secret-input"
        assert endpoint["declaration"]["settings"][0]["name"] == "api_key"
        assert endpoint["declaration"]["settings"][1]["type"] == "boolean"
        assert endpoint["declaration"]["settings"][1]["name"] == "enabled"
        assert endpoint["declaration"]["endpoints"] == []
        assert endpoint["name"] == "endpoint"
        assert endpoint["enabled"] is True
        assert endpoint["url"] == "https://example.test/hook-1"
        assert endpoint["hook_id"] == "hook-1"
        assert endpoint_entity.settings["api_key"] == "pl********et"

    def test_list_invalid_query(self, app: Flask):
        api = EndpointListApi()
        method = inspect.unwrap(api.get)

        with (
            app.test_request_context("/?page=0&page_size=10"),
        ):
            with pytest.raises(ValueError, match="1 validation error for EndpointListQuery\\\npage"):
                method(api, EndpointListQuery(page=0, page_size=10), "t1", "u1")


class TestEndpointListForSinglePluginApi:
    def test_list_for_plugin_success(self, app: Flask):
        api = EndpointListForSinglePluginApi()
        method = inspect.unwrap(api.get)

        with (
            app.test_request_context("/?page=1&page_size=10&plugin_id=p1"),
            patch(
                "controllers.console.workspace.endpoint.EndpointService.list_endpoints_for_single_plugin",
                return_value=[_endpoint_entity()],
            ),
        ):
            result = method(api, EndpointListForPluginQuery(page=1, page_size=10, plugin_id="p1"), "t1", "u1")

        assert result["endpoints"][0]["id"] == "e1"
        assert result["endpoints"][0]["settings"]["api_key"] == "pl********et"
        assert result["endpoints"][0]["settings"]["nested"] == {"limit": 3}

    def test_list_for_plugin_missing_param(self, app: Flask):
        api = EndpointListForSinglePluginApi()
        method = inspect.unwrap(api.get)

        with (
            app.test_request_context("/?page=1&page_size=10"),
        ):
            with pytest.raises(ValueError, match="1 validation error for EndpointListForPluginQuery\\\nplugin_id"):
                method(api, EndpointListForPluginQuery(page=1, page_size=10), "t1", "u1")


class TestEndpointItemApi:
    def test_delete_success(self, app: Flask):
        api = EndpointItemApi()
        method = inspect.unwrap(api.delete)

        with (
            app.test_request_context("/", method="DELETE"),
            patch(
                "controllers.console.workspace.endpoint.EndpointService.delete_endpoint",
                return_value=True,
            ) as mock_delete,
        ):
            result = method(api, "t1", "u1", "e1")

        assert result["success"] is True
        mock_delete.assert_called_once_with(tenant_id="t1", user_id="u1", endpoint_id="e1")

    def test_delete_service_failure(self, app: Flask):
        api = EndpointItemApi()
        method = inspect.unwrap(api.delete)

        with (
            app.test_request_context("/", method="DELETE"),
            patch(
                "controllers.console.workspace.endpoint.EndpointService.delete_endpoint",
                side_effect=PluginDaemonInternalServerError("failed"),
            ),
        ):
            with pytest.raises(PluginDaemonInternalServerError):
                method(api, "t1", "u1", "e1")

    def test_update_success(self, app: Flask):
        api = EndpointItemApi()
        method = inspect.unwrap(api.patch)

        payload = {
            "name": "new-name",
            "settings": {"x": 1},
        }
        req_data = EndpointUpdatePayload(**payload)

        with (
            app.test_request_context("/", method="PATCH", json=payload),
            patch(
                "controllers.console.workspace.endpoint.EndpointService.update_endpoint",
                return_value=True,
            ) as mock_update,
        ):
            result = method(api, req_data, "t1", "u1", "e1")

        assert result["success"] is True
        mock_update.assert_called_once_with(
            tenant_id="t1",
            user_id="u1",
            endpoint_id="e1",
            name="new-name",
            settings={"x": 1},
        )

    def test_update_validation_error(self, app: Flask):
        api = EndpointItemApi()
        method = inspect.unwrap(api.patch)

        payload = {"settings": {}}

        with (
            app.test_request_context("/", method="PATCH", json=payload),
        ):
            with pytest.raises(ValueError, match="1 validation error for EndpointUpdatePayload\\\nname"):
                method(api, EndpointUpdatePayload(**payload), "t1", "u1", "e1")

    def test_update_service_failure(self, app: Flask):
        api = EndpointItemApi()
        method = inspect.unwrap(api.patch)

        payload = {
            "name": "n",
            "settings": {},
        }
        req_data = EndpointUpdatePayload(**payload)

        with (
            app.test_request_context("/", method="PATCH", json=payload),
            patch(
                "controllers.console.workspace.endpoint.EndpointService.update_endpoint",
                side_effect=PluginDaemonInternalServerError("failed"),
            ),
        ):
            with pytest.raises(PluginDaemonInternalServerError):
                method(api, req_data, "t1", "u1", "e1")


class TestDeprecatedEndpointDeleteApi:
    def test_delete_success(self, app: Flask):
        api = DeprecatedEndpointDeleteApi()
        method = inspect.unwrap(api.post)

        payload = {"endpoint_id": "e1"}
        req_data = EndpointIdPayload(**payload)

        with (
            app.test_request_context("/", json=payload),
            patch("controllers.console.workspace.endpoint.EndpointService.delete_endpoint", return_value=True),
        ):
            result = method(api, req_data, "t1", "u1")

        assert result["success"] is True

    def test_delete_invalid_payload(self, app: Flask):
        api = DeprecatedEndpointDeleteApi()
        method = inspect.unwrap(api.post)

        with (
            app.test_request_context("/", json={}),
        ):
            with pytest.raises(ValueError, match="1 validation error for EndpointIdPayload\\\nendpoint_id"):
                method(api, EndpointIdPayload(), "t1", "u1")

    def test_delete_service_failure(self, app: Flask):
        api = DeprecatedEndpointDeleteApi()
        method = inspect.unwrap(api.post)

        payload = {"endpoint_id": "e1"}
        req_data = EndpointIdPayload(**payload)

        with (
            app.test_request_context("/", json=payload),
            patch(
                "controllers.console.workspace.endpoint.EndpointService.delete_endpoint",
                side_effect=PluginDaemonInternalServerError("failed"),
            ),
        ):
            with pytest.raises(PluginDaemonInternalServerError):
                method(api, req_data, "t1", "u1")


class TestDeprecatedEndpointUpdateApi:
    def test_update_success(self, app: Flask):
        api = DeprecatedEndpointUpdateApi()
        method = inspect.unwrap(api.post)

        payload = {
            "endpoint_id": "e1",
            "name": "new-name",
            "settings": {"x": 1},
        }
        req_data = LegacyEndpointUpdatePayload(**payload)

        with (
            app.test_request_context("/", json=payload),
            patch("controllers.console.workspace.endpoint.EndpointService.update_endpoint", return_value=True),
        ):
            result = method(api, req_data, "t1", "u1")

        assert result["success"] is True

    def test_update_validation_error(self, app: Flask):
        api = DeprecatedEndpointUpdateApi()
        method = inspect.unwrap(api.post)

        payload = {"endpoint_id": "e1", "settings": {}}

        with (
            app.test_request_context("/", json=payload),
        ):
            with pytest.raises(ValueError, match="1 validation error for LegacyEndpointUpdatePayload\\\nname"):
                method(api, LegacyEndpointUpdatePayload(**payload), "t1", "u1")

    def test_update_service_failure(self, app: Flask):
        api = DeprecatedEndpointUpdateApi()
        method = inspect.unwrap(api.post)

        payload = {
            "endpoint_id": "e1",
            "name": "n",
            "settings": {},
        }
        req_data = LegacyEndpointUpdatePayload(**payload)

        with (
            app.test_request_context("/", json=payload),
            patch(
                "controllers.console.workspace.endpoint.EndpointService.update_endpoint",
                side_effect=PluginDaemonInternalServerError("failed"),
            ),
        ):
            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
        assert DeprecatedEndpointUpdateApi.post.__apidoc__["deprecated"] is True
        assert EndpointCollectionApi.post.__apidoc__.get("deprecated") is not True
        assert EndpointItemApi.delete.__apidoc__.get("deprecated") is not True
        assert EndpointItemApi.patch.__apidoc__.get("deprecated") is not True

    def test_canonical_and_legacy_write_routes_are_registered(self):
        route_map = {
            resource.__name__: urls
            for resource, urls, _route_doc, _kwargs in console_ns.resources
            if resource.__name__
            in {
                "EndpointCollectionApi",
                "EndpointItemApi",
                "DeprecatedEndpointCreateApi",
                "DeprecatedEndpointDeleteApi",
                "DeprecatedEndpointUpdateApi",
            }
        }

        assert route_map["EndpointCollectionApi"] == ("/workspaces/current/endpoints",)
        assert route_map["EndpointItemApi"] == ("/workspaces/current/endpoints/<string:id>",)
        assert route_map["DeprecatedEndpointCreateApi"] == ("/workspaces/current/endpoints/create",)
        assert route_map["DeprecatedEndpointDeleteApi"] == ("/workspaces/current/endpoints/delete",)
        assert route_map["DeprecatedEndpointUpdateApi"] == ("/workspaces/current/endpoints/update",)


class TestEndpointEnableApi:
    def test_enable_success(self, app: Flask):
        api = EndpointEnableApi()
        method = inspect.unwrap(api.post)

        payload = {"endpoint_id": "e1"}
        req_data = EndpointIdPayload(**payload)

        with (
            app.test_request_context("/", json=payload),
            patch("controllers.console.workspace.endpoint.EndpointService.enable_endpoint", return_value=True),
        ):
            result = method(api, req_data, "t1", "u1")

        assert result["success"] is True

    def test_enable_invalid_payload(self, app: Flask):
        api = EndpointEnableApi()
        method = inspect.unwrap(api.post)

        with (
            app.test_request_context("/", json={}),
        ):
            with pytest.raises(ValueError, match="1 validation error for EndpointIdPayload\\\nendpoint_id"):
                method(api, EndpointIdPayload(), "t1", "u1")

    def test_enable_service_failure(self, app: Flask):
        api = EndpointEnableApi()
        method = inspect.unwrap(api.post)

        payload = {"endpoint_id": "e1"}
        req_data = EndpointIdPayload(**payload)

        with (
            app.test_request_context("/", json=payload),
            patch(
                "controllers.console.workspace.endpoint.EndpointService.enable_endpoint",
                side_effect=PluginDaemonInternalServerError("failed"),
            ),
        ):
            with pytest.raises(PluginDaemonInternalServerError):
                method(api, req_data, "t1", "u1")


class TestEndpointDisableApi:
    def test_disable_success(self, app: Flask):
        api = EndpointDisableApi()
        method = inspect.unwrap(api.post)

        payload = {"endpoint_id": "e1"}
        req_data = EndpointIdPayload(**payload)

        with (
            app.test_request_context("/", json=payload),
            patch("controllers.console.workspace.endpoint.EndpointService.disable_endpoint", return_value=True),
        ):
            result = method(api, req_data, "t1", "u1")

        assert result["success"] is True

    def test_disable_invalid_payload(self, app: Flask):
        api = EndpointDisableApi()
        method = inspect.unwrap(api.post)

        with (
            app.test_request_context("/", json={}),
        ):
            with pytest.raises(ValueError, match="1 validation error for EndpointIdPayload\\\nendpoint_id"):
                method(api, EndpointIdPayload(), "t1", "u1")