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.

18.7 KB
Raw Blame
"""
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="[email protected]")
    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"}