|
| 1 | +import pytest |
| 2 | + |
| 3 | +from essentials.exceptions import EnvironmentVariableNotFound |
| 4 | +from essentials.secrets import Secret |
| 5 | + |
| 6 | + |
| 7 | +def test_from_env_success(monkeypatch): |
| 8 | + """Test creating Secret from environment variable.""" |
| 9 | + monkeypatch.setenv("TEST_SECRET", "secret_value") |
| 10 | + secret = Secret.from_env("TEST_SECRET") |
| 11 | + assert secret.get_value() == "secret_value" |
| 12 | + |
| 13 | + |
| 14 | +def test_from_env_missing_raises_exception(): |
| 15 | + """Test that missing environment variable raises EnvironmentVariableNotFound.""" |
| 16 | + with pytest.raises(EnvironmentVariableNotFound): |
| 17 | + Secret.from_env("NON_EXISTENT_VAR").get_value() |
| 18 | + |
| 19 | + |
| 20 | +def test_from_plain_text_success(): |
| 21 | + """Test creating Secret from plain text.""" |
| 22 | + secret = Secret.from_plain_text("my_secret") |
| 23 | + assert secret.get_value() == "my_secret" |
| 24 | + |
| 25 | + |
| 26 | +def test_direct_constructor_with_env_var(monkeypatch): |
| 27 | + """Test direct constructor with environment variable reference.""" |
| 28 | + monkeypatch.setenv("TEST_SECRET", "secret_value") |
| 29 | + secret = Secret("$TEST_SECRET") |
| 30 | + assert secret.get_value() == "secret_value" |
| 31 | + |
| 32 | + |
| 33 | +def test_direct_constructor_with_direct_value(): |
| 34 | + """Test direct constructor with direct_value=True.""" |
| 35 | + secret = Secret("hardcoded_secret", direct_value=True) |
| 36 | + assert secret.get_value() == "hardcoded_secret" |
| 37 | + |
| 38 | + |
| 39 | +def test_hardcoded_secret_without_permission_raises_error(): |
| 40 | + """Test that hardcoded secrets without permission raise ValueError.""" |
| 41 | + with pytest.raises(ValueError, match="Hardcoded secrets are not allowed"): |
| 42 | + Secret("hardcoded_secret") |
| 43 | + |
| 44 | + |
| 45 | +def test_empty_secret_raises_error(monkeypatch): |
| 46 | + """Test that empty secret values raise ValueError.""" |
| 47 | + monkeypatch.setenv("EMPTY_SECRET", "") |
| 48 | + with pytest.raises(ValueError, match="Secret value must be a non-empty string"): |
| 49 | + Secret.from_env("EMPTY_SECRET") |
| 50 | + |
| 51 | + |
| 52 | +def test_str_representation_hides_value(monkeypatch): |
| 53 | + """Test that __str__ never exposes the actual value.""" |
| 54 | + monkeypatch.setenv("TEST_SECRET", "secret_value") |
| 55 | + secret = Secret.from_env("TEST_SECRET") |
| 56 | + assert str(secret) == "******" |
| 57 | + |
| 58 | + |
| 59 | +def test_repr_shows_env_var_name(monkeypatch): |
| 60 | + """Test that __repr__ shows environment variable name but not value.""" |
| 61 | + monkeypatch.setenv("TEST_SECRET", "secret_value") |
| 62 | + secret = Secret.from_env("TEST_SECRET") |
| 63 | + assert repr(secret) == "Secret.from_env('TEST_SECRET')" |
| 64 | + |
| 65 | + |
| 66 | +def test_repr_hides_hardcoded_value(): |
| 67 | + """Test that __repr__ hides hardcoded secret values.""" |
| 68 | + secret = Secret.from_plain_text("secret") |
| 69 | + assert repr(secret) == "Secret('******')" |
| 70 | + |
| 71 | + |
| 72 | +def test_equality_with_string(monkeypatch): |
| 73 | + """Test that Secret can be compared with strings.""" |
| 74 | + monkeypatch.setenv("TEST_SECRET", "secret_value") |
| 75 | + secret = Secret.from_env("TEST_SECRET") |
| 76 | + assert secret == "secret_value" |
| 77 | + assert secret != "wrong_value" |
| 78 | + |
| 79 | + |
| 80 | +def test_equality_with_another_secret(monkeypatch): |
| 81 | + """Test that Secrets can be compared with each other.""" |
| 82 | + monkeypatch.setenv("SECRET1", "same_value") |
| 83 | + monkeypatch.setenv("SECRET2", "same_value") |
| 84 | + monkeypatch.setenv("SECRET3", "different_value") |
| 85 | + |
| 86 | + secret1 = Secret.from_env("SECRET1") |
| 87 | + secret2 = Secret.from_env("SECRET2") |
| 88 | + secret3 = Secret.from_env("SECRET3") |
| 89 | + |
| 90 | + assert secret1 == secret2 |
| 91 | + assert secret1 != secret3 |
| 92 | + |
| 93 | + |
| 94 | +def test_equality_with_incompatible_type(monkeypatch): |
| 95 | + """Test that comparison with incompatible types returns NotImplemented.""" |
| 96 | + monkeypatch.setenv("TEST_SECRET", "secret_value") |
| 97 | + secret = Secret.from_env("TEST_SECRET") |
| 98 | + assert secret.__eq__(123) == NotImplemented |
| 99 | + assert secret != 123 |
| 100 | + |
| 101 | + |
| 102 | +def test_mixed_secret_sources_comparison(monkeypatch): |
| 103 | + """Test comparing secrets from different sources.""" |
| 104 | + monkeypatch.setenv("ENV_SECRET", "same_value") |
| 105 | + env_secret = Secret.from_env("ENV_SECRET") |
| 106 | + plain_secret = Secret.from_plain_text("same_value") |
| 107 | + |
| 108 | + assert env_secret == plain_secret |
| 109 | + |
| 110 | + |
| 111 | +def test_get_value_called_multiple_times(monkeypatch): |
| 112 | + """Test that get_value works consistently across multiple calls.""" |
| 113 | + monkeypatch.setenv("TEST_SECRET", "secret_value") |
| 114 | + secret = Secret.from_env("TEST_SECRET") |
| 115 | + |
| 116 | + assert secret.get_value() == "secret_value" |
| 117 | + assert secret.get_value() == "secret_value" |
| 118 | + |
| 119 | + |
| 120 | +def test_env_var_change_at_runtime(monkeypatch): |
| 121 | + """Test that Secret picks up environment variable changes.""" |
| 122 | + monkeypatch.setenv("DYNAMIC_SECRET", "initial_value") |
| 123 | + secret = Secret.from_env("DYNAMIC_SECRET") |
| 124 | + assert secret.get_value() == "initial_value" |
| 125 | + |
| 126 | + monkeypatch.setenv("DYNAMIC_SECRET", "changed_value") |
| 127 | + assert secret.get_value() == "changed_value" |
| 128 | + |
| 129 | + |
| 130 | +def test_constructor_validation_with_empty_env_var(monkeypatch): |
| 131 | + """Test constructor validation when env var exists but is empty.""" |
| 132 | + monkeypatch.setenv("EMPTY_VAR", "") |
| 133 | + with pytest.raises(ValueError, match="Secret value must be a non-empty string"): |
| 134 | + Secret("$EMPTY_VAR") |
| 135 | + |
| 136 | + |
| 137 | +def test_from_plain_text_with_empty_string(): |
| 138 | + """Test that from_plain_text with empty string raises ValueError.""" |
| 139 | + with pytest.raises(ValueError, match="Secret value must be a non-empty string"): |
| 140 | + Secret.from_plain_text("") |
0 commit comments