Skip to content

Commit cd213c0

Browse files
committed
Fix failing unit tests
1 parent 36015c6 commit cd213c0

File tree

5 files changed

+30
-28
lines changed

5 files changed

+30
-28
lines changed

google/oauth2/service_account.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def _metric_header_for_usage(self):
435435
return metrics.CRED_TYPE_SA_JWT
436436
return metrics.CRED_TYPE_SA_ASSERTION
437437

438-
@_helpers.copy_docstring(credentials.Credentials)
438+
@_helpers.copy_docstring(credentials.CredentialsWithTrustBoundary)
439439
def _refresh_token(self, request):
440440
if self._always_use_jwt_access and not self._jwt_credentials:
441441
# If self signed jwt should be used but jwt credential is not

tests/compute_engine/test_credentials.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def test_user_provided_universe_domain(self, get_universe_domain):
255255
# domain endpoint.
256256
get_universe_domain.assert_not_called()
257257

258-
@mock.patch("google.oauth2._client.lookup_trust_boundary", autospec=True)
258+
@mock.patch("google.oauth2._client._lookup_trust_boundary", autospec=True)
259259
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
260260
def test_refresh_trust_boundary_lookup_skipped_if_env_var_not_true(
261261
self,
@@ -278,7 +278,7 @@ def test_refresh_trust_boundary_lookup_skipped_if_env_var_not_true(
278278
mock_lookup_tb.assert_not_called()
279279
assert creds._trust_boundary is None
280280

281-
@mock.patch("google.oauth2._client.lookup_trust_boundary", autospec=True)
281+
@mock.patch("google.oauth2._client._lookup_trust_boundary", autospec=True)
282282
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
283283
def test_refresh_trust_boundary_lookup_skipped_if_env_var_missing(
284284
self, mock_metadata_get, mock_lookup_tb
@@ -297,7 +297,7 @@ def test_refresh_trust_boundary_lookup_skipped_if_env_var_missing(
297297
mock_lookup_tb.assert_not_called()
298298
assert creds._trust_boundary is None
299299

300-
@mock.patch.object(_client, "lookup_trust_boundary", autospec=True)
300+
@mock.patch.object(_client, "_lookup_trust_boundary", autospec=True)
301301
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
302302
def test_refresh_trust_boundary_lookup_success(
303303
self, mock_metadata_get, mock_lookup_tb
@@ -329,7 +329,7 @@ def test_refresh_trust_boundary_lookup_success(
329329
mock_lookup_tb.assert_called_once_with(
330330
request,
331331
"https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/resolved-email@example.com/allowedLocations",
332-
"mock_token",
332+
headers={"authorization": "Bearer mock_token"},
333333
)
334334
# Verify trust boundary was set
335335
assert creds._trust_boundary == {
@@ -343,7 +343,7 @@ def test_refresh_trust_boundary_lookup_success(
343343
assert headers_applied["x-allowed-locations"] == "0xABC"
344344

345345
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
346-
@mock.patch.object(_client, "lookup_trust_boundary", autospec=True)
346+
@mock.patch.object(_client, "_lookup_trust_boundary", autospec=True)
347347
def test_refresh_trust_boundary_lookup_fails_no_cache(
348348
self, mock_lookup_tb, mock_metadata_get
349349
):
@@ -369,7 +369,7 @@ def test_refresh_trust_boundary_lookup_fails_no_cache(
369369
mock_lookup_tb.assert_called_once()
370370

371371
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
372-
@mock.patch.object(_client, "lookup_trust_boundary", autospec=True)
372+
@mock.patch.object(_client, "_lookup_trust_boundary", autospec=True)
373373
def test_refresh_trust_boundary_lookup_fails_with_cached_data(
374374
self, mock_lookup_tb, mock_metadata_get
375375
):
@@ -420,7 +420,7 @@ def test_refresh_trust_boundary_lookup_fails_with_cached_data(
420420
mock_lookup_tb.assert_called_once()
421421

422422
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
423-
@mock.patch.object(_client, "lookup_trust_boundary", autospec=True)
423+
@mock.patch.object(_client, "_lookup_trust_boundary", autospec=True)
424424
def test_refresh_fetches_no_op_trust_boundary(
425425
self, mock_lookup_tb, mock_metadata_get
426426
):
@@ -444,19 +444,21 @@ def test_refresh_fetches_no_op_trust_boundary(
444444
mock_lookup_tb.assert_called_once_with(
445445
request,
446446
"https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/resolved-email@example.com/allowedLocations",
447-
"mock_token",
447+
headers={"authorization": "Bearer mock_token"},
448448
)
449449
# Verify that an empty header was added.
450450
headers_applied = {}
451451
creds.apply(headers_applied)
452452
assert headers_applied["x-allowed-locations"] == ""
453453

454454
@mock.patch("google.auth.compute_engine._metadata.get", autospec=True)
455-
@mock.patch.object(_client, "lookup_trust_boundary", autospec=True)
455+
@mock.patch.object(_client, "_lookup_trust_boundary", autospec=True)
456456
def test_refresh_starts_with_no_op_trust_boundary_skips_lookup(
457457
self, mock_lookup_tb, mock_metadata_get
458458
):
459459
creds = self.credentials
460+
# Use pre-cache universe domain to avoid an extra metadata call.
461+
creds._universe_domain_cached = True
460462
creds._trust_boundary = {"locations": [], "encodedLocations": "0x0"}
461463
request = mock.Mock()
462464

tests/oauth2/test__client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ def test_lookup_trust_boundary():
667667

668668
url = "http://example.com"
669669
headers = {"Authorization": "Bearer access_token"}
670-
response = _client.lookup_trust_boundary(mock_request, url, headers=headers)
670+
response = _client._lookup_trust_boundary(mock_request, url, headers=headers)
671671

672672
assert response["encodedLocations"] == "0x80080000000000"
673673
assert response["locations"] == ["us-central1", "us-east1"]
@@ -688,7 +688,7 @@ def test_lookup_trust_boundary_no_op_response_without_locations():
688688
url = "http://example.com"
689689
headers = {"Authorization": "Bearer access_token"}
690690
# for the response to be valid, we only need encodedLocations to be present.
691-
response = _client.lookup_trust_boundary(mock_request, url, headers=headers)
691+
response = _client._lookup_trust_boundary(mock_request, url, headers=headers)
692692
assert response["encodedLocations"] == "0x0"
693693
assert "locations" not in response
694694

@@ -707,7 +707,7 @@ def test_lookup_trust_boundary_no_op_response():
707707

708708
url = "http://example.com"
709709
headers = {"Authorization": "Bearer access_token"}
710-
response = _client.lookup_trust_boundary(mock_request, url, headers=headers)
710+
response = _client._lookup_trust_boundary(mock_request, url, headers=headers)
711711

712712
assert response["encodedLocations"] == "0x0"
713713
assert response["locations"] == []
@@ -726,7 +726,7 @@ def test_lookup_trust_boundary_error():
726726
url = "http://example.com"
727727
headers = {"Authorization": "Bearer access_token"}
728728
with pytest.raises(exceptions.RefreshError) as excinfo:
729-
_client.lookup_trust_boundary(mock_request, url, headers=headers)
729+
_client._lookup_trust_boundary(mock_request, url, headers=headers)
730730
assert excinfo.match("this is an error message")
731731

732732
mock_request.assert_called_with(method="GET", url=url, headers=headers)
@@ -745,7 +745,7 @@ def test_lookup_trust_boundary_missing_encoded_locations():
745745
url = "http://example.com"
746746
headers = {"Authorization": "Bearer access_token"}
747747
with pytest.raises(exceptions.MalformedError) as excinfo:
748-
_client.lookup_trust_boundary(mock_request, url, headers=headers)
748+
_client._lookup_trust_boundary(mock_request, url, headers=headers)
749749
assert excinfo.match("Invalid trust boundary info")
750750

751751
mock_request.assert_called_once_with(method="GET", url=url, headers=headers)
@@ -823,7 +823,7 @@ def test_lookup_trust_boundary_with_headers():
823823
"x-test-header": "test-value",
824824
}
825825

826-
_client.lookup_trust_boundary(mock_request, "http://example.com", headers=headers)
826+
_client._lookup_trust_boundary(mock_request, "http://example.com", headers=headers)
827827

828828
mock_request.assert_called_once_with(
829829
method="GET", url="http://example.com", headers=headers

tests/oauth2/test_service_account.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ def test_refresh_success(self, jwt_grant):
521521
# boundary was provided.
522522
assert credentials._trust_boundary is None
523523

524-
@mock.patch("google.oauth2._client.lookup_trust_boundary")
524+
@mock.patch("google.oauth2._client._lookup_trust_boundary")
525525
@mock.patch("google.oauth2._client.jwt_grant", autospec=True)
526526
def test_refresh_skips_trust_boundary_lookup_non_default_universe(
527527
self, mock_jwt_grant, mock_lookup_trust_boundary
@@ -658,7 +658,7 @@ def test_refresh_non_gdu_domain_wide_delegation_not_supported(self):
658658
credentials.refresh(None)
659659
assert excinfo.match("domain wide delegation is not supported")
660660

661-
@mock.patch("google.oauth2._client.lookup_trust_boundary")
661+
@mock.patch("google.oauth2._client._lookup_trust_boundary")
662662
@mock.patch("google.oauth2._client.jwt_grant", autospec=True)
663663
def test_refresh_success_with_valid_trust_boundary(
664664
self, mock_jwt_grant, mock_lookup_trust_boundary
@@ -702,7 +702,7 @@ def test_refresh_success_with_valid_trust_boundary(
702702
== self.VALID_TRUST_BOUNDARY["encodedLocations"]
703703
)
704704

705-
@mock.patch("google.oauth2._client.lookup_trust_boundary")
705+
@mock.patch("google.oauth2._client._lookup_trust_boundary")
706706
@mock.patch("google.oauth2._client.jwt_grant", autospec=True)
707707
def test_refresh_fetches_no_op_trust_boundary(
708708
self, mock_jwt_grant, mock_lookup_trust_boundary
@@ -736,7 +736,7 @@ def test_refresh_fetches_no_op_trust_boundary(
736736
credentials.apply(headers_applied)
737737
assert headers_applied["x-allowed-locations"] == ""
738738

739-
@mock.patch("google.oauth2._client.lookup_trust_boundary")
739+
@mock.patch("google.oauth2._client._lookup_trust_boundary")
740740
@mock.patch("google.oauth2._client.jwt_grant", autospec=True)
741741
def test_refresh_starts_with_no_op_trust_boundary_skips_lookup(
742742
self, mock_jwt_grant, mock_lookup_trust_boundary
@@ -770,7 +770,7 @@ def test_refresh_starts_with_no_op_trust_boundary_skips_lookup(
770770
credentials.apply(headers_applied)
771771
assert headers_applied["x-allowed-locations"] == ""
772772

773-
@mock.patch("google.oauth2._client.lookup_trust_boundary")
773+
@mock.patch("google.oauth2._client._lookup_trust_boundary")
774774
@mock.patch("google.oauth2._client.jwt_grant", autospec=True)
775775
def test_refresh_trust_boundary_lookup_fails_no_cache(
776776
self, mock_jwt_grant, mock_lookup_trust_boundary
@@ -796,7 +796,7 @@ def test_refresh_trust_boundary_lookup_fails_no_cache(
796796
assert credentials._trust_boundary is None
797797
mock_lookup_trust_boundary.assert_called_once()
798798

799-
@mock.patch("google.oauth2._client.lookup_trust_boundary")
799+
@mock.patch("google.oauth2._client._lookup_trust_boundary")
800800
@mock.patch("google.oauth2._client.jwt_grant", autospec=True)
801801
def test_refresh_trust_boundary_lookup_fails_with_cached_data(
802802
self, mock_jwt_grant, mock_lookup_trust_boundary

tests/test_impersonated_credentials.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def test_token_usage_metrics(self):
281281
assert headers["x-goog-api-client"] == "cred-type/imp"
282282

283283
@pytest.mark.parametrize("use_data_bytes", [True, False])
284-
@mock.patch("google.oauth2._client.lookup_trust_boundary")
284+
@mock.patch("google.oauth2._client._lookup_trust_boundary")
285285
def test_refresh_success(
286286
self, mock_lookup_trust_boundary, use_data_bytes, mock_donor_credentials
287287
):
@@ -344,7 +344,7 @@ def test_refresh_success(
344344
== self.VALID_TRUST_BOUNDARY["encodedLocations"]
345345
)
346346

347-
@mock.patch("google.oauth2._client.lookup_trust_boundary")
347+
@mock.patch("google.oauth2._client._lookup_trust_boundary")
348348
def test_refresh_trust_boundary_lookup_fails_no_cache(
349349
self, mock_lookup_trust_boundary, mock_donor_credentials
350350
):
@@ -376,7 +376,7 @@ def test_refresh_trust_boundary_lookup_fails_no_cache(
376376
assert credentials._trust_boundary is None # Still no trust boundary
377377
mock_lookup_trust_boundary.assert_called_once()
378378

379-
@mock.patch("google.oauth2._client.lookup_trust_boundary")
379+
@mock.patch("google.oauth2._client._lookup_trust_boundary")
380380
def test_refresh_fetches_no_op_trust_boundary(
381381
self, mock_lookup_trust_boundary, mock_donor_credentials
382382
):
@@ -414,7 +414,7 @@ def test_refresh_fetches_no_op_trust_boundary(
414414
credentials.apply(headers_applied)
415415
assert headers_applied["x-allowed-locations"] == ""
416416

417-
@mock.patch("google.oauth2._client.lookup_trust_boundary")
417+
@mock.patch("google.oauth2._client._lookup_trust_boundary")
418418
def test_refresh_skips_trust_boundary_lookup_non_default_universe(
419419
self, mock_lookup_trust_boundary
420420
):
@@ -450,7 +450,7 @@ def test_refresh_skips_trust_boundary_lookup_non_default_universe(
450450
credentials.apply(headers_applied)
451451
assert "x-allowed-locations" not in headers_applied
452452

453-
@mock.patch("google.oauth2._client.lookup_trust_boundary")
453+
@mock.patch("google.oauth2._client._lookup_trust_boundary")
454454
def test_refresh_starts_with_no_op_trust_boundary_skips_lookup(
455455
self, mock_lookup_trust_boundary, mock_donor_credentials
456456
):
@@ -485,7 +485,7 @@ def test_refresh_starts_with_no_op_trust_boundary_skips_lookup(
485485
credentials.apply(headers_applied)
486486
assert headers_applied["x-allowed-locations"] == ""
487487

488-
@mock.patch("google.oauth2._client.lookup_trust_boundary")
488+
@mock.patch("google.oauth2._client._lookup_trust_boundary")
489489
def test_refresh_trust_boundary_lookup_fails_with_cached_data2(
490490
self, mock_lookup_trust_boundary, mock_donor_credentials
491491
):

0 commit comments

Comments
 (0)