54
54
except ImportError :
55
55
HAS_OAUTH2CLIENT = False
56
56
57
+ try :
58
+ from google .api_core import universe
59
+
60
+ HAS_UNIVERSE = True
61
+ except ImportError :
62
+ HAS_UNIVERSE = False
63
+
57
64
from googleapiclient import _helpers as util
58
65
from googleapiclient .discovery import (
59
66
DISCOVERY_URI ,
@@ -2118,6 +2125,7 @@ def test_resumable_media_handle_resume_of_upload_of_unknown_size(self):
2118
2125
def test_pickle (self ):
2119
2126
sorted_resource_keys = [
2120
2127
"_baseUrl" ,
2128
+ "_credentials_validated" ,
2121
2129
"_developerKey" ,
2122
2130
"_dynamic_attrs" ,
2123
2131
"_http" ,
@@ -2126,6 +2134,7 @@ def test_pickle(self):
2126
2134
"_resourceDesc" ,
2127
2135
"_rootDesc" ,
2128
2136
"_schema" ,
2137
+ "_universe_domain" ,
2129
2138
"animals" ,
2130
2139
"global_" ,
2131
2140
"load" ,
@@ -2331,5 +2340,94 @@ def test_get_media(self):
2331
2340
self .assertEqual (b"standing in for media" , response )
2332
2341
2333
2342
2343
+ if HAS_UNIVERSE :
2344
+
2345
+ class Universe (unittest .TestCase ):
2346
+ def test_validate_credentials_with_no_universe (self ):
2347
+ fake_universe = "foo.com"
2348
+
2349
+ http = google_auth_httplib2 .AuthorizedHttp (
2350
+ credentials = None , http = build_http ()
2351
+ )
2352
+ discovery = read_datafile ("zoo.json" )
2353
+ service = build_from_document (
2354
+ discovery ,
2355
+ http = http ,
2356
+ client_options = google .api_core .client_options .ClientOptions (
2357
+ universe_domain = universe .DEFAULT_UNIVERSE
2358
+ ),
2359
+ )
2360
+
2361
+ assert service ._validate_credentials ()
2362
+
2363
+ # TODO(google-api-python-client/issues/2365): Add test case for no configured universe and fake credentials' universe.
2364
+
2365
+ # TODO(google-api-python-client/issues/2365): Add test case for not specifying universe domain via client option.
2366
+
2367
+ def test_validate_credentials_with_default_universe (self ):
2368
+ fake_universe = "foo.com"
2369
+
2370
+ http = google_auth_httplib2 .AuthorizedHttp (
2371
+ credentials = mock .Mock (universe_domain = universe .DEFAULT_UNIVERSE ),
2372
+ http = build_http (),
2373
+ )
2374
+ discovery = read_datafile ("zoo.json" )
2375
+ service = build_from_document (
2376
+ discovery ,
2377
+ http = http ,
2378
+ client_options = google .api_core .client_options .ClientOptions (
2379
+ universe_domain = universe .DEFAULT_UNIVERSE
2380
+ ),
2381
+ )
2382
+
2383
+ assert service ._validate_credentials ()
2384
+
2385
+ # TODO(google-api-python-client/issues/2365): # Add test case for "googleapis.com" configured universe and fake credentials' universe.
2386
+
2387
+ def test_validate_credentials_with_a_different_universe (self ):
2388
+ fake_universe = "foo.com"
2389
+
2390
+ # TODO(google-api-python-client/issues/2365): Add test case for fake configured universe and fake credentials' universe.
2391
+
2392
+ http = google_auth_httplib2 .AuthorizedHttp (
2393
+ credentials = mock .Mock (universe_domain = fake_universe ), http = build_http ()
2394
+ )
2395
+ discovery = read_datafile ("zoo.json" )
2396
+ service = build_from_document (
2397
+ discovery ,
2398
+ http = http ,
2399
+ client_options = google .api_core .client_options .ClientOptions (
2400
+ universe_domain = universe .DEFAULT_UNIVERSE
2401
+ ),
2402
+ )
2403
+
2404
+ with self .assertRaises (universe .UniverseMismatchError ):
2405
+ service ._validate_credentials ()
2406
+
2407
+ def test_validate_credentials_with_already_validated_credentials (self ):
2408
+ fake_universe = "foo.com"
2409
+
2410
+ http = google_auth_httplib2 .AuthorizedHttp (
2411
+ credentials = mock .Mock (universe_domain = universe .DEFAULT_UNIVERSE ),
2412
+ http = build_http (),
2413
+ )
2414
+ discovery = read_datafile ("zoo.json" )
2415
+ service = build_from_document (
2416
+ discovery ,
2417
+ http = http ,
2418
+ client_options = google .api_core .client_options .ClientOptions (
2419
+ universe_domain = universe .DEFAULT_UNIVERSE
2420
+ ),
2421
+ )
2422
+
2423
+ assert service ._validate_credentials ()
2424
+ assert service ._credentials_validated
2425
+
2426
+ # Calling service._validate_credentials() again returns service.credentials_validated.
2427
+ assert service ._validate_credentials ()
2428
+
2429
+ # TODO(google-api-python-client/issues/2365): Add test case for fake configured universe and fake credentials' universe.
2430
+
2431
+
2334
2432
if __name__ == "__main__" :
2335
2433
unittest .main ()
0 commit comments