import pytest import secret_loader def test_hardcoded(): loader = secret_loader.SecretLoader(ENV_KEY_PREFIX='TEST', KEY='VALUE') assert loader.load('ENV_KEY_PREFIX') == 'TEST' assert loader.load('KEY') == 'VALUE' assert loader.load_or_fail('ENV_KEY_PREFIX') == 'TEST' assert loader.load_or_fail('KEY') == 'VALUE' 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' def test_lookup_unknown(): loader = secret_loader.SecretLoader() 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_') def test_lookup_unknown_or_fail(): loader = secret_loader.SecretLoader( ENV_KEY_PREFIX='TEST', PASS_STORE_SUBFOLDER='test', # noqa: S106 ) with pytest.raises( 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() assert loader._convert_pass_process_result_to_password(1, b'') is None # noqa: SLF001 assert ( loader._convert_pass_process_result_to_password(0, b'Hello\nWorld') == 'Hello' # noqa: SLF001 ) assert loader._convert_pass_process_result_to_password(0, b'') == '' # noqa: SLF001