Skip to content

Commit 77666bf

Browse files
feat: validate credentials for universe before request (#2367)
* feat: validate credentials for universe before request * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * nit: update comment * remove validation before fetching next page * nit: fix whitespace * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 6c2b537 commit 77666bf

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

googleapiclient/discovery.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,9 @@ def createMethod(methodName, methodDesc, rootDesc, schema):
10501050
def method(self, **kwargs):
10511051
# Don't bother with doc string, it will be over-written by createMethod.
10521052

1053+
# Validate credentials for the configured universe.
1054+
self._validate_credentials()
1055+
10531056
for name in kwargs:
10541057
if name not in parameters.argmap:
10551058
raise TypeError("Got an unexpected keyword argument {}".format(name))

tests/test_discovery.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,6 +2428,44 @@ def test_validate_credentials_with_already_validated_credentials(self):
24282428

24292429
# TODO(google-api-python-client/issues/2365): Add test case for fake configured universe and fake credentials' universe.
24302430

2431+
def test_validate_credentials_before_api_request(self):
2432+
fake_universe = "foo.com"
2433+
2434+
http = google_auth_httplib2.AuthorizedHttp(
2435+
credentials=mock.Mock(universe_domain=universe.DEFAULT_UNIVERSE),
2436+
http=build_http(),
2437+
)
2438+
discovery = read_datafile("tasks.json")
2439+
tasks = build_from_document(
2440+
discovery,
2441+
http=http,
2442+
client_options=google.api_core.client_options.ClientOptions(
2443+
universe_domain=universe.DEFAULT_UNIVERSE
2444+
),
2445+
)
2446+
2447+
tasklists = tasks.tasklists()
2448+
request = tasklists.list()
2449+
2450+
# Check that credentials are indeed verified before request.
2451+
assert tasklists._validate_credentials()
2452+
2453+
http = google_auth_httplib2.AuthorizedHttp(
2454+
credentials=mock.Mock(universe_domain=fake_universe), http=build_http()
2455+
)
2456+
discovery = read_datafile("tasks.json")
2457+
tasks = build_from_document(
2458+
discovery,
2459+
http=http,
2460+
client_options=google.api_core.client_options.ClientOptions(
2461+
universe_domain=universe.DEFAULT_UNIVERSE
2462+
),
2463+
)
2464+
2465+
# Check that credentials are verified before request.
2466+
with self.assertRaises(universe.UniverseMismatchError):
2467+
request = tasks.tasklists().list()
2468+
24312469

24322470
if __name__ == "__main__":
24332471
unittest.main()

0 commit comments

Comments
 (0)