Beispiel #1
0
 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
Beispiel #2
0
def test_post(client, form_data: Dict[str, Any], permalink_salt: str):
    login(client, "admin")
    resp = client.post(f"api/v1/explore/permalink", json={"formData": form_data})
    assert resp.status_code == 201
    data = json.loads(resp.data.decode("utf-8"))
    key = data["key"]
    url = data["url"]
    assert key in url
    id_ = decode_permalink_id(key, permalink_salt)
    db.session.query(KeyValueEntry).filter_by(id=id_).delete()
    db.session.commit()
Beispiel #3
0
def test_get(test_client, login_as_admin, dashboard_id: int,
             permalink_salt: str):
    key = test_client.post(f"api/v1/dashboard/{dashboard_id}/permalink",
                           json=STATE).json["key"]
    resp = test_client.get(f"api/v1/dashboard/permalink/{key}")
    assert resp.status_code == 200
    result = resp.json
    assert result["dashboardId"] == str(dashboard_id)
    assert result["state"] == STATE
    id_ = decode_permalink_id(key, permalink_salt)
    db.session.query(KeyValueEntry).filter_by(id=id_).delete()
    db.session.commit()
Beispiel #4
0
def test_post(client, dashboard_id: int, permalink_salt: str) -> None:
    login(client, "admin")
    resp = client.post(f"api/v1/dashboard/{dashboard_id}/permalink",
                       json=STATE)
    assert resp.status_code == 201
    data = json.loads(resp.data.decode("utf-8"))
    key = data["key"]
    url = data["url"]
    assert key in url
    id_ = decode_permalink_id(key, permalink_salt)
    db.session.query(KeyValueEntry).filter_by(id=id_).delete()
    db.session.commit()
Beispiel #5
0
def test_get(client, form_data: Dict[str, Any], permalink_salt: str) -> None:
    login(client, "admin")
    resp = client.post(f"api/v1/explore/permalink", json={"formData": form_data})
    data = json.loads(resp.data.decode("utf-8"))
    key = data["key"]
    resp = client.get(f"api/v1/explore/permalink/{key}")
    assert resp.status_code == 200
    result = json.loads(resp.data.decode("utf-8"))
    assert result["state"]["formData"] == form_data
    id_ = decode_permalink_id(key, permalink_salt)
    db.session.query(KeyValueEntry).filter_by(id=id_).delete()
    db.session.commit()
Beispiel #6
0
def test_get(client, dashboard_id: int, permalink_salt: str):
    login(client, "admin")
    resp = client.post(f"api/v1/dashboard/{dashboard_id}/permalink",
                       json=STATE)
    data = json.loads(resp.data.decode("utf-8"))
    key = data["key"]
    resp = client.get(f"api/v1/dashboard/permalink/{key}")
    assert resp.status_code == 200
    result = json.loads(resp.data.decode("utf-8"))
    assert result["dashboardId"] == str(dashboard_id)
    assert result["state"] == STATE
    id_ = decode_permalink_id(key, permalink_salt)
    db.session.query(KeyValueEntry).filter_by(id=id_).delete()
    db.session.commit()
Beispiel #7
0
def test_post(test_client, login_as_admin, dashboard_id: int,
              permalink_salt: str) -> None:
    resp = test_client.post(f"api/v1/dashboard/{dashboard_id}/permalink",
                            json=STATE)
    assert resp.status_code == 201
    data = resp.json
    key = data["key"]
    url = data["url"]
    assert key in url
    id_ = decode_permalink_id(key, permalink_salt)

    assert (
        data == test_client.post(f"api/v1/dashboard/{dashboard_id}/permalink",
                                 json=STATE).json
    ), "Should always return the same permalink key for the same payload"

    db.session.query(KeyValueEntry).filter_by(id=id_).delete()
    db.session.commit()
Beispiel #8
0
 def run(self) -> Optional[DashboardPermalinkValue]:
     self.validate()
     try:
         key = decode_permalink_id(self.key, salt=self.salt)
         command = GetKeyValueCommand(resource=self.resource, key=key)
         value: Optional[DashboardPermalinkValue] = command.run()
         if value:
             DashboardDAO.get_by_id_or_slug(value["dashboardId"])
             return value
         return None
     except (
             DashboardNotFoundError,
             KeyValueGetFailedError,
             KeyValueParseKeyError,
     ) as ex:
         raise DashboardPermalinkGetFailedError(message=ex.message) from ex
     except SQLAlchemyError as ex:
         logger.exception("Error running get command")
         raise DashboardPermalinkGetFailedError() from ex
Beispiel #9
0
def test_decode_permalink_id_invalid() -> None:
    from superset.key_value.utils import decode_permalink_id

    with pytest.raises(KeyValueParseKeyError):
        decode_permalink_id("foo", "bar")