chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动

演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
wuzilongandCursor committed 2026-11-18 13:59:25 +08:00
1 parent 60158935f4
commit 6519682885
15831 files changed
+3063428

No files matched your search

@@ -0,0 +1,493 @@
"""Annotation writes through real Console admission, repositories, and SQLite."""
from decimal import Decimal
from typing import Literal
from uuid import uuid4
import pytest
from sqlalchemy import event, select, text, update
from sqlalchemy.orm import Session
from werkzeug.test import TestResponse
from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN
from controllers.common.rbac import RBAC_CHECKS_ATTR, PlainApp, RBACPermission
from controllers.console.app import annotation as annotation_module
from enums.account import TenantAccountRole
from libs.external_api import ExternalApi
from libs.passport import PassportService
from libs.token import generate_csrf_token
from models import Account, App
from models.account import AccountStatus, TenantAccountJoin
from models.enums import ConversationFromSource
from models.model import AppAnnotationHitHistory, Message, MessageAnnotation
from tests.e2e_tests.contracts.controllers.console.app import test_annotation_queries
from tests.e2e_tests.contracts.controllers.console.app.test_annotation_queries import _annotation, _Harness, _history
from tests.helpers.unit.model_factories import make_account, make_app, make_conversation, make_message
query_harness = test_annotation_queries.harness
type _Operation = Literal["create", "update", "delete", "delete_many", "clear"]
_OPERATIONS: tuple[_Operation, ...] = ("create", "update", "delete", "delete_many", "clear")
@pytest.fixture
def harness(query_harness: _Harness) -> _Harness:
api = ExternalApi(query_harness.app)
api.add_resource(
annotation_module.AnnotationUpdateDeleteApi,
"/apps/<uuid:app_id>/annotations/<uuid:annotation_id>",
endpoint="console.annotation_commands",
)
return query_harness
def _request(
harness: _Harness,
operation: _Operation,
*,
payload: dict[str, object] | None = None,
app_id: str | None = None,
annotation_id: str | None = None,
annotation_ids: list[str] | None = None,
authenticated: bool = True,
csrf: bool = True,
) -> TestResponse:
path = f"/apps/{app_id or harness.target.id}/annotations"
if operation in {"update", "delete"}:
path += f"/{annotation_id or harness.annotation.id}"
query = (
[
("annotation_id", value)
for value in (annotation_ids if annotation_ids is not None else [harness.annotation.id])
]
if operation == "delete_many"
else None
)
client = harness.app.test_client()
token = generate_csrf_token(harness.account.id)
headers = {HEADER_NAME_CSRF_TOKEN: token} if csrf else {}
if authenticated:
headers["Authorization"] = f"Bearer {PassportService().issue({'user_id': harness.account.id})}"
client.set_cookie(COOKIE_NAME_CSRF_TOKEN, token)
commits: list[Session] = []
def record_commit(session: Session) -> None:
commits.append(session)
event.listen(Session, "before_commit", record_commit)
try:
response = client.open(
path,
method="POST" if operation in {"create", "update"} else "DELETE",
json=payload,
query_string=query,
headers=headers,
)
finally:
event.remove(Session, "before_commit", record_commit)
assert len(commits) == (1 if response.status_code < 400 else 0)
assert all(not session.in_transaction() and not session.identity_map for session in harness.sessions)
if response.status_code == 204:
assert response.data == b""
else:
assert response.headers["Content-Type"] == "application/json"
return response
def _annotations(harness: _Harness) -> list[MessageAnnotation]:
with harness.factory() as session:
return list(session.scalars(select(MessageAnnotation).order_by(MessageAnnotation.id)).all())
def _message(harness: _Harness, *, app_id: str | None = None) -> Message:
conversation = make_conversation(
conversation_id=str(uuid4()),
app_id=app_id or harness.target.id,
inputs={},
from_source=ConversationFromSource.CONSOLE,
from_account_id=harness.account.id,
)
message = make_message(
message_id=str(uuid4()),
app_id=conversation.app_id,
conversation_id=conversation.id,
inputs={},
message={},
query="Message question",
answer="Original response",
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
currency="USD",
from_source=ConversationFromSource.CONSOLE,
from_account_id=harness.account.id,
)
with harness.factory.begin() as session:
session.add_all([conversation, message])
return message
@pytest.mark.parametrize(
("answer_fields", "expected"),
[
({"answer": "Answer"}, "Answer"),
({"content": "Legacy content"}, "Legacy content"),
({"answer": "", "content": "Fallback"}, "Fallback"),
({"answer": "Answer", "content": "Ignored"}, "Answer"),
({"content": ""}, ""),
],
)
def test_create_direct_annotation_preserves_answer_fallback_and_response(
harness: _Harness, answer_fields: dict[str, object], expected: str
) -> None:
response = _request(harness, "create", payload={"question": "Manual question", **answer_fields})
assert response.status_code == 201
body = response.get_json()
with harness.factory() as session:
stored = session.get(MessageAnnotation, body["id"])
assert stored is not None
assert (stored.app_id, stored.account_id, stored.conversation_id, stored.message_id) == (
harness.target.id,
harness.account.id,
None,
None,
)
assert body == {
"id": stored.id,
"question": "Manual question",
"answer": expected,
"hit_count": 0,
"created_at": int(stored.created_at.timestamp()),
}
assert stored.question == "Manual question"
assert stored.content == expected
@pytest.mark.parametrize(
("operation", "payload", "message"),
[
("create", {"question": "Question"}, "Either 'answer' or 'content' must be provided"),
("create", {"question": "Question", "answer": ""}, "Either 'answer' or 'content' must be provided"),
("create", {"answer": "Answer"}, "'question' is required when 'message_id' is not provided"),
("create", {"question": "", "answer": "Answer"}, "'question' is required when 'message_id' is not provided"),
("update", {"answer": "Answer"}, "'question' is required"),
("update", {"question": "Question"}, "'answer' is required"),
("update", {"question": "Question", "content": "Ignored"}, "'answer' is required"),
],
)
def test_missing_fields_reject_without_writing(
harness: _Harness, operation: _Operation, payload: dict[str, object], message: str
) -> None:
response = _request(harness, operation, payload=payload)
assert response.status_code == 400
assert response.get_json() == {"code": "invalid_param", "message": message, "status": 400}
[stored] = _annotations(harness)
assert (stored.id, stored.question, stored.content) == (harness.annotation.id, "First question", "First answer")
def test_linked_upsert_preserves_message_links_and_original_author(harness: _Harness) -> None:
message = _message(harness)
response = _request(harness, "create", payload={"message_id": message.id, "answer": "First annotation"})
assert response.status_code == 201
annotation_id = response.get_json()["id"]
assert response.get_json()["question"] == message.query
original_author = make_account(account_id=str(uuid4()), email="original@example.com")
with harness.factory.begin() as session:
session.add(original_author)
session.execute(
update(MessageAnnotation)
.where(MessageAnnotation.id == annotation_id)
.values(account_id=original_author.id, hit_count=5)
)
response = _request(
harness, "create", payload={"message_id": message.id, "question": "Override", "answer": "Updated"}
)
assert response.status_code == 201
assert response.get_json()["id"] == annotation_id
assert response.get_json()["hit_count"] == 5
rows = _annotations(harness)
assert len(rows) == 2
stored = next(row for row in rows if row.id == annotation_id)
assert (stored.app_id, stored.conversation_id, stored.message_id, stored.account_id) == (
harness.target.id,
message.conversation_id,
message.id,
original_author.id,
)
assert (stored.question, stored.content) == ("Override", "Updated")
def test_linked_upsert_does_not_modify_foreign_app_annotation(harness: _Harness) -> None:
message = _message(harness)
other = make_app(app_id=str(uuid4()), tenant_id=harness.target.tenant_id)
foreign = MessageAnnotation(
app_id=other.id,
message_id=message.id,
question="Other question",
content="Other answer",
account_id=harness.account.id,
)
with harness.factory.begin() as session:
session.add_all([other, foreign])
response = _request(harness, "create", payload={"message_id": message.id, "question": "", "answer": "New answer"})
assert response.status_code == 201
assert response.get_json()["id"] != foreign.id
assert response.get_json()["question"] == message.query
with harness.factory() as session:
stored = session.get(MessageAnnotation, foreign.id)
assert stored is not None
assert (stored.question, stored.content) == ("Other question", "Other answer")
@pytest.mark.parametrize("missing", [False, True], ids=["other-app", "missing"])
def test_create_requires_message_in_the_admitted_app(harness: _Harness, missing: bool) -> None:
message_id = str(uuid4())
if not missing:
other = make_app(app_id=str(uuid4()), tenant_id=harness.target.tenant_id)
with harness.factory.begin() as session:
session.add(other)
message_id = _message(harness, app_id=other.id).id
response = _request(harness, "create", payload={"message_id": message_id, "answer": "Answer"})
assert response.status_code == 404
assert response.get_json()["message"] == "Message Not Exists."
assert [row.id for row in _annotations(harness)] == [harness.annotation.id]
@pytest.mark.parametrize(("question", "answer"), [("Updated question", "Updated answer"), ("", "")])
def test_update_accepts_empty_strings_and_ignores_legacy_content(harness: _Harness, question: str, answer: str) -> None:
response = _request(harness, "update", payload={"question": question, "answer": answer, "content": "Ignored"})
assert response.status_code == 200
[stored] = _annotations(harness)
assert (stored.question, stored.content, stored.account_id) == (question, answer, harness.account.id)
assert response.get_json() == {
"id": harness.annotation.id,
"question": question,
"answer": answer,
"hit_count": 3,
"created_at": int(harness.annotation.created_at.timestamp()),
}
def test_delete_returns_empty_204_and_removes_only_owned_hit_history(harness: _Harness) -> None:
owned = _history(harness, index=0)
other_app = _history(harness, index=1, app_id=str(uuid4()))
other_annotation = _history(harness, index=2)
other_annotation.annotation_id = str(uuid4())
with harness.factory.begin() as session:
session.add_all([owned, other_app, other_annotation])
response = _request(harness, "delete")
assert response.status_code == 204
assert _annotations(harness) == []
with harness.factory() as session:
assert set(session.scalars(select(AppAnnotationHitHistory.id)).all()) == {other_app.id, other_annotation.id}
@pytest.mark.parametrize("operation", _OPERATIONS)
@pytest.mark.parametrize("scope", ["missing", "other_tenant", "disabled"])
def test_writes_require_an_available_app_in_the_admitted_tenant(
harness: _Harness, operation: _Operation, scope: str
) -> None:
app_id = harness.target.id
if scope == "missing":
app_id = str(uuid4())
else:
with harness.factory.begin() as session:
if scope == "other_tenant":
session.execute(update(App).where(App.id == app_id).values(tenant_id=str(uuid4())))
else:
session.execute(text("UPDATE apps SET status = 'disabled' WHERE id = :app_id"), {"app_id": app_id})
response = _request(harness, operation, app_id=app_id, payload={"question": "Question", "answer": "Answer"})
assert response.status_code == 404
assert response.get_json()["message"] == "App not found"
[stored] = _annotations(harness)
assert (stored.question, stored.content) == ("First question", "First answer")
@pytest.mark.parametrize("operation", ["update", "delete"])
@pytest.mark.parametrize("scope", ["missing", "other_app"])
def test_update_and_delete_require_annotation_in_the_admitted_app(
harness: _Harness, operation: _Operation, scope: str
) -> None:
annotation_id = str(uuid4())
if scope == "other_app":
with harness.factory.begin() as session:
session.execute(
update(MessageAnnotation)
.where(MessageAnnotation.id == harness.annotation.id)
.values(app_id=str(uuid4()))
)
annotation_id = harness.annotation.id
response = _request(
harness, operation, annotation_id=annotation_id, payload={"question": "Question", "answer": "Answer"}
)
assert response.status_code == 404
assert response.get_json()["message"] == "Annotation not found"
[stored] = _annotations(harness)
assert (stored.question, stored.content) == ("First question", "First answer")
@pytest.mark.parametrize("operation", _OPERATIONS)
@pytest.mark.parametrize("role", list(TenantAccountRole))
def test_write_roles_are_preserved(harness: _Harness, operation: _Operation, role: TenantAccountRole) -> None:
with harness.factory.begin() as session:
session.execute(
update(TenantAccountJoin).where(TenantAccountJoin.account_id == harness.account.id).values(role=role)
)
response = _request(harness, operation, payload={"question": "Question", "answer": "Answer"})
if TenantAccountRole.is_editing_role(role):
assert (
response.status_code
== {"create": 201, "update": 200, "delete": 204, "delete_many": 204, "clear": 204}[operation]
)
else:
assert response.status_code == 403
assert response.get_json()["code"] == "forbidden"
assert [row.id for row in _annotations(harness)] == [harness.annotation.id]
@pytest.mark.parametrize("operation", _OPERATIONS)
def test_writes_require_authentication_csrf_and_initialization(harness: _Harness, operation: _Operation) -> None:
payload: dict[str, object] = {"question": "Question", "answer": "Answer"}
for authenticated, csrf in ((False, True), (True, False)):
response = _request(harness, operation, payload=payload, authenticated=authenticated, csrf=csrf)
assert response.status_code == 401
assert response.get_json()["code"] == "unauthorized"
with harness.factory.begin() as session:
session.execute(
update(Account).where(Account.id == harness.account.id).values(status=AccountStatus.UNINITIALIZED)
)
response = _request(harness, operation, payload=payload)
assert response.status_code == 400
assert response.get_json()["code"] == "account_not_initialized"
assert [row.id for row in _annotations(harness)] == [harness.annotation.id]
def test_single_annotation_writes_keep_app_edit_rbac_declaration() -> None:
for view in (
annotation_module.AnnotationApi.post,
annotation_module.AnnotationUpdateDeleteApi.post,
annotation_module.AnnotationUpdateDeleteApi.delete,
):
[check] = vars(view)[RBAC_CHECKS_ATTR]
assert check.scene == RBACPermission.APP_EDIT
assert isinstance(check.locator, PlainApp)
@pytest.mark.parametrize("operation", ["delete_many", "clear"])
def test_batch_deletion_preserves_foreign_app_rows_and_unrelated_history(
harness: _Harness, operation: _Operation
) -> None:
selected = _annotation(harness, index=1)
unselected = _annotation(harness, index=2)
foreign_app = make_app(app_id=str(uuid4()), tenant_id=harness.target.tenant_id)
foreign = _annotation(harness, index=3, app_id=foreign_app.id)
owned_history = _history(harness, index=0)
selected_history = _history(harness, index=1)
selected_history.annotation_id = selected.id
unselected_history = _history(harness, index=2)
unselected_history.annotation_id = unselected.id
foreign_history = _history(harness, index=3, app_id=foreign_app.id)
foreign_history.annotation_id = foreign.id
foreign_linked_history = _history(harness, index=4, app_id=foreign_app.id)
orphan_history = _history(harness, index=5)
orphan_history.annotation_id = str(uuid4())
with harness.factory.begin() as session:
session.add_all(
[
foreign_app,
selected,
unselected,
foreign,
owned_history,
selected_history,
unselected_history,
foreign_history,
foreign_linked_history,
orphan_history,
]
)
response = _request(
harness,
operation,
annotation_ids=[harness.annotation.id, selected.id, selected.id, foreign.id, str(uuid4())],
)
assert response.status_code == 204
expected_annotations = {foreign.id}
expected_histories = {foreign_history.id, foreign_linked_history.id, orphan_history.id}
if operation == "delete_many":
expected_annotations.add(unselected.id)
expected_histories.add(unselected_history.id)
assert {row.id for row in _annotations(harness)} == expected_annotations
with harness.factory() as session:
assert set(session.scalars(select(AppAnnotationHitHistory.id))) == expected_histories
@pytest.mark.parametrize("values", [[""], ["missing"], ["", "missing"]])
def test_batch_deletion_with_no_matching_ids_does_not_clear_annotations(harness: _Harness, values: list[str]) -> None:
history = _history(harness, index=0)
with harness.factory.begin() as session:
session.add(history)
response = _request(harness, "delete_many", annotation_ids=values)
assert response.status_code == 204
assert [row.id for row in _annotations(harness)] == [harness.annotation.id]
with harness.factory() as session:
assert session.get(AppAnnotationHitHistory, history.id) is not None
def test_batch_deletion_accepts_empty_strings_alongside_valid_ids(harness: _Harness) -> None:
response = _request(harness, "delete_many", annotation_ids=["", harness.annotation.id, ""])
assert response.status_code == 204
assert _annotations(harness) == []
@pytest.mark.parametrize("invalid", [" ", "\t", "\n"])
def test_batch_deletion_rejects_whitespace_before_any_write(harness: _Harness, invalid: str) -> None:
history = _history(harness, index=0)
with harness.factory.begin() as session:
session.add(history)
response = _request(harness, "delete_many", annotation_ids=[harness.annotation.id, invalid])
assert response.status_code == 400
assert response.get_json() == {
"code": "bad_request",
"message": "annotation_ids are required if the parameter is provided.",
"status": 400,
}
assert [row.id for row in _annotations(harness)] == [harness.annotation.id]
with harness.factory() as session:
assert session.get(AppAnnotationHitHistory, history.id) is not None
def test_clearing_an_already_empty_app_is_idempotent(harness: _Harness) -> None:
assert _request(harness, "clear").status_code == 204
assert _request(harness, "clear").status_code == 204
assert _annotations(harness) == []
def test_batch_delete_keeps_app_management_rbac_declaration() -> None:
[check] = vars(annotation_module.AnnotationApi.delete)[RBAC_CHECKS_ATTR]
assert check.scene == RBACPermission.APP_CREATE_AND_MANAGEMENT
assert isinstance(check.locator, PlainApp)
@@ -0,0 +1,220 @@
"""Import admission validates identity, app scope and transport without touching Redis."""
from collections.abc import Callable
from io import BytesIO
from typing import Literal
from uuid import uuid4
import pytest
from sqlalchemy import text, update
from werkzeug.datastructures import MultiDict
from werkzeug.test import TestResponse
from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN
from controllers.common.rbac import RBAC_CHECKS_ATTR, PlainApp, RBACPermission
from controllers.console.app import annotation as annotation_module
from enums.account import TenantAccountRole
from libs.external_api import ExternalApi
from libs.helper import dump_response
from libs.passport import PassportService
from libs.token import generate_csrf_token
from models import Account
from models.account import AccountStatus, TenantAccountJoin
from services.annotation_import_service import (
AnnotationImportResult,
AnnotationImportValidationError,
parse_annotation_csv,
)
from tests.e2e_tests.contracts.controllers.console.app import test_annotation_queries
from tests.e2e_tests.contracts.controllers.console.app.test_annotation_queries import _Harness
from tests.helpers.unit.model_factories import make_app
query_harness = test_annotation_queries.harness
type _Operation = Literal["import", "status"]
_CSV = b"question,answer\nQuestion,Answer\n"
@pytest.fixture
def harness(query_harness: _Harness) -> _Harness:
api = ExternalApi(query_harness.app)
api.add_resource(
annotation_module.AnnotationBatchImportApi,
"/apps/<uuid:app_id>/annotations/batch-import",
endpoint="console.annotation_import",
)
api.add_resource(
annotation_module.AnnotationBatchImportStatusApi,
"/apps/<uuid:app_id>/annotations/batch-import-status/<uuid:job_id>",
endpoint="console.annotation_import_status",
)
return query_harness
def _request(
harness: _Harness,
operation: _Operation,
*,
app_id: str | None = None,
files: tuple[tuple[str, str, bytes], ...] = (("file", "annotations.csv", _CSV),),
authenticated: bool = True,
csrf: bool = True,
) -> TestResponse:
client = harness.app.test_client()
token = generate_csrf_token(harness.account.id)
headers = {HEADER_NAME_CSRF_TOKEN: token} if csrf else {}
if authenticated:
headers["Authorization"] = f"Bearer {PassportService().issue({'user_id': harness.account.id})}"
client.set_cookie(COOKIE_NAME_CSRF_TOKEN, token)
path = f"/apps/{app_id or harness.target.id}/annotations/batch-import"
if operation == "status":
response = client.get(f"{path}-status/{uuid4()}", headers=headers)
else:
data = MultiDict((field, (BytesIO(content), filename)) for field, filename, content in files)
response = client.post(path, data=data, content_type="multipart/form-data", headers=headers)
assert response.headers["Content-Type"] == "application/json"
assert int(response.headers["Content-Length"]) == len(response.data)
assert all(not session.in_transaction() and not session.identity_map for session in harness.sessions)
return response
@pytest.mark.parametrize("operation", ["import", "status"])
@pytest.mark.parametrize("reason", ["missing", "other_tenant", "disabled"])
def test_unavailable_app_rejects_before_reading_or_writing_jobs(
harness: _Harness, operation: _Operation, reason: str
) -> None:
app_id = str(uuid4())
with harness.factory.begin() as session:
if reason == "other_tenant":
session.add(make_app(app_id=app_id, tenant_id=str(uuid4())))
elif reason == "disabled":
app_id = harness.target.id
session.execute(text("UPDATE apps SET status = 'disabled' WHERE id = :id"), {"id": app_id})
response = _request(harness, operation, app_id=app_id)
assert response.status_code == 404
assert response.get_json()["code"] == "not_found"
assert response.get_json()["message"] == "App not found"
@pytest.mark.parametrize(
("files", "code"),
[
((), "no_file_uploaded"),
((("other", "annotations.csv", _CSV),), "no_file_uploaded"),
((("file", "first.csv", _CSV), ("second", "second.csv", _CSV)), "too_many_files"),
((("file", "first.csv", _CSV), ("file", "second.csv", _CSV)), "too_many_files"),
((("file", "annotations.txt", _CSV),), "invalid_param"),
((("file", "", _CSV),), "invalid_param"),
((("file", "annotations.csv", b""),), "invalid_param"),
],
)
def test_invalid_upload_rejects_before_rate_or_concurrency_checks(
harness: _Harness, files: tuple[tuple[str, str, bytes], ...], code: str
) -> None:
response = _request(harness, "import", files=files)
assert response.status_code == 400
assert response.get_json()["code"] == code
def test_upload_size_limit_is_413_before_rate_or_concurrency_checks(
harness: _Harness, config_overrides: Callable[..., None]
) -> None:
config_overrides(ANNOTATION_IMPORT_FILE_SIZE_LIMIT=1)
response = _request(harness, "import", files=(("file", "annotations.csv", b"x" * (1024 * 1024 + 1)),))
assert response.status_code == 413
assert response.get_json()["code"] == "file_too_large"
@pytest.mark.parametrize("role", [TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR])
def test_editing_roles_reach_file_validation(harness: _Harness, role: TenantAccountRole) -> None:
with harness.factory.begin() as session:
session.execute(
update(TenantAccountJoin).where(TenantAccountJoin.account_id == harness.account.id).values(role=role)
)
response = _request(harness, "import", files=())
assert response.status_code == 400
assert response.get_json()["code"] == "no_file_uploaded"
@pytest.mark.parametrize("operation", ["import", "status"])
@pytest.mark.parametrize("role", [TenantAccountRole.NORMAL, TenantAccountRole.DATASET_OPERATOR])
def test_import_and_status_preserve_editor_requirement(
harness: _Harness, operation: _Operation, role: TenantAccountRole
) -> None:
with harness.factory.begin() as session:
session.execute(
update(TenantAccountJoin).where(TenantAccountJoin.account_id == harness.account.id).values(role=role)
)
response = _request(harness, operation)
assert response.status_code == 403
assert response.get_json()["code"] == "forbidden"
@pytest.mark.parametrize("operation", ["import", "status"])
@pytest.mark.parametrize(("authenticated", "csrf"), [(False, True), (True, False)])
def test_import_and_status_require_authentication_and_csrf(
harness: _Harness, operation: _Operation, authenticated: bool, csrf: bool
) -> None:
response = _request(harness, operation, authenticated=authenticated, csrf=csrf)
assert response.status_code == 401
assert response.get_json()["code"] == "unauthorized"
@pytest.mark.parametrize("operation", ["import", "status"])
def test_uninitialized_account_cannot_import_or_read_jobs(harness: _Harness, operation: _Operation) -> None:
with harness.factory.begin() as session:
session.execute(
update(Account).where(Account.id == harness.account.id).values(status=AccountStatus.UNINITIALIZED)
)
response = _request(harness, operation)
assert response.status_code == 400
assert response.get_json()["code"] == "account_not_initialized"
def test_import_and_status_preserve_distinct_rbac_permissions() -> None:
[write_check] = vars(annotation_module.AnnotationBatchImportApi.post)[RBAC_CHECKS_ATTR]
[read_check] = vars(annotation_module.AnnotationBatchImportStatusApi.get)[RBAC_CHECKS_ATTR]
assert write_check.scene == RBACPermission.APP_EDIT
assert read_check.scene == RBACPermission.APP_VIEW_LAYOUT
assert isinstance(write_check.locator, PlainApp)
assert isinstance(read_check.locator, PlainApp)
def test_import_result_serialization_preserves_legacy_nullable_error_field() -> None:
job_id = str(uuid4())
legacy_result = {"job_id": job_id, "job_status": "waiting", "record_count": 2}
result = AnnotationImportResult(job_id=job_id, job_status="waiting", record_count=2)
assert dump_response(annotation_module.AnnotationBatchImportResponse, result) == {
"job_id": job_id,
"job_status": "waiting",
"record_count": 2,
"error_msg": None,
}
assert dump_response(annotation_module.AnnotationBatchImportResponse, result) == dump_response(
annotation_module.AnnotationBatchImportResponse, legacy_result
)
def test_csv_validation_error_serialization_preserves_nullable_success_fields() -> None:
with pytest.raises(AnnotationImportValidationError, match="can't decode byte") as error:
parse_annotation_csv(BytesIO(b"question,answer\n\xff,Answer\n"), min_records=1, max_records=2)
assert dump_response(annotation_module.AnnotationBatchImportResponse, {"error_msg": str(error.value)}) == {
"job_id": None,
"job_status": None,
"record_count": None,
"error_msg": str(error.value),
}
@@ -0,0 +1,553 @@
"""Annotation reads through real Console admission, application wiring and SQLite."""
from collections.abc import Callable, Iterator
from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import Literal
from uuid import uuid4
import pytest
from flask import Blueprint, Flask
from redis import Redis
from sqlalchemy import Connection, Engine, delete, event, text, update
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN
from controllers.common.rbac import RBAC_CHECKS_ATTR, PlainApp, RBACPermission
from controllers.console.app import annotation as annotation_module
from controllers.console.wraps import _is_setup_completed
from enums import DeploymentEdition
from enums.account import TenantAccountRole
from extensions.ext_application_services import build_application_services
from extensions.ext_database import db
from extensions.ext_login import DifyLoginManager, bind_account_loader, load_user_from_request, unauthorized_handler
from extensions.ext_redis import RedisClientWrapper
from libs.datetime_utils import naive_utc_now
from libs.external_api import ExternalApi
from libs.passport import PassportService
from libs.token import generate_csrf_token
from models import Account, App, AppMode, Tenant
from models.account import AccountStatus, TenantAccountJoin
from models.agent import Agent, AgentScope, AgentSource, AgentStatus
from models.dataset import DatasetCollectionBinding
from models.model import AppAnnotationHitHistory, AppAnnotationSetting, DifySetup, MessageAnnotation
from tests.helpers.unit.model_factories import make_account, make_app
type _Endpoint = Literal["count", "list", "setting", "history", "export"]
_ENDPOINTS: tuple[_Endpoint, ...] = ("count", "list", "setting", "history", "export")
_TIME = datetime(2026, 1, 2, 3, 4, 5)
@dataclass(frozen=True)
class _Harness:
app: Flask
account: Account
target: App
annotation: MessageAnnotation
factory: sessionmaker[Session]
sessions: list[Session]
def get(
self,
endpoint: _Endpoint,
*,
app_id: str | None = None,
annotation_id: str | None = None,
query: dict[str, object] | None = None,
authenticated: bool = True,
csrf: bool = True,
) -> TestResponse:
prefix = f"/apps/{app_id or self.target.id}"
routes = {
"count": f"{prefix}/annotations/count",
"list": f"{prefix}/annotations",
"setting": f"{prefix}/annotation-setting",
"history": f"{prefix}/annotations/{annotation_id or self.annotation.id}/hit-histories",
"export": f"{prefix}/annotations/export",
}
client = self.app.test_client()
token = generate_csrf_token(self.account.id)
headers = {HEADER_NAME_CSRF_TOKEN: token} if csrf else {}
if authenticated:
headers["Authorization"] = f"Bearer {PassportService().issue({'user_id': self.account.id})}"
client.set_cookie(COOKIE_NAME_CSRF_TOKEN, token)
commits: list[Session] = []
def record_commit(session: Session) -> None:
commits.append(session)
event.listen(Session, "before_commit", record_commit)
try:
response = client.get(routes[endpoint], query_string=query, headers=headers)
finally:
event.remove(Session, "before_commit", record_commit)
assert not commits
assert response.headers["Content-Type"] == "application/json"
if endpoint == "export" and response.status_code == 200:
assert response.headers["X-Content-Type-Options"] == "nosniff"
assert int(response.headers["Content-Length"]) == len(response.data)
assert all(not session.in_transaction() and not session.identity_map for session in self.sessions)
return response
@pytest.fixture
def harness(
config_overrides: Callable[..., None], sqlite_engine: Engine, sqlite_session_factory: sessionmaker[Session]
) -> Iterator[_Harness]:
config_overrides(
DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY,
SECRET_KEY="annotation-read-passport-test-key-32-bytes",
LOGIN_DISABLED=False,
INIT_PASSWORD="",
CONSOLE_API_URL="http://localhost",
CONSOLE_WEB_URL="http://localhost",
COOKIE_DOMAIN="",
RBAC_ENABLED=False,
)
workspace = Tenant(name="Annotation workspace")
account = make_account(account_id=str(uuid4()))
account.last_active_at = naive_utc_now()
target = make_app(app_id=str(uuid4()), tenant_id=workspace.id)
annotation = MessageAnnotation(
app_id=target.id, question="First question", content="First answer", account_id=account.id
)
annotation.created_at = _TIME
annotation.hit_count = 3
with sqlite_session_factory.begin() as session:
session.add_all(
[
workspace,
account,
target,
annotation,
TenantAccountJoin(
tenant_id=workspace.id, account_id=account.id, role=TenantAccountRole.OWNER, current=True
),
DifySetup(version="test"),
]
)
factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
app = Flask(__name__)
app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))
db.init_app(app)
login_manager = DifyLoginManager()
login_manager.request_loader(load_user_from_request)
login_manager.unauthorized_handler(unauthorized_handler)
login_manager.init_app(app)
redis_client = Redis()
redis = RedisClientWrapper()
redis.initialize(redis_client)
services = build_application_services(
database_client=factory,
deployment_edition=DeploymentEdition.COMMUNITY,
initialization_password="",
redis=redis,
)
app.extensions["application_services"] = services
bind_account_loader(app, services.accounts.identity.load_user)
blueprint = Blueprint("console", __name__)
api = ExternalApi(blueprint)
api.add_resource(annotation_module.MessageAnnotationCountApi, "/apps/<uuid:app_id>/annotations/count")
api.add_resource(annotation_module.AnnotationApi, "/apps/<uuid:app_id>/annotations")
api.add_resource(annotation_module.AnnotationExportApi, "/apps/<uuid:app_id>/annotations/export")
api.add_resource(annotation_module.AppAnnotationSettingDetailApi, "/apps/<uuid:app_id>/annotation-setting")
api.add_resource(
annotation_module.AnnotationHitHistoryListApi,
"/apps/<uuid:app_id>/annotations/<uuid:annotation_id>/hit-histories",
)
app.register_blueprint(blueprint)
_is_setup_completed.reset_success()
with app.app_context():
assert _is_setup_completed()
db.session.remove()
sessions: list[Session] = []
def track(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
sessions.append(session)
factories = (factory, sqlite_session_factory, db.session.session_factory)
for observed in factories:
event.listen(observed, "after_begin", track)
try:
yield _Harness(app, account, target, annotation, factory, sessions)
finally:
for observed in factories:
event.remove(observed, "after_begin", track)
assert redis_client.connection_pool._created_connections == 0
redis_client.close()
_is_setup_completed.reset_success()
with app.app_context():
db.session.remove()
db.engine.dispose()
def _annotation(
harness: _Harness, *, index: int, app_id: str | None = None, question: str | None = None
) -> MessageAnnotation:
row = MessageAnnotation(
app_id=app_id or harness.target.id,
question=question or f"Question {index}",
content=f"Answer {index}",
account_id=harness.account.id,
)
row.created_at = _TIME + timedelta(seconds=index)
row.hit_count = index
return row
def _history(harness: _Harness, *, index: int, app_id: str | None = None) -> AppAnnotationHitHistory:
row = AppAnnotationHitHistory(
app_id=app_id or harness.target.id,
annotation_id=harness.annotation.id,
source="hit-testing",
question=f"Asked {index}",
account_id=harness.account.id,
score=0.9,
message_id=str(uuid4()),
annotation_question=f"Matched {index}",
annotation_content=f"Responded {index}",
)
row.created_at = _TIME + timedelta(seconds=index)
return row
def _error(response: TestResponse, *, status: int, code: str, message: str | None = None) -> None:
assert response.status_code == status
body = response.get_json()
assert body["code"] == code
assert body["status"] == status
if message is not None:
assert body["message"] == message
def test_count_and_list_preserve_shape_order_scope_and_last_page(harness: _Harness) -> None:
second = _annotation(harness, index=1)
with harness.factory.begin() as session:
session.add_all([second, _annotation(harness, index=2, app_id=str(uuid4()))])
assert harness.get("count").get_json() == {"count": 2}
response = harness.get("list", query={"page": 1, "limit": 2})
assert response.status_code == 200
assert response.get_json() == {
"data": [
{
"id": second.id,
"question": second.question,
"answer": second.content,
"hit_count": 1,
"created_at": int(second.created_at.timestamp()),
},
{
"id": harness.annotation.id,
"question": "First question",
"answer": "First answer",
"hit_count": 3,
"created_at": int(_TIME.timestamp()),
},
],
"has_more": False,
"limit": 2,
"total": 2,
"page": 1,
}
assert harness.get("list", query={"page": 1, "limit": 1}).get_json()["has_more"] is True
assert harness.get("list", query={"page": 2, "limit": 1}).get_json()["data"][0]["id"] == harness.annotation.id
assert harness.get("list", query={"page": 3, "limit": 1}).get_json() == {
"data": [],
"has_more": False,
"limit": 1,
"total": 2,
"page": 3,
}
def test_list_keyword_is_literal_case_insensitive_and_searches_answers(harness: _Harness) -> None:
question = _annotation(harness, index=1, question="Keep 50%_Case\\ marker")
answer = _annotation(harness, index=2)
answer.content = "Answer has 50%_case\\ marker"
decoy = _annotation(harness, index=3, question="Keep 500XCase marker")
with harness.factory.begin() as session:
session.add_all([question, answer, decoy])
body = harness.get("list", query={"keyword": "50%_case\\"}).get_json()
assert body["total"] == 2
assert [row["id"] for row in body["data"]] == [answer.id, question.id]
def test_export_returns_all_owned_annotations_in_descending_time_order(harness: _Harness) -> None:
rows = [_annotation(harness, index=index) for index in range(1, 102)]
other_app = make_app(app_id=str(uuid4()), tenant_id=harness.target.tenant_id)
foreign_workspace = Tenant(name="Other annotation workspace")
foreign_app = make_app(app_id=str(uuid4()), tenant_id=foreign_workspace.id)
with harness.factory.begin() as session:
session.add_all(
[
*rows,
other_app,
foreign_workspace,
foreign_app,
_annotation(harness, index=102, app_id=other_app.id),
_annotation(harness, index=103, app_id=foreign_app.id),
]
)
response = harness.get("export", query={"page": 2, "limit": 1, "keyword": "not present"})
assert response.status_code == 200
assert response.get_json() == {
"data": [
{
"id": row.id,
"question": row.question,
"answer": row.content,
"hit_count": row.hit_count,
"created_at": int(row.created_at.timestamp()),
}
for row in [*reversed(rows), harness.annotation]
]
}
@pytest.mark.parametrize(
("value", "expected"),
[
("=1+1", "'=1+1"),
("+SUM(A1:A2)", "'+SUM(A1:A2)"),
("-2", "'-2"),
("@SUM(A1:A2)", "'@SUM(A1:A2)"),
("\tformula", "'\tformula"),
("\rformula", "'\rformula"),
("", ""),
("普通文本", "普通文本"),
],
)
def test_export_sanitizes_question_and_answer_without_persisting_changes(
harness: _Harness, value: str, expected: str
) -> None:
with harness.factory.begin() as session:
session.execute(
update(MessageAnnotation)
.where(MessageAnnotation.id == harness.annotation.id)
.values(question=value, content=value)
)
response = harness.get("export")
assert response.status_code == 200
[item] = response.get_json()["data"]
assert item["question"] == item["answer"] == expected
with harness.factory() as session:
stored = session.get(MessageAnnotation, harness.annotation.id)
assert stored is not None
assert stored.question == stored.content == value
@pytest.mark.parametrize("endpoint", ["list", "history"])
def test_page_size_cap_and_exact_last_page(harness: _Harness, endpoint: _Endpoint) -> None:
with harness.factory.begin() as session:
if endpoint == "list":
session.add_all([_annotation(harness, index=index) for index in range(1, 101)])
else:
session.add_all([_history(harness, index=index) for index in range(101)])
first = harness.get(endpoint, query={"limit": 200}).get_json()
assert (first["page"], first["limit"], first["total"], first["has_more"]) == (1, 100, 101, True)
assert len(first["data"]) == 100
last = harness.get(endpoint, query={"limit": 100, "page": 2}).get_json()
assert len(last["data"]) == 1
assert last["has_more"] is False
@pytest.mark.parametrize("query", [{"page": 0}, {"limit": 0}, {"page": "bad"}, {"limit": "bad"}])
def test_list_rejects_invalid_pagination(harness: _Harness, query: dict[str, object]) -> None:
_error(harness.get("list", query=query), status=422, code="unprocessable_entity")
@pytest.mark.parametrize(
("query", "page", "limit"),
[({"page": 0, "limit": 0}, 1, 1), ({"page": -2, "limit": -3}, 1, 1), ({"page": "bad", "limit": "bad"}, 1, 20)],
)
def test_history_retains_forgiving_pagination(
harness: _Harness, query: dict[str, object], page: int, limit: int
) -> None:
with harness.factory.begin() as session:
session.add(_history(harness, index=0))
body = harness.get("history", query=query).get_json()
assert (body["page"], body["limit"], body["total"], body["has_more"]) == (page, limit, 1, False)
def test_hit_history_aliases_order_and_complete_owner_chain(harness: _Harness) -> None:
first, second = _history(harness, index=0), _history(harness, index=1)
with harness.factory.begin() as session:
session.add_all([first, second, _history(harness, index=2, app_id=str(uuid4()))])
body = harness.get("history", query={"limit": 1}).get_json()
assert body == {
"data": [
{
"id": second.id,
"source": "hit-testing",
"score": 0.9,
"question": "Asked 1",
"created_at": int(second.created_at.timestamp()),
"match": "Matched 1",
"response": "Responded 1",
}
],
"has_more": True,
"page": 1,
"limit": 1,
"total": 2,
}
last = harness.get("history", query={"page": 2, "limit": 1}).get_json()
assert last["data"][0]["id"] == first.id
assert last["has_more"] is False
@pytest.mark.parametrize("binding", ["disabled", "present", "missing"])
def test_settings_preserve_disabled_and_missing_binding_nulls(harness: _Harness, binding: str) -> None:
if binding == "disabled":
expected = {"enabled": False, "id": None, "score_threshold": None, "embedding_model": None}
else:
collection = DatasetCollectionBinding(
provider_name="openai",
model_name="text-embedding-3-small",
collection_name="annotations",
type="annotation",
)
setting = AppAnnotationSetting(
app_id=harness.target.id,
score_threshold=0.75,
collection_binding_id=collection.id,
created_user_id=harness.account.id,
updated_user_id=harness.account.id,
)
with harness.factory.begin() as session:
session.add(setting)
if binding == "present":
session.add(collection)
expected = {
"enabled": True,
"id": setting.id,
"score_threshold": 0.75,
"embedding_model": {
"embedding_provider_name": "openai" if binding == "present" else None,
"embedding_model_name": "text-embedding-3-small" if binding == "present" else None,
},
}
response = harness.get("setting")
assert response.status_code == 200
assert response.get_json() == expected
@pytest.mark.parametrize("endpoint", _ENDPOINTS)
@pytest.mark.parametrize("role", list(TenantAccountRole))
def test_count_allows_members_while_other_reads_require_edit_role(
harness: _Harness, endpoint: _Endpoint, role: TenantAccountRole
) -> None:
with harness.factory.begin() as session:
session.execute(
update(TenantAccountJoin).where(TenantAccountJoin.account_id == harness.account.id).values(role=role)
)
response = harness.get(endpoint)
if endpoint == "count" or TenantAccountRole.is_editing_role(role):
assert response.status_code == 200
else:
_error(response, status=403, code="forbidden")
@pytest.mark.parametrize("endpoint", _ENDPOINTS)
@pytest.mark.parametrize("scope", ["missing", "other_tenant", "disabled"])
def test_app_scope_errors_preserve_endpoint_contract(harness: _Harness, endpoint: _Endpoint, scope: str) -> None:
app_id = harness.target.id
if scope == "missing":
app_id = str(uuid4())
else:
with harness.factory.begin() as session:
if scope == "other_tenant":
session.execute(update(App).where(App.id == app_id).values(tenant_id=str(uuid4())))
else:
# Current model writes allow only NORMAL; exercise persisted historical state.
session.execute(text("UPDATE apps SET status = 'disabled' WHERE id = :app_id"), {"app_id": app_id})
_error(
harness.get(endpoint, app_id=app_id),
status=404,
code="app_not_found" if endpoint == "count" else "not_found",
message="App not found." if endpoint == "count" else "App not found",
)
@pytest.mark.parametrize("scope", ["missing", "other_app"])
def test_history_rejects_missing_or_other_app_annotation(harness: _Harness, scope: str) -> None:
annotation_id = str(uuid4())
if scope == "other_app":
with harness.factory.begin() as session:
row = _annotation(harness, index=1, app_id=str(uuid4()))
session.add(row)
annotation_id = row.id
_error(
harness.get("history", annotation_id=annotation_id),
status=404,
code="not_found",
message="Annotation not found",
)
@pytest.mark.parametrize("status", [AgentStatus.ACTIVE, AgentStatus.ARCHIVED])
def test_workflow_only_backing_app_is_hidden_only_from_count(harness: _Harness, status: AgentStatus) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=AppMode.AGENT))
session.add(
Agent(
tenant_id=harness.target.tenant_id,
name="Workflow-local agent",
scope=AgentScope.WORKFLOW_ONLY,
source=AgentSource.WORKFLOW,
app_id=str(uuid4()),
backing_app_id=harness.target.id,
workflow_id=str(uuid4()),
workflow_node_id="agent-node",
status=status,
)
)
_error(harness.get("count"), status=404, code="app_not_found", message="App not found.")
for endpoint in ("list", "setting", "history", "export"):
assert harness.get(endpoint).status_code == 200
@pytest.mark.parametrize("endpoint", _ENDPOINTS)
def test_admission_requires_authentication_csrf_and_initialization(harness: _Harness, endpoint: _Endpoint) -> None:
_error(harness.get(endpoint, authenticated=False), status=401, code="unauthorized")
_error(harness.get(endpoint, csrf=False), status=401, code="unauthorized")
with harness.factory.begin() as session:
session.execute(
update(Account).where(Account.id == harness.account.id).values(status=AccountStatus.UNINITIALIZED)
)
_error(harness.get(endpoint), status=400, code="account_not_initialized")
@pytest.mark.parametrize("endpoint", _ENDPOINTS)
def test_empty_owned_app_returns_complete_empty_shape(harness: _Harness, endpoint: _Endpoint) -> None:
if endpoint != "history":
with harness.factory.begin() as session:
session.execute(delete(MessageAnnotation).where(MessageAnnotation.id == harness.annotation.id))
response = harness.get(endpoint)
assert response.status_code == 200
if endpoint == "count":
assert response.get_json() == {"count": 0}
elif endpoint in {"list", "history"}:
assert response.get_json() == {"data": [], "has_more": False, "page": 1, "limit": 20, "total": 0}
elif endpoint == "export":
assert response.get_json() == {"data": []}
else:
assert response.get_json() == {"enabled": False, "id": None, "score_threshold": None, "embedding_model": None}
def test_all_read_routes_keep_app_layout_rbac_declaration() -> None:
for resource in (
annotation_module.MessageAnnotationCountApi,
annotation_module.AnnotationApi,
annotation_module.AppAnnotationSettingDetailApi,
annotation_module.AnnotationHitHistoryListApi,
annotation_module.AnnotationExportApi,
):
[check] = vars(resource.get)[RBAC_CHECKS_ATTR]
assert check.scene == RBACPermission.APP_VIEW_LAYOUT
assert isinstance(check.locator, PlainApp)
@@ -0,0 +1,161 @@
"""Reply admission rejects invalid requests before opening any Redis connection."""
from typing import Literal
from uuid import uuid4
import pytest
from sqlalchemy import text, update
from werkzeug.test import TestResponse
from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN
from controllers.common.rbac import RBAC_CHECKS_ATTR, PlainApp, RBACPermission
from controllers.console.app import annotation as annotation_module
from enums.account import TenantAccountRole
from libs.external_api import ExternalApi
from libs.passport import PassportService
from libs.token import generate_csrf_token
from models import Account
from models.account import AccountStatus, TenantAccountJoin
from tests.e2e_tests.contracts.controllers.console.app import test_annotation_queries
from tests.e2e_tests.contracts.controllers.console.app.test_annotation_queries import _Harness
from tests.helpers.unit.model_factories import make_app
query_harness = test_annotation_queries.harness
_PAYLOAD: dict[str, object] = {
"score_threshold": 0.5,
"embedding_provider_name": "openai",
"embedding_model_name": "text-embedding-3-small",
}
type _Operation = Literal["enable", "disable", "status"]
@pytest.fixture
def harness(query_harness: _Harness) -> _Harness:
api = ExternalApi(query_harness.app)
api.add_resource(
annotation_module.AnnotationReplyActionApi,
"/apps/<uuid:app_id>/annotation-reply/<string:action>",
endpoint="console.annotation_reply",
)
api.add_resource(
annotation_module.AnnotationReplyActionStatusApi,
"/apps/<uuid:app_id>/annotation-reply/<string:action>/status/<uuid:job_id>",
endpoint="console.annotation_reply_status",
)
return query_harness
def _request(
harness: _Harness,
operation: _Operation,
*,
app_id: str | None = None,
action: str | None = None,
payload: dict[str, object] | None = None,
authenticated: bool = True,
csrf: bool = True,
) -> TestResponse:
client = harness.app.test_client()
token = generate_csrf_token(harness.account.id)
headers = {HEADER_NAME_CSRF_TOKEN: token} if csrf else {}
if authenticated:
headers["Authorization"] = f"Bearer {PassportService().issue({'user_id': harness.account.id})}"
client.set_cookie(COOKIE_NAME_CSRF_TOKEN, token)
resolved_action = action or ("enable" if operation == "status" else operation)
path = f"/apps/{app_id or harness.target.id}/annotation-reply/{resolved_action}"
if operation == "status":
response = client.get(f"{path}/status/{uuid4()}", headers=headers)
else:
response = client.post(path, json=_PAYLOAD if payload is None else payload, headers=headers)
assert response.headers["Content-Type"] == "application/json"
assert all(not session.in_transaction() and not session.identity_map for session in harness.sessions)
return response
@pytest.mark.parametrize("operation", ["enable", "disable", "status"])
@pytest.mark.parametrize("reason", ["missing", "other_tenant", "disabled"])
def test_unavailable_app_rejects_before_reading_or_writing_jobs(
harness: _Harness, operation: _Operation, reason: str
) -> None:
app_id = str(uuid4())
with harness.factory.begin() as session:
if reason == "other_tenant":
session.add(make_app(app_id=app_id, tenant_id=str(uuid4())))
elif reason == "disabled":
app_id = harness.target.id
session.execute(text("UPDATE apps SET status = 'disabled' WHERE id = :id"), {"id": app_id})
response = _request(harness, operation, app_id=app_id)
assert response.status_code == 404
assert response.get_json()["code"] == "not_found"
assert response.get_json()["message"] == "App not found"
@pytest.mark.parametrize("operation", ["enable", "disable", "status"])
def test_unknown_action_is_validation_error_without_dispatch(harness: _Harness, operation: _Operation) -> None:
response = _request(harness, operation, action="unknown")
assert response.status_code == 400
assert response.get_json()["code"] == "invalid_param"
@pytest.mark.parametrize("operation", ["enable", "disable"])
@pytest.mark.parametrize("payload", [{}, {"score_threshold": "invalid", "embedding_provider_name": "openai"}])
def test_both_actions_require_valid_embedding_payload(
harness: _Harness, operation: _Operation, payload: dict[str, object]
) -> None:
response = _request(harness, operation, payload=payload)
assert response.status_code == 422
assert response.get_json()["code"] == "unprocessable_entity"
@pytest.mark.parametrize("operation", ["enable", "disable", "status"])
@pytest.mark.parametrize("role", [TenantAccountRole.NORMAL, TenantAccountRole.DATASET_OPERATOR])
def test_read_and_write_actions_preserve_editor_requirement(
harness: _Harness, operation: _Operation, role: TenantAccountRole
) -> None:
with harness.factory.begin() as session:
session.execute(
update(TenantAccountJoin).where(TenantAccountJoin.account_id == harness.account.id).values(role=role)
)
response = _request(harness, operation)
assert response.status_code == 403
assert response.get_json()["code"] == "forbidden"
@pytest.mark.parametrize("operation", ["enable", "disable", "status"])
@pytest.mark.parametrize(("authenticated", "csrf"), [(False, True), (True, False)])
def test_admission_requires_authentication_and_csrf(
harness: _Harness, operation: _Operation, authenticated: bool, csrf: bool
) -> None:
response = _request(harness, operation, authenticated=authenticated, csrf=csrf)
assert response.status_code == 401
assert response.get_json()["code"] == "unauthorized"
@pytest.mark.parametrize("operation", ["enable", "disable", "status"])
def test_uninitialized_account_cannot_read_or_dispatch_jobs(harness: _Harness, operation: _Operation) -> None:
with harness.factory.begin() as session:
session.execute(
update(Account).where(Account.id == harness.account.id).values(status=AccountStatus.UNINITIALIZED)
)
response = _request(harness, operation)
assert response.status_code == 400
assert response.get_json()["code"] == "account_not_initialized"
def test_reply_actions_preserve_distinct_rbac_permissions() -> None:
[write_check] = vars(annotation_module.AnnotationReplyActionApi.post)[RBAC_CHECKS_ATTR]
[read_check] = vars(annotation_module.AnnotationReplyActionStatusApi.get)[RBAC_CHECKS_ATTR]
assert write_check.scene == RBACPermission.APP_EDIT
assert read_check.scene == RBACPermission.APP_VIEW_LAYOUT
assert isinstance(write_check.locator, PlainApp)
assert isinstance(read_check.locator, PlainApp)
@@ -0,0 +1,264 @@
"""Annotation setting updates through real Console admission and SQLite."""
from datetime import datetime
from uuid import uuid4
import pytest
from sqlalchemy import delete, event, text, update
from sqlalchemy.orm import Session
from werkzeug.test import TestResponse
from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN
from controllers.common.rbac import RBAC_CHECKS_ATTR, PlainApp, RBACPermission
from controllers.console.app import annotation as annotation_module
from enums.account import TenantAccountRole
from libs.datetime_utils import naive_utc_now
from libs.external_api import ExternalApi
from libs.passport import PassportService
from libs.token import generate_csrf_token
from models import Account, App
from models.account import AccountStatus, TenantAccountJoin
from models.dataset import DatasetCollectionBinding
from models.model import AppAnnotationSetting
from tests.e2e_tests.contracts.controllers.console.app import test_annotation_queries
from tests.e2e_tests.contracts.controllers.console.app.test_annotation_queries import _Harness
from tests.helpers.unit.model_factories import make_app
query_harness = test_annotation_queries.harness
_CREATED_AT = datetime(2026, 1, 2, 3, 4, 5)
@pytest.fixture
def harness(query_harness: _Harness) -> _Harness:
api = ExternalApi(query_harness.app)
api.add_resource(
annotation_module.AppAnnotationSettingUpdateApi,
"/apps/<uuid:app_id>/annotation-settings/<uuid:annotation_setting_id>",
endpoint="console.annotation_setting_commands",
)
return query_harness
@pytest.fixture
def setting(harness: _Harness) -> AppAnnotationSetting:
binding = DatasetCollectionBinding(
provider_name="openai",
model_name="text-embedding-3-small",
collection_name="annotations",
type="annotation",
)
original_author = str(uuid4())
setting = AppAnnotationSetting(
app_id=harness.target.id,
score_threshold=0.75,
collection_binding_id=binding.id,
created_user_id=original_author,
updated_user_id=original_author,
)
setting.created_at = _CREATED_AT
setting.updated_at = _CREATED_AT
with harness.factory.begin() as session:
session.add_all([binding, setting])
return setting
def _request(
harness: _Harness,
*,
setting_id: str,
payload: dict[str, object],
app_id: str | None = None,
authenticated: bool = True,
csrf: bool = True,
) -> TestResponse:
client = harness.app.test_client()
token = generate_csrf_token(harness.account.id)
headers = {HEADER_NAME_CSRF_TOKEN: token} if csrf else {}
if authenticated:
headers["Authorization"] = f"Bearer {PassportService().issue({'user_id': harness.account.id})}"
client.set_cookie(COOKIE_NAME_CSRF_TOKEN, token)
commits: list[Session] = []
def record_commit(session: Session) -> None:
commits.append(session)
event.listen(Session, "before_commit", record_commit)
try:
response = client.post(
f"/apps/{app_id or harness.target.id}/annotation-settings/{setting_id}",
json=payload,
headers=headers,
)
finally:
event.remove(Session, "before_commit", record_commit)
assert len(commits) == (1 if response.status_code < 400 else 0)
assert all(not session.in_transaction() and not session.identity_map for session in harness.sessions)
assert response.headers["Content-Type"] == "application/json"
return response
def _assert_unchanged(harness: _Harness, setting: AppAnnotationSetting) -> None:
with harness.factory() as session:
stored = session.get(AppAnnotationSetting, setting.id)
assert stored is not None
assert (stored.score_threshold, stored.updated_user_id, stored.updated_at) == (
0.75,
setting.updated_user_id,
_CREATED_AT,
)
@pytest.mark.parametrize("threshold", [0.0, 0.9, -0.5, 1.5, "0.8"])
def test_update_preserves_threshold_validation_response_and_audit_fields(
harness: _Harness, setting: AppAnnotationSetting, threshold: float | str
) -> None:
before = naive_utc_now().replace(microsecond=0)
response = _request(harness, setting_id=setting.id, payload={"score_threshold": threshold})
assert response.status_code == 200
assert response.get_json() == {
"enabled": True,
"id": setting.id,
"score_threshold": float(threshold),
"embedding_model": {
"embedding_provider_name": "openai",
"embedding_model_name": "text-embedding-3-small",
},
}
with harness.factory() as session:
stored = session.get(AppAnnotationSetting, setting.id)
assert stored is not None
assert stored.score_threshold == float(threshold)
assert stored.updated_user_id == harness.account.id
assert before <= stored.updated_at <= naive_utc_now()
assert (stored.app_id, stored.collection_binding_id, stored.created_user_id, stored.created_at) == (
harness.target.id,
setting.collection_binding_id,
setting.created_user_id,
_CREATED_AT,
)
def test_missing_binding_preserves_enabled_setting_and_null_model_names(
harness: _Harness, setting: AppAnnotationSetting
) -> None:
with harness.factory.begin() as session:
session.execute(
delete(DatasetCollectionBinding).where(DatasetCollectionBinding.id == setting.collection_binding_id)
)
response = _request(harness, setting_id=setting.id, payload={"score_threshold": 0.0})
assert response.status_code == 200
assert response.get_json() == {
"enabled": True,
"id": setting.id,
"score_threshold": 0.0,
"embedding_model": {"embedding_provider_name": None, "embedding_model_name": None},
}
@pytest.mark.parametrize("payload", [{}, {"score_threshold": None}, {"score_threshold": "invalid"}])
def test_invalid_payload_does_not_write(
harness: _Harness, setting: AppAnnotationSetting, payload: dict[str, object]
) -> None:
response = _request(harness, setting_id=setting.id, payload=payload)
assert response.status_code == 422
assert response.get_json()["code"] == "unprocessable_entity"
_assert_unchanged(harness, setting)
@pytest.mark.parametrize("scope", ["missing", "other_tenant", "disabled"])
def test_update_requires_available_app_in_admitted_tenant(
harness: _Harness, setting: AppAnnotationSetting, scope: str
) -> None:
app_id = harness.target.id
if scope == "missing":
app_id = str(uuid4())
else:
with harness.factory.begin() as session:
if scope == "other_tenant":
session.execute(update(App).where(App.id == app_id).values(tenant_id=str(uuid4())))
else:
session.execute(text("UPDATE apps SET status = 'disabled' WHERE id = :app_id"), {"app_id": app_id})
response = _request(harness, setting_id=setting.id, app_id=app_id, payload={"score_threshold": 0.9})
assert response.status_code == 404
assert response.get_json() == {"code": "not_found", "message": "App not found", "status": 404}
_assert_unchanged(harness, setting)
@pytest.mark.parametrize("scope", ["missing", "other_app", "other_tenant"])
def test_update_requires_setting_in_admitted_app(harness: _Harness, setting: AppAnnotationSetting, scope: str) -> None:
setting_id = str(uuid4())
if scope != "missing":
other = make_app(
app_id=str(uuid4()),
tenant_id=harness.target.tenant_id if scope == "other_app" else str(uuid4()),
)
with harness.factory.begin() as session:
session.add(other)
session.execute(
update(AppAnnotationSetting)
.where(AppAnnotationSetting.id == setting.id)
.values(app_id=other.id, updated_at=_CREATED_AT)
)
setting_id = setting.id
response = _request(harness, setting_id=setting_id, payload={"score_threshold": 0.9})
assert response.status_code == 404
assert response.get_json() == {"code": "not_found", "message": "App annotation not found", "status": 404}
_assert_unchanged(harness, setting)
@pytest.mark.parametrize("role", list(TenantAccountRole))
def test_setting_update_requires_editing_role(
harness: _Harness, setting: AppAnnotationSetting, role: TenantAccountRole
) -> None:
with harness.factory.begin() as session:
session.execute(
update(TenantAccountJoin).where(TenantAccountJoin.account_id == harness.account.id).values(role=role)
)
response = _request(harness, setting_id=setting.id, payload={"score_threshold": 0.9})
if TenantAccountRole.is_editing_role(role):
assert response.status_code == 200
assert response.get_json()["score_threshold"] == 0.9
else:
assert response.status_code == 403
assert response.get_json()["code"] == "forbidden"
_assert_unchanged(harness, setting)
def test_setting_update_requires_authentication_csrf_and_initialization(
harness: _Harness, setting: AppAnnotationSetting
) -> None:
for authenticated, csrf in ((False, True), (True, False)):
response = _request(
harness,
setting_id=setting.id,
payload={"score_threshold": 0.9},
authenticated=authenticated,
csrf=csrf,
)
assert response.status_code == 401
assert response.get_json()["code"] == "unauthorized"
with harness.factory.begin() as session:
session.execute(
update(Account).where(Account.id == harness.account.id).values(status=AccountStatus.UNINITIALIZED)
)
response = _request(harness, setting_id=setting.id, payload={"score_threshold": 0.9})
assert response.status_code == 400
assert response.get_json()["code"] == "account_not_initialized"
_assert_unchanged(harness, setting)
def test_setting_update_preserves_app_edit_rbac_declaration() -> None:
[check] = vars(annotation_module.AppAnnotationSettingUpdateApi.post)[RBAC_CHECKS_ATTR]
assert check.scene == RBACPermission.APP_EDIT
assert isinstance(check.locator, PlainApp)
@@ -0,0 +1,337 @@
"""Console conversation deletion through real account admission and SQLite."""
from collections.abc import Callable, Iterator
from dataclasses import dataclass, field
from http import HTTPStatus
from typing import Literal
from uuid import uuid4
import pytest
from flask import Flask
from sqlalchemy import Connection, Engine, event, select, update
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN
from controllers.common.rbac import RBAC_CHECKS_ATTR, PlainApp, RBACCheck, RBACPermission
from controllers.console.app.conversation import ChatConversationDetailApi, CompletionConversationDetailApi
from enums import DeploymentEdition
from enums.account import TenantAccountRole
from extensions.ext_database import db
from extensions.ext_login import DifyLoginManager, unauthorized_handler
from libs.external_api import ExternalApi
from libs.token import generate_csrf_token
from models import Account, Tenant, TenantAccountJoin
from models.agent import AgentWorkingResourceStatus, AgentWorkspace, AgentWorkspaceOwnerType
from models.enums import ConversationFromSource
from models.model import App, AppMode, Conversation
from repositories.conversation_repository import ConversationRepository
from services.conversation_command_service import ConversationCommandService
from tests.helpers.unit.model_factories import make_account, make_app, make_conversation
type _Kind = Literal["chat", "completion"]
@dataclass(frozen=True)
class _Services:
conversation_commands: ConversationCommandService
@dataclass
class _Harness:
app: Flask
factory: sessionmaker[Session]
account: Account
tenant: Tenant
target: App
conversation: Conversation
sessions: list[Session] = field(default_factory=list)
cleanups: list[tuple[str, str, tuple[str, ...]]] = field(default_factory=list)
def delete(
self,
kind: _Kind,
*,
app_id: str | None = None,
conversation_id: str | None = None,
authenticated: bool = True,
csrf: bool = True,
) -> TestResponse:
client = self.app.test_client()
headers: dict[str, str] = {}
if authenticated:
with client.session_transaction() as session:
session["_user_id"] = self.account.id
session["_fresh"] = True
if csrf:
token = generate_csrf_token(self.account.id)
client.set_cookie(COOKIE_NAME_CSRF_TOKEN, token)
headers[HEADER_NAME_CSRF_TOKEN] = token
response = client.delete(
f"/apps/{app_id or self.target.id}/{kind}-conversations/{conversation_id or self.conversation.id}",
headers=headers,
)
self.assert_closed()
assert response.content_type == "application/json"
return response
def assert_closed(self) -> None:
assert all(not session.in_transaction() and not session.identity_map for session in self.sessions)
def mode(self, mode: AppMode) -> None:
with self.factory.begin() as session:
session.execute(update(App).where(App.id == self.target.id).values(mode=mode))
self.target.mode = mode
def assert_unchanged(self, *, previously_deleted: bool = False) -> None:
with self.factory() as session:
stored = session.get(Conversation, self.conversation.id)
assert stored is not None
assert stored.is_deleted is previously_deleted
assert stored.read_at is None
assert stored.read_account_id is None
assert not self.cleanups
@pytest.fixture
def harness(
sqlite_engine: Engine, sqlite_session_factory: sessionmaker[Session], config_overrides: Callable[..., None]
) -> Iterator[_Harness]:
config_overrides(
DEPLOYMENT_EDITION=DeploymentEdition.CLOUD,
LOGIN_DISABLED=False,
RBAC_ENABLED=False,
SECRET_KEY="conversation-http-tests-key-at-least-32-bytes",
CONSOLE_WEB_URL="http://localhost",
CONSOLE_API_URL="http://localhost",
COOKIE_DOMAIN="",
)
tenant = Tenant(name="Console workspace")
account = make_account(account_id=str(uuid4()), timezone="UTC")
target = make_app(app_id=str(uuid4()), tenant_id=tenant.id)
conversation = make_conversation(
conversation_id=str(uuid4()),
app_id=target.id,
inputs={},
from_source=ConversationFromSource.CONSOLE,
from_account_id=account.id,
is_deleted=False,
)
with sqlite_session_factory.begin() as session:
session.add_all(
[
tenant,
account,
target,
conversation,
TenantAccountJoin(tenant_id=tenant.id, account_id=account.id, role=TenantAccountRole.OWNER),
]
)
app = Flask(__name__)
app.config.update(
TESTING=True,
RESTX_ERROR_404_HELP=False,
SECRET_KEY="console-test-session",
SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url),
)
db.init_app(app)
login_manager = DifyLoginManager()
login_manager.init_app(app)
login_manager.unauthorized_handler(unauthorized_handler)
state = _Harness(app, sqlite_session_factory, account, tenant, target, conversation)
@login_manager.user_loader
def load_account(account_id: str) -> Account | None:
with sqlite_session_factory() as session:
loaded = session.get(Account, account_id)
if loaded is not None:
loaded.set_tenant_id_with_session(tenant.id, session=session)
return loaded
factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
@event.listens_for(factory, "after_begin")
def record_session(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
state.assert_closed()
state.sessions.append(session)
def enqueue_cleanup(*, tenant_id: str, conversation_id: str, retired_workspace_ids: tuple[str, ...]) -> None:
# Cleanup crosses an external queue boundary only after the real command
# transaction has closed and its committed deletion is visible.
state.assert_closed()
with sqlite_session_factory() as session:
stored = session.get(Conversation, conversation_id)
assert stored is not None
assert stored.is_deleted
for workspace_id in retired_workspace_ids:
workspace = session.get(AgentWorkspace, workspace_id)
assert workspace is not None
assert workspace.status == AgentWorkingResourceStatus.RETIRED
assert workspace.active_guard is None
assert workspace.retired_at is not None
state.cleanups.append((tenant_id, conversation_id, retired_workspace_ids))
app.extensions["application_services"] = _Services(
ConversationCommandService(
conversations=ConversationRepository(session_factory=factory), enqueue_delete_cleanup=enqueue_cleanup
)
)
api = ExternalApi(app)
api.add_resource(ChatConversationDetailApi, "/apps/<uuid:app_id>/chat-conversations/<uuid:conversation_id>")
api.add_resource(
CompletionConversationDetailApi, "/apps/<uuid:app_id>/completion-conversations/<uuid:conversation_id>"
)
yield state
with app.app_context():
db.session.remove()
db.engine.dispose()
@pytest.mark.parametrize(
("kind", "mode"),
[
("completion", AppMode.COMPLETION),
("chat", AppMode.CHAT),
("chat", AppMode.AGENT_CHAT),
("chat", AppMode.ADVANCED_CHAT),
("chat", AppMode.AGENT),
],
)
def test_delete_commits_retirement_before_cleanup_and_returns_empty_204(
harness: _Harness, kind: _Kind, mode: AppMode
) -> None:
harness.mode(mode)
with harness.factory.begin() as session:
workspace = AgentWorkspace(
id=str(uuid4()),
tenant_id=harness.target.tenant_id,
app_id=harness.target.id,
owner_type=AgentWorkspaceOwnerType.CONVERSATION,
owner_id=harness.conversation.id,
owner_scope_key="chatflow-node",
backend_workspace_ref="backend-workspace",
status=AgentWorkingResourceStatus.ACTIVE,
active_guard=1,
)
session.add(workspace)
response = harness.delete(kind)
assert response.status_code == HTTPStatus.NO_CONTENT
assert response.data == b""
assert "Content-Length" not in response.headers
assert harness.cleanups == [(harness.tenant.id, harness.conversation.id, (workspace.id,))]
assert harness.sessions
with harness.factory() as session:
stored = session.get(Conversation, harness.conversation.id)
assert stored is not None
assert stored.read_at is None
assert stored.read_account_id is None
repeated = harness.delete(kind)
assert repeated.status_code == HTTPStatus.NOT_FOUND
assert repeated.get_json()["code"] == "conversation_not_found"
assert len(harness.cleanups) == 1
@pytest.mark.parametrize("kind", ["chat", "completion"])
@pytest.mark.parametrize("field", ["app_id", "from_source", "from_account_id", "from_end_user_id", "is_deleted"])
def test_delete_cannot_target_another_conversation_owner(harness: _Harness, kind: _Kind, field: str) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
value: str | bool
if field == "is_deleted":
value = True
elif field == "from_source":
value = ConversationFromSource.API
elif field == "from_end_user_id":
# Account and EndUser IDs remain different owner namespaces even if equal.
value = harness.account.id
else:
value = str(uuid4())
with harness.factory.begin() as session:
session.execute(update(Conversation).where(Conversation.id == harness.conversation.id).values({field: value}))
response = harness.delete(kind)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "conversation_not_found"
harness.assert_unchanged(previously_deleted=field == "is_deleted")
@pytest.mark.parametrize("kind", ["chat", "completion"])
def test_missing_conversation_does_not_delete_existing_conversation(harness: _Harness, kind: _Kind) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
response = harness.delete(kind, conversation_id=str(uuid4()))
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "conversation_not_found"
harness.assert_unchanged()
@pytest.mark.parametrize("kind", ["chat", "completion"])
@pytest.mark.parametrize("unavailable", ["missing", "other_tenant"])
def test_app_scope_is_checked_before_delete(harness: _Harness, kind: _Kind, unavailable: str) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
with harness.factory.begin() as session:
target = session.get(App, harness.target.id)
assert target is not None
if unavailable == "missing":
session.delete(target)
else:
target.tenant_id = str(uuid4())
response = harness.delete(kind)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "app_not_found"
harness.assert_unchanged()
@pytest.mark.parametrize(
("kind", "mode"),
[("chat", AppMode.COMPLETION), ("chat", AppMode.WORKFLOW), ("completion", AppMode.CHAT)],
)
def test_unsupported_app_mode_cannot_delete_conversation(harness: _Harness, kind: _Kind, mode: AppMode) -> None:
harness.mode(mode)
response = harness.delete(kind)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "app_not_found"
harness.assert_unchanged()
@pytest.mark.parametrize("kind", ["chat", "completion"])
@pytest.mark.parametrize(
"role",
[
TenantAccountRole.OWNER,
TenantAccountRole.ADMIN,
TenantAccountRole.EDITOR,
TenantAccountRole.NORMAL,
TenantAccountRole.DATASET_OPERATOR,
],
)
def test_existing_edit_roles_are_preserved(harness: _Harness, kind: _Kind, role: TenantAccountRole) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
with harness.factory.begin() as session:
membership = session.scalar(select(TenantAccountJoin).where(TenantAccountJoin.account_id == harness.account.id))
assert membership is not None
membership.role = role
response = harness.delete(kind)
if TenantAccountRole.is_editing_role(role):
assert response.status_code == HTTPStatus.NO_CONTENT
assert len(harness.cleanups) == 1
else:
assert response.status_code == HTTPStatus.FORBIDDEN
harness.assert_unchanged()
@pytest.mark.parametrize("kind", ["chat", "completion"])
def test_login_and_csrf_remain_required(harness: _Harness, kind: _Kind) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
assert harness.delete(kind, authenticated=False).status_code == HTTPStatus.UNAUTHORIZED
assert harness.delete(kind, csrf=False).status_code == HTTPStatus.UNAUTHORIZED
harness.assert_unchanged()
@pytest.mark.parametrize("handler", [ChatConversationDetailApi.delete, CompletionConversationDetailApi.delete])
def test_app_edit_rbac_check_is_declared(handler: Callable[..., object]) -> None:
checks: tuple[RBACCheck, ...] = vars(handler)[RBAC_CHECKS_ATTR]
assert len(checks) == 1
assert checks[0].scene == RBACPermission.APP_EDIT
assert isinstance(checks[0].locator, PlainApp)
assert checks[0].locator.param == "app_id"
@@ -0,0 +1,477 @@
"""Console detail reads use real admission, queries, rendering and read receipts."""
import json
from collections.abc import Callable
from dataclasses import dataclass
from datetime import datetime, timedelta
from decimal import Decimal
from http import HTTPStatus
from typing import Literal, cast
from uuid import uuid4
import pytest
from sqlalchemy import Connection, event, select, text, update
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN
from controllers.common.rbac import RBAC_CHECKS_ATTR, PlainApp, RBACCheck, RBACPermission
from controllers.console.app.conversation import ChatConversationDetailApi, CompletionConversationDetailApi
from enums.account import TenantAccountRole
from fields.conversation_fields import ConversationDetail, ConversationMessageDetail, ConversationResponseSource
from libs.datetime_utils import naive_utc_now
from libs.helper import dump_response, to_timestamp
from libs.token import generate_csrf_token
from models import TenantAccountJoin
from models.agent import Agent, AgentScope, AgentSource, AgentStatus
from models.enums import ConversationFromSource, CreatorUserRole, FeedbackFromSource, FeedbackRating
from models.model import (
App,
AppAnnotationHitHistory,
AppMode,
Conversation,
Message,
MessageAgentThought,
MessageAnnotation,
MessageFeedback,
)
from models.utils.file_input_compat import restore_input_files
from repositories.conversation_repository import ConversationRepository
from services.conversation_command_service import ConversationCommandService
from services.conversation_query_service import ConversationQueryService
from services.message_query_adapters import MessageFileResolver
from tests.e2e_tests.contracts.controllers.console.app import test_conversation_delete as delete_tests
from tests.e2e_tests.contracts.controllers.console.app.test_conversation_delete import _Harness as DeleteHarness
from tests.helpers.unit.model_factories import make_account, make_message
type _Kind = Literal["chat", "completion"]
_UPDATED_AT = datetime(2024, 2, 3, 4, 5, 6)
delete_harness = delete_tests.harness
@dataclass(frozen=True)
class _Services:
conversation_commands: ConversationCommandService
conversation_queries: ConversationQueryService
@dataclass(frozen=True)
class _Harness:
source: DeleteHarness
def get(
self,
kind: _Kind,
*,
account_id: str | None = None,
conversation_id: str | None = None,
authenticated: bool = True,
csrf: bool = True,
) -> TestResponse:
source = self.source
client = source.app.test_client()
actor = account_id or source.account.id
headers: dict[str, str] = {}
if authenticated:
with client.session_transaction() as session:
session["_user_id"] = actor
session["_fresh"] = True
if csrf:
token = generate_csrf_token(actor)
client.set_cookie(COOKIE_NAME_CSRF_TOKEN, token)
headers[HEADER_NAME_CSRF_TOKEN] = token
response = client.get(
f"/apps/{source.target.id}/{kind}-conversations/{conversation_id or source.conversation.id}",
headers=headers,
)
source.assert_closed()
assert response.content_type == "application/json"
assert int(response.headers["Content-Length"]) == len(response.data)
assert not source.cleanups
return response
def assert_unread(self) -> None:
with self.source.factory() as session:
conversation = session.get_one(Conversation, self.source.conversation.id)
assert conversation.read_at is None
assert conversation.read_account_id is None
assert conversation.updated_at == _UPDATED_AT
@pytest.fixture
def harness(delete_harness: DeleteHarness) -> _Harness:
previous = cast(delete_tests._Services, delete_harness.app.extensions["application_services"])
factory = sessionmaker(bind=delete_harness.factory.kw["bind"], expire_on_commit=False)
@event.listens_for(factory, "after_begin")
def record_session(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
delete_harness.assert_closed()
delete_harness.sessions.append(session)
delete_harness.app.extensions["application_services"] = _Services(
conversation_commands=previous.conversation_commands,
conversation_queries=ConversationQueryService(
conversations=ConversationRepository(session_factory=factory),
restore_inputs=restore_input_files,
files=MessageFileResolver(),
),
)
with delete_harness.factory.begin() as session:
session.execute(
update(Conversation)
.where(Conversation.id == delete_harness.conversation.id)
.values({Conversation.updated_at: _UPDATED_AT, Conversation.introduction: "Welcome"})
)
return _Harness(delete_harness)
def _message(harness: _Harness, *, created_at: datetime = _UPDATED_AT) -> Message:
source = harness.source
message = make_message(
message_id=str(uuid4()),
app_id=source.target.id,
conversation_id=source.conversation.id,
inputs={"empty": "", "zero": 0, "false": False, "null": None, "list": []},
query="The first question",
message=[{"role": "user", "text": "The prompt"}],
answer="The answer",
message_tokens=7,
answer_tokens=13,
provider_response_latency=1.25,
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
total_price=Decimal(0),
currency="USD",
from_source=ConversationFromSource.API,
from_account_id=source.account.id,
created_at=created_at,
updated_at=created_at,
)
message.message_metadata = '{"usage":{"total_tokens":20}}'
with source.factory.begin() as session:
session.add(message)
return message
@pytest.mark.parametrize(
("kind", "mode"),
[
("completion", AppMode.COMPLETION),
("chat", AppMode.CHAT),
("chat", AppMode.AGENT_CHAT),
("chat", AppMode.ADVANCED_CHAT),
("chat", AppMode.AGENT),
],
)
def test_empty_details_keep_response_and_mark_first_read_without_touching_updated_at(
harness: _Harness, kind: _Kind, mode: AppMode
) -> None:
source = harness.source
source.mode(mode)
with source.factory() as session:
stored = session.get_one(Conversation, source.conversation.id)
schema = ConversationMessageDetail if kind == "completion" else ConversationDetail
expected = dump_response(schema, ConversationResponseSource(stored, session=session))
before = naive_utc_now()
response = harness.get(kind)
after = naive_utc_now()
assert response.status_code == HTTPStatus.OK, response.get_json()
assert response.get_json() == expected
assert "read_at" not in response.get_json()
if kind == "completion":
assert response.get_json()["message"] is None
else:
assert response.get_json()["message_count"] == 0
assert response.get_json()["updated_at"] == to_timestamp(_UPDATED_AT)
with source.factory() as session:
stored = session.get_one(Conversation, source.conversation.id)
assert stored.read_at is not None
assert before <= stored.read_at <= after
assert stored.read_account_id == source.account.id
assert stored.updated_at == _UPDATED_AT
@pytest.mark.parametrize("kind", ["chat", "completion"])
@pytest.mark.parametrize("scope", ["other_account", "end_user", "soft_deleted"])
def test_management_detail_preserves_app_wide_visibility(harness: _Harness, kind: _Kind, scope: str) -> None:
source = harness.source
source.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
with source.factory.begin() as session:
conversation = session.get_one(Conversation, source.conversation.id)
if scope == "other_account":
conversation.from_account_id = str(uuid4())
elif scope == "end_user":
conversation.from_source = ConversationFromSource.API
conversation.from_account_id = None
conversation.from_end_user_id = str(uuid4())
else:
conversation.is_deleted = True
conversation.updated_at = _UPDATED_AT
response = harness.get(kind)
assert response.status_code == HTTPStatus.OK, response.get_json()
assert response.get_json()["id"] == source.conversation.id
with source.factory() as session:
stored = session.get_one(Conversation, source.conversation.id)
assert stored.is_deleted is (scope == "soft_deleted")
assert stored.read_account_id == source.account.id
@pytest.mark.parametrize("kind", ["chat", "completion"])
def test_second_reader_does_not_replace_first_read_receipt(harness: _Harness, kind: _Kind) -> None:
source = harness.source
source.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
assert harness.get(kind).status_code == HTTPStatus.OK
with source.factory.begin() as session:
stored = session.get_one(Conversation, source.conversation.id)
first_read_at = stored.read_at
second = make_account(account_id=str(uuid4()), name="Second reader")
session.add_all(
[
second,
TenantAccountJoin(tenant_id=source.tenant.id, account_id=second.id, role=TenantAccountRole.EDITOR),
]
)
assert harness.get(kind, account_id=second.id).status_code == HTTPStatus.OK
with source.factory() as session:
stored = session.get_one(Conversation, source.conversation.id)
assert stored.read_at == first_read_at
assert stored.read_account_id == source.account.id
assert stored.updated_at == _UPDATED_AT
def test_completion_detail_preserves_nested_message_projection(harness: _Harness) -> None:
source = harness.source
source.mode(AppMode.COMPLETION)
first = _message(harness)
_message(harness, created_at=_UPDATED_AT + timedelta(seconds=1))
with source.factory.begin() as session:
stored = session.get_one(Conversation, source.conversation.id)
stored.override_model_configs = json.dumps({"model": {"name": "fixture-model"}, "pre_prompt": "Be useful"})
session.get_one(Message, first.id).message_metadata = json.dumps(
{"usage": {"total_tokens": 20}, "retriever_resources": []}
)
stored.updated_at = _UPDATED_AT
annotation = MessageAnnotation(
app_id=source.target.id,
conversation_id=source.conversation.id,
message_id=first.id,
account_id=source.account.id,
question="Question",
content="Annotated answer",
)
session.add_all(
[
annotation,
MessageFeedback(
app_id=source.target.id,
conversation_id=source.conversation.id,
message_id=first.id,
rating=FeedbackRating.LIKE,
from_source=FeedbackFromSource.USER,
from_account_id=source.account.id,
content="Helpful",
),
AppAnnotationHitHistory(
app_id=source.target.id,
annotation_id=annotation.id,
source="annotation",
question="Question",
account_id=source.account.id,
score=0.8,
message_id=first.id,
annotation_question="Question",
annotation_content="Annotated answer",
),
MessageAgentThought(
message_id=first.id,
position=2,
created_by_role=CreatorUserRole.ACCOUNT,
created_by=source.account.id,
thought="Second thought",
tool_labels_str="{}",
),
MessageAgentThought(
message_id=first.id,
position=1,
created_by_role=CreatorUserRole.ACCOUNT,
created_by=source.account.id,
thought="First thought",
tool="search",
tool_labels_str='{"search":"Search"}',
message_files='["file-1"]',
message_chain_id=str(uuid4()),
),
]
)
with source.app.app_context(), source.factory() as session:
stored = session.get_one(Conversation, source.conversation.id)
expected = dump_response(ConversationMessageDetail, ConversationResponseSource(stored, session=session))
response = harness.get("completion")
assert response.status_code == HTTPStatus.OK, response.get_json()
actual = response.get_json()
assert actual == expected
message = actual["message"]
assert message["id"] == first.id
assert message["inputs"] == {"empty": "", "zero": 0, "false": False, "null": None, "list": []}
assert message["metadata"] == {"usage": {"total_tokens": 20}, "retriever_resources": []}
assert message["annotation_hit_history"]["annotation_id"] == annotation.id
assert message["feedbacks"][0]["from_account"]["id"] == source.account.id
assert [thought["thought"] for thought in message["agent_thoughts"]] == ["First thought", "Second thought"]
assert message["agent_thoughts"][0]["chain_id"] is not None
assert message["message_files"] == []
def test_chat_detail_preserves_annotations_and_feedback_counts(harness: _Harness) -> None:
source = harness.source
first = _message(harness)
_message(harness, created_at=_UPDATED_AT + timedelta(seconds=1))
with source.factory.begin() as session:
session.add_all(
[
MessageAnnotation(
app_id=source.target.id,
conversation_id=source.conversation.id,
message_id=first.id,
account_id=source.account.id,
question="Question",
content="Annotated answer",
),
*[
MessageFeedback(
app_id=source.target.id,
conversation_id=source.conversation.id,
message_id=first.id,
rating=rating,
from_source=feedback_source,
)
for feedback_source, rating in [
(FeedbackFromSource.USER, FeedbackRating.LIKE),
(FeedbackFromSource.USER, FeedbackRating.DISLIKE),
(FeedbackFromSource.ADMIN, FeedbackRating.LIKE),
]
],
]
)
with source.factory() as session:
stored = session.get_one(Conversation, source.conversation.id)
expected = dump_response(ConversationDetail, ConversationResponseSource(stored, session=session))
response = harness.get("chat")
assert response.status_code == HTTPStatus.OK
assert response.get_json() == expected
assert response.get_json()["annotated"] is True
assert response.get_json()["message_count"] == 2
assert response.get_json()["user_feedback_stats"] == {"like": 1, "dislike": 1}
assert response.get_json()["admin_feedback_stats"] == {"like": 1, "dislike": 0}
@pytest.mark.parametrize("kind", ["chat", "completion"])
@pytest.mark.parametrize("gate", ["missing", "tenant", "status", "hidden_agent", "mode"])
def test_unavailable_app_cannot_be_read_or_marked(harness: _Harness, kind: _Kind, gate: str) -> None:
source = harness.source
source.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
with source.factory.begin() as session:
app = session.get_one(App, source.target.id)
if gate == "missing":
session.delete(app)
elif gate == "tenant":
app.tenant_id = str(uuid4())
elif gate == "status":
session.execute(text("UPDATE apps SET status = 'disabled' WHERE id = :id"), {"id": app.id})
elif gate == "mode":
app.mode = AppMode.WORKFLOW
else:
app.mode = AppMode.AGENT
session.add(
Agent(
tenant_id=app.tenant_id,
name="Hidden",
scope=AgentScope.WORKFLOW_ONLY,
source=AgentSource.WORKFLOW,
backing_app_id=app.id,
status=AgentStatus.ARCHIVED,
)
)
response = harness.get(kind)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "app_not_found"
harness.assert_unread()
@pytest.mark.parametrize("kind", ["chat", "completion"])
@pytest.mark.parametrize("change", ["missing", "other_app"])
def test_missing_or_other_app_conversation_does_not_mark_existing_row(
harness: _Harness, kind: _Kind, change: str
) -> None:
source = harness.source
source.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
conversation_id = str(uuid4()) if change == "missing" else source.conversation.id
if change == "other_app":
with source.factory.begin() as session:
session.execute(
update(Conversation)
.where(Conversation.id == conversation_id)
.values({Conversation.app_id: str(uuid4()), Conversation.updated_at: _UPDATED_AT})
)
response = harness.get(kind, conversation_id=conversation_id)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "conversation_not_found"
harness.assert_unread()
@pytest.mark.parametrize("kind", ["chat", "completion"])
@pytest.mark.parametrize("role", list(TenantAccountRole))
def test_detail_preserves_edit_roles(harness: _Harness, kind: _Kind, role: TenantAccountRole) -> None:
source = harness.source
source.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
with source.factory.begin() as session:
membership = session.scalar(select(TenantAccountJoin).where(TenantAccountJoin.account_id == source.account.id))
assert membership is not None
membership.role = role
response = harness.get(kind)
if TenantAccountRole.is_editing_role(role):
assert response.status_code == HTTPStatus.OK
else:
assert response.status_code == HTTPStatus.FORBIDDEN
harness.assert_unread()
@pytest.mark.parametrize("kind", ["chat", "completion"])
def test_detail_requires_login_and_csrf(harness: _Harness, kind: _Kind) -> None:
assert harness.get(kind, authenticated=False).status_code == HTTPStatus.UNAUTHORIZED
assert harness.get(kind, csrf=False).status_code == HTTPStatus.UNAUTHORIZED
harness.assert_unread()
@pytest.mark.parametrize("handler", [ChatConversationDetailApi.get, CompletionConversationDetailApi.get])
def test_detail_declares_app_view_layout(handler: Callable[..., object]) -> None:
checks: tuple[RBACCheck, ...] = vars(handler)[RBAC_CHECKS_ATTR]
assert len(checks) == 1
assert checks[0].scene == RBACPermission.APP_VIEW_LAYOUT
assert isinstance(checks[0].locator, PlainApp)
assert checks[0].locator.param == "app_id"
@pytest.mark.parametrize("failure", ["metadata", "thought_files"])
def test_failed_message_rendering_does_not_mark_conversation_read(harness: _Harness, failure: str) -> None:
source = harness.source
source.mode(AppMode.COMPLETION)
message = _message(harness)
with source.factory.begin() as session:
if failure == "metadata":
stored = session.get_one(Message, message.id)
stored.message_metadata = "invalid-json"
else:
session.add(
MessageAgentThought(
message_id=message.id,
position=1,
created_by_role=CreatorUserRole.ACCOUNT,
created_by=source.account.id,
message_files='[{"not":"a file id"}]',
tool_labels_str="{}",
)
)
response = harness.get("completion")
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "invalid_param"
harness.assert_unread()
@@ -0,0 +1,457 @@
"""Console conversation lists through real account admission, services and SQLite."""
from collections.abc import Callable, Iterator
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from decimal import Decimal
from typing import Literal
from uuid import uuid4
import pytest
from flask import Flask
from sqlalchemy import Connection, Engine, event, select
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN
from controllers.common.rbac import RBAC_CHECKS_ATTR, PlainApp, RBACCheck, RBACPermission
from controllers.console.app.conversation import ChatConversationApi, CompletionConversationApi
from enums import DeploymentEdition
from enums.account import TenantAccountRole
from extensions.ext_database import db
from extensions.ext_login import DifyLoginManager, unauthorized_handler
from fields.conversation_fields import (
ConversationPagination,
ConversationResponseSource,
ConversationWithSummaryPagination,
)
from libs.external_api import ExternalApi
from libs.helper import dump_response
from libs.token import generate_csrf_token
from models import Account, Tenant, TenantAccountJoin
from models.enums import ConversationFromSource, FeedbackFromSource, FeedbackRating
from models.model import App, AppMode, Conversation, Message, MessageAnnotation, MessageFeedback
from models.utils.file_input_compat import restore_input_files
from repositories.conversation_repository import ConversationRepository
from services.conversation_query_service import ConversationQueryService
from services.message_query_adapters import MessageFileResolver
from tests.helpers.unit.model_factories import make_account, make_app, make_conversation, make_message
type _Kind = Literal["chat", "completion"]
_CREATED_AT = datetime(2024, 1, 15, 6, 30, 0)
@dataclass(frozen=True)
class _Services:
conversation_queries: ConversationQueryService
@dataclass
class _Harness:
app: Flask
factory: sessionmaker[Session]
account: Account
tenant: Tenant
target: App
sessions: list[Session] = field(default_factory=list)
after_account_load: Callable[[], None] | None = None
def get(
self,
kind: _Kind,
*,
query: dict[str, str | int] | None = None,
app_id: str | None = None,
authenticated: bool = True,
csrf: bool = True,
) -> TestResponse:
client = self.app.test_client()
headers: dict[str, str] = {}
if authenticated:
with client.session_transaction() as session:
session["_user_id"] = self.account.id
session["_fresh"] = True
if csrf:
token = generate_csrf_token(self.account.id)
client.set_cookie(COOKIE_NAME_CSRF_TOKEN, token)
headers[HEADER_NAME_CSRF_TOKEN] = token
response = client.get(
f"/apps/{app_id or self.target.id}/{kind}-conversations", query_string=query, headers=headers
)
assert all(not session.in_transaction() and not session.identity_map for session in self.sessions)
assert response.content_type == "application/json"
assert int(response.headers["Content-Length"]) == len(response.data)
return response
def mode(self, mode: AppMode) -> None:
with self.factory.begin() as session:
target = session.get(App, self.target.id)
assert target is not None
target.mode = mode
self.target.mode = mode
@pytest.fixture
def harness(
sqlite_engine: Engine, sqlite_session_factory: sessionmaker[Session], config_overrides: Callable[..., None]
) -> Iterator[_Harness]:
config_overrides(
DEPLOYMENT_EDITION=DeploymentEdition.CLOUD,
LOGIN_DISABLED=False,
RBAC_ENABLED=False,
SECRET_KEY="conversation-http-tests-key-at-least-32-bytes",
CONSOLE_WEB_URL="http://localhost",
CONSOLE_API_URL="http://localhost",
COOKIE_DOMAIN="",
)
tenant = Tenant(name="Console workspace")
account = make_account(account_id=str(uuid4()), timezone="Asia/Shanghai")
target = make_app(app_id=str(uuid4()), tenant_id=tenant.id)
with sqlite_session_factory.begin() as session:
session.add_all(
[
tenant,
account,
target,
TenantAccountJoin(tenant_id=tenant.id, account_id=account.id, role=TenantAccountRole.OWNER),
]
)
app = Flask(__name__)
app.config.update(
TESTING=True,
RESTX_ERROR_404_HELP=False,
SECRET_KEY="console-test-session",
SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url),
)
db.init_app(app)
login_manager = DifyLoginManager()
login_manager.init_app(app)
login_manager.unauthorized_handler(unauthorized_handler)
@login_manager.user_loader
def load_account(account_id: str) -> Account | None:
with sqlite_session_factory() as session:
loaded = session.get(Account, account_id)
if loaded is not None:
loaded.set_tenant_id_with_session(tenant.id, session=session)
if state.after_account_load is not None:
state.after_account_load()
return loaded
factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
state = _Harness(app, sqlite_session_factory, account, tenant, target)
@event.listens_for(factory, "after_begin")
def record_session(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
assert all(not previous.in_transaction() and not previous.identity_map for previous in state.sessions)
state.sessions.append(session)
app.extensions["application_services"] = _Services(
ConversationQueryService(
files=MessageFileResolver(),
conversations=ConversationRepository(session_factory=factory),
restore_inputs=restore_input_files,
)
)
api = ExternalApi(app)
api.add_resource(ChatConversationApi, "/apps/<uuid:app_id>/chat-conversations")
api.add_resource(CompletionConversationApi, "/apps/<uuid:app_id>/completion-conversations")
yield state
with app.app_context():
db.session.remove()
db.engine.dispose()
def _conversation(
harness: _Harness,
*,
app_id: str | None = None,
name: str = "Conversation name",
query: str = "First question",
answer: str = "First answer",
created_at: datetime = _CREATED_AT,
updated_at: datetime = _CREATED_AT,
deleted: bool = False,
invoke_from: str = "web-app",
with_message: bool = True,
) -> Conversation:
conversation = make_conversation(
conversation_id=str(uuid4()),
app_id=app_id or harness.target.id,
mode=harness.target.mode,
name=name,
inputs={},
status="normal",
from_source=ConversationFromSource.API,
from_account_id=harness.account.id,
introduction="Conversation introduction",
invoke_from=invoke_from,
is_deleted=deleted,
override_model_configs='{"pre_prompt":"Be useful","model":{"name":"example"}}',
)
conversation.created_at = created_at
conversation.updated_at = updated_at
message = make_message(
message_id=str(uuid4()),
app_id=conversation.app_id,
conversation_id=conversation.id,
inputs={"text": "", "zero": 0, "boolean": False, "empty": [], "null": None},
query=query,
answer=answer,
message=[{"text": "Prompt", "role": "user"}],
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
total_price=Decimal(0),
currency="USD",
from_source=ConversationFromSource.API,
created_at=created_at,
updated_at=updated_at,
)
with harness.factory.begin() as session:
session.add(conversation)
if with_message:
session.add(message)
return conversation
def _ids(response: TestResponse) -> list[str]:
assert response.status_code == 200, response.get_json()
return [row["id"] for row in response.get_json()["data"]]
@pytest.mark.parametrize("kind", ["chat", "completion"])
def test_empty_page_retains_wire_contract(harness: _Harness, kind: _Kind) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
response = harness.get(kind)
assert response.status_code == 200
assert response.get_json() == {"page": 1, "limit": 20, "total": 0, "has_more": False, "data": []}
@pytest.mark.parametrize("kind", ["chat", "completion"])
def test_response_matches_legacy_projection_without_marking_read(harness: _Harness, kind: _Kind) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
conversation = _conversation(harness)
with harness.factory.begin() as session:
message = session.scalar(select(Message).where(Message.conversation_id == conversation.id))
assert message is not None
session.add_all(
[
MessageAnnotation(
app_id=harness.target.id,
conversation_id=conversation.id,
message_id=message.id,
account_id=harness.account.id,
question="Question",
content="Annotation answer",
),
MessageFeedback(
app_id=harness.target.id,
conversation_id=conversation.id,
message_id=message.id,
rating=FeedbackRating.LIKE,
from_source=FeedbackFromSource.USER,
),
]
)
schema = ConversationPagination if kind == "completion" else ConversationWithSummaryPagination
with harness.app.app_context(), harness.factory() as session:
stored = session.get(Conversation, conversation.id)
assert stored is not None
expected = dump_response(
schema,
{
"page": 1,
"per_page": 20,
"total": 1,
"has_next": False,
"items": [ConversationResponseSource(stored, session=session)],
},
)
response = harness.get(kind)
assert response.status_code == 200
assert response.get_json() == expected
assert response.get_json()["data"][0]["model_config"] == {"model": {"name": "example"}, "pre_prompt": "Be useful"}
with harness.factory() as session:
stored = session.get(Conversation, conversation.id)
assert stored is not None
assert stored.read_at is None
assert stored.read_account_id is None
@pytest.mark.parametrize("kind", ["chat", "completion"])
def test_pagination_and_app_deletion_isolation(harness: _Harness, kind: _Kind) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
first = _conversation(harness, created_at=_CREATED_AT, updated_at=_CREATED_AT)
second = _conversation(
harness, created_at=_CREATED_AT + timedelta(hours=1), updated_at=_CREATED_AT + timedelta(hours=1)
)
_conversation(harness, app_id=str(uuid4()))
_conversation(harness, deleted=True)
first_page = harness.get(kind, query={"limit": 1})
assert _ids(first_page) == [second.id]
assert first_page.get_json()["total"] == 2
assert first_page.get_json()["has_more"] is True
second_page = harness.get(kind, query={"limit": 1, "page": 2})
assert _ids(second_page) == [first.id]
assert second_page.get_json()["has_more"] is False
assert _ids(harness.get(kind, query={"page": 3, "limit": 1})) == []
@pytest.mark.parametrize("kind", ["chat", "completion"])
@pytest.mark.parametrize(
"query", [{"limit": 0}, {"limit": 101}, {"page": 0}, {"page": 100000}, {"annotation_status": "bad"}]
)
def test_invalid_query_returns_422(harness: _Harness, kind: _Kind, query: dict[str, str | int]) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
response = harness.get(kind, query=query)
assert response.status_code == 422
@pytest.mark.parametrize("kind", ["chat", "completion"])
@pytest.mark.parametrize("query", [{"start": "bad"}, {"start": "2024-01-16 00:00", "end": "2024-01-15 00:00"}])
def test_invalid_date_range_is_precise_400(harness: _Harness, kind: _Kind, query: dict[str, str | int]) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
response = harness.get(kind, query=query)
assert response.status_code == 400
assert response.get_json()["code"] == "invalid_param"
assert response.get_json()["message"]
@pytest.mark.parametrize(
("kind", "sort_by"), [("completion", "-created_at"), ("chat", "created_at"), ("chat", "-updated_at")]
)
def test_account_timezone_and_exact_end_second_are_inclusive(harness: _Harness, kind: _Kind, sort_by: str) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
boundary = _CREATED_AT.replace(second=59)
expected = _conversation(harness, created_at=boundary, updated_at=boundary)
_conversation(
harness, created_at=boundary + timedelta(microseconds=1), updated_at=boundary + timedelta(microseconds=1)
)
_conversation(harness, created_at=_CREATED_AT - timedelta(seconds=1), updated_at=_CREATED_AT - timedelta(seconds=1))
assert _ids(
harness.get(kind, query={"start": "2024-01-15 14:30", "end": "2024-01-15 14:30", "sort_by": sort_by})
) == [expected.id]
@pytest.mark.parametrize("kind", ["chat", "completion"])
def test_empty_dates_remain_unbounded(harness: _Harness, kind: _Kind) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
conversation = _conversation(harness)
assert _ids(harness.get(kind, query={"start": "", "end": ""})) == [conversation.id]
@pytest.mark.parametrize("field", ["query", "answer"])
@pytest.mark.parametrize("kind", ["chat", "completion"])
def test_keyword_is_case_insensitive_and_escapes_wildcards(harness: _Harness, kind: _Kind, field: str) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
matching = _conversation(
harness,
query="Literal 50%_done" if field == "query" else "First question",
answer="Literal 50%_done" if field == "answer" else "First answer",
)
_conversation(harness, query="50XYZdone", answer="50XYZdone")
assert _ids(harness.get(kind, query={"keyword": "50%_DONE"})) == [matching.id]
@pytest.mark.parametrize("filter_value", ["annotated", "not_annotated"])
@pytest.mark.parametrize("kind", ["chat", "completion"])
def test_annotation_filter(harness: _Harness, kind: _Kind, filter_value: str) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
annotated = _conversation(harness)
plain = _conversation(harness)
with harness.factory.begin() as session:
session.add(
MessageAnnotation(
app_id=harness.target.id,
conversation_id=annotated.id,
account_id=harness.account.id,
content="Annotation",
question="Question",
)
)
assert _ids(harness.get(kind, query={"annotation_status": filter_value})) == [
annotated.id if filter_value == "annotated" else plain.id
]
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])
def test_chat_modes_and_debugger_visibility(harness: _Harness, mode: AppMode) -> None:
harness.mode(mode)
published = _conversation(harness)
debugger = _conversation(harness, invoke_from="debugger")
expected = {published.id} if mode == AppMode.ADVANCED_CHAT else {published.id, debugger.id}
assert set(_ids(harness.get("chat"))) == expected
@pytest.mark.parametrize(
("kind", "mode"), [("chat", AppMode.COMPLETION), ("chat", AppMode.WORKFLOW), ("completion", AppMode.CHAT)]
)
def test_unavailable_app_is_rejected_before_query_validation(harness: _Harness, kind: _Kind, mode: AppMode) -> None:
harness.mode(mode)
response = harness.get(kind, query={"limit": 0})
assert response.status_code == 404
assert response.get_json()["code"] == "app_not_found"
assert "App mode is not in the supported list" in response.get_json()["message"]
@pytest.mark.parametrize("kind", ["chat", "completion"])
def test_another_tenants_app_is_not_visible(harness: _Harness, kind: _Kind) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
with harness.factory.begin() as session:
target = session.get(App, harness.target.id)
assert target is not None
target.tenant_id = str(uuid4())
response = harness.get(kind)
assert response.status_code == 404
assert response.get_json()["code"] == "app_not_found"
@pytest.mark.parametrize(
"role",
[
TenantAccountRole.OWNER,
TenantAccountRole.ADMIN,
TenantAccountRole.EDITOR,
TenantAccountRole.NORMAL,
TenantAccountRole.DATASET_OPERATOR,
],
)
def test_existing_edit_roles_are_preserved(harness: _Harness, role: TenantAccountRole) -> None:
with harness.factory.begin() as session:
membership = session.scalar(select(TenantAccountJoin).where(TenantAccountJoin.account_id == harness.account.id))
assert membership is not None
membership.role = role
response = harness.get("chat")
assert response.status_code == (200 if TenantAccountRole.is_editing_role(role) else 403)
@pytest.mark.parametrize("kind", ["chat", "completion"])
def test_login_and_csrf_remain_required(harness: _Harness, kind: _Kind) -> None:
assert harness.get(kind, authenticated=False).status_code == 401
assert harness.get(kind, csrf=False).status_code == 401
@pytest.mark.parametrize("handler", [ChatConversationApi.get, CompletionConversationApi.get])
def test_app_view_layout_rbac_check_is_declared(handler: Callable[..., object]) -> None:
checks: tuple[RBACCheck, ...] = vars(handler)[RBAC_CHECKS_ATTR]
assert len(checks) == 1
assert checks[0].scene == RBACPermission.APP_VIEW_LAYOUT
assert isinstance(checks[0].locator, PlainApp)
assert checks[0].locator.param == "app_id"
@pytest.mark.parametrize("kind", ["chat", "completion"])
def test_account_removed_after_authentication_is_unauthorized(harness: _Harness, kind: _Kind) -> None:
harness.mode(AppMode.COMPLETION if kind == "completion" else AppMode.CHAT)
def delete_account() -> None:
with harness.factory.begin() as session:
account = session.get(Account, harness.account.id)
assert account is not None
session.delete(account)
harness.after_account_load = delete_account
response = harness.get(kind)
assert response.status_code == 401
assert response.get_json()["code"] == "unauthorized"
assert response.get_json()["message"]
@@ -0,0 +1,165 @@
"""Console detail admission retains app-wide reads without list-only restrictions."""
from collections.abc import Callable
from typing import Literal
from uuid import uuid4
import pytest
from sqlalchemy import delete, func, select, text, update
from models.account import TenantAccountJoin, TenantAccountRole
from models.agent import Agent, AgentScope
from models.enums import ConversationFromSource
from models.model import App, AppMode, Conversation, Message
from tests.e2e_tests.contracts.controllers.console.app.test_message_list import (
_error,
_get,
_route,
harness,
installed_app_harness,
)
from tests.e2e_tests.contracts.controllers.console.explore.test_message_suggested_questions import _Harness
__all__ = ["harness", "installed_app_harness"]
def _detail_route(state: _Harness, kind: Literal["app", "agent"], *, scope: AgentScope = AgentScope.ROSTER) -> str:
return _route(state, kind, scope=scope).removesuffix("/chat-messages") + f"/messages/{state.message.id}"
@pytest.mark.parametrize("kind", ["app", "agent"])
@pytest.mark.parametrize("role", list(TenantAccountRole))
def test_detail_keeps_all_initialized_workspace_roles(
harness: _Harness, kind: Literal["app", "agent"], role: TenantAccountRole
) -> None:
route = _detail_route(harness, kind)
with harness.factory.begin() as session:
session.execute(
update(TenantAccountJoin).where(TenantAccountJoin.account_id == harness.account.id).values(role=role)
)
response = _get(harness, route)
assert response.status_code == 200
assert response.get_json()["id"] == harness.message.id
harness.assert_closed()
@pytest.mark.parametrize("mode", list(AppMode))
def test_plain_app_detail_keeps_every_app_mode(harness: _Harness, mode: AppMode) -> None:
route = _detail_route(harness, "app")
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
response = _get(harness, route)
assert response.status_code == 200
assert response.get_json()["id"] == harness.message.id
@pytest.mark.parametrize("kind", ["app", "agent"])
@pytest.mark.parametrize("actor", ["other-account", "end-user", "deleted-conversation", "missing-conversation"])
def test_detail_keeps_other_actors_and_deleted_agent_conversations_visible(
harness: _Harness, kind: Literal["app", "agent"], actor: str
) -> None:
route = _detail_route(harness, kind)
with harness.factory.begin() as session:
if actor == "deleted-conversation":
session.execute(
update(Conversation).where(Conversation.id == harness.conversation.id).values(is_deleted=True)
)
elif actor == "missing-conversation":
session.execute(delete(Conversation).where(Conversation.id == harness.conversation.id))
else:
for model in (Message, Conversation):
record_id = harness.message.id if model is Message else harness.conversation.id
values = (
{"from_account_id": str(uuid4())}
if actor == "other-account"
else {
"from_account_id": None,
"from_end_user_id": str(uuid4()),
"from_source": ConversationFromSource.API,
}
)
session.execute(update(model).where(model.id == record_id).values(**values))
response = _get(harness, route)
assert response.status_code == 200
assert response.get_json()["id"] == harness.message.id
harness.assert_closed()
@pytest.mark.parametrize("kind", ["app", "agent"])
@pytest.mark.parametrize("missing", ["login", "csrf"])
def test_detail_requires_real_login_and_csrf(harness: _Harness, kind: Literal["app", "agent"], missing: str) -> None:
response = _get(harness, _detail_route(harness, kind), authenticated=missing != "login", csrf=missing != "csrf")
_error(response, status=401, code="unauthorized")
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
@pytest.mark.parametrize("kind", ["app", "agent"])
@pytest.mark.parametrize("missing", ["message", "foreign-message"])
def test_detail_requires_message_in_admitted_app(
harness: _Harness, kind: Literal["app", "agent"], missing: str
) -> None:
route = _detail_route(harness, kind)
with harness.factory.begin() as session:
if missing == "message":
session.execute(delete(Message).where(Message.id == harness.message.id))
else:
session.execute(update(Message).where(Message.id == harness.message.id).values(app_id=str(uuid4())))
response = _get(harness, route)
_error(response, status=404, code="not_found")
assert response.get_json()["message"] == "Message Not Exists."
@pytest.mark.parametrize("kind", ["app", "agent"])
@pytest.mark.parametrize("missing", ["app", "foreign-workspace"])
def test_detail_rejects_missing_or_foreign_app(harness: _Harness, kind: Literal["app", "agent"], missing: str) -> None:
route = _detail_route(harness, kind)
with harness.factory.begin() as session:
if missing == "app":
session.execute(delete(App).where(App.id == harness.target.id))
else:
session.execute(update(App).where(App.id == harness.target.id).values(tenant_id=str(uuid4())))
_error(_get(harness, route), status=404, code="app_not_found" if kind == "app" else "agent_not_found_error")
def test_detail_rejects_archived_app(harness: _Harness) -> None:
route = _detail_route(harness, "app")
with harness.factory.begin() as session:
session.execute(text("UPDATE apps SET status = 'archived' WHERE id = :id"), {"id": harness.target.id})
_error(_get(harness, route), status=404, code="app_not_found")
@pytest.mark.parametrize("scope", [AgentScope.ROSTER, AgentScope.WORKFLOW_ONLY])
def test_detail_resolves_existing_agent_runtime_without_creating_apps(harness: _Harness, scope: AgentScope) -> None:
route = _detail_route(harness, "agent", scope=scope)
response = _get(harness, route)
assert response.status_code == 200
if scope == AgentScope.WORKFLOW_ONLY:
_error(
_get(harness, f"/apps/{harness.target.id}/messages/{harness.message.id}"), status=404, code="app_not_found"
)
with harness.factory.begin() as session:
session.execute(update(Agent).values(backing_app_id=None))
count = session.scalar(select(func.count()).select_from(App))
_error(_get(harness, route), status=404, code="agent_not_found_error")
with harness.factory() as session:
assert session.scalar(select(func.count()).select_from(App)) == count
assert session.scalar(select(Agent.backing_app_id)) is None
def test_plain_detail_rbac_admits_app_maintainer_and_rejects_agent_binding(
harness: _Harness, config_overrides: Callable[..., None]
) -> None:
route = _detail_route(harness, "app")
config_overrides(RBAC_ENABLED=True)
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(maintainer=harness.account.id))
session.execute(
update(TenantAccountJoin)
.where(TenantAccountJoin.account_id == harness.account.id)
.values(role=TenantAccountRole.NORMAL)
)
response = _get(harness, route)
assert response.status_code == 200
assert response.get_json()["id"] == harness.message.id
_route(harness, "agent")
_error(_get(harness, route), status=404, code="not_found")
@@ -0,0 +1,369 @@
"""Console message lists through real HTTP admission, services and SQLite."""
import json
from collections.abc import Iterator
from datetime import datetime, timedelta
from decimal import Decimal
from typing import Literal
from uuid import uuid4
import pytest
from sqlalchemy import delete, func, select, update
from werkzeug.test import TestResponse
from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN
from controllers.console.app.message import AgentChatMessageListApi, AgentMessageApi, ChatMessageListApi, MessageApi
from core.workflow.nodes.human_input.entities import FormDefinition, UserActionConfig
from enums.account import TenantAccountRole
from graphon.file import FileTransferMethod, FileType
from libs.external_api import ExternalApi
from libs.passport import PassportService
from libs.token import generate_csrf_token
from models.account import TenantAccountJoin
from models.agent import Agent, AgentScope
from models.enums import ConversationFromSource, CreatorUserRole, FeedbackFromSource, FeedbackRating
from models.execution_extra_content import HumanInputContent
from models.human_input import HumanInputForm
from models.model import (
App,
AppAnnotationHitHistory,
AppMode,
Conversation,
Message,
MessageAgentThought,
MessageAnnotation,
MessageFeedback,
MessageFile,
)
from tests.e2e_tests.contracts.controllers.console.explore.test_message_suggested_questions import _Harness
from tests.e2e_tests.contracts.controllers.console.explore.test_message_suggested_questions import (
harness as installed_app_harness,
)
from tests.e2e_tests.contracts.controllers.console.test_message_feedback import _admin_route
from tests.helpers.unit.model_factories import make_message
__all__ = ["installed_app_harness"]
_CREATED_AT = datetime(2024, 1, 1)
@pytest.fixture
def harness(installed_app_harness: _Harness) -> Iterator[_Harness]:
state = installed_app_harness
api = ExternalApi(state.app)
api.add_resource(ChatMessageListApi, "/apps/<uuid:app_id>/chat-messages", endpoint="console.app_messages")
api.add_resource(AgentChatMessageListApi, "/agent/<uuid:agent_id>/chat-messages", endpoint="console.agent_messages")
api.add_resource(MessageApi, "/apps/<uuid:app_id>/messages/<uuid:message_id>", endpoint="console.app_message")
api.add_resource(
AgentMessageApi, "/agent/<uuid:agent_id>/messages/<uuid:message_id>", endpoint="console.agent_message"
)
with state.factory.begin() as session:
session.execute(update(Message).where(Message.id == state.message.id).values(created_at=_CREATED_AT))
yield state
if state.sessions:
state.assert_closed()
def _route(state: _Harness, kind: Literal["app", "agent"] = "app", *, scope: AgentScope = AgentScope.ROSTER) -> str:
return _admin_route(state, kind, scope=scope).removesuffix("/feedbacks") + "/chat-messages"
def _get(
state: _Harness,
route: str,
*,
conversation_id: str | None = None,
query: str = "",
authenticated: bool = True,
csrf: bool = True,
) -> TestResponse:
client = state.app.test_client()
token = generate_csrf_token(state.account.id)
headers = {HEADER_NAME_CSRF_TOKEN: token} if csrf else {}
if authenticated:
headers["Authorization"] = f"Bearer {PassportService().issue({'user_id': state.account.id})}"
client.set_cookie(COOKIE_NAME_CSRF_TOKEN, token)
response = client.get(
f"{route}?conversation_id={state.conversation.id if conversation_id is None else conversation_id}&{query}",
headers=headers,
)
assert response.headers["Content-Type"] == "application/json"
assert int(response.headers["Content-Length"]) == len(response.data)
return response
def _error(response: TestResponse, *, status: int, code: str) -> None:
assert response.status_code == status
body = response.get_json()
assert body["status"] == status
assert body["code"] == code
assert body["message"]
def _older_message(state: _Harness, *, age: int) -> Message:
message = make_message(
message_id=str(uuid4()),
app_id=state.target.id,
conversation_id=state.conversation.id,
inputs={"zero": 0, "disabled": False, "null": None, "empty": []},
query="Hello",
message={"text": "Hello"},
answer="你好",
message_tokens=2,
answer_tokens=3,
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
total_price=Decimal(0),
provider_response_latency=0,
currency="USD",
status="normal",
from_source=ConversationFromSource.CONSOLE,
from_account_id=state.account.id,
created_at=_CREATED_AT - timedelta(minutes=age),
)
with state.factory.begin() as session:
session.add(message)
return message
def _add_extra_content(state: _Harness, message_id: str) -> HumanInputForm:
expiration = datetime(2027, 1, 1)
definition = FormDefinition(
form_content="Approve?",
inputs=[],
user_actions=[UserActionConfig(id="approve", title="Approve")],
rendered_content="Approve?",
expiration_time=expiration,
node_title="Approval",
display_in_ui=True,
)
form = HumanInputForm(
tenant_id=state.target.tenant_id,
app_id=state.target.id,
workflow_run_id=str(uuid4()),
node_id="approval",
form_definition=definition.model_dump_json(),
rendered_content="Approve?",
expiration_time=expiration,
)
with state.factory.begin() as session:
app = session.get(App, state.target.id)
assert app is not None
form.tenant_id = app.tenant_id
session.add(form)
session.flush()
session.add(
HumanInputContent.new(workflow_run_id=form.workflow_run_id or "", form_id=form.id, message_id=message_id)
)
return form
@pytest.mark.parametrize("kind", ["app", "agent"])
def test_list_preserves_complete_response_and_older_page_order(
harness: _Harness, kind: Literal["app", "agent"]
) -> None:
route = _route(harness, kind)
oldest = _older_message(harness, age=2)
middle = _older_message(harness, age=1)
form = _add_extra_content(harness, middle.id)
with harness.factory.begin() as session:
session.execute(update(Message).where(Message.id == middle.id).values(message_metadata=json.dumps({"zero": 0})))
annotation = MessageAnnotation(
app_id=harness.target.id,
conversation_id=harness.conversation.id,
message_id=middle.id,
question="Question",
content="Answer",
account_id=harness.account.id,
)
session.add(annotation)
session.flush()
attachment = MessageFile(
message_id=middle.id,
type=FileType.IMAGE,
transfer_method=FileTransferMethod.REMOTE_URL,
url="https://example.com/photo.png",
created_by_role=CreatorUserRole.ACCOUNT,
created_by=harness.account.id,
)
session.add_all(
[
attachment,
MessageFeedback(
app_id=harness.target.id,
conversation_id=harness.conversation.id,
message_id=middle.id,
rating=FeedbackRating.LIKE,
from_source=FeedbackFromSource.ADMIN,
from_account_id=harness.account.id,
content="Useful",
),
AppAnnotationHitHistory(
app_id=harness.target.id,
annotation_id=annotation.id,
source="annotation",
question="Question",
account_id=harness.account.id,
score=0.5,
message_id=middle.id,
annotation_question="Question",
annotation_content="Answer",
),
MessageAgentThought(
message_id=middle.id,
position=1,
created_by_role=CreatorUserRole.ACCOUNT,
created_by=harness.account.id,
thought="Consider the request",
tool_labels_str="{}",
tool_meta_str="{}",
),
]
)
response = _get(harness, route, query="limit=2")
assert response.status_code == 200
body = response.get_json()
assert body["limit"] == 2
assert body["has_more"] is True
assert [item["id"] for item in body["data"]] == [middle.id, harness.message.id]
item = body["data"][0]
assert item["inputs"] == {"zero": 0, "disabled": False, "null": None, "empty": []}
assert item["query"] == "Hello"
assert item["message"] == {"text": "Hello"}
assert item["answer"] == "你好"
assert item["metadata"] == {"zero": 0}
assert item["retriever_resources"] == []
assert item["message_tokens"] == 2
assert item["answer_tokens"] == 3
assert item["created_at"] == int((_CREATED_AT - timedelta(minutes=1)).timestamp())
assert item["feedbacks"][0]["from_account"] == {
"id": harness.account.id,
"name": harness.account.name,
"email": harness.account.email,
}
assert item["feedbacks"][0]["content"] == "Useful"
assert item["annotation"]["id"] == annotation.id
assert item["annotation"]["account"]["id"] == harness.account.id
assert item["annotation_hit_history"]["annotation_id"] == annotation.id
assert item["annotation_hit_history"]["annotation_create_account"]["id"] == harness.account.id
assert item["agent_thoughts"][0]["thought"] == "Consider the request"
assert item["message_files"][0]["id"] == attachment.id
assert item["message_files"][0]["url"] == "https://example.com/photo.png"
assert item["extra_contents"][0]["workflow_run_id"] == form.workflow_run_id
assert item["extra_contents"][0]["submitted"] is False
detail_route = route.removesuffix("/chat-messages") + f"/messages/{middle.id}"
detail = _get(harness, detail_route)
assert detail.status_code == 200
assert detail.get_json() == item
response = _get(harness, route, query=f"limit=2&first_id={middle.id}")
assert response.status_code == 200
assert response.get_json()["has_more"] is False
assert [item["id"] for item in response.get_json()["data"]] == [oldest.id]
harness.assert_closed()
@pytest.mark.parametrize("kind", ["app", "agent"])
@pytest.mark.parametrize("missing", ["login", "csrf"])
def test_list_requires_real_login_and_csrf(harness: _Harness, kind: Literal["app", "agent"], missing: str) -> None:
response = _get(harness, _route(harness, kind), authenticated=missing != "login", csrf=missing != "csrf")
_error(response, status=401, code="unauthorized")
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
@pytest.mark.parametrize("role", list(TenantAccountRole))
@pytest.mark.parametrize("kind", ["app", "agent"])
def test_list_preserves_edit_permission(
harness: _Harness, role: TenantAccountRole, kind: Literal["app", "agent"]
) -> None:
route = _route(harness, kind)
with harness.factory.begin() as session:
session.execute(
update(TenantAccountJoin).where(TenantAccountJoin.account_id == harness.account.id).values(role=role)
)
response = _get(harness, route)
if role in {TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR}:
assert response.status_code == 200
else:
_error(response, status=403, code="forbidden")
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT])
@pytest.mark.parametrize("owner", ["account", "end_user", "deleted"])
def test_plain_console_retains_app_wide_conversation_access(harness: _Harness, mode: AppMode, owner: str) -> None:
route = _route(harness)
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
values = {"is_deleted": True} if owner == "deleted" else {"from_account_id": str(uuid4())}
if owner == "end_user":
values = {
"from_account_id": None,
"from_end_user_id": str(uuid4()),
"from_source": ConversationFromSource.API,
}
session.execute(update(Conversation).where(Conversation.id == harness.conversation.id).values(**values))
response = _get(harness, route)
assert response.status_code == 200
assert response.get_json()["data"][0]["id"] == harness.message.id
@pytest.mark.parametrize("kind", ["app", "agent"])
@pytest.mark.parametrize("owner", ["app_id", "from_account_id", "from_source", "from_end_user_id", "is_deleted"])
def test_agent_app_mode_requires_owned_conversation(
harness: _Harness, kind: Literal["app", "agent"], owner: str
) -> None:
route = _route(harness, "agent")
if kind == "app":
route = f"/apps/{harness.target.id}/chat-messages"
value = True if owner == "is_deleted" else ConversationFromSource.API if owner == "from_source" else str(uuid4())
with harness.factory.begin() as session:
session.execute(update(Conversation).where(Conversation.id == harness.conversation.id).values({owner: value}))
_error(_get(harness, route), status=404, code="not_found")
@pytest.mark.parametrize("query", ["limit=0", "limit=101", "limit=invalid", "first_id=invalid"])
def test_invalid_queries_use_shared_422(harness: _Harness, query: str) -> None:
_error(_get(harness, _route(harness), query=query), status=422, code="unprocessable_entity")
def test_missing_conversation_and_foreign_cursor_keep_precise_errors(harness: _Harness) -> None:
route = _route(harness)
response = _get(harness, route, conversation_id=str(uuid4()))
_error(response, status=404, code="not_found")
assert response.get_json()["message"] == "Conversation Not Exists."
with harness.factory.begin() as session:
session.execute(update(Message).where(Message.id == harness.message.id).values(conversation_id=str(uuid4())))
response = _get(harness, route, query=f"first_id={harness.message.id}")
_error(response, status=404, code="not_found")
assert response.get_json()["message"] == "First message not found"
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
def test_plain_console_rejects_non_chat_modes(harness: _Harness, mode: AppMode) -> None:
route = _route(harness)
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
_error(_get(harness, route), status=404, code="app_not_found")
@pytest.mark.parametrize("scope", [AgentScope.ROSTER, AgentScope.WORKFLOW_ONLY])
def test_agent_runtime_resolution_stays_read_only(harness: _Harness, scope: AgentScope) -> None:
route = _route(harness, "agent", scope=scope)
assert _get(harness, route).status_code == 200
if scope == AgentScope.WORKFLOW_ONLY:
_error(_get(harness, f"/apps/{harness.target.id}/chat-messages"), status=404, code="app_not_found")
with harness.factory.begin() as session:
session.execute(update(Agent).values(backing_app_id=None))
app_count = session.scalar(select(func.count()).select_from(App))
_error(_get(harness, route), status=404, code="agent_not_found_error")
with harness.factory() as session:
assert session.scalar(select(func.count()).select_from(App)) == app_count
assert session.scalar(select(Agent.backing_app_id)) is None
def test_empty_conversation_returns_default_envelope(harness: _Harness) -> None:
route = _route(harness)
with harness.factory.begin() as session:
session.execute(delete(Message).where(Message.id == harness.message.id))
response = _get(harness, route, query="first_id=")
assert response.status_code == 200
assert response.get_json() == {"limit": 20, "has_more": False, "data": []}
@@ -0,0 +1,515 @@
"""Console suggested-question admission and actor/configuration isolation over SQLite."""
import json
from collections.abc import Iterator
from dataclasses import dataclass
from decimal import Decimal
from http import HTTPStatus
from uuid import uuid4
import pytest
from dify_trace_sdk.testing import RecordingEgress
from flask import Flask
from sqlalchemy import Connection, Engine, delete, event, func, select, text, update
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
import controllers.console.wraps as console_wraps
from controllers.console.app import message as controller
from enums import DeploymentEdition
from extensions.application_services.app import AppServices, build_app_services
from extensions.application_services.rbac import RBACServices
from extensions.ext_application_services import _build_oauth_server_service
from extensions.ext_database import db
from extensions.ext_redis import RedisClientWrapper
from graphon.model_runtime.entities.model_entities import ModelType
from libs import login
from libs.external_api import ExternalApi
from models.account import Account, Tenant
from models.agent import (
Agent,
AgentConfigDraft,
AgentConfigDraftType,
AgentConfigVersionKind,
AgentDebugConversation,
AgentScope,
AgentSource,
AgentStatus,
AgentWorkspaceBinding,
)
from models.agent_config_entities import AgentSoulConfig
from models.enums import ConversationFromSource
from models.model import App, AppMode, AppModelConfig, Conversation, DifySetup, Message
from models.provider import Provider, TenantDefaultModel
from repositories.app.agent_app_repository import AgentAppRepository
from repositories.app.console_repository import ConsoleAppRepository
from repositories.app.tracing_config_repository import SQLAlchemyAppTracingConfigRepository
from repositories.message_repository import MessageRepository
from repositories.recommended_app_catalog_repository import DatabaseRecommendedAppCatalogRepository
from services.agent.roster_package_exporter import RosterAgentPackageExporter
from services.app.agent_app_service import AgentAppAccessService
from services.app_tracing_config_service import AppTracingConfigService
from services.entities.message_entities import MessageAccount
from services.message_suggested_questions_generator import SuggestedQuestionsGenerator
from services.message_suggested_questions_queries import SuggestedQuestionsQuery
from services.message_suggested_questions_service import (
MessageSuggestedQuestions,
MessageSuggestedQuestionsService,
)
from services.recommended_app_package_service import RecommendedAppPackageService
from tests.helpers.unit.config_override import apply_config_overrides
from tests.helpers.unit.model_factories import make_account, make_app, make_conversation, make_message
from tests.helpers.unit.rbac_fakes import RBACDomain
from tests.helpers.unit.services.tracing_ports import deployment_provider_configs, untraced_scopes
@dataclass
class _Agents:
access: AgentAppAccessService
@dataclass
class _Services:
apps: AppServices
agent_apps: _Agents
message_suggested_questions: MessageSuggestedQuestions
rbac: RBACServices
@dataclass
class _Harness:
flask_app: Flask
target: App
account: Account
conversation: Conversation
message: Message
agent_id: str
factory: sessionmaker[Session]
services: _Services
sessions: list[Session]
queries: SuggestedQuestionsQuery
def get(self, route: str = "app") -> TestResponse:
resource = f"apps/{self.target.id}" if route == "app" else f"agent/{self.agent_id}"
return self.flask_app.test_client().get(f"/{resource}/chat-messages/{self.message.id}/suggested-questions")
def add_agent(self, *, scope: AgentScope = AgentScope.ROSTER, backing: bool = True) -> None:
with self.factory.begin() as session:
session.execute(update(App).where(App.id == self.target.id).values(mode=AppMode.AGENT))
session.add(
Agent(
id=self.agent_id,
tenant_id=self.target.tenant_id,
name="Agent",
scope=scope,
source=AgentSource.AGENT_APP if scope == AgentScope.ROSTER else AgentSource.WORKFLOW,
status=AgentStatus.ACTIVE,
app_id=self.target.id if scope == AgentScope.ROSTER else str(uuid4()),
backing_app_id=self.target.id if scope == AgentScope.WORKFLOW_ONLY and backing else None,
workflow_id=str(uuid4()) if scope == AgentScope.WORKFLOW_ONLY else None,
workflow_node_id="node" if scope == AgentScope.WORKFLOW_ONLY else None,
)
)
@pytest.fixture
def harness(
_empty_model_provider_daemon: None,
monkeypatch: pytest.MonkeyPatch,
sqlite_engine: Engine,
sqlite_session_factory: sessionmaker[Session],
rbac_domain: RBACDomain,
) -> Iterator[_Harness]:
target = make_app(app_id=str(uuid4()), tenant_id=str(uuid4()))
account = make_account(account_id=str(uuid4()))
tenant = Tenant(name="Workspace")
tenant.id = target.tenant_id
account._current_tenant = tenant
conversation = make_conversation(
conversation_id=str(uuid4()),
app_id=target.id,
from_source=ConversationFromSource.CONSOLE,
from_account_id=account.id,
inputs={},
)
config = AppModelConfig(
app_id=target.id, suggested_questions_after_answer=json.dumps({"enabled": True, "prompt": "Console questions"})
)
message = make_message(
message_id=str(uuid4()),
app_id=target.id,
conversation_id=conversation.id,
inputs={},
message={},
query="How?",
answer="Like this.",
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
currency="USD",
from_source=ConversationFromSource.CONSOLE,
from_account_id=account.id,
)
with sqlite_session_factory.begin() as session:
session.add_all(
[
target,
account,
conversation,
config,
message,
DifySetup(version="test"),
Provider(tenant_id=target.tenant_id, provider_name="invalid/provider", is_valid=True),
TenantDefaultModel(
tenant_id=target.tenant_id,
provider_name="invalid/provider",
model_name="invalid-model",
model_type=ModelType.LLM,
),
]
)
session.flush()
target.app_model_config_id = config.id
conversation.app_model_config_id = config.id
sessions: list[Session] = []
def track_session(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
sessions.append(session)
event.listen(sqlite_session_factory, "after_begin", track_session)
monkeypatch.setattr(login, "current_user", account)
console_wraps._is_setup_completed.reset_success()
apply_config_overrides(
monkeypatch, LOGIN_DISABLED=True, RBAC_ENABLED=False, DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY
)
redis = RedisClientWrapper()
app_services = build_app_services(
database_client=sqlite_session_factory,
repository=ConsoleAppRepository(session_factory=sqlite_session_factory),
rbac_members=rbac_domain.rbac.members,
tracing_configs=AppTracingConfigService(
configs=SQLAlchemyAppTracingConfigRepository(session_factory=sqlite_session_factory),
provider=deployment_provider_configs(RecordingEgress([], grpc_target=None)),
),
oauth=_build_oauth_server_service(database_client=sqlite_session_factory, redis=redis),
recommended_packages=RecommendedAppPackageService(
sources=DatabaseRecommendedAppCatalogRepository(sqlite_session_factory, redis=redis),
exporter=RosterAgentPackageExporter(),
),
)
flask_app = Flask(__name__)
flask_app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))
db.init_app(flask_app)
queries = SuggestedQuestionsQuery(
session_factory=sqlite_session_factory, repository=MessageRepository(session_factory=sqlite_session_factory)
)
services = _Services(
app_services,
_Agents(AgentAppAccessService(references=AgentAppRepository(session_factory=sqlite_session_factory))),
MessageSuggestedQuestionsService(
queries=queries, generator=SuggestedQuestionsGenerator(trace_scopes=untraced_scopes())
),
rbac_domain.rbac,
)
flask_app.extensions["application_services"] = services
api = ExternalApi(flask_app)
api.add_resource(
controller.MessageSuggestedQuestionApi,
"/apps/<uuid:app_id>/chat-messages/<uuid:message_id>/suggested-questions",
)
api.add_resource(
controller.AgentMessageSuggestedQuestionApi,
"/agent/<uuid:agent_id>/chat-messages/<uuid:message_id>/suggested-questions",
)
result = _Harness(
flask_app,
target,
account,
conversation,
message,
str(uuid4()),
sqlite_session_factory,
services,
sessions,
queries,
)
yield result
event.remove(sqlite_session_factory, "after_begin", track_session)
console_wraps._is_setup_completed.reset_success()
with flask_app.app_context():
db.session.remove()
db.engine.dispose()
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT])
def test_model_resolution_failure_keeps_empty_http_success(
harness: _Harness, mode: AppMode, caplog: pytest.LogCaptureFixture
) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
response = harness.get()
assert response.status_code == HTTPStatus.OK
assert response.headers["Content-Type"] == "application/json"
assert response.get_json() == {"data": []}
assert any(
record.exc_info
and isinstance(record.exc_info[1], ValueError)
and "Invalid provider: invalid/provider" in str(record.exc_info[1])
for record in caplog.records
)
assert all(not session.in_transaction() and not session.identity_map for session in harness.sessions)
def test_advanced_chat_without_draft_workflow_returns_empty_success(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=AppMode.ADVANCED_CHAT))
response = harness.get()
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"data": []}
@pytest.mark.parametrize("change", ["tenant", "status", "missing", "workflow", "completion", "hidden"])
def test_app_admission_rejects_unavailable_or_unsupported_apps(harness: _Harness, change: str) -> None:
if change == "hidden":
harness.add_agent(scope=AgentScope.WORKFLOW_ONLY)
else:
field_name, value = {
"tenant": ("tenant_id", str(uuid4())),
"status": ("status", "archived"),
"missing": ("id", str(uuid4())),
"workflow": ("mode", "workflow"),
"completion": ("mode", "completion"),
}[change]
with harness.factory.begin() as session:
if change == "status":
session.execute(
text("UPDATE apps SET status = :status WHERE id = :id"), {"status": value, "id": harness.target.id}
)
else:
session.execute(update(App).where(App.id == harness.target.id).values({field_name: value}))
response = harness.get()
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "app_not_found"
@pytest.mark.parametrize("scope", [AgentScope.ROSTER, AgentScope.WORKFLOW_ONLY])
def test_agent_resolves_existing_backing_app_with_its_debug_configuration(harness: _Harness, scope: AgentScope) -> None:
harness.add_agent(scope=scope)
with harness.factory.begin() as session:
session.add(
AgentDebugConversation(
tenant_id=harness.target.tenant_id,
agent_id=harness.agent_id,
app_id=harness.target.id,
account_id=harness.account.id,
draft_type=AgentConfigDraftType.DEBUG_BUILD,
conversation_id=harness.conversation.id,
)
)
session.add(
AgentConfigDraft(
tenant_id=harness.target.tenant_id,
agent_id=harness.agent_id,
account_id=harness.account.id,
draft_owner_key=harness.account.id,
draft_type=AgentConfigDraftType.DEBUG_BUILD,
config_snapshot=AgentSoulConfig.model_validate(
{"app_features": {"suggested_questions_after_answer": {"enabled": True}}}
),
)
)
response = harness.get("agent")
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"data": []}
assert all(not session.in_transaction() and not session.identity_map for session in harness.sessions)
@pytest.mark.parametrize("change", ["missing_backing", "tenant", "agent_status", "app_status", "app_mode"])
def test_agent_rejects_unavailable_runtime_without_creating_app(harness: _Harness, change: str) -> None:
harness.add_agent(scope=AgentScope.WORKFLOW_ONLY, backing=change != "missing_backing")
if change != "missing_backing":
model, key, value = {
"tenant": (Agent, "tenant_id", str(uuid4())),
"agent_status": (Agent, "status", AgentStatus.ARCHIVED),
"app_status": (App, "status", "archived"),
"app_mode": (App, "mode", AppMode.CHAT),
}[change]
with harness.factory.begin() as session:
if change == "app_status":
session.execute(
text("UPDATE apps SET status = :status WHERE id = :id"), {"status": value, "id": harness.target.id}
)
else:
session.execute(update(model).values({key: value}))
response = harness.get("agent")
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "agent_not_found_error"
with harness.factory() as session:
assert session.scalar(select(func.count()).select_from(App)) == 1
assert session.scalar(select(func.count()).select_from(AppModelConfig)) == 1
if change == "missing_backing":
assert session.scalar(select(Agent.backing_app_id)) is None
@pytest.mark.parametrize("route", ["app", "agent"])
def test_deleted_account_has_explicit_unauthorized_response(harness: _Harness, route: str) -> None:
if route == "agent":
harness.add_agent()
with harness.factory.begin() as session:
session.execute(delete(Account).where(Account.id == harness.account.id))
response = harness.get(route)
assert response.status_code == HTTPStatus.UNAUTHORIZED
assert response.get_json()["code"] == "unauthorized"
assert response.get_json()["message"] == "Account no longer exists"
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
def test_disabled_suggestions_have_specific_http_error(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(update(AppModelConfig).values(suggested_questions_after_answer='{"enabled":false}'))
response = harness.get()
assert response.status_code == HTTPStatus.FORBIDDEN
assert response.get_json()["code"] == "app_suggested_questions_after_answer_disabled"
@pytest.mark.parametrize("route", ["app", "agent"])
def test_missing_published_agent_version_preserves_specific_http_error(harness: _Harness, route: str) -> None:
harness.add_agent()
response = harness.get(route)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.headers["Content-Type"] == "application/json"
assert response.get_json() == {
"code": "agent_version_not_found_error",
"message": "Agent config version not found.",
"status": HTTPStatus.NOT_FOUND,
}
assert all(not session.in_transaction() and not session.identity_map for session in harness.sessions)
def test_model_resolution_preserves_pending_caller_session(harness: _Harness) -> None:
with harness.flask_app.app_context():
assert console_wraps._is_setup_completed()
db.session.remove()
caller_session = db.session()
caller_app = caller_session.get(App, harness.target.id)
assert caller_app is not None
caller_app.name = "Pending caller edit"
response = harness.get()
assert db.session() is caller_session
assert caller_session.in_transaction()
assert caller_app in caller_session.dirty
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"data": []}
@pytest.mark.parametrize("entity", [Message, Conversation])
@pytest.mark.parametrize("change", ["app_id", "from_account_id", "from_source"])
def test_runtime_cannot_read_another_accounts_message_or_conversation(
harness: _Harness, entity: type[Message] | type[Conversation], change: str
) -> None:
with harness.factory.begin() as session:
session.execute(update(entity).values({change: "api" if change == "from_source" else str(uuid4())}))
response = harness.get()
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["message"] == ("Message not found" if entity is Message else "Conversation not found")
def test_agent_runtime_uses_current_accounts_personal_debug_draft(harness: _Harness) -> None:
harness.add_agent()
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values({App.app_model_config_id: None}))
session.add(
AgentDebugConversation(
tenant_id=harness.target.tenant_id,
agent_id=harness.agent_id,
app_id=harness.target.id,
account_id=harness.account.id,
draft_type=AgentConfigDraftType.DEBUG_BUILD,
conversation_id=harness.conversation.id,
)
)
for owner, prompt in [(harness.account.id, "My personal draft"), (str(uuid4()), "Other accounts secret draft")]:
session.add(
AgentConfigDraft(
tenant_id=harness.target.tenant_id,
agent_id=harness.agent_id,
account_id=owner,
draft_owner_key=owner,
draft_type=AgentConfigDraftType.DEBUG_BUILD,
config_snapshot=AgentSoulConfig.model_validate(
{"app_features": {"suggested_questions_after_answer": {"enabled": True, "prompt": prompt}}}
),
)
)
response = harness.get("agent")
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"data": []}
context = harness.queries.prepare(
app_id=harness.target.id,
app_owner_tenant_id=harness.target.tenant_id,
expected_app_mode=AppMode.AGENT,
actor=MessageAccount(harness.account.id),
invoke_from="debugger",
message_id=harness.message.id,
)
assert context is not None
assert context.config["prompt"] == "My personal draft"
@pytest.mark.parametrize("same_tenant", [True, False])
def test_agent_runtime_preserves_conversation_binding_scope(harness: _Harness, same_tenant: bool) -> None:
harness.add_agent()
draft_id, binding_id = str(uuid4()), str(uuid4())
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values({App.app_model_config_id: None}))
session.add(
AgentConfigDraft(
id=draft_id,
tenant_id=harness.target.tenant_id,
agent_id=harness.agent_id,
draft_type=AgentConfigDraftType.DRAFT,
config_snapshot=AgentSoulConfig.model_validate(
{
"app_features": {
"suggested_questions_after_answer": {
"enabled": True,
"prompt": "Conversation bound questions",
}
}
}
),
)
)
session.add(
AgentWorkspaceBinding(
id=binding_id,
tenant_id=harness.target.tenant_id if same_tenant else str(uuid4()),
app_id=harness.target.id,
workspace_id=str(uuid4()),
agent_id=harness.agent_id,
agent_config_version_id=draft_id,
agent_config_version_kind=AgentConfigVersionKind.DRAFT,
backend_binding_ref="existing-binding",
)
)
session.execute(
update(Conversation)
.where(Conversation.id == harness.conversation.id)
.values({Conversation.agent_workspace_binding_id: binding_id})
)
response = harness.get("agent")
if same_tenant:
assert response.status_code == HTTPStatus.OK
context = harness.queries.prepare(
app_id=harness.target.id,
app_owner_tenant_id=harness.target.tenant_id,
expected_app_mode=AppMode.AGENT,
actor=MessageAccount(harness.account.id),
invoke_from="debugger",
message_id=harness.message.id,
)
assert context is not None
assert context.config["prompt"] == "Conversation bound questions"
else:
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "agent_version_not_found_error"
@@ -0,0 +1,185 @@
"""Explore message lists through real HTTP admission, services and SQLite."""
from collections.abc import Iterator
from uuid import uuid4
import pytest
from sqlalchemy import delete, event, update
from sqlalchemy.orm import Session, SessionTransaction
from controllers.console.explore.message import MessageListApi
from libs.external_api import ExternalApi
from models import Account
from models.enums import ConversationFromSource, FeedbackFromSource, FeedbackRating
from models.model import App, AppMode, Conversation, InstalledApp, Message, MessageFeedback
from tests.e2e_tests.contracts.controllers.console.app.test_message_list import (
_CREATED_AT,
_add_extra_content,
_error,
_get,
_older_message,
)
from tests.e2e_tests.contracts.controllers.console.explore.test_message_suggested_questions import _Harness
from tests.e2e_tests.contracts.controllers.console.explore.test_message_suggested_questions import (
harness as installed_app_harness,
)
__all__ = ["installed_app_harness"]
@pytest.fixture
def harness(installed_app_harness: _Harness) -> Iterator[_Harness]:
state = installed_app_harness
api = ExternalApi(state.app)
api.add_resource(MessageListApi, "/installed-apps/<uuid:installed_app_id>/messages", endpoint="console.messages")
with state.factory.begin() as session:
session.execute(update(Message).where(Message.id == state.message.id).values(created_at=_CREATED_AT))
yield state
if state.sessions:
state.assert_closed()
with state.factory() as session:
installation = session.get(InstalledApp, state.installation.id)
if installation is not None:
assert installation.last_used_at is None
def _route(state: _Harness) -> str:
return f"/installed-apps/{state.installation.id}/messages"
def test_list_preserves_response_and_fetches_older_messages_in_display_order(harness: _Harness) -> None:
assert harness.target.tenant_id != harness.installation.tenant_id
oldest = _older_message(harness, age=2)
middle = _older_message(harness, age=1)
form = _add_extra_content(harness, middle.id)
with harness.factory.begin() as session:
session.add_all(
[
MessageFeedback(
app_id=harness.target.id,
conversation_id=harness.conversation.id,
message_id=middle.id,
rating=rating,
from_source=source,
)
for rating, source in (
(FeedbackRating.LIKE, FeedbackFromSource.ADMIN),
(FeedbackRating.DISLIKE, FeedbackFromSource.USER),
)
]
)
response = _get(harness, _route(harness), query="limit=2")
assert response.status_code == 200
body = response.get_json()
assert body["limit"] == 2
assert body["has_more"] is True
assert [item["id"] for item in body["data"]] == [middle.id, harness.message.id]
item = body["data"][0]
assert item["inputs"] == {"zero": 0, "disabled": False, "null": None, "empty": []}
assert item["feedback"] == {"rating": "dislike"}
assert item["total_tokens"] == 5
assert item["total_price"] == "0E-7"
assert item["currency"] == "USD"
assert item["extra_contents"][0]["workflow_run_id"] == form.workflow_run_id
assert item["extra_contents"][0]["submitted"] is False
assert item["extra_contents"][0]["form_submission_data"] is None
response = _get(harness, _route(harness), query=f"limit=2&first_id={middle.id}")
assert response.status_code == 200
assert response.get_json()["has_more"] is False
assert [item["id"] for item in response.get_json()["data"]] == [oldest.id]
harness.assert_closed()
@pytest.mark.parametrize("empty_id", [False, True])
def test_empty_list_keeps_defaults(harness: _Harness, empty_id: bool) -> None:
with harness.factory.begin() as session:
session.execute(delete(Message).where(Message.id == harness.message.id))
response = _get(harness, _route(harness), conversation_id="" if empty_id else None)
assert response.status_code == 200
assert response.get_json() == {"limit": 20, "has_more": False, "data": []}
@pytest.mark.parametrize("query", ["limit=0", "limit=101", "limit=invalid", "first_id=invalid"])
def test_invalid_query_uses_shared_422(harness: _Harness, query: str) -> None:
_error(_get(harness, _route(harness), query=query), status=422, code="unprocessable_entity")
@pytest.mark.parametrize(
"owner", ["app_id", "from_account_id", "from_source", "from_end_user_id", "is_deleted", "missing"]
)
def test_list_requires_complete_conversation_ownership(harness: _Harness, owner: str) -> None:
with harness.factory.begin() as session:
if owner == "missing":
session.execute(delete(Conversation).where(Conversation.id == harness.conversation.id))
else:
value = (
True
if owner == "is_deleted"
else ConversationFromSource.API
if owner == "from_source"
else str(uuid4())
)
session.execute(
update(Conversation).where(Conversation.id == harness.conversation.id).values({owner: value})
)
_error(_get(harness, _route(harness)), status=404, code="conversation_not_found")
def test_list_rejects_cursor_from_another_conversation(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(update(Message).where(Message.id == harness.message.id).values(conversation_id=str(uuid4())))
_error(
_get(harness, _route(harness), query=f"first_id={harness.message.id}"),
status=404,
code="message_cursor_not_found",
)
@pytest.mark.parametrize("missing", ["installation", "workspace", "app"])
def test_list_applies_installed_app_admission(harness: _Harness, missing: str) -> None:
with harness.factory.begin() as session:
if missing == "installation":
session.execute(delete(InstalledApp).where(InstalledApp.id == harness.installation.id))
elif missing == "workspace":
session.execute(
update(InstalledApp).where(InstalledApp.id == harness.installation.id).values(tenant_id=str(uuid4()))
)
else:
session.execute(delete(App).where(App.id == harness.target.id))
_error(_get(harness, _route(harness)), status=404, code="installed_app_not_found")
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW, AppMode.AGENT])
def test_non_chat_modes_have_specific_error(harness: _Harness, mode: AppMode) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
_error(_get(harness, _route(harness)), status=400, code="not_chat_app")
@pytest.mark.parametrize("missing", ["login", "csrf"])
def test_login_and_csrf_are_required(harness: _Harness, missing: str) -> None:
response = _get(harness, _route(harness), authenticated=missing != "login", csrf=missing != "csrf")
_error(response, status=401, code="unauthorized")
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
@pytest.mark.parametrize("change", ["installation", "app", "owner", "account"])
def test_stale_admitted_resource_is_revalidated(harness: _Harness, change: str) -> None:
def change_resource(_session: Session, _transaction: SessionTransaction) -> None:
with harness.factory.begin() as session:
if change == "installation":
session.execute(delete(InstalledApp).where(InstalledApp.id == harness.installation.id))
elif change == "account":
session.execute(delete(Account).where(Account.id == harness.account.id))
elif change == "app":
session.execute(delete(App).where(App.id == harness.target.id))
else:
session.execute(update(App).where(App.id == harness.target.id).values(tenant_id=str(uuid4())))
event.listen(harness.admission_factory, "after_transaction_end", change_resource, once=True)
try:
response = _get(harness, _route(harness))
finally:
event.remove(harness.admission_factory, "after_transaction_end", change_resource)
status, code = (401, "unauthorized") if change == "account" else (404, "installed_app_not_found")
_error(response, status=status, code=code)
@@ -0,0 +1,239 @@
"""More-like-this HTTP failures through real account admission and SQLite services."""
import json
from collections.abc import Iterator
from uuid import uuid4
import pytest
from sqlalchemy import delete, func, select, update
from werkzeug.test import TestResponse
from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN
from controllers.console.explore.message import MessageMoreLikeThisApi
from libs.external_api import ExternalApi
from libs.passport import PassportService
from libs.token import generate_csrf_token
from models.enums import ConversationFromSource
from models.model import App, AppMode, AppModelConfig, Conversation, InstalledApp, Message
from tests.e2e_tests.contracts.controllers.console.explore.test_message_suggested_questions import (
_Harness,
)
from tests.e2e_tests.contracts.controllers.console.explore.test_message_suggested_questions import (
harness as installed_app_harness,
)
__all__ = ["installed_app_harness"]
@pytest.fixture
def harness(installed_app_harness: _Harness) -> Iterator[_Harness]:
"""Reuse real console admission while storing separate current and historical config."""
state = installed_app_harness
historical = AppModelConfig(
app_id=state.target.id,
model=json.dumps({"provider": "langgenius/openai/openai", "name": "old-model", "completion_params": {}}),
)
with state.factory.begin() as session:
session.add(historical)
session.flush()
session.execute(update(App).where(App.id == state.target.id).values(mode=AppMode.COMPLETION))
session.execute(
update(AppModelConfig)
.where(AppModelConfig.id == state.target.app_model_config_id)
.values(more_like_this='{"enabled":true}')
)
conversation = session.get(Conversation, state.conversation.id)
assert conversation is not None
conversation.mode = AppMode.COMPLETION
conversation.app_model_config_id = historical.id
api = ExternalApi(state.app)
api.add_resource(
MessageMoreLikeThisApi,
"/installed-apps/<uuid:installed_app_id>/messages/<uuid:message_id>/more-like-this",
endpoint="console.more_like_this",
)
yield state
state.assert_closed()
with state.factory() as session:
installation = session.get(InstalledApp, state.installation.id)
if installation is None:
assert session.get(App, state.target.id) is None
else:
assert installation.last_used_at is None
def _get(
state: _Harness,
*,
installation_id: str | None = None,
authenticated: bool = True,
csrf: bool = True,
query: str = "response_mode=blocking",
) -> TestResponse:
client = state.app.test_client()
token = generate_csrf_token(state.account.id)
headers = {HEADER_NAME_CSRF_TOKEN: token} if csrf else {}
if authenticated:
headers["Authorization"] = f"Bearer {PassportService().issue({'user_id': state.account.id})}"
client.set_cookie(COOKIE_NAME_CSRF_TOKEN, token)
return client.get(
f"/installed-apps/{installation_id or state.installation.id}/messages/{state.message.id}"
f"/more-like-this?{query}",
headers=headers,
)
def _error(response: TestResponse, *, status: int, code: str) -> None:
assert response.status_code == status
assert response.headers["Content-Type"] == "application/json"
assert int(response.headers["Content-Length"]) == len(response.data)
body = response.get_json()
assert body["status"] == status
assert body["code"] == code
assert isinstance(body["message"], str)
assert body["message"]
@pytest.mark.parametrize("response_mode", ["blocking", "streaming"])
def test_real_provider_resolution_failure_releases_sessions_without_updating_usage(
harness: _Harness, response_mode: str
) -> None:
assert harness.target.tenant_id != harness.installation.tenant_id
response = _get(harness, query=f"response_mode={response_mode}")
# The persisted invalid owner provider reaches the actual model resolver;
# provider discovery and generation are never replaced by test methods.
_error(response, status=400, code="invalid_param")
assert response.get_json()["message"] == "Invalid plugin id invalid/provider"
with harness.factory() as session:
assert session.scalar(select(func.count()).select_from(Message)) == 1
historical_model = session.scalar(
select(AppModelConfig.model).where(
AppModelConfig.id
== select(Conversation.app_model_config_id)
.where(Conversation.id == harness.conversation.id)
.scalar_subquery()
)
)
assert historical_model is not None
assert json.loads(historical_model)["completion_params"] == {}
@pytest.mark.parametrize("missing", ["login", "csrf"])
def test_real_login_and_csrf_are_required(harness: _Harness, missing: str) -> None:
response = _get(harness, authenticated=missing != "login", csrf=missing != "csrf")
_error(response, status=401, code="unauthorized")
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
@pytest.mark.parametrize("query", ["", "response_mode=invalid"])
def test_invalid_response_mode_uses_shared_validation_error(harness: _Harness, query: str) -> None:
_error(_get(harness, query=query), status=422, code="unprocessable_entity")
@pytest.mark.parametrize("mode", [mode for mode in AppMode if mode != AppMode.COMPLETION])
def test_non_completion_modes_reject_generation(harness: _Harness, mode: AppMode) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
_error(_get(harness), status=400, code="not_completion_app")
@pytest.mark.parametrize("missing", ["installation", "workspace", "app"])
def test_missing_or_foreign_installation_is_rejected(harness: _Harness, missing: str) -> None:
installation_id = harness.installation.id
if missing == "installation":
installation_id = str(uuid4())
else:
with harness.factory.begin() as session:
if missing == "workspace":
session.execute(
update(InstalledApp).where(InstalledApp.id == installation_id).values(tenant_id=str(uuid4()))
)
else:
session.execute(delete(App).where(App.id == harness.target.id))
_error(_get(harness, installation_id=installation_id), status=404, code="installed_app_not_found")
@pytest.mark.parametrize("model", [Message, Conversation])
@pytest.mark.parametrize("field", ["app_id", "from_account_id", "from_source", "from_end_user_id"])
def test_source_requires_complete_account_ownership(
harness: _Harness, model: type[Message] | type[Conversation], field: str
) -> None:
record_id = harness.message.id if model is Message else harness.conversation.id
value = ConversationFromSource.API if field == "from_source" else str(uuid4())
with harness.factory.begin() as session:
session.execute(update(model).where(model.id == record_id).values({field: value}))
_error(
_get(harness),
status=404 if model is Message else 400,
code="message_not_found" if model is Message else "app_unavailable",
)
@pytest.mark.parametrize("missing", ["message", "conversation", "current-config", "historical-config"])
def test_missing_records_keep_resource_specific_errors(harness: _Harness, missing: str) -> None:
with harness.factory.begin() as session:
if missing == "message":
session.execute(delete(Message).where(Message.id == harness.message.id))
elif missing == "conversation":
session.execute(delete(Conversation).where(Conversation.id == harness.conversation.id))
elif missing == "current-config":
session.execute(delete(AppModelConfig).where(AppModelConfig.id == harness.target.app_model_config_id))
else:
session.execute(
delete(AppModelConfig).where(
AppModelConfig.id
== select(Conversation.app_model_config_id)
.where(Conversation.id == harness.conversation.id)
.scalar_subquery()
)
)
status, code = {
"message": (404, "message_not_found"),
"conversation": (400, "app_unavailable"),
"current-config": (403, "app_more_like_this_disabled"),
"historical-config": (400, "app_unavailable"),
}[missing]
_error(_get(harness), status=status, code=code)
@pytest.mark.parametrize("feature", [None, "", "{}", '{"enabled":false}'])
def test_current_feature_policy_rejects_disabled_regeneration(harness: _Harness, feature: str | None) -> None:
"""A null or empty persisted feature is the legacy disabled setting."""
with harness.factory.begin() as session:
session.execute(
update(AppModelConfig)
.where(AppModelConfig.id == harness.target.app_model_config_id)
.values(more_like_this=feature)
)
_error(_get(harness), status=403, code="app_more_like_this_disabled")
@pytest.mark.parametrize(
("current", "field", "value"),
[
(True, "more_like_this", '{"secret":"private-config-value"'),
(True, "more_like_this", '["private-config-value"]'),
(False, "model", '{"secret":"private-config-value"'),
(False, "model", '["private-config-value"]'),
(False, "model", '{"completion_params":["private-config-value"]}'),
(False, "model", None),
],
ids=["feature-json", "feature-shape", "model-json", "model-shape", "parameter-shape", "missing-model"],
)
def test_corrupt_persisted_configuration_returns_app_unavailable(
harness: _Harness, current: bool, field: str, value: str | None
) -> None:
"""None represents a missing model payload in an existing configuration row."""
with harness.factory.begin() as session:
config_id = (
harness.target.app_model_config_id
if current
else session.scalar(
select(Conversation.app_model_config_id).where(Conversation.id == harness.conversation.id)
)
)
session.execute(update(AppModelConfig).where(AppModelConfig.id == config_id).values({field: value}))
response = _get(harness)
_error(response, status=400, code="app_unavailable")
assert response.get_json()["message"] == "App unavailable, please check your app configurations."
assert b"private-config-value" not in response.data
@@ -0,0 +1,390 @@
"""Installed-app suggested questions over real HTTP admission and SQLite services."""
from collections.abc import Callable, Iterator
from dataclasses import dataclass, replace
from decimal import Decimal
from uuid import uuid4
import pytest
from flask import Blueprint, Flask
from redis import Redis
from sqlalchemy import Connection, Engine, delete, event, update
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN
from controllers.console.explore.message import MessageSuggestedQuestionApi
from controllers.console.wraps import _is_setup_completed
from enums import DeploymentEdition
from extensions.ext_application_services import build_application_services
from extensions.ext_database import db
from extensions.ext_login import DifyLoginManager, bind_account_loader, load_user_from_request, unauthorized_handler
from extensions.ext_redis import RedisClientWrapper
from graphon.model_runtime.entities.model_entities import ModelType
from libs.datetime_utils import naive_utc_now
from libs.external_api import ExternalApi
from libs.passport import PassportService
from libs.token import generate_csrf_token
from models import Account, Tenant
from models.account import TenantAccountJoin, TenantAccountRole
from models.enums import ConversationFromSource
from models.model import App, AppMode, AppModelConfig, Conversation, DifySetup, InstalledApp, Message
from models.provider import Provider, TenantDefaultModel
from models.workflow import Workflow
from repositories.installed_app_repository import SQLAlchemyInstalledAppRepository
from services.installed_app_access_service import InstalledAppAccessService
from tests.helpers.unit.model_factories import make_account, make_app, make_conversation, make_message
@dataclass(frozen=True)
class _Harness:
app: Flask
account: Account
target: App
installation: InstalledApp
conversation: Conversation
message: Message
factory: sessionmaker[Session]
admission_factory: sessionmaker[Session]
sessions: list[Session]
def get(self, *, installation_id: str | None = None, authenticated: bool = True, csrf: bool = True) -> TestResponse:
client = self.app.test_client()
token = generate_csrf_token(self.account.id)
headers = {HEADER_NAME_CSRF_TOKEN: token} if csrf else {}
if authenticated:
headers["Authorization"] = f"Bearer {PassportService().issue({'user_id': self.account.id})}"
client.set_cookie(COOKIE_NAME_CSRF_TOKEN, token)
return client.get(
f"/installed-apps/{installation_id or self.installation.id}/messages/{self.message.id}/suggested-questions",
headers=headers,
)
def assert_closed(self, *, caller: Session | None = None) -> None:
assert self.sessions
assert all(
not session.in_transaction() and not session.identity_map
for session in self.sessions
if session is not caller
)
@pytest.fixture
def harness(
_empty_model_provider_daemon: None,
config_overrides: Callable[..., None],
sqlite_engine: Engine,
sqlite_session_factory: sessionmaker[Session],
) -> Iterator[_Harness]:
config_overrides(
DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY,
SECRET_KEY="installed-suggested-questions-test-key",
LOGIN_DISABLED=False,
INIT_PASSWORD="",
CONSOLE_API_URL="http://localhost",
CONSOLE_WEB_URL="http://localhost",
COOKIE_DOMAIN="",
)
workspace = Tenant(name="Viewer workspace")
owner = Tenant(name="App owner workspace")
account = make_account(account_id=str(uuid4()))
account.last_active_at = naive_utc_now()
target = make_app(app_id=str(uuid4()), tenant_id=owner.id)
installation = InstalledApp(
tenant_id=workspace.id,
app_id=target.id,
app_owner_tenant_id=owner.id,
position=0,
is_pinned=False,
last_used_at=None,
)
config = AppModelConfig(app_id=target.id, suggested_questions_after_answer='{"enabled":true}')
conversation = make_conversation(
conversation_id=str(uuid4()),
app_id=target.id,
inputs={},
from_source=ConversationFromSource.CONSOLE,
from_account_id=account.id,
)
message = make_message(
message_id=str(uuid4()),
app_id=target.id,
conversation_id=conversation.id,
inputs={},
message={},
query="How?",
answer="Like this.",
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
currency="USD",
from_source=ConversationFromSource.CONSOLE,
from_account_id=account.id,
)
with sqlite_session_factory.begin() as session:
session.add_all(
[
workspace,
owner,
account,
target,
installation,
config,
conversation,
message,
TenantAccountJoin(
tenant_id=workspace.id, account_id=account.id, role=TenantAccountRole.OWNER, current=True
),
DifySetup(version="test"),
# Real provider ID validation fails before plugin discovery or model I/O.
Provider(tenant_id=owner.id, provider_name="invalid/provider", is_valid=True),
TenantDefaultModel(
tenant_id=owner.id,
provider_name="invalid/provider",
model_name="invalid-model",
model_type=ModelType.LLM,
),
]
)
session.flush()
target.app_model_config_id = config.id
conversation.app_model_config_id = config.id
factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
admission_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
app = Flask(__name__)
app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))
db.init_app(app)
login_manager = DifyLoginManager()
login_manager.request_loader(load_user_from_request)
login_manager.unauthorized_handler(unauthorized_handler)
login_manager.init_app(app)
redis_client = Redis()
redis = RedisClientWrapper()
redis.initialize(redis_client)
services = build_application_services(
database_client=factory,
deployment_edition=DeploymentEdition.COMMUNITY,
initialization_password="",
redis=redis,
)
# Give the installed-app query its own observable factory for stale-admission tests.
access = InstalledAppAccessService(
installed_apps=SQLAlchemyInstalledAppRepository(session_factory=admission_factory),
is_user_allowed=services.webapp_access.is_user_allowed,
get_access_modes=services.webapp_access.batch_get_access_modes,
get_user_permissions=services.webapp_access.batch_get_user_permissions,
)
app.extensions["application_services"] = replace(
services, installed_apps=replace(services.installed_apps, access=access)
)
bind_account_loader(app, services.accounts.identity.load_user)
blueprint = Blueprint("console", __name__)
api = ExternalApi(blueprint)
api.add_resource(
MessageSuggestedQuestionApi,
"/installed-apps/<uuid:installed_app_id>/messages/<uuid:message_id>/suggested-questions",
)
app.register_blueprint(blueprint)
_is_setup_completed.reset_success()
with app.app_context():
assert _is_setup_completed()
db.session.remove()
sessions: list[Session] = []
def track_session(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
sessions.append(session)
factories = (factory, admission_factory, sqlite_session_factory, db.session.session_factory)
for observed in factories:
event.listen(observed, "after_begin", track_session)
yield _Harness(app, account, target, installation, conversation, message, factory, admission_factory, sessions)
for observed in factories:
event.remove(observed, "after_begin", track_session)
assert redis_client.connection_pool._created_connections == 0
_is_setup_completed.reset_success()
redis_client.close()
with app.app_context():
db.session.remove()
db.engine.dispose()
def _error(response: TestResponse, *, status: int, code: str) -> None:
assert response.status_code == status
assert response.headers["Content-Type"] == "application/json"
assert int(response.headers["Content-Length"]) == len(response.data)
body = response.get_json()
assert body["status"] == status
assert body["code"] == code
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT])
def test_owner_provider_resolution_failure_returns_empty_questions(
harness: _Harness, mode: AppMode, caplog: pytest.LogCaptureFixture
) -> None:
assert harness.target.tenant_id != harness.installation.tenant_id
with harness.factory.begin() as session:
app = session.get(App, harness.target.id)
assert app is not None
app.mode = mode
if mode == AppMode.ADVANCED_CHAT:
workflow = Workflow.new(
tenant_id=app.tenant_id,
app_id=app.id,
type="chat",
version="published",
graph="{}",
features='{"suggested_questions_after_answer":{"enabled":true}}',
created_by=harness.account.id,
environment_variables=[],
conversation_variables=[],
rag_pipeline_variables=[],
)
session.add(workflow)
session.flush()
app.workflow_id = workflow.id
response = harness.get()
assert response.status_code == 200
assert response.get_json() == {"data": []}
failures = [
record.exc_info[1]
for record in caplog.records
if record.name == "services.message_suggested_questions_generator" and record.exc_info is not None
]
assert len(failures) == 1
assert isinstance(failures[0], ValueError)
assert str(failures[0]) == "Invalid provider: invalid/provider"
harness.assert_closed()
with harness.factory() as session:
installation = session.get(InstalledApp, harness.installation.id)
assert installation is not None
assert installation.last_used_at is None
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW, AppMode.AGENT])
def test_non_chat_modes_return_specific_error(harness: _Harness, mode: AppMode) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
_error(harness.get(), status=400, code="not_chat_app")
harness.assert_closed()
@pytest.mark.parametrize("missing", ["installation", "workspace", "app"])
def test_missing_or_foreign_installation_is_rejected(harness: _Harness, missing: str) -> None:
installation_id = harness.installation.id
if missing == "installation":
installation_id = str(uuid4())
else:
with harness.factory.begin() as session:
if missing == "workspace":
session.execute(
update(InstalledApp).where(InstalledApp.id == installation_id).values(tenant_id=str(uuid4()))
)
else:
session.execute(delete(App).where(App.id == harness.target.id))
_error(harness.get(installation_id=installation_id), status=404, code="installed_app_not_found")
harness.assert_closed()
@pytest.mark.parametrize("missing", ["login", "csrf"])
def test_real_login_and_csrf_are_required(harness: _Harness, missing: str) -> None:
response = harness.get(authenticated=missing != "login", csrf=missing != "csrf")
_error(response, status=401, code="unauthorized")
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
if missing == "login":
assert harness.sessions == []
else:
harness.assert_closed()
@pytest.mark.parametrize("model", [Message, Conversation])
@pytest.mark.parametrize("field", ["app_id", "from_account_id", "from_source"])
def test_history_requires_complete_account_ownership(
harness: _Harness, model: type[Message] | type[Conversation], field: str
) -> None:
record_id = harness.message.id if model is Message else harness.conversation.id
value = ConversationFromSource.API if field == "from_source" else str(uuid4())
with harness.factory.begin() as session:
session.execute(update(model).where(model.id == record_id).values({field: value}))
_error(harness.get(), status=404, code="message_not_found" if model is Message else "conversation_not_found")
harness.assert_closed()
@pytest.mark.parametrize("resource", ["message", "conversation", "disabled", "config"])
def test_missing_resources_and_feature_policy_keep_specific_errors(harness: _Harness, resource: str) -> None:
with harness.factory.begin() as session:
if resource == "message":
session.execute(delete(Message).where(Message.id == harness.message.id))
elif resource == "conversation":
session.execute(delete(Conversation).where(Conversation.id == harness.conversation.id))
elif resource == "disabled":
session.execute(
update(AppModelConfig)
.where(AppModelConfig.app_id == harness.target.id)
.values(suggested_questions_after_answer='{"enabled":false}')
)
else:
session.execute(delete(AppModelConfig).where(AppModelConfig.app_id == harness.target.id))
status, code = {
"message": (404, "message_not_found"),
"conversation": (404, "conversation_not_found"),
"disabled": (403, "app_suggested_questions_after_answer_disabled"),
"config": (500, "internal_server_error"),
}[resource]
_error(harness.get(), status=status, code=code)
harness.assert_closed()
@pytest.mark.parametrize("change", ["mode", "owner", "app", "account"])
def test_stale_admitted_resource_is_revalidated(harness: _Harness, change: str) -> None:
def change_resource(_session: Session, _transaction: SessionTransaction) -> None:
with harness.factory.begin() as session:
if change == "account":
session.execute(delete(Account).where(Account.id == harness.account.id))
elif change == "app":
session.execute(delete(App).where(App.id == harness.target.id))
else:
values = {"mode": AppMode.COMPLETION} if change == "mode" else {"tenant_id": str(uuid4())}
session.execute(update(App).where(App.id == harness.target.id).values(values))
event.listen(harness.admission_factory, "after_transaction_end", change_resource, once=True)
try:
response = harness.get()
finally:
event.remove(harness.admission_factory, "after_transaction_end", change_resource)
_error(
response,
status=401 if change == "account" else 400,
code="unauthorized" if change == "account" else "app_unavailable",
)
if change == "account":
assert response.get_json()["message"] == "Account no longer exists."
harness.assert_closed()
def test_removed_installation_takes_effect_on_next_request(harness: _Harness) -> None:
def remove_installation(_session: Session, _transaction: SessionTransaction) -> None:
with harness.factory.begin() as session:
session.execute(delete(InstalledApp).where(InstalledApp.id == harness.installation.id))
event.listen(harness.admission_factory, "after_transaction_end", remove_installation, once=True)
try:
response = harness.get()
finally:
event.remove(harness.admission_factory, "after_transaction_end", remove_installation)
assert response.status_code == 200
assert response.get_json() == {"data": []}
_error(harness.get(), status=404, code="installed_app_not_found")
harness.assert_closed()
def test_malformed_config_has_opaque_error(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(
update(AppModelConfig)
.where(AppModelConfig.app_id == harness.target.id)
.values(suggested_questions_after_answer="private config diagnostic")
)
response = harness.get()
_error(response, status=500, code="internal_server_error")
assert "private config diagnostic" not in response.get_data(as_text=True)
harness.assert_closed()
@@ -0,0 +1,435 @@
"""Trial suggested questions through HTTP, real ownership queries, and SQLite."""
from collections.abc import Callable, Iterator
from dataclasses import dataclass
from decimal import Decimal
from uuid import uuid4
import pytest
from flask import Flask
from sqlalchemy import Connection, Engine, delete, event, select, update
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
import controllers.console.explore.trial as trial_module
import controllers.console.wraps as console_wraps
import libs.login as login_module
from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN
from core.memory.token_buffer_memory import PreparedHistory
from enums import DeploymentEdition
from extensions.ext_database import db
from extensions.ext_login import DifyLoginManager, unauthorized_handler
from extensions.ext_redis import RedisClientWrapper
from graphon.model_runtime.entities.model_entities import ModelType
from libs.external_api import ExternalApi
from libs.token import generate_csrf_token
from models import Account, AccountTrialAppRecord, App, AppMode, Conversation, Message, Tenant, TrialApp
from models.account import AccountStatus
from models.agent import Agent, AgentScope, AgentSource, AgentStatus
from models.enums import ConversationFromSource
from models.model import AppModelConfig, DifySetup
from models.provider import Provider, TenantDefaultModel
from repositories.message_repository import MessageRepository
from repositories.recommended_app_catalog_repository import DatabaseRecommendedAppCatalogRepository
from repositories.trial_app_repository import TrialAppRepository
from services.message_suggested_questions_generator import SuggestedQuestionsGenerator
from services.message_suggested_questions_queries import SuggestedQuestionsQuery
from services.message_suggested_questions_service import (
MessageSuggestedQuestionsService,
)
from services.recommended_app_query_service import RecommendedAppQueryService
from services.trial_app_access_service import TrialAppAccessService
from tests.helpers.unit.services.tracing_ports import untraced_scopes
@dataclass(frozen=True)
class _TrialAppServices:
access: TrialAppAccessService
@dataclass(frozen=True)
class _ApplicationServices:
trial_apps: _TrialAppServices
recommended_app_queries: RecommendedAppQueryService
message_suggested_questions: MessageSuggestedQuestionsService[PreparedHistory]
@dataclass(frozen=True)
class _Harness:
app: Flask
account: Account
target: App
trial: TrialApp
conversation: Conversation
message: Message
config: AppModelConfig
factory: sessionmaker[Session]
features: RecommendedAppQueryService
read_sessions: list[Session]
legacy_sessions: list[Session]
def get(self, *, app_id: str | None = None, message_id: str | None = None) -> TestResponse:
client = self.app.test_client()
csrf = generate_csrf_token(self.account.id)
client.set_cookie(COOKIE_NAME_CSRF_TOKEN, csrf)
return client.get(
f"/trial-apps/{app_id or self.target.id}/messages/{message_id or self.message.id}/suggested-questions",
headers={HEADER_NAME_CSRF_TOKEN: csrf},
)
def assert_closed(self) -> None:
assert all(
not session.in_transaction() and not session.identity_map
for session in self.read_sessions + self.legacy_sessions
)
def usage(self) -> int | None:
with self.factory() as session:
return session.scalar(
select(AccountTrialAppRecord.count).where(
AccountTrialAppRecord.app_id == self.target.id,
AccountTrialAppRecord.account_id == self.account.id,
)
)
@pytest.fixture
def harness(
_empty_model_provider_daemon: None,
monkeypatch: pytest.MonkeyPatch,
config_overrides: Callable[..., None],
sqlite_session_factory: sessionmaker[Session],
sqlite_engine: Engine,
) -> Iterator[_Harness]:
config_overrides(
DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY,
INIT_PASSWORD="",
LOGIN_DISABLED=False,
RBAC_ENABLED=False,
SECRET_KEY="trial-suggested-question-test-secret",
CONSOLE_WEB_URL="http://localhost",
CONSOLE_API_URL="http://localhost",
COOKIE_DOMAIN="",
)
console_wraps._is_setup_completed.reset_success()
account = Account(name="Trial viewer", email="trial@example.com")
account._current_tenant = Tenant(name="Viewer workspace")
target = App(tenant_id=str(uuid4()), name="Trial app", mode=AppMode.CHAT, enable_site=True, enable_api=False)
target.id = str(uuid4())
trial = TrialApp(app_id=target.id, tenant_id=str(uuid4()), trial_limit=3)
config = AppModelConfig(
app_id=target.id,
suggested_questions_after_answer='{"enabled":true,"prompt":"Suggest concise follow-ups"}',
)
with sqlite_session_factory.begin() as session:
session.add_all(
[
account,
target,
trial,
config,
DifySetup(version="test"),
Provider(tenant_id=target.tenant_id, provider_name="invalid/provider", is_valid=True),
TenantDefaultModel(
tenant_id=target.tenant_id,
provider_name="invalid/provider",
model_name="invalid-model",
model_type=ModelType.LLM,
),
]
)
session.flush()
target.app_model_config_id = config.id
conversation = Conversation(
app_id=target.id,
app_model_config_id=config.id,
mode=AppMode.CHAT,
name="Trial conversation",
_inputs={},
from_source=ConversationFromSource.CONSOLE,
from_account_id=account.id,
)
session.add(conversation)
session.flush()
message = Message(
app_id=target.id,
conversation_id=conversation.id,
_inputs={},
query="What is a trial?",
message={},
message_unit_price=Decimal(0),
answer="A way to try an app.",
answer_unit_price=Decimal(0),
currency="USD",
from_source=ConversationFromSource.CONSOLE,
from_account_id=account.id,
)
session.add(message)
read_sessions: list[Session] = []
read_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
@event.listens_for(read_factory, "after_begin")
def track_read(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
read_sessions.append(session)
trial_apps = TrialAppRepository(session_factory=read_factory)
features = RecommendedAppQueryService(
catalog=DatabaseRecommendedAppCatalogRepository(read_factory, redis=RedisClientWrapper()),
trial_apps=trial_apps,
trial_enabled=True,
)
queries = SuggestedQuestionsQuery(
session_factory=read_factory, repository=MessageRepository(session_factory=read_factory)
)
services = _ApplicationServices(
trial_apps=_TrialAppServices(access=TrialAppAccessService(apps=trial_apps)),
recommended_app_queries=features,
message_suggested_questions=MessageSuggestedQuestionsService(
queries=queries, generator=SuggestedQuestionsGenerator(trace_scopes=untraced_scopes())
),
)
monkeypatch.setattr(login_module, "current_user", account)
app = Flask(__name__)
app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))
db.init_app(app)
app.extensions["application_services"] = services
legacy_sessions: list[Session] = []
def track_legacy(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
legacy_sessions.append(session)
event.listen(db.session.session_factory, "after_begin", track_legacy)
login_manager = DifyLoginManager()
login_manager.init_app(app)
login_manager.unauthorized_handler(unauthorized_handler)
api = ExternalApi(app)
api.add_resource(
trial_module.TrialMessageSuggestedQuestionApi,
"/trial-apps/<uuid:app_id>/messages/<uuid:message_id>/suggested-questions",
)
yield _Harness(
app,
account,
target,
trial,
conversation,
message,
config,
sqlite_session_factory,
features,
read_sessions,
legacy_sessions,
)
event.remove(db.session.session_factory, "after_begin", track_legacy)
console_wraps._is_setup_completed.reset_success()
with app.app_context():
db.session.remove()
db.engine.dispose()
def _assert_error(response: TestResponse, status: int, code: str, message: str | None = None) -> None:
assert response.status_code == status, response.get_json()
body = response.get_json()
assert body["code"] == code
assert body["status"] == status
assert body["message"]
if message is not None:
assert body["message"] == message
assert response.headers["Content-Type"] == "application/json"
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.AGENT])
def test_model_resolution_failure_keeps_empty_response_and_usage(
harness: _Harness, mode: AppMode, caplog: pytest.LogCaptureFixture
) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
session.execute(update(Conversation).where(Conversation.id == harness.conversation.id).values(mode=mode))
session.add_all(
[
AccountTrialAppRecord(app_id=harness.target.id, account_id=harness.account.id, count=1),
AccountTrialAppRecord(app_id=harness.target.id, account_id=str(uuid4()), count=3),
]
)
response = harness.get()
assert response.status_code == 200, response.get_json()
assert response.get_json() == {"data": []}
assert response.headers["Content-Type"] == "application/json"
assert int(response.headers["Content-Length"]) == len(response.data)
assert any(
record.exc_info
and isinstance(record.exc_info[1], ValueError)
and "Invalid provider: invalid/provider" in str(record.exc_info[1])
for record in caplog.records
)
assert harness.target.tenant_id not in {harness.trial.tenant_id, harness.account.current_tenant_id}
assert harness.usage() == 1
harness.assert_closed()
def test_missing_published_agent_version_has_specific_error(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=AppMode.AGENT))
session.add(
Agent(
tenant_id=harness.target.tenant_id,
app_id=harness.target.id,
name="Trial Agent",
scope=AgentScope.ROSTER,
source=AgentSource.AGENT_APP,
status=AgentStatus.ACTIVE,
)
)
_assert_error(harness.get(), 404, "agent_version_not_found_error", "Agent config version not found.")
assert harness.usage() is None
harness.assert_closed()
@pytest.mark.parametrize("entity", ["message", "conversation"])
@pytest.mark.parametrize("mismatch", ["missing", "app", "account", "source", "end-user"])
def test_message_and_conversation_require_complete_owner_chain(harness: _Harness, entity: str, mismatch: str) -> None:
message_id = harness.message.id
with harness.factory.begin() as session:
record = (
session.get(Message, harness.message.id)
if entity == "message"
else session.get(Conversation, harness.conversation.id)
)
assert record is not None
match mismatch:
case "missing":
if entity == "message":
message_id = str(uuid4())
else:
session.delete(record)
case "app":
record.app_id = str(uuid4())
case "account":
record.from_account_id = str(uuid4())
case "source":
record.from_source = ConversationFromSource.API
case "end-user":
record.from_end_user_id = str(uuid4())
response = harness.get(message_id=message_id)
_assert_error(response, 404, "not_found", "Message not found" if entity == "message" else "Conversation not found")
assert harness.usage() is None
harness.assert_closed()
def test_deleted_conversation_is_not_visible(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(update(Conversation).where(Conversation.id == harness.conversation.id).values(is_deleted=True))
_assert_error(harness.get(), 404, "not_found", "Conversation not found")
harness.assert_closed()
@pytest.mark.parametrize("config", [None, '{"enabled":false}'])
def test_disabled_or_unset_suggestions_have_specific_error(harness: _Harness, config: str | None) -> None:
with harness.factory.begin() as session:
session.execute(
update(AppModelConfig)
.where(AppModelConfig.id == harness.config.id)
.values(suggested_questions_after_answer=config)
)
_assert_error(harness.get(), 403, "app_suggested_questions_after_answer_disabled")
harness.assert_closed()
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
def test_unsupported_app_modes_fail_before_runtime(harness: _Harness, mode: AppMode) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
_assert_error(harness.get(), 400, "not_chat_app")
assert len(harness.read_sessions) == 1
harness.assert_closed()
@pytest.mark.parametrize(
("denial", "status", "code"),
[
("setup", 401, "not_setup"),
("feature", 403, "trial_app_feature_disabled"),
("missing_trial", 403, "trial_app_not_allowed"),
("missing_app", 403, "trial_app_not_allowed"),
("quota", 403, "trial_app_limit_exceeded"),
("uninitialized", 400, "account_not_initialized"),
("unauthenticated", 401, "unauthorized"),
],
)
def test_admission_blocks_before_runtime(
harness: _Harness, monkeypatch: pytest.MonkeyPatch, denial: str, status: int, code: str
) -> None:
if denial == "setup":
with harness.factory.begin() as session:
session.execute(delete(DifySetup))
console_wraps._is_setup_completed.reset_success()
elif denial == "feature":
harness.features._trial_enabled = False
elif denial == "uninitialized":
harness.account.status = AccountStatus.UNINITIALIZED
elif denial == "unauthenticated":
monkeypatch.setattr(login_module, "current_user", None)
else:
with harness.factory.begin() as session:
if denial == "missing_trial":
session.execute(delete(TrialApp).where(TrialApp.app_id == harness.target.id))
elif denial == "missing_app":
session.execute(delete(App).where(App.id == harness.target.id))
else:
session.add(AccountTrialAppRecord(app_id=harness.target.id, account_id=harness.account.id, count=3))
response = harness.get()
if denial == "unauthenticated":
assert response.status_code == 401
assert response.get_json() == {"code": "unauthorized", "message": "Unauthorized.", "status": 401}
else:
_assert_error(response, status, code)
harness.assert_closed()
def test_model_resolution_preserves_outer_request_session(harness: _Harness) -> None:
with harness.app.app_context():
# Bootstrap uses the scoped session; complete that real query before the caller edits its entity.
assert console_wraps._is_setup_completed()
db.session.remove()
outer_session = db.session()
app_model = outer_session.get(App, harness.target.id)
assert app_model is not None
app_model.name = "Pending caller change"
response = harness.get()
assert response.status_code == 200
assert db.session() is outer_session
assert outer_session.in_transaction()
assert app_model in outer_session.dirty
assert app_model.name == "Pending caller change"
harness.assert_closed()
with harness.factory() as session:
saved_app = session.get(App, harness.target.id)
assert saved_app is not None
assert saved_app.name == "Trial app"
def test_advanced_chat_without_published_workflow_keeps_empty_success(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=AppMode.ADVANCED_CHAT))
response = harness.get()
assert response.status_code == 200
assert response.get_json() == {"data": []}
assert len(harness.read_sessions) == 2
assert harness.usage() is None
harness.assert_closed()
def test_deleted_account_has_explicit_unauthorized_response(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(delete(Account).where(Account.id == harness.account.id))
response = harness.get()
_assert_error(response, 401, "unauthorized", "Account no longer exists.")
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
assert harness.usage() is None
harness.assert_closed()
@@ -0,0 +1,311 @@
"""Feedback HTTP writes through real Console admission, application services and SQLite."""
from collections.abc import Iterator
from http import HTTPStatus
from typing import Literal
from uuid import uuid4
import pytest
from sqlalchemy import delete, event, func, select, update
from sqlalchemy.orm import Session, SessionTransaction
from werkzeug.test import TestResponse
from constants import COOKIE_NAME_CSRF_TOKEN, HEADER_NAME_CSRF_TOKEN
from controllers.console.app.message import AgentMessageFeedbackApi, MessageFeedbackApi
from controllers.console.explore.message import MessageFeedbackApi as InstalledMessageFeedbackApi
from libs.external_api import ExternalApi
from libs.passport import PassportService
from libs.token import generate_csrf_token
from models.agent import Agent, AgentScope, AgentSource, AgentStatus
from models.enums import ConversationFromSource, FeedbackFromSource, FeedbackRating
from models.model import App, AppMode, InstalledApp, Message, MessageFeedback
from tests.e2e_tests.contracts.controllers.console.explore.test_message_suggested_questions import _Harness
from tests.e2e_tests.contracts.controllers.console.explore.test_message_suggested_questions import (
harness as installed_app_harness,
)
__all__ = ["installed_app_harness"]
@pytest.fixture
def harness(installed_app_harness: _Harness) -> Iterator[_Harness]:
state = installed_app_harness
api = ExternalApi(state.app)
api.add_resource(MessageFeedbackApi, "/apps/<uuid:app_id>/feedbacks", endpoint="console.app_feedback")
api.add_resource(AgentMessageFeedbackApi, "/agent/<uuid:agent_id>/feedbacks", endpoint="console.agent_feedback")
api.add_resource(
InstalledMessageFeedbackApi,
"/installed-apps/<uuid:installed_app_id>/messages/<uuid:message_id>/feedbacks",
endpoint="console.installed_feedback",
)
yield state
if state.sessions:
state.assert_closed()
with state.factory() as session:
installation = session.get(InstalledApp, state.installation.id)
if installation is not None:
assert installation.last_used_at is None
def _post(
state: _Harness,
*,
body: dict[str, object],
route: str | None = None,
installation_id: str | None = None,
message_id: str | None = None,
authenticated: bool = True,
csrf: bool = True,
) -> TestResponse:
client = state.app.test_client()
token = generate_csrf_token(state.account.id)
headers = {HEADER_NAME_CSRF_TOKEN: token} if csrf else {}
if authenticated:
headers["Authorization"] = f"Bearer {PassportService().issue({'user_id': state.account.id})}"
client.set_cookie(COOKIE_NAME_CSRF_TOKEN, token)
if route is None:
route = (
f"/installed-apps/{installation_id or state.installation.id}"
f"/messages/{message_id or state.message.id}/feedbacks"
)
else:
body = {"message_id": message_id or state.message.id, **body}
return client.post(route, headers=headers, json=body)
def _success(response: TestResponse) -> None:
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"result": "success"}
assert response.headers["Content-Type"] == "application/json"
assert int(response.headers["Content-Length"]) == len(response.data)
def _error(response: TestResponse, *, status: HTTPStatus, code: str) -> None:
assert response.status_code == status
assert response.headers["Content-Type"] == "application/json"
assert int(response.headers["Content-Length"]) == len(response.data)
payload = response.get_json()
assert payload["status"] == status
assert payload["code"] == code
assert payload["message"]
def _admin_route(state: _Harness, kind: Literal["app", "agent"], *, scope: AgentScope = AgentScope.ROSTER) -> str:
"""Make the admitted account an app owner while retaining a distinct installation fixture."""
with state.factory.begin() as session:
session.execute(update(App).where(App.id == state.target.id).values(tenant_id=state.installation.tenant_id))
if kind == "agent":
agent_id = str(uuid4())
session.execute(update(App).where(App.id == state.target.id).values(mode=AppMode.AGENT))
session.add(
Agent(
id=agent_id,
tenant_id=state.installation.tenant_id,
name="Feedback agent",
scope=scope,
source=AgentSource.AGENT_APP if scope == AgentScope.ROSTER else AgentSource.WORKFLOW,
status=AgentStatus.ACTIVE,
app_id=state.target.id if scope == AgentScope.ROSTER else str(uuid4()),
backing_app_id=state.target.id if scope == AgentScope.WORKFLOW_ONLY else None,
workflow_id=str(uuid4()) if scope == AgentScope.WORKFLOW_ONLY else None,
workflow_node_id="node" if scope == AgentScope.WORKFLOW_ONLY else None,
)
)
return f"/agent/{agent_id}/feedbacks"
return f"/apps/{state.target.id}/feedbacks"
def test_explore_feedback_create_update_revoke_with_cross_workspace_app(harness: _Harness) -> None:
assert harness.target.tenant_id != harness.installation.tenant_id
feedback_id: str | None = None
for rating, content in (("like", ""), ("dislike", "Changed my mind")):
_success(_post(harness, body={"rating": rating, "content": content}))
with harness.factory() as session:
rows = session.scalars(select(MessageFeedback)).all()
assert len(rows) == 1
feedback = rows[0]
assert feedback_id is None or feedback.id == feedback_id
feedback_id = feedback.id
assert feedback.app_id == harness.target.id
assert feedback.message_id == harness.message.id
assert feedback.conversation_id == harness.conversation.id
assert feedback.rating.value == rating
assert feedback.content == content
assert feedback.from_source == FeedbackFromSource.ADMIN
assert feedback.from_account_id == harness.account.id
assert feedback.from_end_user_id is None
_success(_post(harness, body={"rating": None}))
with harness.factory() as session:
assert session.scalars(select(MessageFeedback)).all() == []
@pytest.mark.parametrize("owner", ["app_id", "from_account_id", "from_source", "from_end_user_id", "missing"])
def test_explore_requires_complete_message_ownership(harness: _Harness, owner: str) -> None:
with harness.factory.begin() as session:
if owner == "missing":
session.execute(delete(Message).where(Message.id == harness.message.id))
else:
value = ConversationFromSource.API if owner == "from_source" else str(uuid4())
session.execute(update(Message).where(Message.id == harness.message.id).values({owner: value}))
_error(_post(harness, body={"rating": "like"}), status=HTTPStatus.NOT_FOUND, code="message_not_found")
with harness.factory() as session:
assert session.scalars(select(MessageFeedback)).all() == []
@pytest.mark.parametrize("mode", list(AppMode))
def test_explore_feedback_accepts_all_app_modes(harness: _Harness, mode: AppMode) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
_success(_post(harness, body={"rating": "like"}))
@pytest.mark.parametrize("admission", ["missing", "workspace", "app"])
def test_explore_rejects_unavailable_installation(harness: _Harness, admission: str) -> None:
with harness.factory.begin() as session:
if admission == "workspace":
session.execute(
update(InstalledApp).where(InstalledApp.id == harness.installation.id).values(tenant_id=str(uuid4()))
)
elif admission == "app":
session.execute(delete(App).where(App.id == harness.target.id))
_error(
_post(harness, body={"rating": "like"}, installation_id=str(uuid4()) if admission == "missing" else None),
status=HTTPStatus.NOT_FOUND,
code="installed_app_not_found",
)
def test_installation_removed_after_admission_cannot_receive_feedback(harness: _Harness) -> None:
def remove_installation(_session: Session, _transaction: SessionTransaction) -> None:
with harness.factory.begin() as session:
session.execute(delete(InstalledApp).where(InstalledApp.id == harness.installation.id))
event.listen(harness.admission_factory, "after_transaction_end", remove_installation, once=True)
try:
response = _post(harness, body={"rating": "like"})
finally:
event.remove(harness.admission_factory, "after_transaction_end", remove_installation)
_error(response, status=HTTPStatus.NOT_FOUND, code="installed_app_not_found")
with harness.factory() as session:
assert session.scalars(select(MessageFeedback)).all() == []
@pytest.mark.parametrize("kind", ["app", "agent"])
@pytest.mark.parametrize("owner", ["another_account", "end_user"])
def test_admin_can_rate_other_actors_messages_in_the_admitted_app(
harness: _Harness, kind: Literal["app", "agent"], owner: str
) -> None:
route = _admin_route(harness, kind)
with harness.factory.begin() as session:
message = session.get(Message, harness.message.id)
assert message is not None
message.from_account_id = str(uuid4()) if owner == "another_account" else None
message.from_end_user_id = str(uuid4()) if owner == "end_user" else None
message.from_source = ConversationFromSource.API if owner == "end_user" else ConversationFromSource.CONSOLE
session.add(
MessageFeedback(
app_id=message.app_id,
conversation_id=message.conversation_id,
message_id=message.id,
rating=FeedbackRating.LIKE,
from_source=FeedbackFromSource.USER,
from_end_user_id=message.from_end_user_id,
content="User feedback stays separate",
)
)
for rating, content in (("like", ""), ("dislike", None)):
_success(_post(harness, route=route, body={"rating": rating, "content": content}))
with harness.factory() as session:
feedback = session.scalar(
select(MessageFeedback).where(MessageFeedback.from_source == FeedbackFromSource.ADMIN)
)
assert feedback is not None
assert feedback.rating.value == rating
assert feedback.content == content
assert feedback.from_account_id == harness.account.id
assert feedback.from_end_user_id is None
_success(_post(harness, route=route, body={"rating": None}))
with harness.factory() as session:
rows = session.scalars(select(MessageFeedback)).all()
assert len(rows) == 1
assert rows[0].from_source == FeedbackFromSource.USER
assert rows[0].content == "User feedback stays separate"
@pytest.mark.parametrize("scope", [AgentScope.ROSTER, AgentScope.WORKFLOW_ONLY])
def test_agent_feedback_resolves_existing_runtime_app(harness: _Harness, scope: AgentScope) -> None:
route = _admin_route(harness, "agent", scope=scope)
_success(_post(harness, route=route, body={"rating": "like"}))
def test_agent_feedback_does_not_create_a_missing_hidden_runtime_app(harness: _Harness) -> None:
route = _admin_route(harness, "agent", scope=AgentScope.WORKFLOW_ONLY)
with harness.factory.begin() as session:
session.execute(update(Agent).values(backing_app_id=None))
app_count = session.scalar(select(func.count()).select_from(App))
_error(
_post(harness, route=route, body={"rating": "like"}), status=HTTPStatus.NOT_FOUND, code="agent_not_found_error"
)
with harness.factory() as session:
assert session.scalar(select(func.count()).select_from(App)) == app_count
assert session.scalar(select(Agent.backing_app_id)) is None
def test_plain_app_feedback_cannot_address_an_agents_hidden_runtime_app(harness: _Harness) -> None:
_admin_route(harness, "agent", scope=AgentScope.WORKFLOW_ONLY)
_error(
_post(harness, route=f"/apps/{harness.target.id}/feedbacks", body={"rating": "like"}),
status=HTTPStatus.NOT_FOUND,
code="app_not_found",
)
@pytest.mark.parametrize("kind", ["app", "agent"])
@pytest.mark.parametrize("missing", ["message", "message_app", "app_workspace", "app"])
def test_admin_feedback_still_requires_the_admitted_app(
harness: _Harness, kind: Literal["app", "agent"], missing: str
) -> None:
route = _admin_route(harness, kind)
with harness.factory.begin() as session:
if missing == "message":
session.execute(delete(Message).where(Message.id == harness.message.id))
elif missing == "message_app":
session.execute(update(Message).where(Message.id == harness.message.id).values(app_id=str(uuid4())))
elif missing == "app_workspace":
session.execute(update(App).where(App.id == harness.target.id).values(tenant_id=str(uuid4())))
else:
session.execute(delete(App).where(App.id == harness.target.id))
code = (
"not_found" if missing.startswith("message") else "app_not_found" if kind == "app" else "agent_not_found_error"
)
_error(_post(harness, route=route, body={"rating": "like"}), status=HTTPStatus.NOT_FOUND, code=code)
with harness.factory() as session:
assert session.scalars(select(MessageFeedback)).all() == []
@pytest.mark.parametrize("kind", ["explore", "app", "agent"])
@pytest.mark.parametrize("body", [{"rating": "invalid"}, {"content": 123}])
def test_invalid_feedback_payload_is_rejected(harness: _Harness, kind: str, body: dict[str, object]) -> None:
route = None if kind == "explore" else _admin_route(harness, "app" if kind == "app" else "agent")
_error(_post(harness, route=route, body=body), status=HTTPStatus.UNPROCESSABLE_ENTITY, code="unprocessable_entity")
@pytest.mark.parametrize("kind", ["explore", "app", "agent"])
def test_revoke_without_feedback_has_specific_error(harness: _Harness, kind: str) -> None:
route = None if kind == "explore" else _admin_route(harness, "app" if kind == "app" else "agent")
_error(
_post(harness, route=route, body={"rating": None}),
status=HTTPStatus.BAD_REQUEST,
code="message_feedback_rating_required",
)
@pytest.mark.parametrize("kind", ["explore", "app", "agent"])
@pytest.mark.parametrize("missing", ["login", "csrf"])
def test_feedback_requires_real_login_and_csrf(harness: _Harness, kind: str, missing: str) -> None:
route = None if kind == "explore" else _admin_route(harness, "app" if kind == "app" else "agent")
response = _post(
harness, route=route, body={"rating": "like"}, authenticated=missing != "login", csrf=missing != "csrf"
)
_error(response, status=HTTPStatus.UNAUTHORIZED, code="unauthorized")
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
@@ -0,0 +1,653 @@
"""Service API annotation contracts through real admission, SQLite and application wiring."""
import logging
from collections.abc import Callable, Iterator
from dataclasses import dataclass
from datetime import datetime, timedelta
from io import StringIO
from typing import Literal
from uuid import uuid4
import pytest
from flask import Blueprint, Flask, Response
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from redis import Redis
from sqlalchemy import Connection, Engine, delete, event, select, update
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
from controllers.service_api.app import annotation as annotation_module
from core.logging.context import clear_request_context
from core.logging.filters import IdentityContextFilter
from enums import DeploymentEdition
from enums.account import TenantAccountRole
from extensions.ext_application_services import build_application_services
from extensions.ext_redis import RedisClientWrapper
from extensions.otel.runtime import on_user_loaded, set_identity_span_attributes
from extensions.otel.semconv import DifySpanAttributes, GenAIAttributes
from libs.external_api import ExternalApi
from models import Account, App, EndUser, Tenant
from models.account import AccountStatus, TenantAccountJoin, TenantStatus
from models.dataset import Dataset
from models.enums import EndUserType
from models.model import AppAnnotationHitHistory, MessageAnnotation
from models.resource_access_token import (
ResourceAccessToken,
ResourceAccessTokenRelation,
ResourceAccessTokenResourceType,
)
from tests.helpers.unit.model_factories import make_account, make_app
type _Operation = Literal["list", "create", "update", "delete", "reply", "status"]
_OPERATIONS: tuple[_Operation, ...] = ("list", "create", "update", "delete", "reply", "status")
_TIME = datetime(2026, 1, 2, 3, 4, 5)
_REPLY_PAYLOAD: dict[str, object] = {
"score_threshold": 0.5,
"embedding_provider_name": "openai",
"embedding_model_name": "embedding",
}
@dataclass(frozen=True)
class _Harness:
app: Flask
account: Account
target: App
annotation: MessageAnnotation
token: ResourceAccessToken
factory: sessionmaker[Session]
sessions: list[Session]
def request(
self,
operation: _Operation,
*,
payload: dict[str, object] | None = None,
annotation_id: str | None = None,
action: str = "invalid",
query: dict[str, object] | None = None,
authorization: str | None = "default",
requested_app_id: str | None = None,
) -> TestResponse:
routes = {
"list": ("GET", "/v1/apps/annotations"),
"create": ("POST", "/v1/apps/annotations"),
"update": ("PUT", f"/v1/apps/annotations/{annotation_id or self.annotation.id}"),
"delete": ("DELETE", f"/v1/apps/annotations/{annotation_id or self.annotation.id}"),
"reply": ("POST", f"/v1/apps/annotation-reply/{action}"),
"status": ("GET", f"/v1/apps/annotation-reply/{action}/status/{uuid4()}"),
}
method, path = routes[operation]
headers = {}
if authorization is not None:
headers["Authorization"] = f"Bearer {self.token.token}" if authorization == "default" else authorization
if requested_app_id is not None:
headers["X-Dify-App-ID"] = requested_app_id
response = self.app.test_client().open(path, method=method, json=payload, query_string=query, headers=headers)
assert all(not session.in_transaction() and not session.identity_map for session in self.sessions)
if response.status_code == 204:
assert response.data == b""
else:
assert response.headers["Content-Type"] == "application/json"
assert int(response.headers["Content-Length"]) == len(response.data)
return response
@pytest.fixture
def harness(
config_overrides: Callable[..., None], sqlite_engine: Engine, sqlite_session_factory: sessionmaker[Session]
) -> Iterator[_Harness]:
clear_request_context()
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY, RBAC_ENABLED=False)
tenant = Tenant(name="Service API annotation workspace")
account = make_account(account_id=str(uuid4()))
target = make_app(app_id=str(uuid4()), tenant_id=tenant.id)
annotation = MessageAnnotation(
app_id=target.id, question="First question", content="First answer", account_id=account.id
)
annotation.created_at = _TIME
annotation.hit_count = 3
token = ResourceAccessToken(
id=str(uuid4()),
tenant_id=tenant.id,
name="Annotation integration",
track_id=uuid4().hex,
token=f"sk-{uuid4()}",
created_by=account.id,
)
with sqlite_session_factory.begin() as session:
session.add_all(
[
tenant,
account,
target,
annotation,
token,
TenantAccountJoin(tenant_id=tenant.id, account_id=account.id, role=TenantAccountRole.OWNER),
ResourceAccessTokenRelation(
token_id=token.id, resource_type=ResourceAccessTokenResourceType.APP, app_id=target.id
),
]
)
factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
app = Flask(__name__)
app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False)
client = Redis()
redis = RedisClientWrapper()
redis.initialize(client)
app.extensions["application_services"] = build_application_services(
database_client=factory,
deployment_edition=DeploymentEdition.COMMUNITY,
initialization_password="",
redis=redis,
)
blueprint = Blueprint("service_api_annotations", __name__, url_prefix="/v1")
api = ExternalApi(blueprint)
api.add_resource(annotation_module.AnnotationListApi, "/apps/annotations")
api.add_resource(annotation_module.AnnotationUpdateDeleteApi, "/apps/annotations/<uuid:annotation_id>")
api.add_resource(annotation_module.AnnotationReplyActionApi, "/apps/annotation-reply/<string:action>")
api.add_resource(
annotation_module.AnnotationReplyActionStatusApi, "/apps/annotation-reply/<string:action>/status/<uuid:job_id>"
)
app.register_blueprint(blueprint)
sessions: list[Session] = []
def track(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
sessions.append(session)
event.listen(factory, "after_begin", track)
try:
yield _Harness(app, account, target, annotation, token, factory, sessions)
finally:
event.remove(factory, "after_begin", track)
assert client.connection_pool._created_connections == 0
client.close()
clear_request_context()
def _error(response: TestResponse, *, status: int, code: str, message: str | None = None) -> None:
assert response.status_code == status
body = response.get_json()
assert body["code"] == code
assert body["status"] == status
if message is not None:
assert body["message"] == message
if status == 401:
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
def _annotation(harness: _Harness, index: int, *, app_id: str | None = None) -> MessageAnnotation:
annotation = MessageAnnotation(
app_id=app_id or harness.target.id,
question=f"Question {index}",
content=f"Answer {index}",
account_id=harness.account.id,
)
annotation.created_at = _TIME + timedelta(seconds=index)
return annotation
def test_list_preserves_shape_order_pagination_and_app_scope(harness: _Harness) -> None:
second = _annotation(harness, 1)
other = make_app(app_id=str(uuid4()), tenant_id=harness.target.tenant_id)
foreign = make_app(app_id=str(uuid4()), tenant_id=str(uuid4()))
with harness.factory.begin() as session:
session.add_all(
[
second,
other,
foreign,
_annotation(harness, 2, app_id=other.id),
_annotation(harness, 3, app_id=foreign.id),
]
)
response = harness.request("list", query={"limit": 1})
assert response.status_code == 200
assert response.get_json() == {
"data": [
{
"id": second.id,
"question": "Question 1",
"answer": "Answer 1",
"hit_count": 0,
"created_at": int(second.created_at.timestamp()),
}
],
"has_more": True,
"limit": 1,
"total": 2,
"page": 1,
}
last = harness.request("list", query={"limit": 1, "page": 2}).get_json()
assert last["data"][0]["id"] == harness.annotation.id
assert last["has_more"] is False
assert harness.request("list", query={"limit": 1, "page": 3}).get_json() == {
"data": [],
"has_more": False,
"limit": 1,
"total": 2,
"page": 3,
}
def test_list_caps_limit_at_100(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.add_all(_annotation(harness, index) for index in range(1, 102))
body = harness.request("list", query={"limit": 1000}).get_json()
assert (body["page"], body["limit"], body["total"], body["has_more"]) == (1, 100, 102, True)
assert len(body["data"]) == 100
def test_keyword_escapes_wildcards_and_searches_question_and_answer(harness: _Harness) -> None:
question, answer, decoy = (_annotation(harness, index) for index in range(1, 4))
question.question = "50%_Case\\ marker"
answer.content = "Answer has 50%_case\\ marker"
decoy.question = "500XCase marker"
with harness.factory.begin() as session:
session.add_all([question, answer, decoy])
body = harness.request("list", query={"keyword": "50%_case\\"}).get_json()
assert body["total"] == 2
assert [row["id"] for row in body["data"]] == [answer.id, question.id]
@pytest.mark.parametrize("field", ["page", "limit"])
@pytest.mark.parametrize("value", ["abc", "1.5", "1e2", "", "0", "-1"])
def test_list_rejects_invalid_pagination(harness: _Harness, field: str, value: str) -> None:
_error(harness.request("list", query={field: value}), status=422, code="unprocessable_entity")
@pytest.mark.parametrize(("question", "answer"), [("什么是人工智能?", "AI & ML <b>100%</b>"), ("", "")])
def test_create_persists_owner_and_preserves_empty_strings(harness: _Harness, question: str, answer: str) -> None:
response = harness.request("create", payload={"question": question, "answer": answer})
assert response.status_code == 201
body = response.get_json()
with harness.factory() as session:
stored = session.get(MessageAnnotation, body["id"])
assert stored is not None
assert (stored.app_id, stored.account_id, stored.conversation_id, stored.message_id) == (
harness.target.id,
harness.account.id,
None,
None,
)
assert body == {
"id": stored.id,
"question": question,
"answer": answer,
"hit_count": 0,
"created_at": int(stored.created_at.timestamp()),
}
assert (stored.question, stored.content) == (question, answer)
@pytest.mark.parametrize(("question", "answer"), [("Updated question", "Updated answer"), ("", "")])
def test_update_preserves_annotation_identity_and_hit_count(harness: _Harness, question: str, answer: str) -> None:
response = harness.request("update", payload={"question": question, "answer": answer})
assert response.status_code == 200
assert response.get_json() == {
"id": harness.annotation.id,
"question": question,
"answer": answer,
"hit_count": 3,
"created_at": int(_TIME.timestamp()),
}
with harness.factory() as session:
stored = session.get(MessageAnnotation, harness.annotation.id)
assert stored is not None
assert (stored.question, stored.content, stored.account_id) == (question, answer, harness.account.id)
@pytest.mark.parametrize("operation", ["create", "update"])
@pytest.mark.parametrize(
"payload",
[{}, {"question": "Q"}, {"answer": "A"}, {"question": None, "answer": "A"}, {"question": "Q", "answer": 42}],
)
def test_writes_validate_payload_without_changing_annotation(
harness: _Harness, operation: _Operation, payload: dict[str, object]
) -> None:
_error(harness.request(operation, payload=payload), status=422, code="unprocessable_entity")
with harness.factory() as session:
stored = session.scalars(select(MessageAnnotation)).one()
assert (stored.question, stored.content) == ("First question", "First answer")
def test_delete_removes_annotation_and_only_its_owned_hit_histories(harness: _Harness) -> None:
histories = [
AppAnnotationHitHistory(
app_id=app_id,
annotation_id=annotation_id,
source="hit-testing",
question="Asked",
account_id=harness.account.id,
score=0.9,
message_id=str(uuid4()),
annotation_question="Matched",
annotation_content="Responded",
)
for app_id, annotation_id in [
(harness.target.id, harness.annotation.id),
(str(uuid4()), harness.annotation.id),
(harness.target.id, str(uuid4())),
]
]
with harness.factory.begin() as session:
session.add_all(histories)
assert harness.request("delete").status_code == 204
with harness.factory() as session:
assert session.get(MessageAnnotation, harness.annotation.id) is None
assert set(session.scalars(select(AppAnnotationHitHistory.id))) == {histories[1].id, histories[2].id}
@pytest.mark.parametrize("operation", ["update", "delete"])
@pytest.mark.parametrize("scope", ["missing", "other_app", "other_tenant"])
def test_annotation_mutation_enforces_app_and_tenant_scope(
harness: _Harness, operation: _Operation, scope: str
) -> None:
annotation_id = str(uuid4())
if scope != "missing":
other = make_app(
app_id=str(uuid4()), tenant_id=harness.target.tenant_id if scope == "other_app" else str(uuid4())
)
with harness.factory.begin() as session:
session.add(other)
session.execute(
update(MessageAnnotation).where(MessageAnnotation.id == harness.annotation.id).values(app_id=other.id)
)
annotation_id = harness.annotation.id
_error(
harness.request(operation, annotation_id=annotation_id, payload={"question": "Changed", "answer": "Changed"}),
status=404,
code="not_found",
message="Annotation not found",
)
with harness.factory() as session:
stored = session.get(MessageAnnotation, harness.annotation.id)
assert stored is not None
assert (stored.question, stored.content) == ("First question", "First answer")
@pytest.mark.parametrize("operation", _OPERATIONS)
@pytest.mark.parametrize(
"authorization", [None, "", "Bearer", "Bearer ", "Bearer ", "Basic secret", "Bearer sk-missing"]
)
def test_every_handler_rejects_missing_malformed_or_unknown_credentials(
harness: _Harness, operation: _Operation, authorization: str | None
) -> None:
_error(
harness.request(operation, authorization=authorization, payload=_REPLY_PAYLOAD), status=401, code="unauthorized"
)
@pytest.mark.parametrize(
("separator", "status"),
[(" ", 200), ("\t", 200), (" \t ", 200), ("\u00a0", 401), ("\u2003", 401)],
)
def test_account_admission_preserves_bearer_separator_rules(harness: _Harness, separator: str, status: int) -> None:
response = harness.request("list", authorization=f"Bearer{separator}{harness.token.token}")
if status == 200:
assert response.status_code == status
assert [row["id"] for row in response.get_json()["data"]] == [harness.annotation.id]
else:
_error(response, status=status, code="unauthorized")
@pytest.mark.parametrize("operation", _OPERATIONS)
@pytest.mark.parametrize(
("gate", "status", "code", "message"),
[
("missing_app", 403, "forbidden", "The app no longer exists."),
("foreign_tenant", 403, "forbidden", "The app no longer exists."),
("api_disabled", 403, "app_api_disabled", "The app's API service has been disabled."),
("missing_tenant", 403, "workspace_not_found", "Tenant does not exist."),
("archived_tenant", 403, "workspace_archived", "The workspace's status is archived."),
("missing_owner", 401, "unauthorized", "Tenant owner account not found or tenant is not active."),
("admin_owner", 401, "unauthorized", "Tenant owner account not found or tenant is not active."),
("editor_owner", 401, "unauthorized", "Tenant owner account not found or tenant is not active."),
("missing_account", 401, "unauthorized", "Tenant owner account not found or tenant is not active."),
],
)
def test_every_handler_checks_app_workspace_and_owner(
harness: _Harness, operation: _Operation, gate: str, status: int, code: str, message: str
) -> None:
with harness.factory.begin() as session:
if gate == "missing_app":
session.execute(delete(App).where(App.id == harness.target.id))
elif gate == "foreign_tenant":
session.execute(update(App).where(App.id == harness.target.id).values(tenant_id=str(uuid4())))
elif gate == "api_disabled":
session.execute(update(App).where(App.id == harness.target.id).values(enable_api=False))
elif gate == "missing_tenant":
session.execute(delete(Tenant).where(Tenant.id == harness.target.tenant_id))
elif gate == "archived_tenant":
session.execute(
update(Tenant).where(Tenant.id == harness.target.tenant_id).values(status=TenantStatus.ARCHIVE)
)
elif gate == "missing_owner":
session.execute(delete(TenantAccountJoin).where(TenantAccountJoin.account_id == harness.account.id))
elif gate in {"admin_owner", "editor_owner"}:
session.execute(
update(TenantAccountJoin)
.where(TenantAccountJoin.account_id == harness.account.id)
.values(role=TenantAccountRole.ADMIN if gate == "admin_owner" else TenantAccountRole.EDITOR)
)
else:
session.execute(delete(Account).where(Account.id == harness.account.id))
_error(
harness.request(operation, payload=_REPLY_PAYLOAD),
status=status,
code=code,
message=message,
)
def test_owner_from_another_workspace_cannot_authorize_annotation_access(harness: _Harness) -> None:
foreign_tenant = Tenant(name="Other workspace")
foreign_owner = make_account(account_id=str(uuid4()), email="other-owner@example.com")
with harness.factory.begin() as session:
session.execute(delete(TenantAccountJoin).where(TenantAccountJoin.tenant_id == harness.target.tenant_id))
session.add_all(
[
foreign_tenant,
foreign_owner,
TenantAccountJoin(
tenant_id=foreign_tenant.id,
account_id=foreign_owner.id,
role=TenantAccountRole.OWNER,
),
]
)
_error(harness.request("create", payload={"question": "Q", "answer": "A"}), status=401, code="unauthorized")
with harness.factory() as session:
assert list(session.scalars(select(MessageAnnotation.id))) == [harness.annotation.id]
@pytest.mark.parametrize("account_status", list(AccountStatus))
def test_account_console_status_does_not_disable_machine_access(
harness: _Harness, account_status: AccountStatus
) -> None:
with harness.factory.begin() as session:
session.execute(update(Account).where(Account.id == harness.account.id).values(status=account_status))
session.execute(
update(App).where(App.id == harness.target.id).values(is_demo=True, is_public=True, is_universal=True)
)
assert harness.request("create", payload={"question": "Q", "answer": "A"}).status_code == 201
@pytest.mark.parametrize("revocation", ["token", "binding"])
def test_resource_token_revocation_is_effective_on_the_next_request(harness: _Harness, revocation: str) -> None:
assert harness.request("list").status_code == 200
with harness.factory.begin() as session:
if revocation == "token":
session.execute(delete(ResourceAccessToken).where(ResourceAccessToken.id == harness.token.id))
else:
session.execute(
delete(ResourceAccessTokenRelation).where(ResourceAccessTokenRelation.token_id == harness.token.id)
)
_error(
harness.request("list"),
status=401 if revocation == "token" else 403,
code="unauthorized" if revocation == "token" else "forbidden",
)
def test_resource_token_creator_does_not_replace_the_workspace_owner(harness: _Harness) -> None:
creator = make_account(account_id=str(uuid4()), email="integration@example.com")
with harness.factory.begin() as session:
session.add_all(
[
creator,
TenantAccountJoin(
tenant_id=harness.target.tenant_id,
account_id=creator.id,
role=TenantAccountRole.NORMAL,
),
]
)
session.execute(
update(ResourceAccessToken).where(ResourceAccessToken.id == harness.token.id).values(created_by=creator.id)
)
response = harness.request("create", payload={"question": "Q", "answer": "A"})
assert response.status_code == 201
with harness.factory() as session:
annotation = session.get(MessageAnnotation, response.get_json()["id"])
assert annotation is not None
assert annotation.account_id == harness.account.id
def test_knowledge_binding_never_authorizes_annotation_access(harness: _Harness) -> None:
dataset = Dataset(
id=str(uuid4()), tenant_id=harness.target.tenant_id, name="Knowledge", created_by=harness.account.id
)
with harness.factory.begin() as session:
session.add(dataset)
session.execute(
update(ResourceAccessTokenRelation)
.where(ResourceAccessTokenRelation.token_id == harness.token.id)
.values(resource_type=ResourceAccessTokenResourceType.KNOWLEDGE, app_id=None, dataset_id=dataset.id)
)
_error(harness.request("list"), status=403, code="forbidden")
def test_resource_token_requires_explicit_selection_for_multiple_bound_apps(harness: _Harness) -> None:
other = make_app(app_id=str(uuid4()), tenant_id=harness.target.tenant_id)
annotation = _annotation(harness, 1, app_id=other.id)
with harness.factory.begin() as session:
session.add_all(
[
other,
annotation,
ResourceAccessTokenRelation(
token_id=harness.token.id,
resource_type=ResourceAccessTokenResourceType.APP,
app_id=other.id,
),
]
)
_error(harness.request("list"), status=400, code="invalid_param")
for app, expected in [(harness.target, harness.annotation), (other, annotation)]:
response = harness.request("list", requested_app_id=app.id)
assert response.status_code == 200
assert [row["id"] for row in response.get_json()["data"]] == [expected.id]
_error(harness.request("list", requested_app_id=str(uuid4())), status=403, code="forbidden")
@pytest.mark.parametrize("operation", ["reply", "status"])
def test_reply_rejects_invalid_action_before_touching_jobs(harness: _Harness, operation: _Operation) -> None:
_error(harness.request(operation, action="invalid", payload=_REPLY_PAYLOAD), status=400, code="invalid_param")
@pytest.mark.parametrize("action", ["enable", "disable"])
@pytest.mark.parametrize("payload", [None, {}, {"score_threshold": 0.5}])
def test_reply_enable_and_disable_require_the_payload(
harness: _Harness, action: str, payload: dict[str, object] | None
) -> None:
_error(harness.request("reply", action=action, payload=payload), status=422, code="unprocessable_entity")
@pytest.mark.parametrize("otel_enabled", [False, True])
@pytest.mark.parametrize("authenticated", [False, True])
def test_admission_preserves_logging_and_trace_identity(
harness: _Harness, config_overrides: Callable[..., None], otel_enabled: bool, authenticated: bool
) -> None:
config_overrides(ENABLE_OTEL=otel_enabled)
output = StringIO()
handler = logging.StreamHandler(output)
handler.addFilter(IdentityContextFilter())
handler.setFormatter(logging.Formatter("%(tenant_id)s|%(user_id)s|%(user_type)s"))
logger = logging.Logger("annotation-admission-identity", logging.INFO)
logger.addHandler(handler)
@harness.app.after_request
def log_response(response: Response) -> Response:
logger.info("Annotation response")
return response
exporter = InMemorySpanExporter()
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(exporter))
try:
with provider.get_tracer(__name__).start_as_current_span("annotation request"):
response = harness.request("list", authorization="default" if authenticated else None)
assert response.status_code == (200 if authenticated else 401)
expected_log = f"{harness.target.tenant_id}|{harness.account.id}|account" if authenticated else "||"
assert output.getvalue().strip() == expected_log
[span] = exporter.get_finished_spans()
assert span.attributes == (
{DifySpanAttributes.TENANT_ID: harness.target.tenant_id, GenAIAttributes.USER_ID: harness.account.id}
if otel_enabled and authenticated
else {}
)
finally:
provider.shutdown()
handler.close()
@pytest.mark.parametrize("user_type", ["account", "end_user"])
def test_legacy_login_signal_preserves_identity_span_attributes(
config_overrides: Callable[..., None], user_type: str
) -> None:
config_overrides(ENABLE_OTEL=True)
tenant = Tenant(name="Tracing workspace")
account = make_account(account_id=str(uuid4()), tenant=tenant)
user = (
account if user_type == "account" else EndUser(id=str(uuid4()), tenant_id=tenant.id, type=EndUserType.BROWSER)
)
exporter = InMemorySpanExporter()
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(exporter))
try:
with provider.get_tracer(__name__).start_as_current_span("legacy login"):
on_user_loaded(None, user)
[span] = exporter.get_finished_spans()
assert span.attributes == {DifySpanAttributes.TENANT_ID: tenant.id, GenAIAttributes.USER_ID: user.id}
finally:
provider.shutdown()
@pytest.mark.parametrize("unavailable", ["tenant", "recording_span"])
def test_span_identity_skips_missing_tenant_and_ended_spans(
config_overrides: Callable[..., None], caplog: pytest.LogCaptureFixture, unavailable: str
) -> None:
config_overrides(ENABLE_OTEL=True)
exporter = InMemorySpanExporter()
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(exporter))
span = provider.get_tracer(__name__).start_span("unavailable identity context")
try:
if unavailable == "recording_span":
span.end()
with trace.use_span(span, end_on_exit=False), caplog.at_level(logging.WARNING):
set_identity_span_attributes(tenant_id=None if unavailable == "tenant" else "workspace", user_id="account")
if unavailable != "recording_span":
span.end()
[finished] = exporter.get_finished_spans()
assert finished.attributes == {}
assert "Setting attribute on ended span" not in caplog.text
assert "Error setting tenant and user attributes" not in caplog.text
finally:
provider.shutdown()
@@ -0,0 +1,298 @@
"""Delete contracts using real API tokens, EndUser admission, and SQLite writes."""
from dataclasses import dataclass
from http import HTTPStatus
from uuid import uuid4
import pytest
from sqlalchemy import event, select, update
from sqlalchemy.orm import Session, SessionTransaction
from werkzeug.test import TestResponse
from constants.resource_access_token import ResourceAccessTokenResourceType
from controllers.service_api.app.conversation import ConversationDetailApi
from machinery.context import RequestContext
from models import Tenant, TenantStatus
from models.enums import ConversationFromSource
from models.model import App, AppMode, Conversation, EndUser
from models.resource_access_token import ResourceAccessToken
from repositories.conversation_repository import ConversationRepository
from services.auth.resource_access_token_contracts import ResourceAccessTokenResource
from services.conversation_command_service import ConversationCommandService
from tests.e2e_tests.contracts.controllers.service_api.app import test_conversation_rename as rename_tests
from tests.e2e_tests.contracts.controllers.service_api.app.test_conversation_rename import _Harness as RenameHarness
from tests.e2e_tests.contracts.controllers.service_api.app.test_conversation_rename import _Services as RenameServices
from tests.helpers.unit.model_factories import make_app
rename_harness = rename_tests.harness
@dataclass(frozen=True)
class _Services(RenameServices):
conversation_commands: ConversationCommandService
@dataclass(frozen=True)
class _Harness:
source: RenameHarness
cleanups: list[tuple[str, str, tuple[str, ...]]]
def delete(
self,
*,
conversation_id: str | None = None,
user: str | None = "alice",
authorization: str | None = "Bearer rename-token",
requested_app_id: str | None = None,
) -> TestResponse:
headers = {"Authorization": authorization} if authorization is not None else {}
if requested_app_id is not None:
headers["X-Dify-App-ID"] = requested_app_id
return self.source.app.test_client().delete(
f"/conversations/{conversation_id or self.source.conversation.id}",
json={"user": user} if user is not None else {},
headers=headers,
)
def assert_not_deleted(self) -> None:
with self.source.factory() as session:
stored = session.get(Conversation, self.source.conversation.id)
assert stored is not None
assert not stored.is_deleted
assert self.cleanups == []
@pytest.fixture
def harness(rename_harness: RenameHarness) -> _Harness:
cleanups: list[tuple[str, str, tuple[str, ...]]] = []
def enqueue_cleanup(*, tenant_id: str, conversation_id: str, retired_workspace_ids: tuple[str, ...]) -> None:
rename_harness.assert_closed()
with rename_harness.factory() as session:
stored = session.get(Conversation, conversation_id)
assert stored is not None
assert stored.is_deleted
cleanups.append((tenant_id, conversation_id, retired_workspace_ids))
original = rename_harness.app.extensions["application_services"]
assert isinstance(original, RenameServices)
rename_harness.app.extensions["application_services"] = _Services(
app_definitions=original.app_definitions,
app_scoped_end_users=original.app_scoped_end_users,
conversation_names=original.conversation_names,
resource_access_tokens=original.resource_access_tokens,
conversation_commands=ConversationCommandService(
conversations=ConversationRepository(session_factory=rename_harness.repository_factory),
enqueue_delete_cleanup=enqueue_cleanup,
),
)
rename_harness.api.add_resource(ConversationDetailApi, "/conversations/<uuid:conversation_id>")
return _Harness(rename_harness, cleanups)
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])
def test_delete_commits_before_cleanup_after_admission_connections_close(harness: _Harness, mode: AppMode) -> None:
source = harness.source
with source.factory.begin() as session:
session.execute(update(App).where(App.id == source.target.id).values(mode=mode))
response = harness.delete()
assert response.status_code == HTTPStatus.NO_CONTENT
assert response.data == b""
assert response.headers["Content-Type"] == "application/json"
assert harness.cleanups == [(source.tenant.id, source.conversation.id, ())]
assert source.admissions
assert source.provisions
assert source.queries
assert source.scoped_sessions == []
source.assert_closed()
repeated = harness.delete()
assert repeated.status_code == HTTPStatus.NOT_FOUND
assert repeated.json is not None
assert repeated.json["code"] == "conversation_not_found"
assert len(harness.cleanups) == 1
source.assert_closed()
@pytest.mark.parametrize("field", ["app_id", "from_end_user_id", "from_source", "from_account_id", "is_deleted"])
def test_delete_rejects_conversation_outside_end_user_scope(harness: _Harness, field: str) -> None:
source = harness.source
value: str | bool
if field == "is_deleted":
value = True
elif field == "from_source":
value = ConversationFromSource.CONSOLE
elif field == "from_account_id":
value = source.end_user.id
else:
value = str(uuid4())
with source.factory.begin() as session:
session.execute(update(Conversation).where(Conversation.id == source.conversation.id).values({field: value}))
response = harness.delete()
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.json is not None
assert response.json == {
"code": "conversation_not_found",
"message": "The conversation was not found for this user and app.",
"status": HTTPStatus.NOT_FOUND,
}
assert harness.cleanups == []
with source.factory() as session:
stored = session.get(Conversation, source.conversation.id)
assert stored is not None
assert stored.is_deleted is (field == "is_deleted")
source.assert_closed()
@pytest.mark.parametrize("change_app_too", [False, True], ids=["end-user-workspace", "app-workspace"])
def test_workspace_change_after_admission_prevents_delete(harness: _Harness, change_app_too: bool) -> None:
source = harness.source
def change_owner(_session: Session, _transaction: SessionTransaction) -> None:
with source.factory.begin() as session:
end_user = session.get(EndUser, source.end_user.id)
assert end_user is not None
end_user.tenant_id = str(uuid4())
if change_app_too:
target = session.get(App, source.target.id)
assert target is not None
target.tenant_id = end_user.tenant_id
event.listen(source.provision_factory, "after_transaction_end", change_owner, once=True)
try:
response = harness.delete()
finally:
event.remove(source.provision_factory, "after_transaction_end", change_owner)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.json is not None
assert response.json["code"] == "conversation_not_found"
harness.assert_not_deleted()
source.assert_closed()
def test_unknown_conversation_cannot_delete_existing_decoy(harness: _Harness) -> None:
response = harness.delete(conversation_id=str(uuid4()))
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.json is not None
assert response.json["code"] == "conversation_not_found"
harness.assert_not_deleted()
harness.source.assert_closed()
@pytest.mark.parametrize("user", [None, ""])
def test_anonymous_user_cannot_delete_identified_users_conversation(harness: _Harness, user: str | None) -> None:
response = harness.delete(user=user)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.json is not None
assert response.json["code"] == "conversation_not_found"
with harness.source.factory() as session:
end_users = session.scalars(select(EndUser)).all()
assert len(end_users) == 2
assert len([user for user in end_users if user.id != harness.source.end_user.id]) == 1
harness.assert_not_deleted()
harness.source.assert_closed()
@pytest.mark.parametrize("authorization", [None, "Basic rename-token", "Bearer invalid-token"])
def test_invalid_token_is_rejected_before_admission(harness: _Harness, authorization: str | None) -> None:
source = harness.source
response = harness.delete(authorization=authorization)
assert response.status_code == HTTPStatus.UNAUTHORIZED
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
assert response.json is not None
assert response.json["code"] == "unauthorized"
assert source.admissions == source.provisions == source.queries == []
harness.assert_not_deleted()
source.assert_closed()
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
def test_non_chat_mode_is_rejected_before_delete(harness: _Harness, mode: AppMode) -> None:
source = harness.source
with source.factory.begin() as session:
session.execute(update(App).where(App.id == source.target.id).values(mode=mode))
response = harness.delete()
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.json is not None
assert response.json["code"] == "not_chat_app"
assert source.queries == []
harness.assert_not_deleted()
source.assert_closed()
@pytest.mark.parametrize(
("failure", "code"), [("disabled_api", "app_api_disabled"), ("archived_workspace", "workspace_archived")]
)
def test_disabled_app_or_workspace_is_rejected_before_provisioning(harness: _Harness, failure: str, code: str) -> None:
source = harness.source
with source.factory.begin() as session:
if failure == "disabled_api":
session.execute(update(App).where(App.id == source.target.id).values(enable_api=False))
else:
session.execute(update(Tenant).where(Tenant.id == source.tenant.id).values(status=TenantStatus.ARCHIVE))
response = harness.delete(user="new-user")
assert response.status_code == HTTPStatus.FORBIDDEN
assert response.json is not None
assert response.json["code"] == code
assert source.provisions == source.queries == []
with source.factory() as session:
assert session.scalars(select(EndUser.id)).all() == [source.end_user.id]
harness.assert_not_deleted()
source.assert_closed()
@pytest.mark.parametrize("multiple_apps", [False, True], ids=["implicit-single-app", "explicit-multiple-apps"])
def test_resource_token_deletes_bound_app_and_records_usage(harness: _Harness, multiple_apps: bool) -> None:
source = harness.source
app_ids = (source.target.id,)
if multiple_apps:
other = make_app(app_id=str(uuid4()), tenant_id=source.tenant.id)
with source.factory.begin() as session:
session.add(other)
app_ids = (other.id, source.target.id)
services = source.app.extensions["application_services"]
assert isinstance(services, _Services)
token = services.resource_access_tokens.create(
RequestContext(
request_id="delete-resource-token",
trace_id=None,
account_id=str(uuid4()),
active_workspace_id=source.tenant.id,
),
name="Delete app token",
resources=tuple(ResourceAccessTokenResource(ResourceAccessTokenResourceType.APP, app_id) for app_id in app_ids),
)
response = harness.delete(
authorization=f"Bearer {token.token}",
requested_app_id=source.target.id if multiple_apps else None,
)
assert response.status_code == HTTPStatus.NO_CONTENT
assert response.data == b""
assert harness.cleanups == [(source.tenant.id, source.conversation.id, ())]
source.assert_closed()
with source.factory() as session:
stored_token = session.get(ResourceAccessToken, token.token_id)
assert stored_token is not None
assert stored_token.last_used_at is not None
assert session.scalars(select(EndUser.id)).all() == [source.end_user.id]
def test_failed_delete_commit_rolls_back_without_publishing_cleanup(harness: _Harness) -> None:
def reject_commit(_session: Session) -> None:
raise RuntimeError("Database commit failed")
source = harness.source
event.listen(source.repository_factory, "before_commit", reject_commit)
try:
response = harness.delete()
finally:
event.remove(source.repository_factory, "before_commit", reject_commit)
assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
harness.assert_not_deleted()
source.assert_closed()
@@ -0,0 +1,283 @@
"""List contracts with real token admission, shared queries, and SQLite."""
from collections.abc import Mapping
from dataclasses import dataclass
from datetime import timedelta
from http import HTTPStatus
from uuid import uuid4
import pytest
from sqlalchemy import event, select, update
from sqlalchemy.orm import Session, SessionTransaction
from werkzeug.test import TestResponse
from constants.resource_access_token import ResourceAccessTokenResourceType
from controllers.service_api.app.conversation import ConversationApi
from machinery.context import RequestContext
from models import Tenant, TenantStatus
from models.enums import ConversationFromSource, InvokeFrom
from models.model import App, AppMode, Conversation, EndUser
from models.resource_access_token import ResourceAccessToken
from models.utils.file_input_compat import restore_input_files
from repositories.conversation_repository import ConversationRepository
from services.auth.resource_access_token_contracts import ResourceAccessTokenResource
from services.conversation_query_service import ConversationQueryService
from services.entities.conversation_entities import ConversationSort
from services.message_query_adapters import MessageFileResolver
from tests.e2e_tests.contracts.controllers.service_api.app import test_conversation_rename as rename_tests
from tests.e2e_tests.contracts.controllers.service_api.app.test_conversation_rename import _Harness as RenameHarness
from tests.e2e_tests.contracts.controllers.service_api.app.test_conversation_rename import _Services as RenameServices
from tests.helpers.unit.model_factories import make_app, make_conversation
rename_harness = rename_tests.harness
@dataclass(frozen=True)
class _Services(RenameServices):
conversation_queries: ConversationQueryService
@dataclass(frozen=True)
class _Harness:
source: RenameHarness
def get(
self,
query: Mapping[str, str | int] | None = None,
*,
user: str | None = "alice",
authorization: str | None = "Bearer rename-token",
requested_app_id: str | None = None,
) -> TestResponse:
headers = {"Authorization": authorization} if authorization is not None else {}
if requested_app_id is not None:
headers["X-Dify-App-ID"] = requested_app_id
args = dict(query or {})
if user is not None:
args["user"] = user
return self.source.app.test_client().get("/conversations", query_string=args, headers=headers)
@pytest.fixture
def harness(rename_harness: RenameHarness) -> _Harness:
original = rename_harness.app.extensions["application_services"]
assert isinstance(original, RenameServices)
rename_harness.app.extensions["application_services"] = _Services(
app_definitions=original.app_definitions,
app_scoped_end_users=original.app_scoped_end_users,
conversation_names=original.conversation_names,
resource_access_tokens=original.resource_access_tokens,
conversation_queries=ConversationQueryService(
files=MessageFileResolver(),
conversations=ConversationRepository(session_factory=rename_harness.repository_factory),
restore_inputs=restore_input_files,
),
)
rename_harness.api.add_resource(ConversationApi, "/conversations")
return _Harness(rename_harness)
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])
def test_list_preserves_wire_contract_and_closes_admission_before_query(harness: _Harness, mode: AppMode) -> None:
source = harness.source
with source.factory.begin() as session:
session.execute(update(App).where(App.id == source.target.id).values(mode=mode))
response = harness.get()
assert response.status_code == HTTPStatus.OK
assert response.headers["Content-Type"] == "application/json"
assert response.get_json() == {
"limit": 20,
"has_more": False,
"data": [
{
"id": source.conversation.id,
"name": "Original name",
"inputs": {"text": "", "list": [], "zero": 0, "flag": False, "null": None},
"status": "normal",
"introduction": None,
"created_at": int(source.conversation.created_at.timestamp()),
"updated_at": int(source.conversation.updated_at.timestamp()),
}
],
}
assert source.admissions
assert source.provisions
assert source.queries
assert source.scoped_sessions == []
source.assert_closed()
@pytest.mark.parametrize("sort_by", ["created_at", "-created_at", "updated_at", "-updated_at"])
def test_sort_cursor_and_empty_last_id_follow_existing_contract(harness: _Harness, sort_by: ConversationSort) -> None:
source = harness.source
newest = make_conversation(
conversation_id=str(uuid4()),
app_id=source.target.id,
from_source=ConversationFromSource.API,
from_end_user_id=source.end_user.id,
inputs={},
)
newest.created_at = source.conversation.created_at + timedelta(days=1)
newest.updated_at = source.conversation.updated_at + timedelta(days=1)
newest.invoke_from = InvokeFrom.SERVICE_API
with source.factory.begin() as session:
session.add(newest)
ordered_ids = [source.conversation.id, newest.id]
if sort_by.startswith("-"):
ordered_ids.reverse()
first = harness.get({"sort_by": sort_by, "limit": 1, "last_id": ""})
assert first.status_code == HTTPStatus.OK
assert first.get_json()["has_more"] is True
assert [item["id"] for item in first.get_json()["data"]] == ordered_ids[:1]
second = harness.get({"sort_by": sort_by, "limit": 1, "last_id": ordered_ids[0]})
assert second.status_code == HTTPStatus.OK
assert second.get_json()["has_more"] is False
assert [item["id"] for item in second.get_json()["data"]] == ordered_ids[1:]
source.assert_closed()
@pytest.mark.parametrize(
"field", ["app_id", "from_end_user_id", "from_source", "from_account_id", "is_deleted", "invoke_from"]
)
def test_list_and_cursor_do_not_cross_user_app_or_invocation_scope(harness: _Harness, field: str) -> None:
source = harness.source
value: str | bool
if field == "is_deleted":
value = True
elif field == "from_source":
value = ConversationFromSource.CONSOLE
elif field == "invoke_from":
value = InvokeFrom.WEB_APP.value
else:
value = str(uuid4())
with source.factory.begin() as session:
session.execute(update(Conversation).where(Conversation.id == source.conversation.id).values({field: value}))
response = harness.get()
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"limit": 20, "has_more": False, "data": []}
cursor = harness.get({"last_id": source.conversation.id})
assert cursor.status_code == HTTPStatus.NOT_FOUND
assert cursor.get_json() == {
"code": "conversation_cursor_not_found",
"message": "The conversation cursor is not in the current list. Refresh the list and try again.",
"status": HTTPStatus.NOT_FOUND,
}
source.assert_closed()
@pytest.mark.parametrize("query", [{"limit": "0"}, {"limit": "101"}, {"last_id": "invalid"}, {"sort_by": "name"}])
def test_invalid_query_returns_422_before_querying(harness: _Harness, query: dict[str, str]) -> None:
response = harness.get(query)
assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
assert response.get_json()["code"] == "unprocessable_entity"
assert harness.source.queries == []
harness.source.assert_closed()
@pytest.mark.parametrize("user", [None, "", "another-user"])
def test_new_or_anonymous_user_receives_empty_list(harness: _Harness, user: str | None) -> None:
response = harness.get(user=user)
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"limit": 20, "has_more": False, "data": []}
with harness.source.factory() as session:
assert len(session.scalars(select(EndUser)).all()) == 2
harness.source.assert_closed()
@pytest.mark.parametrize("authorization", [None, "Basic rename-token", "Bearer invalid-token"])
def test_invalid_token_preserves_authentication_error_and_header(harness: _Harness, authorization: str | None) -> None:
source = harness.source
response = harness.get(authorization=authorization)
assert response.status_code == HTTPStatus.UNAUTHORIZED
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
assert response.get_json()["code"] == "unauthorized"
assert source.admissions == source.provisions == source.queries == []
source.assert_closed()
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
def test_non_chat_mode_is_rejected_before_querying(harness: _Harness, mode: AppMode) -> None:
source = harness.source
with source.factory.begin() as session:
session.execute(update(App).where(App.id == source.target.id).values(mode=mode))
response = harness.get()
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "not_chat_app"
assert source.queries == []
source.assert_closed()
@pytest.mark.parametrize("change_app_too", [False, True], ids=["end-user-workspace", "app-workspace"])
def test_workspace_change_after_admission_prevents_list(harness: _Harness, change_app_too: bool) -> None:
source = harness.source
def change_owner(_session: Session, _transaction: SessionTransaction) -> None:
with source.factory.begin() as session:
end_user = session.get(EndUser, source.end_user.id)
assert end_user is not None
end_user.tenant_id = str(uuid4())
if change_app_too:
target = session.get(App, source.target.id)
assert target is not None
target.tenant_id = end_user.tenant_id
event.listen(source.provision_factory, "after_transaction_end", change_owner, once=True)
try:
response = harness.get()
finally:
event.remove(source.provision_factory, "after_transaction_end", change_owner)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "conversation_not_found"
source.assert_closed()
@pytest.mark.parametrize(
("failure", "code"), [("disabled_api", "app_api_disabled"), ("archived_workspace", "workspace_archived")]
)
def test_disabled_app_or_workspace_is_rejected_before_provisioning(harness: _Harness, failure: str, code: str) -> None:
source = harness.source
with source.factory.begin() as session:
if failure == "disabled_api":
session.execute(update(App).where(App.id == source.target.id).values(enable_api=False))
else:
session.execute(update(Tenant).where(Tenant.id == source.tenant.id).values(status=TenantStatus.ARCHIVE))
response = harness.get(user="new-user")
assert response.status_code == HTTPStatus.FORBIDDEN
assert response.get_json()["code"] == code
assert source.provisions == source.queries == []
source.assert_closed()
@pytest.mark.parametrize("multiple_apps", [False, True], ids=["implicit-single-app", "explicit-multiple-apps"])
def test_resource_token_lists_bound_app_and_records_usage(harness: _Harness, multiple_apps: bool) -> None:
source = harness.source
app_ids = (source.target.id,)
if multiple_apps:
other = make_app(app_id=str(uuid4()), tenant_id=source.tenant.id)
with source.factory.begin() as session:
session.add(other)
app_ids = (other.id, source.target.id)
services = source.app.extensions["application_services"]
assert isinstance(services, _Services)
token = services.resource_access_tokens.create(
RequestContext(
request_id="list-resource-token",
trace_id=None,
account_id=str(uuid4()),
active_workspace_id=source.tenant.id,
),
name="List app token",
resources=tuple(ResourceAccessTokenResource(ResourceAccessTokenResourceType.APP, app_id) for app_id in app_ids),
)
response = harness.get(
authorization=f"Bearer {token.token}",
requested_app_id=source.target.id if multiple_apps else None,
)
assert response.status_code == HTTPStatus.OK
assert [item["id"] for item in response.get_json()["data"]] == [source.conversation.id]
source.assert_closed()
with source.factory() as session:
stored_token = session.get(ResourceAccessToken, token.token_id)
assert stored_token is not None
assert stored_token.last_used_at is not None
assert session.scalars(select(EndUser.id)).all() == [source.end_user.id]
@@ -0,0 +1,468 @@
"""Rename HTTP contracts with real token admission, services, and SQLite transactions."""
from collections.abc import Iterator
from dataclasses import dataclass
from datetime import datetime
from decimal import Decimal
from uuid import uuid4
import pytest
from flask import Flask
from flask_login import LoginManager
from pydantic import JsonValue
from sqlalchemy import Connection, Engine, event, select
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
from constants.resource_access_token import ResourceAccessTokenResourceType
from controllers.service_api.app.conversation import ConversationRenameApi
from core.errors.error import ProviderTokenNotInitError
from extensions.application_services.resource_access_token import build_resource_access_token_service
from extensions.ext_database import db
from libs.external_api import ExternalApi
from machinery.context import RequestContext
from models import Tenant, TenantStatus
from models.enums import ApiTokenType, ConversationFromSource, EndUserType
from models.model import ApiToken, App, AppMode, Conversation, EndUser
from models.resource_access_token import ResourceAccessToken
from repositories.app_definition_query_repository import AppDefinitionQueryRepository
from repositories.app_scoped_end_user_repository import AppScopedEndUserRepo
from repositories.conversation_repository import ConversationRepository
from services.app_definition_query_service import AppDefinitionQueryService
from services.app_scoped_end_user_service import AppScopedEndUserService
from services.auth.resource_access_token_contracts import ResourceAccessTokenResource
from services.conversation_name_generator import DefaultModelConversationNameGenerator
from services.conversation_name_service import ConversationNameService
from services.resource_access_token_service import ResourceAccessTokenService
from tests.helpers.unit.model_factories import make_app, make_conversation, make_end_user, make_message
from tests.helpers.unit.services.tracing_ports import untraced_scopes
_CREATED_AT = datetime(2026, 1, 2, 3, 4, 5)
_UPDATED_AT = datetime(2026, 1, 3, 3, 4, 5)
_INPUTS: dict[str, JsonValue] = {"text": "", "list": [], "zero": 0, "flag": False, "null": None}
@dataclass(frozen=True)
class _EndUsers:
commands: AppScopedEndUserService[EndUser]
@dataclass(frozen=True)
class _Services:
app_definitions: AppDefinitionQueryService
app_scoped_end_users: _EndUsers
conversation_names: ConversationNameService
resource_access_tokens: ResourceAccessTokenService
@dataclass(frozen=True)
class _Harness:
app: Flask
target: App
tenant: Tenant
end_user: EndUser
conversation: Conversation
factory: sessionmaker[Session]
repository_factory: sessionmaker[Session]
api: ExternalApi
provision_factory: sessionmaker[Session]
admissions: list[Session]
provisions: list[Session]
queries: list[Session]
scoped_sessions: list[Session]
def post(
self,
*,
body: dict[str, object],
user: str | None = "alice",
authorization: str | None = "Bearer rename-token",
requested_app_id: str | None = None,
conversation_id: str | None = None,
) -> TestResponse:
headers: dict[str, str] = {"Authorization": authorization} if authorization is not None else {}
if requested_app_id is not None:
headers["X-Dify-App-ID"] = requested_app_id
payload = dict(body)
if user is not None:
payload["user"] = user
return self.app.test_client().post(
f"/conversations/{conversation_id or self.conversation.id}/name", json=payload, headers=headers
)
def assert_closed(self) -> None:
assert all(
not session.in_transaction() and not session.identity_map
for session in self.admissions + self.provisions + self.queries + self.scoped_sessions
)
def assert_name(self, name: str) -> None:
with self.factory() as session:
conversation = session.get(Conversation, self.conversation.id)
assert conversation is not None
assert conversation.name == name
@pytest.fixture
def harness(sqlite_engine: Engine, sqlite_session_factory: sessionmaker[Session]) -> Iterator[_Harness]:
tenant = Tenant(name="Rename workspace")
target = make_app(app_id=str(uuid4()), tenant_id=tenant.id)
end_user = make_end_user(
end_user_id=str(uuid4()),
tenant_id=tenant.id,
app_id=target.id,
end_user_type=EndUserType.SERVICE_API,
session_id="alice",
external_user_id="alice",
)
conversation = make_conversation(
conversation_id=str(uuid4()),
app_id=target.id,
name="Original name",
inputs=dict(_INPUTS),
from_source=ConversationFromSource.API,
from_end_user_id=end_user.id,
)
conversation.created_at = _CREATED_AT
conversation.updated_at = _UPDATED_AT
conversation.introduction = None
token = ApiToken(app_id=target.id, tenant_id=tenant.id, type=ApiTokenType.APP, token="rename-token")
with sqlite_session_factory.begin() as session:
session.add_all([tenant, target, end_user, conversation, token])
app = Flask(__name__)
app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))
db.init_app(app)
LoginManager(app)
admissions: list[Session] = []
provisions: list[Session] = []
queries: list[Session] = []
scoped_sessions: list[Session] = []
admission_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
provision_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
query_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
def track_admission(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
admissions.append(session)
def track_provision(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
assert all(not admission.in_transaction() and not admission.identity_map for admission in admissions)
provisions.append(session)
def track_query(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
# This catches the legacy admission retaining a second connection while
# the rename repository opens its own transaction, before request teardown.
assert not db.session().in_transaction()
assert all(
not previous.in_transaction() and not previous.identity_map
for previous in admissions + provisions + queries + scoped_sessions
)
queries.append(session)
def track_scoped(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
scoped_sessions.append(session)
event.listen(admission_factory, "after_begin", track_admission)
event.listen(provision_factory, "after_begin", track_provision)
event.listen(query_factory, "after_begin", track_query)
event.listen(db.session.session_factory, "after_begin", track_scoped)
app.extensions["application_services"] = _Services(
app_definitions=AppDefinitionQueryService(
definitions=AppDefinitionQueryRepository(session_factory=admission_factory), builtin_icon_url_prefix=""
),
app_scoped_end_users=_EndUsers(
AppScopedEndUserService(end_users=AppScopedEndUserRepo(session_factory=provision_factory))
),
conversation_names=ConversationNameService(
conversations=ConversationRepository(session_factory=query_factory),
generate_name=DefaultModelConversationNameGenerator(trace_scopes=untraced_scopes()),
),
resource_access_tokens=build_resource_access_token_service(database_client=admission_factory),
)
api = ExternalApi(app)
api.add_resource(ConversationRenameApi, "/conversations/<uuid:conversation_id>/name")
yield _Harness(
app,
target,
tenant,
end_user,
conversation,
sqlite_session_factory,
query_factory,
api,
provision_factory,
admissions,
provisions,
queries,
scoped_sessions,
)
event.remove(admission_factory, "after_begin", track_admission)
event.remove(provision_factory, "after_begin", track_provision)
event.remove(query_factory, "after_begin", track_query)
event.remove(db.session.session_factory, "after_begin", track_scoped)
with app.app_context():
db.session.remove()
db.engine.dispose()
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])
def test_manual_rename_persists_wire_contract_after_admission_connections_close(
harness: _Harness, mode: AppMode
) -> None:
with harness.factory.begin() as session:
target = session.get(App, harness.target.id)
assert target is not None
target.mode = mode
response = harness.post(body={"name": "Renamed title"})
assert response.status_code == 200
assert response.headers["Content-Type"] == "application/json"
assert response.json is not None
assert response.json == {
"id": harness.conversation.id,
"name": "Renamed title",
"inputs": _INPUTS,
"status": harness.conversation.status,
"introduction": None,
"created_at": int(_CREATED_AT.timestamp()),
"updated_at": response.json["updated_at"],
}
assert isinstance(response.json["updated_at"], int)
assert response.json["updated_at"] > int(_UPDATED_AT.timestamp())
assert harness.admissions
assert harness.provisions
assert harness.queries
assert harness.scoped_sessions == []
harness.assert_closed()
with harness.factory() as session:
conversation = session.get(Conversation, harness.conversation.id)
assert conversation is not None
assert conversation.name == "Renamed title"
assert conversation.updated_at is not None
assert response.json["updated_at"] == int(conversation.updated_at.timestamp())
@pytest.mark.parametrize("body", [{}, {"name": None}, {"name": ""}, {"name": " "}])
def test_invalid_manual_name_returns_422_without_renaming(harness: _Harness, body: dict[str, object]) -> None:
response = harness.post(body=body)
assert response.status_code == 422
assert response.json is not None
assert response.json["code"] == "unprocessable_entity"
assert harness.queries == []
harness.assert_name("Original name")
harness.assert_closed()
@pytest.mark.parametrize("body", [{"auto_generate": True}, {"name": "Ignored manual name", "auto_generate": True}])
def test_auto_name_ignores_manual_name_and_preserves_current_name_on_provider_failure(
harness: _Harness, body: dict[str, object], caplog: pytest.LogCaptureFixture
) -> None:
with harness.factory.begin() as session:
session.add_all(
[
make_message(
message_id=str(uuid4()),
app_id=harness.target.id,
conversation_id=harness.conversation.id,
query="Explain this topic",
inputs={},
message={},
answer="Explained.",
currency="USD",
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
from_source=ConversationFromSource.API,
from_end_user_id=harness.end_user.id,
),
]
)
response = harness.post(body=body)
assert response.status_code == 200
assert response.json is not None
assert response.json["name"] == "Original name"
assert response.json["updated_at"] == int(_UPDATED_AT.timestamp())
failures = [
record.exc_info[1]
for record in caplog.records
if record.name == "services.conversation_name_service" and record.exc_info is not None
]
assert len(failures) == 1
assert isinstance(failures[0], ProviderTokenNotInitError)
assert str(failures[0]) == "Default model not found for llm"
harness.assert_name("Original name")
harness.assert_closed()
def test_auto_name_without_messages_returns_explicit_404(harness: _Harness) -> None:
response = harness.post(body={"auto_generate": True})
assert response.status_code == 404
assert response.json is not None
assert response.json["code"] == "conversation_first_message_not_found"
assert response.json["message"] == "The conversation has no message from which to generate a name."
harness.assert_name("Original name")
harness.assert_closed()
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
def test_non_chat_mode_is_rejected_without_renaming(harness: _Harness, mode: AppMode) -> None:
with harness.factory.begin() as session:
target = session.get(App, harness.target.id)
assert target is not None
target.mode = mode
response = harness.post(body={"name": "Rejected"})
assert response.status_code == 400
assert response.json is not None
assert response.json["code"] == "not_chat_app"
assert harness.queries == []
harness.assert_name("Original name")
harness.assert_closed()
@pytest.mark.parametrize("failure", ["different_user", "different_app", "different_workspace", "console", "deleted"])
def test_conversation_ownership_is_enforced_before_rename(harness: _Harness, failure: str) -> None:
with harness.factory.begin() as session:
conversation = session.get(Conversation, harness.conversation.id)
assert conversation is not None
if failure == "different_user":
other = make_end_user(
end_user_id=str(uuid4()),
tenant_id=harness.tenant.id,
app_id=harness.target.id,
end_user_type=EndUserType.SERVICE_API,
session_id="bob",
external_user_id="bob",
)
session.add(other)
conversation.from_end_user_id = other.id
elif failure in {"different_app", "different_workspace"}:
tenant_id = harness.tenant.id if failure == "different_app" else str(uuid4())
other_app = make_app(app_id=str(uuid4()), tenant_id=tenant_id)
session.add(other_app)
conversation.app_id = other_app.id
elif failure == "console":
conversation.from_source = ConversationFromSource.CONSOLE
conversation.from_account_id = str(uuid4())
else:
conversation.is_deleted = True
response = harness.post(body={"name": "Rejected"})
assert response.status_code == 404
assert response.json == {
"code": "conversation_not_found",
"message": "The conversation was not found for this user and app.",
"status": 404,
}
harness.assert_name("Original name")
harness.assert_closed()
@pytest.mark.parametrize("change_app_too", [False, True], ids=["end-user-workspace", "app-workspace"])
def test_workspace_change_after_admission_prevents_rename(harness: _Harness, change_app_too: bool) -> None:
def change_owner(_session: Session, _transaction: SessionTransaction) -> None:
with harness.factory.begin() as session:
end_user = session.get(EndUser, harness.end_user.id)
assert end_user is not None
end_user.tenant_id = str(uuid4())
if change_app_too:
target = session.get(App, harness.target.id)
assert target is not None
target.tenant_id = end_user.tenant_id
event.listen(harness.provision_factory, "after_transaction_end", change_owner, once=True)
try:
response = harness.post(body={"name": "Rejected"})
finally:
event.remove(harness.provision_factory, "after_transaction_end", change_owner)
assert response.status_code == 404
assert response.json is not None
assert response.json["code"] == "conversation_not_found"
harness.assert_name("Original name")
harness.assert_closed()
def test_unknown_conversation_cannot_rename_an_existing_decoy(harness: _Harness) -> None:
response = harness.post(body={"name": "Rejected"}, conversation_id=str(uuid4()))
assert response.status_code == 404
assert response.json is not None
assert response.json["code"] == "conversation_not_found"
harness.assert_name("Original name")
harness.assert_closed()
@pytest.mark.parametrize("authorization", [None, "Basic rename-token", "Bearer invalid-token"])
def test_invalid_token_returns_401_before_rename(harness: _Harness, authorization: str | None) -> None:
response = harness.post(body={"name": "Rejected"}, authorization=authorization)
assert response.status_code == 401
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
assert response.json is not None
assert response.json["code"] == "unauthorized"
assert harness.admissions == harness.provisions == harness.queries == []
harness.assert_name("Original name")
harness.assert_closed()
@pytest.mark.parametrize(
("failure", "code"), [("disabled_api", "app_api_disabled"), ("archived_workspace", "workspace_archived")]
)
def test_disabled_app_or_workspace_stops_before_provisioning(harness: _Harness, failure: str, code: str) -> None:
with harness.factory.begin() as session:
if failure == "disabled_api":
target = session.get(App, harness.target.id)
assert target is not None
target.enable_api = False
else:
tenant = session.get(Tenant, harness.tenant.id)
assert tenant is not None
tenant.status = TenantStatus.ARCHIVE
response = harness.post(body={"name": "Rejected"}, user="new-user")
assert response.status_code == 403
assert response.json is not None
assert response.json["code"] == code
assert harness.provisions == harness.queries == []
with harness.factory() as session:
assert session.scalars(select(EndUser.id)).all() == [harness.end_user.id]
harness.assert_name("Original name")
harness.assert_closed()
@pytest.mark.parametrize("multiple_apps", [False, True], ids=["implicit-single-app", "explicit-multiple-apps"])
def test_resource_token_renames_bound_app_and_records_usage(harness: _Harness, multiple_apps: bool) -> None:
app_ids = (harness.target.id,)
if multiple_apps:
other = make_app(app_id=str(uuid4()), tenant_id=harness.tenant.id)
with harness.factory.begin() as session:
session.add(other)
app_ids = (other.id, harness.target.id)
services = harness.app.extensions["application_services"]
assert isinstance(services, _Services)
token = services.resource_access_tokens.create(
RequestContext(
request_id="rename-resource-token",
trace_id=None,
account_id=str(uuid4()),
active_workspace_id=harness.tenant.id,
),
name="Rename app token",
resources=tuple(ResourceAccessTokenResource(ResourceAccessTokenResourceType.APP, app_id) for app_id in app_ids),
)
response = harness.post(
body={"name": "Resource token rename"},
authorization=f"Bearer {token.token}",
requested_app_id=harness.target.id if multiple_apps else None,
)
assert response.status_code == 200
assert response.json is not None
assert response.json["name"] == "Resource token rename"
harness.assert_name("Resource token rename")
harness.assert_closed()
with harness.factory() as session:
stored_token = session.get(ResourceAccessToken, token.token_id)
assert stored_token is not None
assert stored_token.last_used_at is not None
assert session.scalars(select(EndUser.id)).all() == [harness.end_user.id]
@@ -0,0 +1,505 @@
"""Conversation-variable HTTP contracts through real admission, services, and SQLite."""
from collections.abc import Mapping
from dataclasses import dataclass
from datetime import datetime, timedelta
from http import HTTPStatus
from uuid import UUID, uuid4
import pytest
from pydantic import JsonValue
from sqlalchemy import event, select, update
from sqlalchemy.orm import Session, SessionTransaction
from werkzeug.test import TestResponse
from configs import dify_config
from constants.resource_access_token import ResourceAccessTokenResourceType
from controllers.service_api.app.conversation import ConversationVariableDetailApi, ConversationVariablesApi
from factories import variable_factory
from machinery.context import RequestContext
from models import Tenant, TenantStatus
from models.enums import ConversationFromSource
from models.model import App, AppMode, Conversation, EndUser
from models.resource_access_token import ResourceAccessToken
from models.workflow import ConversationVariable
from repositories.conversation_variable_repository import ConversationVariableRepository
from services.auth.resource_access_token_contracts import ResourceAccessTokenResource
from services.conversation_variable_command_service import ConversationVariableCommandService
from tests.e2e_tests.contracts.controllers.service_api.app import test_conversation_rename as rename_tests
from tests.e2e_tests.contracts.controllers.service_api.app.test_conversation_rename import _Harness as RenameHarness
from tests.e2e_tests.contracts.controllers.service_api.app.test_conversation_rename import _Services as RenameServices
from tests.helpers.unit.model_factories import make_app, make_conversation
rename_harness = rename_tests.harness
_CREATED_AT = datetime(2026, 1, 2, 3, 4, 5)
_UPDATED_AT = datetime(2026, 1, 3, 3, 4, 5)
@dataclass(frozen=True)
class _Services(RenameServices):
conversation_variables: ConversationVariableRepository
conversation_variable_commands: ConversationVariableCommandService
@dataclass(frozen=True)
class _Harness:
source: RenameHarness
row: ConversationVariable
def request(
self,
method: str,
*,
body: Mapping[str, JsonValue] | None = None,
query: Mapping[str, str | int] | None = None,
conversation_id: str | None = None,
variable_id: str | None = None,
user: str | None = "alice",
authorization: str | None = "Bearer rename-token",
requested_app_id: str | None = None,
) -> TestResponse:
headers = {"Authorization": authorization} if authorization is not None else {}
if requested_app_id is not None:
headers["X-Dify-App-ID"] = requested_app_id
url = f"/conversations/{conversation_id or self.source.conversation.id}/variables"
if method == "PUT":
url += f"/{variable_id or self.row.id}"
payload = dict(body) if body is not None else {"value": "Updated"}
if user is not None:
payload["user"] = user
return self.source.app.test_client().put(url, json=payload, headers=headers)
params = dict(query or {})
if user is not None:
params["user"] = user
return self.source.app.test_client().get(url, query_string=params, headers=headers)
def add_variable(
self,
name: str,
*,
value_type: str = "string",
value: JsonValue = "Original",
app_id: str | None = None,
conversation_id: str | None = None,
offset: int = 0,
) -> ConversationVariable:
row = _variable(
self.source,
name=name,
value_type=value_type,
value=value,
app_id=app_id,
conversation_id=conversation_id,
offset=offset,
)
with self.source.factory.begin() as session:
session.add(row)
return row
def assert_unchanged(self) -> None:
with self.source.factory() as session:
row = session.get(ConversationVariable, (self.row.id, self.row.conversation_id))
assert row is not None
assert row.data == self.row.data
assert row.updated_at == self.row.updated_at
def _variable(
source: RenameHarness,
*,
name: str,
value_type: str,
value: JsonValue,
app_id: str | None = None,
conversation_id: str | None = None,
offset: int = 0,
) -> ConversationVariable:
variable = variable_factory.build_conversation_variable_from_mapping(
{
"id": f"draft-{name}",
"name": name,
"description": "Saved variable description",
"value_type": value_type,
"value": value,
}
)
row = ConversationVariable.from_variable(
app_id=app_id or source.target.id,
conversation_id=conversation_id or source.conversation.id,
variable=variable,
)
row.created_at = _CREATED_AT + timedelta(seconds=offset)
row.updated_at = _UPDATED_AT
return row
@pytest.fixture
def harness(rename_harness: RenameHarness) -> _Harness:
original = rename_harness.app.extensions["application_services"]
assert isinstance(original, RenameServices)
repository = ConversationVariableRepository(session_factory=rename_harness.repository_factory)
rename_harness.app.extensions["application_services"] = _Services(
app_definitions=original.app_definitions,
app_scoped_end_users=original.app_scoped_end_users,
conversation_names=original.conversation_names,
resource_access_tokens=original.resource_access_tokens,
conversation_variables=repository,
conversation_variable_commands=ConversationVariableCommandService(variables=repository),
)
rename_harness.api.add_resource(ConversationVariablesApi, "/conversations/<uuid:conversation_id>/variables")
rename_harness.api.add_resource(
ConversationVariableDetailApi, "/conversations/<uuid:conversation_id>/variables/<uuid:variable_id>"
)
row = _variable(rename_harness, name="greeting", value_type="string", value="Original")
with rename_harness.factory.begin() as session:
session.add(row)
return _Harness(rename_harness, row)
@pytest.mark.parametrize("method", ["GET", "PUT"])
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT])
def test_supported_modes_preserve_wire_contract_and_close_admission_before_repository(
harness: _Harness, method: str, mode: AppMode
) -> None:
source = harness.source
with source.factory.begin() as session:
session.execute(update(App).where(App.id == source.target.id).values(mode=mode))
response = harness.request(method)
assert response.status_code == HTTPStatus.OK
assert response.headers["Content-Type"] == "application/json"
payload = response.get_json()
value = payload if method == "PUT" else payload["data"][0]
assert value == {
"id": harness.row.id,
"name": "greeting",
"value_type": "string",
"value": "Updated" if method == "PUT" else "Original",
"description": "Saved variable description",
"created_at": int(_CREATED_AT.timestamp()),
"updated_at": value["updated_at"] if method == "PUT" else int(_UPDATED_AT.timestamp()),
}
assert str(UUID(value["id"])) == value["id"]
with source.factory() as session:
stored = session.get(ConversationVariable, (harness.row.id, source.conversation.id))
assert stored is not None
variable = stored.to_variable()
assert variable.id == "draft-greeting"
assert variable.value == value["value"]
assert variable.selector == ["conversation", "greeting"]
if method == "PUT":
assert value["updated_at"] > int(_UPDATED_AT.timestamp())
assert value["updated_at"] == int(stored.updated_at.timestamp())
assert source.admissions
assert source.provisions
assert source.queries
assert source.scoped_sessions == []
source.assert_closed()
@pytest.mark.parametrize(
("value_type", "old_value", "new_value", "exposed_type", "wire_value"),
[
("string", "before", "", "string", ""),
("integer", 1, 0, "number", "0"),
("float", 1.5, 0.5, "number", "0.5"),
("boolean", True, False, "boolean", "False"),
("object", {"before": True}, {}, "object", "{}"),
("array[string]", ["before"], [], "array[string]", "[]"),
("array[number]", [1], [2, 3.5], "array[number]", "[2, 3.5]"),
("array[object]", [{"before": True}], [{"value": None}], "array[object]", "[{'value': None}]"),
("array[boolean]", [True], [False], "array[boolean]", "[False]"),
],
)
def test_update_retains_stringified_json_values_and_variable_metadata(
harness: _Harness,
value_type: str,
old_value: JsonValue,
new_value: JsonValue,
exposed_type: str,
wire_value: str,
) -> None:
row = harness.add_variable("boundary", value_type=value_type, value=old_value)
response = harness.request("PUT", variable_id=row.id, body={"value": new_value})
assert response.status_code == HTTPStatus.OK
assert response.get_json()["value_type"] == exposed_type
assert response.get_json()["value"] == wire_value
with harness.source.factory() as session:
stored = session.get(ConversationVariable, (row.id, row.conversation_id))
assert stored is not None
variable = stored.to_variable()
assert variable.value == new_value
assert variable.id == "draft-boundary"
assert variable.description == "Saved variable description"
assert variable.selector == ["conversation", "boundary"]
listed = harness.request("GET", query={"variable_name": "boundary"})
assert listed.status_code == HTTPStatus.OK
assert listed.get_json()["data"] == [response.get_json()]
harness.source.assert_closed()
def test_pagination_and_case_insensitive_filter_keep_cursor_inside_filtered_list(harness: _Harness) -> None:
first = harness.add_variable("Preference_first", offset=1)
second = harness.add_variable("preference_second", offset=2)
harness.add_variable("unrelated", offset=3)
first_response = harness.request("GET", query={"variable_name": "PREFERENCE", "limit": 1})
assert first_response.status_code == HTTPStatus.OK
assert first_response.get_json()["has_more"] is True
assert [row["id"] for row in first_response.get_json()["data"]] == [first.id]
second_response = harness.request("GET", query={"variable_name": "PREFERENCE", "limit": 1, "last_id": first.id})
assert second_response.get_json()["has_more"] is False
assert [row["id"] for row in second_response.get_json()["data"]] == [second.id]
bad_cursor = harness.request("GET", query={"variable_name": "PREFERENCE", "last_id": harness.row.id})
assert bad_cursor.status_code == HTTPStatus.NOT_FOUND
assert bad_cursor.get_json()["code"] == "conversation_variable_not_found"
harness.source.assert_closed()
def test_variable_name_underscore_is_literal_and_empty_cursor_means_first_page(harness: _Harness) -> None:
matching = harness.add_variable("pref_name")
harness.add_variable("prefXname")
response = harness.request("GET", query={"variable_name": "PREF_", "last_id": ""})
assert response.status_code == HTTPStatus.OK
assert [item["id"] for item in response.get_json()["data"]] == [matching.id]
harness.source.assert_closed()
@pytest.mark.parametrize("cursor", [False, True], ids=["no-cursor", "with-cursor"])
def test_other_conversation_and_app_variables_do_not_leak(harness: _Harness, cursor: bool) -> None:
other_conversation = make_conversation(
conversation_id=str(uuid4()),
app_id=harness.source.target.id,
name="Other",
inputs={},
from_source=ConversationFromSource.API,
from_end_user_id=harness.source.end_user.id,
)
with harness.source.factory.begin() as session:
session.add(other_conversation)
foreign = harness.add_variable("foreign", conversation_id=other_conversation.id)
harness.add_variable("wrong_app", app_id=str(uuid4()))
response = harness.request("GET", query={"last_id": foreign.id} if cursor else {})
if cursor:
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "conversation_variable_not_found"
else:
assert response.status_code == HTTPStatus.OK
assert [row["id"] for row in response.get_json()["data"]] == [harness.row.id]
denied_update = harness.request("PUT", variable_id=foreign.id)
assert denied_update.status_code == HTTPStatus.NOT_FOUND
assert denied_update.get_json()["code"] == "conversation_variable_not_found"
harness.assert_unchanged()
harness.source.assert_closed()
@pytest.mark.parametrize("method", ["GET", "PUT"])
@pytest.mark.parametrize("field", ["app_id", "from_end_user_id", "from_source", "from_account_id", "is_deleted"])
def test_conversation_ownership_is_required_for_read_and_write(harness: _Harness, method: str, field: str) -> None:
value: str | bool
if field == "is_deleted":
value = True
elif field == "from_source":
value = ConversationFromSource.CONSOLE
else:
value = str(uuid4())
with harness.source.factory.begin() as session:
session.execute(
update(Conversation).where(Conversation.id == harness.source.conversation.id).values({field: value})
)
response = harness.request(method)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json() == {
"code": "conversation_not_found",
"message": "The conversation was not found for this user and app.",
"status": HTTPStatus.NOT_FOUND,
}
harness.assert_unchanged()
harness.source.assert_closed()
@pytest.mark.parametrize("method", ["GET", "PUT"])
@pytest.mark.parametrize("user", [None, "", "another-user"])
def test_missing_or_other_end_user_cannot_access_variables(harness: _Harness, method: str, user: str | None) -> None:
response = harness.request(method, user=user)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "conversation_not_found"
harness.assert_unchanged()
harness.source.assert_closed()
@pytest.mark.parametrize("method", ["GET", "PUT"])
@pytest.mark.parametrize("change_app_too", [False, True], ids=["end-user-workspace", "app-workspace"])
def test_workspace_change_after_admission_blocks_variable_access(
harness: _Harness, method: str, change_app_too: bool
) -> None:
source = harness.source
def change_owner(_session: Session, _transaction: SessionTransaction) -> None:
with source.factory.begin() as session:
end_user = session.get(EndUser, source.end_user.id)
assert end_user is not None
end_user.tenant_id = str(uuid4())
if change_app_too:
target = session.get(App, source.target.id)
assert target is not None
target.tenant_id = end_user.tenant_id
event.listen(source.provision_factory, "after_transaction_end", change_owner, once=True)
try:
response = harness.request(method)
finally:
event.remove(source.provision_factory, "after_transaction_end", change_owner)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "conversation_not_found"
harness.assert_unchanged()
source.assert_closed()
@pytest.mark.parametrize("method", ["GET", "PUT"])
@pytest.mark.parametrize("authorization", [None, "Basic rename-token", "Bearer invalid-token"])
def test_invalid_credentials_stop_before_database_use_case(
harness: _Harness, method: str, authorization: str | None
) -> None:
response = harness.request(method, authorization=authorization)
assert response.status_code == HTTPStatus.UNAUTHORIZED
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
assert response.get_json()["code"] == "unauthorized"
source = harness.source
assert source.admissions == source.provisions == source.queries == []
harness.assert_unchanged()
source.assert_closed()
@pytest.mark.parametrize("method", ["GET", "PUT"])
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW, AppMode.AGENT])
def test_unsupported_modes_fail_before_repository(harness: _Harness, method: str, mode: AppMode) -> None:
with harness.source.factory.begin() as session:
session.execute(update(App).where(App.id == harness.source.target.id).values(mode=mode))
response = harness.request(method)
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "not_chat_app"
assert harness.source.queries == []
harness.assert_unchanged()
harness.source.assert_closed()
@pytest.mark.parametrize(
"query",
[
{"limit": "0"},
{"limit": "101"},
{"last_id": "invalid"},
{"variable_name": ""},
{"variable_name": "a" * 256},
{"variable_name": "foo'; DROP TABLE app;"},
],
)
def test_invalid_query_returns_422_before_repository(harness: _Harness, query: dict[str, str]) -> None:
response = harness.request("GET", query=query)
assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
assert response.get_json()["code"] == "unprocessable_entity"
assert harness.source.queries == []
harness.source.assert_closed()
def test_missing_value_returns_422_without_write(harness: _Harness) -> None:
response = harness.request("PUT", body={})
assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
assert response.get_json()["code"] == "unprocessable_entity"
assert harness.source.queries == []
harness.assert_unchanged()
harness.source.assert_closed()
@pytest.mark.parametrize("new_value", [0, False, [], {}, None])
def test_type_mismatch_returns_specific_error_and_rolls_back(harness: _Harness, new_value: JsonValue) -> None:
response = harness.request("PUT", body={"value": new_value})
assert response.status_code == HTTPStatus.BAD_REQUEST
error = response.get_json()
assert error["code"] == "conversation_variable_type_mismatch"
assert "variable 'greeting' expects string" in error["message"]
assert "but got" in error["message"]
assert error["status"] == HTTPStatus.BAD_REQUEST
harness.assert_unchanged()
harness.source.assert_closed()
@pytest.mark.parametrize("method", ["GET", "PUT"])
def test_missing_variable_or_cursor_returns_specific_404(harness: _Harness, method: str) -> None:
variable_id = str(uuid4())
response = harness.request(method, query={"last_id": variable_id}, variable_id=variable_id)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json() == {
"code": "conversation_variable_not_found",
"message": "The conversation variable was not found for this conversation.",
"status": HTTPStatus.NOT_FOUND,
}
harness.assert_unchanged()
harness.source.assert_closed()
@pytest.mark.parametrize("method", ["GET", "PUT"])
@pytest.mark.parametrize("multiple_apps", [False, True], ids=["single-app", "multiple-apps"])
def test_resource_token_reuses_bound_end_user_and_records_usage(
harness: _Harness, method: str, multiple_apps: bool
) -> None:
source = harness.source
app_ids = (source.target.id,)
if multiple_apps:
other = make_app(app_id=str(uuid4()), tenant_id=source.tenant.id)
with source.factory.begin() as session:
session.add(other)
app_ids = (other.id, source.target.id)
services = source.app.extensions["application_services"]
assert isinstance(services, _Services)
token = services.resource_access_tokens.create(
RequestContext(
request_id="variable-resource-token",
trace_id=None,
account_id=str(uuid4()),
active_workspace_id=source.tenant.id,
),
name="Variable app token",
resources=tuple(ResourceAccessTokenResource(ResourceAccessTokenResourceType.APP, app_id) for app_id in app_ids),
)
response = harness.request(
method,
authorization=f"Bearer {token.token}",
requested_app_id=source.target.id if multiple_apps else None,
)
assert response.status_code == HTTPStatus.OK
source.assert_closed()
with source.factory() as session:
stored_token = session.get(ResourceAccessToken, token.token_id)
assert stored_token is not None
assert stored_token.last_used_at is not None
assert session.scalars(select(EndUser.id)).all() == [source.end_user.id]
@pytest.mark.parametrize("method", ["GET", "PUT"])
@pytest.mark.parametrize(
("failure", "code"), [("disabled_api", "app_api_disabled"), ("archived_workspace", "workspace_archived")]
)
def test_disabled_app_or_workspace_prevents_provisioning(
harness: _Harness, method: str, failure: str, code: str
) -> None:
source = harness.source
with source.factory.begin() as session:
if failure == "disabled_api":
session.execute(update(App).where(App.id == source.target.id).values(enable_api=False))
else:
session.execute(update(Tenant).where(Tenant.id == source.tenant.id).values(status=TenantStatus.ARCHIVE))
response = harness.request(method, user="new-user")
assert response.status_code == HTTPStatus.FORBIDDEN
assert response.get_json()["code"] == code
assert source.provisions == source.queries == []
harness.assert_unchanged()
source.assert_closed()
def test_factory_size_rejection_preserves_error_and_rolls_back(harness: _Harness) -> None:
response = harness.request("PUT", body={"value": "x" * (dify_config.MAX_VARIABLE_SIZE + 1)})
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "invalid_param"
assert f"exceeds limit {dify_config.MAX_VARIABLE_SIZE}" in response.get_json()["message"]
harness.assert_unchanged()
harness.source.assert_closed()
@@ -0,0 +1,519 @@
"""Feedback endpoints through real API token admission, repositories, and SQLite."""
from collections.abc import Iterator
from dataclasses import dataclass
from datetime import datetime
from decimal import Decimal
from http import HTTPStatus
from uuid import uuid4
import pytest
from flask import Flask
from flask_login import LoginManager
from redis import Redis
from sqlalchemy import Connection, Engine, delete, event, select, update
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
from constants.resource_access_token import ResourceAccessTokenResourceType
from controllers.service_api.app.message import AppGetFeedbacksApi, MessageFeedbackApi, MessageListApi
from enums import DeploymentEdition
from extensions.ext_application_services import build_application_services
from extensions.ext_database import db
from extensions.ext_redis import RedisClientWrapper
from libs.external_api import ExternalApi
from machinery.context import RequestContext
from models.account import Account, Tenant, TenantAccountJoin, TenantAccountRole, TenantStatus
from models.enums import ApiTokenType, ConversationFromSource, EndUserType, FeedbackFromSource, FeedbackRating
from models.model import ApiToken, App, EndUser, Message, MessageFeedback
from models.resource_access_token import ResourceAccessToken
from services.auth.resource_access_token_contracts import ResourceAccessTokenCreateResult, ResourceAccessTokenResource
from services.resource_access_token_service import ResourceAccessTokenService
from tests.helpers.unit.model_factories import make_app, make_conversation, make_end_user, make_message
@dataclass(frozen=True)
class _Harness:
app: Flask
target: App
tenant: Tenant
owner: Account
end_user: EndUser
message: Message
factory: sessionmaker[Session]
sessions: list[Session]
resource_access_tokens: ResourceAccessTokenService
end_user_queries: list[str]
def post(
self,
payload: dict[str, object],
*,
user: str | None = "alice",
message_id: str | None = None,
authorization: str | None = "Bearer feedback-test-token",
) -> TestResponse:
body = dict(payload)
if user is not None:
body["user"] = user
headers: dict[str, str] = {"Authorization": authorization} if authorization is not None else {}
return self.app.test_client().post(
f"/messages/{message_id or self.message.id}/feedbacks", json=body, headers=headers
)
def get(
self,
query: str = "",
*,
authorization: str = "Bearer feedback-test-token",
requested_app_id: str | None = None,
) -> TestResponse:
headers = {"Authorization": authorization}
if requested_app_id is not None:
headers["X-Dify-App-ID"] = requested_app_id
return self.app.test_client().get(f"/app/feedbacks{query}", headers=headers)
def create_resource_token(self, *app_ids: str) -> ResourceAccessTokenCreateResult:
return self.resource_access_tokens.create(
RequestContext(
request_id="feedback-resource-token-test",
trace_id=None,
account_id=self.owner.id,
active_workspace_id=self.tenant.id,
),
name="Feedback integration",
resources=tuple(
ResourceAccessTokenResource(ResourceAccessTokenResourceType.APP, app_id) for app_id in app_ids
),
)
def feedbacks(self) -> list[MessageFeedback]:
with self.factory() as session:
return list(session.scalars(select(MessageFeedback)).all())
def assert_closed(self) -> None:
assert self.sessions
assert all(not session.in_transaction() and not session.identity_map for session in self.sessions)
@pytest.fixture
def harness(sqlite_engine: Engine, sqlite_session_factory: sessionmaker[Session]) -> Iterator[_Harness]:
tenant = Tenant(name="API feedback workspace")
target = make_app(app_id=str(uuid4()), tenant_id=tenant.id)
owner = Account(name="Owner", email=f"owner-{tenant.id}@example.com")
membership = TenantAccountJoin(tenant_id=tenant.id, account_id=owner.id, role=TenantAccountRole.OWNER)
end_user = make_end_user(
end_user_id=str(uuid4()),
tenant_id=tenant.id,
app_id=target.id,
end_user_type=EndUserType.SERVICE_API,
session_id="alice",
external_user_id="alice",
)
conversation = make_conversation(
conversation_id=str(uuid4()),
inputs={},
app_id=target.id,
from_source=ConversationFromSource.API,
from_end_user_id=end_user.id,
)
message = make_message(
message_id=str(uuid4()),
inputs={},
message={},
query="question",
answer="answer",
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
currency="USD",
app_id=target.id,
conversation_id=conversation.id,
from_source=ConversationFromSource.API,
from_end_user_id=end_user.id,
)
token = ApiToken(app_id=target.id, tenant_id=tenant.id, type=ApiTokenType.APP, token="feedback-test-token")
with sqlite_session_factory.begin() as session:
session.add_all([tenant, owner, membership, target, end_user, conversation, message, token])
app = Flask(__name__)
app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))
db.init_app(app)
LoginManager(app)
factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
sessions: list[Session] = []
end_user_queries: list[str] = []
def track(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
assert all(not previous.in_transaction() for previous in sessions if previous is not session)
sessions.append(session)
def track_end_user_queries(_connection: Connection, _cursor: object, statement: str, *_args: object) -> None:
if "end_users" in statement:
end_user_queries.append(statement)
event.listen(sqlite_session_factory, "after_begin", track)
event.listen(factory, "after_begin", track)
event.listen(db.session.session_factory, "after_begin", track)
event.listen(sqlite_engine, "before_cursor_execute", track_end_user_queries)
redis_client = Redis()
redis = RedisClientWrapper()
redis.initialize(redis_client)
services = build_application_services(
database_client=factory, deployment_edition=DeploymentEdition.COMMUNITY, initialization_password="", redis=redis
)
app.extensions["application_services"] = services
api = ExternalApi(app)
api.add_resource(MessageFeedbackApi, "/messages/<uuid:message_id>/feedbacks")
api.add_resource(MessageListApi, "/messages")
api.add_resource(AppGetFeedbacksApi, "/app/feedbacks")
yield _Harness(
app,
target,
tenant,
owner,
end_user,
message,
sqlite_session_factory,
sessions,
services.resource_access_tokens,
end_user_queries,
)
event.remove(sqlite_session_factory, "after_begin", track)
event.remove(factory, "after_begin", track)
event.remove(db.session.session_factory, "after_begin", track)
event.remove(sqlite_engine, "before_cursor_execute", track_end_user_queries)
redis_client.close()
with app.app_context():
db.session.remove()
db.engine.dispose()
def test_feedback_create_update_and_revoke(harness: _Harness) -> None:
created = harness.post({"rating": "like", "content": "useful"})
assert created.status_code == HTTPStatus.OK
assert created.get_json() == {"result": "success"}
assert created.headers["Content-Type"] == "application/json"
[feedback] = harness.feedbacks()
original_id = feedback.id
assert feedback.from_source == FeedbackFromSource.USER
assert feedback.from_end_user_id == harness.end_user.id
assert feedback.from_account_id is None
assert feedback.app_id == harness.target.id
assert feedback.message_id == harness.message.id
assert feedback.conversation_id == harness.message.conversation_id
assert feedback.rating == FeedbackRating.LIKE
assert feedback.content == "useful"
updated = harness.post({"rating": "dislike", "content": None})
assert updated.status_code == HTTPStatus.OK
[feedback] = harness.feedbacks()
assert feedback.id == original_id
assert feedback.rating == FeedbackRating.DISLIKE
assert feedback.content is None
assert harness.post({"rating": None}).get_json() == {"result": "success"}
assert harness.feedbacks() == []
harness.assert_closed()
@pytest.mark.parametrize("rating", [None, "like"])
def test_other_end_user_cannot_change_the_message_feedback(harness: _Harness, rating: str | None) -> None:
assert harness.post({"rating": "like"}).status_code == HTTPStatus.OK
response = harness.post({"rating": rating}, user="other-user")
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "not_found"
[feedback] = harness.feedbacks()
assert feedback.from_end_user_id == harness.end_user.id
assert feedback.rating == FeedbackRating.LIKE
harness.assert_closed()
@pytest.mark.parametrize("field", ["app_id", "from_source", "from_account_id"])
def test_foreign_app_or_console_message_is_not_accessible(harness: _Harness, field: str) -> None:
value = ConversationFromSource.CONSOLE if field == "from_source" else str(uuid4())
with harness.factory.begin() as session:
session.execute(update(Message).where(Message.id == harness.message.id).values({field: value}))
response = harness.post({"rating": "like"})
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "not_found"
assert harness.feedbacks() == []
def test_revoke_without_existing_feedback_has_precise_error(harness: _Harness) -> None:
response = harness.post({"rating": None})
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "message_feedback_rating_required"
assert harness.feedbacks() == []
harness.assert_closed()
@pytest.mark.parametrize("user", [None, ""])
def test_missing_user_does_not_write_feedback(harness: _Harness, user: str | None) -> None:
response = harness.post({"rating": "like"}, user=user)
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["message"] == "Arg user must be provided."
assert harness.feedbacks() == []
@pytest.mark.parametrize("authorization", [None, "Basic feedback-test-token"])
def test_invalid_authorization_is_rejected(harness: _Harness, authorization: str | None) -> None:
response = harness.post({"rating": "like"}, authorization=authorization)
assert response.status_code == HTTPStatus.UNAUTHORIZED
assert response.get_json()["code"] == "unauthorized"
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
assert harness.sessions == []
def test_app_only_list_rejects_a_key_from_another_workspace(harness: _Harness) -> None:
with harness.factory.begin() as session:
foreign_tenant = Tenant(name="Other API key workspace")
session.add(foreign_tenant)
token = session.scalar(select(ApiToken).where(ApiToken.token == "feedback-test-token"))
assert token is not None
token.tenant_id = foreign_tenant.id
response = harness.get("?user=must-not-be-provisioned")
assert response.status_code == HTTPStatus.FORBIDDEN
assert response.get_json() == {
"code": "app_not_found",
"message": "The app no longer exists.",
"status": HTTPStatus.FORBIDDEN,
}
assert harness.end_user_queries == []
harness.assert_closed()
def test_app_only_list_accepts_a_historical_key_without_workspace(harness: _Harness) -> None:
with harness.factory.begin() as session:
token = session.scalar(select(ApiToken).where(ApiToken.token == "feedback-test-token"))
assert token is not None
token.tenant_id = None
response = harness.get("?user=must-not-be-provisioned")
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"data": []}
assert harness.end_user_queries == []
harness.assert_closed()
def test_invalid_rating_is_rejected(harness: _Harness) -> None:
response = harness.post({"rating": "neutral"})
assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
assert response.get_json()["code"] == "unprocessable_entity"
assert harness.feedbacks() == []
def test_feedback_list_preserves_shape_nulls_timestamp_format_and_stable_order(harness: _Harness) -> None:
timestamp = datetime(2024, 1, 2, 3, 4, 5)
records = [
MessageFeedback(
app_id=harness.target.id,
conversation_id=harness.message.conversation_id,
message_id=harness.message.id,
rating=FeedbackRating.LIKE,
from_source=FeedbackFromSource.USER,
from_end_user_id=harness.end_user.id,
),
MessageFeedback(
app_id=harness.target.id,
conversation_id=harness.message.conversation_id,
message_id=harness.message.id,
rating=FeedbackRating.DISLIKE,
content="admin comment",
from_source=FeedbackFromSource.ADMIN,
from_account_id=harness.owner.id,
),
MessageFeedback(
app_id=str(uuid4()),
conversation_id=str(uuid4()),
message_id=str(uuid4()),
rating=FeedbackRating.LIKE,
from_source=FeedbackFromSource.USER,
from_end_user_id=str(uuid4()),
),
]
for index, record in enumerate(records, start=1):
record.id = f"00000000-0000-0000-0000-{index:012d}"
record.created_at = timestamp
record.updated_at = timestamp
with harness.factory.begin() as session:
session.add_all(records)
response = harness.get()
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"data": [records[1].to_dict(), records[0].to_dict()]}
assert response.headers["Content-Type"] == "application/json"
assert harness.get("?page=2&limit=1").get_json() == {"data": [records[0].to_dict()]}
assert harness.get("?page=3&limit=1").get_json() == {"data": []}
harness.assert_closed()
@pytest.mark.parametrize("query", ["?page=0", "?limit=0", "?limit=102"])
def test_list_rejects_invalid_page_or_limit(harness: _Harness, query: str) -> None:
response = harness.get(query)
assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
assert response.get_json()["code"] == "unprocessable_entity"
harness.assert_closed()
@pytest.mark.parametrize("missing", ["membership", "account"])
def test_app_only_list_still_requires_workspace_owner(harness: _Harness, missing: str) -> None:
with harness.factory.begin() as session:
if missing == "membership":
session.execute(delete(TenantAccountJoin).where(TenantAccountJoin.tenant_id == harness.tenant.id))
else:
session.execute(delete(Account).where(Account.id == harness.owner.id))
response = harness.get()
assert response.status_code == HTTPStatus.UNAUTHORIZED
assert response.get_json()["code"] == "unauthorized"
assert response.get_json()["message"] == "Tenant owner account not found or tenant is not active."
harness.assert_closed()
@pytest.mark.parametrize("role", [TenantAccountRole.ADMIN, TenantAccountRole.EDITOR])
def test_app_only_list_rejects_non_owner_membership(harness: _Harness, role: TenantAccountRole) -> None:
with harness.factory.begin() as session:
membership = session.scalar(
select(TenantAccountJoin).where(
TenantAccountJoin.tenant_id == harness.tenant.id,
TenantAccountJoin.account_id == harness.owner.id,
)
)
assert membership is not None
membership.role = role
response = harness.get()
assert response.status_code == HTTPStatus.UNAUTHORIZED
assert response.get_json()["code"] == "unauthorized"
assert response.get_json()["message"] == "Tenant owner account not found or tenant is not active."
harness.assert_closed()
@pytest.mark.parametrize("endpoint", ["write", "list"])
def test_archived_workspace_is_rejected(harness: _Harness, endpoint: str) -> None:
with harness.factory.begin() as session:
session.execute(update(Tenant).where(Tenant.id == harness.tenant.id).values(status=TenantStatus.ARCHIVE))
response = harness.post({"rating": "like"}) if endpoint == "write" else harness.get()
assert response.status_code == HTTPStatus.FORBIDDEN
assert response.get_json()["code"] == "workspace_archived"
assert harness.feedbacks() == []
harness.assert_closed()
def test_empty_feedback_list_keeps_data_array(harness: _Harness) -> None:
response = harness.get("?limit=101")
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"data": []}
harness.assert_closed()
def test_resource_token_can_create_and_list_feedback_without_provisioning_on_get(harness: _Harness) -> None:
token = harness.create_resource_token(harness.target.id)
authorization = f"Bearer {token.token}"
response = harness.post({"rating": "like", "content": "resource token feedback"}, authorization=authorization)
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"result": "success"}
[feedback] = harness.feedbacks()
assert feedback.from_end_user_id == harness.end_user.id
assert feedback.content == "resource token feedback"
assert harness.end_user_queries
harness.end_user_queries.clear()
response = harness.get("?user=must-not-be-provisioned", authorization=authorization)
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"data": [feedback.to_dict()]}
assert harness.end_user_queries == []
with harness.factory() as session:
assert session.scalars(select(EndUser.id)).all() == [harness.end_user.id]
stored_token = session.get(ResourceAccessToken, token.token_id)
assert stored_token is not None
assert stored_token.last_used_at is not None
harness.assert_closed()
@pytest.mark.parametrize("select_other_app", [False, True], ids=["first-app", "second-app"])
def test_multiple_app_resource_token_lists_only_header_selected_app(harness: _Harness, select_other_app: bool) -> None:
other = make_app(app_id=str(uuid4()), tenant_id=harness.tenant.id)
records = [
MessageFeedback(
app_id=app_id,
conversation_id=str(uuid4()),
message_id=str(uuid4()),
rating=FeedbackRating.LIKE,
from_source=FeedbackFromSource.ADMIN,
from_account_id=harness.owner.id,
content=app_id,
)
for app_id in (harness.target.id, other.id)
]
with harness.factory.begin() as session:
session.add_all([other, *records])
token = harness.create_resource_token(harness.target.id, other.id)
selected = records[1 if select_other_app else 0]
response = harness.get(
"?user=must-not-be-provisioned",
authorization=f"Bearer {token.token}",
requested_app_id=selected.app_id,
)
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"data": [selected.to_dict()]}
assert harness.end_user_queries == []
with harness.factory() as session:
stored_token = session.get(ResourceAccessToken, token.token_id)
assert stored_token is not None
assert stored_token.last_used_at is not None
harness.assert_closed()
def test_multiple_app_resource_token_list_requires_app_header(harness: _Harness) -> None:
other = make_app(app_id=str(uuid4()), tenant_id=harness.tenant.id)
with harness.factory.begin() as session:
session.add(other)
token = harness.create_resource_token(harness.target.id, other.id)
response = harness.get("?user=must-not-be-provisioned", authorization=f"Bearer {token.token}")
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json() == {
"code": "invalid_param",
"message": "App ID is required when a resource access token is bound to multiple apps.",
"status": 400,
}
assert harness.end_user_queries == []
with harness.factory() as session:
stored_token = session.get(ResourceAccessToken, token.token_id)
assert stored_token is not None
assert stored_token.last_used_at is None
harness.assert_closed()
@pytest.mark.parametrize("missing", ["membership", "account"])
def test_resource_token_requires_owner_for_list_but_not_feedback_write(harness: _Harness, missing: str) -> None:
token = harness.create_resource_token(harness.target.id)
authorization = f"Bearer {token.token}"
with harness.factory.begin() as session:
if missing == "membership":
session.execute(delete(TenantAccountJoin).where(TenantAccountJoin.tenant_id == harness.tenant.id))
else:
session.execute(delete(Account).where(Account.id == harness.owner.id))
response = harness.get("?user=must-not-be-provisioned", authorization=authorization)
assert response.status_code == HTTPStatus.UNAUTHORIZED
assert response.get_json()["code"] == "unauthorized"
assert response.get_json()["message"] == "Tenant owner account not found or tenant is not active."
assert harness.end_user_queries == []
assert harness.feedbacks() == []
harness.assert_closed()
response = harness.post({"rating": "like"}, authorization=authorization)
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"result": "success"}
[feedback] = harness.feedbacks()
assert feedback.from_end_user_id == harness.end_user.id
assert harness.end_user_queries
harness.assert_closed()
@@ -0,0 +1,249 @@
"""Service API history through real API token admission and detached SQLite reads."""
import json
from datetime import datetime, timedelta
from decimal import Decimal
from http import HTTPStatus
from uuid import uuid4
import pytest
from sqlalchemy import delete, select, update
from werkzeug.test import TestResponse
from models.account import Tenant, TenantStatus
from models.enums import DEFAULT_END_USER_SESSION_ID, ConversationFromSource
from models.model import App, AppMode, Conversation, EndUser, Message
from tests.e2e_tests.contracts.controllers.service_api.app.test_message_feedback import _Harness
from tests.e2e_tests.contracts.controllers.service_api.app.test_message_feedback import harness as admission_harness
from tests.helpers.unit.model_factories import make_conversation, make_message
harness = admission_harness
def _get(
harness: _Harness,
*,
user: str | None = "alice",
authorization: str | None = "Bearer feedback-test-token",
**query: object,
) -> TestResponse:
query_string = {"conversation_id": harness.message.conversation_id, **query}
if user is not None:
query_string["user"] = user
headers: dict[str, str] = {"Authorization": authorization} if authorization is not None else {}
return harness.app.test_client().get("/messages", query_string=query_string, headers=headers)
def test_history_shape_omits_web_metadata_and_preserves_nulls(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(
update(Message)
.where(Message.id == harness.message.id)
.values(
created_at=datetime(2024, 1, 1, 12),
message_metadata=json.dumps({"private": "web-only", "retriever_resources": []}),
message_tokens=3,
answer_tokens=5,
total_price=Decimal("0.0012"),
provider_response_latency=0.25,
)
)
response = _get(harness)
assert response.status_code == HTTPStatus.OK
assert response.headers["Content-Type"] == "application/json"
body = response.get_json()
assert body["limit"] == 20
assert body["has_more"] is False
[item] = body["data"]
assert item == {
"id": harness.message.id,
"conversation_id": harness.message.conversation_id,
"parent_message_id": None,
"inputs": {},
"query": "question",
"answer": "answer",
"feedback": None,
"retriever_resources": [],
"created_at": int(datetime(2024, 1, 1, 12).timestamp()),
"agent_thoughts": [],
"message_files": [],
"message_tokens": 3,
"answer_tokens": 5,
"provider_response_latency": 0.25,
"total_price": item["total_price"],
"currency": "USD",
"status": "normal",
"error": None,
"extra_contents": [],
"total_tokens": 8,
}
assert isinstance(item["total_price"], str)
assert Decimal(item["total_price"]) == Decimal("0.0012")
harness.assert_closed()
def test_history_pages_are_ascending_and_cursor_excludes_equal_timestamps(harness: _Harness) -> None:
created_at = datetime(2024, 1, 1)
messages = [
make_message(
message_id=str(uuid4()),
app_id=harness.target.id,
conversation_id=harness.message.conversation_id,
inputs={},
query=f"question-{index}",
message={},
answer=f"answer-{index}",
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
currency="USD",
from_source=ConversationFromSource.API,
from_end_user_id=harness.end_user.id,
created_at=created_at + timedelta(seconds=index),
)
for index in (1, 2)
]
with harness.factory.begin() as session:
session.execute(update(Message).where(Message.id == harness.message.id).values(created_at=created_at))
session.add_all(messages)
response = _get(harness, limit=2)
assert response.status_code == HTTPStatus.OK
body = response.get_json()
assert body["limit"] == 2
assert body["has_more"] is True
assert [item["id"] for item in body["data"]] == [message.id for message in messages]
previous = _get(harness, first_id=messages[0].id, limit=2).get_json()
assert previous["has_more"] is False
assert [item["id"] for item in previous["data"]] == [harness.message.id]
with harness.factory.begin() as session:
session.execute(
update(Message).where(Message.id == harness.message.id).values(created_at=messages[0].created_at)
)
assert _get(harness, first_id=messages[0].id, limit=2).get_json() == {"limit": 2, "has_more": False, "data": []}
harness.assert_closed()
def test_empty_conversation_keeps_pagination_shape(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(delete(Message).where(Message.id == harness.message.id))
response = _get(harness, limit=100)
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"limit": 100, "has_more": False, "data": []}
harness.assert_closed()
@pytest.mark.parametrize("user", [None, ""])
def test_optional_user_is_provisioned_and_cannot_read_existing_users_history(
harness: _Harness, user: str | None
) -> None:
response = _get(harness, user=user)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["message"] == "Conversation Not Exists."
with harness.factory() as session:
users = session.scalars(select(EndUser).where(EndUser.app_id == harness.target.id)).all()
anonymous = [end_user for end_user in users if end_user.id != harness.end_user.id]
assert len(anonymous) == 1
assert anonymous[0].session_id == DEFAULT_END_USER_SESSION_ID
assert anonymous[0].external_user_id == DEFAULT_END_USER_SESSION_ID
harness.assert_closed()
def test_other_end_user_cannot_read_history(harness: _Harness) -> None:
response = _get(harness, user="other-user")
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["message"] == "Conversation Not Exists."
harness.assert_closed()
def test_resource_token_lists_only_the_admitted_users_history(harness: _Harness) -> None:
token = harness.create_resource_token(harness.target.id)
authorization = f"Bearer {token.token}"
response = _get(harness, authorization=authorization)
assert response.status_code == HTTPStatus.OK
body = response.get_json()
assert body["limit"] == 20
assert body["has_more"] is False
assert [item["id"] for item in body["data"]] == [harness.message.id]
harness.assert_closed()
response = _get(harness, authorization=authorization, user="other-user")
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["message"] == "Conversation Not Exists."
harness.assert_closed()
@pytest.mark.parametrize("field", ["app_id", "from_end_user_id", "from_account_id", "from_source"])
def test_conversation_ownership_is_enforced(harness: _Harness, field: str) -> None:
value = ConversationFromSource.CONSOLE if field == "from_source" else str(uuid4())
with harness.factory.begin() as session:
session.execute(
update(Conversation).where(Conversation.id == harness.message.conversation_id).values({field: value})
)
response = _get(harness)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "not_found"
assert response.get_json()["message"] == "Conversation Not Exists."
harness.assert_closed()
@pytest.mark.parametrize("query", [{"conversation_id": str(uuid4())}, {"first_id": str(uuid4())}])
def test_missing_conversation_or_cursor_has_precise_error(harness: _Harness, query: dict[str, str]) -> None:
response = _get(harness, **query)
assert response.status_code == HTTPStatus.NOT_FOUND
expected = "Conversation Not Exists." if "conversation_id" in query else "First Message Not Exists."
assert response.get_json()["message"] == expected
harness.assert_closed()
def test_cursor_from_another_conversation_is_rejected(harness: _Harness) -> None:
conversation = make_conversation(
conversation_id=str(uuid4()),
app_id=harness.target.id,
inputs={},
from_source=ConversationFromSource.API,
from_end_user_id=harness.end_user.id,
)
with harness.factory.begin() as session:
session.add(conversation)
response = _get(harness, conversation_id=conversation.id, first_id=harness.message.id)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["message"] == "First Message Not Exists."
harness.assert_closed()
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
def test_non_chat_app_is_rejected(harness: _Harness, mode: AppMode) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
response = _get(harness)
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "not_chat_app"
harness.assert_closed()
@pytest.mark.parametrize("query", [{"limit": "0"}, {"limit": "101"}, {"conversation_id": "bad"}, {"first_id": "bad"}])
def test_invalid_query_is_rejected(harness: _Harness, query: dict[str, str]) -> None:
response = _get(harness, **query)
assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
assert response.get_json()["code"] == "unprocessable_entity"
harness.assert_closed()
@pytest.mark.parametrize("authorization", [None, "Basic feedback-test-token"])
def test_invalid_authorization_is_rejected(harness: _Harness, authorization: str | None) -> None:
response = _get(harness, authorization=authorization)
assert response.status_code == HTTPStatus.UNAUTHORIZED
assert response.get_json()["code"] == "unauthorized"
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
assert harness.sessions == []
def test_archived_workspace_is_rejected(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(update(Tenant).where(Tenant.id == harness.tenant.id).values(status=TenantStatus.ARCHIVE))
response = _get(harness)
assert response.status_code == HTTPStatus.FORBIDDEN
assert response.get_json()["code"] == "workspace_archived"
harness.assert_closed()
@@ -0,0 +1,788 @@
"""Service API suggested questions with real admission, end-user provisioning, and SQLite."""
from collections.abc import Iterator
from dataclasses import FrozenInstanceError, asdict, dataclass
from decimal import Decimal
from typing import Literal
from uuid import uuid4
import pytest
from flask import Flask
from flask_login import LoginManager, current_user
from sqlalchemy import Connection, Engine, delete, event, select, text
from sqlalchemy.orm import Session, SessionTransaction, object_session, sessionmaker
from werkzeug.test import TestResponse
from constants.resource_access_token import ResourceAccessTokenResourceType
from controllers.service_api.app.message import MessageSuggestedApi
from controllers.service_api.flask_admission import service_api_end_user_admission
from controllers.service_api.wraps import FetchUserArg, WhereisUserArg, validate_app_token
from extensions.application_services.resource_access_token import build_resource_access_token_service
from extensions.ext_database import db
from graphon.model_runtime.entities.model_entities import ModelType
from libs.external_api import ExternalApi
from machinery.context import RequestContext, ServiceApiEndUserContext
from models import Tenant, TenantStatus
from models.agent import Agent, AgentScope, AgentSource
from models.enums import ApiTokenType, ConversationFromSource, EndUserType
from models.model import ApiToken, App, AppMode, AppModelConfig, Conversation, EndUser, Message
from models.provider import Provider, TenantDefaultModel
from models.resource_access_token import ResourceAccessToken, ResourceAccessTokenRelation
from models.workflow import Workflow, WorkflowType
from repositories.app_definition_query_repository import AppDefinitionQueryRepository
from repositories.app_scoped_end_user_repository import AppScopedEndUserRepo
from repositories.message_repository import MessageRepository
from services.app_definition_query_service import AppDefinitionQueryService, AppDefinitionUnavailableError
from services.app_scoped_end_user_service import AppScopedEndUserService
from services.auth.resource_access_token_contracts import ResourceAccessTokenCreateResult, ResourceAccessTokenResource
from services.errors.app import AppAbnormalStatusError, AppApiDisabledError
from services.errors.workspace import WorkspaceArchivedError, WorkspaceNotFoundError
from services.message_suggested_questions_generator import SuggestedQuestionsGenerator
from services.message_suggested_questions_queries import SuggestedQuestionsQuery
from services.message_suggested_questions_service import (
MessageSuggestedQuestions,
MessageSuggestedQuestionsService,
)
from services.resource_access_token_service import ResourceAccessTokenService
from tests.helpers.unit.model_factories import make_app, make_conversation, make_end_user, make_message
from tests.helpers.unit.services.tracing_ports import untraced_scopes
@dataclass(frozen=True)
class _EndUsers:
commands: AppScopedEndUserService[EndUser]
@dataclass(frozen=True)
class _Services:
app_definitions: AppDefinitionQueryService
message_suggested_questions: MessageSuggestedQuestions
app_scoped_end_users: _EndUsers
resource_access_tokens: ResourceAccessTokenService
@dataclass(frozen=True)
class _Harness:
app: Flask
target: App
tenant: Tenant
end_user: EndUser
conversation: Conversation
config: AppModelConfig
provision_factory: sessionmaker[Session]
factory: sessionmaker[Session]
message_id: str
admissions: list[Session]
provisions: list[Session]
queries: list[Session]
scoped_sessions: list[Session]
def get(
self,
*,
user: str | None = "alice",
authorization: str | None = "Bearer test-token",
requested_app_id: str | None = None,
) -> TestResponse:
headers: dict[str, str] = {"Authorization": authorization} if authorization is not None else {}
if requested_app_id is not None:
headers["X-Dify-App-ID"] = requested_app_id
query: dict[str, str] = {"user": user} if user is not None else {}
return self.app.test_client().get(f"/messages/{self.message_id}/suggested", query_string=query, headers=headers)
def assert_closed(self, *, caller: Session | None = None) -> None:
assert all(
not session.in_transaction() and not session.identity_map
for session in self.admissions + self.provisions + self.queries + self.scoped_sessions
if session is not caller
)
@pytest.fixture
def harness(
_empty_model_provider_daemon: None,
sqlite_engine: Engine,
sqlite_session_factory: sessionmaker[Session],
) -> Iterator[_Harness]:
tenant = Tenant(name="API workspace")
target = make_app(app_id=str(uuid4()), tenant_id=tenant.id)
end_user = make_end_user(
end_user_id=str(uuid4()),
tenant_id=tenant.id,
app_id=target.id,
end_user_type=EndUserType.SERVICE_API,
session_id="alice",
external_user_id="alice",
)
config = AppModelConfig(app_id=target.id, suggested_questions_after_answer='{"enabled": true}')
config.id = str(uuid4())
target.app_model_config_id = config.id
conversation = make_conversation(
conversation_id=str(uuid4()),
app_id=target.id,
inputs={},
from_source=ConversationFromSource.API,
from_end_user_id=end_user.id,
)
conversation.app_model_config_id = config.id
message = make_message(
message_id=str(uuid4()),
app_id=target.id,
conversation_id=conversation.id,
inputs={},
query="How does this work?",
message={},
answer="Like this.",
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
currency="USD",
from_source=ConversationFromSource.API,
from_end_user_id=end_user.id,
)
token = ApiToken(app_id=target.id, tenant_id=tenant.id, type=ApiTokenType.APP, token="test-token")
with sqlite_session_factory.begin() as session:
# Fail the actual provider ID validation before plugin discovery can run.
provider = Provider(tenant_id=tenant.id, provider_name="invalid/provider", is_valid=True)
session.add(
TenantDefaultModel(
tenant_id=tenant.id,
provider_name="invalid/provider",
model_name="invalid-model",
model_type=ModelType.LLM,
)
)
session.add_all([tenant, target, end_user, config, conversation, message, token, provider])
app = Flask(__name__)
app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))
db.init_app(app)
LoginManager(app)
admissions: list[Session] = []
provisions: list[Session] = []
query_sessions: list[Session] = []
scoped_sessions: list[Session] = []
admission_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
provision_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
query_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
def track_admission(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
admissions.append(session)
def track_provision(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
# End-user lookup/creation must not overlap the admission transaction.
assert all(not admission.in_transaction() and not admission.identity_map for admission in admissions)
provisions.append(session)
def track_query(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
query_sessions.append(session)
def track_scoped(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
scoped_sessions.append(session)
event.listen(admission_factory, "after_begin", track_admission)
event.listen(provision_factory, "after_begin", track_provision)
event.listen(query_factory, "after_begin", track_query)
event.listen(db.session.session_factory, "after_begin", track_scoped)
queries = SuggestedQuestionsQuery(
session_factory=query_factory, repository=MessageRepository(session_factory=query_factory)
)
service = MessageSuggestedQuestionsService(
queries=queries, generator=SuggestedQuestionsGenerator(trace_scopes=untraced_scopes())
)
app.extensions["application_services"] = _Services(
app_definitions=AppDefinitionQueryService(
definitions=AppDefinitionQueryRepository(session_factory=admission_factory), builtin_icon_url_prefix=""
),
message_suggested_questions=service,
app_scoped_end_users=_EndUsers(
AppScopedEndUserService(end_users=AppScopedEndUserRepo(session_factory=provision_factory))
),
resource_access_tokens=build_resource_access_token_service(database_client=admission_factory),
)
api = ExternalApi(app)
api.add_resource(MessageSuggestedApi, "/messages/<uuid:message_id>/suggested")
yield _Harness(
app,
target,
tenant,
end_user,
conversation,
config,
provision_factory,
sqlite_session_factory,
message.id,
admissions,
provisions,
query_sessions,
scoped_sessions,
)
event.remove(admission_factory, "after_begin", track_admission)
event.remove(provision_factory, "after_begin", track_provision)
event.remove(query_factory, "after_begin", track_query)
event.remove(db.session.session_factory, "after_begin", track_scoped)
with app.app_context():
db.session.remove()
db.engine.dispose()
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])
def test_admitted_identity_and_mode_return_empty_questions_for_invalid_provider_config(
harness: _Harness, mode: AppMode, caplog: pytest.LogCaptureFixture
) -> None:
with harness.factory.begin() as session:
target = session.get(App, harness.target.id)
assert target is not None
target.mode = mode
if mode == AppMode.ADVANCED_CHAT:
workflow = Workflow(
tenant_id=harness.tenant.id,
app_id=target.id,
type=WorkflowType.CHAT,
version="published",
graph='{"nodes":[],"edges":[]}',
_features='{"suggested_questions_after_answer":{"enabled":true}}',
created_by=str(uuid4()),
)
session.add(workflow)
session.flush()
target.workflow_id = workflow.id
response = harness.get()
assert response.status_code == 200
assert response.json == {"result": "success", "data": []}
assert response.headers["Content-Type"] == "application/json"
failures = [
record.exc_info[1]
for record in caplog.records
if record.name == "services.message_suggested_questions_generator" and record.exc_info is not None
]
assert len(failures) == 1
assert isinstance(failures[0], ValueError)
assert str(failures[0]) == "Invalid provider: invalid/provider"
assert len(harness.queries) == 1
with harness.factory() as session:
end_user = session.scalar(select(EndUser).where(EndUser.app_id == harness.target.id))
assert end_user is not None
assert end_user.type == EndUserType.SERVICE_API
assert end_user.external_user_id == "alice"
assert end_user.id == harness.end_user.id
harness.assert_closed()
def test_existing_user_is_reused_within_the_token_app_only(harness: _Harness) -> None:
decoy = make_end_user(
end_user_id=str(uuid4()),
tenant_id=str(uuid4()),
app_id=str(uuid4()),
end_user_type=EndUserType.SERVICE_API,
session_id="alice",
external_user_id="alice",
)
with harness.factory.begin() as session:
session.add(decoy)
assert harness.get().status_code == 200
with harness.factory() as session:
assert len(session.scalars(select(EndUser)).all()) == 2
@pytest.mark.parametrize("authorization", [None, "Basic test-token"])
def test_invalid_authorization_stops_before_database_admission(harness: _Harness, authorization: str | None) -> None:
response = harness.get(authorization=authorization)
assert response.status_code == 401
assert response.json is not None
assert response.json["code"] == "unauthorized"
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
assert harness.admissions == harness.provisions == harness.queries == []
def test_app_key_with_foreign_workspace_is_rejected_before_end_user_provisioning(harness: _Harness) -> None:
with harness.factory.begin() as session:
foreign_tenant = Tenant(name="Other API key workspace")
session.add(foreign_tenant)
token = session.scalar(select(ApiToken).where(ApiToken.token == "test-token"))
assert token is not None
token.tenant_id = foreign_tenant.id
response = harness.get(user="must-not-be-provisioned")
assert response.status_code == 403
assert response.json == {"code": "app_not_found", "message": "The app no longer exists.", "status": 403}
assert harness.provisions == harness.queries == []
with harness.factory() as session:
assert session.scalars(select(EndUser.id)).all() == [harness.end_user.id]
harness.assert_closed()
def test_historical_app_key_without_workspace_uses_the_apps_workspace(harness: _Harness) -> None:
with harness.factory.begin() as session:
token = session.scalar(select(ApiToken).where(ApiToken.token == "test-token"))
assert token is not None
token.tenant_id = None
response = harness.get()
assert response.status_code == 200
assert response.json == {"result": "success", "data": []}
assert len(harness.provisions) == len(harness.queries) == 1
harness.assert_closed()
@pytest.mark.parametrize("user", [None, ""])
def test_required_user_rejected_before_provisioning(harness: _Harness, user: str | None) -> None:
response = harness.get(user=user)
assert response.status_code == 400
assert response.json is not None
assert response.json["code"] == "invalid_param"
assert response.json["message"] == "Arg user must be provided."
assert harness.provisions == harness.queries == []
harness.assert_closed()
@pytest.mark.parametrize(
("failure", "domain_error", "code", "message"),
[
("missing_app", AppDefinitionUnavailableError, "app_not_found", "The app no longer exists."),
("null_app", AppDefinitionUnavailableError, "app_not_found", "The app no longer exists."),
("abnormal_app", AppAbnormalStatusError, "app_abnormal_status", "The app's status is abnormal."),
("empty_app_status", AppAbnormalStatusError, "app_abnormal_status", "The app's status is abnormal."),
("disabled_api", AppApiDisabledError, "app_api_disabled", "The app's API service has been disabled."),
("archived_tenant", WorkspaceArchivedError, "workspace_archived", "The workspace's status is archived."),
("missing_tenant", WorkspaceNotFoundError, "workspace_not_found", "Tenant does not exist."),
],
)
def test_app_and_workspace_admission(
harness: _Harness,
failure: Literal[
"missing_app",
"null_app",
"abnormal_app",
"empty_app_status",
"disabled_api",
"archived_tenant",
"missing_tenant",
],
domain_error: type[Exception],
code: str,
message: str,
) -> None:
with harness.factory.begin() as session:
target = session.get(App, harness.target.id)
tenant = session.get(Tenant, harness.tenant.id)
assert target is not None
assert tenant is not None
match failure:
case "missing_app":
session.add(make_app(app_id=str(uuid4()), tenant_id=tenant.id))
session.delete(target)
case "null_app":
token = session.scalar(select(ApiToken).where(ApiToken.token == "test-token"))
assert token is not None
token.app_id = None
case "abnormal_app" | "empty_app_status":
# Bypass EnumText validation to exercise malformed persisted status values.
session.execute(
text("UPDATE apps SET status = :status WHERE id = :app_id"),
{"status": "disabled" if failure == "abnormal_app" else "", "app_id": target.id},
)
case "disabled_api":
target.enable_api = False
case "archived_tenant":
tenant.status = TenantStatus.ARCHIVE
case "missing_tenant":
session.delete(tenant)
services = harness.app.extensions["application_services"]
assert isinstance(services, _Services)
with pytest.raises(domain_error):
services.app_definitions.get_service_api_app(None if failure == "null_app" else harness.target.id)
harness.assert_closed()
response = harness.get(user="new-user")
assert response.status_code == 403
assert response.json == {"code": code, "message": message, "status": 403}
assert harness.provisions == harness.queries == []
with harness.factory() as session:
assert session.scalars(select(EndUser.id)).all() == [harness.end_user.id]
harness.assert_closed()
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
def test_unsupported_mode_does_not_invoke_questions(harness: _Harness, mode: AppMode) -> None:
with harness.factory.begin() as session:
target = session.get(App, harness.target.id)
assert target is not None
target.mode = mode
response = harness.get()
assert response.status_code == 400
assert response.json is not None
assert response.json["code"] == "not_chat_app"
assert harness.queries == []
harness.assert_closed()
@pytest.mark.parametrize(
("failure", "status", "code", "message"),
[
("message", 404, "not_found", "Message Not Exists."),
("conversation", 404, "not_found", "Conversation not found"),
("agent_version", 404, "agent_version_not_found_error", "Agent config version not found."),
("disabled", 403, "app_suggested_questions_after_answer_disabled", ""),
],
)
def test_domain_errors_have_specific_http_codes(
harness: _Harness,
failure: Literal["message", "conversation", "agent_version", "disabled"],
status: int,
code: str,
message: str,
) -> None:
with harness.factory.begin() as session:
match failure:
case "message":
message_record = session.get(Message, harness.message_id)
assert message_record is not None
session.delete(message_record)
case "conversation":
conversation = session.get(Conversation, harness.conversation.id)
assert conversation is not None
session.delete(conversation)
case "disabled":
config = session.get(AppModelConfig, harness.config.id)
assert config is not None
config.suggested_questions_after_answer = '{"enabled":false}'
case "agent_version":
target = session.get(App, harness.target.id)
assert target is not None
target.mode = AppMode.AGENT
session.add(
Agent(
tenant_id=harness.tenant.id,
app_id=target.id,
name="Missing published version",
scope=AgentScope.ROSTER,
source=AgentSource.AGENT_APP,
active_config_snapshot_id=str(uuid4()),
)
)
response = harness.get()
assert response.status_code == status
assert response.headers["Content-Type"] == "application/json"
assert response.json is not None
assert response.json["code"] == code
assert response.json["status"] == status
if message:
assert response.json["message"] == message
harness.assert_closed()
@pytest.mark.parametrize(
("change", "status", "code", "message"),
[
("app_mode", 400, "app_unavailable", ""),
("end_user_tenant", 404, "not_found", "End user not found"),
],
)
def test_scope_changes_after_admission_have_specific_http_codes(
harness: _Harness,
change: Literal["app_mode", "end_user_tenant"],
status: int,
code: str,
message: str,
) -> None:
def change_scope(_session: Session, _transaction: SessionTransaction) -> None:
with harness.factory.begin() as session:
if change == "app_mode":
target = session.get(App, harness.target.id)
assert target is not None
target.mode = AppMode.ADVANCED_CHAT
else:
end_user = session.get(EndUser, harness.end_user.id)
assert end_user is not None
end_user.tenant_id = str(uuid4())
event.listen(harness.provision_factory, "after_transaction_end", change_scope, once=True)
try:
response = harness.get()
finally:
event.remove(harness.provision_factory, "after_transaction_end", change_scope)
assert response.status_code == status
assert response.headers["Content-Type"] == "application/json"
assert response.json is not None
assert response.json["code"] == code
assert response.json["status"] == status
if message:
assert response.json["message"] == message
harness.assert_closed()
def test_malformed_configuration_returns_an_opaque_internal_error(harness: _Harness) -> None:
with harness.factory.begin() as session:
config = session.get(AppModelConfig, harness.config.id)
assert config is not None
config.suggested_questions_after_answer = "private diagnostic: invalid JSON"
response = harness.get()
assert response.status_code == 500
assert response.headers["Content-Type"] == "application/json"
assert response.json is not None
assert response.json["code"] == "internal_server_error"
assert response.json["status"] == 500
assert "private diagnostic" not in response.get_data(as_text=True)
harness.assert_closed()
def test_caller_session_and_pending_edits_are_preserved(harness: _Harness) -> None:
with harness.app.app_context():
caller = db.session()
target = caller.get(App, harness.target.id)
assert target is not None
target.name = "Pending caller change"
transaction = caller.get_transaction()
response = harness.get()
assert response.status_code == 200
assert response.json == {"result": "success", "data": []}
assert db.session() is caller
assert caller.get_transaction() is transaction
assert target in caller.dirty
assert target.name == "Pending caller change"
harness.assert_closed(caller=caller)
caller.rollback()
with harness.factory() as session:
target = session.get(App, harness.target.id)
assert target is not None
assert target.name == harness.target.name
def test_default_admission_keeps_legacy_app_attached(harness: _Harness) -> None:
@validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY, required=True))
def view(*, app_model: App, end_user: EndUser) -> str:
assert object_session(app_model) is db.session()
assert app_model.id == harness.target.id
assert end_user.app_id == app_model.id
return "legacy"
harness.app.add_url_rule("/legacy", view_func=view)
with harness.app.test_request_context("/legacy?user=alice", headers={"Authorization": "Bearer test-token"}):
assert harness.app.dispatch_request() == "legacy"
assert db.session().in_transaction()
assert harness.admissions == []
def test_request_identity_remains_installed_after_generation(harness: _Harness) -> None:
with harness.app.test_client() as client:
response = client.get(
f"/messages/{harness.message_id}/suggested?user=alice", headers={"Authorization": "Bearer test-token"}
)
assert response.status_code == 200
assert response.json == {"result": "success", "data": []}
assert current_user.id == harness.end_user.id
assert current_user.app_id == harness.target.id
assert current_user.tenant_id == harness.tenant.id
@pytest.mark.parametrize("fetch_from", [WhereisUserArg.QUERY, WhereisUserArg.JSON, WhereisUserArg.FORM])
def test_admission_injects_immutable_scope_and_provisions_request_user(
harness: _Harness,
fetch_from: WhereisUserArg,
) -> None:
class AdmittedView:
@service_api_end_user_admission(fetch_user_arg=FetchUserArg(fetch_from=fetch_from, required=True))
def get(self, context: ServiceApiEndUserContext) -> dict[str, str]:
assert isinstance(context, ServiceApiEndUserContext)
harness.assert_closed()
assert current_user.id == context.end_user_id
payload = asdict(context)
for field_name in payload:
with pytest.raises(FrozenInstanceError):
setattr(context, field_name, "untrusted")
return payload
harness.app.add_url_rule("/admitted", view_func=AdmittedView().get, methods=["GET", "POST"])
client = harness.app.test_client()
headers = {"Authorization": "Bearer test-token"}
match fetch_from:
case WhereisUserArg.QUERY:
response = client.get("/admitted?user=bob", headers=headers)
case WhereisUserArg.JSON:
response = client.post("/admitted", json={"user": "bob"}, headers=headers)
case WhereisUserArg.FORM:
response = client.post("/admitted", data={"user": "bob"}, headers=headers)
assert response.status_code == 200
with harness.factory() as session:
end_user = session.scalar(
select(EndUser).where(EndUser.app_id == harness.target.id, EndUser.external_user_id == "bob")
)
assert end_user is not None
assert end_user.session_id == "bob"
assert response.json == {
"app_id": harness.target.id,
"tenant_id": harness.tenant.id,
"app_mode": harness.target.mode,
"end_user_id": end_user.id,
}
def _create_resource_token(harness: _Harness, *, app_ids: tuple[str, ...]) -> ResourceAccessTokenCreateResult:
services = harness.app.extensions["application_services"]
assert isinstance(services, _Services)
result = services.resource_access_tokens.create(
RequestContext(
request_id="resource-token-regression",
trace_id=None,
account_id=str(uuid4()),
active_workspace_id=harness.tenant.id,
),
name="Suggested questions integration",
resources=tuple(ResourceAccessTokenResource(ResourceAccessTokenResourceType.APP, app_id) for app_id in app_ids),
)
assert result.token.startswith("sk-")
assert all(row.last_used_at is None for row in result.rows)
harness.assert_closed()
return result
@pytest.mark.parametrize("multiple_apps", [False, True], ids=["implicit-single-app", "explicit-multiple-apps"])
def test_resource_access_token_admits_bound_app_and_records_usage(harness: _Harness, multiple_apps: bool) -> None:
app_ids = (harness.target.id,)
if multiple_apps:
other = make_app(app_id=str(uuid4()), tenant_id=harness.tenant.id)
with harness.factory.begin() as session:
session.add(other)
app_ids = (other.id, harness.target.id)
token = _create_resource_token(harness, app_ids=app_ids)
response = harness.get(
authorization=f"Bearer {token.token}",
requested_app_id=harness.target.id if multiple_apps else None,
)
assert response.status_code == 200
assert response.json == {"result": "success", "data": []}
assert response.headers["Content-Type"] == "application/json"
assert len(harness.provisions) == len(harness.queries) == 1
harness.assert_closed()
with harness.factory() as session:
stored_token = session.get(ResourceAccessToken, token.token_id)
assert stored_token is not None
assert stored_token.last_used_at is not None
assert session.scalars(select(EndUser.id)).all() == [harness.end_user.id]
def test_multiple_app_resource_token_requires_app_header_before_provisioning(harness: _Harness) -> None:
other = make_app(app_id=str(uuid4()), tenant_id=harness.tenant.id)
with harness.factory.begin() as session:
session.add(other)
token = _create_resource_token(harness, app_ids=(harness.target.id, other.id))
response = harness.get(authorization=f"Bearer {token.token}", user="new-user")
assert response.status_code == 400
assert response.json == {
"code": "invalid_param",
"message": "App ID is required when a resource access token is bound to multiple apps.",
"status": 400,
}
assert harness.provisions == harness.queries == []
harness.assert_closed()
with harness.factory() as session:
stored_token = session.get(ResourceAccessToken, token.token_id)
assert stored_token is not None
assert stored_token.last_used_at is None
assert session.scalars(select(EndUser.id)).all() == [harness.end_user.id]
@pytest.mark.parametrize(
("failure", "status", "code", "message"),
[
("unbound_app", 403, "forbidden", "Resource access token is not allowed to access this app."),
("foreign_app", 403, "forbidden", "Resource access token is not allowed to access this app."),
("no_bindings", 403, "forbidden", "Resource access token is not allowed to access this app."),
("foreign_binding", 403, "forbidden", "The app no longer exists."),
("deleted_app", 403, "forbidden", "The app no longer exists."),
("invalid_token", 401, "unauthorized", "Resource access token is invalid."),
],
)
def test_resource_token_denials_stop_before_end_user_provisioning(
harness: _Harness, failure: str, status: int, code: str, message: str
) -> None:
token = _create_resource_token(harness, app_ids=(harness.target.id,))
requested_app_id = None
authorization = f"Bearer {token.token}"
with harness.factory.begin() as session:
if failure in {"unbound_app", "foreign_app", "foreign_binding"}:
tenant_id = harness.tenant.id
if failure != "unbound_app":
foreign_tenant = Tenant(name="Other resource owner")
session.add(foreign_tenant)
tenant_id = foreign_tenant.id
other = make_app(app_id=str(uuid4()), tenant_id=tenant_id)
session.add(other)
if failure == "foreign_binding":
relation = session.scalar(
select(ResourceAccessTokenRelation).where(ResourceAccessTokenRelation.token_id == token.token_id)
)
assert relation is not None
relation.app_id = other.id
else:
requested_app_id = other.id
elif failure == "no_bindings":
session.execute(
delete(ResourceAccessTokenRelation).where(ResourceAccessTokenRelation.token_id == token.token_id)
)
elif failure == "deleted_app":
session.execute(delete(App).where(App.id == harness.target.id))
else:
authorization = f"Bearer sk-{uuid4()}"
response = harness.get(authorization=authorization, requested_app_id=requested_app_id, user="new-user")
assert response.status_code == status
assert response.json == {"code": code, "message": message, "status": status}
assert harness.provisions == harness.queries == []
harness.assert_closed()
with harness.factory() as session:
stored_token = session.get(ResourceAccessToken, token.token_id)
assert stored_token is not None
assert stored_token.last_used_at is None
assert session.scalars(select(EndUser.id)).all() == [harness.end_user.id]
@pytest.mark.parametrize(
("failure", "code", "message"),
[
("disabled_api", "app_api_disabled", "The app's API service has been disabled."),
("archived_tenant", "workspace_archived", "The workspace's status is archived."),
],
)
def test_resource_token_keeps_app_and_workspace_policy_errors(
harness: _Harness, failure: str, code: str, message: str
) -> None:
token = _create_resource_token(harness, app_ids=(harness.target.id,))
with harness.factory.begin() as session:
if failure == "disabled_api":
target = session.get(App, harness.target.id)
assert target is not None
target.enable_api = False
else:
tenant = session.get(Tenant, harness.tenant.id)
assert tenant is not None
tenant.status = TenantStatus.ARCHIVE
response = harness.get(authorization=f"Bearer {token.token}", user="new-user")
assert response.status_code == 403
assert response.json == {"code": code, "message": message, "status": 403}
assert harness.provisions == harness.queries == []
harness.assert_closed()
with harness.factory() as session:
assert session.scalars(select(EndUser.id)).all() == [harness.end_user.id]
def test_explicit_service_api_app_tenant_scope_excludes_foreign_apps(harness: _Harness) -> None:
services = harness.app.extensions["application_services"]
assert isinstance(services, _Services)
record = services.app_definitions.get_service_api_app(harness.target.id, tenant_id=harness.tenant.id)
assert (record.app_id, record.tenant_id) == (harness.target.id, harness.tenant.id)
with pytest.raises(AppDefinitionUnavailableError):
services.app_definitions.get_service_api_app(harness.target.id, tenant_id=str(uuid4()))
assert services.app_definitions.get_service_api_app(harness.target.id) == record
assert harness.provisions == harness.queries == []
harness.assert_closed()
@@ -0,0 +1,75 @@
"""State-based checks for shared service-API authentication fixtures."""
from uuid import uuid4
from sqlalchemy import select
from models.account import Account, Tenant, TenantAccountJoin, TenantAccountRole
from tests.unit_tests.conftest import (
persist_service_api_dataset_owner,
persist_service_api_tenant_owner,
)
from tests.unit_tests.controllers.conftest import app_query_services as app_query_services # noqa: PLC0414
from tests.unit_tests.controllers.conftest import (
datasource_application_dependencies as datasource_application_dependencies, # noqa: PLC0414
)
from tests.unit_tests.controllers.conftest import error_response_contract as error_response_contract # noqa: PLC0414
from tests.unit_tests.controllers.conftest import pipeline_application as pipeline_application # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import ServiceApiIdentity
from tests.unit_tests.controllers.service_api.conftest import app as app # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import mock_account as mock_account # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import mock_api_token as mock_api_token # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import mock_app_id as mock_app_id # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import mock_app_model as mock_app_model # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import mock_child_chunk as mock_child_chunk # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import mock_dataset as mock_dataset # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import mock_dataset_api_token as mock_dataset_api_token # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import mock_document as mock_document # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import mock_end_user as mock_end_user # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import mock_segment as mock_segment # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import mock_tenant as mock_tenant # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import mock_tenant_id as mock_tenant_id # noqa: PLC0414
from tests.unit_tests.controllers.service_api.conftest import service_api_identity as service_api_identity # noqa: PLC0414
def test_service_api_identity_persists_tenant_scoped_owner(service_api_identity: ServiceApiIdentity) -> None:
identity = service_api_identity
owner_row = identity.session.execute(
select(Tenant, Account)
.join(TenantAccountJoin, Tenant.id == TenantAccountJoin.tenant_id)
.join(Account, TenantAccountJoin.account_id == Account.id)
.where(
Tenant.id == identity.tenant.id,
TenantAccountJoin.role == TenantAccountRole.OWNER,
)
).one()
assert owner_row == (identity.tenant, identity.account)
assert identity.account.current_tenant is identity.tenant
def test_shared_helpers_persist_real_app_and_dataset_owner_rows(service_api_identity: ServiceApiIdentity) -> None:
session = service_api_identity.session
app_tenant = Tenant(name="App Workspace")
app_tenant.id = str(uuid4())
app_owner = Account(name="App Owner", email=f"app-owner-{app_tenant.id}@example.com")
app_owner.id = str(uuid4())
app_membership = persist_service_api_tenant_owner(session, app_tenant, app_owner)
dataset_tenant = Tenant(name="Dataset Workspace")
dataset_tenant.id = str(uuid4())
dataset_membership = TenantAccountJoin(
tenant_id=dataset_tenant.id,
account_id=service_api_identity.account.id,
role=TenantAccountRole.OWNER,
)
persist_service_api_dataset_owner(session, dataset_tenant, dataset_membership)
assert session.get(TenantAccountJoin, app_membership.id) is app_membership
assert session.execute(
select(Tenant, TenantAccountJoin)
.join(TenantAccountJoin, Tenant.id == TenantAccountJoin.tenant_id)
.where(Tenant.id == dataset_tenant.id)
).one() == (dataset_tenant, dataset_membership)
@@ -0,0 +1,257 @@
"""Conversation commands through real Web admission and SQLite transactions."""
from dataclasses import dataclass
from http import HTTPStatus
from typing import Literal
from uuid import uuid4
import pytest
from sqlalchemy import event, select, update
from sqlalchemy.orm import Session
from werkzeug.test import TestResponse
from constants import HEADER_NAME_APP_CODE, HEADER_NAME_PASSPORT
from controllers.web.conversation import ConversationApi, ConversationPinApi, ConversationUnPinApi
from models.agent import AgentWorkingResourceStatus, AgentWorkspace, AgentWorkspaceOwnerType
from models.enums import ConversationFromSource, CreatorUserRole
from models.model import AppMode, Conversation, EndUser
from models.web import PinnedConversation
from repositories.conversation_repository import ConversationRepository
from services.conversation_command_service import ConversationCommandService
from tests.e2e_tests.contracts.controllers.web import test_conversation_rename as rename_tests
from tests.e2e_tests.contracts.controllers.web.test_conversation_rename import _Harness as RenameHarness
rename_harness = rename_tests.harness
@dataclass(frozen=True)
class _Services:
conversation_commands: ConversationCommandService
@dataclass(frozen=True)
class _Harness:
source: RenameHarness
cleanups: list[tuple[str, str, tuple[str, ...]]]
def request(self, action: str, *, conversation_id: str | None = None) -> TestResponse:
path = f"/conversations/{conversation_id or self.source.conversation.id}"
return self.source.app.test_client().open(
path if action == "delete" else f"{path}/{action}",
method="DELETE" if action == "delete" else "PATCH",
headers={HEADER_NAME_APP_CODE: "public-app", HEADER_NAME_PASSPORT: self.source.passport},
)
def assert_not_deleted(self) -> None:
with self.source.factory() as session:
stored = session.get(Conversation, self.source.conversation.id)
assert stored is not None
assert not stored.is_deleted
assert self.cleanups == []
@pytest.fixture
def harness(rename_harness: RenameHarness) -> _Harness:
cleanups: list[tuple[str, str, tuple[str, ...]]] = []
def enqueue_cleanup(*, tenant_id: str, conversation_id: str, retired_workspace_ids: tuple[str, ...]) -> None:
# Observe the real external boundary: committed state must be visible on
# another connection, with neither admission nor command transactions open.
rename_harness.assert_closed()
with rename_harness.factory() as session:
stored = session.get(Conversation, conversation_id)
assert stored is not None
assert stored.is_deleted
for workspace_id in retired_workspace_ids:
workspace = session.get(AgentWorkspace, workspace_id)
assert workspace is not None
assert workspace.status == AgentWorkingResourceStatus.RETIRED
assert workspace.active_guard is None
assert workspace.retired_at is not None
cleanups.append((tenant_id, conversation_id, retired_workspace_ids))
rename_harness.app.extensions["application_services"] = _Services(
conversation_commands=ConversationCommandService(
conversations=ConversationRepository(session_factory=rename_harness.repository_factory),
enqueue_delete_cleanup=enqueue_cleanup,
)
)
rename_harness.api.add_resource(ConversationApi, "/conversations/<uuid:c_id>")
rename_harness.api.add_resource(ConversationPinApi, "/conversations/<uuid:c_id>/pin")
rename_harness.api.add_resource(ConversationUnPinApi, "/conversations/<uuid:c_id>/unpin")
return _Harness(rename_harness, cleanups)
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])
def test_delete_commits_retirement_before_cleanup_and_returns_empty_204(harness: _Harness, mode: AppMode) -> None:
source = harness.source
rename_tests._publish_mode(source, mode)
with source.factory.begin() as session:
workspace = AgentWorkspace(
id=str(uuid4()),
tenant_id=source.target.tenant_id,
app_id=source.target.id,
owner_type=AgentWorkspaceOwnerType.CONVERSATION,
owner_id=source.conversation.id,
owner_scope_key="chatflow-node",
backend_workspace_ref="backend-workspace",
status=AgentWorkingResourceStatus.ACTIVE,
active_guard=1,
)
session.add(workspace)
response = harness.request("delete")
assert response.status_code == HTTPStatus.NO_CONTENT
assert response.data == b""
assert response.headers["Content-Type"] == "application/json"
assert harness.cleanups == [(source.target.tenant_id, source.conversation.id, (workspace.id,))]
assert source.repository_sessions
source.assert_closed()
repeated = harness.request("delete")
assert repeated.status_code == HTTPStatus.NOT_FOUND
assert repeated.json is not None
assert repeated.json["code"] == "conversation_not_found"
assert len(harness.cleanups) == 1
source.assert_closed()
@pytest.mark.parametrize("action", ["delete", "pin"])
@pytest.mark.parametrize("field", ["app_id", "from_end_user_id", "from_source", "from_account_id", "is_deleted"])
def test_commands_cannot_target_conversations_outside_end_user_scope(
harness: _Harness, action: str, field: str
) -> None:
source = harness.source
value: str | bool
if field == "is_deleted":
value = True
elif field == "from_source":
value = ConversationFromSource.CONSOLE
elif field == "from_account_id":
# Account and EndUser IDs are independent namespaces, even if equal.
value = source.end_user.id
else:
value = str(uuid4())
with source.factory.begin() as session:
session.execute(update(Conversation).where(Conversation.id == source.conversation.id).values({field: value}))
response = harness.request(action)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.json is not None
assert response.json["code"] == "conversation_not_found"
assert harness.cleanups == []
with source.factory() as session:
stored = session.get(Conversation, source.conversation.id)
assert stored is not None
assert stored.is_deleted is (field == "is_deleted")
assert session.scalars(select(PinnedConversation)).all() == []
source.assert_closed()
@pytest.mark.parametrize("action", ["delete", "pin", "unpin"])
@pytest.mark.parametrize("field", ["tenant_id", "app_id"])
def test_commands_reject_end_user_outside_app_owner_scope(
harness: _Harness, action: str, field: Literal["tenant_id", "app_id"]
) -> None:
source = harness.source
with source.factory.begin() as session:
session.execute(update(EndUser).where(EndUser.id == source.end_user.id).values({field: str(uuid4())}))
response = harness.request(action)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.json is not None
assert response.json["code"] == "not_found"
harness.assert_not_deleted()
source.assert_closed()
def test_pin_and_unpin_are_idempotent_and_isolate_actor_role(harness: _Harness) -> None:
source = harness.source
with source.factory.begin() as session:
other_pins = [
PinnedConversation(
app_id=source.target.id,
conversation_id=source.conversation.id,
created_by_role=role,
created_by=actor_id,
)
for role, actor_id in [
(CreatorUserRole.ACCOUNT, source.end_user.id),
(CreatorUserRole.END_USER, str(uuid4())),
]
]
session.add_all(other_pins)
for _ in range(2):
response = harness.request("pin")
assert response.status_code == HTTPStatus.OK
assert response.headers["Content-Type"] == "application/json"
assert response.json is not None
assert response.json == {"result": "success"}
source.assert_closed()
with source.factory() as session:
pins = session.scalars(select(PinnedConversation)).all()
own_pins = [
pin
for pin in pins
if pin.created_by_role == CreatorUserRole.END_USER and pin.created_by == source.end_user.id
]
assert len(pins) == 3
assert len(own_pins) == 1
assert own_pins[0].app_id == source.target.id
assert own_pins[0].conversation_id == source.conversation.id
for _ in range(2):
response = harness.request("unpin")
assert response.status_code == HTTPStatus.OK
assert response.json is not None
assert response.json == {"result": "success"}
source.assert_closed()
with source.factory() as session:
assert set(session.scalars(select(PinnedConversation.id))) == {pin.id for pin in other_pins}
harness.assert_not_deleted()
@pytest.mark.parametrize("action", ["delete", "pin", "unpin"])
def test_unknown_conversation_preserves_existing_decoy(harness: _Harness, action: str) -> None:
response = harness.request(action, conversation_id=str(uuid4()))
if action == "unpin":
assert response.status_code == HTTPStatus.OK
assert response.json is not None
assert response.json == {"result": "success"}
else:
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.json is not None
assert response.json["code"] == "conversation_not_found"
harness.assert_not_deleted()
harness.source.assert_closed()
@pytest.mark.parametrize("action", ["delete", "pin", "unpin"])
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
def test_non_chat_mode_is_rejected_before_command_transaction(harness: _Harness, action: str, mode: AppMode) -> None:
source = harness.source
rename_tests._publish_mode(source, mode)
response = harness.request(action)
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.json is not None
assert response.json["code"] == "not_chat_app"
assert source.repository_sessions == []
harness.assert_not_deleted()
source.assert_closed()
def test_failed_delete_commit_rolls_back_without_publishing_cleanup(harness: _Harness) -> None:
def reject_commit(_session: Session) -> None:
raise RuntimeError("Database commit failed")
source = harness.source
event.listen(source.repository_factory, "before_commit", reject_commit)
try:
response = harness.request("delete")
finally:
event.remove(source.repository_factory, "before_commit", reject_commit)
assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
harness.assert_not_deleted()
source.assert_closed()
@@ -0,0 +1,297 @@
"""Conversation lists through real Web admission, queries, and SQLite reads."""
from collections.abc import Callable, Mapping
from dataclasses import dataclass
from datetime import timedelta
from http import HTTPStatus
from uuid import uuid4
import pytest
from pydantic import JsonValue
from sqlalchemy import Engine, event, update
from sqlalchemy.orm import Session, SessionTransaction
from werkzeug.test import TestResponse
from constants import HEADER_NAME_APP_CODE, HEADER_NAME_PASSPORT
from controllers.web.conversation import ConversationListApi
from graphon.file import File, FileTransferMethod, FileType
from models.enums import ConversationFromSource, CreatorUserRole, InvokeFrom
from models.model import App, AppMode, Conversation, EndUser
from models.utils.file_input_compat import restore_input_files
from models.web import PinnedConversation
from repositories.conversation_repository import ConversationRepository
from services.conversation_query_service import ConversationQueryService
from services.entities.conversation_entities import ConversationSort
from services.message_query_adapters import MessageFileResolver
from tests.e2e_tests.contracts.controllers.web import test_conversation_rename as rename_tests
from tests.e2e_tests.contracts.controllers.web.test_conversation_rename import _Harness as RenameHarness
from tests.helpers.unit.model_factories import make_conversation
rename_harness = rename_tests.harness
@dataclass(frozen=True)
class _Services:
conversation_queries: ConversationQueryService
@dataclass(frozen=True)
class _Harness:
source: RenameHarness
restored_inputs: list[dict[str, JsonValue | File | list[File]]]
def get(self, query: Mapping[str, str | int] | None = None) -> TestResponse:
return self.source.app.test_client().get(
"/conversations",
query_string=query,
headers={HEADER_NAME_APP_CODE: "public-app", HEADER_NAME_PASSPORT: self.source.passport},
)
@pytest.fixture
def harness(rename_harness: RenameHarness) -> _Harness:
restored_inputs: list[dict[str, JsonValue | File | list[File]]] = []
def restore(
*,
inputs: Mapping[str, JsonValue | File | list[File]],
tenant_resolver: Callable[[], str],
) -> dict[str, JsonValue | File | list[File]]:
# Exercise the real restoration, observing its transaction boundary.
rename_harness.assert_closed()
restored = restore_input_files(inputs=inputs, tenant_resolver=tenant_resolver)
restored_inputs.append(restored)
return restored
rename_harness.app.extensions["application_services"] = _Services(
conversation_queries=ConversationQueryService(
files=MessageFileResolver(),
conversations=ConversationRepository(session_factory=rename_harness.repository_factory),
restore_inputs=restore,
)
)
rename_harness.api.add_resource(ConversationListApi, "/conversations")
return _Harness(rename_harness, restored_inputs)
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])
def test_list_preserves_wire_contract_and_restores_inputs_after_session_close(harness: _Harness, mode: AppMode) -> None:
source = harness.source
rename_tests._publish_mode(source, mode)
response = harness.get()
assert response.status_code == HTTPStatus.OK
assert response.headers["Content-Type"] == "application/json"
assert response.get_json() == {
"limit": 20,
"has_more": False,
"data": [
{
"id": source.conversation.id,
"name": "Original title",
"inputs": {"text": "", "list": [], "zero": 0, "flag": False, "null": None},
"status": "normal",
"introduction": None,
"created_at": int(source.conversation.created_at.timestamp()),
"updated_at": int(source.conversation.updated_at.timestamp()),
}
],
}
assert len(harness.restored_inputs) == 1
assert source.repository_sessions
source.assert_closed()
@pytest.mark.parametrize("sort_by", ["created_at", "-created_at", "updated_at", "-updated_at"])
def test_sort_and_cursor_return_the_next_page(harness: _Harness, sort_by: ConversationSort) -> None:
source = harness.source
newest = make_conversation(
conversation_id=str(uuid4()),
app_id=source.target.id,
from_source=ConversationFromSource.API,
from_end_user_id=source.end_user.id,
inputs={},
)
newest.created_at = source.conversation.created_at + timedelta(days=1)
newest.updated_at = source.conversation.updated_at + timedelta(days=1)
newest.invoke_from = InvokeFrom.WEB_APP
with source.factory.begin() as session:
session.add(newest)
ordered_ids = [source.conversation.id, newest.id]
if sort_by.startswith("-"):
ordered_ids.reverse()
first = harness.get({"sort_by": sort_by, "limit": 1, "last_id": ""})
assert first.status_code == HTTPStatus.OK
assert first.get_json()["has_more"] is True
assert [item["id"] for item in first.get_json()["data"]] == ordered_ids[:1]
second = harness.get({"sort_by": sort_by, "limit": 1, "last_id": ordered_ids[0]})
assert second.status_code == HTTPStatus.OK
assert second.get_json()["has_more"] is False
assert [item["id"] for item in second.get_json()["data"]] == ordered_ids[1:]
last = harness.get({"sort_by": sort_by, "last_id": ordered_ids[1]})
assert last.get_json() == {"limit": 20, "has_more": False, "data": []}
source.assert_closed()
@pytest.mark.parametrize(
"field", ["app_id", "from_end_user_id", "from_source", "from_account_id", "is_deleted", "invoke_from"]
)
def test_list_and_cursor_cannot_expose_other_owners_or_invocation_sources(harness: _Harness, field: str) -> None:
source = harness.source
value: str | bool
if field == "is_deleted":
value = True
elif field == "from_source":
value = ConversationFromSource.CONSOLE
elif field == "invoke_from":
value = InvokeFrom.SERVICE_API.value
else:
value = str(uuid4())
with source.factory.begin() as session:
session.execute(update(Conversation).where(Conversation.id == source.conversation.id).values({field: value}))
response = harness.get()
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"limit": 20, "has_more": False, "data": []}
cursor = harness.get({"last_id": source.conversation.id})
assert cursor.status_code == HTTPStatus.NOT_FOUND
assert cursor.get_json()["code"] == "conversation_cursor_not_found"
source.assert_closed()
@pytest.mark.parametrize("pinned", [True, False])
def test_pin_filter_is_scoped_to_app_actor_and_role(harness: _Harness, pinned: bool) -> None:
source = harness.source
with source.factory.begin() as session:
session.add(
PinnedConversation(
app_id=source.target.id,
conversation_id=source.conversation.id,
created_by_role=CreatorUserRole.END_USER,
created_by=source.end_user.id,
)
)
response = harness.get({"pinned": str(pinned).lower()})
assert response.status_code == HTTPStatus.OK
assert [item["id"] for item in response.get_json()["data"]] == ([source.conversation.id] if pinned else [])
source.assert_closed()
@pytest.mark.parametrize("field", ["app_id", "created_by", "created_by_role"])
def test_another_owner_pin_does_not_hide_the_unpinned_conversation(harness: _Harness, field: str) -> None:
source = harness.source
pin = PinnedConversation(
app_id=source.target.id,
conversation_id=source.conversation.id,
created_by_role=CreatorUserRole.END_USER,
created_by=source.end_user.id,
)
if field == "created_by_role":
pin.created_by_role = CreatorUserRole.ACCOUNT
elif field == "created_by":
pin.created_by = str(uuid4())
else:
pin.app_id = str(uuid4())
with source.factory.begin() as session:
session.add(pin)
response = harness.get({"pinned": "false"})
assert response.status_code == HTTPStatus.OK
assert [item["id"] for item in response.get_json()["data"]] == [source.conversation.id]
# Existing behavior: a requested pinned list with no pins is empty even
# when a cursor is supplied; it does not first try to resolve that cursor.
empty = harness.get({"pinned": "true", "last_id": str(uuid4())})
assert empty.status_code == HTTPStatus.OK
assert empty.get_json() == {"limit": 20, "has_more": False, "data": []}
source.assert_closed()
@pytest.mark.parametrize("query", [{"limit": "0"}, {"limit": "101"}, {"last_id": "invalid"}, {"sort_by": "name"}])
def test_invalid_query_preserves_400_before_querying(harness: _Harness, query: dict[str, str]) -> None:
response = harness.get(query)
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "invalid_param"
assert not harness.source.repository_sessions
harness.source.assert_closed()
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
def test_non_chat_mode_is_rejected_before_querying(harness: _Harness, mode: AppMode) -> None:
source = harness.source
rename_tests._publish_mode(source, mode)
response = harness.get()
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "not_chat_app"
assert not source.repository_sessions
source.assert_closed()
@pytest.mark.parametrize("field", ["tenant_id", "app_id"])
def test_end_user_outside_app_owner_scope_is_rejected(harness: _Harness, field: str) -> None:
source = harness.source
with source.factory.begin() as session:
session.execute(update(EndUser).where(EndUser.id == source.end_user.id).values({field: str(uuid4())}))
response = harness.get()
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "not_found"
source.assert_closed()
@pytest.mark.parametrize("change_app_too", [False, True], ids=["end-user-workspace", "app-workspace"])
def test_workspace_change_after_admission_returns_conversation_not_found(
harness: _Harness, sqlite_engine: Engine, change_app_too: bool
) -> None:
source = harness.source
owner_changed = False
def change_owner(session: Session, _transaction: SessionTransaction) -> None:
nonlocal owner_changed
assert session in source.admission_sessions
assert not source.repository_sessions
# Change persisted ownership after passport resolution has closed its transaction.
new_tenant_id = str(uuid4())
with Session(sqlite_engine) as writer, writer.begin():
writer.execute(update(EndUser).where(EndUser.id == source.end_user.id).values(tenant_id=new_tenant_id))
if change_app_too:
writer.execute(update(App).where(App.id == source.target.id).values(tenant_id=new_tenant_id))
owner_changed = True
event.listen(source.factory, "after_transaction_end", change_owner, once=True)
try:
response = harness.get()
finally:
event.remove(source.factory, "after_transaction_end", change_owner)
assert owner_changed
assert source.repository_sessions
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json() == {
"code": "conversation_not_found",
"message": "The conversation was not found for this user and app.",
"status": HTTPStatus.NOT_FOUND,
}
assert harness.restored_inputs == []
source.assert_closed()
def test_remote_file_input_keeps_its_existing_wire_shape(harness: _Harness) -> None:
source = harness.source
file = File(
file_id=str(uuid4()),
file_type=FileType.DOCUMENT,
transfer_method=FileTransferMethod.REMOTE_URL,
remote_url="https://example.com/document.pdf",
filename="document.pdf",
extension=".pdf",
mime_type="application/pdf",
size=12,
)
with source.factory.begin() as session:
conversation = session.get(Conversation, source.conversation.id)
assert conversation is not None
conversation.inputs = {"document": file, "documents": [file]}
response = harness.get()
assert response.status_code == HTTPStatus.OK
assert response.get_json()["data"][0]["inputs"] == {
"document": dict(file.to_dict()),
"documents": [dict(file.to_dict())],
}
source.assert_closed()
@@ -0,0 +1,384 @@
"""Rename through real Web admission, application service, and SQLite persistence."""
from collections.abc import Callable, Iterator
from dataclasses import dataclass
from datetime import datetime
from decimal import Decimal
from http import HTTPStatus
from typing import Literal
from uuid import uuid4
import pytest
from flask import Flask
from pydantic import JsonValue
from sqlalchemy import Connection, Engine, delete, event, update
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
from constants import HEADER_NAME_APP_CODE, HEADER_NAME_PASSPORT
from controllers.web.conversation import ConversationRenameApi
from core.errors.error import ProviderTokenNotInitError
from enums import DeploymentEdition
from extensions.ext_database import db
from graphon.file import File, FileTransferMethod, FileType
from libs.external_api import ExternalApi
from libs.passport import PassportService
from models.agent import (
Agent,
AgentConfigRevision,
AgentConfigRevisionOperation,
AgentConfigSnapshot,
AgentScope,
AgentSource,
)
from models.agent_config_entities import AgentSoulConfig
from models.enums import ConversationFromSource, CustomizeTokenStrategy
from models.model import App, AppMode, AppModelConfig, Conversation, EndUser, Message, Site
from models.workflow import WorkflowType
from repositories.conversation_repository import ConversationRepository
from services.conversation_name_generator import DefaultModelConversationNameGenerator
from services.conversation_name_service import ConversationNameService
from tests.helpers.unit.model_factories import make_app, make_conversation, make_end_user, make_message, make_workflow
from tests.helpers.unit.services.tracing_ports import untraced_scopes
_CREATED_AT = datetime(2026, 1, 2, 3, 4, 5)
_UPDATED_AT = datetime(2026, 1, 3, 3, 4, 5)
_INPUTS: dict[str, JsonValue] = {"text": "", "list": [], "zero": 0, "flag": False, "null": None}
@dataclass
class _Services:
conversation_names: ConversationNameService
@dataclass(frozen=True)
class _Harness:
app: Flask
target: App
end_user: EndUser
conversation: Conversation
factory: sessionmaker[Session]
repository_factory: sessionmaker[Session]
api: ExternalApi
passport: str
admission_sessions: list[Session]
repository_sessions: list[Session]
provider_sessions: list[Session]
def post(self, body: dict[str, object]) -> TestResponse:
return self.app.test_client().post(
f"/conversations/{self.conversation.id}/name",
json=body,
headers={HEADER_NAME_APP_CODE: "public-app", HEADER_NAME_PASSPORT: self.passport},
)
def assert_closed(self) -> None:
sessions = self.admission_sessions + self.repository_sessions + self.provider_sessions
assert sessions
assert all(not session.in_transaction() and not session.identity_map for session in sessions)
def assert_name(self, name: str) -> None:
with self.factory() as session:
conversation = session.get(Conversation, self.conversation.id)
assert conversation is not None
assert conversation.name == name
@pytest.fixture
def harness(
config_overrides: Callable[..., None],
sqlite_engine: Engine,
sqlite_session_factory: sessionmaker[Session],
) -> Iterator[_Harness]:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY, SECRET_KEY="conversation-rename-test-secret-key")
target = make_app(app_id=str(uuid4()), tenant_id=str(uuid4()))
end_user = make_end_user(end_user_id=str(uuid4()), tenant_id=target.tenant_id, app_id=target.id)
site = Site(
app_id=target.id,
title="Public app",
default_language="en-US",
customize_token_strategy=CustomizeTokenStrategy.UUID,
code="public-app",
)
conversation = make_conversation(
conversation_id=str(uuid4()),
app_id=target.id,
from_source=ConversationFromSource.API,
from_end_user_id=end_user.id,
inputs=_INPUTS,
name="Original title",
)
conversation.created_at = _CREATED_AT
conversation.updated_at = _UPDATED_AT
message = make_message(
message_id=str(uuid4()),
app_id=target.id,
conversation_id=conversation.id,
inputs={},
message={},
query="How does it work?",
answer="Like this.",
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
currency="USD",
from_source=ConversationFromSource.API,
from_end_user_id=end_user.id,
)
with sqlite_session_factory.begin() as session:
config = AppModelConfig(app_id=target.id)
session.add_all([target, end_user, site, conversation, message, config])
session.flush()
target.app_model_config_id = config.id
repository_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
admission_sessions: list[Session] = []
repository_sessions: list[Session] = []
provider_sessions: list[Session] = []
def track_admission(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
admission_sessions.append(session)
def track_repository(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
# Admission and provider lookup must release their transactions before
# rename starts its own read/write transaction.
assert all(not item.in_transaction() for item in admission_sessions + provider_sessions)
repository_sessions.append(session)
def track_provider(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
assert all(not item.in_transaction() for item in admission_sessions + repository_sessions)
provider_sessions.append(session)
event.listen(sqlite_session_factory, "after_begin", track_admission)
event.listen(repository_factory, "after_begin", track_repository)
passport = PassportService().issue({"app_code": site.code, "app_id": target.id, "end_user_id": end_user.id})
app = Flask(__name__)
app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))
db.init_app(app)
event.listen(db.session.session_factory, "after_begin", track_provider)
app.extensions["application_services"] = _Services(
ConversationNameService(
conversations=ConversationRepository(session_factory=repository_factory),
generate_name=DefaultModelConversationNameGenerator(trace_scopes=untraced_scopes()),
)
)
api = ExternalApi(app)
api.add_resource(ConversationRenameApi, "/conversations/<uuid:c_id>/name")
yield _Harness(
app,
target,
end_user,
conversation,
sqlite_session_factory,
repository_factory,
api,
passport,
admission_sessions,
repository_sessions,
provider_sessions,
)
event.remove(sqlite_session_factory, "after_begin", track_admission)
event.remove(repository_factory, "after_begin", track_repository)
event.remove(db.session.session_factory, "after_begin", track_provider)
with app.app_context():
db.session.remove()
db.engine.dispose()
def _publish_mode(harness: _Harness, mode: AppMode) -> None:
with harness.factory.begin() as session:
target = session.get(App, harness.target.id)
assert target is not None
target.mode = mode
if mode in {AppMode.ADVANCED_CHAT, AppMode.WORKFLOW}:
workflow = make_workflow(
tenant_id=target.tenant_id,
app_id=target.id,
workflow_type=WorkflowType.CHAT if mode == AppMode.ADVANCED_CHAT else WorkflowType.WORKFLOW,
version="published",
)
session.add(workflow)
session.flush()
target.workflow_id = workflow.id
elif mode == AppMode.AGENT:
agent = Agent(
tenant_id=target.tenant_id,
app_id=target.id,
name="Published agent",
scope=AgentScope.ROSTER,
source=AgentSource.AGENT_APP,
)
session.add(agent)
session.flush()
snapshot = AgentConfigSnapshot(
tenant_id=target.tenant_id,
agent_id=agent.id,
version=1,
config_snapshot=AgentSoulConfig(),
)
session.add(snapshot)
session.flush()
agent.active_config_snapshot_id = snapshot.id
session.add(
AgentConfigRevision(
tenant_id=target.tenant_id,
agent_id=agent.id,
current_snapshot_id=snapshot.id,
revision=1,
operation=AgentConfigRevisionOperation.PUBLISH_DRAFT,
)
)
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])
def test_manual_rename_persists_name_and_preserves_response_contract(harness: _Harness, mode: AppMode) -> None:
_publish_mode(harness, mode)
response = harness.post({"name": "Manual title"})
assert response.status_code == HTTPStatus.OK
assert response.headers["Content-Type"] == "application/json"
body = response.get_json()
assert body == {
"id": harness.conversation.id,
"name": "Manual title",
"inputs": _INPUTS,
"status": "normal",
"introduction": None,
"created_at": int(_CREATED_AT.timestamp()),
"updated_at": body["updated_at"],
}
assert isinstance(body["updated_at"], int)
assert body["updated_at"] > int(_UPDATED_AT.timestamp())
harness.assert_name("Manual title")
assert harness.repository_sessions
harness.assert_closed()
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
def test_non_chat_mode_is_rejected_before_repository(harness: _Harness, mode: AppMode) -> None:
_publish_mode(harness, mode)
response = harness.post({"name": "Manual title"})
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "not_chat_app"
assert not harness.repository_sessions
harness.assert_name("Original title")
harness.assert_closed()
@pytest.mark.parametrize("body", [{}, {"name": None}, {"name": ""}, {"name": " "}, {"name": []}])
def test_invalid_manual_payload_is_rejected_without_writing(harness: _Harness, body: dict[str, object]) -> None:
response = harness.post(body)
assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
assert response.get_json()["code"] == "unprocessable_entity"
assert not harness.repository_sessions
harness.assert_name("Original title")
harness.assert_closed()
@pytest.mark.parametrize("field", ["app_id", "from_end_user_id", "from_source", "from_account_id", "is_deleted"])
def test_conversation_outside_end_user_scope_is_not_renamed(harness: _Harness, field: str) -> None:
value: str | bool
if field == "is_deleted":
value = True
elif field == "from_source":
value = ConversationFromSource.CONSOLE
else:
value = str(uuid4())
with harness.factory.begin() as session:
session.execute(update(Conversation).where(Conversation.id == harness.conversation.id).values({field: value}))
response = harness.post({"name": "Manual title"})
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "conversation_not_found"
assert response.get_json()["message"] == "The conversation was not found for this user and app."
harness.assert_name("Original title")
harness.assert_closed()
@pytest.mark.parametrize("field", ["app_id", "tenant_id"])
def test_end_user_outside_app_owner_scope_is_rejected(harness: _Harness, field: Literal["app_id", "tenant_id"]) -> None:
with harness.factory.begin() as session:
session.execute(update(EndUser).where(EndUser.id == harness.end_user.id).values({field: str(uuid4())}))
response = harness.post({"name": "Manual title"})
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "not_found"
assert not harness.repository_sessions
harness.assert_name("Original title")
harness.assert_closed()
def test_auto_rename_preserves_existing_name_when_real_provider_lookup_fails(
harness: _Harness, caplog: pytest.LogCaptureFixture
) -> None:
response = harness.post({"auto_generate": True, "name": "Ignored manual title"})
assert response.status_code == HTTPStatus.OK
assert response.get_json()["name"] == "Original title"
assert response.get_json()["updated_at"] == int(_UPDATED_AT.timestamp())
failures = [
record.exc_info[1]
for record in caplog.records
if record.name == "services.conversation_name_service" and record.exc_info is not None
]
assert len(failures) == 1
assert isinstance(failures[0], ProviderTokenNotInitError)
assert str(failures[0]) == "Default model not found for llm"
harness.assert_name("Original title")
harness.assert_closed()
def test_auto_rename_without_first_message_returns_specific_not_found(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(delete(Message).where(Message.conversation_id == harness.conversation.id))
response = harness.post({"auto_generate": True})
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "conversation_first_message_not_found"
assert response.get_json()["message"] == "The conversation has no message from which to generate a name."
harness.assert_name("Original title")
harness.assert_closed()
def test_restored_remote_input_file_keeps_existing_wire_shape(harness: _Harness) -> None:
file = File(
file_id=str(uuid4()),
file_type=FileType.DOCUMENT,
transfer_method=FileTransferMethod.REMOTE_URL,
remote_url="https://example.com/document.pdf",
filename="document.pdf",
extension=".pdf",
mime_type="application/pdf",
size=12,
)
with harness.factory.begin() as session:
conversation = session.get(Conversation, harness.conversation.id)
assert conversation is not None
conversation.inputs = {"document": file}
response = harness.post({"name": "Manual title"})
assert response.status_code == HTTPStatus.OK
assert response.get_json()["inputs"] == {"document": dict(file.to_dict())}
harness.assert_name("Manual title")
harness.assert_closed()
@pytest.mark.parametrize("passport", ["missing", "invalid"])
def test_web_admission_rejects_missing_or_invalid_passport(harness: _Harness, passport: str) -> None:
headers = {HEADER_NAME_APP_CODE: "public-app"}
if passport == "invalid":
headers[HEADER_NAME_PASSPORT] = "invalid-passport"
response = harness.app.test_client().post(
f"/conversations/{harness.conversation.id}/name", json={"name": "Manual title"}, headers=headers
)
assert response.status_code == HTTPStatus.UNAUTHORIZED
assert response.get_json()["code"] == "unauthorized"
assert response.get_json()["message"] == ("App token is missing." if passport == "missing" else "Invalid token.")
assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
assert not harness.repository_sessions
harness.assert_name("Original title")
def test_web_admission_rejects_disabled_site_before_rename(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(enable_site=False))
response = harness.post({"name": "Manual title"})
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "app_not_found"
assert response.get_json()["message"] == "App not found."
assert not harness.repository_sessions
harness.assert_name("Original title")
harness.assert_closed()
@@ -0,0 +1,225 @@
"""Web feedback through real passport admission, services, and SQLite writes."""
from collections.abc import Callable, Iterator
from dataclasses import dataclass
from decimal import Decimal
from http import HTTPStatus
from uuid import uuid4
import pytest
from flask import Flask
from redis import Redis
from sqlalchemy import Connection, Engine, event, select, update
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
from constants import HEADER_NAME_APP_CODE, HEADER_NAME_PASSPORT
from controllers.web.message import MessageFeedbackApi, MessageListApi
from enums import DeploymentEdition
from extensions.ext_application_services import build_application_services
from extensions.ext_database import db
from extensions.ext_redis import RedisClientWrapper
from libs.external_api import ExternalApi
from libs.passport import PassportService
from models.enums import ConversationFromSource, CustomizeTokenStrategy, FeedbackFromSource, FeedbackRating
from models.model import App, AppModelConfig, EndUser, Message, MessageFeedback, Site
from tests.helpers.unit.model_factories import make_app, make_conversation, make_end_user, make_message
@dataclass(frozen=True)
class _Harness:
app: Flask
target: App
end_user: EndUser
message: Message
factory: sessionmaker[Session]
sessions: list[Session]
headers: dict[str, str]
def post(self, payload: dict[str, object], *, message_id: str | None = None) -> TestResponse:
return self.app.test_client().post(
f"/messages/{message_id or self.message.id}/feedbacks", json=payload, headers=self.headers
)
def feedbacks(self) -> list[MessageFeedback]:
with self.factory() as session:
return list(session.scalars(select(MessageFeedback)).all())
def assert_closed(self) -> None:
assert self.sessions
assert all(not session.in_transaction() and not session.identity_map for session in self.sessions)
@pytest.fixture
def harness(
config_overrides: Callable[..., None],
sqlite_engine: Engine,
sqlite_session_factory: sessionmaker[Session],
) -> Iterator[_Harness]:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY, SECRET_KEY="feedback-passport-test-key-32-bytes")
target = make_app(app_id=str(uuid4()), tenant_id=str(uuid4()))
end_user = make_end_user(end_user_id=str(uuid4()), tenant_id=target.tenant_id, app_id=target.id)
config = AppModelConfig(app_id=target.id)
target.app_model_config_id = config.id
conversation = make_conversation(
conversation_id=str(uuid4()),
inputs={},
app_id=target.id,
from_source=ConversationFromSource.API,
from_end_user_id=end_user.id,
)
message = make_message(
message_id=str(uuid4()),
inputs={},
message={},
query="question",
answer="answer",
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
currency="USD",
app_id=target.id,
conversation_id=conversation.id,
from_source=ConversationFromSource.API,
from_end_user_id=end_user.id,
)
site = Site(
app_id=target.id,
title="Feedback app",
default_language="en-US",
customize_token_strategy=CustomizeTokenStrategy.UUID,
code="feedback-app",
)
with sqlite_session_factory.begin() as session:
session.add_all([target, config, end_user, conversation, message, site])
app = Flask(__name__)
app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))
db.init_app(app)
factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
sessions: list[Session] = []
def track(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
assert all(not previous.in_transaction() for previous in sessions if previous is not session)
sessions.append(session)
event.listen(sqlite_session_factory, "after_begin", track)
event.listen(factory, "after_begin", track)
event.listen(db.session.session_factory, "after_begin", track)
redis_client = Redis()
redis = RedisClientWrapper()
redis.initialize(redis_client)
app.extensions["application_services"] = build_application_services(
database_client=factory, deployment_edition=DeploymentEdition.COMMUNITY, initialization_password="", redis=redis
)
api = ExternalApi(app)
api.add_resource(MessageFeedbackApi, "/messages/<uuid:message_id>/feedbacks")
api.add_resource(MessageListApi, "/messages")
passport = PassportService().issue({"app_code": site.code, "app_id": target.id, "end_user_id": end_user.id})
yield _Harness(
app,
target,
end_user,
message,
sqlite_session_factory,
sessions,
{HEADER_NAME_APP_CODE: "feedback-app", HEADER_NAME_PASSPORT: passport},
)
event.remove(sqlite_session_factory, "after_begin", track)
event.remove(factory, "after_begin", track)
event.remove(db.session.session_factory, "after_begin", track)
redis_client.close()
with app.app_context():
db.session.remove()
db.engine.dispose()
def test_create_update_and_revoke_user_feedback(harness: _Harness) -> None:
created = harness.post({"rating": "like", "content": "有帮助"})
assert created.status_code == HTTPStatus.OK
assert created.get_json() == {"result": "success"}
assert created.headers["Content-Type"] == "application/json"
[feedback] = harness.feedbacks()
assert feedback.app_id == harness.target.id
assert feedback.conversation_id == harness.message.conversation_id
assert feedback.message_id == harness.message.id
assert feedback.from_end_user_id == harness.end_user.id
assert feedback.from_account_id is None
assert feedback.from_source == FeedbackFromSource.USER
assert feedback.rating == FeedbackRating.LIKE
assert feedback.content == "有帮助"
feedback_id = feedback.id
updated = harness.post({"rating": "dislike", "content": ""})
assert updated.status_code == HTTPStatus.OK
[feedback] = harness.feedbacks()
assert feedback.id == feedback_id
assert feedback.rating == FeedbackRating.DISLIKE
assert feedback.content == ""
revoked = harness.post({"rating": None})
assert revoked.status_code == HTTPStatus.OK
assert revoked.get_json() == {"result": "success"}
assert harness.feedbacks() == []
harness.assert_closed()
@pytest.mark.parametrize("payload", [{}, {"rating": None, "content": "comment only"}])
def test_revoke_without_feedback_has_specific_error(harness: _Harness, payload: dict[str, object]) -> None:
response = harness.post(payload)
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "message_feedback_rating_required"
assert harness.feedbacks() == []
harness.assert_closed()
@pytest.mark.parametrize("field", ["app_id", "from_end_user_id", "from_account_id", "from_source"])
def test_message_ownership_is_enforced(harness: _Harness, field: str) -> None:
value = ConversationFromSource.CONSOLE if field == "from_source" else str(uuid4())
with harness.factory.begin() as session:
session.execute(update(Message).where(Message.id == harness.message.id).values({field: value}))
response = harness.post({"rating": "like"})
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "not_found"
assert harness.feedbacks() == []
harness.assert_closed()
def test_missing_message_returns_not_found(harness: _Harness) -> None:
response = harness.post({"rating": "like"}, message_id=str(uuid4()))
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["message"] == "Message Not Exists."
harness.assert_closed()
@pytest.mark.parametrize("field", ["app_id", "tenant_id"])
def test_corrupt_end_user_scope_cannot_write_feedback(harness: _Harness, field: str) -> None:
with harness.factory.begin() as session:
session.execute(update(EndUser).where(EndUser.id == harness.end_user.id).values({field: str(uuid4())}))
response = harness.post({"rating": "like"})
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "not_found"
assert harness.feedbacks() == []
def test_invalid_rating_is_rejected_without_persistence(harness: _Harness) -> None:
response = harness.post({"rating": "neutral"})
assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
assert response.get_json()["code"] == "unprocessable_entity"
assert harness.feedbacks() == []
def test_revoking_user_feedback_preserves_admin_feedback(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.add(
MessageFeedback(
app_id=harness.target.id,
conversation_id=harness.message.conversation_id,
message_id=harness.message.id,
rating=FeedbackRating.LIKE,
from_source=FeedbackFromSource.ADMIN,
from_account_id=str(uuid4()),
)
)
assert harness.post({"rating": "dislike"}).status_code == HTTPStatus.OK
assert harness.post({"rating": None}).status_code == HTTPStatus.OK
[remaining] = harness.feedbacks()
assert remaining.from_source == FeedbackFromSource.ADMIN
@@ -0,0 +1,346 @@
"""Web message history through real passport admission and detached SQLite reads."""
import json
from collections.abc import Callable, Iterator
from datetime import datetime, timedelta
from decimal import Decimal
from http import HTTPStatus
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from threading import Thread
from typing import override
from uuid import uuid4
import pytest
from sqlalchemy import delete, event, update
from sqlalchemy.orm import Session
from werkzeug.test import TestResponse
from core.helper import http_client_pooling
from graphon.file import FileTransferMethod, FileType
from models.enums import (
ConversationFromSource,
CreatorUserRole,
FeedbackFromSource,
FeedbackRating,
MessageFileBelongsTo,
)
from models.model import App, AppMode, Conversation, EndUser, Message, MessageAgentThought, MessageFeedback, MessageFile
from models.workflow import Workflow
from tests.e2e_tests.contracts.controllers.web.test_message_feedback import _Harness
from tests.e2e_tests.contracts.controllers.web.test_message_feedback import harness as admission_harness
from tests.helpers.unit.model_factories import make_message
harness = admission_harness
@pytest.fixture
def remote_files(
harness: _Harness, config_overrides: Callable[..., None], monkeypatch: pytest.MonkeyPatch
) -> Iterator[tuple[str, list[tuple[str, str, bool]]]]:
config_overrides(SSRF_PROXY_HTTP_URL="", SSRF_PROXY_HTTPS_URL="", SSRF_PROXY_ALL_URL="")
requests: list[tuple[str, str, bool]] = []
class Handler(BaseHTTPRequestHandler):
@override
def log_message(self, format: str, *args: object) -> None:
pass
def do_HEAD(self) -> None:
requests.append((self.command, self.path, any(session.in_transaction() for session in harness.sessions)))
self.send_response(HTTPStatus.OK)
self.send_header("Content-Type", "application/octet-stream")
self.send_header("Content-Length", "42")
self.send_header("Content-Disposition", "attachment; filename*=UTF-8''downloaded%20image.png")
self.end_headers()
http_pool = http_client_pooling.HttpClientPoolFactory()
monkeypatch.setattr(http_client_pooling, "_factory", http_pool)
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
thread = Thread(target=server.serve_forever, kwargs={"poll_interval": 0.01}, daemon=True)
thread.start()
try:
yield f"http://127.0.0.1:{server.server_port}", requests
finally:
server.shutdown()
server.server_close()
thread.join()
http_pool.close_all()
def _get(harness: _Harness, **query: object) -> TestResponse:
return harness.app.test_client().get(
"/messages",
query_string={"conversation_id": harness.message.conversation_id, **query},
headers=harness.headers,
)
def test_message_list_preserves_wire_shape_and_loaded_relationships(
harness: _Harness, remote_files: tuple[str, list[tuple[str, str, bool]]]
) -> None:
remote_url, requests = remote_files
created_at = datetime(2024, 1, 1, 12)
thought = MessageAgentThought(
message_id=harness.message.id,
message_chain_id=str(uuid4()),
position=1,
created_by_role=CreatorUserRole.END_USER,
created_by=harness.end_user.id,
thought="thinking",
tool="search",
tool_labels_str='{"search": "Search"}',
message_files='["file-a"]',
)
thought.created_at = created_at
attachment = MessageFile(
message_id=harness.message.id,
type=FileType.IMAGE,
transfer_method=FileTransferMethod.REMOTE_URL,
created_by_role=CreatorUserRole.END_USER,
created_by=harness.end_user.id,
belongs_to=MessageFileBelongsTo.USER,
url=f"{remote_url}/user-image.png",
)
assistant_attachment = MessageFile(
message_id=harness.message.id,
type=FileType.IMAGE,
transfer_method=FileTransferMethod.REMOTE_URL,
created_by_role=CreatorUserRole.END_USER,
created_by=harness.end_user.id,
belongs_to=MessageFileBelongsTo.ASSISTANT,
url=f"{remote_url}/assistant-image.png",
)
resource_id = str(uuid4())
metadata = {
"custom": "metadata",
"retriever_resources": [{"id": resource_id, "message_id": harness.message.id, "position": 1}],
}
with harness.factory.begin() as session:
session.execute(
update(Message)
.where(Message.id == harness.message.id)
.values(
{
Message._inputs: {"text": "hello", "nested": {"value": [1, None]}},
Message.message_metadata: json.dumps(metadata),
Message.created_at: created_at,
Message.message_tokens: 3,
Message.answer_tokens: 5,
Message.total_price: Decimal("0.0012"),
Message.provider_response_latency: 0.25,
}
)
)
session.add_all(
[
thought,
attachment,
assistant_attachment,
MessageFeedback(
app_id=harness.target.id,
conversation_id=harness.message.conversation_id,
message_id=harness.message.id,
rating=FeedbackRating.LIKE,
from_source=FeedbackFromSource.USER,
from_end_user_id=harness.end_user.id,
),
]
)
commits: list[Session] = []
def record_commit(session: Session) -> None:
commits.append(session)
event.listen(Session, "before_commit", record_commit)
try:
response = _get(harness)
finally:
event.remove(Session, "before_commit", record_commit)
assert response.status_code == HTTPStatus.OK
assert not commits
assert sorted(requests) == [("HEAD", "/assistant-image.png", False), ("HEAD", "/user-image.png", False)]
assert response.headers["Content-Type"] == "application/json"
body = response.get_json()
assert body["limit"] == 20
assert body["has_more"] is False
[item] = body["data"]
assert set(item) == {
"id",
"conversation_id",
"parent_message_id",
"inputs",
"query",
"answer",
"feedback",
"retriever_resources",
"created_at",
"agent_thoughts",
"message_files",
"message_tokens",
"answer_tokens",
"provider_response_latency",
"total_price",
"currency",
"status",
"error",
"extra_contents",
"total_tokens",
"metadata",
}
assert item["id"] == harness.message.id
assert item["conversation_id"] == harness.message.conversation_id
assert item["parent_message_id"] is None
assert item["inputs"] == {"text": "hello", "nested": {"value": [1, None]}}
assert item["query"] == "question"
assert item["answer"] == "answer"
assert item["feedback"] == {"rating": "like"}
assert item["metadata"] == metadata
assert item["retriever_resources"][0]["id"] == resource_id
assert item["created_at"] == int(created_at.timestamp())
assert item["message_tokens"] == 3
assert item["answer_tokens"] == 5
assert item["total_tokens"] == 8
assert Decimal(item["total_price"]) == Decimal("0.0012")
assert item["provider_response_latency"] == 0.25
assert item["currency"] == "USD"
assert item["status"] == "normal"
assert item["error"] is None
assert item["extra_contents"] == []
[actual_thought] = item["agent_thoughts"]
assert actual_thought["id"] == thought.id
assert actual_thought["chain_id"] == thought.message_chain_id
assert actual_thought["tool_labels"] == {"search": "Search"}
assert actual_thought["files"] == ["file-a"]
assert actual_thought["created_at"] == int(created_at.timestamp())
assert len(item["message_files"]) == 2
actual_files = {file["id"]: file for file in item["message_files"]}
for expected in (attachment, assistant_attachment):
actual_file = actual_files[expected.id]
assert expected.belongs_to is not None
assert actual_file["url"] == expected.url
assert actual_file["transfer_method"] == "remote_url"
assert actual_file["type"] == "image"
assert actual_file["belongs_to"] == expected.belongs_to.value
assert actual_file["filename"] == "downloaded image.png"
assert actual_file["mime_type"] == "image/png"
assert actual_file["size"] == 42
harness.assert_closed()
def test_pagination_returns_latest_slice_in_ascending_order(harness: _Harness) -> None:
created_at = datetime(2024, 1, 1)
messages = [
make_message(
message_id=str(uuid4()),
app_id=harness.target.id,
conversation_id=harness.message.conversation_id,
inputs={},
query=f"question-{index}",
message={},
answer=f"answer-{index}",
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
currency="USD",
from_source=ConversationFromSource.API,
from_end_user_id=harness.end_user.id,
created_at=created_at + timedelta(seconds=index),
)
for index in (1, 2)
]
with harness.factory.begin() as session:
session.execute(update(Message).where(Message.id == harness.message.id).values(created_at=created_at))
session.add_all(messages)
response = _get(harness, limit=2)
assert response.status_code == HTTPStatus.OK
body = response.get_json()
assert body["limit"] == 2
assert body["has_more"] is True
assert [item["id"] for item in body["data"]] == [message.id for message in messages]
previous = _get(harness, first_id=messages[0].id, limit=2).get_json()
assert previous["has_more"] is False
assert [item["id"] for item in previous["data"]] == [harness.message.id]
assert _get(harness, first_id=harness.message.id, limit=2).get_json() == {"limit": 2, "has_more": False, "data": []}
harness.assert_closed()
def test_empty_conversation_keeps_pagination_shape(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(delete(Message).where(Message.id == harness.message.id))
assert _get(harness, limit=100).get_json() == {"limit": 100, "has_more": False, "data": []}
harness.assert_closed()
@pytest.mark.parametrize("field", ["app_id", "from_end_user_id", "from_account_id", "from_source"])
def test_conversation_ownership_is_enforced(harness: _Harness, field: str) -> None:
value = ConversationFromSource.CONSOLE if field == "from_source" else str(uuid4())
with harness.factory.begin() as session:
session.execute(
update(Conversation).where(Conversation.id == harness.message.conversation_id).values({field: value})
)
response = _get(harness)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "not_found"
assert response.get_json()["message"] == "Conversation Not Exists."
harness.assert_closed()
@pytest.mark.parametrize("query", [{"conversation_id": str(uuid4())}, {"first_id": str(uuid4())}])
def test_missing_conversation_or_cursor_has_precise_error(harness: _Harness, query: dict[str, str]) -> None:
response = _get(harness, **query)
assert response.status_code == HTTPStatus.NOT_FOUND
expected = "Conversation Not Exists." if "conversation_id" in query else "First Message Not Exists."
assert response.get_json()["message"] == expected
harness.assert_closed()
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
def test_non_chat_app_is_rejected(harness: _Harness, mode: AppMode) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
if mode == AppMode.WORKFLOW:
workflow = Workflow.new(
tenant_id=harness.target.tenant_id,
app_id=harness.target.id,
type="workflow",
version="published",
graph="{}",
features="{}",
created_by=str(uuid4()),
environment_variables=[],
conversation_variables=[],
rag_pipeline_variables=[],
)
session.add(workflow)
session.flush()
session.execute(update(App).where(App.id == harness.target.id).values(workflow_id=workflow.id))
response = _get(harness)
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "not_chat_app"
harness.assert_closed()
@pytest.mark.parametrize("query", [{"limit": 0}, {"limit": 101}, {"conversation_id": "bad"}, {"first_id": "bad"}])
def test_invalid_query_is_rejected(harness: _Harness, query: dict[str, object]) -> None:
response = _get(harness, **query)
assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
assert response.get_json()["code"] == "unprocessable_entity"
harness.assert_closed()
@pytest.mark.parametrize("field", ["app_id", "tenant_id"])
def test_corrupt_end_user_scope_is_rejected(harness: _Harness, field: str) -> None:
with harness.factory.begin() as session:
session.execute(update(EndUser).where(EndUser.id == harness.end_user.id).values({field: str(uuid4())}))
response = _get(harness)
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "not_found"
harness.assert_closed()
def test_missing_passport_is_rejected(harness: _Harness) -> None:
response = harness.app.test_client().get(
"/messages", query_string={"conversation_id": harness.message.conversation_id}
)
assert response.status_code == HTTPStatus.UNAUTHORIZED
assert response.get_json()["code"] == "unauthorized"
@@ -0,0 +1,415 @@
"""Web more-like-this through real JWT, services, generator, and SQLite.
Persisted invalid provider metadata triggers model preparation failure without a
model server. Successful generation belongs to runtime integration coverage.
"""
import json
from collections.abc import Callable, Iterator
from dataclasses import dataclass
from decimal import Decimal
from http import HTTPStatus
from uuid import uuid4
import pytest
from flask import Flask
from redis import Redis
from sqlalchemy import Connection, Engine, delete, event, update
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
import controllers.web.message as module
from constants import HEADER_NAME_APP_CODE, HEADER_NAME_PASSPORT
from enums import DeploymentEdition
from extensions.ext_application_services import build_application_services
from extensions.ext_database import db
from extensions.ext_redis import RedisClientWrapper
from libs.external_api import ExternalApi
from libs.passport import PassportService
from models.agent import (
Agent,
AgentConfigRevision,
AgentConfigRevisionOperation,
AgentConfigSnapshot,
AgentScope,
AgentSource,
)
from models.agent_config_entities import AgentSoulConfig
from models.enums import ConversationFromSource, CustomizeTokenStrategy
from models.model import App, AppMode, AppModelConfig, Conversation, EndUser, Message, Site
from models.provider import Provider
from models.workflow import Workflow
from tests.helpers.unit.model_factories import make_app, make_conversation, make_end_user, make_message
@dataclass(frozen=True)
class _Harness:
app: Flask
target: App
end_user: EndUser
message: Message
current_config: AppModelConfig
historical_config: AppModelConfig
factory: sessionmaker[Session]
sessions: list[Session]
query_sessions: list[Session]
passport: str
@property
def headers(self) -> dict[str, str]:
return {HEADER_NAME_APP_CODE: "more-like-this-app", HEADER_NAME_PASSPORT: self.passport}
def url(self, *, query: str = "response_mode=blocking") -> str:
return f"/messages/{self.message.id}/more-like-this?{query}"
def get(self, *, query: str = "response_mode=blocking") -> TestResponse:
return self.app.test_client().get(self.url(query=query), headers=self.headers)
def assert_closed(self, *, caller: Session | None = None) -> None:
assert self.sessions
assert all(
not session.in_transaction() and not session.identity_map
for session in self.sessions
if session is not caller
)
@pytest.fixture
def harness(
config_overrides: Callable[..., None],
sqlite_engine: Engine,
sqlite_session_factory: sessionmaker[Session],
) -> Iterator[_Harness]:
config_overrides(
DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY, SECRET_KEY="more-like-this-http-test-key-at-least-32-bytes"
)
target = make_app(app_id=str(uuid4()), tenant_id=str(uuid4()), mode=AppMode.COMPLETION)
end_user = make_end_user(end_user_id=str(uuid4()), tenant_id=target.tenant_id, app_id=target.id)
current_config = AppModelConfig(app_id=target.id, more_like_this='{"enabled":true}', pre_prompt="New prompt")
historical_config = AppModelConfig(
app_id=target.id,
more_like_this='{"enabled":false}',
pre_prompt="Historical prompt",
model=json.dumps(
{
"provider": "openai",
"name": "historical-model",
"mode": "completion",
"completion_params": {"temperature": 0.2},
}
),
)
site = Site(
app_id=target.id,
title="Completion app",
default_language="en-US",
customize_token_strategy=CustomizeTokenStrategy.UUID,
code="more-like-this-app",
)
conversation = make_conversation(
conversation_id=str(uuid4()),
app_id=target.id,
mode=AppMode.COMPLETION,
from_source=ConversationFromSource.API,
from_end_user_id=end_user.id,
inputs={"topic": "Earlier topic"},
)
message = make_message(
message_id=str(uuid4()),
app_id=target.id,
conversation_id=conversation.id,
inputs={"topic": "Earlier topic"},
message={},
query="Original query",
answer="Original answer",
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
currency="USD",
from_source=ConversationFromSource.API,
from_end_user_id=end_user.id,
)
with sqlite_session_factory.begin() as session:
# A real stored provider ID fails validation before plugin discovery.
provider = Provider(tenant_id=target.tenant_id, provider_name="invalid/provider", is_valid=True)
session.add_all([target, end_user, current_config, historical_config, site, conversation, message, provider])
session.flush()
target.app_model_config_id = current_config.id
conversation.app_model_config_id = historical_config.id
admission_factory = sqlite_session_factory
query_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
sessions: list[Session] = []
query_sessions: list[Session] = []
def track(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
sessions.append(session)
def track_query(session: Session, transaction: SessionTransaction, connection: Connection) -> None:
query_sessions.append(session)
track(session, transaction, connection)
event.listen(admission_factory, "after_begin", track)
event.listen(query_factory, "after_begin", track_query)
passport = PassportService().issue({"app_code": site.code, "app_id": target.id, "end_user_id": end_user.id})
app = Flask(__name__)
app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))
db.init_app(app)
event.listen(db.session.session_factory, "after_begin", track)
redis_client = Redis()
redis = RedisClientWrapper()
redis.initialize(redis_client)
app.extensions["application_services"] = build_application_services(
database_client=query_factory,
deployment_edition=DeploymentEdition.COMMUNITY,
initialization_password="",
redis=redis,
)
api = ExternalApi(app)
api.add_resource(module.MessageMoreLikeThisApi, "/messages/<uuid:message_id>/more-like-this")
yield _Harness(
app,
target,
end_user,
message,
current_config,
historical_config,
sqlite_session_factory,
sessions,
query_sessions,
passport,
)
event.remove(admission_factory, "after_begin", track)
event.remove(query_factory, "after_begin", track_query)
event.remove(db.session.session_factory, "after_begin", track)
redis_client.close()
with app.app_context():
db.session.remove()
db.engine.dispose()
@pytest.mark.parametrize("response_mode", ["blocking", "streaming"])
def test_stored_provider_error_propagates_through_the_real_generator(harness: _Harness, response_mode: str) -> None:
response = harness.get(query=f"response_mode={response_mode}")
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json() == {
"code": "invalid_param",
"message": "Invalid plugin id invalid/provider",
"status": HTTPStatus.BAD_REQUEST,
}
assert response.headers["Content-Type"] == "application/json"
with harness.factory() as session:
persisted = session.get(AppModelConfig, harness.historical_config.id)
assert persisted is not None
assert persisted.model_dict["completion_params"]["temperature"] == 0.2
harness.assert_closed()
def test_real_preparation_failure_preserves_caller_session_and_pending_edits(harness: _Harness) -> None:
with harness.app.app_context():
caller = db.session()
app_model = caller.get(App, harness.target.id)
assert app_model is not None
app_model.name = "Uncommitted caller change"
response = harness.get()
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["message"] == "Invalid plugin id invalid/provider"
assert db.session() is caller
assert caller.in_transaction()
assert app_model in caller.dirty
harness.assert_closed(caller=caller)
with harness.factory() as session:
persisted = session.get(App, harness.target.id)
assert persisted is not None
assert persisted.name == harness.target.name
@pytest.mark.parametrize("query", ["", "response_mode=invalid"])
def test_invalid_response_mode_is_rejected_before_source_queries(harness: _Harness, query: str) -> None:
response = harness.get(query=query)
assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
assert response.get_json()["code"] == "unprocessable_entity"
assert harness.query_sessions == []
harness.assert_closed()
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT, AppMode.WORKFLOW])
def test_non_completion_app_is_rejected_before_source_queries(harness: _Harness, mode: AppMode) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
if mode in {AppMode.ADVANCED_CHAT, AppMode.WORKFLOW}:
workflow = Workflow.new(
tenant_id=harness.target.tenant_id,
app_id=harness.target.id,
type="chat" if mode == AppMode.ADVANCED_CHAT else "workflow",
version="published",
graph="{}",
features="{}",
created_by=str(uuid4()),
environment_variables=[],
conversation_variables=[],
rag_pipeline_variables=[],
)
session.add(workflow)
session.flush()
session.execute(update(App).where(App.id == harness.target.id).values(workflow_id=workflow.id))
elif mode == AppMode.AGENT:
agent = Agent(
tenant_id=harness.target.tenant_id,
app_id=harness.target.id,
name="Published agent",
scope=AgentScope.ROSTER,
source=AgentSource.AGENT_APP,
)
session.add(agent)
session.flush()
snapshot = AgentConfigSnapshot(
tenant_id=agent.tenant_id,
agent_id=agent.id,
version=1,
config_snapshot=AgentSoulConfig(),
)
session.add(snapshot)
session.flush()
agent.active_config_snapshot_id = snapshot.id
session.add(
AgentConfigRevision(
tenant_id=agent.tenant_id,
agent_id=agent.id,
current_snapshot_id=snapshot.id,
revision=1,
operation=AgentConfigRevisionOperation.PUBLISH_DRAFT,
)
)
response = harness.get()
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "not_completion_app"
assert harness.query_sessions == []
@pytest.mark.parametrize("feature", [None, "", "{}", '{"enabled":false}'])
def test_disabled_current_feature_wins_over_corrupt_historical_configuration(
harness: _Harness, feature: str | None
) -> None:
with harness.factory.begin() as session:
session.execute(
update(AppModelConfig).where(AppModelConfig.id == harness.current_config.id).values(more_like_this=feature)
)
session.execute(
update(AppModelConfig).where(AppModelConfig.id == harness.historical_config.id).values(model="broken-json")
)
response = harness.get()
assert response.status_code == HTTPStatus.FORBIDDEN
assert response.get_json()["code"] == "app_more_like_this_disabled"
harness.assert_closed()
@pytest.mark.parametrize(
("current", "field", "value"),
[
(True, "more_like_this", '{"secret":"private-config-value"'),
(True, "more_like_this", '["private-config-value"]'),
(False, "model", '{"secret":"private-config-value"'),
(False, "model", '["private-config-value"]'),
(False, "model", '{"completion_params":["private-config-value"]}'),
(False, "model", None),
],
ids=["feature-json", "feature-shape", "model-json", "model-shape", "parameter-shape", "missing-model"],
)
def test_corrupt_persisted_configuration_returns_app_unavailable(
harness: _Harness, current: bool, field: str, value: str | None
) -> None:
"""None represents a missing model payload in an existing configuration row."""
config_id = harness.current_config.id if current else harness.historical_config.id
with harness.factory.begin() as session:
session.execute(update(AppModelConfig).where(AppModelConfig.id == config_id).values({field: value}))
response = harness.get()
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json() == {
"code": "app_unavailable",
"message": "App unavailable, please check your app configurations.",
"status": HTTPStatus.BAD_REQUEST,
}
assert b"private-config-value" not in response.data
harness.assert_closed()
@pytest.mark.parametrize("field", ["app_id", "from_end_user_id", "from_account_id", "from_source"])
def test_message_ownership_is_enforced_through_http(harness: _Harness, field: str) -> None:
value = ConversationFromSource.CONSOLE if field == "from_source" else str(uuid4())
with harness.factory.begin() as session:
session.execute(update(Message).where(Message.id == harness.message.id).values({field: value}))
response = harness.get()
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "not_found"
harness.assert_closed()
@pytest.mark.parametrize("field", ["app_id", "tenant_id"])
def test_foreign_end_user_is_rejected_at_jwt_admission(harness: _Harness, field: str) -> None:
with harness.factory.begin() as session:
session.execute(update(EndUser).where(EndUser.id == harness.end_user.id).values({field: str(uuid4())}))
response = harness.get()
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["code"] == "not_found"
assert harness.query_sessions == []
harness.assert_closed()
@pytest.mark.parametrize("field", ["app_id", "from_end_user_id", "from_account_id", "from_source"])
def test_foreign_conversation_cannot_supply_historical_config(harness: _Harness, field: str) -> None:
value = ConversationFromSource.CONSOLE if field == "from_source" else str(uuid4())
with harness.factory.begin() as session:
session.execute(
update(Conversation).where(Conversation.id == harness.message.conversation_id).values({field: value})
)
response = harness.get()
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "app_unavailable"
harness.assert_closed()
@pytest.mark.parametrize("resource", ["conversation", "historical_config", "foreign_config"])
def test_missing_or_foreign_history_is_app_unavailable(harness: _Harness, resource: str) -> None:
with harness.factory.begin() as session:
if resource == "conversation":
session.execute(delete(Conversation).where(Conversation.id == harness.message.conversation_id))
elif resource == "historical_config":
session.execute(delete(AppModelConfig).where(AppModelConfig.id == harness.historical_config.id))
else:
session.execute(
update(AppModelConfig)
.where(AppModelConfig.id == harness.historical_config.id)
.values(app_id=str(uuid4()))
)
response = harness.get()
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "app_unavailable"
harness.assert_closed()
def test_incomplete_historical_model_reaches_generic_error_without_exposing_configuration(
harness: _Harness, caplog: pytest.LogCaptureFixture
) -> None:
# The persisted model is an object, but it lacks the provider field required
# by the real completion configuration converter.
with harness.factory.begin() as session:
session.execute(
update(AppModelConfig)
.where(AppModelConfig.id == harness.historical_config.id)
.values(model=json.dumps({"name": "private-model-name", "completion_params": {}}))
)
response = harness.get()
assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
assert response.get_json()["code"] == "internal_server_error"
assert b"private-model-name" not in response.data
failures = [
record.exc_info[1]
for record in caplog.records
if record.name == "controllers.web.message" and record.exc_info is not None
]
assert len(failures) == 1
assert isinstance(failures[0], KeyError)
assert failures[0].args == ("provider",)
harness.assert_closed()
@@ -0,0 +1,437 @@
"""Web suggested questions through HTTP, real admission, and SQLite ownership queries."""
import json
from collections.abc import Callable, Iterator
from dataclasses import dataclass
from decimal import Decimal
from http import HTTPStatus
from typing import Literal
from uuid import uuid4
import pytest
from flask import Flask
from sqlalchemy import Connection, Engine, delete, event, select, update
from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
from werkzeug.test import TestResponse
import controllers.web.message as message_controller
from constants import HEADER_NAME_APP_CODE, HEADER_NAME_PASSPORT
from enums import DeploymentEdition
from extensions.ext_database import db
from graphon.model_runtime.entities.model_entities import ModelType
from libs.external_api import ExternalApi
from libs.passport import PassportService
from models.agent import (
Agent,
AgentConfigRevision,
AgentConfigRevisionOperation,
AgentConfigSnapshot,
AgentScope,
AgentSource,
)
from models.agent_config_entities import AgentSoulConfig
from models.enums import ConversationFromSource, CustomizeTokenStrategy
from models.model import App, AppMode, AppModelConfig, Conversation, EndUser, Message, Site
from models.provider import Provider, TenantDefaultModel
from models.workflow import Workflow
from repositories.message_repository import MessageRepository
from services.message_suggested_questions_generator import SuggestedQuestionsGenerator
from services.message_suggested_questions_queries import SuggestedQuestionsQuery
from services.message_suggested_questions_service import (
MessageSuggestedQuestions,
MessageSuggestedQuestionsService,
)
from tests.helpers.unit.model_factories import make_app, make_conversation, make_end_user, make_message
from tests.helpers.unit.services.tracing_ports import untraced_scopes
@dataclass
class _Services:
message_suggested_questions: MessageSuggestedQuestions
@dataclass(frozen=True)
class _Harness:
app: Flask
target: App
end_user: EndUser
message: Message
factory: sessionmaker[Session]
admission_factory: sessionmaker[Session]
passport: str
sessions: list[Session]
def get(self) -> TestResponse:
return self.app.test_client().get(
f"/messages/{self.message.id}/suggested-questions",
headers={HEADER_NAME_APP_CODE: "public-app", HEADER_NAME_PASSPORT: self.passport},
)
def assert_closed(self, *, caller: Session | None = None) -> None:
assert self.sessions
assert all(
not session.in_transaction() and not session.identity_map
for session in self.sessions
if session is not caller
)
@pytest.fixture
def harness(
_empty_model_provider_daemon: None,
config_overrides: Callable[..., None],
sqlite_engine: Engine,
sqlite_session_factory: sessionmaker[Session],
) -> Iterator[_Harness]:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY, SECRET_KEY="suggested-questions-test-secret-key")
target = make_app(app_id=str(uuid4()), tenant_id=str(uuid4()))
end_user = make_end_user(end_user_id=str(uuid4()), tenant_id=target.tenant_id, app_id=target.id)
config = AppModelConfig(
app_id=target.id,
suggested_questions_after_answer=json.dumps({"enabled": True, "prompt": "Ask concise questions"}),
)
site = Site(
app_id=target.id,
title="Public app",
default_language="en-US",
customize_token_strategy=CustomizeTokenStrategy.UUID,
code="public-app",
)
conversation = make_conversation(
conversation_id=str(uuid4()),
app_id=target.id,
from_source=ConversationFromSource.API,
from_end_user_id=end_user.id,
inputs={},
)
message = make_message(
message_id=str(uuid4()),
app_id=target.id,
conversation_id=conversation.id,
inputs={},
message={},
query="How does it work?",
answer="Like this.",
message_unit_price=Decimal(0),
answer_unit_price=Decimal(0),
currency="USD",
from_source=ConversationFromSource.API,
from_end_user_id=end_user.id,
)
with sqlite_session_factory.begin() as session:
# The real provider lookup rejects this stored ID before plugin discovery.
# Generation remains best-effort and must return an empty list.
provider = Provider(tenant_id=target.tenant_id, provider_name="invalid/provider", is_valid=True)
session.add(
TenantDefaultModel(
tenant_id=target.tenant_id,
provider_name="invalid/provider",
model_name="invalid-model",
model_type=ModelType.LLM,
)
)
session.add_all([target, end_user, config, site, conversation, message, provider])
session.flush()
target.app_model_config_id = config.id
conversation.app_model_config_id = config.id
read_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
admission_factory = sqlite_session_factory
sessions: list[Session] = []
def track_session(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
sessions.append(session)
admission_sessions: list[Session] = []
def track_admission(session: Session, transaction: SessionTransaction, connection: Connection) -> None:
admission_sessions.append(session)
track_session(session, transaction, connection)
event.listen(admission_factory, "after_begin", track_admission)
event.listen(read_factory, "after_begin", track_session)
passport = PassportService().issue({"app_code": site.code, "app_id": target.id, "end_user_id": end_user.id})
app = Flask(__name__)
app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))
db.init_app(app)
event.listen(db.session.session_factory, "after_begin", track_session)
queries = SuggestedQuestionsQuery(
session_factory=read_factory, repository=MessageRepository(session_factory=read_factory)
)
services = _Services(
MessageSuggestedQuestionsService(
queries=queries, generator=SuggestedQuestionsGenerator(trace_scopes=untraced_scopes())
)
)
app.extensions["application_services"] = services
api = ExternalApi(app)
api.add_resource(message_controller.MessageSuggestedQuestionApi, "/messages/<uuid:message_id>/suggested-questions")
yield _Harness(
app,
target,
end_user,
message,
sqlite_session_factory,
admission_factory,
passport,
sessions,
)
event.remove(admission_factory, "after_begin", track_admission)
event.remove(read_factory, "after_begin", track_session)
event.remove(db.session.session_factory, "after_begin", track_session)
with app.app_context():
db.session.remove()
db.engine.dispose()
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])
def test_chat_modes_return_empty_questions_when_stored_provider_config_is_invalid(
harness: _Harness,
mode: AppMode,
caplog: pytest.LogCaptureFixture,
) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
if mode == AppMode.ADVANCED_CHAT:
workflow = Workflow.new(
tenant_id=harness.target.tenant_id,
app_id=harness.target.id,
type="chat",
version="published",
graph="{}",
features='{"suggested_questions_after_answer":{"enabled":true}}',
created_by=str(uuid4()),
environment_variables=[],
conversation_variables=[],
rag_pipeline_variables=[],
)
session.add(workflow)
session.flush()
app_model = session.get(App, harness.target.id)
assert app_model is not None
app_model.workflow_id = workflow.id
elif mode == AppMode.AGENT:
agent = Agent(
tenant_id=harness.target.tenant_id,
app_id=harness.target.id,
name="Agent",
scope=AgentScope.ROSTER,
source=AgentSource.AGENT_APP,
)
session.add(agent)
session.flush()
snapshot = AgentConfigSnapshot(
tenant_id=agent.tenant_id,
agent_id=agent.id,
version=1,
config_snapshot=AgentSoulConfig.model_validate(
{"app_features": {"suggested_questions_after_answer": {"enabled": True}}}
),
)
session.add(snapshot)
session.flush()
agent.active_config_snapshot_id = snapshot.id
session.add(
AgentConfigRevision(
tenant_id=agent.tenant_id,
agent_id=agent.id,
current_snapshot_id=snapshot.id,
revision=1,
operation=AgentConfigRevisionOperation.PUBLISH_DRAFT,
)
)
response = harness.get()
assert response.status_code == HTTPStatus.OK
assert response.headers["Content-Type"] == "application/json"
assert response.get_json() == {"data": []}
failures = [
record.exc_info[1]
for record in caplog.records
if record.name == "services.message_suggested_questions_generator" and record.exc_info is not None
]
assert len(failures) == 1
assert isinstance(failures[0], ValueError)
assert str(failures[0]) == "Invalid provider: invalid/provider"
harness.assert_closed()
@pytest.mark.parametrize("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
def test_non_chat_app_is_rejected_before_shared_capability(harness: _Harness, mode: AppMode) -> None:
with harness.factory.begin() as session:
session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
if mode == AppMode.WORKFLOW:
workflow = Workflow.new(
tenant_id=harness.target.tenant_id,
app_id=harness.target.id,
type="workflow",
version="published",
graph="{}",
features="{}",
created_by=str(uuid4()),
environment_variables=[],
conversation_variables=[],
rag_pipeline_variables=[],
)
session.add(workflow)
session.flush()
session.execute(update(App).where(App.id == harness.target.id).values(workflow_id=workflow.id))
response = harness.get()
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_json()["code"] == "not_chat_app"
harness.assert_closed()
@pytest.mark.parametrize(
("resource", "status", "code", "message"),
[
("agent_version", 404, "app_not_found", "App not found"),
("message", 404, "not_found", "Message not found"),
("conversation", 404, "not_found", "Conversation not found"),
("disabled", 403, "app_suggested_questions_after_answer_disabled", "disabled"),
],
)
def test_resource_errors_keep_specific_http_codes(
harness: _Harness,
resource: Literal["agent_version", "message", "conversation", "disabled"],
status: int,
code: str,
message: str,
) -> None:
with harness.factory.begin() as session:
if resource == "message":
session.execute(delete(Message).where(Message.id == harness.message.id))
elif resource == "conversation":
session.execute(delete(Conversation).where(Conversation.id == harness.message.conversation_id))
elif resource == "disabled":
session.execute(
update(AppModelConfig)
.where(AppModelConfig.app_id == harness.target.id)
.values(suggested_questions_after_answer='{"enabled":false}')
)
else:
session.execute(update(App).where(App.id == harness.target.id).values(mode=AppMode.AGENT))
session.add(
Agent(
tenant_id=harness.target.tenant_id,
app_id=harness.target.id,
name="Broken agent",
scope=AgentScope.ROSTER,
source=AgentSource.AGENT_APP,
active_config_snapshot_id=str(uuid4()),
)
)
response = harness.get()
assert response.status_code == status
assert response.headers["Content-Type"] == "application/json"
body = response.get_json()
assert body["code"] == code
assert body["status"] == status
assert message in body["message"]
harness.assert_closed()
@pytest.mark.parametrize(
("resource", "status", "code", "message"),
[
("end_user", 404, "not_found", "End user not found"),
(
"app_mode",
400,
"app_unavailable",
"App unavailable, please check your app configurations.",
),
],
)
def test_resource_changes_after_admission_keep_specific_errors(
harness: _Harness,
resource: Literal["end_user", "app_mode"],
status: int,
code: str,
message: str,
) -> None:
# Change persisted state after the real admission transaction ends; the
# application service and repository must reject the now-stale reference.
def change_resource(_session: Session, _transaction: SessionTransaction) -> None:
with harness.factory.begin() as session:
if resource == "end_user":
session.execute(delete(EndUser).where(EndUser.id == harness.end_user.id))
else:
session.execute(update(App).where(App.id == harness.target.id).values(mode=AppMode.WORKFLOW))
event.listen(harness.admission_factory, "after_transaction_end", change_resource, once=True)
try:
response = harness.get()
finally:
event.remove(harness.admission_factory, "after_transaction_end", change_resource)
assert response.status_code == status
assert response.get_json()["code"] == code
assert response.get_json()["message"] == message
harness.assert_closed()
def test_model_resolution_failure_preserves_caller_session_and_pending_edits(harness: _Harness) -> None:
with harness.app.app_context():
caller = db.session()
app_model = caller.get(App, harness.target.id)
assert app_model is not None
app_model.name = "Uncommitted caller change"
response = harness.get()
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"data": []}
assert db.session() is caller
assert caller.in_transaction()
assert app_model in caller.dirty
harness.assert_closed(caller=caller)
with harness.factory() as session:
persisted = session.get(App, harness.target.id)
assert persisted is not None
assert persisted.name == harness.target.name
@pytest.mark.parametrize("field_name", ["tenant_id", "app_id"])
def test_admission_rejects_end_user_from_another_owner(
harness: _Harness,
field_name: Literal["tenant_id", "app_id"],
) -> None:
with harness.factory.begin() as session:
session.execute(update(EndUser).where(EndUser.id == harness.end_user.id).values({field_name: str(uuid4())}))
response = harness.get()
assert response.status_code == HTTPStatus.NOT_FOUND
# Passport admission rejects the owner mismatch before the message service.
assert response.get_json()["code"] == "not_found"
harness.assert_closed()
@pytest.mark.parametrize("model", [Message, Conversation])
@pytest.mark.parametrize("field_name", ["app_id", "from_end_user_id", "from_source"])
def test_real_runtime_rejects_message_or_conversation_outside_actor_scope(
harness: _Harness,
model: type[Message] | type[Conversation],
field_name: Literal["app_id", "from_end_user_id", "from_source"],
) -> None:
record_id = harness.message.id if model is Message else harness.message.conversation_id
value = ConversationFromSource.CONSOLE if field_name == "from_source" else str(uuid4())
with harness.factory.begin() as session:
session.execute(update(model).where(model.id == record_id).values({field_name: value}))
response = harness.get()
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_json()["message"] == ("Message not found" if model is Message else "Conversation not found")
harness.assert_closed()
def test_missing_saved_default_returns_empty_questions_without_creating_a_default(harness: _Harness) -> None:
with harness.factory.begin() as session:
session.execute(delete(TenantDefaultModel).where(TenantDefaultModel.tenant_id == harness.target.tenant_id))
response = harness.get()
assert response.status_code == HTTPStatus.OK
assert response.get_json() == {"data": []}
with harness.factory() as session:
assert (
session.scalar(
select(TenantDefaultModel.id).where(TenantDefaultModel.tenant_id == harness.target.tenant_id)
)
is None
)
harness.assert_closed()