@@ -2343,6 +2343,25 @@ def test_get_media(self):
2343
2343
if HAS_UNIVERSE :
2344
2344
2345
2345
class Universe (unittest .TestCase ):
2346
+ def test_validate_credentials_with_no_client_options (self ):
2347
+ http = build_http ()
2348
+ discovery = read_datafile ("zoo.json" )
2349
+ service = build_from_document (
2350
+ discovery ,
2351
+ http = http ,
2352
+ )
2353
+ assert service ._validate_credentials ()
2354
+
2355
+ def test_validate_credentials_with_client_options_without_universe (self ):
2356
+ http = build_http ()
2357
+ discovery = read_datafile ("zoo.json" )
2358
+ service = build_from_document (
2359
+ discovery ,
2360
+ http = http ,
2361
+ client_options = google .api_core .client_options .ClientOptions (),
2362
+ )
2363
+ assert service ._validate_credentials ()
2364
+
2346
2365
def test_validate_credentials_with_no_universe (self ):
2347
2366
fake_universe = "foo.com"
2348
2367
@@ -2360,9 +2379,20 @@ def test_validate_credentials_with_no_universe(self):
2360
2379
2361
2380
assert service ._validate_credentials ()
2362
2381
2363
- # TODO(google-api-python-client/issues/2365): Add test case for no configured universe and fake credentials' universe.
2382
+ http = google_auth_httplib2 .AuthorizedHttp (
2383
+ credentials = None , http = build_http ()
2384
+ )
2385
+ discovery = read_datafile ("zoo.json" )
2386
+ service = build_from_document (
2387
+ discovery ,
2388
+ http = http ,
2389
+ client_options = google .api_core .client_options .ClientOptions (
2390
+ universe_domain = fake_universe
2391
+ ),
2392
+ )
2364
2393
2365
- # TODO(google-api-python-client/issues/2365): Add test case for not specifying universe domain via client option.
2394
+ with self .assertRaises (universe .UniverseMismatchError ):
2395
+ service ._validate_credentials ()
2366
2396
2367
2397
def test_validate_credentials_with_default_universe (self ):
2368
2398
fake_universe = "foo.com"
@@ -2382,12 +2412,39 @@ def test_validate_credentials_with_default_universe(self):
2382
2412
2383
2413
assert service ._validate_credentials ()
2384
2414
2385
- # TODO(google-api-python-client/issues/2365): # Add test case for "googleapis.com" configured universe and fake credentials' universe.
2415
+ http = google_auth_httplib2 .AuthorizedHttp (
2416
+ credentials = mock .Mock (universe_domain = universe .DEFAULT_UNIVERSE ),
2417
+ http = build_http (),
2418
+ )
2419
+ discovery = read_datafile ("zoo.json" )
2420
+ service = build_from_document (
2421
+ discovery ,
2422
+ http = http ,
2423
+ client_options = google .api_core .client_options .ClientOptions (
2424
+ universe_domain = fake_universe
2425
+ ),
2426
+ )
2427
+
2428
+ with self .assertRaises (universe .UniverseMismatchError ):
2429
+ service ._validate_credentials ()
2386
2430
2387
2431
def test_validate_credentials_with_a_different_universe (self ):
2388
2432
fake_universe = "foo.com"
2389
2433
2390
- # TODO(google-api-python-client/issues/2365): Add test case for fake configured universe and fake credentials' universe.
2434
+ http = google_auth_httplib2 .AuthorizedHttp (
2435
+ credentials = mock .Mock (universe_domain = fake_universe ),
2436
+ http = build_http (),
2437
+ )
2438
+ discovery = read_datafile ("zoo.json" )
2439
+ service = build_from_document (
2440
+ discovery ,
2441
+ http = http ,
2442
+ client_options = google .api_core .client_options .ClientOptions (
2443
+ universe_domain = fake_universe
2444
+ ),
2445
+ )
2446
+
2447
+ assert service ._validate_credentials ()
2391
2448
2392
2449
http = google_auth_httplib2 .AuthorizedHttp (
2393
2450
credentials = mock .Mock (universe_domain = fake_universe ), http = build_http ()
@@ -2426,21 +2483,34 @@ def test_validate_credentials_with_already_validated_credentials(self):
2426
2483
# Calling service._validate_credentials() again returns service.credentials_validated.
2427
2484
assert service ._validate_credentials ()
2428
2485
2429
- # TODO(google-api-python-client/issues/2365): Add test case for fake configured universe and fake credentials' universe.
2430
-
2431
- def test_validate_credentials_before_api_request (self ):
2432
- fake_universe = "foo.com"
2433
-
2434
2486
http = google_auth_httplib2 .AuthorizedHttp (
2435
- credentials = mock .Mock (universe_domain = universe .DEFAULT_UNIVERSE ),
2436
- http = build_http (),
2487
+ credentials = mock .Mock (universe_domain = fake_universe ), http = build_http ()
2488
+ )
2489
+ discovery = read_datafile ("zoo.json" )
2490
+ service = build_from_document (
2491
+ discovery ,
2492
+ http = http ,
2493
+ client_options = google .api_core .client_options .ClientOptions (
2494
+ universe_domain = fake_universe
2495
+ ),
2437
2496
)
2497
+
2498
+ assert service ._validate_credentials ()
2499
+ assert service ._credentials_validated
2500
+
2501
+ # Calling service._validate_credentials() again returns service.credentials_validated.
2502
+ assert service ._validate_credentials ()
2503
+
2504
+ def test_validate_credentials_before_api_request_success (self ):
2505
+ fake_universe = "foo.com"
2506
+ credentials = mock .Mock (spec = google .auth .credentials .Credentials )
2507
+ credentials .universe_domain = fake_universe
2438
2508
discovery = read_datafile ("tasks.json" )
2439
2509
tasks = build_from_document (
2440
2510
discovery ,
2441
- http = http ,
2511
+ credentials = credentials ,
2442
2512
client_options = google .api_core .client_options .ClientOptions (
2443
- universe_domain = universe . DEFAULT_UNIVERSE
2513
+ universe_domain = fake_universe
2444
2514
),
2445
2515
)
2446
2516
@@ -2450,13 +2520,14 @@ def test_validate_credentials_before_api_request(self):
2450
2520
# Check that credentials are indeed verified before request.
2451
2521
assert tasklists ._validate_credentials ()
2452
2522
2453
- http = google_auth_httplib2 .AuthorizedHttp (
2454
- credentials = mock .Mock (universe_domain = fake_universe ), http = build_http ()
2455
- )
2523
+ def test_validate_credentials_before_api_request_failure (self ):
2524
+ fake_universe = "foo.com"
2525
+ credentials = mock .Mock (spec = google .auth .credentials .Credentials )
2526
+ credentials .universe_domain = fake_universe
2456
2527
discovery = read_datafile ("tasks.json" )
2457
2528
tasks = build_from_document (
2458
2529
discovery ,
2459
- http = http ,
2530
+ credentials = credentials ,
2460
2531
client_options = google .api_core .client_options .ClientOptions (
2461
2532
universe_domain = universe .DEFAULT_UNIVERSE
2462
2533
),
@@ -2466,6 +2537,69 @@ def test_validate_credentials_before_api_request(self):
2466
2537
with self .assertRaises (universe .UniverseMismatchError ):
2467
2538
request = tasks .tasklists ().list ()
2468
2539
2540
+ def test_validate_credentials_before_another_universe_api_request_failure (self ):
2541
+ fake_universe = "foo.com"
2542
+ credentials = mock .Mock (spec = google .auth .credentials .Credentials )
2543
+ credentials .universe_domain = fake_universe
2544
+ another_fake_universe = "bar.com"
2545
+ discovery = read_datafile ("tasks.json" )
2546
+ tasks = build_from_document (
2547
+ discovery ,
2548
+ credentials = credentials ,
2549
+ client_options = google .api_core .client_options .ClientOptions (
2550
+ universe_domain = another_fake_universe
2551
+ ),
2552
+ )
2553
+
2554
+ # Check that credentials are verified before request.
2555
+ with self .assertRaises (universe .UniverseMismatchError ):
2556
+ request = tasks .tasklists ().list ()
2557
+
2558
+ def test_client_options_with_empty_universe (self ):
2559
+ fake_universe = "foo.com"
2560
+ credentials = mock .Mock (spec = google .auth .credentials .Credentials )
2561
+ discovery = read_datafile ("tasks.json" )
2562
+
2563
+ with self .assertRaises (universe .EmptyUniverseError ):
2564
+ tasks = build_from_document (
2565
+ discovery ,
2566
+ credentials = credentials ,
2567
+ client_options = google .api_core .client_options .ClientOptions (
2568
+ universe_domain = ""
2569
+ ),
2570
+ )
2571
+
2572
+ def test_client_options_universe_configured_with_mtls (self ):
2573
+ fake_universe = "foo.com"
2574
+ discovery = read_datafile ("tasks.json" )
2575
+
2576
+ with self .assertRaises (MutualTLSChannelError ):
2577
+ with mock .patch .dict (
2578
+ "os.environ" , {"GOOGLE_API_USE_MTLS_ENDPOINT" : "always" }
2579
+ ):
2580
+ tasks = build_from_document (
2581
+ discovery ,
2582
+ client_options = google .api_core .client_options .ClientOptions (
2583
+ universe_domain = fake_universe
2584
+ ),
2585
+ )
2586
+
2587
+ def test_client_options_universe_configured_with_api_override (self ):
2588
+ fake_universe = "foo.com"
2589
+ fake_api_endpoint = "https://www.bar.com/"
2590
+ credentials = mock .Mock (universe_domain = fake_universe )
2591
+ discovery = read_datafile ("tasks.json" )
2592
+
2593
+ tasks = build_from_document (
2594
+ discovery ,
2595
+ credentials = credentials ,
2596
+ client_options = google .api_core .client_options .ClientOptions (
2597
+ api_endpoint = fake_api_endpoint , universe_domain = fake_universe
2598
+ ),
2599
+ )
2600
+
2601
+ assert tasks ._baseUrl == fake_api_endpoint
2602
+
2469
2603
2470
2604
if __name__ == "__main__" :
2471
2605
unittest .main ()
0 commit comments