diff --git a/api/models/account.py b/api/models/account.py index f2bfd25e93d..c30fd805c06 100644 --- a/api/models/account.py +++ b/api/models/account.py @@ -14,7 +14,6 @@ from typing_extensions import deprecated from configs import dify_config from .base import TypeBase -from .engine import db from .types import EnumText, LongText, StringUUID @@ -131,18 +130,6 @@ class Account(UserMixin, TypeBase): def current_tenant(self): return self._current_tenant - @current_tenant.setter - def current_tenant(self, tenant: "Tenant") -> None: - with Session(db.engine, expire_on_commit=False) as session: - # TODO: A workaround to reload the tenant with `expire_on_commit=False`, allowing - # access to it after the session has been closed. - # This prevents `DetachedInstanceError` when accessing the tenant outside - # the session's lifecycle. - # (The `tenant` argument is typically loaded by `db.session` without the - # `expire_on_commit=False` flag, meaning its lifetime is tied to the web - # request's lifecycle.) - self.set_current_tenant_with_session(tenant, session=session) - def set_current_tenant_with_session(self, tenant: "Tenant", *, session: Session) -> None: """Set the current tenant and role using the caller-owned session.""" tenant_join_query = select(TenantAccountJoin).where( @@ -162,10 +149,6 @@ class Account(UserMixin, TypeBase): def current_tenant_id(self) -> str | None: return self._current_tenant.id if self._current_tenant else None - def set_tenant_id(self, tenant_id: str) -> None: - with Session(db.engine, expire_on_commit=False) as session: - self.set_tenant_id_with_session(tenant_id, session=session) - def set_tenant_id_with_session(self, tenant_id: str, *, session: Session) -> None: """Set the current tenant by id using the caller-owned session.""" query = select(Tenant, TenantAccountJoin).where( diff --git a/api/providers/trace/trace-tencent/tests/unit_tests/tencent_trace/test_tencent_trace.py b/api/providers/trace/trace-tencent/tests/unit_tests/tencent_trace/test_tencent_trace.py index feea2560ec5..369176efc42 100644 --- a/api/providers/trace/trace-tencent/tests/unit_tests/tencent_trace/test_tencent_trace.py +++ b/api/providers/trace/trace-tencent/tests/unit_tests/tencent_trace/test_tencent_trace.py @@ -461,7 +461,6 @@ class TestTencentDataTrace: trace_info.workflow_run_id = "run-1" database = SimpleNamespace(engine=sqlite3_session.get_bind()) monkeypatch.setattr("dify_trace_tencent.tencent_trace.db", database) - monkeypatch.setattr("models.account.db", database) with patch("dify_trace_tencent.tencent_trace.SQLAlchemyWorkflowNodeExecutionRepository") as mock_repo: mock_repo.return_value.get_by_workflow_execution.return_value = [] diff --git a/api/tests/integration_tests/controllers/console/app/test_chat_message_permissions.py b/api/tests/integration_tests/controllers/console/app/test_chat_message_permissions.py index c6905455abb..1efaa2705cd 100644 --- a/api/tests/integration_tests/controllers/console/app/test_chat_message_permissions.py +++ b/api/tests/integration_tests/controllers/console/app/test_chat_message_permissions.py @@ -57,11 +57,7 @@ class TestChatMessageApiPermissions: mock_scalars_result.one.return_value = tenant monkeypatch.setattr(mock_session_instance, "scalars", mock.Mock(return_value=mock_scalars_result)) - mock_session_context = mock.Mock() - mock_session_context.__enter__.return_value = mock_session_instance - monkeypatch.setattr("models.account.Session", lambda _, expire_on_commit: mock_session_context) - - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=mock_session_instance) return account @pytest.mark.parametrize( diff --git a/api/tests/integration_tests/controllers/console/app/test_feedback_export_api.py b/api/tests/integration_tests/controllers/console/app/test_feedback_export_api.py index f30abb4ed05..b382814e3c0 100644 --- a/api/tests/integration_tests/controllers/console/app/test_feedback_export_api.py +++ b/api/tests/integration_tests/controllers/console/app/test_feedback_export_api.py @@ -58,11 +58,7 @@ class TestFeedbackExportApi: mock_scalars_result.one.return_value = tenant monkeypatch.setattr(mock_session_instance, "scalars", mock.Mock(return_value=mock_scalars_result)) - mock_session_context = mock.Mock() - mock_session_context.__enter__.return_value = mock_session_instance - monkeypatch.setattr("models.account.Session", lambda _, expire_on_commit: mock_session_context) - - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=mock_session_instance) return account @pytest.fixture diff --git a/api/tests/integration_tests/controllers/console/app/test_model_config_permissions.py b/api/tests/integration_tests/controllers/console/app/test_model_config_permissions.py index 1237abc387b..8de1026374d 100644 --- a/api/tests/integration_tests/controllers/console/app/test_model_config_permissions.py +++ b/api/tests/integration_tests/controllers/console/app/test_model_config_permissions.py @@ -53,11 +53,7 @@ class TestModelConfigResourcePermissions: mock_scalars_result.one.return_value = tenant monkeypatch.setattr(mock_session_instance, "scalars", mock.Mock(return_value=mock_scalars_result)) - mock_session_context = mock.Mock() - mock_session_context.__enter__.return_value = mock_session_instance - monkeypatch.setattr("models.account.Session", lambda _, expire_on_commit: mock_session_context) - - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=mock_session_instance) return account @pytest.mark.parametrize( diff --git a/api/tests/test_containers_integration_tests/controllers/console/app/test_chat_conversation_status_count_api.py b/api/tests/test_containers_integration_tests/controllers/console/app/test_chat_conversation_status_count_api.py index 0efd77934e9..3e1477dacc9 100644 --- a/api/tests/test_containers_integration_tests/controllers/console/app/test_chat_conversation_status_count_api.py +++ b/api/tests/test_containers_integration_tests/controllers/console/app/test_chat_conversation_status_count_api.py @@ -43,7 +43,7 @@ def _create_account_and_tenant(db_session: Session) -> tuple[Account, Tenant]: db_session.add(join) db_session.commit() - account.set_tenant_id(tenant.id) + account.set_tenant_id_with_session(tenant.id, session=db_session) account.timezone = "UTC" db_session.commit() diff --git a/api/tests/test_containers_integration_tests/controllers/console/helpers.py b/api/tests/test_containers_integration_tests/controllers/console/helpers.py index 2b30a70a384..7854de54fac 100644 --- a/api/tests/test_containers_integration_tests/controllers/console/helpers.py +++ b/api/tests/test_containers_integration_tests/controllers/console/helpers.py @@ -53,7 +53,7 @@ def create_console_account_and_tenant(db_session: Session) -> tuple[Account, Ten ) db_session.commit() - account.set_tenant_id(tenant.id) + account.set_tenant_id_with_session(tenant.id, session=db_session) account.timezone = "UTC" db_session.commit() diff --git a/api/tests/test_containers_integration_tests/controllers/console/workspace/test_members.py b/api/tests/test_containers_integration_tests/controllers/console/workspace/test_members.py index 33bbdcb2f69..6829fc4e567 100644 --- a/api/tests/test_containers_integration_tests/controllers/console/workspace/test_members.py +++ b/api/tests/test_containers_integration_tests/controllers/console/workspace/test_members.py @@ -78,7 +78,7 @@ class WorkspaceMembersIntegrationFactory: ) db_session_with_containers.add(join) db_session_with_containers.commit() - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account @staticmethod diff --git a/api/tests/test_containers_integration_tests/core/workflow/test_human_input_resume_node_execution.py b/api/tests/test_containers_integration_tests/core/workflow/test_human_input_resume_node_execution.py index 1b74f4a5851..c5cc7a37cbd 100644 --- a/api/tests/test_containers_integration_tests/core/workflow/test_human_input_resume_node_execution.py +++ b/api/tests/test_containers_integration_tests/core/workflow/test_human_input_resume_node_execution.py @@ -203,7 +203,7 @@ class TestHumanInputResumeNodeExecutionIntegration: db_session_with_containers.add(tenant_join) db_session_with_containers.commit() - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) app = App( tenant_id=tenant.id, diff --git a/api/tests/test_containers_integration_tests/models/test_account.py b/api/tests/test_containers_integration_tests/models/test_account.py index 676bf013bcd..f8c8bb96e33 100644 --- a/api/tests/test_containers_integration_tests/models/test_account.py +++ b/api/tests/test_containers_integration_tests/models/test_account.py @@ -6,8 +6,8 @@ Migrated from unit_tests/models/test_account_models.py, replacing Also absorbs unit_tests/models/test_account.py role helper coverage. Covers: -- Account.current_tenant setter (sets _current_tenant and role from TenantAccountJoin) -- Account.set_tenant_id (resolves tenant + role from real join row) +- Account.set_current_tenant_with_session (sets _current_tenant and role from TenantAccountJoin) +- Account.set_tenant_id_with_session (resolves tenant + role from real join row) - Tenant.get_accounts (returns accounts linked via TenantAccountJoin) """ @@ -103,8 +103,8 @@ class TestTenantAccountRole: assert not TenantAccountRole.is_privileged_role(cast(TenantAccountRole, "")) -class TestAccountCurrentTenantSetter(_DBTrackingTestBase): - """Integration tests for Account.current_tenant property setter.""" +class TestAccountSetCurrentTenant(_DBTrackingTestBase): + """Integration tests for Account.current_tenant and set_current_tenant_with_session.""" def test_current_tenant_property_returns_cached_tenant(self, db_session_with_containers: Session) -> None: """current_tenant getter returns the in-memory _current_tenant without DB access.""" @@ -114,59 +114,61 @@ class TestAccountCurrentTenantSetter(_DBTrackingTestBase): assert account.current_tenant is tenant - def test_current_tenant_setter_sets_tenant_and_role_when_join_exists( + def test_set_current_tenant_with_session_sets_tenant_and_role_when_join_exists( self, db_session_with_containers: Session ) -> None: - """Setting current_tenant loads the join row and assigns role when relationship exists.""" + """set_current_tenant_with_session loads the join row and assigns role when relationship exists.""" tenant = self._create_tenant(db_session_with_containers) account = self._create_account(db_session_with_containers) self._create_join(db_session_with_containers, tenant.id, account.id, TenantAccountRole.OWNER) db_session_with_containers.commit() - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) assert account._current_tenant is not None assert account._current_tenant.id == tenant.id assert account.role == TenantAccountRole.OWNER - def test_current_tenant_setter_sets_none_when_no_join_exists(self, db_session_with_containers: Session) -> None: - """Setting current_tenant results in _current_tenant=None when no join row exists.""" + def test_set_current_tenant_with_session_sets_none_when_no_join_exists( + self, db_session_with_containers: Session + ) -> None: + """set_current_tenant_with_session results in _current_tenant=None when no join row exists.""" tenant = self._create_tenant(db_session_with_containers) account = self._create_account(db_session_with_containers) db_session_with_containers.commit() - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) assert account._current_tenant is None class TestAccountSetTenantId(_DBTrackingTestBase): - """Integration tests for Account.set_tenant_id method.""" + """Integration tests for Account.set_tenant_id_with_session method.""" - def test_set_tenant_id_sets_tenant_and_role_when_relationship_exists( + def test_set_tenant_id_with_session_sets_tenant_and_role_when_relationship_exists( self, db_session_with_containers: Session ) -> None: - """set_tenant_id loads the tenant and assigns role when a join row exists.""" + """set_tenant_id_with_session loads the tenant and assigns role when a join row exists.""" tenant = self._create_tenant(db_session_with_containers) account = self._create_account(db_session_with_containers) self._create_join(db_session_with_containers, tenant.id, account.id, TenantAccountRole.ADMIN) db_session_with_containers.commit() - account.set_tenant_id(tenant.id) + account.set_tenant_id_with_session(tenant.id, session=db_session_with_containers) assert account._current_tenant is not None assert account._current_tenant.id == tenant.id assert account.role == TenantAccountRole.ADMIN - def test_set_tenant_id_does_not_set_tenant_when_no_relationship_exists( + def test_set_tenant_id_with_session_does_not_set_tenant_when_no_relationship_exists( self, db_session_with_containers: Session ) -> None: - """set_tenant_id does nothing when no join row matches the tenant.""" + """set_tenant_id_with_session does nothing when no join row matches the tenant.""" tenant = self._create_tenant(db_session_with_containers) account = self._create_account(db_session_with_containers) db_session_with_containers.commit() - account.set_tenant_id(tenant.id) + account.set_tenant_id_with_session(tenant.id, session=db_session_with_containers) assert account._current_tenant is None diff --git a/api/tests/test_containers_integration_tests/services/dataset_service_update_delete.py b/api/tests/test_containers_integration_tests/services/dataset_service_update_delete.py index 3acf8e5cd8e..2d6545bc6af 100644 --- a/api/tests/test_containers_integration_tests/services/dataset_service_update_delete.py +++ b/api/tests/test_containers_integration_tests/services/dataset_service_update_delete.py @@ -57,7 +57,7 @@ class DatasetUpdateDeleteTestDataFactory: db_session_with_containers.add(join) db_session_with_containers.commit() - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant @staticmethod diff --git a/api/tests/test_containers_integration_tests/services/test_account_service.py b/api/tests/test_containers_integration_tests/services/test_account_service.py index 9bd696f4394..28f8e7fd55b 100644 --- a/api/tests/test_containers_integration_tests/services/test_account_service.py +++ b/api/tests/test_containers_integration_tests/services/test_account_service.py @@ -882,7 +882,7 @@ class TestTenantService: TenantService.create_tenant_member(tenant2, account, db_session_with_containers, role="admin") # Set initial current tenant - account.current_tenant = tenant1 + account.set_current_tenant_with_session(tenant1, session=db_session_with_containers) db_session_with_containers.commit() @@ -1535,7 +1535,7 @@ class TestTenantService: ) existing_tenant = TenantService.create_tenant(name=existing_tenant_name, session=db_session_with_containers) TenantService.create_tenant_member(existing_tenant, account, db_session_with_containers, role="owner") - account.current_tenant = existing_tenant + account.set_current_tenant_with_session(existing_tenant, session=db_session_with_containers) db_session_with_containers.commit() diff --git a/api/tests/test_containers_integration_tests/services/test_dataset_permission_service.py b/api/tests/test_containers_integration_tests/services/test_dataset_permission_service.py index a20367a9b4f..3f7db13c41e 100644 --- a/api/tests/test_containers_integration_tests/services/test_dataset_permission_service.py +++ b/api/tests/test_containers_integration_tests/services/test_dataset_permission_service.py @@ -55,7 +55,7 @@ class DatasetPermissionTestDataFactory: db.session.add(join) db.session.commit() - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db.session) return account, tenant @staticmethod diff --git a/api/tests/test_containers_integration_tests/services/test_dataset_service_delete_dataset.py b/api/tests/test_containers_integration_tests/services/test_dataset_service_delete_dataset.py index 7881cc00733..b3ee73721d4 100644 --- a/api/tests/test_containers_integration_tests/services/test_dataset_service_delete_dataset.py +++ b/api/tests/test_containers_integration_tests/services/test_dataset_service_delete_dataset.py @@ -43,7 +43,7 @@ class DatasetDeleteIntegrationDataFactory: db_session_with_containers.add(join) db_session_with_containers.commit() - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant @staticmethod diff --git a/api/tests/test_containers_integration_tests/services/test_dataset_service_get_segments.py b/api/tests/test_containers_integration_tests/services/test_dataset_service_get_segments.py index b4fe36d07c4..84b9bcd7c5d 100644 --- a/api/tests/test_containers_integration_tests/services/test_dataset_service_get_segments.py +++ b/api/tests/test_containers_integration_tests/services/test_dataset_service_get_segments.py @@ -54,7 +54,7 @@ class SegmentServiceTestDataFactory: db_session_with_containers.add(join) db_session_with_containers.commit() - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant @staticmethod diff --git a/api/tests/test_containers_integration_tests/services/test_dataset_service_retrieval.py b/api/tests/test_containers_integration_tests/services/test_dataset_service_retrieval.py index ed8e11e52c4..5643bfbeba7 100644 --- a/api/tests/test_containers_integration_tests/services/test_dataset_service_retrieval.py +++ b/api/tests/test_containers_integration_tests/services/test_dataset_service_retrieval.py @@ -62,7 +62,7 @@ class DatasetRetrievalTestDataFactory: db_session_with_containers.add(join) db_session_with_containers.commit() - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant @staticmethod @@ -88,7 +88,7 @@ class DatasetRetrievalTestDataFactory: db_session_with_containers.add(join) db_session_with_containers.commit() - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account @staticmethod diff --git a/api/tests/test_containers_integration_tests/services/test_dataset_service_update_dataset.py b/api/tests/test_containers_integration_tests/services/test_dataset_service_update_dataset.py index d9fb23e8e33..8f4099fa484 100644 --- a/api/tests/test_containers_integration_tests/services/test_dataset_service_update_dataset.py +++ b/api/tests/test_containers_integration_tests/services/test_dataset_service_update_dataset.py @@ -51,7 +51,7 @@ class DatasetUpdateTestDataFactory: db_session_with_containers.add(join) db_session_with_containers.commit() - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant @staticmethod diff --git a/api/tests/test_containers_integration_tests/services/test_file_service.py b/api/tests/test_containers_integration_tests/services/test_file_service.py index 63d09e2fbaa..dc91e29085f 100644 --- a/api/tests/test_containers_integration_tests/services/test_file_service.py +++ b/api/tests/test_containers_integration_tests/services/test_file_service.py @@ -91,7 +91,7 @@ class TestFileService: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account diff --git a/api/tests/test_containers_integration_tests/services/test_metadata_service.py b/api/tests/test_containers_integration_tests/services/test_metadata_service.py index d0b8a71cd56..6f7bf625563 100644 --- a/api/tests/test_containers_integration_tests/services/test_metadata_service.py +++ b/api/tests/test_containers_integration_tests/services/test_metadata_service.py @@ -93,7 +93,7 @@ class TestMetadataService: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant diff --git a/api/tests/test_containers_integration_tests/services/test_model_load_balancing_service.py b/api/tests/test_containers_integration_tests/services/test_model_load_balancing_service.py index 71d2c1c6800..c435af52a06 100644 --- a/api/tests/test_containers_integration_tests/services/test_model_load_balancing_service.py +++ b/api/tests/test_containers_integration_tests/services/test_model_load_balancing_service.py @@ -106,7 +106,7 @@ class TestModelLoadBalancingService: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant diff --git a/api/tests/test_containers_integration_tests/services/test_model_provider_service.py b/api/tests/test_containers_integration_tests/services/test_model_provider_service.py index 8955a3b5f23..f250488bbd7 100644 --- a/api/tests/test_containers_integration_tests/services/test_model_provider_service.py +++ b/api/tests/test_containers_integration_tests/services/test_model_provider_service.py @@ -77,7 +77,7 @@ class TestModelProviderService: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant diff --git a/api/tests/test_containers_integration_tests/services/test_schedule_service.py b/api/tests/test_containers_integration_tests/services/test_schedule_service.py index 357bb12d4ef..3eb55e5fe2a 100644 --- a/api/tests/test_containers_integration_tests/services/test_schedule_service.py +++ b/api/tests/test_containers_integration_tests/services/test_schedule_service.py @@ -42,7 +42,7 @@ class ScheduleServiceIntegrationFactory: db_session_with_containers.add(join) db_session_with_containers.commit() - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant @staticmethod diff --git a/api/tests/test_containers_integration_tests/services/test_webapp_auth_service.py b/api/tests/test_containers_integration_tests/services/test_webapp_auth_service.py index 6242a574226..38a8dc7e81b 100644 --- a/api/tests/test_containers_integration_tests/services/test_webapp_auth_service.py +++ b/api/tests/test_containers_integration_tests/services/test_webapp_auth_service.py @@ -83,7 +83,7 @@ class TestWebAppAuthService: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant @@ -146,7 +146,7 @@ class TestWebAppAuthService: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant, password diff --git a/api/tests/test_containers_integration_tests/services/test_webhook_service_relationships.py b/api/tests/test_containers_integration_tests/services/test_webhook_service_relationships.py index 22fb7da062e..acb786fc758 100644 --- a/api/tests/test_containers_integration_tests/services/test_webhook_service_relationships.py +++ b/api/tests/test_containers_integration_tests/services/test_webhook_service_relationships.py @@ -77,7 +77,7 @@ class WebhookServiceRelationshipFactory: db_session_with_containers.add(join) db_session_with_containers.commit() - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant @staticmethod diff --git a/api/tests/test_containers_integration_tests/services/test_workflow_service.py b/api/tests/test_containers_integration_tests/services/test_workflow_service.py index c9d8ee51c72..d8ce43c65e2 100644 --- a/api/tests/test_containers_integration_tests/services/test_workflow_service.py +++ b/api/tests/test_containers_integration_tests/services/test_workflow_service.py @@ -76,7 +76,7 @@ class TestWorkflowService: db_session_with_containers.commit() # Set the current tenant for the account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account diff --git a/api/tests/test_containers_integration_tests/services/test_workspace_service.py b/api/tests/test_containers_integration_tests/services/test_workspace_service.py index e8a14c68acf..343665b856b 100644 --- a/api/tests/test_containers_integration_tests/services/test_workspace_service.py +++ b/api/tests/test_containers_integration_tests/services/test_workspace_service.py @@ -83,7 +83,7 @@ class TestWorkspaceService: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant diff --git a/api/tests/test_containers_integration_tests/services/tools/test_api_tools_manage_service.py b/api/tests/test_containers_integration_tests/services/tools/test_api_tools_manage_service.py index 28ae8e5935e..5039875d62e 100644 --- a/api/tests/test_containers_integration_tests/services/tools/test_api_tools_manage_service.py +++ b/api/tests/test_containers_integration_tests/services/tools/test_api_tools_manage_service.py @@ -89,7 +89,7 @@ class TestApiToolManageService: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant diff --git a/api/tests/test_containers_integration_tests/services/tools/test_mcp_tools_manage_service.py b/api/tests/test_containers_integration_tests/services/tools/test_mcp_tools_manage_service.py index ca44c5b72d5..ba2ae62a053 100644 --- a/api/tests/test_containers_integration_tests/services/tools/test_mcp_tools_manage_service.py +++ b/api/tests/test_containers_integration_tests/services/tools/test_mcp_tools_manage_service.py @@ -87,7 +87,7 @@ class TestMCPToolManageService: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant diff --git a/api/tests/test_containers_integration_tests/services/workflow/test_workflow_converter.py b/api/tests/test_containers_integration_tests/services/workflow/test_workflow_converter.py index fa102683cdf..dfddf26ddf6 100644 --- a/api/tests/test_containers_integration_tests/services/workflow/test_workflow_converter.py +++ b/api/tests/test_containers_integration_tests/services/workflow/test_workflow_converter.py @@ -131,7 +131,7 @@ class TestWorkflowConverter: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant diff --git a/api/tests/test_containers_integration_tests/tasks/test_batch_clean_document_task.py b/api/tests/test_containers_integration_tests/tasks/test_batch_clean_document_task.py index 770a145c6cb..adac71a2edf 100644 --- a/api/tests/test_containers_integration_tests/tasks/test_batch_clean_document_task.py +++ b/api/tests/test_containers_integration_tests/tasks/test_batch_clean_document_task.py @@ -95,7 +95,7 @@ class TestBatchCleanDocumentTask: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account diff --git a/api/tests/test_containers_integration_tests/tasks/test_batch_create_segment_to_index_task.py b/api/tests/test_containers_integration_tests/tasks/test_batch_create_segment_to_index_task.py index 580dd88382c..b0f57dba084 100644 --- a/api/tests/test_containers_integration_tests/tasks/test_batch_create_segment_to_index_task.py +++ b/api/tests/test_containers_integration_tests/tasks/test_batch_create_segment_to_index_task.py @@ -123,7 +123,7 @@ class TestBatchCreateSegmentToIndexTask: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant diff --git a/api/tests/test_containers_integration_tests/tasks/test_create_segment_to_index_task.py b/api/tests/test_containers_integration_tests/tasks/test_create_segment_to_index_task.py index 0410fb5ebeb..40bffa766b1 100644 --- a/api/tests/test_containers_integration_tests/tasks/test_create_segment_to_index_task.py +++ b/api/tests/test_containers_integration_tests/tasks/test_create_segment_to_index_task.py @@ -99,7 +99,7 @@ class TestCreateSegmentToIndexTask: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant diff --git a/api/tests/test_containers_integration_tests/tasks/test_disable_segment_from_index_task.py b/api/tests/test_containers_integration_tests/tasks/test_disable_segment_from_index_task.py index a5577217e81..1d8fcc6b5d7 100644 --- a/api/tests/test_containers_integration_tests/tasks/test_disable_segment_from_index_task.py +++ b/api/tests/test_containers_integration_tests/tasks/test_disable_segment_from_index_task.py @@ -78,7 +78,7 @@ class TestDisableSegmentFromIndexTask: db_session_with_containers.commit() # Set current tenant for account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account, tenant diff --git a/api/tests/test_containers_integration_tests/tasks/test_disable_segments_from_index_task.py b/api/tests/test_containers_integration_tests/tasks/test_disable_segments_from_index_task.py index 19611bd6ff2..a218336deef 100644 --- a/api/tests/test_containers_integration_tests/tasks/test_disable_segments_from_index_task.py +++ b/api/tests/test_containers_integration_tests/tasks/test_disable_segments_from_index_task.py @@ -81,7 +81,7 @@ class TestDisableSegmentsFromIndexTask: db_session_with_containers.commit() # Set the current tenant for the account - account.current_tenant = tenant + account.set_current_tenant_with_session(tenant, session=db_session_with_containers) return account diff --git a/api/tests/unit_tests/core/ops/test_base_trace_instance.py b/api/tests/unit_tests/core/ops/test_base_trace_instance.py index 8f4ad4e0bef..f0f99cae104 100644 --- a/api/tests/unit_tests/core/ops/test_base_trace_instance.py +++ b/api/tests/unit_tests/core/ops/test_base_trace_instance.py @@ -25,10 +25,9 @@ class ConcreteTraceInstance(BaseTraceInstance): @pytest.fixture(autouse=True) def _bind_production_sessions(monkeypatch: pytest.MonkeyPatch, sqlite_engine: Engine) -> None: - """Bind both service-owned ORM sessions to the test's SQLite engine.""" + """Bind the service-owned ORM session to the test's SQLite engine.""" database = SimpleNamespace(engine=sqlite_engine) monkeypatch.setattr("core.ops.base_trace_instance.db", database) - monkeypatch.setattr("models.account.db", database) def _persist_app(session: Session, *, created_by: str | None) -> App: diff --git a/api/tests/unit_tests/core/tools/workflow_as_tool/test_tool.py b/api/tests/unit_tests/core/tools/workflow_as_tool/test_tool.py index c9f6dcca0f2..b64eea358d8 100644 --- a/api/tests/unit_tests/core/tools/workflow_as_tool/test_tool.py +++ b/api/tests/unit_tests/core/tools/workflow_as_tool/test_tool.py @@ -52,15 +52,11 @@ def sqlite_tool_db( monkeypatch: pytest.MonkeyPatch, sqlite_engine: Engine, ) -> Iterator[SqliteToolDb]: - """Bind service-owned sessions and Account tenant reloads to SQLite.""" + """Bind service-owned sessions to SQLite.""" models = (App, Workflow, EndUser, Account, Tenant, TenantAccountJoin) TypeBase.metadata.create_all(sqlite_engine, tables=[model.__table__ for model in models]) session_maker = sessionmaker(bind=sqlite_engine, expire_on_commit=False) monkeypatch.setattr(workflow_tool_module.session_factory, "create_session", session_maker) - - from models import account as account_module - - monkeypatch.setattr(account_module, "db", SimpleNamespace(engine=sqlite_engine)) with session_maker() as caller_session: yield SqliteToolDb(engine=sqlite_engine, session_maker=session_maker, caller_session=caller_session) diff --git a/api/tests/unit_tests/models/test_account_models.py b/api/tests/unit_tests/models/test_account_models.py index bf71119f0e8..14677e13c71 100644 --- a/api/tests/unit_tests/models/test_account_models.py +++ b/api/tests/unit_tests/models/test_account_models.py @@ -335,6 +335,14 @@ class TestTenantRelationshipIntegrity: # Assert assert tenant_id_none is None + def test_current_tenant_is_read_only(self): + """current_tenant has no setter, so tenant switching cannot open a hidden session.""" + account = Account(name="Test User", email="test@example.com") + tenant = Tenant(name="Test Tenant") + + with pytest.raises(AttributeError): + account.current_tenant = tenant # type: ignore[misc] + @pytest.mark.parametrize("sqlite_session", [(Account, Tenant, TenantAccountJoin)], indirect=True) def test_set_current_tenant_with_session_uses_caller_session(self, sqlite_session: Session): account = Account(name="Test User", email="test@example.com")