|
| 1 | +"""Unit tests for AuthenticationConfiguration model.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pydantic import ValidationError |
| 8 | + |
| 9 | +from models.config import ( |
| 10 | + AuthenticationConfiguration, |
| 11 | + Configuration, |
| 12 | + JwkConfiguration, |
| 13 | + LlamaStackConfiguration, |
| 14 | + ServiceConfiguration, |
| 15 | + UserDataCollection, |
| 16 | +) |
| 17 | + |
| 18 | +from constants import ( |
| 19 | + AUTH_MOD_NOOP, |
| 20 | + AUTH_MOD_K8S, |
| 21 | + AUTH_MOD_JWK_TOKEN, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def test_authentication_configuration() -> None: |
| 26 | + """Test the AuthenticationConfiguration constructor.""" |
| 27 | + |
| 28 | + auth_config = AuthenticationConfiguration( |
| 29 | + module=AUTH_MOD_NOOP, |
| 30 | + skip_tls_verification=False, |
| 31 | + k8s_ca_cert_path=None, |
| 32 | + k8s_cluster_api=None, |
| 33 | + ) |
| 34 | + assert auth_config is not None |
| 35 | + assert auth_config.module == AUTH_MOD_NOOP |
| 36 | + assert auth_config.skip_tls_verification is False |
| 37 | + assert auth_config.k8s_ca_cert_path is None |
| 38 | + assert auth_config.k8s_cluster_api is None |
| 39 | + |
| 40 | + # try to retrieve JWK configuration |
| 41 | + with pytest.raises( |
| 42 | + ValueError, |
| 43 | + match="JWK configuration is only available for JWK token authentication module", |
| 44 | + ): |
| 45 | + _ = auth_config.jwk_configuration |
| 46 | + |
| 47 | + |
| 48 | +def test_authentication_configuration_jwk_token() -> None: |
| 49 | + """Test the AuthenticationConfiguration with JWK token.""" |
| 50 | + |
| 51 | + auth_config = AuthenticationConfiguration( |
| 52 | + module=AUTH_MOD_JWK_TOKEN, |
| 53 | + skip_tls_verification=False, |
| 54 | + k8s_ca_cert_path=None, |
| 55 | + k8s_cluster_api=None, |
| 56 | + jwk_config=JwkConfiguration(url="http://foo.bar.baz"), |
| 57 | + ) |
| 58 | + assert auth_config is not None |
| 59 | + assert auth_config.module == AUTH_MOD_JWK_TOKEN |
| 60 | + assert auth_config.skip_tls_verification is False |
| 61 | + assert auth_config.k8s_ca_cert_path is None |
| 62 | + assert auth_config.k8s_cluster_api is None |
| 63 | + |
| 64 | + # try to retrieve JWK configuration |
| 65 | + assert auth_config.jwk_configuration is not None |
| 66 | + |
| 67 | + |
| 68 | +def test_authentication_configuration_jwk_token_but_insufficient_config() -> None: |
| 69 | + """Test the AuthenticationConfiguration with JWK token.""" |
| 70 | + |
| 71 | + with pytest.raises(ValidationError, match="JwkConfiguration"): |
| 72 | + AuthenticationConfiguration( |
| 73 | + module=AUTH_MOD_JWK_TOKEN, |
| 74 | + skip_tls_verification=False, |
| 75 | + k8s_ca_cert_path=None, |
| 76 | + k8s_cluster_api=None, |
| 77 | + jwk_config=JwkConfiguration(), |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +def test_authentication_configuration_jwk_token_but_not_config() -> None: |
| 82 | + """Test the AuthenticationConfiguration with JWK token.""" |
| 83 | + |
| 84 | + with pytest.raises( |
| 85 | + ValidationError, |
| 86 | + match="Value error, JWK configuration must be specified when using JWK token", |
| 87 | + ): |
| 88 | + AuthenticationConfiguration( |
| 89 | + module=AUTH_MOD_JWK_TOKEN, |
| 90 | + skip_tls_verification=False, |
| 91 | + k8s_ca_cert_path=None, |
| 92 | + k8s_cluster_api=None, |
| 93 | + # no JwkConfiguration |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +def test_authentication_configuration_jwk_broken_config() -> None: |
| 98 | + """Test the AuthenticationConfiguration with JWK set, but not configured.""" |
| 99 | + |
| 100 | + auth_config = AuthenticationConfiguration( |
| 101 | + module=AUTH_MOD_JWK_TOKEN, |
| 102 | + skip_tls_verification=False, |
| 103 | + k8s_ca_cert_path=None, |
| 104 | + k8s_cluster_api=None, |
| 105 | + jwk_config=JwkConfiguration(url="http://foo.bar.baz"), |
| 106 | + ) |
| 107 | + assert auth_config is not None |
| 108 | + |
| 109 | + # emulate broken config |
| 110 | + auth_config.jwk_config = None |
| 111 | + # try to retrieve JWK configuration |
| 112 | + |
| 113 | + with pytest.raises(ValueError, match="JWK configuration should not be None"): |
| 114 | + _ = auth_config.jwk_configuration |
| 115 | + |
| 116 | + |
| 117 | +def test_authentication_configuration_supported() -> None: |
| 118 | + """Test the AuthenticationConfiguration constructor.""" |
| 119 | + auth_config = AuthenticationConfiguration( |
| 120 | + module=AUTH_MOD_K8S, |
| 121 | + skip_tls_verification=False, |
| 122 | + k8s_ca_cert_path=None, |
| 123 | + k8s_cluster_api=None, |
| 124 | + ) |
| 125 | + assert auth_config is not None |
| 126 | + assert auth_config.module == AUTH_MOD_K8S |
| 127 | + assert auth_config.skip_tls_verification is False |
| 128 | + assert auth_config.k8s_ca_cert_path is None |
| 129 | + assert auth_config.k8s_cluster_api is None |
| 130 | + |
| 131 | + |
| 132 | +def test_authentication_configuration_module_unsupported() -> None: |
| 133 | + """Test the AuthenticationConfiguration constructor with module as None.""" |
| 134 | + with pytest.raises(ValidationError, match="Unsupported authentication module"): |
| 135 | + AuthenticationConfiguration( |
| 136 | + module="non-existing-module", |
| 137 | + skip_tls_verification=False, |
| 138 | + k8s_ca_cert_path=None, |
| 139 | + k8s_cluster_api=None, |
| 140 | + ) |
| 141 | + |
| 142 | + |
| 143 | +def test_authentication_configuration_in_config() -> None: |
| 144 | + """Test the authentication configuration in main config.""" |
| 145 | + cfg = Configuration( |
| 146 | + name="test_name", |
| 147 | + service=ServiceConfiguration(), |
| 148 | + llama_stack=LlamaStackConfiguration( |
| 149 | + use_as_library_client=True, |
| 150 | + library_client_config_path="tests/configuration/run.yaml", |
| 151 | + ), |
| 152 | + user_data_collection=UserDataCollection( |
| 153 | + feedback_enabled=False, feedback_storage=None |
| 154 | + ), |
| 155 | + mcp_servers=[], |
| 156 | + ) |
| 157 | + assert cfg.authentication is not None |
| 158 | + assert cfg.authentication.module == AUTH_MOD_NOOP |
| 159 | + assert cfg.authentication.skip_tls_verification is False |
| 160 | + assert cfg.authentication.k8s_ca_cert_path is None |
| 161 | + assert cfg.authentication.k8s_cluster_api is None |
| 162 | + |
| 163 | + cfg2 = Configuration( |
| 164 | + name="test_name", |
| 165 | + service=ServiceConfiguration(), |
| 166 | + llama_stack=LlamaStackConfiguration( |
| 167 | + use_as_library_client=True, |
| 168 | + library_client_config_path="tests/configuration/run.yaml", |
| 169 | + ), |
| 170 | + user_data_collection=UserDataCollection( |
| 171 | + feedback_enabled=False, feedback_storage=None |
| 172 | + ), |
| 173 | + mcp_servers=[], |
| 174 | + authentication=AuthenticationConfiguration( |
| 175 | + module=AUTH_MOD_K8S, |
| 176 | + skip_tls_verification=True, |
| 177 | + k8s_ca_cert_path="tests/configuration/server.crt", |
| 178 | + k8s_cluster_api=None, |
| 179 | + ), |
| 180 | + ) |
| 181 | + assert cfg2.authentication is not None |
| 182 | + assert cfg2.authentication.module == AUTH_MOD_K8S |
| 183 | + assert cfg2.authentication.skip_tls_verification is True |
| 184 | + assert cfg2.authentication.k8s_ca_cert_path == Path( |
| 185 | + "tests/configuration/server.crt" |
| 186 | + ) |
| 187 | + assert cfg2.authentication.k8s_cluster_api is None |
0 commit comments