mirror of https://github.com/langgenius/dify.git
Merge badd9b33f5 into a813b9f103
This commit is contained in:
commit
bf1820259d
|
|
@ -158,10 +158,11 @@ class DataSourceApi(Resource):
|
|||
@login_required
|
||||
@account_initialization_required
|
||||
def patch(self, binding_id, action: Literal["enable", "disable"]):
|
||||
_, current_tenant_id = current_account_with_tenant()
|
||||
binding_id = str(binding_id)
|
||||
with Session(db.engine) as session:
|
||||
data_source_binding = session.execute(
|
||||
select(DataSourceOauthBinding).filter_by(id=binding_id)
|
||||
select(DataSourceOauthBinding).filter_by(id=binding_id, tenant_id=current_tenant_id)
|
||||
).scalar_one_or_none()
|
||||
if data_source_binding is None:
|
||||
raise NotFound("Data source binding not found.")
|
||||
|
|
|
|||
|
|
@ -143,6 +143,31 @@ class TestDataSourceApi:
|
|||
with pytest.raises(NotFound):
|
||||
method(api, "b1", "enable")
|
||||
|
||||
def test_patch_binding_scoped_to_current_tenant(self, app, patch_tenant, mock_engine):
|
||||
api = DataSourceApi()
|
||||
method = unwrap(api.patch)
|
||||
|
||||
binding = MagicMock(id="b1", disabled=True)
|
||||
|
||||
with (
|
||||
app.test_request_context("/"),
|
||||
patch("controllers.console.datasets.data_source.Session") as mock_session_class,
|
||||
patch("controllers.console.datasets.data_source.db.session.add"),
|
||||
patch("controllers.console.datasets.data_source.db.session.commit"),
|
||||
):
|
||||
mock_session = MagicMock()
|
||||
mock_session_class.return_value.__enter__.return_value = mock_session
|
||||
mock_session.execute.return_value.scalar_one_or_none.return_value = binding
|
||||
|
||||
method(api, "b1", "enable")
|
||||
|
||||
statement = mock_session.execute.call_args.args[0]
|
||||
where_clause = getattr(statement, "whereclause", None)
|
||||
assert where_clause is not None
|
||||
compiled_where = str(where_clause)
|
||||
assert "data_source_oauth_bindings.tenant_id" in compiled_where
|
||||
assert "data_source_oauth_bindings.id" in compiled_where
|
||||
|
||||
def test_patch_enable_already_enabled(self, app, patch_tenant, mock_engine):
|
||||
api = DataSourceApi()
|
||||
method = unwrap(api.patch)
|
||||
|
|
|
|||
Loading…
Reference in New Issue