Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions superset/commands/dashboard/permalink/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
from superset.commands.dashboard.permalink.base import BaseDashboardPermalinkCommand
from superset.daos.dashboard import DashboardDAO
from superset.daos.key_value import KeyValueDAO
from superset.dashboards.permalink.exceptions import DashboardPermalinkGetFailedError
from superset.dashboards.permalink.exceptions import (
DashboardPermalinkGetFailedError,
DashboardPermalinkInvalidKeyError,
)
from superset.dashboards.permalink.types import DashboardPermalinkValue
from superset.key_value.exceptions import (
KeyValueCodecDecodeException,
Expand All @@ -48,11 +51,12 @@ def run(self) -> Optional[DashboardPermalinkValue]:
DashboardDAO.get_by_id_or_slug(value["dashboardId"])
return value
return None
except KeyValueParseKeyError as ex:
raise DashboardPermalinkInvalidKeyError() from ex
except (
DashboardNotFoundError,
KeyValueCodecDecodeException,
KeyValueGetFailedError,
KeyValueParseKeyError,
) as ex:
raise DashboardPermalinkGetFailedError(message=ex.message) from ex
except SQLAlchemyError as ex:
Expand Down
7 changes: 5 additions & 2 deletions superset/dashboards/permalink/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
from superset.commands.dashboard.permalink.create import CreateDashboardPermalinkCommand
from superset.commands.dashboard.permalink.get import GetDashboardPermalinkCommand
from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP
from superset.dashboards.permalink.exceptions import DashboardPermalinkInvalidStateError
from superset.dashboards.permalink.exceptions import (
DashboardPermalinkInvalidKeyError,
DashboardPermalinkInvalidStateError,
)
from superset.dashboards.permalink.schemas import DashboardPermalinkStateSchema
from superset.extensions import event_logger
from superset.key_value.exceptions import KeyValueAccessDeniedError
Expand Down Expand Up @@ -225,5 +228,5 @@ def get(self, key: str) -> Response:
return self.response(200, **value)
except DashboardAccessDeniedError as ex:
return self.response(403, message=str(ex))
except DashboardNotFoundError as ex:
except (DashboardNotFoundError, DashboardPermalinkInvalidKeyError) as ex:
return self.response(404, message=str(ex))
5 changes: 5 additions & 0 deletions superset/dashboards/permalink/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ class DashboardPermalinkCreateFailedError(CreateFailedError):

class DashboardPermalinkGetFailedError(CommandException):
message = _("An error occurred while accessing the value.")


class DashboardPermalinkInvalidKeyError(DashboardPermalinkGetFailedError):
status = 404
message = _("Invalid permalink key")
6 changes: 6 additions & 0 deletions tests/integration_tests/dashboards/permalink/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,9 @@ def test_get(dashboard_id: int, permalink_salt: str, test_client, login_as_admin
id_ = decode_permalink_id(key, permalink_salt)
db.session.query(KeyValueEntry).filter_by(id=id_).delete()
db.session.commit()


def test_get_invalid_key(test_client, login_as_admin):
resp = test_client.get("api/v1/dashboard/permalink/undefined")
assert resp.status_code == 404
assert resp.json["message"] == "Invalid permalink key"
55 changes: 55 additions & 0 deletions tests/unit_tests/commands/dashboard/permalink_get_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from unittest.mock import patch, PropertyMock

import pytest
from sqlalchemy.exc import SQLAlchemyError

from superset.commands.dashboard.permalink.get import GetDashboardPermalinkCommand
from superset.dashboards.permalink.exceptions import (
DashboardPermalinkGetFailedError,
DashboardPermalinkInvalidKeyError,
)

MODULE = "superset.commands.dashboard.permalink.get"


@pytest.fixture(autouse=True)
def salt():
with patch(
f"{MODULE}.GetDashboardPermalinkCommand.salt",
new_callable=PropertyMock,
return_value="test-salt",
):
yield


def test_invalid_key_raises_invalid_key_error() -> None:
with pytest.raises(DashboardPermalinkInvalidKeyError) as excinfo:
GetDashboardPermalinkCommand("undefined").run()
assert excinfo.value.status == 404


def test_storage_failure_still_raises_generic_error() -> None:
with (
patch(f"{MODULE}.decode_permalink_id", return_value=1),
patch(f"{MODULE}.KeyValueDAO.get_value", side_effect=SQLAlchemyError()),
pytest.raises(DashboardPermalinkGetFailedError) as excinfo,
):
GetDashboardPermalinkCommand("abc").run()
assert not isinstance(excinfo.value, DashboardPermalinkInvalidKeyError)
assert excinfo.value.status == 500
Loading