|
| 1 | +import json |
1 | 2 | import os |
2 | 3 | from contextlib import contextmanager |
3 | 4 | from unittest.mock import patch |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 |
|
7 | | -from stacklet.client.platform.config import StackletCredentials |
| 8 | +from stacklet.client.platform.config import StackletConfig, StackletCredentials |
| 9 | +from stacklet.client.platform.exceptions import ConfigValidationException |
8 | 10 |
|
9 | 11 |
|
10 | 12 | @pytest.fixture |
@@ -45,3 +47,87 @@ def test_api_token_prefer_env(self, creds): |
45 | 47 | creds.write("id token", "access token") |
46 | 48 | with self.env_api_key("a-b-c"): |
47 | 49 | assert creds.api_token() == "a-b-c" |
| 50 | + |
| 51 | + |
| 52 | +class TestStackletConfig: |
| 53 | + @pytest.fixture |
| 54 | + def config(self): |
| 55 | + return { |
| 56 | + "api": "https://api.example.com", |
| 57 | + "cognito_user_pool_id": "pool-123", |
| 58 | + "cognito_client_id": "client-456", |
| 59 | + "region": "us-east-1", |
| 60 | + "idp_id": "idp-789", |
| 61 | + "auth_url": "https://auth.example.com", |
| 62 | + "cubejs": "https://cubejs.example.com", |
| 63 | + } |
| 64 | + |
| 65 | + def test_validate_validpresent(self, config): |
| 66 | + StackletConfig.validate(config) |
| 67 | + |
| 68 | + @pytest.mark.parametrize( |
| 69 | + "missing_field", |
| 70 | + [ |
| 71 | + "api", |
| 72 | + "cognito_user_pool_id", |
| 73 | + "cognito_client_id", |
| 74 | + "region", |
| 75 | + "idp_id", |
| 76 | + "auth_url", |
| 77 | + "cubejs", |
| 78 | + ], |
| 79 | + ) |
| 80 | + def test_validate_missing_required_field(self, config, missing_field): |
| 81 | + config_copy = config.copy() |
| 82 | + del config_copy[missing_field] |
| 83 | + with pytest.raises(ConfigValidationException) as err: |
| 84 | + StackletConfig.validate(config_copy) |
| 85 | + assert f"'{missing_field}' is a required property" in str(err.value) |
| 86 | + |
| 87 | + def test_from_file_valid_config(self, config, tmp_path): |
| 88 | + config_file = tmp_path / "config.json" |
| 89 | + config_file.write_text(json.dumps(config)) |
| 90 | + stacklet_config = StackletConfig.from_file(config_file) |
| 91 | + assert stacklet_config.api == config["api"] |
| 92 | + assert stacklet_config.cognito_user_pool_id == config["cognito_user_pool_id"] |
| 93 | + assert stacklet_config.cognito_client_id == config["cognito_client_id"] |
| 94 | + assert stacklet_config.region == config["region"] |
| 95 | + assert stacklet_config.idp_id == config["idp_id"] |
| 96 | + assert stacklet_config.auth_url == config["auth_url"] |
| 97 | + assert stacklet_config.cubejs == config["cubejs"] |
| 98 | + |
| 99 | + def test_from_file_missing_file(self, tmp_path): |
| 100 | + config_file = tmp_path / "nonexistent.json" |
| 101 | + with pytest.raises(ConfigValidationException): |
| 102 | + StackletConfig.from_file(config_file) |
| 103 | + |
| 104 | + def test_from_file_invalid_config(self, tmp_path): |
| 105 | + config_file = tmp_path / "config.json" |
| 106 | + incomplete_config = {"api": "https://api.example.com"} |
| 107 | + config_file.write_text(json.dumps(incomplete_config)) |
| 108 | + with pytest.raises(ConfigValidationException): |
| 109 | + StackletConfig.from_file(config_file) |
| 110 | + |
| 111 | + def test_to_file(self, config, tmp_path): |
| 112 | + config_file = tmp_path / "output.json" |
| 113 | + stacklet_config = StackletConfig(**config) |
| 114 | + stacklet_config.to_file(config_file) |
| 115 | + assert config_file.exists() |
| 116 | + |
| 117 | + loaded_config = StackletConfig.from_file(config_file) |
| 118 | + assert loaded_config.api == config["api"] |
| 119 | + assert loaded_config.cognito_user_pool_id == config["cognito_user_pool_id"] |
| 120 | + assert loaded_config.cognito_client_id == config["cognito_client_id"] |
| 121 | + assert loaded_config.region == config["region"] |
| 122 | + assert loaded_config.idp_id == config["idp_id"] |
| 123 | + assert loaded_config.auth_url == config["auth_url"] |
| 124 | + assert loaded_config.cubejs == config["cubejs"] |
| 125 | + |
| 126 | + def test_to_file_creates_parent_directories(self, config, tmp_path): |
| 127 | + config_file = tmp_path / "nested" / "dir" / "config.json" |
| 128 | + stacklet_config = StackletConfig(**config) |
| 129 | + stacklet_config.to_file(config_file) |
| 130 | + assert config_file.exists() |
| 131 | + |
| 132 | + loaded_config = StackletConfig.from_file(config_file) |
| 133 | + assert loaded_config.api == config["api"] |
0 commit comments