1515import json
1616from typing import Any , Optional
1717
18- from aiohttp import web
18+ from aiohttp import ClientResponseError , web
19+ from aioresponses import aioresponses
1920from mocks import FakeCredentials
2021import pytest
2122
@@ -138,6 +139,76 @@ async def test__get_metadata_with_psc(
138139 }
139140
140141
142+ async def test__get_metadata_error (
143+ credentials : FakeCredentials ,
144+ ) -> None :
145+ """
146+ Test that AlloyDB API error messages are raised for _get_metadata.
147+ """
148+ # mock AlloyDB API calls with exceptions
149+ client = AlloyDBClient (
150+ alloydb_api_endpoint = "https://alloydb.googleapis.com" ,
151+ quota_project = None ,
152+ credentials = credentials ,
153+ )
154+ get_url = "https://alloydb.googleapis.com/v1beta/projects/my-project/locations/my-region/clusters/my-cluster/instances/my-instance/connectionInfo"
155+ resp_body = {
156+ "error" : {
157+ "code" : 403 ,
158+ "message" : "AlloyDB API has not been used in project 123456789 before or it is disabled" ,
159+ }
160+ }
161+ with aioresponses () as mocked :
162+ mocked .get (
163+ get_url ,
164+ status = 403 ,
165+ payload = resp_body ,
166+ repeat = True ,
167+ )
168+ with pytest .raises (ClientResponseError ) as exc_info :
169+ await client ._get_metadata ("my-project" , "my-region" , "my-cluster" , "my-instance" )
170+ exc = exc_info .value
171+ assert exc .status == 403
172+ assert (
173+ exc .message
174+ == "AlloyDB API has not been used in project 123456789 before or it is disabled"
175+ )
176+ await client .close ()
177+
178+
179+ async def test__get_metadata_error_parsing_json (
180+ credentials : FakeCredentials ,
181+ ) -> None :
182+ """
183+ Test that AlloyDB API error messages are raised for _get_metadata when
184+ response JSON fails to be parsed.
185+ """
186+ # mock AlloyDB API calls with exceptions
187+ client = AlloyDBClient (
188+ alloydb_api_endpoint = "https://alloydb.googleapis.com" ,
189+ quota_project = None ,
190+ credentials = credentials ,
191+ )
192+ get_url = "https://alloydb.googleapis.com/v1beta/projects/my-project/locations/my-region/clusters/my-cluster/instances/my-instance/connectionInfo"
193+ resp_body = ["error" ] # invalid json
194+ with aioresponses () as mocked :
195+ mocked .get (
196+ get_url ,
197+ status = 403 ,
198+ payload = resp_body ,
199+ repeat = True ,
200+ )
201+ with pytest .raises (ClientResponseError ) as exc_info :
202+ await client ._get_metadata ("my-project" , "my-region" , "my-cluster" , "my-instance" )
203+ exc = exc_info .value
204+ assert exc .status == 403
205+ assert (
206+ exc .message
207+ != "AlloyDB API has not been used in project 123456789 before or it is disabled"
208+ )
209+ await client .close ()
210+
211+
141212@pytest .mark .asyncio
142213async def test__get_client_certificate (
143214 client : Any , credentials : FakeCredentials
@@ -157,6 +228,70 @@ async def test__get_client_certificate(
157228 assert cert_chain [2 ] == "This is the root cert"
158229
159230
231+ async def test__get_client_certificate_error (
232+ credentials : FakeCredentials ,
233+ ) -> None :
234+ """
235+ Test that AlloyDB API error messages are raised for _get_client_certificate.
236+ """
237+ # mock AlloyDB API calls with exceptions
238+ client = AlloyDBClient (
239+ alloydb_api_endpoint = "https://alloydb.googleapis.com" ,
240+ quota_project = None ,
241+ credentials = credentials ,
242+ )
243+ post_url = "https://alloydb.googleapis.com/v1beta/projects/my-project/locations/my-region/clusters/my-cluster:generateClientCertificate"
244+ resp_body = {
245+ "error" : {
246+ "code" : 404 ,
247+ "message" : "The AlloyDB instance does not exist." ,
248+ }
249+ }
250+ with aioresponses () as mocked :
251+ mocked .post (
252+ post_url ,
253+ status = 404 ,
254+ payload = resp_body ,
255+ repeat = True ,
256+ )
257+ with pytest .raises (ClientResponseError ) as exc_info :
258+ await client ._get_client_certificate ("my-project" , "my-region" , "my-cluster" , "" )
259+ exc = exc_info .value
260+ assert exc .status == 404
261+ assert exc .message == "The AlloyDB instance does not exist."
262+ await client .close ()
263+
264+
265+ async def test__get_client_certificate_error_parsing_json (
266+ credentials : FakeCredentials ,
267+ ) -> None :
268+ """
269+ Test that AlloyDB API error messages are raised for _get_client_certificate
270+ when response JSON fails to be parsed.
271+ """
272+ # mock AlloyDB API calls with exceptions
273+ client = AlloyDBClient (
274+ alloydb_api_endpoint = "https://alloydb.googleapis.com" ,
275+ quota_project = None ,
276+ credentials = credentials ,
277+ )
278+ post_url = "https://alloydb.googleapis.com/v1beta/projects/my-project/locations/my-region/clusters/my-cluster:generateClientCertificate"
279+ resp_body = ["error" ] # invalid json
280+ with aioresponses () as mocked :
281+ mocked .post (
282+ post_url ,
283+ status = 404 ,
284+ payload = resp_body ,
285+ repeat = True ,
286+ )
287+ with pytest .raises (ClientResponseError ) as exc_info :
288+ await client ._get_client_certificate ("my-project" , "my-region" , "my-cluster" , "" )
289+ exc = exc_info .value
290+ assert exc .status == 404
291+ assert exc .message != "The AlloyDB instance does not exist."
292+ await client .close ()
293+
294+
160295@pytest .mark .asyncio
161296async def test_AlloyDBClient_init_ (credentials : FakeCredentials ) -> None :
162297 """
0 commit comments