Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test(api): verify segment ownership rejection with unbound session (#45148)

zhoulongjie777-gif9dbb3edf6313
diff --git a/api/tests/unit_tests/services/knowledge/segments/test_adapters.py b/api/tests/unit_tests/services/knowledge/segments/test_adapters.py
index 2cc52e817c7..4e63f1c339b 100644
--- a/api/tests/unit_tests/services/knowledge/segments/test_adapters.py
+++ b/api/tests/unit_tests/services/knowledge/segments/test_adapters.py
@@ -1604,7 +1604,9 @@ def test_service_api_partial_qa_update_uses_shared_word_count_and_indexing(
@pytest.mark.parametrize("owner_field", ["tenant_id", "dataset_id", "document_id"])
-def test_legacy_segment_mutation_rejects_mismatched_rows_before_dispatch(owner_field: str) -> None:
+def test_legacy_segment_mutation_rejects_mismatched_rows_before_dispatch(
+ owner_field: str, unbound_session: Session
+) -> None:
from services.knowledge.segments.application import SegmentMutationService
dataset = _dataset("dataset-1", "workspace-1")
@@ -1612,19 +1614,22 @@ def test_legacy_segment_mutation_rejects_mismatched_rows_before_dispatch(owner_f
segment = _segment("segment-1", "workspace-1", "dataset-1", "document-1")
setattr(segment, owner_field, "foreign-owner")
mutations = create_autospec(SegmentMutationService, instance=True, spec_set=True)
- session = create_autospec(Session, instance=True, spec_set=True)
+ commits: list[bool] = []
+ event.listen(unbound_session, "after_commit", lambda _session: commits.append(True))
with pytest.raises(ValueError, match="Segment does not belong"):
SegmentService.update_segment(
SegmentUpdateArgs(content="new"),
segment,
document,
dataset,
- session,
+ unbound_session,
actor_id="author",
mutations=mutations,
)
mutations.update_segment.assert_not_called()
- session.commit.assert_not_called()
+ assert not commits
+ assert not unbound_session.new
+ assert not unbound_session.deleted
def _persist_index_batch(