@@ -99,7 +99,18 @@ def test_default_state(self):
9999 )
100100 @mock .patch ("google.auth.compute_engine._metadata.get" , autospec = True )
101101 def test_refresh_success (self , get , utcnow ):
102- get .side_effect = [{"access_token" : "token" , "expires_in" : 500 }]
102+ get .side_effect = [
103+ {
104+ # First request is for sevice account info.
105+ "email" : "service-account@example.com" ,
106+ "scopes" : ["one" , "two" ],
107+ },
108+ {
109+ # Second request is for the token.
110+ "access_token" : "token" ,
111+ "expires_in" : 500 ,
112+ },
113+ ]
103114
104115 # Refresh credentials
105116 self .credentials .refresh (None )
@@ -109,8 +120,8 @@ def test_refresh_success(self, get, utcnow):
109120 assert self .credentials .expiry == (utcnow () + datetime .timedelta (seconds = 500 ))
110121
111122 # Check the credential info
112- assert self .credentials .service_account_email == "default "
113- assert self .credentials ._scopes is None
123+ assert self .credentials .service_account_email == "service-account@example.com "
124+ assert self .credentials ._scopes == [ "one" , "two" ]
114125
115126 # Check that the credentials are valid (have a token and are not
116127 # expired)
@@ -126,7 +137,18 @@ def test_refresh_success(self, get, utcnow):
126137 )
127138 @mock .patch ("google.auth.compute_engine._metadata.get" , autospec = True )
128139 def test_refresh_success_with_scopes (self , get , utcnow , mock_metrics_header_value ):
129- get .side_effect = [{"access_token" : "token" , "expires_in" : 500 }]
140+ get .side_effect = [
141+ {
142+ # First request is for sevice account info.
143+ "email" : "service-account@example.com" ,
144+ "scopes" : ["one" , "two" ],
145+ },
146+ {
147+ # Second request is for the token.
148+ "access_token" : "token" ,
149+ "expires_in" : 500 ,
150+ },
151+ ]
130152
131153 # Refresh credentials
132154 scopes = ["three" , "four" ]
@@ -138,7 +160,7 @@ def test_refresh_success_with_scopes(self, get, utcnow, mock_metrics_header_valu
138160 assert self .credentials .expiry == (utcnow () + datetime .timedelta (seconds = 500 ))
139161
140162 # Check the credential info
141- assert self .credentials .service_account_email == "default "
163+ assert self .credentials .service_account_email == "service-account@example.com "
142164 assert self .credentials ._scopes == scopes
143165
144166 # Check that the credentials are valid (have a token and are not
@@ -162,7 +184,18 @@ def test_refresh_error(self, get):
162184
163185 @mock .patch ("google.auth.compute_engine._metadata.get" , autospec = True )
164186 def test_before_request_refreshes (self , get ):
165- get .side_effect = [{"access_token" : "token" , "expires_in" : 500 }]
187+ get .side_effect = [
188+ {
189+ # First request is for sevice account info.
190+ "email" : "service-account@example.com" ,
191+ "scopes" : "one two" ,
192+ },
193+ {
194+ # Second request is for the token.
195+ "access_token" : "token" ,
196+ "expires_in" : 500 ,
197+ },
198+ ]
166199
167200 # Credentials should start as invalid
168201 assert not self .credentials .valid
@@ -440,6 +473,20 @@ def test_with_target_audience_integration(self):
440473 have been mocked.
441474 """
442475
476+ # mock information about credentials
477+ responses .add (
478+ responses .GET ,
479+ "http://metadata.google.internal/computeMetadata/v1/instance/"
480+ "service-accounts/default/?recursive=true" ,
481+ status = 200 ,
482+ content_type = "application/json" ,
483+ json = {
484+ "scopes" : "email" ,
485+ "email" : "service-account@example.com" ,
486+ "aliases" : ["default" ],
487+ },
488+ )
489+
443490 # mock information about universe_domain
444491 responses .add (
445492 responses .GET ,
@@ -454,7 +501,7 @@ def test_with_target_audience_integration(self):
454501 responses .add (
455502 responses .GET ,
456503 "http://metadata.google.internal/computeMetadata/v1/instance/"
457- "service-accounts/default /token" ,
504+ "service-accounts/service-account@example.com /token" ,
458505 status = 200 ,
459506 content_type = "application/json" ,
460507 json = {
@@ -594,11 +641,25 @@ def test_with_quota_project_integration(self):
594641 have been mocked.
595642 """
596643
644+ # mock information about credentials
645+ responses .add (
646+ responses .GET ,
647+ "http://metadata.google.internal/computeMetadata/v1/instance/"
648+ "service-accounts/default/?recursive=true" ,
649+ status = 200 ,
650+ content_type = "application/json" ,
651+ json = {
652+ "scopes" : "email" ,
653+ "email" : "service-account@example.com" ,
654+ "aliases" : ["default" ],
655+ },
656+ )
657+
597658 # mock token for credentials
598659 responses .add (
599660 responses .GET ,
600661 "http://metadata.google.internal/computeMetadata/v1/instance/"
601- "service-accounts/default /token" ,
662+ "service-accounts/service-account@example.com /token" ,
602663 status = 200 ,
603664 content_type = "application/json" ,
604665 json = {
0 commit comments