1616from typing import Any
1717
1818import aiohttp
19- import google .auth
19+ from google .auth .credentials import Credentials
20+ import json
2021import pytest # noqa F401 Needed to run the tests
22+ from mock import AsyncMock , Mock , patch
2123
2224from google .cloud .sql .connector .refresh_utils import _get_ephemeral , _get_metadata
2325from google .cloud .sql .connector .utils import generate_keys
2426
2527
28+ class FakeClientSessionGet :
29+ """Helper class to return mock data for get request."""
30+
31+ async def text (self ) -> str :
32+ response = {
33+ "kind" : "sql#connectSettings" ,
34+ "serverCaCert" : {
35+ "kind" : "sql#sslCert" ,
36+ "certSerialNumber" : "0" ,
37+ "cert" : "-----BEGIN CERTIFICATE-----\n abc123\n -----END CERTIFICATE-----" ,
38+ "commonName" : "Google" ,
39+ "sha1Fingerprint" : "abc" ,
40+ "instance" : "my-instance" ,
41+ "createTime" : "2021-10-18T18:48:03.785Z" ,
42+ "expirationTime" : "2031-10-16T18:49:03.785Z" ,
43+ },
44+ "ipAddresses" : [
45+ {"type" : "PRIMARY" , "ipAddress" : "0.0.0.0" },
46+ {"type" : "PRIVATE" , "ipAddress" : "1.0.0.0" },
47+ ],
48+ "region" : "my-region" ,
49+ "databaseVersion" : "MYSQL_8_0" ,
50+ "backendType" : "SECOND_GEN" ,
51+ }
52+ return json .dumps (response )
53+
54+
55+ class FakeClientSessionPost :
56+ """Helper class to return mock data for post request."""
57+
58+ async def text (self ) -> str :
59+ response = {
60+ "ephemeralCert" : {
61+ "kind" : "sql#sslCert" ,
62+ "certSerialNumber" : "" ,
63+ "cert" : "-----BEGIN CERTIFICATE-----\n abc123\n -----END CERTIFICATE-----" ,
64+ }
65+ }
66+ return json .dumps (response )
67+
68+
69+ @pytest .fixture
70+ def credentials () -> Credentials :
71+ credentials = Mock (spec = Credentials )
72+ credentials .valid = True
73+ credentials .token = "12345"
74+ return credentials
75+
76+
2677@pytest .mark .asyncio
27- async def test_get_ephemeral (connect_string : str ) -> None :
78+ @patch ("aiohttp.ClientSession.post" , new_callable = AsyncMock )
79+ async def test_get_ephemeral (mock_post : AsyncMock , credentials : Credentials ) -> None :
2880 """
29- Test to check whether _get_ephemeral runs without problems given a valid
30- connection string .
81+ Test to check whether _get_ephemeral runs without problems given valid
82+ parameters .
3183 """
84+ mock_post .return_value = FakeClientSessionPost ()
3285
33- project = connect_string .split (":" )[0 ]
34- instance = connect_string .split (":" )[2 ]
35-
36- credentials , project = google .auth .default (
37- scopes = [
38- "https://www.googleapis.com/auth/sqlservice.admin" ,
39- "https://www.googleapis.com/auth/cloud-platform" ,
40- ]
41- )
86+ project = "my-project"
87+ instance = "my-instance"
4288
4389 _ , pub_key = await generate_keys ()
4490
@@ -56,21 +102,16 @@ async def test_get_ephemeral(connect_string: str) -> None:
56102
57103
58104@pytest .mark .asyncio
59- async def test_get_metadata (connect_string : str ) -> None :
105+ @patch ("aiohttp.ClientSession.get" , new_callable = AsyncMock )
106+ async def test_get_metadata (mock_get : AsyncMock , credentials : Credentials ) -> None :
60107 """
61- Test to check whether _get_metadata runs without problems given a valid
62- connection string .
108+ Test to check whether _get_metadata runs without problems given valid
109+ parameters .
63110 """
111+ mock_get .return_value = FakeClientSessionGet ()
64112
65- project = connect_string .split (":" )[0 ]
66- instance = connect_string .split (":" )[2 ]
67-
68- credentials , project = google .auth .default (
69- scopes = [
70- "https://www.googleapis.com/auth/sqlservice.admin" ,
71- "https://www.googleapis.com/auth/cloud-platform" ,
72- ]
73- )
113+ project = "my-project"
114+ instance = "my-instance"
74115
75116 async with aiohttp .ClientSession () as client_session :
76117 result = await _get_metadata (client_session , credentials , project , instance )
0 commit comments