@@ -99,7 +99,18 @@ def test_default_state(self):
99
99
)
100
100
@mock .patch ("google.auth.compute_engine._metadata.get" , autospec = True )
101
101
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
+ ]
103
114
104
115
# Refresh credentials
105
116
self .credentials .refresh (None )
@@ -109,8 +120,8 @@ def test_refresh_success(self, get, utcnow):
109
120
assert self .credentials .expiry == (utcnow () + datetime .timedelta (seconds = 500 ))
110
121
111
122
# 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" ]
114
125
115
126
# Check that the credentials are valid (have a token and are not
116
127
# expired)
@@ -126,7 +137,18 @@ def test_refresh_success(self, get, utcnow):
126
137
)
127
138
@mock .patch ("google.auth.compute_engine._metadata.get" , autospec = True )
128
139
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
+ ]
130
152
131
153
# Refresh credentials
132
154
scopes = ["three" , "four" ]
@@ -138,7 +160,7 @@ def test_refresh_success_with_scopes(self, get, utcnow, mock_metrics_header_valu
138
160
assert self .credentials .expiry == (utcnow () + datetime .timedelta (seconds = 500 ))
139
161
140
162
# Check the credential info
141
- assert self .credentials .service_account_email == "default "
163
+ assert self .credentials .service_account_email == "service-account@example.com "
142
164
assert self .credentials ._scopes == scopes
143
165
144
166
# Check that the credentials are valid (have a token and are not
@@ -162,7 +184,18 @@ def test_refresh_error(self, get):
162
184
163
185
@mock .patch ("google.auth.compute_engine._metadata.get" , autospec = True )
164
186
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
+ ]
166
199
167
200
# Credentials should start as invalid
168
201
assert not self .credentials .valid
@@ -440,6 +473,20 @@ def test_with_target_audience_integration(self):
440
473
have been mocked.
441
474
"""
442
475
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
+
443
490
# mock information about universe_domain
444
491
responses .add (
445
492
responses .GET ,
@@ -454,7 +501,7 @@ def test_with_target_audience_integration(self):
454
501
responses .add (
455
502
responses .GET ,
456
503
"http://metadata.google.internal/computeMetadata/v1/instance/"
457
- "service-accounts/default /token" ,
504
+ "service-accounts/service-account@example.com /token" ,
458
505
status = 200 ,
459
506
content_type = "application/json" ,
460
507
json = {
@@ -594,11 +641,25 @@ def test_with_quota_project_integration(self):
594
641
have been mocked.
595
642
"""
596
643
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
+
597
658
# mock token for credentials
598
659
responses .add (
599
660
responses .GET ,
600
661
"http://metadata.google.internal/computeMetadata/v1/instance/"
601
- "service-accounts/default /token" ,
662
+ "service-accounts/service-account@example.com /token" ,
602
663
status = 200 ,
603
664
content_type = "application/json" ,
604
665
json = {
0 commit comments