|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Unit tests for mtls_utils.""" |
| 16 | + |
| 17 | +import os |
| 18 | +from unittest.mock import MagicMock |
| 19 | +from unittest.mock import patch |
| 20 | + |
| 21 | +from google.adk.utils import mtls_utils |
| 22 | +import pytest |
| 23 | + |
| 24 | +_DEFAULT_TEMPLATE = "service.{location}.rep.googleapis.com" |
| 25 | +_MTLS_TEMPLATE = "service.{location}.rep.mtls.googleapis.com" |
| 26 | +_LOCATION = "us-central1" |
| 27 | + |
| 28 | + |
| 29 | +class TestMtlsUtils: |
| 30 | + """Tests for mtls_utils functions.""" |
| 31 | + |
| 32 | + @patch("google.auth.transport.mtls.should_use_client_cert") |
| 33 | + def test_use_client_cert_effective_with_mtls_cert_true( |
| 34 | + self, mock_should_use_client_cert |
| 35 | + ): |
| 36 | + mock_should_use_client_cert.return_value = True |
| 37 | + assert mtls_utils.use_client_cert_effective() is True |
| 38 | + mock_should_use_client_cert.assert_called_once() |
| 39 | + |
| 40 | + @patch("google.auth.transport.mtls.should_use_client_cert") |
| 41 | + def test_use_client_cert_effective_with_mtls_cert_false( |
| 42 | + self, mock_should_use_client_cert |
| 43 | + ): |
| 44 | + mock_should_use_client_cert.return_value = False |
| 45 | + assert mtls_utils.use_client_cert_effective() is False |
| 46 | + mock_should_use_client_cert.assert_called_once() |
| 47 | + |
| 48 | + @patch("google.auth.transport.mtls.should_use_client_cert") |
| 49 | + @patch.dict("os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}) |
| 50 | + def test_use_client_cert_effective_fallback_true( |
| 51 | + self, mock_should_use_client_cert |
| 52 | + ): |
| 53 | + mock_should_use_client_cert.side_effect = AttributeError |
| 54 | + assert mtls_utils.use_client_cert_effective() is True |
| 55 | + |
| 56 | + @patch("google.auth.transport.mtls.should_use_client_cert") |
| 57 | + @patch.dict("os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}) |
| 58 | + def test_use_client_cert_effective_fallback_false( |
| 59 | + self, mock_should_use_client_cert |
| 60 | + ): |
| 61 | + mock_should_use_client_cert.side_effect = AttributeError |
| 62 | + assert mtls_utils.use_client_cert_effective() is False |
| 63 | + |
| 64 | + @patch("google.auth.transport.mtls.should_use_client_cert") |
| 65 | + @patch.dict("os.environ", {}, clear=True) |
| 66 | + def test_use_client_cert_effective_fallback_default_false( |
| 67 | + self, mock_should_use_client_cert |
| 68 | + ): |
| 69 | + mock_should_use_client_cert.side_effect = AttributeError |
| 70 | + assert mtls_utils.use_client_cert_effective() is False |
| 71 | + |
| 72 | + @patch("google.adk.utils.mtls_utils.use_client_cert_effective") |
| 73 | + @patch.dict("os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}) |
| 74 | + def test_get_api_endpoint_always(self, mock_use_client_cert): |
| 75 | + endpoint = mtls_utils.get_api_endpoint( |
| 76 | + _LOCATION, _DEFAULT_TEMPLATE, _MTLS_TEMPLATE |
| 77 | + ) |
| 78 | + assert endpoint == _MTLS_TEMPLATE.format(location=_LOCATION) |
| 79 | + mock_use_client_cert.assert_not_called() |
| 80 | + |
| 81 | + @patch("google.adk.utils.mtls_utils.use_client_cert_effective") |
| 82 | + @patch.dict("os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}) |
| 83 | + def test_get_api_endpoint_never(self, mock_use_client_cert): |
| 84 | + endpoint = mtls_utils.get_api_endpoint( |
| 85 | + _LOCATION, _DEFAULT_TEMPLATE, _MTLS_TEMPLATE |
| 86 | + ) |
| 87 | + assert endpoint == _DEFAULT_TEMPLATE.format(location=_LOCATION) |
| 88 | + mock_use_client_cert.assert_not_called() |
| 89 | + |
| 90 | + @patch("google.adk.utils.mtls_utils.use_client_cert_effective") |
| 91 | + @patch.dict("os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) |
| 92 | + def test_get_api_endpoint_auto_with_cert(self, mock_use_client_cert): |
| 93 | + mock_use_client_cert.return_value = True |
| 94 | + endpoint = mtls_utils.get_api_endpoint( |
| 95 | + _LOCATION, _DEFAULT_TEMPLATE, _MTLS_TEMPLATE |
| 96 | + ) |
| 97 | + assert endpoint == _MTLS_TEMPLATE.format(location=_LOCATION) |
| 98 | + mock_use_client_cert.assert_called_once() |
| 99 | + |
| 100 | + @patch("google.adk.utils.mtls_utils.use_client_cert_effective") |
| 101 | + @patch.dict("os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) |
| 102 | + def test_get_api_endpoint_auto_without_cert(self, mock_use_client_cert): |
| 103 | + mock_use_client_cert.return_value = False |
| 104 | + endpoint = mtls_utils.get_api_endpoint( |
| 105 | + _LOCATION, _DEFAULT_TEMPLATE, _MTLS_TEMPLATE |
| 106 | + ) |
| 107 | + assert endpoint == _DEFAULT_TEMPLATE.format(location=_LOCATION) |
| 108 | + mock_use_client_cert.assert_called_once() |
| 109 | + |
| 110 | + @patch("google.adk.utils.mtls_utils.use_client_cert_effective") |
| 111 | + @patch.dict("os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid_value"}) |
| 112 | + def test_get_api_endpoint_invalid_fallback_to_auto( |
| 113 | + self, mock_use_client_cert |
| 114 | + ): |
| 115 | + mock_use_client_cert.return_value = True |
| 116 | + endpoint = mtls_utils.get_api_endpoint( |
| 117 | + _LOCATION, _DEFAULT_TEMPLATE, _MTLS_TEMPLATE |
| 118 | + ) |
| 119 | + assert endpoint == _MTLS_TEMPLATE.format(location=_LOCATION) |
| 120 | + mock_use_client_cert.assert_called_once() |
0 commit comments