From 77666bf96f9a13b0c0c849f00272ccbf8b9f6441 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Mon, 25 Mar 2024 13:36:54 -0400 Subject: [PATCH] feat: validate credentials for universe before request (#2367) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- googleapiclient/discovery.py | 3 +++ tests/test_discovery.py | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/googleapiclient/discovery.py b/googleapiclient/discovery.py index 5f160485c46..eb01be65ae5 100644 --- a/googleapiclient/discovery.py +++ b/googleapiclient/discovery.py @@ -1050,6 +1050,9 @@ def createMethod(methodName, methodDesc, rootDesc, schema): def method(self, **kwargs): # Don't bother with doc string, it will be over-written by createMethod. + # Validate credentials for the configured universe. + self._validate_credentials() + for name in kwargs: if name not in parameters.argmap: raise TypeError("Got an unexpected keyword argument {}".format(name)) diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 8ffed4907e3..635d2acd594 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -2428,6 +2428,44 @@ def test_validate_credentials_with_already_validated_credentials(self): # TODO(google-api-python-client/issues/2365): Add test case for fake configured universe and fake credentials' universe. + def test_validate_credentials_before_api_request(self): + fake_universe = "foo.com" + + http = google_auth_httplib2.AuthorizedHttp( + credentials=mock.Mock(universe_domain=universe.DEFAULT_UNIVERSE), + http=build_http(), + ) + discovery = read_datafile("tasks.json") + tasks = build_from_document( + discovery, + http=http, + client_options=google.api_core.client_options.ClientOptions( + universe_domain=universe.DEFAULT_UNIVERSE + ), + ) + + tasklists = tasks.tasklists() + request = tasklists.list() + + # Check that credentials are indeed verified before request. + assert tasklists._validate_credentials() + + http = google_auth_httplib2.AuthorizedHttp( + credentials=mock.Mock(universe_domain=fake_universe), http=build_http() + ) + discovery = read_datafile("tasks.json") + tasks = build_from_document( + discovery, + http=http, + client_options=google.api_core.client_options.ClientOptions( + universe_domain=universe.DEFAULT_UNIVERSE + ), + ) + + # Check that credentials are verified before request. + with self.assertRaises(universe.UniverseMismatchError): + request = tasks.tasklists().list() + if __name__ == "__main__": unittest.main()