1515import json
1616from typing import Any , Optional
1717
18+ from aiohttp import ClientResponseError
1819from aiohttp import web
20+ from aioresponses import aioresponses
1921from mocks import FakeCredentials
2022import pytest
2123
@@ -138,6 +140,75 @@ async def test__get_metadata_with_psc(
138140 }
139141
140142
143+ async def test__get_metadata_error (
144+ credentials : FakeCredentials ,
145+ ) -> None :
146+ """
147+ Test that AlloyDB API error messages are raised for _get_metadata.
148+ """
149+ # mock AlloyDB API calls with exceptions
150+ client = AlloyDBClient (
151+ alloydb_api_endpoint = "https://alloydb.googleapis.com" ,
152+ quota_project = None ,
153+ credentials = credentials ,
154+ )
155+ get_url = "https://alloydb.googleapis.com/v1beta/projects/my-project/locations/my-region/clusters/my-cluster/instances/my-instance/connectionInfo"
156+ resp_body = {
157+ "error" : {
158+ "code" : 403 ,
159+ "message" : "AlloyDB API has not been used in project 123456789 before or it is disabled" ,
160+ }
161+ }
162+ with aioresponses () as mocked :
163+ mocked .get (
164+ get_url ,
165+ status = 403 ,
166+ payload = resp_body ,
167+ repeat = True ,
168+ )
169+ with pytest .raises (ClientResponseError ) as exc_info :
170+ await client ._get_metadata (
171+ "my-project" , "my-region" , "my-cluster" , "my-instance"
172+ )
173+ assert exc_info .value .status == 403
174+ assert (
175+ exc_info .value .message
176+ == "AlloyDB API has not been used in project 123456789 before or it is disabled"
177+ )
178+ await client .close ()
179+
180+
181+ async def test__get_metadata_error_parsing_json (
182+ credentials : FakeCredentials ,
183+ ) -> None :
184+ """
185+ Test that aiohttp default error messages are raised when _get_metadata gets
186+ a bad JSON response.
187+ """
188+ # mock AlloyDB API calls with exceptions
189+ client = AlloyDBClient (
190+ alloydb_api_endpoint = "https://alloydb.googleapis.com" ,
191+ quota_project = None ,
192+ credentials = credentials ,
193+ )
194+ get_url = "https://alloydb.googleapis.com/v1beta/projects/my-project/locations/my-region/clusters/my-cluster/instances/my-instance/connectionInfo"
195+ resp_body = ["error" ] # invalid json
196+ with aioresponses () as mocked :
197+ mocked .get (
198+ get_url ,
199+ status = 403 ,
200+ payload = resp_body ,
201+ repeat = True ,
202+ )
203+ with pytest .raises (ClientResponseError ) as exc_info :
204+ await client ._get_metadata (
205+ "my-project" , "my-region" , "my-cluster" , "my-instance"
206+ )
207+ assert exc_info .value .status == 403
208+ assert exc_info .value .message == "Forbidden"
209+ await client .close ()
210+
211+
141212@pytest .mark .asyncio
142213async def test__get_client_certificate (
143214 client : Any , credentials : FakeCredentials
@@ -157,6 +228,72 @@ 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 (
259+ "my-project" , "my-region" , "my-cluster" , ""
260+ )
261+ assert exc_info .value .status == 404
262+ assert exc_info .value .message == "The AlloyDB instance does not exist."
263+ await client .close ()
264+
265+
266+ async def test__get_client_certificate_error_parsing_json (
267+ credentials : FakeCredentials ,
268+ ) -> None :
269+ """
270+ Test that aiohttp default error messages are raised when
271+ _get_client_certificate gets a bad JSON response.
272+ """
273+ # mock AlloyDB API calls with exceptions
274+ client = AlloyDBClient (
275+ alloydb_api_endpoint = "https://alloydb.googleapis.com" ,
276+ quota_project = None ,
277+ credentials = credentials ,
278+ )
279+ post_url = "https://alloydb.googleapis.com/v1beta/projects/my-project/locations/my-region/clusters/my-cluster:generateClientCertificate"
280+ resp_body = ["error" ] # invalid json
281+ with aioresponses () as mocked :
282+ mocked .post (
283+ post_url ,
284+ status = 404 ,
285+ payload = resp_body ,
286+ repeat = True ,
287+ )
288+ with pytest .raises (ClientResponseError ) as exc_info :
289+ await client ._get_client_certificate (
290+ "my-project" , "my-region" , "my-cluster" , ""
291+ )
292+ assert exc_info .value .status == 404
293+ assert exc_info .value .message == "Not Found"
294+ await client .close ()
295+
296+
160297@pytest .mark .asyncio
161298async def test_AlloyDBClient_init_ (credentials : FakeCredentials ) -> None :
162299 """
0 commit comments