Skip to content

Commit 2cb4fd3

Browse files
authored
refactor(test): add login_as_admin in global conftest (apache#20703)
1 parent 5beb1aa commit 2cb4fd3

27 files changed

+497
-457
lines changed

superset/examples/birth_names.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ def _add_table_metrics(datasource: SqlaTable) -> None:
160160
col.is_dttm = True
161161
break
162162

163+
datasource.columns = columns
164+
datasource.metrics = metrics
165+
163166

164167
def create_slices(tbl: SqlaTable, admin_owner: bool) -> Tuple[List[Slice], List[Slice]]:
165168
metrics = [

superset/views/core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,8 @@ def dashboard(
19791979
return self.render_template(
19801980
"superset/spa.html",
19811981
entry="spa",
1982+
# dashboard title is always visible
1983+
title=dashboard.dashboard_title,
19821984
bootstrap_data=json.dumps(
19831985
bootstrap_data, default=utils.pessimistic_json_iso_dttm_ser
19841986
),

tests/integration_tests/advanced_data_type/api_tests.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,9 @@
1818
"""Unit tests for Superset"""
1919
import json
2020
import prison
21-
from sqlalchemy import null
2221

23-
from superset.connectors.sqla.models import SqlaTable
2422
from superset.utils.core import get_example_default_schema
2523

26-
from tests.integration_tests.base_tests import (
27-
SupersetTestCase,
28-
logged_in_admin,
29-
test_client,
30-
)
31-
from tests.integration_tests.test_app import app
3224
from tests.integration_tests.utils.get_dashboards import get_dashboards_ids
3325
from unittest import mock
3426
from sqlalchemy import Column
@@ -80,7 +72,7 @@ def translate_filter_func(col: Column, op: FilterOperator, values: List[Any]):
8072
"superset.advanced_data_type.api.ADVANCED_DATA_TYPES",
8173
{"type": 1},
8274
)
83-
def test_types_type_request(logged_in_admin):
75+
def test_types_type_request(test_client, login_as_admin):
8476
"""
8577
Advanced Data Type API: Test to see if the API call returns all the valid advanced data types
8678
"""
@@ -91,7 +83,7 @@ def test_types_type_request(logged_in_admin):
9183
assert data == {"result": ["type"]}
9284

9385

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

10395

104-
def test_types_convert_bad_request_no_type(logged_in_admin):
96+
def test_types_convert_bad_request_no_type(test_client, login_as_admin):
10597
"""
10698
Advanced Data Type API: Test request to see if it behaves as expected when no type is passed
10799
"""
@@ -115,7 +107,7 @@ def test_types_convert_bad_request_no_type(logged_in_admin):
115107
"superset.advanced_data_type.api.ADVANCED_DATA_TYPES",
116108
{"type": 1},
117109
)
118-
def test_types_convert_bad_request_type_not_found(logged_in_admin):
110+
def test_types_convert_bad_request_type_not_found(test_client, login_as_admin):
119111
"""
120112
Advanced Data Type API: Test request to see if it behaves as expected when passed in type is
121113
not found/not valid
@@ -130,7 +122,7 @@ def test_types_convert_bad_request_type_not_found(logged_in_admin):
130122
"superset.advanced_data_type.api.ADVANCED_DATA_TYPES",
131123
{"type": test_type},
132124
)
133-
def test_types_convert_request(logged_in_admin):
125+
def test_types_convert_request(test_client, login_as_admin):
134126
"""
135127
Advanced Data Type API: Test request to see if it behaves as expected when a valid type
136128
and valid values are passed in

tests/integration_tests/base_tests.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from unittest.mock import Mock, patch, MagicMock
2525

2626
import pandas as pd
27-
import pytest
2827
from flask import Response
2928
from flask_appbuilder.security.sqla import models as ab_models
3029
from flask_testing import TestCase
@@ -34,7 +33,7 @@
3433
from sqlalchemy.sql import func
3534
from sqlalchemy.dialects.mysql import dialect
3635

37-
from tests.integration_tests.test_app import app
36+
from tests.integration_tests.test_app import app, login
3837
from superset.sql_parse import CtasMethod
3938
from superset import db, security_manager
4039
from superset.connectors.base.models import BaseDatasource
@@ -52,11 +51,6 @@
5251
test_client = app.test_client()
5352

5453

55-
def login(client: Any, username: str = "admin", password: str = "general"):
56-
resp = get_resp(client, "/login/", data=dict(username=username, password=password))
57-
assert "User confirmation needed" not in resp
58-
59-
6054
def get_resp(
6155
client: Any,
6256
url: str,
@@ -101,15 +95,6 @@ def post_assert_metric(
10195
return rv
10296

10397

104-
@pytest.fixture
105-
def logged_in_admin():
106-
"""Fixture with app context and logged in admin user."""
107-
with app.app_context():
108-
login(test_client, username="admin")
109-
yield
110-
test_client.get("/logout/", follow_redirects=True)
111-
112-
11398
class SupersetTestCase(TestCase):
11499
default_schema_backend_map = {
115100
"sqlite": "main",

tests/integration_tests/cachekeys/api_tests.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,33 @@
1818
"""Unit tests for Superset"""
1919
from typing import Dict, Any
2020

21-
from tests.integration_tests.test_app import app # noqa
21+
import pytest
2222

2323
from superset.extensions import cache_manager, db
2424
from superset.models.cache import CacheKey
2525
from superset.utils.core import get_example_default_schema
2626
from tests.integration_tests.base_tests import (
2727
SupersetTestCase,
2828
post_assert_metric,
29-
test_client,
30-
logged_in_admin,
31-
) # noqa
29+
)
3230

3331

34-
def invalidate(params: Dict[str, Any]):
35-
return post_assert_metric(
36-
test_client, "api/v1/cachekey/invalidate", params, "invalidate"
37-
)
32+
@pytest.fixture
33+
def invalidate(test_client, login_as_admin):
34+
def _invalidate(params: Dict[str, Any]):
35+
return post_assert_metric(
36+
test_client, "api/v1/cachekey/invalidate", params, "invalidate"
37+
)
38+
39+
return _invalidate
3840

3941

40-
def test_invalidate_cache(logged_in_admin):
42+
def test_invalidate_cache(invalidate):
4143
rv = invalidate({"datasource_uids": ["3__table"]})
4244
assert rv.status_code == 201
4345

4446

45-
def test_invalidate_existing_cache(logged_in_admin):
47+
def test_invalidate_existing_cache(invalidate):
4648
db.session.add(CacheKey(cache_key="cache_key", datasource_uid="3__table"))
4749
db.session.commit()
4850
cache_manager.cache.set("cache_key", "value")
@@ -56,7 +58,7 @@ def test_invalidate_existing_cache(logged_in_admin):
5658
)
5759

5860

59-
def test_invalidate_cache_empty_input(logged_in_admin):
61+
def test_invalidate_cache_empty_input(invalidate):
6062
rv = invalidate({"datasource_uids": []})
6163
assert rv.status_code == 201
6264

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

6971

70-
def test_invalidate_cache_bad_request(logged_in_admin):
72+
def test_invalidate_cache_bad_request(invalidate):
7173
rv = invalidate(
7274
{
7375
"datasource_uids": [],
@@ -93,7 +95,7 @@ def test_invalidate_cache_bad_request(logged_in_admin):
9395
assert rv.status_code == 400
9496

9597

96-
def test_invalidate_existing_caches(logged_in_admin):
98+
def test_invalidate_existing_caches(invalidate):
9799
schema = get_example_default_schema() or ""
98100
bn = SupersetTestCase.get_birth_names_dataset()
99101

0 commit comments

Comments
 (0)