"""Tests for src/connectors/snowflake/utils.py.""" from __future__ import annotations from unittest.mock import MagicMock, patch from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives.serialization import ( Encoding, NoEncryption, PrivateFormat, ) from src.connectors.snowflake.utils import ( _encode_passphrase, get_private_key, get_private_key_from_str, ) class TestEncodePassphrase: """Tests for _encode_passphrase.""" def test_none_returns_none(self) -> None: assert _encode_passphrase(None) is None def test_string_returns_utf8_bytes(self) -> None: result = _encode_passphrase('my_passphrase') assert result == b'my_passphrase' def test_empty_string_returns_none(self) -> None: assert _encode_passphrase('') is None class TestGetPrivateKey: """Tests for get_private_key dispatch logic.""" def test_returns_none_when_no_args(self) -> None: assert get_private_key() is None def test_returns_none_when_all_none(self) -> None: assert get_private_key(None, None, None) is None @patch('src.connectors.snowflake.utils.get_private_key_from_str') def test_dispatches_to_str_when_key_provided( self, mock_from_str: MagicMock ) -> None: mock_from_str.return_value = b'der_bytes' result = get_private_key( private_key='pem_content', private_key_passphrase='pass' ) mock_from_str.assert_called_once_with('pem_content', 'pass') assert result == b'der_bytes' @patch('src.connectors.snowflake.utils.get_private_key_from_file') def test_dispatches_to_file_when_path_provided( self, mock_from_file: MagicMock ) -> None: mock_from_file.return_value = b'der_bytes' result = get_private_key( private_key_path='/path/to/key.p8', private_key_passphrase='pass' ) mock_from_file.assert_called_once_with('/path/to/key.p8', 'pass') assert result == b'der_bytes' @patch('src.connectors.snowflake.utils.get_private_key_from_str') @patch('src.connectors.snowflake.utils.get_private_key_from_file') def test_str_takes_priority_over_path( self, mock_from_file: MagicMock, mock_from_str: MagicMock ) -> None: mock_from_str.return_value = b'from_str' result = get_private_key( private_key='pem_content', private_key_path='/path/to/key.p8', ) mock_from_str.assert_called_once() mock_from_file.assert_not_called() assert result == b'from_str' @patch('src.connectors.snowflake.utils.get_private_key_from_str') def test_passphrase_forwarded_to_str(self, mock_from_str: MagicMock) -> None: mock_from_str.return_value = b'der_bytes' get_private_key(private_key='pem', private_key_passphrase='secret') _, called_passphrase = mock_from_str.call_args.args assert called_passphrase == 'secret' def _generate_pem() -> str: """Generate a throwaway RSA PEM string for testing.""" key = rsa.generate_private_key(public_exponent=65537, key_size=2048) return key.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption()).decode( 'utf-8' ) class TestGetPrivateKeyFromStr: """Tests for get_private_key_from_str with real PEM parsing.""" def test_real_newlines(self) -> None: pem = _generate_pem() result = get_private_key_from_str(pem) assert isinstance(result, bytes) assert len(result) > 0