3232import json
3333import os
3434import pickle
35- import pytest
3635import re
3736import sys
3837import unittest
3938from unittest import mock
4039import urllib
4140
4241import google .api_core .exceptions
43- import google .auth .credentials
4442from google .auth import __version__ as auth_version
43+ import google .auth .credentials
4544from google .auth .exceptions import MutualTLSChannelError
4645import google_auth_httplib2
4746import httplib2
4847from parameterized import parameterized
48+ import pytest
4949import uritemplate
5050
5151try :
6464 HAS_UNIVERSE = False
6565
6666from googleapiclient import _helpers as util
67- from googleapiclient .discovery import (DISCOVERY_URI ,
68- MEDIA_BODY_PARAMETER_DEFAULT_VALUE ,
69- MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE ,
70- STACK_QUERY_PARAMETER_DEFAULT_VALUE ,
71- STACK_QUERY_PARAMETERS ,
72- V1_DISCOVERY_URI , V2_DISCOVERY_URI ,
73- APICoreVersionError ,
74- ResourceMethodParameters ,
75- _fix_up_media_path_base_url ,
76- _fix_up_media_upload ,
77- _fix_up_method_description ,
78- _fix_up_parameters , _urljoin , build ,
79- build_from_document , key2param )
67+ from googleapiclient .discovery import (
68+ DISCOVERY_URI ,
69+ MEDIA_BODY_PARAMETER_DEFAULT_VALUE ,
70+ MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE ,
71+ STACK_QUERY_PARAMETER_DEFAULT_VALUE ,
72+ STACK_QUERY_PARAMETERS ,
73+ V1_DISCOVERY_URI ,
74+ V2_DISCOVERY_URI ,
75+ APICoreVersionError ,
76+ ResourceMethodParameters ,
77+ _fix_up_media_path_base_url ,
78+ _fix_up_media_upload ,
79+ _fix_up_method_description ,
80+ _fix_up_parameters ,
81+ _urljoin ,
82+ build ,
83+ build_from_document ,
84+ key2param ,
85+ )
8086from googleapiclient .discovery_cache import DISCOVERY_DOC_MAX_AGE
8187from googleapiclient .discovery_cache .base import Cache
82- from googleapiclient .errors import (HttpError , InvalidJsonError ,
83- MediaUploadSizeError , ResumableUploadError ,
84- UnacceptableMimeTypeError ,
85- UnknownApiNameOrVersion , UnknownFileType )
86- from googleapiclient .http import (HttpMock , HttpMockSequence , MediaFileUpload ,
87- MediaIoBaseUpload , MediaUpload ,
88- MediaUploadProgress , build_http ,
89- tunnel_patch )
88+ from googleapiclient .errors import (
89+ HttpError ,
90+ InvalidJsonError ,
91+ MediaUploadSizeError ,
92+ ResumableUploadError ,
93+ UnacceptableMimeTypeError ,
94+ UnknownApiNameOrVersion ,
95+ UnknownFileType ,
96+ )
97+ from googleapiclient .http import (
98+ HttpMock ,
99+ HttpMockSequence ,
100+ MediaFileUpload ,
101+ MediaIoBaseUpload ,
102+ MediaUpload ,
103+ MediaUploadProgress ,
104+ build_http ,
105+ tunnel_patch ,
106+ )
90107from googleapiclient .model import JsonModel
91108from googleapiclient .schema import Schemas
92109
@@ -141,6 +158,7 @@ def read_datafile(filename, mode="r"):
141158 with open (datafile (filename ), mode = mode ) as f :
142159 return f .read ()
143160
161+
144162def parse_version_to_tuple (version_string ):
145163 """Safely converts a semantic version string to a comparable tuple of integers.
146164
@@ -164,6 +182,7 @@ def parse_version_to_tuple(version_string):
164182 break
165183 return tuple (parts )
166184
185+
167186class SetupHttplib2 (unittest .TestCase ):
168187 def test_retries (self ):
169188 # Merely loading googleapiclient.discovery should set the RETRIES to 1.
@@ -799,6 +818,7 @@ def test_self_signed_jwt_disabled(self):
799818 "cert_configs" : {},
800819}
801820
821+
802822class DiscoveryFromDocumentMutualTLS (unittest .TestCase ):
803823 MOCK_CREDENTIALS = mock .Mock (spec = google .auth .credentials .Credentials )
804824 _reset_universe_domain (MOCK_CREDENTIALS )
@@ -905,7 +925,7 @@ def test_mtls_with_provided_client_cert(
905925
906926 @parameterized .expand (
907927 [
908- ("never" , "" , CONFIG_DATA_WITH_WORKLOAD , REGULAR_ENDPOINT ),
928+ ("never" , "" , CONFIG_DATA_WITH_WORKLOAD , REGULAR_ENDPOINT ),
909929 ("auto" , "" , CONFIG_DATA_WITH_WORKLOAD , MTLS_ENDPOINT ),
910930 ("always" , "" , CONFIG_DATA_WITH_WORKLOAD , MTLS_ENDPOINT ),
911931 ("never" , "" , CONFIG_DATA_WITHOUT_WORKLOAD , REGULAR_ENDPOINT ),
@@ -914,18 +934,18 @@ def test_mtls_with_provided_client_cert(
914934 ]
915935 )
916936 @pytest .mark .skipif (
917- parse_version_to_tuple (auth_version ) < (2 ,43 ,0 ),
937+ parse_version_to_tuple (auth_version ) < (2 , 43 , 0 ),
918938 reason = "automatic mtls enablement when supported certs present only"
919- "enabled in google-auth<=2.43.0"
939+ "enabled in google-auth<=2.43.0" ,
920940 )
921941 def test_mtls_with_provided_client_cert_unset_environment_variable (
922942 self , use_mtls_env , use_client_cert , config_data , base_url
923943 ):
924944 """Tests that mTLS is correctly handled when a client certificate is provided.
925945
926946 This test case verifies that when a client certificate is explicitly provided
927- via `client_options` and GOOGLE_API_USE_CLIENT_CERTIFICATE is unset, the
928- discovery document build process correctly configures the base URL for mTLS
947+ via `client_options` and GOOGLE_API_USE_CLIENT_CERTIFICATE is unset, the
948+ discovery document build process correctly configures the base URL for mTLS
929949 or regular endpoints based on the `GOOGLE_API_USE_MTLS_ENDPOINT` environment variable.
930950 """
931951 if hasattr (google .auth .transport .mtls , "should_use_client_cert" ):
@@ -941,7 +961,10 @@ def test_mtls_with_provided_client_cert_unset_environment_variable(
941961 "os.environ" , {"GOOGLE_API_USE_CLIENT_CERTIFICATE" : use_client_cert }
942962 ):
943963 with mock .patch ("builtins.open" , m ):
944- with mock .patch .dict ("os.environ" , {"GOOGLE_API_CERTIFICATE_CONFIG" : config_filename }):
964+ with mock .patch .dict (
965+ "os.environ" ,
966+ {"GOOGLE_API_CERTIFICATE_CONFIG" : config_filename },
967+ ):
945968 plus = build_from_document (
946969 discovery ,
947970 credentials = self .MOCK_CREDENTIALS ,
@@ -1029,6 +1052,7 @@ def test_mtls_with_default_client_cert(
10291052 self .assertIsNotNone (plus )
10301053 self .check_http_client_cert (plus , has_client_cert = use_client_cert )
10311054 self .assertEqual (plus ._baseUrl , base_url )
1055+
10321056 @parameterized .expand (
10331057 [
10341058 ("never" , "" , CONFIG_DATA_WITH_WORKLOAD , REGULAR_ENDPOINT ),
@@ -1046,9 +1070,9 @@ def test_mtls_with_default_client_cert(
10461070 "google.auth.transport.mtls.default_client_encrypted_cert_source" , autospec = True
10471071 )
10481072 @pytest .mark .skipif (
1049- parse_version_to_tuple (auth_version ) < (2 ,43 ,0 ),
1073+ parse_version_to_tuple (auth_version ) < (2 , 43 , 0 ),
10501074 reason = "automatic mtls enablement when supported certs present only"
1051- "enabled in google-auth<=2.43.0"
1075+ "enabled in google-auth<=2.43.0" ,
10521076 )
10531077 def test_mtls_with_default_client_cert_with_unset_environment_variable (
10541078 self ,
@@ -1085,12 +1109,15 @@ def test_mtls_with_default_client_cert_with_unset_environment_variable(
10851109 "os.environ" , {"GOOGLE_API_USE_CLIENT_CERTIFICATE" : use_client_cert }
10861110 ):
10871111 with mock .patch ("builtins.open" , m ):
1088- with mock .patch .dict ("os.environ" , {"GOOGLE_API_CERTIFICATE_CONFIG" : config_filename }):
1112+ with mock .patch .dict (
1113+ "os.environ" ,
1114+ {"GOOGLE_API_CERTIFICATE_CONFIG" : config_filename },
1115+ ):
10891116 plus = build_from_document (
1090- discovery ,
1091- credentials = self .MOCK_CREDENTIALS ,
1092- adc_cert_path = self .ADC_CERT_PATH ,
1093- adc_key_path = self .ADC_KEY_PATH ,
1117+ discovery ,
1118+ credentials = self .MOCK_CREDENTIALS ,
1119+ adc_cert_path = self .ADC_CERT_PATH ,
1120+ adc_key_path = self .ADC_KEY_PATH ,
10941121 )
10951122 self .assertIsNotNone (plus )
10961123 self .assertEqual (plus ._baseUrl , base_url )
0 commit comments