Skip to content

Commit 2f5678c

Browse files
committed
check if the universe attribute exists
1 parent 4524943 commit 2f5678c

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

tests/test_discovery.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@
107107

108108
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
109109

110+
def _reset_universe_domain(credentials, universe_domain=None):
111+
if hasattr(credentials, "universe_domain"):
112+
credentials.universe_domain = universe_domain
110113

111114
def assertUrisEqual(testcase, expected, actual):
112115
"""Test that URIs are the same, up to reordering of query parameters."""
@@ -542,7 +545,8 @@ def test_credentials_and_credentials_file_mutually_exclusive(self):
542545

543546
class DiscoveryFromDocument(unittest.TestCase):
544547
MOCK_CREDENTIALS = mock.Mock(spec=google.auth.credentials.Credentials)
545-
MOCK_CREDENTIALS.universe_domain = None
548+
_reset_universe_domain(MOCK_CREDENTIALS)
549+
546550
def test_can_build_from_local_document(self):
547551
discovery = read_datafile("plus.json")
548552
plus = build_from_document(
@@ -694,7 +698,7 @@ def test_scopes_from_client_options(self):
694698
discovery = read_datafile("plus.json")
695699

696700
with mock.patch("googleapiclient._auth.default_credentials") as default:
697-
default.return_value.universe_domain = None
701+
_reset_universe_domain(default.return_value)
698702
plus = build_from_document(
699703
discovery,
700704
client_options={"scopes": ["1", "2"]},
@@ -706,7 +710,7 @@ def test_quota_project_from_client_options(self):
706710
discovery = read_datafile("plus.json")
707711

708712
with mock.patch("googleapiclient._auth.default_credentials") as default:
709-
default.return_value.universe_domain = None
713+
_reset_universe_domain(default.return_value)
710714
plus = build_from_document(
711715
discovery,
712716
client_options=google.api_core.client_options.ClientOptions(
@@ -720,7 +724,7 @@ def test_credentials_file_from_client_options(self):
720724
discovery = read_datafile("plus.json")
721725

722726
with mock.patch("googleapiclient._auth.credentials_from_file") as default:
723-
default.return_value.universe_domain = None
727+
_reset_universe_domain(default.return_value)
724728
plus = build_from_document(
725729
discovery,
726730
client_options=google.api_core.client_options.ClientOptions(
@@ -776,7 +780,7 @@ def test_self_signed_jwt_disabled(self):
776780

777781
class DiscoveryFromDocumentMutualTLS(unittest.TestCase):
778782
MOCK_CREDENTIALS = mock.Mock(spec=google.auth.credentials.Credentials)
779-
MOCK_CREDENTIALS.universe_domain = None
783+
_reset_universe_domain(MOCK_CREDENTIALS)
780784
ADC_CERT_PATH = "adc_cert_path"
781785
ADC_KEY_PATH = "adc_key_path"
782786
ADC_PASSPHRASE = "adc_passphrase"
@@ -1533,7 +1537,7 @@ def test_plus_resources(self):
15331537
@unittest.skipIf(not HAS_OAUTH2CLIENT, "oauth2client unavailable.")
15341538
def test_oauth2client_credentials(self):
15351539
credentials = mock.Mock(spec=GoogleCredentials)
1536-
credentials.universe_domain = None
1540+
_reset_universe_domain(credentials)
15371541
credentials.create_scoped_required.return_value = False
15381542

15391543
discovery = read_datafile("plus.json")
@@ -1542,7 +1546,7 @@ def test_oauth2client_credentials(self):
15421546

15431547
def test_google_auth_credentials(self):
15441548
credentials = mock.Mock(spec=google.auth.credentials.Credentials)
1545-
credentials.universe_domain = None
1549+
_reset_universe_domain(credentials)
15461550
discovery = read_datafile("plus.json")
15471551
service = build_from_document(discovery, credentials=credentials)
15481552

@@ -2671,21 +2675,22 @@ def test_universe_env_var_configured_with_client_options_universe(self):
26712675
)
26722676

26732677
assert tasks._universe_domain == fake_universe
2678+
26742679
else:
2675-
def test_client_options_universe_with_older_version_of_api_core(self):
2676-
fake_universe = "foo.com"
2677-
credentials = mock.Mock(spec=google.auth.credentials.Credentials)
2678-
credentials.universe_domain = fake_universe
2679-
discovery = read_datafile("tasks.json")
2680-
with self.assertRaises(APICoreVersionError):
2681-
tasks = build_from_document(
2682-
discovery,
2683-
credentials=credentials,
2684-
client_options=google.api_core.client_options.ClientOptions(
2685-
universe_domain=fake_universe
2686-
),
2687-
)
2688-
2680+
if hasattr(google.api_core.client_options.ClientOptions, "universe_domain"):
2681+
def test_client_options_universe_with_older_version_of_api_core(self):
2682+
fake_universe = "foo.com"
2683+
credentials = mock.Mock(spec=google.auth.credentials.Credentials)
2684+
credentials.universe_domain = fake_universe
2685+
discovery = read_datafile("tasks.json")
2686+
with self.assertRaises(APICoreVersionError):
2687+
tasks = build_from_document(
2688+
discovery,
2689+
credentials=credentials,
2690+
client_options=google.api_core.client_options.ClientOptions(
2691+
universe_domain=fake_universe
2692+
),
2693+
)
26892694

26902695
def test_credentials_universe_with_older_version_of_api_core(self):
26912696
fake_universe = "foo.com"

0 commit comments

Comments
 (0)