1
0
secret_loader/test/test_init.py

94 lines
2.9 KiB
Python
Raw Normal View History

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')
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
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'
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():
loader = secret_loader.SecretLoader()
2024-07-07 21:36:58 +00:00
assert loader.load('UNKNOWN') is None
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_')
2024-10-27 17:00:32 +00:00
def test_fail_to_load_from_password_store_due_to_status():
loader = secret_loader.SecretLoader(
PASS_STORE_SUBFOLDER='test', # noqa: S106
)
loader.pass_executable = '/usr/bin/false' # noqa: S105
with pytest.raises(
ValueError,
match='Failed to load secret with key:.*UNKNOWN.*',
):
assert loader.load_or_fail('UNKNOWN')
def test_load_empty_from_password_store():
loader = secret_loader.SecretLoader(
PASS_STORE_SUBFOLDER='test', # noqa: S106
)
loader.pass_executable = '/usr/bin/true' # noqa: S105
assert loader.load_or_fail('UNKNOWN') == ''
def test_fail_due_to_unknown_executable():
loader = secret_loader.SecretLoader(
PASS_STORE_SUBFOLDER='test', # noqa: S106
)
loader.pass_executable = '/not/an/executable' # noqa: S105
with pytest.raises(
ValueError,
match='Failed to load secret with key:.*UNKNOWN.*',
):
assert loader.load_or_fail('UNKNOWN')
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
)
with pytest.raises(
2024-10-27 16:49:06 +00:00
ValueError,
match='Failed to load secret with key:.*UNKNOWN.*',
) 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
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:49:06 +00:00
assert loader._convert_pass_process_result_to_password(0, b'') == '' # noqa: SLF001