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.

8.7 KB
Raw Blame
"""The catalog on /openapi/v1: every guarded route is in it, and its schemas are a shape an agent can read raw."""

import hashlib
import re
from collections.abc import Iterator

import pytest
from flask import Flask
from flask_restx.model import ModelBase
from jsonschema import Draft202012Validator

from configs import dify_config
from controllers.common.fields import EventStreamResponse
from controllers.openapi import bp as openapi_bp
from controllers.openapi._catalog import (
    CATALOG_HEADER,
    CATALOG_PATH,
    CatalogOp,
    build_catalog,
    catalog_for,
    iter_handlers,
)
from controllers.openapi._errors import OpenApiErrorCode
from controllers.openapi._models import Hinted
from controllers.openapi.auth.spec import Effect, EndpointSpec, Kind, spec_of
from tests.unit_tests.controllers.openapi.conftest import AdmittedWorld

OP_ID_RE = re.compile(r"^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$")
MAX_DEPTH = 4
_UNCATALOGUED_PREFIX = "/openapi/v1/oauth/"
# The client fills these from its own context (the workspace pin), so examples leave them out.
_CLIENT_CONTEXT = frozenset({"workspace_id"})
_UNCATALOGUED = {
    "/openapi/v1/",
    "/openapi/v1/openapi.json",
    "/openapi/v1/_health",
    "/openapi/v1/_version",
    CATALOG_PATH,
}


@pytest.fixture
def app() -> Flask:
    a = Flask(__name__)
    a.config["TESTING"] = True
    a.register_blueprint(openapi_bp)
    return a


@pytest.fixture
def ops(app: Flask) -> dict[str, CatalogOp]:
    return build_catalog(app)["ops"]


def _response_model_names(fn: object) -> set[str]:
    responses: dict[str, tuple[object, ...]] = vars(fn).get("__apidoc__", {}).get("responses", {})
    return {entry[1].name for entry in responses.values() if isinstance(entry[1], ModelBase)}


def _walk(
    node: object, path: tuple[str, ...] = (), depth: int = 0
) -> Iterator[tuple[tuple[str, ...], int, dict[str, object]]]:
    if isinstance(node, dict):
        yield path, depth, node
        for key in ("properties", "$defs"):
            for name, child in (node.get(key) or {}).items():
                yield from _walk(child, (*path, name), depth + 1)
        for key in ("items", "additionalProperties"):
            if isinstance(node.get(key), dict):
                yield from _walk(node[key], (*path, key), depth + 1)
        for alt in node.get("anyOf") or []:
            yield from _walk(alt, path, depth)


def test_every_guarded_route_declares_catalog_meta_once(app: Flask, ops: dict[str, CatalogOp]) -> None:
    seen: dict[str, str] = {}
    streaming: set[str] = set()
    for rule, verb, fn in iter_handlers(app):
        spec = spec_of(fn)
        if rule.rule.startswith(_UNCATALOGUED_PREFIX) or rule.rule in _UNCATALOGUED:
            assert spec is None, f"{verb} {rule.rule} must stay out of the catalog"
            continue
        assert isinstance(spec, EndpointSpec), f"{verb} {rule.rule}"
        meta = spec.catalog
        assert OP_ID_RE.fullmatch(meta.op), meta.op
        assert meta.summary.strip(), meta.op
        assert meta.op not in seen, f"{meta.op} declared twice: {seen[meta.op]} and {verb} {rule.rule}"
        seen[meta.op] = f"{verb} {rule.rule}"
        admitted = spec.edition is None or dify_config.DEPLOYMENT_EDITION in spec.edition
        assert (meta.op in ops) is admitted, meta.op
        if EventStreamResponse.__name__ in _response_model_names(fn):
            streaming.add(meta.op)
            assert meta.kind is Kind.SSE, meta.op
    assert set(ops) <= set(seen)
    per_mode = {f"run.console_app.{mode}" for mode in ("workflow", "chat", "advanced_chat", "completion")}
    draft = {f"test.console_app.{mode}" for mode in ("workflow", "advanced_chat")}
    assert streaming == per_mode | draft | {"get.run.event"}


# Every op whose effect differs from its HTTP method, plus one default per method.
@pytest.mark.parametrize(
    ("op", "effect"),
    [
        ("reset.webapp", Effect.DESTRUCTIVE),
        ("reset.webapp.agent", Effect.DESTRUCTIVE),
        ("restore.console_app.version", Effect.DESTRUCTIVE),
        ("import.console_app.dsl", Effect.DESTRUCTIVE),
        ("confirm.console_app.dsl_import", Effect.DESTRUCTIVE),
        ("import.skill", Effect.DESTRUCTIVE),
        ("stop.run", Effect.DESTRUCTIVE),
        ("stop.task", Effect.DESTRUCTIVE),
        ("check.console_app.dsl", Effect.READ),
        ("run.console_app.chat", Effect.WRITE),
        ("get.run.event", Effect.READ),
    ],
)
def test_catalog_rows_carry_the_op_effect(ops: dict[str, CatalogOp], op: str, effect: Effect) -> None:
    assert ops[op]["effect"] == effect.value


