70
70
STACK_QUERY_PARAMETERS ,
71
71
V1_DISCOVERY_URI ,
72
72
V2_DISCOVERY_URI ,
73
+ APICoreVersionError ,
73
74
ResourceMethodParameters ,
74
75
_fix_up_media_path_base_url ,
75
76
_fix_up_media_upload ,
107
108
DATA_DIR = os .path .join (os .path .dirname (__file__ ), "data" )
108
109
109
110
111
+ def _reset_universe_domain (credentials , universe_domain = None ):
112
+ if hasattr (credentials , "universe_domain" ):
113
+ credentials .universe_domain = universe_domain
114
+
115
+
110
116
def assertUrisEqual (testcase , expected , actual ):
111
117
"""Test that URIs are the same, up to reordering of query parameters."""
112
118
expected = urllib .parse .urlparse (expected )
@@ -541,6 +547,7 @@ def test_credentials_and_credentials_file_mutually_exclusive(self):
541
547
542
548
class DiscoveryFromDocument (unittest .TestCase ):
543
549
MOCK_CREDENTIALS = mock .Mock (spec = google .auth .credentials .Credentials )
550
+ _reset_universe_domain (MOCK_CREDENTIALS )
544
551
545
552
def test_can_build_from_local_document (self ):
546
553
discovery = read_datafile ("plus.json" )
@@ -693,6 +700,7 @@ def test_scopes_from_client_options(self):
693
700
discovery = read_datafile ("plus.json" )
694
701
695
702
with mock .patch ("googleapiclient._auth.default_credentials" ) as default :
703
+ _reset_universe_domain (default .return_value )
696
704
plus = build_from_document (
697
705
discovery ,
698
706
client_options = {"scopes" : ["1" , "2" ]},
@@ -704,6 +712,7 @@ def test_quota_project_from_client_options(self):
704
712
discovery = read_datafile ("plus.json" )
705
713
706
714
with mock .patch ("googleapiclient._auth.default_credentials" ) as default :
715
+ _reset_universe_domain (default .return_value )
707
716
plus = build_from_document (
708
717
discovery ,
709
718
client_options = google .api_core .client_options .ClientOptions (
@@ -717,6 +726,7 @@ def test_credentials_file_from_client_options(self):
717
726
discovery = read_datafile ("plus.json" )
718
727
719
728
with mock .patch ("googleapiclient._auth.credentials_from_file" ) as default :
729
+ _reset_universe_domain (default .return_value )
720
730
plus = build_from_document (
721
731
discovery ,
722
732
client_options = google .api_core .client_options .ClientOptions (
@@ -772,6 +782,7 @@ def test_self_signed_jwt_disabled(self):
772
782
773
783
class DiscoveryFromDocumentMutualTLS (unittest .TestCase ):
774
784
MOCK_CREDENTIALS = mock .Mock (spec = google .auth .credentials .Credentials )
785
+ _reset_universe_domain (MOCK_CREDENTIALS )
775
786
ADC_CERT_PATH = "adc_cert_path"
776
787
ADC_KEY_PATH = "adc_key_path"
777
788
ADC_PASSPHRASE = "adc_passphrase"
@@ -1528,6 +1539,7 @@ def test_plus_resources(self):
1528
1539
@unittest .skipIf (not HAS_OAUTH2CLIENT , "oauth2client unavailable." )
1529
1540
def test_oauth2client_credentials (self ):
1530
1541
credentials = mock .Mock (spec = GoogleCredentials )
1542
+ _reset_universe_domain (credentials )
1531
1543
credentials .create_scoped_required .return_value = False
1532
1544
1533
1545
discovery = read_datafile ("plus.json" )
@@ -1536,6 +1548,7 @@ def test_oauth2client_credentials(self):
1536
1548
1537
1549
def test_google_auth_credentials (self ):
1538
1550
credentials = mock .Mock (spec = google .auth .credentials .Credentials )
1551
+ _reset_universe_domain (credentials )
1539
1552
discovery = read_datafile ("plus.json" )
1540
1553
service = build_from_document (discovery , credentials = credentials )
1541
1554
@@ -2340,9 +2353,9 @@ def test_get_media(self):
2340
2353
self .assertEqual (b"standing in for media" , response )
2341
2354
2342
2355
2343
- if HAS_UNIVERSE :
2356
+ class Universe (unittest .TestCase ):
2357
+ if HAS_UNIVERSE :
2344
2358
2345
- class Universe (unittest .TestCase ):
2346
2359
def test_validate_credentials_with_no_client_options (self ):
2347
2360
http = build_http ()
2348
2361
discovery = read_datafile ("zoo.json" )
@@ -2665,6 +2678,66 @@ def test_universe_env_var_configured_with_client_options_universe(self):
2665
2678
2666
2679
assert tasks ._universe_domain == fake_universe
2667
2680
2681
+ else :
2682
+ if hasattr (google .api_core .client_options .ClientOptions , "universe_domain" ):
2683
+
2684
+ def test_client_options_universe_with_older_version_of_api_core (self ):
2685
+ fake_universe = "foo.com"
2686
+ credentials = mock .Mock (spec = google .auth .credentials .Credentials )
2687
+ credentials .universe_domain = fake_universe
2688
+ discovery = read_datafile ("tasks.json" )
2689
+ with self .assertRaises (APICoreVersionError ):
2690
+ tasks = build_from_document (
2691
+ discovery ,
2692
+ credentials = credentials ,
2693
+ client_options = google .api_core .client_options .ClientOptions (
2694
+ universe_domain = fake_universe
2695
+ ),
2696
+ )
2697
+
2698
+ def test_credentials_universe_with_older_version_of_api_core (self ):
2699
+ fake_universe = "foo.com"
2700
+ credentials = mock .Mock (spec = google .auth .credentials .Credentials )
2701
+ credentials .universe_domain = fake_universe
2702
+ discovery = read_datafile ("tasks.json" )
2703
+ with self .assertRaises (APICoreVersionError ):
2704
+ tasks = build_from_document (
2705
+ discovery ,
2706
+ credentials = credentials ,
2707
+ )
2708
+
2709
+ def test_credentials_default_universe_with_older_version_of_api_core (self ):
2710
+ credentials = mock .Mock (spec = google .auth .credentials .Credentials )
2711
+ credentials .universe_domain = "googleapis.com"
2712
+ discovery = read_datafile ("tasks.json" )
2713
+ tasks = build_from_document (
2714
+ discovery ,
2715
+ credentials = credentials ,
2716
+ )
2717
+
2718
+ def test_http_credentials_universe_with_older_version_of_api_core (self ):
2719
+ fake_universe = "foo.com"
2720
+ http = google_auth_httplib2 .AuthorizedHttp (
2721
+ credentials = mock .Mock (universe_domain = fake_universe ), http = build_http ()
2722
+ )
2723
+ discovery = read_datafile ("tasks.json" )
2724
+ with self .assertRaises (APICoreVersionError ):
2725
+ tasks = build_from_document (
2726
+ discovery ,
2727
+ http = http ,
2728
+ )
2729
+
2730
+ def test_http_credentials_default_universe_with_older_version_of_api_core (self ):
2731
+ http = google_auth_httplib2 .AuthorizedHttp (
2732
+ credentials = mock .Mock (universe_domain = "googleapis.com" ),
2733
+ http = build_http (),
2734
+ )
2735
+ discovery = read_datafile ("tasks.json" )
2736
+ tasks = build_from_document (
2737
+ discovery ,
2738
+ http = http ,
2739
+ )
2740
+
2668
2741
2669
2742
if __name__ == "__main__" :
2670
2743
unittest .main ()
0 commit comments