Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AAfghahi committed Jan 25, 2022
1 parent 602ebba commit 565e2c1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
4 changes: 3 additions & 1 deletion superset/reports/notifications/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from superset.reports.notifications.base import BaseNotification
from superset.reports.notifications.exceptions import NotificationError
from superset.utils.core import send_email_smtp
from superset.utils.urls import get_screenshot_explorelink

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -94,6 +95,7 @@ def _get_content(self) -> EmailContent:
html_table = ""

call_to_action = __("Explore in Superset")
url = get_screenshot_explorelink(self._content.url)
img_tags = []
for msgid in images.keys():
img_tags.append(
Expand Down Expand Up @@ -122,7 +124,7 @@ def _get_content(self) -> EmailContent:
</head>
<body>
<p>{description}</p>
<b><a href="{self._content.url}">{call_to_action}</a></b><p></p>
<b><a href="{url}">{call_to_action}</a></b><p></p>
{html_table}
{img_tag}
</body>
Expand Down
11 changes: 10 additions & 1 deletion superset/utils/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.
import urllib
from typing import Any
from typing import Any, Optional

from flask import current_app, url_for

Expand All @@ -33,3 +33,12 @@ def headless_url(path: str, user_friendly: bool = False) -> str:
def get_url_path(view: str, user_friendly: bool = False, **kwargs: Any) -> str:
with current_app.test_request_context():
return headless_url(url_for(view, **kwargs), user_friendly=user_friendly)


def get_screenshot_explorelink(url: Optional[str]) -> Optional[str]:
data = list(urllib.parse.urlsplit(url))
params = urllib.parse.parse_qs(data[3])
params["standalone"] = ["0"]
data[3] = "&".join(f"{k}={urllib.parse.quote(v[0])}" for k, v in params.items())
url = urllib.parse.urlunsplit(data)
return url
35 changes: 35 additions & 0 deletions tests/unit_tests/utils/urls_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# 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.utils.urls import get_screenshot_explorelink

EXPLORE_CHART_LINK = "http://localhost:9000/superset/explore/?form_data=%7B%22slice_id%22%3A+76%7D&standalone=true&force=false"

EXPLORE_DASHBOARD_LINK = "http://localhost:9000/superset/dashboard/3/?standalone=3"


def test_convert_chart_link() -> None:
test_url = get_screenshot_explorelink(EXPLORE_CHART_LINK)
assert (
test_url
== "http://localhost:9000/superset/explore/?form_data=%7B%22slice_id%22%3A%2076%7D&standalone=0&force=false"
)


def test_convert_dashboard_link() -> None:
test_url = get_screenshot_explorelink(EXPLORE_DASHBOARD_LINK)
assert test_url == "http://localhost:9000/superset/dashboard/3/?standalone=0"

0 comments on commit 565e2c1

Please sign in to comment.