chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
@@ -0,0 +1,865 @@
|
||||
"""Tests for the file endpoints reached with an AppDeploy file grant."""
|
||||
|
||||
import time
|
||||
from collections.abc import Callable, Iterator
|
||||
from datetime import datetime
|
||||
from io import BytesIO
|
||||
from types import SimpleNamespace
|
||||
from typing import IO, cast, overload
|
||||
from unittest.mock import MagicMock, patch
|
||||
from uuid import UUID
|
||||
|
||||
import jwt
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from pydantic import TypeAdapter
|
||||
from sqlalchemy import null, update
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from controllers.common.errors import (
|
||||
BlockedFileExtensionError,
|
||||
FilenameNotExistsError,
|
||||
FileTooLargeError,
|
||||
NoFileUploadedError,
|
||||
TooManyFilesError,
|
||||
)
|
||||
from controllers.files.appdeploy_files import (
|
||||
GrantedFileContentApi,
|
||||
GrantedFileResolveApi,
|
||||
GrantedFileUploadApi,
|
||||
GrantedRemoteFileUploadApi,
|
||||
InvalidFileRequestError,
|
||||
ProducedFileApi,
|
||||
)
|
||||
from controllers.files.wraps import FileGrantInvalidError, GrantedFileNotFoundError
|
||||
from extensions.ext_application_services import _build_file_grant_service
|
||||
from extensions.storage.storage_type import StorageType
|
||||
from libs.datetime_utils import naive_utc_now
|
||||
from models.enums import CreatorUserRole, EndUserType
|
||||
from models.model import EndUser, UploadFile
|
||||
from models.tools import ToolFile
|
||||
from services.entities.file_grant_entities import FileGrantContext, FileGrantScope, FileKind, RemoteFile
|
||||
from services.errors.file import FileTooLargeError as FileTooLargeServiceError
|
||||
from services.file_grant_gateways import FILE_CONTENT_AUDIENCE, FileGrantFileGateway
|
||||
from services.file_grant_service import MAX_FILE_GRANT_REFS, FileGrantService
|
||||
from tests.helpers.unit.file_grant_test_utils import issue_file_grant
|
||||
|
||||
CONTROLLER_MODULE = "controllers.files.appdeploy_files"
|
||||
|
||||
SECRET_KEY = "file-grant-test-secret-long-enough-for-hs256"
|
||||
TENANT_ID = "11111111-1111-4111-8111-111111111111"
|
||||
OTHER_TENANT_ID = "1a1a1a1a-1111-4111-8111-111111111111"
|
||||
APP_ID = "22222222-2222-4222-8222-222222222222"
|
||||
FILE_ID = UUID("66666666-6666-4666-8666-666666666666")
|
||||
UPLOAD_FILE_ID = "77777777-7777-4777-8777-777777777777"
|
||||
UPLOADED_AT = datetime(2026, 8, 20, 12, 0)
|
||||
|
||||
# What ``POST /v1/files/upload`` answers with. The grant channel is a drop-in
|
||||
# for it, so the whole key set travels together and a key dify leaves null must
|
||||
# still be present and null here.
|
||||
DIFY_UPLOAD_RESPONSE_KEYS = frozenset(
|
||||
{
|
||||
"id",
|
||||
"reference",
|
||||
"name",
|
||||
"size",
|
||||
"extension",
|
||||
"mime_type",
|
||||
"created_by",
|
||||
"created_at",
|
||||
"preview_url",
|
||||
"source_url",
|
||||
"original_url",
|
||||
"user_id",
|
||||
"tenant_id",
|
||||
"conversation_id",
|
||||
"file_key",
|
||||
}
|
||||
)
|
||||
|
||||
# The subset dify's own web upload client reads, from
|
||||
# ``web/app/components/base/file-uploader/utils.ts``.
|
||||
WEB_CLIENT_KEYS = frozenset(
|
||||
{
|
||||
"id",
|
||||
"name",
|
||||
"size",
|
||||
"extension",
|
||||
"mime_type",
|
||||
"created_by",
|
||||
"created_at",
|
||||
"preview_url",
|
||||
"source_url",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def granted_config(config_overrides: Callable[..., None]) -> None:
|
||||
config_overrides(
|
||||
SECRET_KEY=SECRET_KEY,
|
||||
FILES_URL="https://files.example.com",
|
||||
INTERNAL_FILES_URL="http://dify-api.dify.svc:5001",
|
||||
FILES_ACCESS_TIMEOUT=300,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sqlite_db(sqlite_engine: Engine) -> Iterator[FileGrantService]:
|
||||
service = _build_file_grant_service(database_client=sessionmaker(bind=sqlite_engine, expire_on_commit=False))
|
||||
services = SimpleNamespace(file_grants=service)
|
||||
with (
|
||||
patch(f"{CONTROLLER_MODULE}.application_services", return_value=services),
|
||||
patch("controllers.files.wraps.application_services", return_value=services),
|
||||
):
|
||||
yield service
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def file_gateway(sqlite_db: FileGrantService) -> FileGrantFileGateway:
|
||||
return cast(FileGrantFileGateway, sqlite_db._files)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def end_user(sqlite_session: Session) -> EndUser:
|
||||
record = EndUser(
|
||||
tenant_id=TENANT_ID,
|
||||
app_id=APP_ID,
|
||||
type=EndUserType.APP_DEPLOY,
|
||||
_is_anonymous=True,
|
||||
session_id="seeded",
|
||||
external_user_id="adp1.seeded",
|
||||
)
|
||||
sqlite_session.add(record)
|
||||
sqlite_session.commit()
|
||||
return record
|
||||
|
||||
|
||||
def _bearer(*scopes: FileGrantScope, end_user_id: str, tenant_id: str = TENANT_ID) -> dict[str, str]:
|
||||
token, _ = issue_file_grant(
|
||||
end_user_id=end_user_id,
|
||||
tenant_id=tenant_id,
|
||||
app_id=APP_ID,
|
||||
scopes=scopes,
|
||||
ttl_seconds=600,
|
||||
)
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
type _UploadResource = GrantedFileUploadApi | GrantedRemoteFileUploadApi | ProducedFileApi
|
||||
type _UploadResult = tuple[dict[str, object], int]
|
||||
type _ResolveResult = dict[str, list[dict[str, object]]]
|
||||
|
||||
|
||||
@overload
|
||||
def _post_with_file_grant(resource: _UploadResource) -> _UploadResult: ...
|
||||
|
||||
|
||||
@overload
|
||||
def _post_with_file_grant(resource: GrantedFileResolveApi) -> _ResolveResult: ...
|
||||
|
||||
|
||||
def _post_with_file_grant(resource: _UploadResource | GrantedFileResolveApi) -> _UploadResult | _ResolveResult:
|
||||
"""Call the grant decorator and validate its raw response.
|
||||
|
||||
Preconditions: The resource owns a callable grant-decorated POST method.
|
||||
Postconditions: Return its validated upload tuple or resolution dictionary.
|
||||
Invalid response shapes raise a validation error. Grant admission is preserved.
|
||||
"""
|
||||
post: Callable[..., object] = resource.post
|
||||
result = post()
|
||||
return TypeAdapter(_UploadResult | _ResolveResult).validate_python(result, strict=True)
|
||||
|
||||
|
||||
def _content_token(*, file_id: str, kind: FileKind, expires_in: int = 300) -> str:
|
||||
return jwt.encode(
|
||||
{
|
||||
"aud": FILE_CONTENT_AUDIENCE,
|
||||
"kind": str(kind),
|
||||
"file_id": file_id,
|
||||
"nonce": "0011223344556677",
|
||||
"exp": int(time.time()) + expires_in,
|
||||
},
|
||||
SECRET_KEY,
|
||||
algorithm="HS256",
|
||||
)
|
||||
|
||||
|
||||
def _stub_upload_file(**overrides: object) -> SimpleNamespace:
|
||||
"""Stand in for the ``upload_files`` row ``FileService`` hands back."""
|
||||
|
||||
return SimpleNamespace(
|
||||
**{
|
||||
"id": UPLOAD_FILE_ID,
|
||||
"name": "report.pdf",
|
||||
"size": 2048,
|
||||
"extension": "pdf",
|
||||
"mime_type": "application/pdf",
|
||||
"tenant_id": TENANT_ID,
|
||||
"created_by": "99999999-9999-4999-8999-999999999999",
|
||||
"created_at": UPLOADED_AT,
|
||||
"source_url": "",
|
||||
**overrides,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _persist_upload_file(session: Session, *, owner_id: str, tenant_id: str = TENANT_ID) -> UploadFile:
|
||||
upload_file = UploadFile(
|
||||
tenant_id=tenant_id,
|
||||
storage_type=StorageType.OPENDAL,
|
||||
key="upload_files/report.pdf",
|
||||
name="report.pdf",
|
||||
size=2048,
|
||||
extension="pdf",
|
||||
mime_type="application/pdf",
|
||||
created_by=owner_id,
|
||||
created_by_role=CreatorUserRole.END_USER,
|
||||
created_at=naive_utc_now(),
|
||||
used=False,
|
||||
)
|
||||
session.add(upload_file)
|
||||
session.commit()
|
||||
return upload_file
|
||||
|
||||
|
||||
def _persist_tool_file(session: Session, *, owner_id: str, mimetype: str = "image/png") -> ToolFile:
|
||||
tool_file = ToolFile(
|
||||
user_id=owner_id,
|
||||
tenant_id=TENANT_ID,
|
||||
conversation_id=None,
|
||||
file_key="tools/chart.png",
|
||||
mimetype=mimetype,
|
||||
name="chart.png",
|
||||
size=64,
|
||||
)
|
||||
session.add(tool_file)
|
||||
session.commit()
|
||||
return tool_file
|
||||
|
||||
|
||||
def test_upload_stores_the_file_for_the_grant_end_user(
|
||||
app: Flask, end_user: EndUser, file_gateway: FileGrantFileGateway
|
||||
) -> None:
|
||||
with (
|
||||
patch.object(file_gateway, "store_upload", wraps=file_gateway.store_upload) as store_upload,
|
||||
patch.object(file_gateway._file_service, "upload_file", return_value=_stub_upload_file()),
|
||||
):
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/upload",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id),
|
||||
data={"file": (BytesIO(b"pdf-bytes"), "report.pdf")},
|
||||
content_type="multipart/form-data",
|
||||
):
|
||||
body, status = _post_with_file_grant(GrantedFileUploadApi())
|
||||
|
||||
assert status == 201
|
||||
assert body["id"] == UPLOAD_FILE_ID
|
||||
assert body["extension"] == "pdf"
|
||||
assert store_upload.call_args.kwargs["context"].end_user_id == end_user.id
|
||||
|
||||
|
||||
def test_upload_answers_in_dify_s_own_upload_shape(
|
||||
app: Flask, end_user: EndUser, file_gateway: FileGrantFileGateway
|
||||
) -> None:
|
||||
"""A client moving off ``POST /v1/files/upload`` must not meet a second shape."""
|
||||
|
||||
with patch.object(
|
||||
file_gateway._file_service,
|
||||
"upload_file",
|
||||
return_value=_stub_upload_file(created_by=end_user.id),
|
||||
):
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/upload",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id),
|
||||
data={"file": (BytesIO(b"pdf-bytes"), "report.pdf")},
|
||||
content_type="multipart/form-data",
|
||||
):
|
||||
body, _ = _post_with_file_grant(GrantedFileUploadApi())
|
||||
|
||||
source_url = body.pop("source_url")
|
||||
assert body == {
|
||||
"id": UPLOAD_FILE_ID,
|
||||
"reference": None,
|
||||
"name": "report.pdf",
|
||||
"size": 2048,
|
||||
"extension": "pdf",
|
||||
"mime_type": "application/pdf",
|
||||
"created_by": end_user.id,
|
||||
"created_at": int(UPLOADED_AT.timestamp()),
|
||||
"preview_url": None,
|
||||
"original_url": None,
|
||||
"user_id": None,
|
||||
"tenant_id": TENANT_ID,
|
||||
"conversation_id": None,
|
||||
"file_key": None,
|
||||
}
|
||||
assert isinstance(source_url, str)
|
||||
assert source_url.startswith(f"https://files.example.com/files/appdeploy/{UPLOAD_FILE_ID}/content?token=")
|
||||
|
||||
|
||||
def test_upload_carries_every_key_dify_s_web_client_reads(
|
||||
app: Flask, end_user: EndUser, file_gateway: FileGrantFileGateway
|
||||
) -> None:
|
||||
with patch.object(file_gateway._file_service, "upload_file", return_value=_stub_upload_file()):
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/upload",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id),
|
||||
data={"file": (BytesIO(b"pdf-bytes"), "report.pdf")},
|
||||
content_type="multipart/form-data",
|
||||
):
|
||||
body, _ = _post_with_file_grant(GrantedFileUploadApi())
|
||||
|
||||
assert set(body) >= WEB_CLIENT_KEYS
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db")
|
||||
def test_upload_rejects_a_grant_from_another_tenant(app: Flask, end_user: EndUser) -> None:
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/upload",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id, tenant_id=OTHER_TENANT_ID),
|
||||
data={"file": (BytesIO(b"pdf-bytes"), "report.pdf")},
|
||||
content_type="multipart/form-data",
|
||||
):
|
||||
with pytest.raises(GrantedFileNotFoundError):
|
||||
_post_with_file_grant(GrantedFileUploadApi())
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db")
|
||||
def test_upload_applies_the_shared_per_extension_size_limit(
|
||||
app: Flask, end_user: EndUser, config_overrides: Callable[..., None]
|
||||
) -> None:
|
||||
config_overrides(UPLOAD_FILE_SIZE_LIMIT=1)
|
||||
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/upload",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id),
|
||||
data={"file": (BytesIO(b"0" * (1024 * 1024 + 1)), "big.bin")},
|
||||
content_type="multipart/form-data",
|
||||
):
|
||||
with pytest.raises(FileTooLargeError):
|
||||
_post_with_file_grant(GrantedFileUploadApi())
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db")
|
||||
def test_upload_rejects_a_blacklisted_extension(
|
||||
app: Flask, end_user: EndUser, config_overrides: Callable[..., None]
|
||||
) -> None:
|
||||
config_overrides(inner_UPLOAD_FILE_EXTENSION_BLACKLIST="exe")
|
||||
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/upload",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id),
|
||||
data={"file": (BytesIO(b"MZ"), "payload.exe")},
|
||||
content_type="multipart/form-data",
|
||||
):
|
||||
with pytest.raises(BlockedFileExtensionError):
|
||||
_post_with_file_grant(GrantedFileUploadApi())
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db")
|
||||
def test_upload_rejects_a_request_carrying_no_file(app: Flask, end_user: EndUser) -> None:
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/upload",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id),
|
||||
data={"note": "no file here"},
|
||||
content_type="multipart/form-data",
|
||||
):
|
||||
with pytest.raises(NoFileUploadedError):
|
||||
_post_with_file_grant(GrantedFileUploadApi())
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db")
|
||||
def test_upload_rejects_a_batch_of_files(app: Flask, end_user: EndUser) -> None:
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/upload",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id),
|
||||
data={
|
||||
"file": (BytesIO(b"first"), "first.pdf"),
|
||||
"second": (BytesIO(b"second"), "second.pdf"),
|
||||
},
|
||||
content_type="multipart/form-data",
|
||||
):
|
||||
with pytest.raises(TooManyFilesError):
|
||||
_post_with_file_grant(GrantedFileUploadApi())
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db")
|
||||
def test_upload_rejects_a_file_without_a_name(app: Flask, end_user: EndUser) -> None:
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/upload",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id),
|
||||
data={"file": (BytesIO(b"nameless"), "")},
|
||||
content_type="multipart/form-data",
|
||||
):
|
||||
with pytest.raises(FilenameNotExistsError):
|
||||
_post_with_file_grant(GrantedFileUploadApi())
|
||||
|
||||
|
||||
def test_remote_upload_fetches_through_the_ssrf_safe_fetcher(
|
||||
app: Flask, end_user: EndUser, sqlite_db: FileGrantService, file_gateway: FileGrantFileGateway
|
||||
) -> None:
|
||||
url = "https://example.com/docs/report.pdf"
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
sqlite_db._remote_files,
|
||||
"fetch",
|
||||
return_value=RemoteFile(filename="report.pdf", mimetype="application/pdf", content=b"pdf-bytes"),
|
||||
) as fetch,
|
||||
patch.object(file_gateway._file_service, "upload_file", return_value=_stub_upload_file()) as upload_file,
|
||||
):
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/remote-upload",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id),
|
||||
json={"url": url},
|
||||
):
|
||||
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
|
||||
files_ns.payload = {"url": url}
|
||||
body, status = _post_with_file_grant(GrantedRemoteFileUploadApi())
|
||||
|
||||
assert status == 201
|
||||
assert body["id"] == UPLOAD_FILE_ID
|
||||
fetch.assert_called_once_with(url)
|
||||
kwargs = upload_file.call_args.kwargs
|
||||
assert kwargs["source_url"] == url
|
||||
assert kwargs["content"] == b"pdf-bytes"
|
||||
assert kwargs["user"].id == end_user.id
|
||||
|
||||
|
||||
def test_remote_upload_answers_in_the_upload_shape_plus_dify_s_url_key(
|
||||
app: Flask, end_user: EndUser, sqlite_db: FileGrantService, file_gateway: FileGrantFileGateway
|
||||
) -> None:
|
||||
"""Dify's own remote upload answers under ``url``, so this one answers under both."""
|
||||
|
||||
url = "https://example.com/docs/report.pdf"
|
||||
with (
|
||||
patch.object(
|
||||
sqlite_db._remote_files,
|
||||
"fetch",
|
||||
return_value=RemoteFile(filename="report.pdf", mimetype="application/pdf", content=b"pdf-bytes"),
|
||||
),
|
||||
patch.object(
|
||||
file_gateway._file_service,
|
||||
"upload_file",
|
||||
return_value=_stub_upload_file(source_url=url),
|
||||
),
|
||||
):
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/remote-upload",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id),
|
||||
json={"url": url},
|
||||
):
|
||||
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
|
||||
files_ns.payload = {"url": url}
|
||||
body, _ = _post_with_file_grant(GrantedRemoteFileUploadApi())
|
||||
|
||||
assert set(body) == DIFY_UPLOAD_RESPONSE_KEYS | {"url"}
|
||||
# The row records where the bytes came from; the response hands back the
|
||||
# signed URL that fetches them, exactly as dify's own remote upload does.
|
||||
assert isinstance(body["source_url"], str)
|
||||
assert body["source_url"].startswith(f"https://files.example.com/files/appdeploy/{UPLOAD_FILE_ID}/content?token=")
|
||||
# One URL under both names, not two signings of the same file.
|
||||
assert body["url"] == body["source_url"]
|
||||
|
||||
|
||||
def test_remote_upload_honours_the_size_precheck(app: Flask, end_user: EndUser, sqlite_db: FileGrantService) -> None:
|
||||
url = "https://example.com/docs/huge.pdf"
|
||||
with patch.object(
|
||||
sqlite_db._remote_files,
|
||||
"fetch",
|
||||
side_effect=FileTooLargeServiceError("too large"),
|
||||
):
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/remote-upload",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id),
|
||||
json={"url": url},
|
||||
):
|
||||
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
|
||||
files_ns.payload = {"url": url}
|
||||
with pytest.raises(FileTooLargeError):
|
||||
_post_with_file_grant(GrantedRemoteFileUploadApi())
|
||||
|
||||
|
||||
def test_produced_stores_a_tool_file_and_returns_both_urls(
|
||||
app: Flask, end_user: EndUser, file_gateway: FileGrantFileGateway
|
||||
) -> None:
|
||||
tool_file = SimpleNamespace(
|
||||
id="88888888-8888-4888-8888-888888888888",
|
||||
name="chart.png",
|
||||
size=64,
|
||||
mimetype="image/png",
|
||||
)
|
||||
|
||||
with patch.object(file_gateway._tool_files, "create_file_by_raw", return_value=tool_file) as create_file:
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/produced",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.PRODUCE, end_user_id=end_user.id),
|
||||
data={"file": (BytesIO(b"0" * 16), "chart.png")},
|
||||
content_type="multipart/form-data",
|
||||
):
|
||||
body, status = _post_with_file_grant(ProducedFileApi())
|
||||
|
||||
assert status == 201
|
||||
kwargs = create_file.call_args.kwargs
|
||||
assert kwargs["user_id"] == end_user.id
|
||||
assert kwargs["tenant_id"] == TENANT_ID
|
||||
assert kwargs["conversation_id"] is None
|
||||
assert isinstance(body["url"], str)
|
||||
assert isinstance(body["internal_url"], str)
|
||||
assert body["url"].startswith(f"https://files.example.com/files/appdeploy/{tool_file.id}/content?token=")
|
||||
assert body["internal_url"].startswith(
|
||||
f"http://dify-api.dify.svc:5001/files/appdeploy/{tool_file.id}/content?token="
|
||||
)
|
||||
|
||||
|
||||
def test_produced_rejects_a_grant_whose_subject_was_deleted(app: Flask, file_gateway: FileGrantFileGateway) -> None:
|
||||
with patch.object(file_gateway._tool_files, "create_file_by_raw") as create_file:
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/produced",
|
||||
method="POST",
|
||||
headers=_bearer(
|
||||
FileGrantScope.PRODUCE,
|
||||
end_user_id="99999999-9999-4999-8999-999999999999",
|
||||
),
|
||||
data={"file": (BytesIO(b"content"), "chart.png")},
|
||||
content_type="multipart/form-data",
|
||||
):
|
||||
with pytest.raises(GrantedFileNotFoundError):
|
||||
_post_with_file_grant(ProducedFileApi())
|
||||
|
||||
create_file.assert_not_called()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def one_megabyte_image_limit(config_overrides: Callable[..., None]) -> int:
|
||||
"""Hold images to one mebibyte while every other kind stays far above it."""
|
||||
|
||||
config_overrides(
|
||||
UPLOAD_FILE_SIZE_LIMIT=64,
|
||||
UPLOAD_IMAGE_FILE_SIZE_LIMIT=1,
|
||||
UPLOAD_VIDEO_FILE_SIZE_LIMIT=64,
|
||||
UPLOAD_AUDIO_FILE_SIZE_LIMIT=64,
|
||||
)
|
||||
return 1024 * 1024
|
||||
|
||||
|
||||
def test_produced_accepts_a_file_of_exactly_the_per_extension_limit(
|
||||
app: Flask, end_user: EndUser, file_gateway: FileGrantFileGateway, one_megabyte_image_limit: int
|
||||
) -> None:
|
||||
with patch.object(
|
||||
file_gateway._tool_files,
|
||||
"create_file_by_raw",
|
||||
return_value=SimpleNamespace(
|
||||
id="88888888-8888-4888-8888-888888888888",
|
||||
name="chart.png",
|
||||
size=one_megabyte_image_limit,
|
||||
mimetype="image/png",
|
||||
),
|
||||
) as create_file:
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/produced",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.PRODUCE, end_user_id=end_user.id),
|
||||
data={"file": (BytesIO(b"0" * one_megabyte_image_limit), "chart.png")},
|
||||
content_type="multipart/form-data",
|
||||
):
|
||||
_, status = _post_with_file_grant(ProducedFileApi())
|
||||
|
||||
assert status == 201
|
||||
assert len(create_file.call_args.kwargs["file_binary"]) == one_megabyte_image_limit
|
||||
|
||||
|
||||
def test_produced_rejects_a_file_one_byte_over_the_per_extension_limit(
|
||||
app: Flask, end_user: EndUser, file_gateway: FileGrantFileGateway, one_megabyte_image_limit: int
|
||||
) -> None:
|
||||
"""``create_file_by_raw`` has no limit of its own, so nothing else would stop this."""
|
||||
|
||||
with patch.object(file_gateway._tool_files, "create_file_by_raw") as create_file:
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/produced",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.PRODUCE, end_user_id=end_user.id),
|
||||
data={"file": (BytesIO(b"0" * (one_megabyte_image_limit + 1)), "chart.png")},
|
||||
content_type="multipart/form-data",
|
||||
):
|
||||
with pytest.raises(FileTooLargeError) as raised:
|
||||
_post_with_file_grant(ProducedFileApi())
|
||||
|
||||
assert raised.value.code == 413
|
||||
create_file.assert_not_called()
|
||||
|
||||
|
||||
class _CountingStream:
|
||||
"""A body that reports how much of itself a reader actually pulled."""
|
||||
|
||||
def __init__(self, size: int) -> None:
|
||||
self._remaining = size
|
||||
self.bytes_read = 0
|
||||
|
||||
def read(self, size: int = -1) -> bytes:
|
||||
served = self._remaining if size < 0 else min(size, self._remaining)
|
||||
self._remaining -= served
|
||||
self.bytes_read += served
|
||||
return b"0" * served
|
||||
|
||||
|
||||
def test_produced_stops_reading_an_oversized_body_at_the_per_extension_limit(
|
||||
end_user: EndUser,
|
||||
sqlite_db: FileGrantService,
|
||||
file_gateway: FileGrantFileGateway,
|
||||
one_megabyte_image_limit: int,
|
||||
) -> None:
|
||||
"""The caller is a worker running plugin code with no proxy body limit in front of it."""
|
||||
|
||||
stream = _CountingStream(one_megabyte_image_limit * 64)
|
||||
|
||||
with patch.object(file_gateway._tool_files, "create_file_by_raw") as create_file:
|
||||
with pytest.raises(FileTooLargeServiceError):
|
||||
sqlite_db.store_produced(
|
||||
context=FileGrantContext(TENANT_ID, APP_ID, end_user.id),
|
||||
filename="chart.png",
|
||||
stream=cast(IO[bytes], stream),
|
||||
mimetype="image/png",
|
||||
)
|
||||
|
||||
assert stream.bytes_read == one_megabyte_image_limit + 1
|
||||
create_file.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db")
|
||||
def test_resolve_signs_urls_per_item_and_hides_foreign_files(
|
||||
app: Flask, end_user: EndUser, sqlite_session: Session
|
||||
) -> None:
|
||||
owned = _persist_upload_file(sqlite_session, owner_id=end_user.id)
|
||||
foreign = _persist_upload_file(sqlite_session, owner_id="00000000-0000-4000-8000-000000000000")
|
||||
payload = {
|
||||
"files": [
|
||||
{"id": owned.id, "kind": "upload"},
|
||||
{"id": foreign.id, "kind": "upload"},
|
||||
]
|
||||
}
|
||||
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/resolve",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.RESOLVE, end_user_id=end_user.id),
|
||||
json=payload,
|
||||
):
|
||||
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
|
||||
files_ns.payload = payload
|
||||
body = _post_with_file_grant(GrantedFileResolveApi())
|
||||
|
||||
resolved, hidden = body["files"]
|
||||
assert resolved["ok"] is True
|
||||
assert resolved["kind"] == "upload"
|
||||
assert resolved["extension"] == "pdf"
|
||||
assert isinstance(resolved["url"], str)
|
||||
assert isinstance(resolved["internal_url"], str)
|
||||
assert resolved["url"].startswith(f"https://files.example.com/files/appdeploy/{owned.id}/content?token=")
|
||||
assert resolved["internal_url"].startswith(
|
||||
f"http://dify-api.dify.svc:5001/files/appdeploy/{owned.id}/content?token="
|
||||
)
|
||||
assert hidden == {
|
||||
"id": foreign.id,
|
||||
"ok": False,
|
||||
"kind": None,
|
||||
"name": None,
|
||||
"size": None,
|
||||
"extension": None,
|
||||
"mime_type": None,
|
||||
"url": None,
|
||||
"internal_url": None,
|
||||
"error": "not_found",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db")
|
||||
def test_resolve_answers_a_mixed_batch_item_by_item(app: Flask, end_user: EndUser, sqlite_session: Session) -> None:
|
||||
owned_upload = _persist_upload_file(sqlite_session, owner_id=end_user.id)
|
||||
owned_tool = _persist_tool_file(sqlite_session, owner_id=end_user.id)
|
||||
missing_id = "44444444-4444-4444-8444-444444444444"
|
||||
payload = {
|
||||
"files": [
|
||||
{"id": owned_upload.id, "kind": "upload"},
|
||||
{"id": missing_id, "kind": "upload"},
|
||||
# A real file, but looked up in the wrong table.
|
||||
{"id": owned_upload.id, "kind": "tool"},
|
||||
{"id": owned_tool.id, "kind": "tool"},
|
||||
]
|
||||
}
|
||||
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/resolve",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.RESOLVE, end_user_id=end_user.id),
|
||||
json=payload,
|
||||
):
|
||||
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
|
||||
files_ns.payload = payload
|
||||
body = _post_with_file_grant(GrantedFileResolveApi())
|
||||
|
||||
assert [(file["id"], file["ok"], file["kind"], file["error"]) for file in body["files"]] == [
|
||||
(owned_upload.id, True, "upload", None),
|
||||
(missing_id, False, None, "not_found"),
|
||||
(owned_upload.id, False, None, "not_found"),
|
||||
(owned_tool.id, True, "tool", None),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db")
|
||||
def test_resolve_returns_an_empty_batch_unchanged(app: Flask, end_user: EndUser) -> None:
|
||||
payload: dict[str, object] = {"files": []}
|
||||
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/resolve",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.RESOLVE, end_user_id=end_user.id),
|
||||
json=payload,
|
||||
):
|
||||
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
|
||||
files_ns.payload = payload
|
||||
assert _post_with_file_grant(GrantedFileResolveApi()) == {"files": []}
|
||||
|
||||
|
||||
def test_resolve_rejects_an_unbounded_batch(app: Flask, end_user: EndUser, sqlite_db: FileGrantService) -> None:
|
||||
del sqlite_db
|
||||
payload = {
|
||||
"files": [
|
||||
{"id": f"00000000-0000-4000-8000-{index:012d}", "kind": "upload"}
|
||||
for index in range(MAX_FILE_GRANT_REFS + 1)
|
||||
]
|
||||
}
|
||||
with app.test_request_context(
|
||||
"/files/appdeploy/resolve",
|
||||
method="POST",
|
||||
headers=_bearer(FileGrantScope.RESOLVE, end_user_id=end_user.id),
|
||||
json=payload,
|
||||
):
|
||||
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
|
||||
files_ns.payload = payload
|
||||
with pytest.raises(InvalidFileRequestError):
|
||||
_post_with_file_grant(GrantedFileResolveApi())
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def stored_bytes(file_gateway: FileGrantFileGateway) -> Iterator[MagicMock]:
|
||||
with patch.object(file_gateway._storage, "load", return_value=iter([b"file-bytes"])) as load:
|
||||
yield load
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db", "stored_bytes")
|
||||
@pytest.mark.parametrize(
|
||||
("mime_type", "expected_content_type", "expects_attachment"),
|
||||
[
|
||||
("image/png", "image/png", False),
|
||||
("image/jpeg", "image/jpeg", False),
|
||||
("image/gif", "image/gif", False),
|
||||
("image/webp", "image/webp", False),
|
||||
# Case and parameters are normalized before the whitelist is consulted.
|
||||
("IMAGE/PNG; charset=binary", "image/png", False),
|
||||
("application/pdf", "application/octet-stream", True),
|
||||
("image/svg+xml", "application/octet-stream", True),
|
||||
("text/html", "application/octet-stream", True),
|
||||
("application/xhtml+xml", "application/octet-stream", True),
|
||||
("text/javascript", "application/octet-stream", True),
|
||||
("audio/mpeg", "application/octet-stream", True),
|
||||
("video/mp4", "application/octet-stream", True),
|
||||
],
|
||||
)
|
||||
def test_content_disposition_follows_the_inline_whitelist(
|
||||
app: Flask,
|
||||
sqlite_session: Session,
|
||||
mime_type: str,
|
||||
expected_content_type: str,
|
||||
expects_attachment: bool,
|
||||
) -> None:
|
||||
tool_file = _persist_tool_file(sqlite_session, owner_id="anyone", mimetype=mime_type)
|
||||
token = _content_token(file_id=tool_file.id, kind=FileKind.TOOL)
|
||||
|
||||
with app.test_request_context(f"/files/appdeploy/{tool_file.id}/content", query_string={"token": token}):
|
||||
response = GrantedFileContentApi().get(UUID(tool_file.id))
|
||||
|
||||
assert response.headers["X-Content-Type-Options"] == "nosniff"
|
||||
assert response.headers["Content-Type"].startswith(expected_content_type)
|
||||
assert ("Content-Disposition" in response.headers) is expects_attachment
|
||||
if expects_attachment:
|
||||
assert response.headers["Content-Disposition"] == "attachment; filename*=UTF-8''chart.png"
|
||||
# Range is never honoured here, so the hint must not be advertised either.
|
||||
assert "Accept-Ranges" not in response.headers
|
||||
assert response.headers["Content-Length"] == "64"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db", "stored_bytes")
|
||||
def test_content_downloads_a_file_with_no_recorded_mime_type(app: Flask, sqlite_session: Session) -> None:
|
||||
upload_file = _persist_upload_file(sqlite_session, owner_id="anyone")
|
||||
sqlite_session.execute(update(UploadFile).where(UploadFile.id == upload_file.id).values(mime_type=null()))
|
||||
sqlite_session.commit()
|
||||
token = _content_token(file_id=upload_file.id, kind=FileKind.UPLOAD)
|
||||
|
||||
with app.test_request_context(f"/files/appdeploy/{upload_file.id}/content", query_string={"token": token}):
|
||||
response = GrantedFileContentApi().get(UUID(upload_file.id))
|
||||
|
||||
assert response.headers["X-Content-Type-Options"] == "nosniff"
|
||||
assert response.headers["Content-Type"].startswith("application/octet-stream")
|
||||
assert response.headers["Content-Disposition"] == "attachment; filename*=UTF-8''report.pdf"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db", "stored_bytes")
|
||||
def test_content_rejects_an_expired_token(app: Flask, sqlite_session: Session) -> None:
|
||||
tool_file = _persist_tool_file(sqlite_session, owner_id="anyone")
|
||||
token = _content_token(file_id=tool_file.id, kind=FileKind.TOOL, expires_in=-1)
|
||||
|
||||
with app.test_request_context(f"/files/appdeploy/{tool_file.id}/content", query_string={"token": token}):
|
||||
with pytest.raises(FileGrantInvalidError):
|
||||
GrantedFileContentApi().get(UUID(tool_file.id))
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db", "stored_bytes")
|
||||
def test_content_rejects_a_token_minted_for_another_file(app: Flask, sqlite_session: Session) -> None:
|
||||
tool_file = _persist_tool_file(sqlite_session, owner_id="anyone")
|
||||
token = _content_token(file_id=str(FILE_ID), kind=FileKind.TOOL)
|
||||
|
||||
with app.test_request_context(f"/files/appdeploy/{tool_file.id}/content", query_string={"token": token}):
|
||||
with pytest.raises(GrantedFileNotFoundError):
|
||||
GrantedFileContentApi().get(UUID(tool_file.id))
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db", "stored_bytes")
|
||||
def test_content_rejects_a_token_naming_the_wrong_table(app: Flask, sqlite_session: Session) -> None:
|
||||
tool_file = _persist_tool_file(sqlite_session, owner_id="anyone")
|
||||
token = _content_token(file_id=tool_file.id, kind=FileKind.UPLOAD)
|
||||
|
||||
with app.test_request_context(f"/files/appdeploy/{tool_file.id}/content", query_string={"token": token}):
|
||||
with pytest.raises(GrantedFileNotFoundError):
|
||||
GrantedFileContentApi().get(UUID(tool_file.id))
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sqlite_db", "stored_bytes")
|
||||
def test_content_rejects_a_file_grant_replayed_as_a_content_token(app: Flask, sqlite_session: Session) -> None:
|
||||
tool_file = _persist_tool_file(sqlite_session, owner_id="anyone")
|
||||
grant, _ = issue_file_grant(
|
||||
end_user_id="anyone",
|
||||
tenant_id=TENANT_ID,
|
||||
app_id=APP_ID,
|
||||
scopes=[FileGrantScope.RESOLVE],
|
||||
ttl_seconds=600,
|
||||
)
|
||||
|
||||
with app.test_request_context(f"/files/appdeploy/{tool_file.id}/content", query_string={"token": grant}):
|
||||
with pytest.raises(FileGrantInvalidError):
|
||||
GrantedFileContentApi().get(UUID(tool_file.id))
|
||||
@@ -0,0 +1,332 @@
|
||||
import io
|
||||
import types
|
||||
from collections.abc import Callable
|
||||
from inspect import unwrap
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from pydantic import ValidationError
|
||||
|
||||
import controllers.files.plugin_file_upload as module
|
||||
from controllers.files import bp as files_blueprint
|
||||
from core.workflow.file_reference import build_file_reference
|
||||
from enums import DeploymentEdition
|
||||
from services.errors.file import FileTooLargeError as ServiceFileTooLargeError
|
||||
from services.errors.file import UnsupportedFileTypeError as ServiceUnsupportedFileTypeError
|
||||
from services.plugin_file_upload_service import PluginFileUploadAccessDeniedError, PluginFileUploadResult
|
||||
|
||||
|
||||
class DummyFile:
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
filename: str | None = "report.pdf",
|
||||
mimetype: str | None = "application/pdf",
|
||||
content: bytes = b"content",
|
||||
) -> None:
|
||||
self.filename = filename
|
||||
self.mimetype = mimetype
|
||||
self.stream = io.BytesIO(content)
|
||||
|
||||
|
||||
def _fake_request(args: dict[str, object], *, file: DummyFile | None = None) -> types.SimpleNamespace:
|
||||
return types.SimpleNamespace(
|
||||
args=types.SimpleNamespace(to_dict=lambda **_kwargs: args),
|
||||
files={"file": file} if file is not None else {},
|
||||
)
|
||||
|
||||
|
||||
def _result() -> PluginFileUploadResult:
|
||||
return PluginFileUploadResult(
|
||||
id="file-id",
|
||||
reference=build_file_reference(record_id="file-id"),
|
||||
name="report.pdf",
|
||||
size=7,
|
||||
extension=".pdf",
|
||||
mime_type="application/pdf",
|
||||
preview_url="https://files.example.com/files/tools/file-id.pdf?signed",
|
||||
source_url=None,
|
||||
original_url=None,
|
||||
user_id="user-id",
|
||||
tenant_id="tenant-id",
|
||||
conversation_id="conversation-id",
|
||||
file_key="tools/tenant-id/file.pdf",
|
||||
)
|
||||
|
||||
|
||||
def _valid_args() -> dict[str, object]:
|
||||
return {
|
||||
"timestamp": "123",
|
||||
"nonce": "nonce",
|
||||
"sign": "signature",
|
||||
"tenant_id": "tenant-id",
|
||||
"user_id": "user-id",
|
||||
"user_from": "end-user",
|
||||
"conversation_id": "conversation-id",
|
||||
"max_size": "1024",
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def files_app(config_overrides: Callable[..., None]) -> Flask:
|
||||
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.CLOUD)
|
||||
app = Flask(__name__)
|
||||
app.config["TESTING"] = True
|
||||
app.register_blueprint(files_blueprint)
|
||||
return app
|
||||
|
||||
|
||||
class TestPluginUploadFileApi:
|
||||
def test_upload_query_requires_the_signed_user_id(self) -> None:
|
||||
args = _valid_args()
|
||||
del args["user_id"]
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
module.PluginUploadQuery.model_validate(args)
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_upload_returns_the_existing_plugin_file_contract(
|
||||
self,
|
||||
application_services: MagicMock,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
file = DummyFile()
|
||||
monkeypatch.setattr(module, "request", _fake_request(_valid_args(), file=file))
|
||||
service = application_services.return_value.plugin_file_uploads
|
||||
service.upload.return_value = _result()
|
||||
|
||||
response, status = unwrap(module.PluginUploadFileApi.post)(module.PluginUploadFileApi())
|
||||
|
||||
assert status == 201
|
||||
assert response == {
|
||||
"id": "file-id",
|
||||
"reference": build_file_reference(record_id="file-id"),
|
||||
"name": "report.pdf",
|
||||
"size": 7,
|
||||
"extension": ".pdf",
|
||||
"mime_type": "application/pdf",
|
||||
"created_by": None,
|
||||
"created_at": None,
|
||||
"preview_url": "https://files.example.com/files/tools/file-id.pdf?signed",
|
||||
"source_url": None,
|
||||
"original_url": None,
|
||||
"user_id": "user-id",
|
||||
"tenant_id": "tenant-id",
|
||||
"conversation_id": "conversation-id",
|
||||
"file_key": "tools/tenant-id/file.pdf",
|
||||
}
|
||||
service.upload.assert_called_once_with(
|
||||
stream=file.stream,
|
||||
filename="report.pdf",
|
||||
mimetype="application/pdf",
|
||||
tenant_id="tenant-id",
|
||||
user_id="user-id",
|
||||
user_from="end-user",
|
||||
conversation_id="conversation-id",
|
||||
timestamp="123",
|
||||
nonce="nonce",
|
||||
sign="signature",
|
||||
max_size=1024,
|
||||
)
|
||||
|
||||
def test_missing_file_has_a_specific_client_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(module, "request", _fake_request(_valid_args()))
|
||||
|
||||
with pytest.raises(module.NoFileUploadedError):
|
||||
unwrap(module.PluginUploadFileApi.post)(module.PluginUploadFileApi())
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("file", "expected_error"),
|
||||
[
|
||||
pytest.param(DummyFile(filename=""), module.FilenameNotExistsError, id="filename"),
|
||||
pytest.param(DummyFile(mimetype=""), module.UnsupportedFileTypeError, id="mimetype"),
|
||||
],
|
||||
)
|
||||
def test_invalid_file_metadata_has_a_specific_client_error(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
file: DummyFile,
|
||||
expected_error: type[Exception],
|
||||
) -> None:
|
||||
monkeypatch.setattr(module, "request", _fake_request(_valid_args(), file=file))
|
||||
|
||||
with pytest.raises(expected_error):
|
||||
unwrap(module.PluginUploadFileApi.post)(module.PluginUploadFileApi())
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_access_denied_is_reported_without_leaking_identity_details(
|
||||
self,
|
||||
application_services: MagicMock,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(module, "request", _fake_request(_valid_args(), file=DummyFile()))
|
||||
application_services.return_value.plugin_file_uploads.upload.side_effect = PluginFileUploadAccessDeniedError()
|
||||
|
||||
with pytest.raises(module.InvalidPluginFileUploadError) as error_info:
|
||||
unwrap(module.PluginUploadFileApi.post)(module.PluginUploadFileApi())
|
||||
|
||||
assert error_info.value.code == 403
|
||||
assert error_info.value.error_code == "invalid_plugin_file_upload"
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_signed_size_limit_is_reported_as_413(
|
||||
self,
|
||||
application_services: MagicMock,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(module, "request", _fake_request(_valid_args(), file=DummyFile()))
|
||||
application_services.return_value.plugin_file_uploads.upload.side_effect = ServiceFileTooLargeError(
|
||||
"signed limit exceeded"
|
||||
)
|
||||
|
||||
with pytest.raises(module.FileTooLargeError) as error_info:
|
||||
unwrap(module.PluginUploadFileApi.post)(module.PluginUploadFileApi())
|
||||
|
||||
assert error_info.value.code == 413
|
||||
assert error_info.value.__cause__ is not None
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_unsupported_file_type_is_reported_as_415(
|
||||
self,
|
||||
application_services: MagicMock,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(module, "request", _fake_request(_valid_args(), file=DummyFile()))
|
||||
application_services.return_value.plugin_file_uploads.upload.side_effect = ServiceUnsupportedFileTypeError()
|
||||
|
||||
with pytest.raises(module.UnsupportedFileTypeError) as error_info:
|
||||
unwrap(module.PluginUploadFileApi.post)(module.PluginUploadFileApi())
|
||||
|
||||
assert error_info.value.code == 415
|
||||
assert error_info.value.__cause__ is not None
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_unexpected_failure_is_not_relabelled_as_a_client_error(
|
||||
self,
|
||||
application_services: MagicMock,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
storage_error = OSError("storage unavailable")
|
||||
monkeypatch.setattr(module, "request", _fake_request(_valid_args(), file=DummyFile()))
|
||||
application_services.return_value.plugin_file_uploads.upload.side_effect = storage_error
|
||||
|
||||
with pytest.raises(OSError, match="storage unavailable") as error_info:
|
||||
unwrap(module.PluginUploadFileApi.post)(module.PluginUploadFileApi())
|
||||
|
||||
assert error_info.value is storage_error
|
||||
|
||||
|
||||
class TestPluginUploadFileHttpContract:
|
||||
@patch.object(module, "application_services")
|
||||
def test_multipart_upload_returns_strict_201_and_consumer_fields(
|
||||
self,
|
||||
application_services: MagicMock,
|
||||
files_app: Flask,
|
||||
) -> None:
|
||||
application_services.return_value.plugin_file_uploads.upload.return_value = _result()
|
||||
|
||||
response = files_app.test_client().post(
|
||||
"/files/upload/for-plugin",
|
||||
query_string=_valid_args(),
|
||||
data={"file": (io.BytesIO(b"content"), "report.pdf")},
|
||||
content_type="multipart/form-data",
|
||||
)
|
||||
|
||||
assert response.status_code == 201
|
||||
assert response.get_json() == {
|
||||
"id": "file-id",
|
||||
"reference": build_file_reference(record_id="file-id"),
|
||||
"name": "report.pdf",
|
||||
"size": 7,
|
||||
"extension": ".pdf",
|
||||
"mime_type": "application/pdf",
|
||||
"created_by": None,
|
||||
"created_at": None,
|
||||
"preview_url": "https://files.example.com/files/tools/file-id.pdf?signed",
|
||||
"source_url": None,
|
||||
"original_url": None,
|
||||
"user_id": "user-id",
|
||||
"tenant_id": "tenant-id",
|
||||
"conversation_id": "conversation-id",
|
||||
"file_key": "tools/tenant-id/file.pdf",
|
||||
}
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_invalid_signature_returns_a_structured_403(
|
||||
self,
|
||||
application_services: MagicMock,
|
||||
files_app: Flask,
|
||||
) -> None:
|
||||
application_services.return_value.plugin_file_uploads.upload.side_effect = PluginFileUploadAccessDeniedError()
|
||||
|
||||
response = files_app.test_client().post(
|
||||
"/files/upload/for-plugin",
|
||||
query_string=_valid_args(),
|
||||
data={"file": (io.BytesIO(b"content"), "report.pdf")},
|
||||
content_type="multipart/form-data",
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
assert response.get_json() == {
|
||||
"code": "invalid_plugin_file_upload",
|
||||
"message": "The plugin file upload request is invalid or expired.",
|
||||
"status": 403,
|
||||
}
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_signed_size_limit_returns_a_structured_413(
|
||||
self,
|
||||
application_services: MagicMock,
|
||||
files_app: Flask,
|
||||
) -> None:
|
||||
application_services.return_value.plugin_file_uploads.upload.side_effect = ServiceFileTooLargeError(
|
||||
"signed limit exceeded"
|
||||
)
|
||||
|
||||
response = files_app.test_client().post(
|
||||
"/files/upload/for-plugin",
|
||||
query_string=_valid_args(),
|
||||
data={"file": (io.BytesIO(b"content"), "report.pdf")},
|
||||
content_type="multipart/form-data",
|
||||
)
|
||||
|
||||
assert response.status_code == 413
|
||||
assert response.get_json() == {
|
||||
"code": "file_too_large",
|
||||
"message": "signed limit exceeded",
|
||||
"status": 413,
|
||||
}
|
||||
|
||||
def test_missing_signed_user_id_returns_400_before_service_call(self, files_app: Flask) -> None:
|
||||
query = _valid_args()
|
||||
del query["user_id"]
|
||||
|
||||
with patch.object(module, "application_services") as application_services:
|
||||
response = files_app.test_client().post(
|
||||
"/files/upload/for-plugin",
|
||||
query_string=query,
|
||||
data={"file": (io.BytesIO(b"content"), "report.pdf")},
|
||||
content_type="multipart/form-data",
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.get_json()["code"] == "invalid_param"
|
||||
application_services.assert_not_called()
|
||||
|
||||
def test_missing_file_returns_a_structured_400(self, files_app: Flask) -> None:
|
||||
with patch.object(module, "application_services") as application_services:
|
||||
response = files_app.test_client().post(
|
||||
"/files/upload/for-plugin",
|
||||
query_string=_valid_args(),
|
||||
data={},
|
||||
content_type="multipart/form-data",
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.get_json() == {
|
||||
"code": "no_file_uploaded",
|
||||
"message": "Please upload your file.",
|
||||
"status": 400,
|
||||
}
|
||||
application_services.assert_not_called()
|
||||
@@ -0,0 +1,148 @@
|
||||
from collections.abc import Iterator
|
||||
from inspect import unwrap
|
||||
from unittest.mock import MagicMock, patch
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
from flask import Flask, Response
|
||||
from werkzeug.exceptions import Forbidden, NotFound, UnprocessableEntity
|
||||
|
||||
import controllers.files.tool_files as module
|
||||
from services.tool_file_download_service import (
|
||||
ToolFileDownload,
|
||||
ToolFileDownloadAccessDeniedError,
|
||||
ToolFileDownloadNotFoundError,
|
||||
)
|
||||
|
||||
|
||||
def _query(*, sign: str = "sig", as_attachment: bool = False) -> module.ToolFileQuery:
|
||||
return module.ToolFileQuery(timestamp="123", nonce="abc", sign=sign, as_attachment=as_attachment)
|
||||
|
||||
|
||||
def _get(query: module.ToolFileQuery, file_id: str, extension: str) -> Response:
|
||||
api = module.ToolFileApi()
|
||||
return unwrap(api.get)(api, query, file_id, extension)
|
||||
|
||||
|
||||
def _download(
|
||||
*,
|
||||
content: Iterator[bytes] | None = None,
|
||||
mime_type: str | None = "text/plain",
|
||||
filename: str | None = "tool.txt",
|
||||
size: int = 10,
|
||||
) -> ToolFileDownload:
|
||||
return ToolFileDownload(
|
||||
content=content if content is not None else iter([b"data"]),
|
||||
mime_type=mime_type,
|
||||
filename=filename,
|
||||
size=size,
|
||||
)
|
||||
|
||||
|
||||
class TestToolFileApi:
|
||||
@patch.object(module, "application_services")
|
||||
def test_success_stream(self, mock_application_services: MagicMock) -> None:
|
||||
stream = iter([b"data"])
|
||||
service = mock_application_services.return_value.tool_file_downloads
|
||||
service.get_signed_file.return_value = _download(content=stream, size=100)
|
||||
|
||||
response = _get(_query(), "file-id", "txt")
|
||||
|
||||
assert response.response is stream
|
||||
assert response.mimetype == "text/plain"
|
||||
assert response.headers["Content-Length"] == "100"
|
||||
assert response.direct_passthrough is True
|
||||
service.get_signed_file.assert_called_once_with(
|
||||
file_id="file-id",
|
||||
timestamp="123",
|
||||
nonce="abc",
|
||||
sign="sig",
|
||||
)
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_zero_size_omits_content_length(self, mock_application_services: MagicMock) -> None:
|
||||
mock_application_services.return_value.tool_file_downloads.get_signed_file.return_value = _download(size=0)
|
||||
|
||||
response = _get(_query(), "file-id", "txt")
|
||||
|
||||
assert "Content-Length" not in response.headers
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_as_attachment_preserves_mime_type(self, mock_application_services: MagicMock) -> None:
|
||||
mock_application_services.return_value.tool_file_downloads.get_signed_file.return_value = _download(
|
||||
mime_type="application/pdf",
|
||||
filename="报告.pdf",
|
||||
)
|
||||
|
||||
response = _get(_query(as_attachment=True), "file-id", "pdf")
|
||||
|
||||
assert response.headers["Content-Disposition"] == "attachment; filename*=UTF-8''%E6%8A%A5%E5%91%8A.pdf"
|
||||
assert response.headers["Content-Type"] == "application/pdf"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("mime_type", "filename", "route_extension"),
|
||||
[
|
||||
pytest.param("text/html", "file.txt", "txt", id="mime-type"),
|
||||
pytest.param("text/plain", "file.HTML", "txt", id="filename"),
|
||||
pytest.param("text/plain", "file.txt", "html", id="route-extension"),
|
||||
],
|
||||
)
|
||||
@patch.object(module, "application_services")
|
||||
def test_html_forces_download(
|
||||
self,
|
||||
mock_application_services: MagicMock,
|
||||
mime_type: str,
|
||||
filename: str,
|
||||
route_extension: str,
|
||||
) -> None:
|
||||
mock_application_services.return_value.tool_file_downloads.get_signed_file.return_value = _download(
|
||||
mime_type=mime_type,
|
||||
filename=filename,
|
||||
)
|
||||
|
||||
response = _get(_query(), "file-id", route_extension)
|
||||
|
||||
assert response.headers["Content-Disposition"].startswith("attachment")
|
||||
assert response.headers["Content-Type"] == "application/octet-stream"
|
||||
assert response.headers["X-Content-Type-Options"] == "nosniff"
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_invalid_signature(self, mock_application_services: MagicMock) -> None:
|
||||
mock_application_services.return_value.tool_file_downloads.get_signed_file.side_effect = (
|
||||
ToolFileDownloadAccessDeniedError()
|
||||
)
|
||||
|
||||
with pytest.raises(Forbidden, match=r"Invalid request\."):
|
||||
_get(_query(sign="bad-sig"), "file-id", "txt")
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_file_not_found(self, mock_application_services: MagicMock) -> None:
|
||||
mock_application_services.return_value.tool_file_downloads.get_signed_file.side_effect = (
|
||||
ToolFileDownloadNotFoundError()
|
||||
)
|
||||
|
||||
with pytest.raises(NotFound, match="file is not found"):
|
||||
_get(_query(), "file-id", "txt")
|
||||
|
||||
@pytest.mark.parametrize("service_error", [RuntimeError("database unavailable"), OSError("storage unavailable")])
|
||||
@patch.object(module, "application_services")
|
||||
def test_unexpected_error_is_not_converted(
|
||||
self,
|
||||
mock_application_services: MagicMock,
|
||||
service_error: Exception,
|
||||
) -> None:
|
||||
mock_application_services.return_value.tool_file_downloads.get_signed_file.side_effect = service_error
|
||||
|
||||
with pytest.raises(type(service_error)) as error_info:
|
||||
_get(_query(), "file-id", "txt")
|
||||
|
||||
assert error_info.value is service_error
|
||||
|
||||
def test_missing_signature_is_rejected_before_the_service_call(self, app: Flask) -> None:
|
||||
"""`timestamp`, `nonce` and `sign` are required, so this covers the decorator's rejection path."""
|
||||
with patch.object(module, "application_services") as application_services:
|
||||
with app.test_request_context("/files/tools/file-id.txt"):
|
||||
with pytest.raises(UnprocessableEntity):
|
||||
module.ToolFileApi().get(UUID(int=1), "txt")
|
||||
|
||||
application_services.assert_not_called()
|
||||
@@ -0,0 +1,254 @@
|
||||
from inspect import unwrap
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from werkzeug.exceptions import NotFound, UnprocessableEntity
|
||||
|
||||
import controllers.files.upload_file_delivery as module
|
||||
from services.errors.file import UnsupportedFileTypeError as UnsupportedFileTypeServiceError
|
||||
from services.upload_file_delivery_service import (
|
||||
UploadFileDelivery,
|
||||
UploadFileDeliveryNotFoundError,
|
||||
UploadFileDeliveryRecord,
|
||||
)
|
||||
|
||||
|
||||
def _delivery(
|
||||
*,
|
||||
mime_type: str | None = "text/plain",
|
||||
size: int = 10,
|
||||
name: str = "test.txt",
|
||||
extension: str = "txt",
|
||||
content: bytes | None = None,
|
||||
) -> UploadFileDelivery:
|
||||
return UploadFileDelivery(
|
||||
content=content if content is not None else iter([b"data"]),
|
||||
file=UploadFileDeliveryRecord(
|
||||
key="uploads/file-id",
|
||||
name=name,
|
||||
size=size,
|
||||
extension=extension,
|
||||
mime_type=mime_type,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
_SIGNED_QUERY = "/?timestamp=123&nonce=abc&sign=sig"
|
||||
|
||||
|
||||
class TestImagePreviewApi:
|
||||
@patch.object(module, "application_services")
|
||||
def test_success(self, mock_application_services, app: Flask):
|
||||
service = mock_application_services.return_value.upload_file_delivery
|
||||
service.get_signed_image_preview.return_value = _delivery(mime_type="image/png", extension="png")
|
||||
|
||||
with app.test_request_context(_SIGNED_QUERY):
|
||||
response = module.ImagePreviewApi().get("file-id")
|
||||
|
||||
assert response.mimetype == "image/png"
|
||||
service.get_signed_image_preview.assert_called_once_with(
|
||||
file_id="file-id",
|
||||
timestamp="123",
|
||||
nonce="abc",
|
||||
sign="sig",
|
||||
)
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_not_found(self, mock_application_services, app: Flask):
|
||||
service = mock_application_services.return_value.upload_file_delivery
|
||||
service.get_signed_image_preview.side_effect = UploadFileDeliveryNotFoundError(
|
||||
"File not found or signature is invalid"
|
||||
)
|
||||
|
||||
with (
|
||||
app.test_request_context(_SIGNED_QUERY),
|
||||
pytest.raises(NotFound, match="File not found or signature is invalid"),
|
||||
):
|
||||
module.ImagePreviewApi().get("file-id")
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_unsupported_file_type(self, mock_application_services, app: Flask):
|
||||
service = mock_application_services.return_value.upload_file_delivery
|
||||
service.get_signed_image_preview.side_effect = UnsupportedFileTypeServiceError()
|
||||
|
||||
with app.test_request_context(_SIGNED_QUERY), pytest.raises(module.UnsupportedFileTypeError):
|
||||
module.ImagePreviewApi().get("file-id")
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_missing_signature_params_are_rejected(self, mock_application_services, app: Flask):
|
||||
"""An incomplete signature must fail validation before the service is reached."""
|
||||
with app.test_request_context("/?timestamp=123"), pytest.raises(UnprocessableEntity):
|
||||
module.ImagePreviewApi().get("file-id")
|
||||
|
||||
mock_application_services.return_value.upload_file_delivery.get_signed_image_preview.assert_not_called()
|
||||
|
||||
|
||||
class TestFilePreviewApi:
|
||||
@patch.object(module, "enforce_download_for_html")
|
||||
@patch.object(module, "application_services")
|
||||
def test_inline_preview_uses_file_metadata(self, mock_application_services, mock_enforce, app: Flask):
|
||||
service = mock_application_services.return_value.upload_file_delivery
|
||||
service.get_signed_file_preview.return_value = _delivery(
|
||||
mime_type="application/pdf",
|
||||
size=100,
|
||||
name="doc.pdf",
|
||||
extension="pdf",
|
||||
)
|
||||
|
||||
with app.test_request_context(_SIGNED_QUERY):
|
||||
response = module.FilePreviewApi().get("file-id")
|
||||
|
||||
assert response.mimetype == "application/pdf"
|
||||
assert response.headers["Content-Type"] == "application/pdf"
|
||||
assert response.headers["Content-Length"] == "100"
|
||||
assert "Accept-Ranges" not in response.headers
|
||||
mock_enforce.assert_called_once_with(
|
||||
response,
|
||||
mime_type="application/pdf",
|
||||
filename="doc.pdf",
|
||||
extension="pdf",
|
||||
)
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_audio_preview_supports_ranges(self, mock_application_services, app: Flask):
|
||||
mock_application_services.return_value.upload_file_delivery.get_signed_file_preview.return_value = _delivery(
|
||||
mime_type="audio/mpeg",
|
||||
extension="mp3",
|
||||
)
|
||||
|
||||
with app.test_request_context(_SIGNED_QUERY):
|
||||
response = module.FilePreviewApi().get("file-id")
|
||||
|
||||
assert response.headers["Accept-Ranges"] == "bytes"
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_zero_size_omits_content_length(self, mock_application_services, app: Flask):
|
||||
mock_application_services.return_value.upload_file_delivery.get_signed_file_preview.return_value = _delivery(
|
||||
size=0
|
||||
)
|
||||
|
||||
with app.test_request_context(_SIGNED_QUERY):
|
||||
response = module.FilePreviewApi().get("file-id")
|
||||
|
||||
assert "Content-Length" not in response.headers
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("mime_type", "name", "extension"),
|
||||
[
|
||||
("Image/SVG+XML; charset=UTF-8", "image.png", "png"),
|
||||
("image/png", "image.SVG", "png"),
|
||||
("image/png", "image.png", ".SVG"),
|
||||
],
|
||||
ids=("mime-type", "filename", "extension"),
|
||||
)
|
||||
@patch.object(module, "application_services")
|
||||
def test_svg_preview_forces_download(self, mock_application_services, mime_type, name, extension, app: Flask):
|
||||
mock_application_services.return_value.upload_file_delivery.get_signed_file_preview.return_value = _delivery(
|
||||
mime_type=mime_type,
|
||||
size=11,
|
||||
name=name,
|
||||
extension=extension,
|
||||
)
|
||||
|
||||
with app.test_request_context(_SIGNED_QUERY):
|
||||
response = module.FilePreviewApi().get("file-id")
|
||||
|
||||
assert response.headers["Content-Disposition"].startswith("attachment")
|
||||
assert response.headers["Content-Type"] == "application/octet-stream"
|
||||
assert response.headers["X-Content-Type-Options"] == "nosniff"
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_html_preview_still_forces_download(self, mock_application_services, app: Flask):
|
||||
mock_application_services.return_value.upload_file_delivery.get_signed_file_preview.return_value = _delivery(
|
||||
mime_type="text/html",
|
||||
size=25,
|
||||
name="unsafe.html",
|
||||
extension="html",
|
||||
)
|
||||
|
||||
with app.test_request_context(_SIGNED_QUERY):
|
||||
response = module.FilePreviewApi().get("file-id")
|
||||
|
||||
assert response.headers["Content-Disposition"].startswith("attachment")
|
||||
assert response.headers["Content-Type"] == "application/octet-stream"
|
||||
assert response.headers["X-Content-Type-Options"] == "nosniff"
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_as_attachment_encodes_filename(self, mock_application_services, app: Flask):
|
||||
mock_application_services.return_value.upload_file_delivery.get_signed_file_preview.return_value = _delivery(
|
||||
mime_type="application/pdf",
|
||||
name="报告.pdf",
|
||||
extension="pdf",
|
||||
)
|
||||
|
||||
with app.test_request_context(f"{_SIGNED_QUERY}&as_attachment=true"):
|
||||
response = module.FilePreviewApi().get("file-id")
|
||||
|
||||
assert response.headers["Content-Disposition"] == "attachment; filename*=UTF-8''%E6%8A%A5%E5%91%8A.pdf"
|
||||
assert response.headers["Content-Type"] == "application/octet-stream"
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_not_found(self, mock_application_services, app: Flask):
|
||||
mock_application_services.return_value.upload_file_delivery.get_signed_file_preview.side_effect = (
|
||||
UploadFileDeliveryNotFoundError("File not found or signature is invalid")
|
||||
)
|
||||
|
||||
with (
|
||||
app.test_request_context(_SIGNED_QUERY),
|
||||
pytest.raises(NotFound, match="File not found or signature is invalid"),
|
||||
):
|
||||
module.FilePreviewApi().get("file-id")
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_missing_signature_params_are_rejected(self, mock_application_services, app: Flask):
|
||||
"""An incomplete signature must fail validation before the service is reached."""
|
||||
with app.test_request_context("/?nonce=abc"), pytest.raises(UnprocessableEntity):
|
||||
module.FilePreviewApi().get("file-id")
|
||||
|
||||
mock_application_services.return_value.upload_file_delivery.get_signed_file_preview.assert_not_called()
|
||||
|
||||
|
||||
class TestWorkspaceWebappLogoApi:
|
||||
@patch.object(module, "application_services")
|
||||
def test_success(self, mock_application_services):
|
||||
service = mock_application_services.return_value.upload_file_delivery
|
||||
service.get_workspace_webapp_logo.return_value = _delivery(
|
||||
content=b"logo",
|
||||
mime_type="image/png",
|
||||
extension="png",
|
||||
)
|
||||
|
||||
response = unwrap(module.WorkspaceWebappLogoApi().get)("workspace-id")
|
||||
|
||||
assert response.mimetype == "image/png"
|
||||
service.get_workspace_webapp_logo.assert_called_once_with(workspace_id="workspace-id")
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_logo_not_configured(self, mock_application_services):
|
||||
mock_application_services.return_value.upload_file_delivery.get_workspace_webapp_logo.side_effect = (
|
||||
UploadFileDeliveryNotFoundError("webapp logo is not found")
|
||||
)
|
||||
|
||||
with pytest.raises(NotFound, match="webapp logo is not found"):
|
||||
unwrap(module.WorkspaceWebappLogoApi().get)("workspace-id")
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_workspace_not_found_uses_default_404(self, mock_application_services):
|
||||
mock_application_services.return_value.upload_file_delivery.get_workspace_webapp_logo.side_effect = (
|
||||
UploadFileDeliveryNotFoundError()
|
||||
)
|
||||
|
||||
with pytest.raises(NotFound) as error:
|
||||
unwrap(module.WorkspaceWebappLogoApi().get)("workspace-id")
|
||||
|
||||
assert error.value.description == NotFound.description
|
||||
|
||||
@patch.object(module, "application_services")
|
||||
def test_unsupported_file_type(self, mock_application_services):
|
||||
mock_application_services.return_value.upload_file_delivery.get_workspace_webapp_logo.side_effect = (
|
||||
UnsupportedFileTypeServiceError()
|
||||
)
|
||||
|
||||
with pytest.raises(module.UnsupportedFileTypeError):
|
||||
unwrap(module.WorkspaceWebappLogoApi().get)("workspace-id")
|
||||
@@ -0,0 +1,151 @@
|
||||
"""Tests for the Bearer file-grant decorator."""
|
||||
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
from types import SimpleNamespace
|
||||
from typing import cast
|
||||
|
||||
import jwt
|
||||
import pytest
|
||||
from flask import Flask
|
||||
|
||||
from controllers.files.wraps import FileGrantInvalidError, FileGrantScopeDeniedError, file_grant_required
|
||||
from libs.passport import PassportService
|
||||
from services.entities.file_grant_entities import FileGrantClaims, FileGrantScope
|
||||
from services.file_grant_gateways import FILE_GRANT_AUDIENCE
|
||||
from tests.helpers.unit.file_grant_test_utils import issue_file_grant, token_gateway
|
||||
|
||||
SECRET_KEY = "file-grant-test-secret-long-enough-for-hs256"
|
||||
TENANT_ID = "11111111-1111-4111-8111-111111111111"
|
||||
APP_ID = "22222222-2222-4222-8222-222222222222"
|
||||
END_USER_ID = "55555555-5555-4555-8555-555555555555"
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def granted_config(config_overrides: Callable[..., None]) -> None:
|
||||
config_overrides(SECRET_KEY=SECRET_KEY)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def file_grant_service(granted_config: None, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
del granted_config
|
||||
service = SimpleNamespace(decode_grant=token_gateway().decode_grant)
|
||||
monkeypatch.setattr(
|
||||
"controllers.files.wraps.application_services",
|
||||
lambda: SimpleNamespace(file_grants=service),
|
||||
)
|
||||
|
||||
|
||||
@file_grant_required(FileGrantScope.UPLOAD)
|
||||
def _view(grant: FileGrantClaims) -> FileGrantClaims:
|
||||
return grant
|
||||
|
||||
|
||||
def _call(app: Flask, authorization: str | None) -> FileGrantClaims:
|
||||
headers: dict[str, str] = {"Authorization": authorization} if authorization is not None else {}
|
||||
with app.test_request_context("/", method="POST", headers=headers):
|
||||
return cast(Callable[[], FileGrantClaims], _view)()
|
||||
|
||||
|
||||
def _grant(*scopes: FileGrantScope, ttl_seconds: int = 600) -> str:
|
||||
token, _ = issue_file_grant(
|
||||
end_user_id=END_USER_ID,
|
||||
tenant_id=TENANT_ID,
|
||||
app_id=APP_ID,
|
||||
scopes=scopes,
|
||||
ttl_seconds=ttl_seconds,
|
||||
)
|
||||
return token
|
||||
|
||||
|
||||
def test_valid_grant_is_injected_into_the_view(app: Flask) -> None:
|
||||
claims = _call(app, f"Bearer {_grant(FileGrantScope.UPLOAD, FileGrantScope.RESOLVE)}")
|
||||
|
||||
assert claims.sub == END_USER_ID
|
||||
assert claims.tenant_id == TENANT_ID
|
||||
assert claims.app_id == APP_ID
|
||||
assert claims.scopes == [FileGrantScope.UPLOAD, FileGrantScope.RESOLVE]
|
||||
|
||||
|
||||
def test_missing_authorization_is_rejected(app: Flask) -> None:
|
||||
with pytest.raises(FileGrantInvalidError):
|
||||
_call(app, None)
|
||||
|
||||
|
||||
def test_non_bearer_authorization_is_rejected(app: Flask) -> None:
|
||||
with pytest.raises(FileGrantInvalidError):
|
||||
_call(app, f"Basic {_grant(FileGrantScope.UPLOAD)}")
|
||||
|
||||
|
||||
def test_webapp_passport_cannot_be_replayed_as_a_grant(app: Flask) -> None:
|
||||
"""The passport is signed with the same key, so only ``aud`` separates them."""
|
||||
|
||||
passport = PassportService().issue(
|
||||
{
|
||||
"iss": "SELF_HOSTED",
|
||||
"sub": "Web API Passport",
|
||||
"app_id": APP_ID,
|
||||
"end_user_id": END_USER_ID,
|
||||
"exp": int(time.time()) + 600,
|
||||
}
|
||||
)
|
||||
|
||||
with pytest.raises(FileGrantInvalidError):
|
||||
_call(app, f"Bearer {passport}")
|
||||
|
||||
|
||||
def test_content_token_audience_is_not_accepted_as_a_grant(app: Flask) -> None:
|
||||
content_token = jwt.encode(
|
||||
{
|
||||
"aud": "dify-files-content",
|
||||
"kind": "upload",
|
||||
"file_id": "66666666-6666-4666-8666-666666666666",
|
||||
"exp": int(time.time()) + 600,
|
||||
},
|
||||
SECRET_KEY,
|
||||
algorithm="HS256",
|
||||
)
|
||||
|
||||
with pytest.raises(FileGrantInvalidError):
|
||||
_call(app, f"Bearer {content_token}")
|
||||
|
||||
|
||||
def test_expired_grant_is_rejected(app: Flask) -> None:
|
||||
expired = jwt.encode(
|
||||
{
|
||||
"aud": FILE_GRANT_AUDIENCE,
|
||||
"sub": END_USER_ID,
|
||||
"tenant_id": TENANT_ID,
|
||||
"app_id": APP_ID,
|
||||
"scopes": ["upload"],
|
||||
"exp": int(time.time()) - 1,
|
||||
},
|
||||
SECRET_KEY,
|
||||
algorithm="HS256",
|
||||
)
|
||||
|
||||
with pytest.raises(FileGrantInvalidError):
|
||||
_call(app, f"Bearer {expired}")
|
||||
|
||||
|
||||
def test_grant_signed_with_another_key_is_rejected(app: Flask) -> None:
|
||||
forged = jwt.encode(
|
||||
{
|
||||
"aud": FILE_GRANT_AUDIENCE,
|
||||
"sub": END_USER_ID,
|
||||
"tenant_id": TENANT_ID,
|
||||
"app_id": APP_ID,
|
||||
"scopes": ["upload"],
|
||||
"exp": int(time.time()) + 600,
|
||||
},
|
||||
"some-other-secret-long-enough-for-hs256-signing",
|
||||
algorithm="HS256",
|
||||
)
|
||||
|
||||
with pytest.raises(FileGrantInvalidError):
|
||||
_call(app, f"Bearer {forged}")
|
||||
|
||||
|
||||
def test_grant_without_the_required_scope_is_denied(app: Flask) -> None:
|
||||
with pytest.raises(FileGrantScopeDeniedError):
|
||||
_call(app, f"Bearer {_grant(FileGrantScope.RESOLVE, FileGrantScope.PRODUCE)}")
|
||||
Reference in new issue
Block a user