|
| 1 | +import os |
| 2 | + |
| 3 | +from pydantic import ValidationError |
| 4 | +import pytest |
| 5 | +from web3_google_hsm.accounts.gcp_kms_account import GCPKmsAccount |
| 6 | +from web3_google_hsm.config import BaseConfig |
| 7 | +import json |
| 8 | + |
| 9 | +# Define required environment variables |
| 10 | +REQUIRED_ENV_VARS = { |
| 11 | + "GOOGLE_CLOUD_PROJECT": os.getenv("GOOGLE_CLOUD_PROJECT"), |
| 12 | + "GOOGLE_CLOUD_REGION": os.getenv("GOOGLE_CLOUD_REGION"), |
| 13 | + "KEY_RING": os.getenv("KEY_RING"), |
| 14 | + "KEY_NAME": os.getenv("KEY_NAME"), |
| 15 | + "GCP_ADC_CREDENTIALS_STRING": os.getenv("GCP_ADC_CREDENTIALS_STRING"), |
| 16 | +} |
| 17 | + |
| 18 | +# Skip all tests if any required env var is missing |
| 19 | +missing_vars = [k for k, v in REQUIRED_ENV_VARS.items() if not v] |
| 20 | +pytestmark = pytest.mark.skipif( |
| 21 | + bool(missing_vars), |
| 22 | + reason=f"Missing required environment variables: {', '.join(missing_vars)}" |
| 23 | +) |
| 24 | + |
| 25 | +def test_account_initialization_with_both(): |
| 26 | + """Test initializing account with both config and credentials.""" |
| 27 | + # Load credentials from GCP_ADC_CREDENTIALS_STRING env var |
| 28 | + credentials = json.loads(os.environ["GCP_ADC_CREDENTIALS_STRING"]) |
| 29 | + |
| 30 | + # Create config from environment |
| 31 | + config = BaseConfig.from_env() |
| 32 | + |
| 33 | + # Initialize account with both |
| 34 | + account = GCPKmsAccount(config=config, credentials=credentials) |
| 35 | + |
| 36 | + # Verify initialization |
| 37 | + assert account._client is not None |
| 38 | + assert account._settings is not None |
| 39 | + assert account.key_path is not None |
| 40 | + assert account.address.startswith("0x") |
| 41 | + |
| 42 | + # Test basic functionality |
| 43 | + message = "Test message" |
| 44 | + signature = account.sign_message(message) |
| 45 | + assert signature.v in (27, 28) |
| 46 | + assert len(signature.r) == 32 |
| 47 | + assert len(signature.s) == 32 |
| 48 | + |
| 49 | +def test_account_initialization_with_neither(): |
| 50 | + """Test initializing account with neither config nor credentials (using env vars).""" |
| 51 | + # Initialize account without explicit config or credentials |
| 52 | + account = GCPKmsAccount() |
| 53 | + |
| 54 | + # Verify initialization |
| 55 | + assert account._client is not None |
| 56 | + assert account._settings is not None # Should be created from env |
| 57 | + assert account.key_path is not None |
| 58 | + assert account.address.startswith("0x") |
| 59 | + |
| 60 | + # Test basic functionality |
| 61 | + message = "Test message" |
| 62 | + signature = account.sign_message(message) |
| 63 | + assert signature.v in (27, 28) |
| 64 | + assert len(signature.r) == 32 |
| 65 | + assert len(signature.s) == 32 |
| 66 | + |
| 67 | +def test_fail_account_initialization_with_only_config(monkeypatch): |
| 68 | + """Test that initializing with only config raises error.""" |
| 69 | + env_vars_to_clear = [ |
| 70 | + "GOOGLE_CLOUD_PROJECT", |
| 71 | + "GOOGLE_CLOUD_REGION", |
| 72 | + "KEY_RING", |
| 73 | + "KEY_NAME", |
| 74 | + "GOOGLE_APPLICATION_CREDENTIALS", |
| 75 | + "GCP_ADC_CREDENTIALS_STRING" |
| 76 | + ] |
| 77 | + |
| 78 | + for env_var in env_vars_to_clear: |
| 79 | + monkeypatch.delenv(env_var, raising=False) |
| 80 | + |
| 81 | + with pytest.raises(ValidationError): |
| 82 | + config = BaseConfig.from_env() |
| 83 | + GCPKmsAccount(config=config) |
| 84 | + |
| 85 | +def test_fail_account_initialization_with_only_credentials(monkeypatch): |
| 86 | + """Test that initializing with only credentials raises error.""" |
| 87 | + env_vars_to_clear = [ |
| 88 | + "GOOGLE_CLOUD_PROJECT", |
| 89 | + "GOOGLE_CLOUD_REGION", |
| 90 | + "KEY_RING", |
| 91 | + "KEY_NAME", |
| 92 | + ] |
| 93 | + |
| 94 | + for env_var in env_vars_to_clear: |
| 95 | + monkeypatch.delenv(env_var, raising=False) |
| 96 | + credentials = json.loads(os.environ["GCP_ADC_CREDENTIALS_STRING"]) |
| 97 | + |
| 98 | + with pytest.raises(ValidationError): |
| 99 | + GCPKmsAccount(credentials=credentials) |
| 100 | + |
| 101 | +def test_key_path_matches_config(monkeypatch): |
| 102 | + """Test that the key path matches the config values.""" |
| 103 | + # Load both config and credentials |
| 104 | + credentials = json.loads(os.environ["GCP_ADC_CREDENTIALS_STRING"]) |
| 105 | + |
| 106 | + config = BaseConfig.from_env() |
| 107 | + account = GCPKmsAccount(config=config, credentials=credentials) |
| 108 | + |
| 109 | + # Verify key path contains all the expected components |
| 110 | + assert config.project_id in account.key_path |
| 111 | + assert config.location_id in account.key_path |
| 112 | + assert config.key_ring_id in account.key_path |
| 113 | + assert config.key_id in account.key_path |
0 commit comments