2024-10-27 16:47:07 +00:00
|
|
|
import pytest
|
2024-06-12 21:02:31 +00:00
|
|
|
|
2024-10-27 16:49:06 +00:00
|
|
|
import secret_loader
|
|
|
|
|
2024-06-12 21:36:32 +00:00
|
|
|
|
2024-07-16 19:39:58 +00:00
|
|
|
def test_hardcoded():
|
2024-07-22 22:45:59 +00:00
|
|
|
loader = secret_loader.SecretLoader(ENV_KEY_PREFIX='TEST', KEY='VALUE')
|
2024-07-07 22:00:50 +00:00
|
|
|
assert loader.load('ENV_KEY_PREFIX') == 'TEST'
|
2024-06-12 21:36:21 +00:00
|
|
|
assert loader.load('KEY') == 'VALUE'
|
2024-07-07 21:36:58 +00:00
|
|
|
|
2024-10-27 16:47:07 +00:00
|
|
|
assert loader.load_or_fail('ENV_KEY_PREFIX') == 'TEST'
|
|
|
|
assert loader.load_or_fail('KEY') == 'VALUE'
|
|
|
|
|
2024-07-22 22:45:59 +00:00
|
|
|
|
2024-09-04 17:37:06 +00:00
|
|
|
def test_lookup_secrets_dir():
|
|
|
|
loader = secret_loader.SecretLoader(SECRETS_DIRECTORY='test/example-secrets')
|
|
|
|
assert loader.load('MY_SECRET') == 'HELLO SECRET'
|
2024-10-27 16:47:07 +00:00
|
|
|
assert loader.load_or_fail('MY_SECRET') == 'HELLO SECRET'
|
2024-09-04 17:37:06 +00:00
|
|
|
|
|
|
|
|
2024-07-07 21:36:58 +00:00
|
|
|
def test_lookup_unknown():
|
2024-07-07 22:00:50 +00:00
|
|
|
loader = secret_loader.SecretLoader()
|
2024-07-07 21:36:58 +00:00
|
|
|
assert loader.load('UNKNOWN') is None
|
2024-10-27 16:47:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_fail_hardcoded_prefix_lowercase():
|
|
|
|
with pytest.raises(ValueError, match='Prefix must be uppercase'):
|
|
|
|
secret_loader.SecretLoader(ENV_KEY_PREFIX='test')
|
|
|
|
|
|
|
|
|
|
|
|
def test_fail_hardcoded_prefix_with_trailing_underscore():
|
|
|
|
with pytest.raises(
|
|
|
|
ValueError,
|
|
|
|
match=r'Prefix must not end with "_" \(this will be added automatically\)',
|
|
|
|
):
|
|
|
|
secret_loader.SecretLoader(ENV_KEY_PREFIX='TEST_')
|
|
|
|
|
|
|
|
|
|
|
|
def test_lookup_unknown_or_fail():
|
|
|
|
loader = secret_loader.SecretLoader(
|
2024-10-27 16:49:06 +00:00
|
|
|
ENV_KEY_PREFIX='TEST',
|
|
|
|
PASS_STORE_SUBFOLDER='test', # noqa: S106
|
2024-10-27 16:47:07 +00:00
|
|
|
)
|
|
|
|
with pytest.raises(
|
2024-10-27 16:49:06 +00:00
|
|
|
ValueError,
|
|
|
|
match='Failed to load secret with key:.*UNKNOWN.*',
|
2024-10-27 16:47:07 +00:00
|
|
|
) as e:
|
|
|
|
assert loader.load_or_fail('UNKNOWN')
|
|
|
|
|
|
|
|
assert 'Write secret to file' in str(e.value)
|
|
|
|
assert 'Add environment variable pointing to written secret' in str(e.value)
|
|
|
|
assert 'Write secret to password store entry' in str(e.value)
|
|
|
|
|
|
|
|
|
|
|
|
def test_convert_process():
|
|
|
|
loader = secret_loader.SecretLoader()
|
2024-10-27 16:49:06 +00:00
|
|
|
assert loader._convert_pass_process_result_to_password(1, b'') is None # noqa: SLF001
|
2024-10-27 16:47:07 +00:00
|
|
|
assert (
|
2024-10-27 16:49:06 +00:00
|
|
|
loader._convert_pass_process_result_to_password(0, b'Hello\nWorld') == 'Hello' # noqa: SLF001
|
2024-10-27 16:47:07 +00:00
|
|
|
)
|
2024-10-27 16:49:06 +00:00
|
|
|
assert loader._convert_pass_process_result_to_password(0, b'') == '' # noqa: SLF001
|