|
1 | | -import os |
2 | | -import pytest |
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# Licensed under the Apache License, Version 2.0... |
| 3 | + |
3 | 4 | from unittest import mock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
4 | 8 | from google.auth import exceptions |
5 | | -# Assuming the provided code is in a file named google/auth/transport/aio/mtls_helper.py |
6 | | -from google.auth.transport.aio import mtls_helper |
| 9 | +from google.auth.aio.transport import mtls |
7 | 10 |
|
8 | 11 | CERT_DATA = b"client-cert" |
9 | 12 | KEY_DATA = b"client-key" |
10 | 13 |
|
11 | | -class TestMTLSHelper: |
12 | 14 |
|
13 | | - @mock.patch("os.path.expanduser") |
14 | | - @mock.patch("os.path.exists") |
| 15 | +class TestMTLS: |
| 16 | + @mock.patch("google.auth.aio.transport.mtls.path.expanduser") |
| 17 | + @mock.patch("google.auth.aio.transport.mtls.path.exists") |
15 | 18 | def test__check_config_path_exists(self, mock_exists, mock_expand): |
16 | 19 | mock_expand.side_effect = lambda x: x.replace("~", "/home/user") |
17 | 20 | mock_exists.return_value = True |
18 | | - |
19 | | - path = "/home/user/config.json" |
20 | | - result = mtls_helper._check_config_path("~/config.json") |
21 | | - |
22 | | - assert result == path |
23 | | - mock_exists.assert_called_with(path) |
24 | | - |
25 | | - @mock.patch("os.path.exists", return_value=False) |
| 21 | + |
| 22 | + input_path = "~/config.json" |
| 23 | + expected_path = "/home/user/config.json" |
| 24 | + result = mtls._check_config_path(input_path) |
| 25 | + |
| 26 | + assert result == expected_path |
| 27 | + mock_exists.assert_called_with(expected_path) |
| 28 | + |
| 29 | + @mock.patch("google.auth.aio.transport.mtls.path.exists", return_value=False) |
26 | 30 | def test__check_config_path_not_found(self, mock_exists): |
27 | | - result = mtls_helper._check_config_path("nonexistent.json") |
| 31 | + result = mtls._check_config_path("nonexistent.json") |
28 | 32 | assert result is None |
29 | 33 |
|
30 | | - @mock.patch("google.auth.transport.aio.mtls_helper._check_config_path") |
31 | | - @mock.patch("os.getenv") |
32 | | - def test_has_default_client_cert_source_default_path(self, mock_getenv, mock_check): |
33 | | - # Case 1: Default config path exists |
34 | | - mock_check.side_effect = lambda x: x if x == mtls_helper.CERTIFICATE_CONFIGURATION_DEFAULT_PATH else None |
35 | | - |
36 | | - assert mtls_helper.has_default_client_cert_source() is True |
37 | | - |
38 | | - @mock.patch("google.auth.transport.aio.mtls_helper._check_config_path") |
39 | | - @mock.patch("os.getenv") |
| 34 | + @mock.patch("google.auth.aio.transport.mtls._check_config_path") |
| 35 | + @mock.patch("google.auth.aio.transport.mtls.getenv") |
40 | 36 | def test_has_default_client_cert_source_env_var(self, mock_getenv, mock_check): |
41 | | - # Case 2: Default path doesn't exist, but env var path does |
| 37 | + # Mocking so the default path fails but the env var path succeeds |
42 | 38 | custom_path = "/custom/path.json" |
43 | | - mock_check.side_effect = lambda x: x if x == custom_path else None |
| 39 | + mock_check.side_effect = lambda x: custom_path if x == custom_path else None |
44 | 40 | mock_getenv.return_value = custom_path |
45 | | - |
46 | | - assert mtls_helper.has_default_client_cert_source() is True |
47 | 41 |
|
48 | | - @mock.patch("google.auth.transport.aio.mtls_helper._check_config_path", return_value=None) |
49 | | - def test_has_default_client_cert_source_none(self, mock_check): |
50 | | - assert mtls_helper.has_default_client_cert_source() is False |
| 42 | + assert mtls.has_default_client_cert_source() is True |
51 | 43 |
|
52 | 44 | @mock.patch("google.auth.transport._mtls_helper._get_workload_cert_and_key") |
53 | 45 | def test_get_client_ssl_credentials_success(self, mock_workload): |
54 | 46 | mock_workload.return_value = (CERT_DATA, KEY_DATA) |
55 | | - |
56 | | - success, cert, key, passphrase = mtls_helper.get_client_ssl_credentials() |
57 | | - |
| 47 | + |
| 48 | + success, cert, key, passphrase = mtls.get_client_ssl_credentials() |
| 49 | + |
58 | 50 | assert success is True |
59 | 51 | assert cert == CERT_DATA |
60 | 52 | assert key == KEY_DATA |
61 | 53 | assert passphrase is None |
62 | 54 |
|
63 | | - @mock.patch("google.auth.transport._mtls_helper._get_workload_cert_and_key", return_value=(None, None)) |
64 | | - def test_get_client_ssl_credentials_fail(self, mock_workload): |
65 | | - success, cert, key, passphrase = mtls_helper.get_client_ssl_credentials() |
66 | | - assert success is False |
67 | | - assert cert is None |
68 | | - |
69 | 55 | def test_get_client_cert_and_key_callback(self): |
70 | | - # Callback should take priority |
| 56 | + # The callback should be tried first and return immediately |
71 | 57 | callback = mock.Mock(return_value=(CERT_DATA, KEY_DATA)) |
72 | | - |
73 | | - success, cert, key = mtls_helper.get_client_cert_and_key(callback) |
74 | | - |
| 58 | + |
| 59 | + success, cert, key = mtls.get_client_cert_and_key(callback) |
| 60 | + |
75 | 61 | assert success is True |
76 | 62 | assert cert == CERT_DATA |
77 | 63 | assert key == KEY_DATA |
78 | 64 | callback.assert_called_once() |
79 | 65 |
|
80 | | - @mock.patch("google.auth.transport.aio.mtls_helper.get_client_ssl_credentials") |
| 66 | + @mock.patch("google.auth.aio.transport.mtls.get_client_ssl_credentials") |
81 | 67 | def test_get_client_cert_and_key_default(self, mock_get_ssl): |
| 68 | + # If no callback, it should call get_client_ssl_credentials |
82 | 69 | mock_get_ssl.return_value = (True, CERT_DATA, KEY_DATA, None) |
83 | | - |
84 | | - success, cert, key = mtls_helper.get_client_cert_and_key(None) |
85 | | - |
| 70 | + |
| 71 | + success, cert, key = mtls.get_client_cert_and_key(None) |
| 72 | + |
86 | 73 | assert success is True |
87 | 74 | assert cert == CERT_DATA |
88 | 75 | assert key == KEY_DATA |
| 76 | + mock_get_ssl.assert_called_with(generate_encrypted_key=False) |
| 77 | + |
| 78 | + @mock.patch("google.auth.transport._mtls_helper._get_workload_cert_and_key") |
| 79 | + def test_get_client_ssl_credentials_error(self, mock_workload): |
| 80 | + """Tests that ClientCertError is propagated correctly.""" |
| 81 | + # Setup the mock to raise the specific google-auth exception |
| 82 | + mock_workload.side_effect = exceptions.ClientCertError( |
| 83 | + "Failed to read metadata" |
| 84 | + ) |
| 85 | + |
| 86 | + # Verify that calling our function raises the same exception |
| 87 | + with pytest.raises(exceptions.ClientCertError, match="Failed to read metadata"): |
| 88 | + mtls.get_client_ssl_credentials() |
| 89 | + |
| 90 | + @mock.patch("google.auth.aio.transport.mtls.get_client_ssl_credentials") |
| 91 | + def test_get_client_cert_and_key_exception_propagation(self, mock_get_ssl): |
| 92 | + """Tests that get_client_cert_and_key propagates errors from its internal calls.""" |
| 93 | + mock_get_ssl.side_effect = exceptions.ClientCertError( |
| 94 | + "Underlying credentials failed" |
| 95 | + ) |
| 96 | + |
| 97 | + with pytest.raises( |
| 98 | + exceptions.ClientCertError, match="Underlying credentials failed" |
| 99 | + ): |
| 100 | + # Pass None for callback so it attempts to call get_client_ssl_credentials |
| 101 | + mtls.get_client_cert_and_key(client_cert_callback=None) |
0 commit comments