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.

2.5 KB
Raw Blame
from __future__ import annotations

from collections.abc import Callable
from io import BytesIO

from flask import Flask
from sqlalchemy.orm import Session
from werkzeug.datastructures import FileStorage

from controllers.openapi._models import FileUploadPayload
from controllers.openapi.auth.requirements import CheckWorkspaceMember
from controllers.openapi.files import AppFileUploadApi
from models import Account, App
from services.app_service import AppService, CreateAppParams
from tests.e2e_tests.containers.controllers.openapi.conftest import context_for


def _create_app(db_session: Session, account: Account, *, name: str = "Uploader") -> App:
    """Create a workflow app (no model_config → no ModelManager) for the upload context."""
    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)
    db_session.commit()
    return app_model


class TestAppFileUpload:
    def test_upload_persists_and_returns_metadata(
        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
        app_model = _create_app(db_session_with_containers, account)
        content = b"hello integration world"

        api = AppFileUploadApi()
        body = FileUploadPayload(
            file=FileStorage(stream=BytesIO(content), filename="note.txt", content_type="text/plain")
        )
        with app.test_request_context(f"/openapi/v1/apps/{app_model.id}/files", method="POST"):
            result = vars(api.post)["__handler__"](
                api,
                context_for(
                    account,
                    session=db_session_with_containers,
                    view_args={"app_id": app_model.id},
                    requirements=(CheckWorkspaceMember(),),
                ),
                app_model.id,
                body=body,
            )

        assert result.id
        assert result.name == "note.txt"
        assert result.size == len(content)
        assert result.extension == "txt"
        assert result.mime_type == "text/plain"
        # Persisted under the caller's tenant.
        assert result.tenant_id == str(tenant.id)