|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +from unittest.mock import patch, PropertyMock |
| 18 | + |
| 19 | +import pytest |
| 20 | +from sqlalchemy.exc import SQLAlchemyError |
| 21 | + |
| 22 | +from superset.commands.dashboard.permalink.get import GetDashboardPermalinkCommand |
| 23 | +from superset.dashboards.permalink.exceptions import ( |
| 24 | + DashboardPermalinkGetFailedError, |
| 25 | + DashboardPermalinkInvalidKeyError, |
| 26 | +) |
| 27 | + |
| 28 | +MODULE = "superset.commands.dashboard.permalink.get" |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture(autouse=True) |
| 32 | +def salt(): |
| 33 | + with patch( |
| 34 | + f"{MODULE}.GetDashboardPermalinkCommand.salt", |
| 35 | + new_callable=PropertyMock, |
| 36 | + return_value="test-salt", |
| 37 | + ): |
| 38 | + yield |
| 39 | + |
| 40 | + |
| 41 | +def test_invalid_key_raises_invalid_key_error() -> None: |
| 42 | + with pytest.raises(DashboardPermalinkInvalidKeyError) as excinfo: |
| 43 | + GetDashboardPermalinkCommand("undefined").run() |
| 44 | + assert excinfo.value.status == 404 |
| 45 | + |
| 46 | + |
| 47 | +def test_storage_failure_still_raises_generic_error() -> None: |
| 48 | + with ( |
| 49 | + patch(f"{MODULE}.decode_permalink_id", return_value=1), |
| 50 | + patch(f"{MODULE}.KeyValueDAO.get_value", side_effect=SQLAlchemyError()), |
| 51 | + pytest.raises(DashboardPermalinkGetFailedError) as excinfo, |
| 52 | + ): |
| 53 | + GetDashboardPermalinkCommand("abc").run() |
| 54 | + assert not isinstance(excinfo.value, DashboardPermalinkInvalidKeyError) |
| 55 | + assert excinfo.value.status == 500 |
0 commit comments