Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add missing before request to async oauth2 credentials. #1420

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions google/oauth2/_credentials_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ async def refresh(self, request):
)
)

@_helpers.copy_docstring(credentials.Credentials)
async def before_request(self, request, method, url, headers):
if not self.valid:
await self.refresh(request)
self.apply(headers)


class UserAccessTokenCredentials(oauth2_credentials.UserAccessTokenCredentials):
"""Access token credentials for user account.
Expand Down
Binary file modified system_tests/secrets.tar.enc
Binary file not shown.
23 changes: 23 additions & 0 deletions tests_async/oauth2/test_credentials_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,29 @@ def test_unpickle_old_credentials_pickle(self):
credentials = pickle.load(f)
assert credentials.quota_project_id is None

@mock.patch("google.oauth2._credentials_async.Credentials.apply", autospec=True)
@mock.patch("google.oauth2._credentials_async.Credentials.refresh", autospec=True)
@pytest.mark.asyncio
async def test_before_request(self, refresh, apply):
cred = self.make_credentials()
assert not cred.valid
await cred.before_request(mock.Mock(), "GET", "https://example.com", {})
refresh.assert_called()
apply.assert_called()

@mock.patch("google.oauth2._credentials_async.Credentials.apply", autospec=True)
@mock.patch("google.oauth2._credentials_async.Credentials.refresh", autospec=True)
@pytest.mark.asyncio
async def test_before_request_no_refresh(self, refresh, apply):
cred = self.make_credentials()
cred.token = refresh
cred.expiry = None

assert cred.valid
await cred.before_request(mock.Mock(), "GET", "https://example.com", {})
refresh.assert_not_called()
apply.assert_called()


class TestUserAccessTokenCredentials(object):
def test_instance(self):
Expand Down