# 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. # isort:skip_file """Unit tests for Superset""" import csv import datetime import doctest import html import io import json import logging from typing import Dict, List from urllib.parse import quote from tests.fixtures.birth_names_dashboard import load_birth_names_dashboard_with_slices import pytest import pytz import random import re import unittest from unittest import mock, skipUnless import pandas as pd import sqlalchemy as sqla from sqlalchemy.exc import SQLAlchemyError from superset.models.cache import CacheKey from superset.utils.core import get_example_database from tests.fixtures.energy_dashboard import load_energy_table_with_slice from tests.test_app import app import superset.views.utils from superset import ( dataframe, db, jinja_context, security_manager, sql_lab, is_feature_enabled, ) from superset.connectors.sqla.models import SqlaTable from superset.db_engine_specs.base import BaseEngineSpec from superset.db_engine_specs.mssql import MssqlEngineSpec from superset.exceptions import SupersetException from superset.extensions import async_query_manager from superset.models import core as models from superset.models.annotations import Annotation, AnnotationLayer from superset.models.dashboard import Dashboard from superset.models.datasource_access_request import DatasourceAccessRequest from superset.models.slice import Slice from superset.models.sql_lab import Query from superset.result_set import SupersetResultSet from superset.utils import core as utils from superset.views import core as views from superset.views.database.views import DatabaseView from .base_tests import SupersetTestCase from tests.fixtures.world_bank_dashboard import load_world_bank_dashboard_with_slices logger = logging.getLogger(__name__) class TestCore(SupersetTestCase): def setUp(self): db.session.query(Query).delete() db.session.query(DatasourceAccessRequest).delete() db.session.query(models.Log).delete() self.table_ids = { tbl.table_name: tbl.id for tbl in (db.session.query(SqlaTable).all()) } self.original_unsafe_db_setting = app.config["PREVENT_UNSAFE_DB_CONNECTIONS"] def tearDown(self): db.session.query(Query).delete() app.config["PREVENT_UNSAFE_DB_CONNECTIONS"] = self.original_unsafe_db_setting def test_login(self): resp = self.get_resp("/login/", data=dict(username="admin", password="general")) self.assertNotIn("User confirmation needed", resp) resp = self.get_resp("/logout/", follow_redirects=True) self.assertIn("User confirmation needed", resp) resp = self.get_resp( "/login/", data=dict(username="admin", password="wrongPassword") ) self.assertIn("User confirmation needed", resp) def test_dashboard_endpoint(self): self.login() resp = self.client.get("/superset/dashboard/-1/") assert resp.status_code == 404 @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") def test_slice_endpoint(self): self.login(username="admin") slc = self.get_slice("Girls", db.session) resp = self.get_resp("/superset/slice/{}/".format(slc.id)) assert "Original value" in resp assert "List Roles" in resp # Testing overrides resp = self.get_resp("/superset/slice/{}/?standalone=true".format(slc.id)) assert '