diff --git a/tests/integration_tests/conftest.py b/tests/integration_tests/conftest.py index 67d73ff147b..0abdcff4c89 100644 --- a/tests/integration_tests/conftest.py +++ b/tests/integration_tests/conftest.py @@ -21,15 +21,18 @@ from typing import Any, Callable, Generator, Optional, TYPE_CHECKING from unittest.mock import patch import pytest +from flask.ctx import AppContext from sqlalchemy.engine import Engine from superset import db from superset.extensions import feature_flag_manager from superset.utils.core import json_dumps_w_dates from superset.utils.database import get_example_database, remove_database -from tests.integration_tests.test_app import app +from tests.integration_tests.test_app import app, login if TYPE_CHECKING: + from flask.testing import FlaskClient + from superset.connectors.sqla.models import Database CTAS_SCHEMA_NAME = "sqllab_test_db" @@ -42,6 +45,12 @@ def app_context(): yield +@pytest.fixture +def test_client(app_context: AppContext): + with app.test_client() as client: + yield client + + @pytest.fixture(autouse=True, scope="session") def setup_sample_data() -> Any: # TODO(john-bodley): Determine a cleaner way of setting up the sample data without @@ -75,6 +84,18 @@ def setup_sample_data() -> Any: db.session.commit() +@pytest.fixture +def login_as(test_client: "FlaskClient[Any]"): + """Fixture with app context and logged in admin user.""" + + def _login_as(username: str, password: str = "general"): + login(test_client, username=username, password=password) + + yield _login_as + # no need to log out as both app_context and test_client are + # function level fixtures anyway + + @pytest.fixture def login_as_admin(login_as: Callable[..., None]): yield login_as("admin") diff --git a/tests/integration_tests/test_app.py b/tests/integration_tests/test_app.py index 798f3e9cda2..2ca39ba16bb 100644 --- a/tests/integration_tests/test_app.py +++ b/tests/integration_tests/test_app.py @@ -19,6 +19,24 @@ Here is where we create the app which ends up being shared across all tests.integration_tests. A future optimization will be to create a separate app instance for each test class. """ +from typing import TYPE_CHECKING + +from superset.app import create_app + +if TYPE_CHECKING: + from typing import Any + + from flask.testing import FlaskClient from superset.app import create_app app = create_app() + + +def login( + client: "FlaskClient[Any]", username: str = "admin", password: str = "general" +): + resp = client.post( + "/login/", + data=dict(username=username, password=password), + ).get_data(as_text=True) + assert "User confirmation needed" not in resp diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/conftest.py index bba43caaaf9..274aff81944 100644 --- a/tests/unit_tests/conftest.py +++ b/tests/unit_tests/conftest.py @@ -70,6 +70,7 @@ def app() -> Iterator[SupersetApp]: app.config.from_object("superset.config") app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://" + app.config["WTF_CSRF_ENABLED"] = False app.config["TESTING"] = True # ``superset.extensions.appbuilder`` is a singleton, and won't rebuild the diff --git a/tests/unit_tests/utils/urls_tests.py b/tests/unit_tests/utils/urls_tests.py index e3a8b75fa25..59c7841b5d9 100644 --- a/tests/unit_tests/utils/urls_tests.py +++ b/tests/unit_tests/utils/urls_tests.py @@ -53,7 +53,7 @@ def test_convert_dashboard_link() -> None: ("xpto://localhost:[3/1/", False), ], ) -def test_is_safe_url(url: str, is_safe: bool) -> None: +def test_is_safe_url(app_context: None, url: str, is_safe: bool) -> None: from superset import app from superset.utils.urls import is_safe_url