Skip to content

Commit b73ecfd

Browse files
committed
use None as universe domain for mocked values
1 parent 7875e80 commit b73ecfd

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

googleapiclient/discovery.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
GOOGLE_API_USE_CLIENT_CERTIFICATE = "GOOGLE_API_USE_CLIENT_CERTIFICATE"
125125
GOOGLE_API_USE_MTLS_ENDPOINT = "GOOGLE_API_USE_MTLS_ENDPOINT"
126126
GOOGLE_CLOUD_UNIVERSE_DOMAIN = "GOOGLE_CLOUD_UNIVERSE_DOMAIN"
127-
127+
DEFAULT_UNIVERSE = "googleapis.com"
128128
# Parameters accepted by the stack, but not visible via discovery.
129129
# TODO(dhermes): Remove 'userip' in 'v2'.
130130
STACK_QUERY_PARAMETERS = frozenset(["trace", "pp", "userip", "strict"])
@@ -565,7 +565,8 @@ def build_from_document(
565565
)
566566
base = base.replace(universe.DEFAULT_UNIVERSE, universe_domain)
567567
else:
568-
if client_options.universe_domain:
568+
client_universe = getattr(client_options, "universe_domain", None)
569+
if client_universe:
569570
raise APICoreVersionError
570571

571572
audience_for_self_signed_jwt = base
@@ -606,6 +607,11 @@ def build_from_document(
606607
quota_project_id=client_options.quota_project_id,
607608
)
608609

610+
if not HAS_UNIVERSE:
611+
credentials_universe = getattr(credentials, "universe_domain", None)
612+
if credentials_universe and credentials_universe != DEFAULT_UNIVERSE:
613+
raise APICoreVersionError
614+
609615
# The credentials need to be scoped.
610616
# If the user provided scopes via client_options don't override them
611617
if not client_options.scopes:
@@ -696,12 +702,6 @@ def build_from_document(
696702
)
697703
base = mtls_endpoint
698704

699-
if not HAS_UNIVERSE:
700-
credentials = getattr(http, "credentials", None)
701-
universe_domain = getattr(credentials, "universe_domain", None)
702-
if universe_domain != "googleapis.com":
703-
raise APICoreVersionError
704-
705705
if model is None:
706706
features = service.get("features", [])
707707
model = JsonModel("dataWrapper" in features)

tests/test_discovery.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ def test_credentials_and_credentials_file_mutually_exclusive(self):
542542

543543
class DiscoveryFromDocument(unittest.TestCase):
544544
MOCK_CREDENTIALS = mock.Mock(spec=google.auth.credentials.Credentials)
545-
545+
MOCK_CREDENTIALS.universe_domain = None
546546
def test_can_build_from_local_document(self):
547547
discovery = read_datafile("plus.json")
548548
plus = build_from_document(
@@ -694,6 +694,7 @@ def test_scopes_from_client_options(self):
694694
discovery = read_datafile("plus.json")
695695

696696
with mock.patch("googleapiclient._auth.default_credentials") as default:
697+
default.return_value.universe_domain = None
697698
plus = build_from_document(
698699
discovery,
699700
client_options={"scopes": ["1", "2"]},
@@ -705,6 +706,7 @@ def test_quota_project_from_client_options(self):
705706
discovery = read_datafile("plus.json")
706707

707708
with mock.patch("googleapiclient._auth.default_credentials") as default:
709+
default.return_value.universe_domain = None
708710
plus = build_from_document(
709711
discovery,
710712
client_options=google.api_core.client_options.ClientOptions(
@@ -718,6 +720,7 @@ def test_credentials_file_from_client_options(self):
718720
discovery = read_datafile("plus.json")
719721

720722
with mock.patch("googleapiclient._auth.credentials_from_file") as default:
723+
default.return_value.universe_domain = None
721724
plus = build_from_document(
722725
discovery,
723726
client_options=google.api_core.client_options.ClientOptions(
@@ -773,6 +776,7 @@ def test_self_signed_jwt_disabled(self):
773776

774777
class DiscoveryFromDocumentMutualTLS(unittest.TestCase):
775778
MOCK_CREDENTIALS = mock.Mock(spec=google.auth.credentials.Credentials)
779+
MOCK_CREDENTIALS.universe_domain = None
776780
ADC_CERT_PATH = "adc_cert_path"
777781
ADC_KEY_PATH = "adc_key_path"
778782
ADC_PASSPHRASE = "adc_passphrase"
@@ -1529,6 +1533,7 @@ def test_plus_resources(self):
15291533
@unittest.skipIf(not HAS_OAUTH2CLIENT, "oauth2client unavailable.")
15301534
def test_oauth2client_credentials(self):
15311535
credentials = mock.Mock(spec=GoogleCredentials)
1536+
credentials.universe_domain = None
15321537
credentials.create_scoped_required.return_value = False
15331538

15341539
discovery = read_datafile("plus.json")
@@ -1537,6 +1542,7 @@ def test_oauth2client_credentials(self):
15371542

15381543
def test_google_auth_credentials(self):
15391544
credentials = mock.Mock(spec=google.auth.credentials.Credentials)
1545+
credentials.universe_domain = None
15401546
discovery = read_datafile("plus.json")
15411547
service = build_from_document(discovery, credentials=credentials)
15421548

0 commit comments

Comments
 (0)