Skip to content

Commit

Permalink
feat: validate credentials for universe before request (#2367)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
ohmayr and gcf-owl-bot[bot] committed Mar 25, 2024
1 parent 6c2b537 commit 77666bf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions googleapiclient/discovery.py
Expand Up @@ -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))
Expand Down
38 changes: 38 additions & 0 deletions tests/test_discovery.py
Expand Up @@ -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()

0 comments on commit 77666bf

Please sign in to comment.