-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract expired scope on AccessToken & AccessGrant
Like I did for AccessToken.expires_after, I've reused the `#expiration_time_sql` method built-in to the `doorkeeper` gem to make the code robust against a change of database e.g. from MySQL to PostgreSQL. I've re-opened the `Doorkeeper:AccessGrant` class like I did for `Doorkeeper::AccessToken`. Unfortunately I've had to explicitly include the `Models::ExpirationTimeSqlMath` concern, because while `Doorkeeper::AccessToken` included it, `Doorkeeper:AccessGrant` does not. Extracting this scope has allowed me to simplify `ExpiredOauthAccessRecordsDeleter`. Note that as before I've had to wrap the call to `#expiration_time_sql` in a call to `ActiveRecord::Sanitization::ClassMethods#sanitize_sql` [1] in order to avoid Brakeman failing with a SQL Injection warning. [1]: https://api.rubyonrails.org/v7.0.8/classes/ActiveRecord/Sanitization/ClassMethods.html#method-i-sanitize_sql
- Loading branch information
1 parent
1d985b2
commit b6c4279
Showing
5 changed files
with
36 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Doorkeeper | ||
class AccessGrant < ::ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord | ||
include Models::ExpirationTimeSqlMath | ||
|
||
scope :expired, -> { where.not(expires_in: nil).where("#{sanitize_sql(expiration_time_sql)} < ?", Time.current) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "test_helper" | ||
|
||
class Doorkeeper::AccessGrantTest < ActiveSupport::TestCase | ||
context ".expired" do | ||
should "return grants that have expired" do | ||
grant_expiring_1_day_ago = create(:access_grant, expires_in: -1.day) | ||
grant_expiring_in_1_day = create(:access_grant, expires_in: 1.day) | ||
|
||
grants = Doorkeeper::AccessGrant.expired | ||
|
||
assert_includes grants, grant_expiring_1_day_ago | ||
assert_not_includes grants, grant_expiring_in_1_day | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters