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.

6.4 KB
Raw Blame
from __future__ import annotations

from collections.abc import Callable
from typing import Protocol, cast
from uuid import uuid4

import pytest
from flask import Flask
from sqlalchemy.orm import Session
from werkzeug.exceptions import NotFound

from controllers.openapi._models import AppDescribeQuery, AppDescribeResponse, AppListQuery, AppListResponse
from controllers.openapi.apps import AppDescribeApi, AppListApi
from machinery.context import AppRequestContext, RequestContext
from models import Account, App
from services.app_service import AppService, CreateAppParams


class _Endpoint[T](Protocol):
    __handler__: Callable[..., T]


def _create_app(
    db_session: Session,
    account: Account,
    *,
    name: str,
    enable_api: bool = True,
) -> App:
    """Create a workflow app in the account's owner tenant.

    Workflow mode is used because its template seeds no ``model_config``, so
    ``AppService.create_app`` never reaches ``ModelManager`` — keeping the
    fixture free of model-runtime patching.
    """
    tenant = account.current_tenant
    assert tenant is not None
    params = CreateAppParams(
        name=name,
        description="",
        mode="workflow",
        icon_type="emoji",
        icon="🤖",
        icon_background="#FF6B6B",
    )
    app_model = AppService().create_app(tenant.id, params, account, session=db_session)
    # The openapi surface gate keys off ``enable_api``; flip it explicitly so
    # the test states the visibility precondition rather than relying on the
    # template default.
    app_model.enable_api = enable_api
    db_session.add(app_model)
    db_session.commit()
    return app_model


class TestAppList:
    def test_lists_only_api_enabled_apps_in_workspace(
        self, app: Flask, db_session_with_containers: Session, make_account: Callable[..., Account]
    ) -> None:
        account = make_account()
        tenant = account.current_tenant
        assert tenant is not None
        visible = _create_app(db_session_with_containers, account, name="Visible", enable_api=True)
        _create_app(db_session_with_containers, account, name="Hidden", enable_api=False)

        api = AppListApi()
        with app.test_request_context(f"/openapi/v1/apps?workspace_id={tenant.id}"):
            result = cast(_Endpoint[AppListResponse], api.get).__handler__(
                api,
                RequestContext("request", None, account.id, tenant.id),
                query=AppListQuery(workspace_id=str(tenant.id)),
            )

        # The api-disabled app is gated out, so it counts neither in `data`
        # nor in `total` (the gate is pushed into the query for stable paging).
        assert result.total == 1
        assert [row.id for row in result.data] == [visible.id]
        assert result.has_more is False

    def test_uuid_name_filter_returns_matching_app(
        self, app: Flask, db_session_with_containers: Session, make_account: Callable[..., Account]
    ) -> None:
        account = make_account()
        tenant = account.current_tenant
        assert tenant is not None
        target = _create_app(db_session_with_containers, account, name="Target", enable_api=True)

        api = AppListApi()
        with app.test_request_context(f"/openapi/v1/apps?workspace_id={tenant.id}&name={target.id}"):
            result = cast(_Endpoint[AppListResponse], api.get).__handler__(
                api,
                RequestContext("request", None, account.id, tenant.id),
                query=AppListQuery(workspace_id=str(tenant.id), name=str(target.id)),
            )

        assert result.total == 1
        assert result.data[0].id == target.id
        assert result.data[0].workspace_id == str(tenant.id)

    def test_uuid_name_filter_for_foreign_app_returns_empty(
        self, app: Flask, db_session_with_containers: Session, make_account: Callable[..., Account]
    ) -> None:
        """A UUID that resolves to an app in another workspace must not leak
        across tenants — the list returns empty rather than the foreign app."""
        owner = make_account()
        outsider = make_account()
        foreign_app = _create_app(db_session_with_containers, owner, name="Foreign", enable_api=True)
        outsider_tenant = outsider.current_tenant
        assert outsider_tenant is not None

        api = AppListApi()
        with app.test_request_context(f"/openapi/v1/apps?workspace_id={outsider_tenant.id}&name={foreign_app.id}"):
            result = cast(_Endpoint[AppListResponse], api.get).__handler__(
                api,
                RequestContext("request", None, outsider.id, outsider_tenant.id),
                query=AppListQuery(workspace_id=str(outsider_tenant.id), name=str(foreign_app.id)),
            )

        assert result.total == 0
        assert result.data == []


class TestAppDescribe:
    def test_describe_info_returns_metadata(
        self, app: Flask, db_session_with_containers: Session, make_account: Callable[..., Account]
    ) -> None:
        account = make_account()
        app_model = _create_app(db_session_with_containers, account, name="Describe Me", enable_api=True)

        api = AppDescribeApi()
        with app.test_request_context(f"/openapi/v1/apps/{app_model.id}?fields=info"):
            result = cast(_Endpoint[AppDescribeResponse], api.get).__handler__(
                api,
                AppRequestContext(tenant_id=app_model.tenant_id, app_id=app_model.id),
                app_model.id,
                query=AppDescribeQuery(fields="info"),
            )

        assert result.info is not None
        assert result.info.id == app_model.id
        assert result.info.name == "Describe Me"
        assert result.info.service_api_enabled is True
        # Only the requested block is materialized.
        assert result.parameters is None
        assert result.input_schema is None

    def test_describe_unknown_app_is_404(self, app: Flask, make_account: Callable[..., Account]) -> None:
        account = make_account()
        tenant = account.current_tenant
        assert tenant is not None
        missing_id = str(uuid4())

        api = AppDescribeApi()
        with app.test_request_context(f"/openapi/v1/apps/{missing_id}"):
            with pytest.raises(NotFound):
                cast(_Endpoint[AppDescribeResponse], api.get).__handler__(
                    api,
                    AppRequestContext(tenant_id=tenant.id, app_id=missing_id),
                    missing_id,
                    query=AppDescribeQuery(),
                )