|
25 | 25 |
|
26 | 26 | class TestGcpUtils(unittest.TestCase): |
27 | 27 |
|
| 28 | + def setUp(self): |
| 29 | + super().setUp() |
| 30 | + patcher = mock.patch( |
| 31 | + "google.adk.cli.utils.gcp_utils._mtls_utils.use_client_cert_effective", |
| 32 | + return_value=False, |
| 33 | + ) |
| 34 | + self.mock_use_client_cert_effective = patcher.start() |
| 35 | + self.addCleanup(patcher.stop) |
| 36 | + |
28 | 37 | @mock.patch("google.auth.default") |
29 | 38 | def test_check_adc_success(self, mock_auth_default): |
30 | 39 | mock_auth_default.return_value = (mock.Mock(), "test-project") |
@@ -168,6 +177,52 @@ def test_list_gcp_projects_import_error(self): |
168 | 177 | ): |
169 | 178 | gcp_utils.list_gcp_projects() |
170 | 179 |
|
| 180 | + @mock.patch("google.adk.cli.utils.gcp_utils.AuthorizedSession") |
| 181 | + @mock.patch("google.auth.default") |
| 182 | + def test_retrieve_express_project_mtls_enabled( |
| 183 | + self, mock_auth_default, mock_session_cls |
| 184 | + ): |
| 185 | + # Enable mtls |
| 186 | + self.mock_use_client_cert_effective.return_value = True |
| 187 | + |
| 188 | + mock_auth_default.return_value = (mock.Mock(), "test-project-id") |
| 189 | + |
| 190 | + mock_session = mock.Mock() |
| 191 | + mock_session_cls.return_value = mock_session |
| 192 | + mock_response = mock.Mock() |
| 193 | + mock_response.json.return_value = { |
| 194 | + "expressProject": { |
| 195 | + "projectId": "test-project", |
| 196 | + "defaultApiKey": "test-api-key", |
| 197 | + "region": "us-central1", |
| 198 | + } |
| 199 | + } |
| 200 | + mock_session.get.return_value = mock_response |
| 201 | + |
| 202 | + with mock.patch( |
| 203 | + "google.adk.cli.utils.gcp_utils._mtls_utils.get_api_endpoint", |
| 204 | + return_value=( |
| 205 | + "https://us-central1-aiplatform.mtls.googleapis.com/v1beta1" |
| 206 | + ), |
| 207 | + ) as mock_get_api_endpoint: |
| 208 | + result = gcp_utils.retrieve_express_project() |
| 209 | + |
| 210 | + self.assertEqual(result["project_id"], "test-project") |
| 211 | + mock_session.configure_mtls_channel.assert_called_once() |
| 212 | + mock_get_api_endpoint.assert_called_once_with( |
| 213 | + location="us-central1", |
| 214 | + default_template="https://{location}-aiplatform.googleapis.com/v1beta1", |
| 215 | + mtls_template=( |
| 216 | + "https://{location}-aiplatform.mtls.googleapis.com/v1beta1" |
| 217 | + ), |
| 218 | + ) |
| 219 | + mock_session.get.assert_called_once() |
| 220 | + args, _ = mock_session.get.call_args |
| 221 | + self.assertEqual( |
| 222 | + args[0], |
| 223 | + "https://us-central1-aiplatform.mtls.googleapis.com/v1beta1/vertexExpress:retrieveExpressProject", |
| 224 | + ) |
| 225 | + |
171 | 226 |
|
172 | 227 | if __name__ == "__main__": |
173 | 228 | unittest.main() |
0 commit comments