|
15 | 15 | import datetime |
16 | 16 | from typing import Optional |
17 | 17 |
|
| 18 | +from aiohttp import ClientResponseError |
| 19 | +from aioresponses import aioresponses |
| 20 | +from google.auth.credentials import Credentials |
18 | 21 | from mocks import FakeCredentials |
19 | 22 | import pytest |
20 | 23 |
|
@@ -138,3 +141,78 @@ async def test_CloudSQLClient_user_agent( |
138 | 141 | assert client._user_agent == f"cloud-sql-python-connector/{version}+{driver}" |
139 | 142 | # close client |
140 | 143 | await client.close() |
| 144 | + |
| 145 | + |
| 146 | +@pytest.mark.asyncio |
| 147 | +async def test_cloud_sql_error_messages_get_metadata( |
| 148 | + fake_credentials: Credentials, |
| 149 | +) -> None: |
| 150 | + """ |
| 151 | + Test that Cloud SQL Admin API error messages are raised for _get_metadata. |
| 152 | + """ |
| 153 | + # mock Cloud SQL Admin API calls with exceptions |
| 154 | + client = CloudSQLClient( |
| 155 | + sqladmin_api_endpoint="https://sqladmin.googleapis.com", |
| 156 | + quota_project=None, |
| 157 | + credentials=fake_credentials, |
| 158 | + ) |
| 159 | + get_url = "https://sqladmin.googleapis.com/sql/v1beta4/projects/my-project/instances/my-instance/connectSettings" |
| 160 | + resp_body = { |
| 161 | + "error": { |
| 162 | + "code": 403, |
| 163 | + "message": "Cloud SQL Admin API has not been used in project 123456789 before or it is disabled", |
| 164 | + } |
| 165 | + } |
| 166 | + with aioresponses() as mocked: |
| 167 | + mocked.get( |
| 168 | + get_url, |
| 169 | + status=403, |
| 170 | + payload=resp_body, |
| 171 | + repeat=True, |
| 172 | + ) |
| 173 | + try: |
| 174 | + await client._get_metadata("my-project", "my-region", "my-instance") |
| 175 | + except ClientResponseError as e: |
| 176 | + assert e.status == 403 |
| 177 | + assert ( |
| 178 | + e.message |
| 179 | + == "Cloud SQL Admin API has not been used in project 123456789 before or it is disabled" |
| 180 | + ) |
| 181 | + finally: |
| 182 | + await client.close() |
| 183 | + |
| 184 | + |
| 185 | +@pytest.mark.asyncio |
| 186 | +async def test_cloud_sql_error_messages_get_ephemeral( |
| 187 | + fake_credentials: Credentials, |
| 188 | +) -> None: |
| 189 | + """ |
| 190 | + Test that Cloud SQL Admin API error messages are raised for _get_ephemeral. |
| 191 | + """ |
| 192 | + # mock Cloud SQL Admin API calls with exceptions |
| 193 | + client = CloudSQLClient( |
| 194 | + sqladmin_api_endpoint="https://sqladmin.googleapis.com", |
| 195 | + quota_project=None, |
| 196 | + credentials=fake_credentials, |
| 197 | + ) |
| 198 | + post_url = "https://sqladmin.googleapis.com/sql/v1beta4/projects/my-project/instances/my-instance:generateEphemeralCert" |
| 199 | + resp_body = { |
| 200 | + "error": { |
| 201 | + "code": 404, |
| 202 | + "message": "The Cloud SQL instance does not exist.", |
| 203 | + } |
| 204 | + } |
| 205 | + with aioresponses() as mocked: |
| 206 | + mocked.post( |
| 207 | + post_url, |
| 208 | + status=404, |
| 209 | + payload=resp_body, |
| 210 | + repeat=True, |
| 211 | + ) |
| 212 | + try: |
| 213 | + await client._get_ephemeral("my-project", "my-instance", "my-key") |
| 214 | + except ClientResponseError as e: |
| 215 | + assert e.status == 404 |
| 216 | + assert e.message == "The Cloud SQL instance does not exist." |
| 217 | + finally: |
| 218 | + await client.close() |
0 commit comments