|
16 | 16 | # under the License.
|
17 | 17 | from __future__ import annotations
|
18 | 18 |
|
| 19 | +import os |
| 20 | + |
19 | 21 | import pytest
|
20 | 22 |
|
| 23 | +import airflow |
| 24 | +from airflow.configuration import conf |
| 25 | +from airflow.exceptions import AirflowConfigException |
| 26 | +from airflow.www.extensions.init_appbuilder import AirflowAppBuilder |
| 27 | +from airflow.www.session import AirflowDatabaseSessionInterface |
| 28 | + |
21 | 29 | from tests_common.test_utils.compat import ignore_provider_compatibility_error
|
| 30 | +from tests_common.test_utils.config import conf_vars |
22 | 31 |
|
23 | 32 | with ignore_provider_compatibility_error("2.9.0+", __file__):
|
24 | 33 | from airflow.providers.fab.auth_manager.cli_commands.utils import get_application_builder
|
25 | 34 |
|
26 |
| -from airflow.www.extensions.init_appbuilder import AirflowAppBuilder |
27 |
| - |
28 | 35 | pytestmark = pytest.mark.db_test
|
29 | 36 |
|
30 | 37 |
|
| 38 | +@pytest.fixture |
| 39 | +def flask_app(): |
| 40 | + """Fixture to set up the Flask app with the necessary configuration.""" |
| 41 | + # Get the webserver config file path |
| 42 | + webserver_config = conf.get_mandatory_value("webserver", "config_file") |
| 43 | + |
| 44 | + with get_application_builder() as appbuilder: |
| 45 | + flask_app = appbuilder.app |
| 46 | + |
| 47 | + # Load webserver configuration |
| 48 | + flask_app.config.from_pyfile(webserver_config, silent=True) |
| 49 | + |
| 50 | + yield flask_app |
| 51 | + |
| 52 | + |
31 | 53 | class TestCliUtils:
|
32 | 54 | def test_get_application_builder(self):
|
| 55 | + """Test that get_application_builder returns an AirflowAppBuilder instance.""" |
33 | 56 | with get_application_builder() as appbuilder:
|
34 | 57 | assert isinstance(appbuilder, AirflowAppBuilder)
|
| 58 | + |
| 59 | + def test_sqlalchemy_uri_configured(self, flask_app): |
| 60 | + """Test that the SQLALCHEMY_DATABASE_URI is correctly set in the Flask app.""" |
| 61 | + sqlalchemy_uri = conf.get("database", "SQL_ALCHEMY_CONN") |
| 62 | + |
| 63 | + # Assert that the SQLAlchemy URI is correctly set |
| 64 | + assert sqlalchemy_uri == flask_app.config["SQLALCHEMY_DATABASE_URI"] |
| 65 | + |
| 66 | + def test_relative_path_sqlite_raises_exception(self): |
| 67 | + """Test that a relative SQLite path raises an AirflowConfigException.""" |
| 68 | + # Directly simulate the configuration for relative SQLite path |
| 69 | + with conf_vars({("database", "SQL_ALCHEMY_CONN"): "sqlite://relative/path"}): |
| 70 | + with pytest.raises(AirflowConfigException, match="Cannot use relative path"): |
| 71 | + with get_application_builder(): |
| 72 | + pass |
| 73 | + |
| 74 | + def test_static_folder_exists(self, flask_app): |
| 75 | + """Test that the static folder is correctly configured in the Flask app.""" |
| 76 | + static_folder = os.path.join(os.path.dirname(airflow.__file__), "www", "static") |
| 77 | + assert flask_app.static_folder == static_folder |
| 78 | + |
| 79 | + def test_database_auth_backend_in_session(self, flask_app): |
| 80 | + """Test that the database is used for session management when AUTH_BACKEND is set to 'database'.""" |
| 81 | + with get_application_builder() as appbuilder: |
| 82 | + flask_app = appbuilder.app |
| 83 | + # Ensure that the correct session interface is set (for 'database' auth backend) |
| 84 | + assert isinstance(flask_app.session_interface, AirflowDatabaseSessionInterface) |
0 commit comments