22
22
from google .auth import crypt
23
23
from google .auth import exceptions
24
24
from google .oauth2 import credentials as gcredentials
25
+ from google .oauth2 import _credentials_async as gcredentials_async
25
26
from google .oauth2 import service_account
27
+ from google .oauth2 import _service_account_async as service_account_async
26
28
import pytest
27
29
28
30
from firebase_admin import credentials
@@ -33,16 +35,15 @@ def check_scopes(g_credential):
33
35
assert isinstance (g_credential , google .auth .credentials .ReadOnlyScoped )
34
36
assert sorted (credentials ._scopes ) == sorted (g_credential .scopes )
35
37
38
+ invalid_certs = {
39
+ 'NonExistingFile' : ('non_existing.json' , IOError ),
40
+ 'RefreskToken' : ('refresh_token.json' , ValueError ),
41
+ 'MalformedPrivateKey' : ('malformed_key.json' , ValueError ),
42
+ 'MissingClientId' : ('no_client_email_service_account.json' , ValueError ),
43
+ }
36
44
37
45
class TestCertificate :
38
46
39
- invalid_certs = {
40
- 'NonExistingFile' : ('non_existing.json' , IOError ),
41
- 'RefreskToken' : ('refresh_token.json' , ValueError ),
42
- 'MalformedPrivateKey' : ('malformed_key.json' , ValueError ),
43
- 'MissingClientId' : ('no_client_email_service_account.json' , ValueError ),
44
- }
45
-
46
47
def test_init_from_file (self ):
47
48
credential = credentials .Certificate (
48
49
testutils .resource_filename ('service_account.json' ))
@@ -86,6 +87,45 @@ def _verify_credential(self, credential):
86
87
assert isinstance (access_token .expiry , datetime .datetime )
87
88
88
89
90
+ class TestCertificateAsync :
91
+
92
+ @pytest .mark .asyncio
93
+ async def test_init_from_file (self ):
94
+ credential = credentials .Certificate (
95
+ testutils .resource_filename ('service_account.json' ))
96
+ await self ._verify_credential (credential )
97
+
98
+ @pytest .mark .asyncio
99
+ async def test_init_from_path_like (self ):
100
+ path = pathlib .Path (testutils .resource_filename ('service_account.json' ))
101
+ credential = credentials .Certificate (path )
102
+ await self ._verify_credential (credential )
103
+
104
+
105
+ @pytest .mark .asyncio
106
+ async def test_init_from_dict (self ):
107
+ parsed_json = json .loads (testutils .resource ('service_account.json' ))
108
+ credential = credentials .Certificate (parsed_json )
109
+ await self ._verify_credential (credential )
110
+
111
+ @pytest .mark .asyncio
112
+ async def _verify_credential (self , credential ):
113
+ assert credential .project_id == 'mock-project-id'
114
+ assert credential .
service_account_email == '[email protected] '
115
+ assert isinstance (credential .signer , crypt .Signer )
116
+
117
+ g_credential_async = credential .get_credential_async ()
118
+ assert isinstance (g_credential_async , service_account_async .Credentials )
119
+ assert g_credential_async .token is None
120
+ check_scopes (g_credential_async )
121
+
122
+ mock_response = {'access_token' : 'mock_access_token' , 'expires_in' : 3600 }
123
+ credentials ._request_async = testutils .MockAsyncRequest (200 , json .dumps (mock_response ))
124
+ access_token_async = await credential .get_access_token_async ()
125
+ assert access_token_async .access_token == 'mock_access_token'
126
+ assert isinstance (access_token_async .expiry , datetime .datetime )
127
+
128
+
89
129
@pytest .fixture
90
130
def app_default (request ):
91
131
var_name = 'GOOGLE_APPLICATION_CREDENTIALS'
@@ -129,6 +169,38 @@ def test_nonexisting_path(self, app_default):
129
169
creds .get_credential () # This now throws.
130
170
131
171
172
+ class TestApplicationDefaultAsync :
173
+
174
+ @pytest .mark .asyncio
175
+ @pytest .mark .parametrize ('app_default' , [testutils .resource_filename ('service_account.json' )],
176
+ indirect = True )
177
+ async def test_init (self , app_default ):
178
+ del app_default
179
+ credential = credentials .ApplicationDefault ()
180
+ assert credential .project_id == 'mock-project-id'
181
+
182
+ g_credential_async = credential .get_credential_async ()
183
+ assert isinstance (g_credential_async , google .auth .credentials .Credentials )
184
+ assert g_credential_async .token is None
185
+ check_scopes (g_credential_async )
186
+
187
+ mock_response = {'access_token' : 'mock_access_token' , 'expires_in' : 3600 }
188
+ credentials ._request_async = testutils .MockAsyncRequest (200 , json .dumps (mock_response ))
189
+ access_token_async = await credential .get_access_token_async ()
190
+ assert access_token_async .access_token == 'mock_access_token'
191
+ assert isinstance (access_token_async .expiry , datetime .datetime )
192
+
193
+ @pytest .mark .parametrize ('app_default' , [testutils .resource_filename ('non_existing.json' )],
194
+ indirect = True )
195
+ def test_nonexisting_path (self , app_default ):
196
+ del app_default
197
+ # This does not yet throw because the credentials are lazily loaded.
198
+ creds = credentials .ApplicationDefault ()
199
+
200
+ with pytest .raises (exceptions .DefaultCredentialsError ):
201
+ creds .get_credential_async () # This now throws.
202
+
203
+
132
204
class TestRefreshToken :
133
205
134
206
def test_init_from_file (self ):
@@ -191,3 +263,44 @@ def _verify_credential(self, credential):
191
263
access_token = credential .get_access_token ()
192
264
assert access_token .access_token == 'mock_access_token'
193
265
assert isinstance (access_token .expiry , datetime .datetime )
266
+
267
+
268
+ class TestRefreshTokenAsync :
269
+
270
+ @pytest .mark .asyncio
271
+ async def test_init_from_file (self ):
272
+ credential = credentials .RefreshToken (
273
+ testutils .resource_filename ('refresh_token.json' ))
274
+ await self ._verify_credential (credential )
275
+
276
+ @pytest .mark .asyncio
277
+ async def test_init_from_path_like (self ):
278
+ path = pathlib .Path (testutils .resource_filename ('refresh_token.json' ))
279
+ credential = credentials .RefreshToken (path )
280
+ await self ._verify_credential (credential )
281
+
282
+ @pytest .mark .asyncio
283
+ async def test_init_from_dict (self ):
284
+ parsed_json = json .loads (testutils .resource ('refresh_token.json' ))
285
+ credential = credentials .RefreshToken (parsed_json )
286
+ await self ._verify_credential (credential )
287
+
288
+ @pytest .mark .asyncio
289
+ async def _verify_credential (self , credential ):
290
+ assert credential .client_id == 'mock.apps.googleusercontent.com'
291
+ assert credential .client_secret == 'mock-secret'
292
+ assert credential .refresh_token == 'mock-refresh-token'
293
+
294
+ g_credential_async = credential .get_credential_async ()
295
+ assert isinstance (g_credential_async , gcredentials_async .Credentials )
296
+ assert g_credential_async .token is None
297
+ check_scopes (g_credential_async )
298
+
299
+ mock_response = {
300
+ 'access_token' : 'mock_access_token' ,
301
+ 'expires_in' : 3600
302
+ }
303
+ credentials ._request_async = testutils .MockAsyncRequest (200 , json .dumps (mock_response ))
304
+ access_token_async = await credential .get_access_token_async ()
305
+ assert access_token_async .access_token == 'mock_access_token'
306
+ assert isinstance (access_token_async .expiry , datetime .datetime )
0 commit comments