Skip to content

Commit

Permalink
refactor(test): add login_as_admin in global conftest (#20703)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmud authored Jul 15, 2022
1 parent 5beb1aa commit 2cb4fd3
Show file tree
Hide file tree
Showing 27 changed files with 497 additions and 457 deletions.
3 changes: 3 additions & 0 deletions superset/examples/birth_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ def _add_table_metrics(datasource: SqlaTable) -> None:
col.is_dttm = True
break

datasource.columns = columns
datasource.metrics = metrics


def create_slices(tbl: SqlaTable, admin_owner: bool) -> Tuple[List[Slice], List[Slice]]:
metrics = [
Expand Down
2 changes: 2 additions & 0 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,8 @@ def dashboard(
return self.render_template(
"superset/spa.html",
entry="spa",
# dashboard title is always visible
title=dashboard.dashboard_title,
bootstrap_data=json.dumps(
bootstrap_data, default=utils.pessimistic_json_iso_dttm_ser
),
Expand Down
18 changes: 5 additions & 13 deletions tests/integration_tests/advanced_data_type/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,9 @@
"""Unit tests for Superset"""
import json
import prison
from sqlalchemy import null

from superset.connectors.sqla.models import SqlaTable
from superset.utils.core import get_example_default_schema

from tests.integration_tests.base_tests import (
SupersetTestCase,
logged_in_admin,
test_client,
)
from tests.integration_tests.test_app import app
from tests.integration_tests.utils.get_dashboards import get_dashboards_ids
from unittest import mock
from sqlalchemy import Column
Expand Down Expand Up @@ -80,7 +72,7 @@ def translate_filter_func(col: Column, op: FilterOperator, values: List[Any]):
"superset.advanced_data_type.api.ADVANCED_DATA_TYPES",
{"type": 1},
)
def test_types_type_request(logged_in_admin):
def test_types_type_request(test_client, login_as_admin):
"""
Advanced Data Type API: Test to see if the API call returns all the valid advanced data types
"""
Expand All @@ -91,7 +83,7 @@ def test_types_type_request(logged_in_admin):
assert data == {"result": ["type"]}


def test_types_convert_bad_request_no_vals(logged_in_admin):
def test_types_convert_bad_request_no_vals(test_client, login_as_admin):
"""
Advanced Data Type API: Test request to see if it behaves as expected when no values are passed
"""
Expand All @@ -101,7 +93,7 @@ def test_types_convert_bad_request_no_vals(logged_in_admin):
assert response_value.status_code == 400


def test_types_convert_bad_request_no_type(logged_in_admin):
def test_types_convert_bad_request_no_type(test_client, login_as_admin):
"""
Advanced Data Type API: Test request to see if it behaves as expected when no type is passed
"""
Expand All @@ -115,7 +107,7 @@ def test_types_convert_bad_request_no_type(logged_in_admin):
"superset.advanced_data_type.api.ADVANCED_DATA_TYPES",
{"type": 1},
)
def test_types_convert_bad_request_type_not_found(logged_in_admin):
def test_types_convert_bad_request_type_not_found(test_client, login_as_admin):
"""
Advanced Data Type API: Test request to see if it behaves as expected when passed in type is
not found/not valid
Expand All @@ -130,7 +122,7 @@ def test_types_convert_bad_request_type_not_found(logged_in_admin):
"superset.advanced_data_type.api.ADVANCED_DATA_TYPES",
{"type": test_type},
)
def test_types_convert_request(logged_in_admin):
def test_types_convert_request(test_client, login_as_admin):
"""
Advanced Data Type API: Test request to see if it behaves as expected when a valid type
and valid values are passed in
Expand Down
17 changes: 1 addition & 16 deletions tests/integration_tests/base_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from unittest.mock import Mock, patch, MagicMock

import pandas as pd
import pytest
from flask import Response
from flask_appbuilder.security.sqla import models as ab_models
from flask_testing import TestCase
Expand All @@ -34,7 +33,7 @@
from sqlalchemy.sql import func
from sqlalchemy.dialects.mysql import dialect

from tests.integration_tests.test_app import app
from tests.integration_tests.test_app import app, login
from superset.sql_parse import CtasMethod
from superset import db, security_manager
from superset.connectors.base.models import BaseDatasource
Expand All @@ -52,11 +51,6 @@
test_client = app.test_client()


def login(client: Any, username: str = "admin", password: str = "general"):
resp = get_resp(client, "/login/", data=dict(username=username, password=password))
assert "User confirmation needed" not in resp


def get_resp(
client: Any,
url: str,
Expand Down Expand Up @@ -101,15 +95,6 @@ def post_assert_metric(
return rv


@pytest.fixture
def logged_in_admin():
"""Fixture with app context and logged in admin user."""
with app.app_context():
login(test_client, username="admin")
yield
test_client.get("/logout/", follow_redirects=True)


class SupersetTestCase(TestCase):
default_schema_backend_map = {
"sqlite": "main",
Expand Down
28 changes: 15 additions & 13 deletions tests/integration_tests/cachekeys/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,33 @@
"""Unit tests for Superset"""
from typing import Dict, Any

from tests.integration_tests.test_app import app # noqa
import pytest

from superset.extensions import cache_manager, db
from superset.models.cache import CacheKey
from superset.utils.core import get_example_default_schema
from tests.integration_tests.base_tests import (
SupersetTestCase,
post_assert_metric,
test_client,
logged_in_admin,
) # noqa
)


def invalidate(params: Dict[str, Any]):
return post_assert_metric(
test_client, "api/v1/cachekey/invalidate", params, "invalidate"
)
@pytest.fixture
def invalidate(test_client, login_as_admin):
def _invalidate(params: Dict[str, Any]):
return post_assert_metric(
test_client, "api/v1/cachekey/invalidate", params, "invalidate"
)

return _invalidate


def test_invalidate_cache(logged_in_admin):
def test_invalidate_cache(invalidate):
rv = invalidate({"datasource_uids": ["3__table"]})
assert rv.status_code == 201


def test_invalidate_existing_cache(logged_in_admin):
def test_invalidate_existing_cache(invalidate):
db.session.add(CacheKey(cache_key="cache_key", datasource_uid="3__table"))
db.session.commit()
cache_manager.cache.set("cache_key", "value")
Expand All @@ -56,7 +58,7 @@ def test_invalidate_existing_cache(logged_in_admin):
)


def test_invalidate_cache_empty_input(logged_in_admin):
def test_invalidate_cache_empty_input(invalidate):
rv = invalidate({"datasource_uids": []})
assert rv.status_code == 201

Expand All @@ -67,7 +69,7 @@ def test_invalidate_cache_empty_input(logged_in_admin):
assert rv.status_code == 201


def test_invalidate_cache_bad_request(logged_in_admin):
def test_invalidate_cache_bad_request(invalidate):
rv = invalidate(
{
"datasource_uids": [],
Expand All @@ -93,7 +95,7 @@ def test_invalidate_cache_bad_request(logged_in_admin):
assert rv.status_code == 400


def test_invalidate_existing_caches(logged_in_admin):
def test_invalidate_existing_caches(invalidate):
schema = get_example_default_schema() or ""
bn = SupersetTestCase.get_birth_names_dataset()

Expand Down
Loading

0 comments on commit 2cb4fd3

Please sign in to comment.