def run(self) -> Optional[ExplorePermalinkValue]: self.validate() try: key = decode_permalink_id(self.key, salt=self.salt) value: Optional[ExplorePermalinkValue] = GetKeyValueCommand( resource=self.resource, key=key, ).run() if value: chart_id: Optional[int] = value.get("chartId") # keep this backward compatible for old permalinks datasource_id: int = ( value.get("datasourceId") or value.get("datasetId") or 0 ) datasource_type = DatasourceType( value.get("datasourceType", DatasourceType.TABLE) ) check_chart_access(datasource_id, chart_id, datasource_type) return value return None except ( DatasetNotFoundError, KeyValueGetFailedError, KeyValueParseKeyError, ) as ex: raise ExplorePermalinkGetFailedError(message=ex.message) from ex except SQLAlchemyError as ex: logger.exception("Error running get command") raise ExplorePermalinkGetFailedError() from ex
def run(self) -> str: self.validate() try: d_id, d_type = self.datasource.split("__") datasource_id = int(d_id) datasource_type = DatasourceType(d_type) check_chart_access(datasource_id, self.chart_id, datasource_type) value = { "chartId": self.chart_id, "datasourceId": datasource_id, "datasourceType": datasource_type, "datasource": self.datasource, "state": self.state, } command = CreateKeyValueCommand( resource=self.resource, value=value, ) key = command.run() if key.id is None: raise ExplorePermalinkCreateFailedError( "Unexpected missing key id") return encode_permalink_key(key=key.id, salt=self.salt) except SQLAlchemyError as ex: logger.exception("Error running create command") raise ExplorePermalinkCreateFailedError() from ex
def test_unsaved_chart_no_dataset_id(app_context: AppContext) -> None: from superset.explore.utils import check_access as check_chart_access with raises(DatasourceNotFoundValidationError): check_chart_access( datasource_id=0, chart_id=0, actor=User(), datasource_type=DatasourceType.TABLE, )
def test_unsaved_chart_no_dataset_id() -> None: from superset.explore.utils import check_access as check_chart_access with raises(DatasourceNotFoundValidationError): with override_user(User()): check_chart_access( datasource_id=0, chart_id=0, datasource_type=DatasourceType.TABLE, )
def test_unsaved_chart_unknown_query_id(mocker: MockFixture, app_context: AppContext) -> None: from superset.explore.utils import check_access as check_chart_access with raises(QueryNotFoundValidationError): mocker.patch(query_find_by_id, return_value=None) check_chart_access( datasource_id=1, chart_id=0, actor=User(), datasource_type=DatasourceType.QUERY, )
def test_unsaved_chart_unknown_dataset_id(mocker: MockFixture) -> None: from superset.explore.utils import check_access as check_chart_access with raises(DatasetNotFoundError): mocker.patch(dataset_find_by_id, return_value=None) with override_user(User()): check_chart_access( datasource_id=1, chart_id=0, datasource_type=DatasourceType.TABLE, )
def test_unsaved_chart_authorized_dataset(mocker: MockFixture, app_context: AppContext) -> None: from superset.connectors.sqla.models import SqlaTable from superset.explore.utils import check_access as check_chart_access mocker.patch(dataset_find_by_id, return_value=SqlaTable()) mocker.patch(can_access_datasource, return_value=True) check_chart_access( datasource_id=1, chart_id=0, actor=User(), datasource_type=DatasourceType.TABLE, )
def test_unsaved_chart_unauthorized_dataset(mocker: MockFixture) -> None: from superset.connectors.sqla.models import SqlaTable from superset.explore.utils import check_access as check_chart_access with raises(DatasetAccessDeniedError): mocker.patch(dataset_find_by_id, return_value=SqlaTable()) mocker.patch(can_access_datasource, return_value=False) with override_user(User()): check_chart_access( datasource_id=1, chart_id=0, datasource_type=DatasourceType.TABLE, )
def test_saved_chart_unknown_chart_id(mocker: MockFixture, app_context: AppContext) -> None: from superset.connectors.sqla.models import SqlaTable from superset.explore.utils import check_access as check_chart_access with raises(ChartNotFoundError): mocker.patch(dataset_find_by_id, return_value=SqlaTable()) mocker.patch(can_access_datasource, return_value=True) mocker.patch(chart_find_by_id, return_value=None) check_chart_access( datasource_id=1, chart_id=1, actor=User(), datasource_type=DatasourceType.TABLE, )
def test_saved_chart_is_admin(mocker: MockFixture, app_context: AppContext) -> None: from superset.connectors.sqla.models import SqlaTable from superset.explore.utils import check_access as check_chart_access from superset.models.slice import Slice mocker.patch(dataset_find_by_id, return_value=SqlaTable()) mocker.patch(can_access_datasource, return_value=True) mocker.patch(is_user_admin, return_value=True) mocker.patch(chart_find_by_id, return_value=Slice()) check_chart_access( datasource_id=1, chart_id=1, actor=User(), datasource_type=DatasourceType.TABLE, )
def test_saved_chart_is_owner(mocker: MockFixture) -> None: from superset.connectors.sqla.models import SqlaTable from superset.explore.utils import check_access as check_chart_access from superset.models.slice import Slice mocker.patch(dataset_find_by_id, return_value=SqlaTable()) mocker.patch(can_access_datasource, return_value=True) mocker.patch(is_admin, return_value=False) mocker.patch(is_owner, return_value=True) mocker.patch(chart_find_by_id, return_value=Slice()) with override_user(User()): check_chart_access( datasource_id=1, chart_id=1, datasource_type=DatasourceType.TABLE, )