Skip to content

Conversation

@floehopper
Copy link
Contributor

Previously, if User#expires_at was defined, but it was set to nil, the expires_in calculation was returning a large negative integer. This was because nil.to_i is 0 and thus user.expires_at.to_i - Time.zone.now.to_i was a value like -1744820986 (for 2025-04-16 17:30) which meant the credentials were effectively immediately expired some time ago.

This isn't a problem in e.g. code-club-frontend, because the user factory sets User#expires_at to a time a random number of seconds in the future, i.e. `rand(60..300).seconds.from_now.

We've had to do something similar in experience-cs, but I think a better solution is to default to using the 3600 fallback value if User#expires_at is nil just like we do if User#expires_at is not defined.

@floehopper floehopper requested review from grega and patch0 April 16, 2025 16:36
@github-actions
Copy link

github-actions bot commented Apr 16, 2025

Test coverage: 100.0%

grega
grega previously approved these changes Apr 16, 2025
@grega
Copy link
Member

grega commented Apr 16, 2025

Thanks for these improvements @floehopper, much appreciated.

Previously, if `User#expires_at` was defined, but it was set to `nil`,
the `expires_in` calculation was returning a large negative integer.
This was because `nil.to_i` is 0 and thus `user.expires_at.to_i -
Time.zone.now.to_i` was a value like `-1744820986` (for 2025-04-16
17:30).

This isn't a problem in e.g. `code-club-frontend`, because the user
factory sets `User#expires_at` to a time a random number of seconds in
the future, i.e. `rand(60..300).seconds.from_now [1].

We've had to do something similar in `experience-cs` [2], but I think a
better solution is to default to using the 3600 fallback value if
`User#expires_at` is `nil` just like we do if `User#expires_at` is not
defined.

[1]: https://github.com/RaspberryPiFoundation/code-club-frontend/blob/f7e965798d910584fed0d1eb7867f32a899f9ce8/spec/factories/user.rb#L21
[2]: https://github.com/RaspberryPiFoundation/experience-cs/blob/47d11bd60da7b7bd93968534542f84edca4054aa/spec/factories/user.rb#L5
@floehopper floehopper force-pushed the fix-use-of-user-expires-at-in-spec-helpers branch from c4d19f0 to 64bf109 Compare April 16, 2025 16:49
@floehopper
Copy link
Contributor Author

@grega Thanks - just realised I forgot to add a changelog entry - force-pushing that now which will probably require another approval!

@floehopper
Copy link
Contributor Author

Thanks, @grega. I've just run the changes against experience-cs locally and it seems to work OK, so I'll get it merged.

@floehopper floehopper merged commit e54cc7a into main Apr 16, 2025
13 checks passed
@floehopper floehopper deleted the fix-use-of-user-expires-at-in-spec-helpers branch April 16, 2025 16:59
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