|
| 1 | +# Copyright 2025 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 the SecretManagerClient.""" |
| 16 | + |
| 17 | +import json |
| 18 | +from unittest.mock import MagicMock |
| 19 | +from unittest.mock import patch |
| 20 | + |
| 21 | +from google.adk.tools.apihub_tool.clients.secret_client import SecretManagerClient |
| 22 | +import pytest |
| 23 | + |
| 24 | +import google |
| 25 | + |
| 26 | + |
| 27 | +class TestSecretManagerClient: |
| 28 | + """Tests for the SecretManagerClient class.""" |
| 29 | + |
| 30 | + @patch("google.cloud.secretmanager.SecretManagerServiceClient") |
| 31 | + @patch( |
| 32 | + "google.adk.tools.apihub_tool.clients.secret_client.default_service_credential" |
| 33 | + ) |
| 34 | + def test_init_with_default_credentials( |
| 35 | + self, mock_default_service_credential, mock_secret_manager_client |
| 36 | + ): |
| 37 | + """Test initialization with default credentials.""" |
| 38 | + # Setup |
| 39 | + mock_credentials = MagicMock() |
| 40 | + mock_default_service_credential.return_value = ( |
| 41 | + mock_credentials, |
| 42 | + "test-project", |
| 43 | + ) |
| 44 | + |
| 45 | + # Execute |
| 46 | + client = SecretManagerClient() |
| 47 | + |
| 48 | + # Verify |
| 49 | + mock_default_service_credential.assert_called_once_with( |
| 50 | + scopes=["https://www.googleapis.com/auth/cloud-platform"] |
| 51 | + ) |
| 52 | + mock_secret_manager_client.assert_called_once_with( |
| 53 | + credentials=mock_credentials |
| 54 | + ) |
| 55 | + assert client._credentials == mock_credentials |
| 56 | + assert client._client == mock_secret_manager_client.return_value |
| 57 | + |
| 58 | + @patch("google.cloud.secretmanager.SecretManagerServiceClient") |
| 59 | + @patch("google.oauth2.service_account.Credentials.from_service_account_info") |
| 60 | + def test_init_with_service_account_json( |
| 61 | + self, mock_from_service_account_info, mock_secret_manager_client |
| 62 | + ): |
| 63 | + """Test initialization with service account JSON.""" |
| 64 | + # Setup |
| 65 | + mock_credentials = MagicMock() |
| 66 | + mock_from_service_account_info.return_value = mock_credentials |
| 67 | + service_account_json = json.dumps({ |
| 68 | + "type": "service_account", |
| 69 | + "project_id": "test-project", |
| 70 | + "private_key_id": "key-id", |
| 71 | + "private_key": "private-key", |
| 72 | + "client_email": "test@example.com", |
| 73 | + }) |
| 74 | + |
| 75 | + # Execute |
| 76 | + client = SecretManagerClient(service_account_json=service_account_json) |
| 77 | + |
| 78 | + # Verify |
| 79 | + mock_from_service_account_info.assert_called_once_with( |
| 80 | + json.loads(service_account_json) |
| 81 | + ) |
| 82 | + mock_secret_manager_client.assert_called_once_with( |
| 83 | + credentials=mock_credentials |
| 84 | + ) |
| 85 | + assert client._credentials == mock_credentials |
| 86 | + assert client._client == mock_secret_manager_client.return_value |
| 87 | + |
| 88 | + @patch("google.cloud.secretmanager.SecretManagerServiceClient") |
| 89 | + def test_init_with_auth_token(self, mock_secret_manager_client): |
| 90 | + """Test initialization with auth token.""" |
| 91 | + # Setup |
| 92 | + auth_token = "test-token" |
| 93 | + mock_credentials = MagicMock() |
| 94 | + |
| 95 | + # Mock the entire credentials creation process |
| 96 | + with ( |
| 97 | + patch("google.auth.credentials.Credentials") as mock_credentials_class, |
| 98 | + patch("google.auth.transport.requests.Request") as mock_request, |
| 99 | + ): |
| 100 | + # Configure the mock to return our mock_credentials when instantiated |
| 101 | + mock_credentials_class.return_value = mock_credentials |
| 102 | + |
| 103 | + # Execute |
| 104 | + client = SecretManagerClient(auth_token=auth_token) |
| 105 | + |
| 106 | + # Verify |
| 107 | + mock_credentials.refresh.assert_called_once() |
| 108 | + mock_secret_manager_client.assert_called_once_with( |
| 109 | + credentials=mock_credentials |
| 110 | + ) |
| 111 | + assert client._credentials == mock_credentials |
| 112 | + assert client._client == mock_secret_manager_client.return_value |
| 113 | + |
| 114 | + @patch( |
| 115 | + "google.adk.tools.apihub_tool.clients.secret_client.default_service_credential" |
| 116 | + ) |
| 117 | + def test_init_with_default_credentials_error( |
| 118 | + self, mock_default_service_credential |
| 119 | + ): |
| 120 | + """Test initialization with default credentials that fails.""" |
| 121 | + # Setup |
| 122 | + mock_default_service_credential.side_effect = Exception("Auth error") |
| 123 | + |
| 124 | + # Execute and verify |
| 125 | + with pytest.raises( |
| 126 | + ValueError, |
| 127 | + match="error occurred while trying to use default credentials", |
| 128 | + ): |
| 129 | + SecretManagerClient() |
| 130 | + |
| 131 | + def test_init_with_invalid_service_account_json(self): |
| 132 | + """Test initialization with invalid service account JSON.""" |
| 133 | + # Execute and verify |
| 134 | + with pytest.raises(ValueError, match="Invalid service account JSON"): |
| 135 | + SecretManagerClient(service_account_json="invalid-json") |
| 136 | + |
| 137 | + @patch("google.cloud.secretmanager.SecretManagerServiceClient") |
| 138 | + @patch( |
| 139 | + "google.adk.tools.apihub_tool.clients.secret_client.default_service_credential" |
| 140 | + ) |
| 141 | + def test_get_secret( |
| 142 | + self, mock_default_service_credential, mock_secret_manager_client |
| 143 | + ): |
| 144 | + """Test getting a secret.""" |
| 145 | + # Setup |
| 146 | + mock_credentials = MagicMock() |
| 147 | + mock_default_service_credential.return_value = ( |
| 148 | + mock_credentials, |
| 149 | + "test-project", |
| 150 | + ) |
| 151 | + |
| 152 | + mock_client = MagicMock() |
| 153 | + mock_secret_manager_client.return_value = mock_client |
| 154 | + mock_response = MagicMock() |
| 155 | + mock_response.payload.data.decode.return_value = "secret-value" |
| 156 | + mock_client.access_secret_version.return_value = mock_response |
| 157 | + |
| 158 | + # Execute - use default credentials instead of auth_token |
| 159 | + client = SecretManagerClient() |
| 160 | + result = client.get_secret( |
| 161 | + "projects/test-project/secrets/test-secret/versions/latest" |
| 162 | + ) |
| 163 | + |
| 164 | + # Verify |
| 165 | + assert result == "secret-value" |
| 166 | + mock_client.access_secret_version.assert_called_once_with( |
| 167 | + name="projects/test-project/secrets/test-secret/versions/latest" |
| 168 | + ) |
| 169 | + mock_response.payload.data.decode.assert_called_once_with("UTF-8") |
| 170 | + |
| 171 | + @patch("google.cloud.secretmanager.SecretManagerServiceClient") |
| 172 | + @patch( |
| 173 | + "google.adk.tools.apihub_tool.clients.secret_client.default_service_credential" |
| 174 | + ) |
| 175 | + def test_get_secret_error( |
| 176 | + self, mock_default_service_credential, mock_secret_manager_client |
| 177 | + ): |
| 178 | + """Test getting a secret that fails.""" |
| 179 | + # Setup |
| 180 | + mock_credentials = MagicMock() |
| 181 | + mock_default_service_credential.return_value = ( |
| 182 | + mock_credentials, |
| 183 | + "test-project", |
| 184 | + ) |
| 185 | + |
| 186 | + mock_client = MagicMock() |
| 187 | + mock_secret_manager_client.return_value = mock_client |
| 188 | + mock_client.access_secret_version.side_effect = Exception("Secret error") |
| 189 | + |
| 190 | + # Execute and verify - use default credentials instead of auth_token |
| 191 | + client = SecretManagerClient() |
| 192 | + with pytest.raises(Exception, match="Secret error"): |
| 193 | + client.get_secret( |
| 194 | + "projects/test-project/secrets/test-secret/versions/latest" |
| 195 | + ) |
0 commit comments