Skip to content

Conversation

@floehopper
Copy link
Contributor

@floehopper floehopper commented May 7, 2025

The changes in #83 (which were released in v4.1.0) moved the OauthClient class (which uses the oauth2 gem) from the Rails applications into the rpi_auth gem. At that point the oauth2 gem was made a direct dependency of rpi_auth and the direct dependency on oauth2 was removed from the Rails applications.

I think this meant that the oauth2 gem was no longer being required automatically. And, although individual files in the oauth2 gem were being required from individual files in rpi_auth, this meant that not all of the files in oauth2 were being required. In particular, the lib/oauth2.rb file which defines the top-level OAuth2 module and the OAuth2.config method was no longer being required. This led to exceptions like this one in experience-cs when
RpiAuth::OauthClient#refresh_credentials was called:

NoMethodError: undefined method 'config' for module OAuth2

This commit changes the require statements to just require oauth2 which in turn requires all the relevant files in the oauth2 gem. The require statement in spec/rpi_auth/oauth_client_spec.rb is unnecessary, because it's automatically loading RpiAuth::OauthClient which in turn loads oauth2. This should mean we no longer see exceptions like the one above.

I've tested that this fix works in experience-cs in https://github.com/RaspberryPiFoundation/experience-cs/pull/421 - I ran the Rails app and triggered a token refresh to check that NoMethodError was no longer being raised.

The changes in #83 (which were released in v4.1.0) moved the
`OauthClient` class (which uses the `oauth2` gem) from the Rails
applications into the `rpi_auth` gem. At that point the `oauth2` gem was
made a direct dependency of `rpi_auth` and the direct dependency on
`oauth2` was removed from the Rails applications. I _think_ this meant
that the `oauth2` gem was no longer being required automatically. And,
although individual files in the `oauth2` gem were being required from
individual files in `rpi_auth`, this meant that not all of the files in
`oauth2` were being required. In particular, the `lib/oauth2.rb` file
which defines the top-level `OAuth2` module and the `OAuth2.config`
method were no longer being required. This led to exceptions like this
one [1] in `experience-cs` when
`RpiAuth::OauthClient#refresh_credentials` was called:

    NoMethodError: undefined method 'config' for module OAuth2

This commit changes the require statements to just require `oauth2`
which in turn requires all the relevant files [2] in the `oauth2` gem.
The require statement in `spec/rpi_auth/oauth_client_spec.rb` is
unnecessary, because it's automatically loading `RpiAuth::OauthClient`
which in turn loads `oauth2`. This should mean we no longer see
exceptions like the one above.

[1]: https://raspberrypifoundation.sentry.io/issues/6590882516?project=4508953237651456
[2]: https://gitlab.com/oauth-xx/oauth2/-/blob/v2.0.9/lib/oauth2.rb?ref_type=tags#L11-23
@github-actions
Copy link

github-actions bot commented May 7, 2025

Test coverage: 100.0%

@floehopper floehopper requested review from grega and patch0 May 7, 2025 13:44
Copy link
Contributor

@patch0 patch0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. Thanks!

@floehopper floehopper merged commit 97e40f8 into main May 7, 2025
13 checks passed
@floehopper floehopper deleted the fix-requiring-of-oauth2 branch May 7, 2025 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants