|
| 1 | +"""Tests for the auth-principal portion of the environments.yaml validator. |
| 2 | +
|
| 3 | +Covers the rule that an env config's principal must carry exactly one of |
| 4 | +`user_id` or `service_account_id` — the same shape that downstream services |
| 5 | +(agentex-auth, SGP) expect on the wire. |
| 6 | +""" |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from agentex.lib.sdk.config.validation import ( |
| 11 | + EnvironmentsValidationError, |
| 12 | + validate_environments_config, |
| 13 | +) |
| 14 | +from agentex.lib.sdk.config.environment_config import ( |
| 15 | + AgentAuthConfig, |
| 16 | + AgentKubernetesConfig, |
| 17 | + AgentEnvironmentConfig, |
| 18 | + AgentEnvironmentsConfig, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def _config_with_principal(principal: dict) -> AgentEnvironmentsConfig: |
| 23 | + return AgentEnvironmentsConfig( |
| 24 | + schema_version="v1", |
| 25 | + environments={ |
| 26 | + "dev": AgentEnvironmentConfig( |
| 27 | + kubernetes=AgentKubernetesConfig(namespace="dev-ns"), |
| 28 | + auth=AgentAuthConfig(principal=principal), |
| 29 | + ) |
| 30 | + }, |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def test_user_only_principal_passes(): |
| 35 | + """Existing user_id-only configs continue to validate (backwards compat).""" |
| 36 | + config = _config_with_principal({"user_id": "73d0c8bd-4726-434c-9686-eb627d89f078", "account_id": "acct-1"}) |
| 37 | + |
| 38 | + validate_environments_config(config) |
| 39 | + |
| 40 | + |
| 41 | +def test_service_account_only_principal_passes(): |
| 42 | + """New service_account_id-only configs validate.""" |
| 43 | + config = _config_with_principal( |
| 44 | + {"service_account_id": "a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d", "account_id": "acct-1"} |
| 45 | + ) |
| 46 | + |
| 47 | + validate_environments_config(config) |
| 48 | + |
| 49 | + |
| 50 | +def test_principal_with_neither_id_is_rejected(): |
| 51 | + """A principal with no identity id fails fast with a clear error.""" |
| 52 | + config = _config_with_principal({"account_id": "acct-1"}) |
| 53 | + |
| 54 | + with pytest.raises(EnvironmentsValidationError) as exc_info: |
| 55 | + validate_environments_config(config) |
| 56 | + |
| 57 | + msg = str(exc_info.value) |
| 58 | + assert "user_id" in msg |
| 59 | + assert "service_account_id" in msg |
| 60 | + |
| 61 | + |
| 62 | +def test_principal_with_both_ids_is_rejected(): |
| 63 | + """Setting both ids is a config error — the principal must commit to one identity type.""" |
| 64 | + config = _config_with_principal( |
| 65 | + { |
| 66 | + "user_id": "73d0c8bd-4726-434c-9686-eb627d89f078", |
| 67 | + "service_account_id": "a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d", |
| 68 | + "account_id": "acct-1", |
| 69 | + } |
| 70 | + ) |
| 71 | + |
| 72 | + with pytest.raises(EnvironmentsValidationError) as exc_info: |
| 73 | + validate_environments_config(config) |
| 74 | + |
| 75 | + msg = str(exc_info.value) |
| 76 | + assert "only one of" in msg.lower() or "not both" in msg.lower() |
0 commit comments