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_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( 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 assert ( loader._convert_pass_process_result_to_password(0, b'Hello\nWorld') == 'Hello' ) assert loader._convert_pass_process_result_to_password(0, b'') == ''