chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
+493
@@ -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)
|
||||
+220
@@ -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),
|
||||
}
|
||||
+553
@@ -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)
|
||||
+161
@@ -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)
|
||||
+264
@@ -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)
|
||||
+337
@@ -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"
|
||||
+477
@@ -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()
|
||||
+457
@@ -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"]
|
||||
+165
@@ -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")
|
||||
+369
@@ -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": []}
|
||||
+515
@@ -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"
|
||||
+185
@@ -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)
|
||||
+239
@@ -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
|
||||
+390
@@ -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()
|
||||
+435
@@ -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()
|
||||
+311
@@ -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"'
|
||||
Reference in new issue
Block a user