|
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | 15 |
|
16 | | -import os |
17 | | -from unittest import mock |
18 | | - |
19 | 16 | import pytest |
20 | 17 | from google.auth.exceptions import MutualTLSChannelError |
21 | 18 |
|
|
26 | 23 | ) |
27 | 24 |
|
28 | 25 |
|
29 | | -class MockClient: |
30 | | - _DEFAULT_UNIVERSE = "googleapis.com" |
31 | | - DEFAULT_MTLS_ENDPOINT = "foo.mtls.googleapis.com" |
32 | | - _DEFAULT_ENDPOINT_TEMPLATE = "foo.{UNIVERSE_DOMAIN}" |
33 | | - |
34 | | - |
35 | 26 | def test_get_default_mtls_endpoint(): |
36 | 27 | # Test valid API endpoints |
37 | 28 | assert get_default_mtls_endpoint("foo.googleapis.com") == "foo.mtls.googleapis.com" |
@@ -89,156 +80,101 @@ def test_get_default_mtls_endpoint(): |
89 | 80 |
|
90 | 81 |
|
91 | 82 | @pytest.mark.parametrize( |
92 | | - "api_override,client_cert_source,universe_domain,use_mtls_endpoint,default_universe,default_mtls_endpoint,default_endpoint_template,expected", |
| 83 | + "api_override,universe_domain,default_universe,default_mtls_endpoint,default_endpoint_template,use_mtls,expected", |
93 | 84 | [ |
94 | 85 | ( |
95 | 86 | "foo.com", |
96 | | - mock.Mock(), |
97 | 87 | "googleapis.com", |
98 | | - "always", |
99 | 88 | "googleapis.com", |
100 | 89 | "foo.mtls.googleapis.com", |
101 | 90 | "foo.{UNIVERSE_DOMAIN}", |
| 91 | + True, |
102 | 92 | "foo.com", |
103 | 93 | ), |
104 | 94 | ( |
105 | 95 | None, |
106 | | - mock.Mock(), |
107 | | - "googleapis.com", |
108 | | - "auto", |
109 | | - "googleapis.com", |
110 | | - "foo.mtls.googleapis.com", |
111 | | - "foo.{UNIVERSE_DOMAIN}", |
112 | | - "foo.mtls.googleapis.com", |
113 | | - ), |
114 | | - ( |
115 | | - None, |
116 | | - None, |
117 | | - "googleapis.com", |
118 | | - "auto", |
119 | | - "googleapis.com", |
120 | | - "foo.mtls.googleapis.com", |
121 | | - "foo.{UNIVERSE_DOMAIN}", |
122 | | - "foo.googleapis.com", |
123 | | - ), |
124 | | - ( |
125 | | - None, |
126 | | - None, |
127 | | - "googleapis.com", |
128 | | - "always", |
129 | | - "googleapis.com", |
130 | | - "foo.mtls.googleapis.com", |
131 | | - "foo.{UNIVERSE_DOMAIN}", |
132 | | - "foo.mtls.googleapis.com", |
133 | | - ), |
134 | | - ( |
135 | | - None, |
136 | | - mock.Mock(), |
137 | 96 | "googleapis.com", |
138 | | - "always", |
139 | 97 | "googleapis.com", |
140 | 98 | "foo.mtls.googleapis.com", |
141 | 99 | "foo.{UNIVERSE_DOMAIN}", |
| 100 | + True, |
142 | 101 | "foo.mtls.googleapis.com", |
143 | 102 | ), |
144 | 103 | ( |
145 | | - None, |
146 | | - None, |
147 | | - "bar.com", |
148 | | - "never", |
149 | | - "googleapis.com", |
150 | | - "foo.mtls.googleapis.com", |
151 | | - "foo.{UNIVERSE_DOMAIN}", |
152 | | - "foo.bar.com", |
153 | | - ), |
154 | | - ( |
155 | | - None, |
156 | 104 | None, |
157 | 105 | "googleapis.com", |
158 | | - "never", |
159 | 106 | "googleapis.com", |
160 | 107 | "foo.mtls.googleapis.com", |
161 | 108 | "foo.{UNIVERSE_DOMAIN}", |
| 109 | + False, |
162 | 110 | "foo.googleapis.com", |
163 | 111 | ), |
164 | 112 | ( |
165 | 113 | None, |
166 | | - mock.Mock(), |
167 | 114 | "bar.com", |
168 | | - "auto", |
169 | 115 | "googleapis.com", |
170 | 116 | "foo.mtls.googleapis.com", |
171 | 117 | "foo.{UNIVERSE_DOMAIN}", |
| 118 | + True, |
172 | 119 | MutualTLSChannelError, |
173 | 120 | ), |
174 | 121 | ( |
175 | 122 | None, |
176 | | - mock.Mock(), |
177 | 123 | "googleapis.com", |
178 | | - "always", |
179 | 124 | "googleapis.com", |
180 | 125 | None, |
181 | 126 | "foo.{UNIVERSE_DOMAIN}", |
| 127 | + True, |
182 | 128 | ValueError, |
183 | 129 | ), |
184 | 130 | ], |
185 | 131 | ) |
186 | 132 | def test_get_api_endpoint( |
187 | 133 | api_override, |
188 | | - client_cert_source, |
189 | 134 | universe_domain, |
190 | | - use_mtls_endpoint, |
191 | 135 | default_universe, |
192 | 136 | default_mtls_endpoint, |
193 | 137 | default_endpoint_template, |
| 138 | + use_mtls, |
194 | 139 | expected, |
195 | 140 | ): |
196 | | - with mock.patch.dict( |
197 | | - os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_endpoint} |
198 | | - ): |
199 | | - if isinstance(expected, type) and issubclass(expected, Exception): |
200 | | - with pytest.raises(expected): |
201 | | - get_api_endpoint( |
202 | | - api_override, |
203 | | - client_cert_source, |
204 | | - universe_domain, |
205 | | - default_universe, |
206 | | - default_mtls_endpoint, |
207 | | - default_endpoint_template, |
208 | | - ) |
209 | | - else: |
210 | | - assert ( |
211 | | - get_api_endpoint( |
212 | | - api_override, |
213 | | - client_cert_source, |
214 | | - universe_domain, |
215 | | - default_universe, |
216 | | - default_mtls_endpoint, |
217 | | - default_endpoint_template, |
218 | | - ) |
219 | | - == expected |
| 141 | + if isinstance(expected, type) and issubclass(expected, Exception): |
| 142 | + with pytest.raises(expected): |
| 143 | + get_api_endpoint( |
| 144 | + api_override, |
| 145 | + universe_domain, |
| 146 | + default_universe, |
| 147 | + default_mtls_endpoint, |
| 148 | + default_endpoint_template, |
| 149 | + use_mtls, |
220 | 150 | ) |
| 151 | + else: |
| 152 | + assert ( |
| 153 | + get_api_endpoint( |
| 154 | + api_override, |
| 155 | + universe_domain, |
| 156 | + default_universe, |
| 157 | + default_mtls_endpoint, |
| 158 | + default_endpoint_template, |
| 159 | + use_mtls, |
| 160 | + ) |
| 161 | + == expected |
| 162 | + ) |
221 | 163 |
|
222 | 164 |
|
223 | | -def test__get_universe_domain(): |
224 | | - client_universe_domain = "foo.com" |
225 | | - universe_domain_env = "bar.com" |
| 165 | +def test_get_universe_domain(): |
| 166 | + # When universe_domain is provided |
| 167 | + assert get_universe_domain("foo.com", "default.com") == "foo.com" |
| 168 | + assert get_universe_domain(" foo.com ", "default.com") == "foo.com" |
226 | 169 |
|
227 | | - assert ( |
228 | | - get_universe_domain( |
229 | | - client_universe_domain, universe_domain_env, MockClient._DEFAULT_UNIVERSE |
230 | | - ) |
231 | | - == client_universe_domain |
232 | | - ) |
233 | | - assert ( |
234 | | - get_universe_domain(None, universe_domain_env, MockClient._DEFAULT_UNIVERSE) |
235 | | - == universe_domain_env |
236 | | - ) |
237 | | - assert ( |
238 | | - get_universe_domain(None, None, MockClient._DEFAULT_UNIVERSE) |
239 | | - == MockClient._DEFAULT_UNIVERSE |
240 | | - ) |
| 170 | + # When universe_domain is None, falls back to default_universe |
| 171 | + assert get_universe_domain(None, "default.com") == "default.com" |
| 172 | + |
| 173 | + # ValueError raised when resolved value is empty string |
| 174 | + with pytest.raises(ValueError) as excinfo: |
| 175 | + get_universe_domain("", "default.com") |
| 176 | + assert str(excinfo.value) == "Universe Domain cannot be an empty string." |
241 | 177 |
|
242 | 178 | with pytest.raises(ValueError) as excinfo: |
243 | | - get_universe_domain("", None, MockClient._DEFAULT_UNIVERSE) |
| 179 | + get_universe_domain(" ", "default.com") |
244 | 180 | assert str(excinfo.value) == "Universe Domain cannot be an empty string." |
0 commit comments