|
16 | 16 | # under the License. |
17 | 17 | from __future__ import annotations |
18 | 18 |
|
19 | | -from airflow.providers.keycloak.auth_manager.cli.definition import KEYCLOAK_AUTH_MANAGER_COMMANDS |
| 19 | +import argparse |
| 20 | +import importlib |
| 21 | +from unittest.mock import patch |
| 22 | + |
| 23 | +import pytest |
| 24 | + |
| 25 | +from airflow.cli import cli_parser |
| 26 | +from airflow.providers.keycloak.auth_manager.cli.definition import KEYCLOAK_AUTH_MANAGER_COMMANDS, Password |
| 27 | + |
| 28 | +from tests_common.test_utils.config import conf_vars |
20 | 29 |
|
21 | 30 |
|
22 | 31 | class TestKeycloakCliDefinition: |
23 | | - def test_aws_auth_manager_cli_commands(self): |
| 32 | + @classmethod |
| 33 | + def setup_class(cls): |
| 34 | + with conf_vars( |
| 35 | + { |
| 36 | + ( |
| 37 | + "core", |
| 38 | + "auth_manager", |
| 39 | + ): "airflow.providers.keycloak.auth_manager.keycloak_auth_manager.KeycloakAuthManager", |
| 40 | + } |
| 41 | + ): |
| 42 | + importlib.reload(cli_parser) |
| 43 | + cls.arg_parser = cli_parser.get_parser() |
| 44 | + |
| 45 | + def test_keycloak_auth_manager_cli_commands(self): |
24 | 46 | assert len(KEYCLOAK_AUTH_MANAGER_COMMANDS) == 4 |
| 47 | + |
| 48 | + @pytest.mark.parametrize( |
| 49 | + "command", |
| 50 | + ["create-scopes", "create-resources", "create-permissions", "create-all"], |
| 51 | + ) |
| 52 | + def test_password_with_explicit_value(self, command): |
| 53 | + """Test commands are defined correctly to allow passing password explicitly via --password value.""" |
| 54 | + params = [ |
| 55 | + "keycloak-auth-manager", |
| 56 | + command, |
| 57 | + "--username", |
| 58 | + "test", |
| 59 | + "--password", |
| 60 | + "my_password", |
| 61 | + ] |
| 62 | + args = self.arg_parser.parse_args(params) |
| 63 | + assert args.password == "my_password" |
| 64 | + |
| 65 | + @pytest.mark.parametrize( |
| 66 | + "command", |
| 67 | + ["create-scopes", "create-resources", "create-permissions", "create-all"], |
| 68 | + ) |
| 69 | + @patch("getpass.getpass", return_value="stdin_password") |
| 70 | + def test_password_from_stdin(self, mock_getpass, command): |
| 71 | + """Test commands are defined correctly to allow password prompting from stdin when --password has no value.""" |
| 72 | + params = [ |
| 73 | + "keycloak-auth-manager", |
| 74 | + command, |
| 75 | + "--username", |
| 76 | + "test", |
| 77 | + "--password", |
| 78 | + ] |
| 79 | + args = self.arg_parser.parse_args(params) |
| 80 | + mock_getpass.assert_called_once_with(prompt="Password: ") |
| 81 | + assert args.password == "stdin_password" |
| 82 | + |
| 83 | + |
| 84 | +class TestPasswordAction: |
| 85 | + """Tests for the Password argparse action.""" |
| 86 | + |
| 87 | + def test_password_with_explicit_value(self): |
| 88 | + """Test passing password explicitly via --password value.""" |
| 89 | + |
| 90 | + parser = argparse.ArgumentParser() |
| 91 | + parser.add_argument("--password", action=Password, nargs="?", dest="password") |
| 92 | + |
| 93 | + args = parser.parse_args(["--password", "my_password"]) |
| 94 | + assert args.password == "my_password" |
| 95 | + |
| 96 | + @patch("getpass.getpass", return_value="stdin_password") |
| 97 | + def test_password_from_stdin(self, mock_getpass): |
| 98 | + """Test password prompted from stdin when --password has no value.""" |
| 99 | + |
| 100 | + parser = argparse.ArgumentParser() |
| 101 | + parser.add_argument("--password", action=Password, nargs="?", dest="password") |
| 102 | + |
| 103 | + args = parser.parse_args(["--password"]) |
| 104 | + mock_getpass.assert_called_once_with(prompt="Password: ") |
| 105 | + assert args.password == "stdin_password" |
0 commit comments