2020-04-15 04:40:14 -04:00
|
|
|
# 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 superset import db
|
|
|
|
# from superset.models.dashboard import Dashboard
|
2022-12-14 08:02:31 -05:00
|
|
|
|
|
|
|
import json
|
2020-04-15 04:40:14 -04:00
|
|
|
import urllib.request
|
2021-05-14 15:49:25 -04:00
|
|
|
from io import BytesIO
|
2022-12-14 08:02:31 -05:00
|
|
|
from typing import Tuple
|
2020-04-15 04:40:14 -04:00
|
|
|
from unittest import skipUnless
|
2022-12-13 16:31:36 -05:00
|
|
|
from unittest.mock import ANY, call, MagicMock, patch
|
2020-04-15 04:40:14 -04:00
|
|
|
|
2022-12-13 16:31:36 -05:00
|
|
|
import pytest
|
2020-04-15 04:40:14 -04:00
|
|
|
from flask_testing import LiveServerTestCase
|
|
|
|
from sqlalchemy.sql import func
|
|
|
|
|
2021-05-14 15:49:25 -04:00
|
|
|
from superset import db, is_feature_enabled, security_manager
|
2020-08-12 16:28:41 -04:00
|
|
|
from superset.extensions import machine_auth_provider_factory
|
2020-04-15 04:40:14 -04:00
|
|
|
from superset.models.dashboard import Dashboard
|
|
|
|
from superset.models.slice import Slice
|
2022-12-14 08:02:31 -05:00
|
|
|
from superset.tasks.types import ExecutorType
|
2020-08-12 16:28:41 -04:00
|
|
|
from superset.utils.screenshots import ChartScreenshot, DashboardScreenshot
|
2022-12-14 08:02:31 -05:00
|
|
|
from superset.utils.urls import get_url_path
|
2022-12-13 16:31:36 -05:00
|
|
|
from superset.utils.webdriver import find_unexpected_errors, WebDriverProxy
|
2021-07-01 11:03:07 -04:00
|
|
|
from tests.integration_tests.conftest import with_feature_flags
|
2022-12-14 08:02:31 -05:00
|
|
|
from tests.integration_tests.fixtures.birth_names_dashboard import (
|
|
|
|
load_birth_names_dashboard_with_slices,
|
|
|
|
load_birth_names_data,
|
|
|
|
)
|
2021-07-01 11:03:07 -04:00
|
|
|
from tests.integration_tests.test_app import app
|
2020-04-15 04:40:14 -04:00
|
|
|
|
|
|
|
from .base_tests import SupersetTestCase
|
|
|
|
|
2022-12-14 08:02:31 -05:00
|
|
|
CHART_URL = "/api/v1/chart/"
|
|
|
|
DASHBOARD_URL = "/api/v1/dashboard/"
|
|
|
|
|
2020-04-15 04:40:14 -04:00
|
|
|
|
2020-06-29 18:36:06 -04:00
|
|
|
class TestThumbnailsSeleniumLive(LiveServerTestCase):
|
2020-04-15 04:40:14 -04:00
|
|
|
def create_app(self):
|
|
|
|
return app
|
|
|
|
|
|
|
|
def url_open_auth(self, username: str, url: str):
|
|
|
|
admin_user = security_manager.find_user(username=username)
|
2020-08-12 16:28:41 -04:00
|
|
|
cookies = machine_auth_provider_factory.instance.get_auth_cookies(admin_user)
|
2020-04-15 04:40:14 -04:00
|
|
|
opener = urllib.request.build_opener()
|
|
|
|
opener.addheaders.append(("Cookie", f"session={cookies['session']}"))
|
|
|
|
return opener.open(f"{self.get_server_url()}/{url}")
|
|
|
|
|
|
|
|
@skipUnless((is_feature_enabled("THUMBNAILS")), "Thumbnails feature")
|
|
|
|
def test_get_async_dashboard_screenshot(self):
|
|
|
|
"""
|
2021-04-12 16:18:17 -04:00
|
|
|
Thumbnails: Simple get async dashboard screenshot
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
|
|
|
with patch("superset.dashboards.api.DashboardRestApi.get") as mock_get:
|
2022-12-14 08:02:31 -05:00
|
|
|
rv = self.client.get(DASHBOARD_URL)
|
|
|
|
resp = json.loads(rv.data.decode("utf-8"))
|
|
|
|
thumbnail_url = resp["result"][0]["thumbnail_url"]
|
|
|
|
|
2020-04-15 04:40:14 -04:00
|
|
|
response = self.url_open_auth(
|
|
|
|
"admin",
|
2022-12-14 08:02:31 -05:00
|
|
|
thumbnail_url,
|
2020-04-15 04:40:14 -04:00
|
|
|
)
|
|
|
|
self.assertEqual(response.getcode(), 202)
|
|
|
|
|
|
|
|
|
2022-12-13 16:31:36 -05:00
|
|
|
class TestWebDriverScreenshotErrorDetector(SupersetTestCase):
|
|
|
|
@patch("superset.utils.webdriver.WebDriverWait")
|
|
|
|
@patch("superset.utils.webdriver.firefox")
|
|
|
|
@patch("superset.utils.webdriver.find_unexpected_errors")
|
|
|
|
def test_not_call_find_unexpected_errors_if_feature_disabled(
|
|
|
|
self, mock_find_unexpected_errors, mock_firefox, mock_webdriver_wait
|
|
|
|
):
|
|
|
|
webdriver_proxy = WebDriverProxy("firefox")
|
|
|
|
user = security_manager.get_user_by_username(
|
|
|
|
app.config["THUMBNAIL_SELENIUM_USER"]
|
|
|
|
)
|
|
|
|
url = get_url_path("Superset.dashboard", dashboard_id_or_slug=1)
|
|
|
|
webdriver_proxy.get_screenshot(url, "grid-container", user=user)
|
|
|
|
|
|
|
|
assert not mock_find_unexpected_errors.called
|
|
|
|
|
|
|
|
@patch("superset.utils.webdriver.WebDriverWait")
|
|
|
|
@patch("superset.utils.webdriver.firefox")
|
|
|
|
@patch("superset.utils.webdriver.find_unexpected_errors")
|
|
|
|
def test_call_find_unexpected_errors_if_feature_enabled(
|
|
|
|
self, mock_find_unexpected_errors, mock_firefox, mock_webdriver_wait
|
|
|
|
):
|
|
|
|
app.config["SCREENSHOT_REPLACE_UNEXPECTED_ERRORS"] = True
|
|
|
|
webdriver_proxy = WebDriverProxy("firefox")
|
|
|
|
user = security_manager.get_user_by_username(
|
|
|
|
app.config["THUMBNAIL_SELENIUM_USER"]
|
|
|
|
)
|
|
|
|
url = get_url_path("Superset.dashboard", dashboard_id_or_slug=1)
|
|
|
|
webdriver_proxy.get_screenshot(url, "grid-container", user=user)
|
|
|
|
|
|
|
|
assert mock_find_unexpected_errors.called
|
|
|
|
|
|
|
|
app.config["SCREENSHOT_REPLACE_UNEXPECTED_ERRORS"] = False
|
|
|
|
|
|
|
|
def test_find_unexpected_errors_no_alert(self):
|
|
|
|
webdriver = MagicMock()
|
|
|
|
|
|
|
|
webdriver.find_elements.return_value = []
|
|
|
|
|
|
|
|
unexpected_errors = find_unexpected_errors(driver=webdriver)
|
|
|
|
assert len(unexpected_errors) == 0
|
|
|
|
|
|
|
|
assert "alert" in webdriver.find_elements.call_args_list[0][0][1]
|
|
|
|
|
|
|
|
@patch("superset.utils.webdriver.WebDriverWait")
|
|
|
|
def test_find_unexpected_errors(self, mock_webdriver_wait):
|
|
|
|
webdriver = MagicMock()
|
|
|
|
alert_div = MagicMock()
|
|
|
|
|
|
|
|
webdriver.find_elements.return_value = [alert_div]
|
|
|
|
alert_div.find_elements.return_value = MagicMock()
|
|
|
|
|
|
|
|
unexpected_errors = find_unexpected_errors(driver=webdriver)
|
|
|
|
assert len(unexpected_errors) == 1
|
|
|
|
|
|
|
|
# attempt to find alerts
|
|
|
|
assert "alert" in webdriver.find_elements.call_args_list[0][0][1]
|
|
|
|
# attempt to click on "See more" buttons
|
|
|
|
assert "button" in alert_div.find_element.call_args_list[0][0][1]
|
|
|
|
# Wait for error modal to show up and to hide
|
|
|
|
assert 2 == len(mock_webdriver_wait.call_args_list)
|
|
|
|
# replace the text in alert div, eg, "unexpected errors"
|
|
|
|
assert alert_div == webdriver.execute_script.call_args_list[0][0][1]
|
|
|
|
|
|
|
|
|
2021-06-08 10:33:42 -04:00
|
|
|
class TestWebDriverProxy(SupersetTestCase):
|
|
|
|
@patch("superset.utils.webdriver.WebDriverWait")
|
|
|
|
@patch("superset.utils.webdriver.firefox")
|
|
|
|
@patch("superset.utils.webdriver.sleep")
|
|
|
|
def test_screenshot_selenium_headstart(
|
|
|
|
self, mock_sleep, mock_webdriver, mock_webdriver_wait
|
|
|
|
):
|
|
|
|
webdriver = WebDriverProxy("firefox")
|
|
|
|
user = security_manager.get_user_by_username(
|
|
|
|
app.config["THUMBNAIL_SELENIUM_USER"]
|
|
|
|
)
|
|
|
|
url = get_url_path("Superset.slice", slice_id=1, standalone="true")
|
|
|
|
app.config["SCREENSHOT_SELENIUM_HEADSTART"] = 5
|
|
|
|
webdriver.get_screenshot(url, "chart-container", user=user)
|
|
|
|
assert mock_sleep.call_args_list[0] == call(5)
|
|
|
|
|
|
|
|
@patch("superset.utils.webdriver.WebDriverWait")
|
|
|
|
@patch("superset.utils.webdriver.firefox")
|
|
|
|
def test_screenshot_selenium_locate_wait(self, mock_webdriver, mock_webdriver_wait):
|
|
|
|
app.config["SCREENSHOT_LOCATE_WAIT"] = 15
|
|
|
|
webdriver = WebDriverProxy("firefox")
|
|
|
|
user = security_manager.get_user_by_username(
|
|
|
|
app.config["THUMBNAIL_SELENIUM_USER"]
|
|
|
|
)
|
|
|
|
url = get_url_path("Superset.slice", slice_id=1, standalone="true")
|
|
|
|
webdriver.get_screenshot(url, "chart-container", user=user)
|
|
|
|
assert mock_webdriver_wait.call_args_list[0] == call(ANY, 15)
|
|
|
|
|
|
|
|
@patch("superset.utils.webdriver.WebDriverWait")
|
|
|
|
@patch("superset.utils.webdriver.firefox")
|
|
|
|
def test_screenshot_selenium_load_wait(self, mock_webdriver, mock_webdriver_wait):
|
|
|
|
app.config["SCREENSHOT_LOAD_WAIT"] = 15
|
|
|
|
webdriver = WebDriverProxy("firefox")
|
|
|
|
user = security_manager.get_user_by_username(
|
|
|
|
app.config["THUMBNAIL_SELENIUM_USER"]
|
|
|
|
)
|
|
|
|
url = get_url_path("Superset.slice", slice_id=1, standalone="true")
|
|
|
|
webdriver.get_screenshot(url, "chart-container", user=user)
|
|
|
|
assert mock_webdriver_wait.call_args_list[1] == call(ANY, 15)
|
|
|
|
|
2021-07-15 21:25:48 -04:00
|
|
|
@patch("superset.utils.webdriver.WebDriverWait")
|
|
|
|
@patch("superset.utils.webdriver.firefox")
|
|
|
|
@patch("superset.utils.webdriver.sleep")
|
|
|
|
def test_screenshot_selenium_animation_wait(
|
|
|
|
self, mock_sleep, mock_webdriver, mock_webdriver_wait
|
|
|
|
):
|
|
|
|
webdriver = WebDriverProxy("firefox")
|
|
|
|
user = security_manager.get_user_by_username(
|
|
|
|
app.config["THUMBNAIL_SELENIUM_USER"]
|
|
|
|
)
|
|
|
|
url = get_url_path("Superset.slice", slice_id=1, standalone="true")
|
|
|
|
app.config["SCREENSHOT_SELENIUM_ANIMATION_WAIT"] = 4
|
|
|
|
webdriver.get_screenshot(url, "chart-container", user=user)
|
|
|
|
assert mock_sleep.call_args_list[1] == call(4)
|
|
|
|
|
2021-06-08 10:33:42 -04:00
|
|
|
|
2020-06-29 18:36:06 -04:00
|
|
|
class TestThumbnails(SupersetTestCase):
|
2020-04-15 04:40:14 -04:00
|
|
|
|
|
|
|
mock_image = b"bytes mock image"
|
2022-12-14 08:02:31 -05:00
|
|
|
digest_return_value = "foo_bar"
|
|
|
|
digest_hash = "5c7d96a3dd7a87850a2ef34087565a6e"
|
2020-04-15 04:40:14 -04:00
|
|
|
|
2022-12-14 08:02:31 -05:00
|
|
|
def _get_id_and_thumbnail_url(self, url: str) -> Tuple[int, str]:
|
|
|
|
rv = self.client.get(url)
|
|
|
|
resp = json.loads(rv.data.decode("utf-8"))
|
|
|
|
obj = resp["result"][0]
|
|
|
|
return obj["id"], obj["thumbnail_url"]
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
2021-05-14 15:49:25 -04:00
|
|
|
@with_feature_flags(THUMBNAILS=False)
|
2020-04-15 04:40:14 -04:00
|
|
|
def test_dashboard_thumbnail_disabled(self):
|
|
|
|
"""
|
2021-04-12 16:18:17 -04:00
|
|
|
Thumbnails: Dashboard thumbnail disabled
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
|
|
|
self.login(username="admin")
|
2022-12-14 08:02:31 -05:00
|
|
|
_, thumbnail_url = self._get_id_and_thumbnail_url(DASHBOARD_URL)
|
|
|
|
rv = self.client.get(thumbnail_url)
|
2020-04-15 04:40:14 -04:00
|
|
|
self.assertEqual(rv.status_code, 404)
|
|
|
|
|
2022-12-14 08:02:31 -05:00
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
2021-05-14 15:49:25 -04:00
|
|
|
@with_feature_flags(THUMBNAILS=False)
|
2020-04-15 04:40:14 -04:00
|
|
|
def test_chart_thumbnail_disabled(self):
|
|
|
|
"""
|
2021-04-12 16:18:17 -04:00
|
|
|
Thumbnails: Chart thumbnail disabled
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
|
|
|
self.login(username="admin")
|
2022-12-14 08:02:31 -05:00
|
|
|
_, thumbnail_url = self._get_id_and_thumbnail_url(CHART_URL)
|
|
|
|
rv = self.client.get(thumbnail_url)
|
2020-04-15 04:40:14 -04:00
|
|
|
self.assertEqual(rv.status_code, 404)
|
|
|
|
|
2022-12-14 08:02:31 -05:00
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
2021-05-14 15:49:25 -04:00
|
|
|
@with_feature_flags(THUMBNAILS=True)
|
2022-12-14 08:02:31 -05:00
|
|
|
def test_get_async_dashboard_screenshot_as_selenium(self):
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
2022-12-14 08:02:31 -05:00
|
|
|
Thumbnails: Simple get async dashboard screenshot as selenium user
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
2022-12-14 08:02:31 -05:00
|
|
|
self.login(username="alpha")
|
2020-04-15 04:40:14 -04:00
|
|
|
with patch(
|
2022-12-14 08:02:31 -05:00
|
|
|
"superset.thumbnails.digest._adjust_string_for_executor"
|
|
|
|
) as mock_adjust_string:
|
|
|
|
mock_adjust_string.return_value = self.digest_return_value
|
|
|
|
_, thumbnail_url = self._get_id_and_thumbnail_url(DASHBOARD_URL)
|
|
|
|
assert self.digest_hash in thumbnail_url
|
|
|
|
assert mock_adjust_string.call_args[0][1] == ExecutorType.SELENIUM
|
|
|
|
assert mock_adjust_string.call_args[0][2] == "admin"
|
|
|
|
|
|
|
|
rv = self.client.get(thumbnail_url)
|
2020-04-15 04:40:14 -04:00
|
|
|
self.assertEqual(rv.status_code, 202)
|
|
|
|
|
2022-12-14 08:02:31 -05:00
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
|
|
|
@with_feature_flags(THUMBNAILS=True)
|
|
|
|
def test_get_async_dashboard_screenshot_as_current_user(self):
|
|
|
|
"""
|
|
|
|
Thumbnails: Simple get async dashboard screenshot as current user
|
|
|
|
"""
|
|
|
|
username = "alpha"
|
|
|
|
self.login(username=username)
|
|
|
|
with patch.dict(
|
|
|
|
"superset.thumbnails.digest.current_app.config",
|
|
|
|
{
|
|
|
|
"THUMBNAIL_EXECUTE_AS": [ExecutorType.CURRENT_USER],
|
|
|
|
},
|
|
|
|
), patch(
|
|
|
|
"superset.thumbnails.digest._adjust_string_for_executor"
|
|
|
|
) as mock_adjust_string:
|
|
|
|
mock_adjust_string.return_value = self.digest_return_value
|
|
|
|
_, thumbnail_url = self._get_id_and_thumbnail_url(DASHBOARD_URL)
|
|
|
|
assert self.digest_hash in thumbnail_url
|
|
|
|
assert mock_adjust_string.call_args[0][1] == ExecutorType.CURRENT_USER
|
|
|
|
assert mock_adjust_string.call_args[0][2] == username
|
|
|
|
|
|
|
|
rv = self.client.get(thumbnail_url)
|
|
|
|
self.assertEqual(rv.status_code, 202)
|
2021-05-14 15:49:25 -04:00
|
|
|
|
2022-12-14 08:02:31 -05:00
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
2021-05-14 15:49:25 -04:00
|
|
|
@with_feature_flags(THUMBNAILS=True)
|
2020-04-15 04:40:14 -04:00
|
|
|
def test_get_async_dashboard_notfound(self):
|
|
|
|
"""
|
2021-04-12 16:18:17 -04:00
|
|
|
Thumbnails: Simple get async dashboard not found
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
|
|
|
max_id = db.session.query(func.max(Dashboard.id)).scalar()
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/{max_id + 1}/thumbnail/1234/"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
self.assertEqual(rv.status_code, 404)
|
|
|
|
|
2022-12-14 08:02:31 -05:00
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
2020-04-15 04:40:14 -04:00
|
|
|
@skipUnless((is_feature_enabled("THUMBNAILS")), "Thumbnails feature")
|
|
|
|
def test_get_async_dashboard_not_allowed(self):
|
|
|
|
"""
|
2021-04-12 16:18:17 -04:00
|
|
|
Thumbnails: Simple get async dashboard not allowed
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
|
|
|
self.login(username="gamma")
|
2022-12-14 08:02:31 -05:00
|
|
|
_, thumbnail_url = self._get_id_and_thumbnail_url(DASHBOARD_URL)
|
|
|
|
rv = self.client.get(thumbnail_url)
|
2020-04-15 04:40:14 -04:00
|
|
|
self.assertEqual(rv.status_code, 404)
|
|
|
|
|
2022-12-14 08:02:31 -05:00
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
2021-05-14 15:49:25 -04:00
|
|
|
@with_feature_flags(THUMBNAILS=True)
|
2022-12-14 08:02:31 -05:00
|
|
|
def test_get_async_chart_screenshot_as_selenium(self):
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
2022-12-14 08:02:31 -05:00
|
|
|
Thumbnails: Simple get async chart screenshot as selenium user
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
2022-12-14 08:02:31 -05:00
|
|
|
self.login(username="alpha")
|
2020-04-15 04:40:14 -04:00
|
|
|
with patch(
|
2022-12-14 08:02:31 -05:00
|
|
|
"superset.thumbnails.digest._adjust_string_for_executor"
|
|
|
|
) as mock_adjust_string:
|
|
|
|
mock_adjust_string.return_value = self.digest_return_value
|
|
|
|
_, thumbnail_url = self._get_id_and_thumbnail_url(CHART_URL)
|
|
|
|
assert self.digest_hash in thumbnail_url
|
|
|
|
assert mock_adjust_string.call_args[0][1] == ExecutorType.SELENIUM
|
|
|
|
assert mock_adjust_string.call_args[0][2] == "admin"
|
|
|
|
|
|
|
|
rv = self.client.get(thumbnail_url)
|
|
|
|
self.assertEqual(rv.status_code, 202)
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
|
|
|
@with_feature_flags(THUMBNAILS=True)
|
|
|
|
def test_get_async_chart_screenshot_as_current_user(self):
|
|
|
|
"""
|
|
|
|
Thumbnails: Simple get async chart screenshot as current user
|
|
|
|
"""
|
|
|
|
username = "alpha"
|
|
|
|
self.login(username=username)
|
|
|
|
with patch.dict(
|
|
|
|
"superset.thumbnails.digest.current_app.config",
|
|
|
|
{
|
|
|
|
"THUMBNAIL_EXECUTE_AS": [ExecutorType.CURRENT_USER],
|
|
|
|
},
|
|
|
|
), patch(
|
|
|
|
"superset.thumbnails.digest._adjust_string_for_executor"
|
|
|
|
) as mock_adjust_string:
|
|
|
|
mock_adjust_string.return_value = self.digest_return_value
|
|
|
|
_, thumbnail_url = self._get_id_and_thumbnail_url(CHART_URL)
|
|
|
|
assert self.digest_hash in thumbnail_url
|
|
|
|
assert mock_adjust_string.call_args[0][1] == ExecutorType.CURRENT_USER
|
|
|
|
assert mock_adjust_string.call_args[0][2] == username
|
|
|
|
|
|
|
|
rv = self.client.get(thumbnail_url)
|
2020-04-15 04:40:14 -04:00
|
|
|
self.assertEqual(rv.status_code, 202)
|
|
|
|
|
2022-12-14 08:02:31 -05:00
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
2021-05-14 15:49:25 -04:00
|
|
|
@with_feature_flags(THUMBNAILS=True)
|
2020-04-15 04:40:14 -04:00
|
|
|
def test_get_async_chart_notfound(self):
|
|
|
|
"""
|
2021-04-12 16:18:17 -04:00
|
|
|
Thumbnails: Simple get async chart not found
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
|
|
|
max_id = db.session.query(func.max(Slice.id)).scalar()
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/chart/{max_id + 1}/thumbnail/1234/"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
self.assertEqual(rv.status_code, 404)
|
|
|
|
|
2022-12-14 08:02:31 -05:00
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
2021-05-14 15:49:25 -04:00
|
|
|
@with_feature_flags(THUMBNAILS=True)
|
2020-04-15 04:40:14 -04:00
|
|
|
def test_get_cached_chart_wrong_digest(self):
|
|
|
|
"""
|
2021-04-12 16:18:17 -04:00
|
|
|
Thumbnails: Simple get chart with wrong digest
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
2021-05-14 15:49:25 -04:00
|
|
|
with patch.object(
|
|
|
|
ChartScreenshot, "get_from_cache", return_value=BytesIO(self.mock_image)
|
|
|
|
):
|
|
|
|
self.login(username="admin")
|
2022-12-14 08:02:31 -05:00
|
|
|
id_, thumbnail_url = self._get_id_and_thumbnail_url(CHART_URL)
|
|
|
|
rv = self.client.get(f"api/v1/chart/{id_}/thumbnail/1234/")
|
2021-05-14 15:49:25 -04:00
|
|
|
self.assertEqual(rv.status_code, 302)
|
2023-01-09 05:10:31 -05:00
|
|
|
self.assertEqual(rv.headers["Location"], thumbnail_url)
|
2020-04-15 04:40:14 -04:00
|
|
|
|
2022-12-14 08:02:31 -05:00
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
2021-05-14 15:49:25 -04:00
|
|
|
@with_feature_flags(THUMBNAILS=True)
|
2020-04-15 04:40:14 -04:00
|
|
|
def test_get_cached_dashboard_screenshot(self):
|
|
|
|
"""
|
2021-04-12 16:18:17 -04:00
|
|
|
Thumbnails: Simple get cached dashboard screenshot
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
2021-05-14 15:49:25 -04:00
|
|
|
with patch.object(
|
|
|
|
DashboardScreenshot, "get_from_cache", return_value=BytesIO(self.mock_image)
|
|
|
|
):
|
|
|
|
self.login(username="admin")
|
2022-12-14 08:02:31 -05:00
|
|
|
_, thumbnail_url = self._get_id_and_thumbnail_url(DASHBOARD_URL)
|
|
|
|
rv = self.client.get(thumbnail_url)
|
2021-05-14 15:49:25 -04:00
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.data, self.mock_image)
|
2020-04-15 04:40:14 -04:00
|
|
|
|
2022-12-14 08:02:31 -05:00
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
2021-05-14 15:49:25 -04:00
|
|
|
@with_feature_flags(THUMBNAILS=True)
|
2020-04-15 04:40:14 -04:00
|
|
|
def test_get_cached_chart_screenshot(self):
|
|
|
|
"""
|
2021-04-12 16:18:17 -04:00
|
|
|
Thumbnails: Simple get cached chart screenshot
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
2021-05-14 15:49:25 -04:00
|
|
|
with patch.object(
|
|
|
|
ChartScreenshot, "get_from_cache", return_value=BytesIO(self.mock_image)
|
|
|
|
):
|
|
|
|
self.login(username="admin")
|
2022-12-14 08:02:31 -05:00
|
|
|
id_, thumbnail_url = self._get_id_and_thumbnail_url(CHART_URL)
|
|
|
|
rv = self.client.get(thumbnail_url)
|
2021-05-14 15:49:25 -04:00
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.data, self.mock_image)
|
2020-04-15 04:40:14 -04:00
|
|
|
|
2022-12-14 08:02:31 -05:00
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
2021-05-14 15:49:25 -04:00
|
|
|
@with_feature_flags(THUMBNAILS=True)
|
2020-04-15 04:40:14 -04:00
|
|
|
def test_get_cached_dashboard_wrong_digest(self):
|
|
|
|
"""
|
2021-04-12 16:18:17 -04:00
|
|
|
Thumbnails: Simple get dashboard with wrong digest
|
2020-04-15 04:40:14 -04:00
|
|
|
"""
|
2021-05-14 15:49:25 -04:00
|
|
|
with patch.object(
|
|
|
|
DashboardScreenshot, "get_from_cache", return_value=BytesIO(self.mock_image)
|
|
|
|
):
|
|
|
|
self.login(username="admin")
|
2022-12-14 08:02:31 -05:00
|
|
|
id_, thumbnail_url = self._get_id_and_thumbnail_url(DASHBOARD_URL)
|
|
|
|
rv = self.client.get(f"api/v1/dashboard/{id_}/thumbnail/1234/")
|
2021-05-14 15:49:25 -04:00
|
|
|
self.assertEqual(rv.status_code, 302)
|
2023-01-09 05:10:31 -05:00
|
|
|
self.assertEqual(rv.headers["Location"], thumbnail_url)
|