Fix logic in "last used at per application" oauth token list (#32912)

This commit is contained in:
Matt Jankowski 2024-11-15 10:52:37 -05:00 committed by GitHub
parent 297ce9ef44
commit 319109029d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 7 deletions

View File

@ -35,12 +35,6 @@ class Oauth::AuthorizedApplicationsController < Doorkeeper::AuthorizedApplicatio
end
def set_last_used_at_by_app
@last_used_at_by_app = Doorkeeper::AccessToken
.select('DISTINCT ON (application_id) application_id, last_used_at')
.where(resource_owner_id: current_resource_owner.id)
.where.not(last_used_at: nil)
.order(application_id: :desc, last_used_at: :desc)
.pluck(:application_id, :last_used_at)
.to_h
@last_used_at_by_app = current_resource_owner.applications_last_used
end
end

View File

@ -280,6 +280,15 @@ class User < ApplicationRecord
save!
end
def applications_last_used
Doorkeeper::AccessToken
.where(resource_owner_id: id)
.where.not(last_used_at: nil)
.group(:application_id)
.maximum(:last_used_at)
.to_h
end
def token_for_app(app)
return nil if app.nil? || app.owner != self

View File

@ -598,4 +598,27 @@ RSpec.describe User do
end
end
end
describe '#applications_last_used' do
let!(:user) { Fabricate(:user) }
let!(:never_used_application) { Fabricate :application, owner: user }
let!(:application_one) { Fabricate :application, owner: user }
let!(:application_two) { Fabricate :application, owner: user }
before do
_other_user_token = Fabricate :access_token, last_used_at: 3.days.ago
_never_used_token = Fabricate :access_token, application: never_used_application, resource_owner_id: user.id, last_used_at: nil
_app_one_old_token = Fabricate :access_token, application: application_one, resource_owner_id: user.id, last_used_at: 5.days.ago
_app_one_new_token = Fabricate :access_token, application: application_one, resource_owner_id: user.id, last_used_at: 1.day.ago
_never_used_token = Fabricate :access_token, application: application_two, resource_owner_id: user.id, last_used_at: 5.days.ago
end
it 'returns a hash of unique applications with last used values' do
expect(user.applications_last_used)
.to include(application_one.id => be_within(1.0).of(1.day.ago))
.and include(application_two.id => be_within(1.0).of(5.days.ago))
.and not_include(never_used_application.id)
end
end
end