Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions airflow-ctl/src/airflowctl/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ def input_cli_config_file(self) -> str:
def save(self):
"""Save the credentials to keyring and URL to disk as a file."""
default_config_dir = os.environ.get("AIRFLOW_HOME", os.path.expanduser("~/airflow"))
if not os.path.exists(default_config_dir):
os.makedirs(default_config_dir)
os.makedirs(default_config_dir, exist_ok=True)
Comment thread
potiuk marked this conversation as resolved.
with open(os.path.join(default_config_dir, self.input_cli_config_file), "w") as f:
json.dump({"api_url": self.api_url}, f)
try:
Expand Down
49 changes: 26 additions & 23 deletions airflow-ctl/tests/airflow_ctl/ctl/commands/test_auth_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io
import json
import os
import tempfile
from unittest import mock
from unittest.mock import patch

Expand All @@ -40,32 +41,34 @@ class TestCliAuthCommands:
@patch.dict(os.environ, {"AIRFLOW_CLI_ENVIRONMENT": "TEST_AUTH_LOGIN"})
@patch("airflowctl.api.client.keyring")
@pytest.mark.flaky(reruns=3, reruns_delay=10)
def test_login(self, mock_keyring, api_client_maker):
api_client = api_client_maker(
path="/auth/token/cli",
response_json=self.login_response.model_dump(),
expected_http_status_code=201,
kind=ClientKind.AUTH,
)
def test_login(self, mock_keyring, api_client_maker, monkeypatch):
with tempfile.TemporaryDirectory() as temp_airflow_home:
monkeypatch.setenv("AIRFLOW_HOME", temp_airflow_home)

mock_keyring.set_password = mock.MagicMock()
mock_keyring.get_password.return_value = None
env = "TEST_AUTH_LOGIN"
api_client = api_client_maker(
path="/auth/token/cli",
response_json=self.login_response.model_dump(),
expected_http_status_code=201,
kind=ClientKind.AUTH,
)

auth_command.login(
self.parser.parse_args(["auth", "login", "--api-url", "http://localhost:8080"]),
api_client=api_client,
)
default_config_dir = os.environ.get("AIRFLOW_HOME", os.path.expanduser("~/airflow"))
assert os.path.exists(default_config_dir)
with open(os.path.join(default_config_dir, f"{env}.json")) as f:
assert json.load(f) == {
"api_url": "http://localhost:8080",
}
mock_keyring.set_password = mock.MagicMock()
mock_keyring.get_password.return_value = None
env = "TEST_AUTH_LOGIN"

mock_keyring.set_password.assert_called_once_with(
"airflowctl", "api_token-TEST_AUTH_LOGIN", "TEST_TOKEN"
)
auth_command.login(
self.parser.parse_args(["auth", "login", "--api-url", "http://localhost:8080"]),
api_client=api_client,
)

config_path = os.path.join(temp_airflow_home, f"{env}.json")
assert os.path.exists(config_path)
with open(config_path) as f:
assert json.load(f) == {"api_url": "http://localhost:8080"}

mock_keyring.set_password.assert_called_once_with(
"airflowctl", "api_token-TEST_AUTH_LOGIN", "TEST_TOKEN"
)

# Test auth login with username and password
@patch("airflowctl.api.client.keyring")
Expand Down