Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 1.41 KB

File metadata and controls

27 lines (22 loc) · 1.41 KB

Bug Explanation

What was the bug?

The Client.request method failed to refresh the authentication token when self.oauth2_token was a dictionary (or any truthy value that wasn't an OAuth2Token instance). This resulted in API requests being sent without a valid Authorization header when the client was initialized with a dictionary token.

Why did it happen?

The logic to trigger a token refresh was:

if not self.oauth2_token or (
    isinstance(self.oauth2_token, OAuth2Token) and self.oauth2_token.expired
):

When self.oauth2_token is a dictionary:

  1. not self.oauth2_token is False.
  2. isinstance(self.oauth2_token, OAuth2Token) is False.

Thus, the entire condition evaluated to False, skipping the refresh_oauth2() call.

Why does my fix solve it?

The updated condition is:

if not isinstance(self.oauth2_token, OAuth2Token) or self.oauth2_token.expired:

Now, if self.oauth2_token is not an instance of OAuth2Token (e.g., it's a dictionary or None), the first part of the or condition is True, correctly triggering a refresh.

One realistic case / edge case my tests still don’t cover

The tests do not cover the scenario where refresh_oauth2() fails (e.g., raises an exception due to network issues). In a real application, we would need error handling around the refresh call to prevent the application from crashing or proceeding with an invalid state.