|
| 1 | +import unittest.mock |
| 2 | +import pytest |
| 3 | +import tempfile |
| 4 | + |
| 5 | +from exasol.nb_connector.secret_store import Secrets |
| 6 | +from exasol.nb_connector.ai_lab_config import AILabConfig as CKey |
| 7 | +from exasol.nb_connector.extension_wrapper_common import encapsulate_bucketfs_credentials |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def filled_secrets(secrets) -> Secrets: |
| 12 | + secrets.save(CKey.db_host_name, 'localhost') |
| 13 | + secrets.save(CKey.db_port, '8888') |
| 14 | + secrets.save(CKey.db_user, 'user') |
| 15 | + secrets.save(CKey.db_password, 'password') |
| 16 | + secrets.save(CKey.bfs_port, '6666') |
| 17 | + secrets.save(CKey.bfs_encryption, 'True') |
| 18 | + secrets.save(CKey.bfs_service, 'bfsdefault') |
| 19 | + secrets.save(CKey.bfs_bucket, 'default') |
| 20 | + secrets.save(CKey.bfs_user, 'user'), |
| 21 | + secrets.save(CKey.bfs_password, 'password') |
| 22 | + return secrets |
| 23 | + |
| 24 | + |
| 25 | +@unittest.mock.patch("pyexasol.connect") |
| 26 | +def test_bucketfs_credentials_default(mock_connect, filled_secrets): |
| 27 | + |
| 28 | + path_in_bucket = 'location' |
| 29 | + |
| 30 | + mock_connection = unittest.mock.MagicMock() |
| 31 | + mock_connection.__enter__.return_value = mock_connection |
| 32 | + mock_connect.return_value = mock_connection |
| 33 | + |
| 34 | + encapsulate_bucketfs_credentials(filled_secrets, path_in_bucket=path_in_bucket, |
| 35 | + connection_name='whatever') |
| 36 | + expected_url = f"https://localhost:6666/default/{path_in_bucket};bfsdefault" |
| 37 | + |
| 38 | + mock_connection.execute.assert_called_once() |
| 39 | + query = mock_connection.execute.call_args_list[0].kwargs['query'] |
| 40 | + assert f"TO '{expected_url}'" in query |
| 41 | + |
| 42 | + |
| 43 | +@unittest.mock.patch("pyexasol.connect") |
| 44 | +def test_bucketfs_credentials_verify(mock_connect, filled_secrets): |
| 45 | + |
| 46 | + path_in_bucket = 'location' |
| 47 | + filled_secrets.save(CKey.cert_vld, 'yes') |
| 48 | + |
| 49 | + mock_connection = unittest.mock.MagicMock() |
| 50 | + mock_connection.__enter__.return_value = mock_connection |
| 51 | + mock_connect.return_value = mock_connection |
| 52 | + |
| 53 | + encapsulate_bucketfs_credentials(filled_secrets, path_in_bucket=path_in_bucket, |
| 54 | + connection_name='whatever') |
| 55 | + expected_url = f"https://localhost:6666/default/{path_in_bucket};bfsdefault#True" |
| 56 | + |
| 57 | + mock_connection.execute.assert_called_once() |
| 58 | + query = mock_connection.execute.call_args_list[0].kwargs['query'] |
| 59 | + assert f"TO '{expected_url}'" in query |
| 60 | + |
| 61 | + |
| 62 | +@unittest.mock.patch("pyexasol.connect") |
| 63 | +def test_bucketfs_credentials_ca(mock_connect, filled_secrets): |
| 64 | + |
| 65 | + with tempfile.NamedTemporaryFile() as f: |
| 66 | + path_in_bucket = 'location' |
| 67 | + filled_secrets.save(CKey.trusted_ca, f.name) |
| 68 | + filled_secrets.save(CKey.cert_vld, 'yes') |
| 69 | + |
| 70 | + mock_connection = unittest.mock.MagicMock() |
| 71 | + mock_connection.__enter__.return_value = mock_connection |
| 72 | + mock_connect.return_value = mock_connection |
| 73 | + |
| 74 | + encapsulate_bucketfs_credentials(filled_secrets, path_in_bucket=path_in_bucket, |
| 75 | + connection_name='whatever') |
| 76 | + expected_url = f"https://localhost:6666/default/{path_in_bucket};bfsdefault#False" |
| 77 | + |
| 78 | + mock_connection.execute.assert_called_once() |
| 79 | + query = mock_connection.execute.call_args_list[0].kwargs['query'] |
| 80 | + assert f"TO '{expected_url}'" in query |
0 commit comments