def test_run_entries_carry_path_bind_kind_and_flags(ops: dict[str, CatalogOp]) -> None:
    chat = ops["run.console_app.chat"]
    assert set(chat) == {
        "summary",
        "method",
        "path",
        "kind",
        "effect",
        "input",
        "bind",
        "internal",
        "deprecated",
        "examples",
    }
    assert (chat["method"], chat["path"], chat["kind"]) == (
        "POST",
        "/openapi/v1/apps/{app_id}/chat:run",
        "sse",
    )
    assert {"app_id", "query"} <= set(chat["input"]["required"])
    assert set(chat["bind"]) == set(chat["input"]["properties"])
    assert {k: chat["bind"][k] for k in ("app_id", "inputs", "files", "attachments")} == {
        "app_id": "path",
        "inputs": "body",
        "files": "file",
        "attachments": "file",
    }
    assert chat["deprecated"] is False
    assert ops["upload.console_app.file"]["bind"]["file"] == "file"
    assert ops["get.console_app"]["bind"]["page"] == "query"
    assert ops["get.run.event"]["bind"]["continue_on_pause"] == "query"


def test_input_schemas_are_flat_shallow_and_described(ops: dict[str, CatalogOp]) -> None:
    bad = [
        (op, p, k)
        for op, e in ops.items()
        for p, d, n in _walk(e["input"])
        for k in ("oneOf", "$ref", "$defs")
        if k in n or d > MAX_DEPTH
    ]
    assert bad == []
    undocumented = [
        (op, name)
        for op, e in ops.items()
        for name, prop in e["input"]["properties"].items()
        if "object" in {prop.get("type")} | {a.get("type") for a in prop.get("anyOf", [])}
        and not prop.get("description")
    ]
    assert undocumented == []
    assert [
        op for op, e in ops.items() if e["kind"] == "list" and not {"page", "limit"} <= set(e["input"]["properties"])
    ] == []
    assert [op for op, e in ops.items() if set(Hinted.model_fields) & set(e["input"]["properties"])] == []
    desc = ops["run.console_app.chat"]["input"]["properties"]["inputs"]["description"]
    assert "describe.console_app" in desc
    assert "input_schema" in desc


def test_every_op_carries_examples_that_pass_its_input_schema(ops: dict[str, CatalogOp]) -> None:
    assert [op for op, e in ops.items() if not e["examples"]] == []
    bad = []
    for op, e in ops.items():
        schema = {
            **e["input"],
            "required": [name for name in e["input"]["required"] if name not in _CLIENT_CONTEXT],
            "additionalProperties": False,
        }
        validator = Draft202012Validator(schema)
        for example in e["examples"]:
            assert example["title"].strip(), op
            bad.extend((op, example["title"], err.message) for err in validator.iter_errors(example["input"]))
    assert bad == []


def test_catalog_route_serves_canonical_bytes_and_every_response_carries_the_fingerprint(app: Flask) -> None:
    client = app.test_client()
    raw, fingerprint = catalog_for(app)
    res = client.get(CATALOG_PATH)
    assert (res.status_code, res.mimetype, res.data) == (200, "application/json", raw)
    assert fingerprint == hashlib.sha256(raw).hexdigest() == res.headers[CATALOG_HEADER]
    assert client.get("/openapi/v1/apps").headers[CATALOG_HEADER] == fingerprint
    assert client.get("/openapi/v1/does-not-exist").headers[CATALOG_HEADER] == fingerprint


def test_a_guarded_route_refuses_a_request_that_does_not_name_the_current_catalog(
    admitted_bearer: AdmittedWorld,
) -> None:
    """The wire shape of the refusal: 412 with the canonical body, and the
    current fingerprint on the response so the client can refetch and retry.
    The same bearer with the current fingerprint gets past the guard: the
    unknown query answers 422 from `@accepts`, which sits inside it.
    """
    path = f"/openapi/v1/workspaces/{admitted_bearer.workspace_id}/members?nope=1"
    current = admitted_bearer.headers[CATALOG_HEADER]
    without = {"Authorization": admitted_bearer.headers["Authorization"]}

    refused = admitted_bearer.client.get(path, headers=without)
    admitted = admitted_bearer.client.get(path, headers=admitted_bearer.headers)

    assert refused.status_code == 412
    assert refused.get_json()["code"] == OpenApiErrorCode.CATALOG_STALE
    assert refused.headers[CATALOG_HEADER] == current
    assert admitted.status_code == 422