This commit is contained in:
samzong 2026-03-24 19:28:41 +09:00 committed by GitHub
commit bf1820259d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View File

@ -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.")

View File

@ -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)