Purge expired RS authorization codes from a recurring job class #274

Merged
raucao merged 1 commits from bugfix/solid_queues_prod into master 2026-11-27 12:06:38 +00:00
3 changed files with 47 additions and 1 deletions

No files matched your search

@@ -0,0 +1,7 @@
class PurgeRsAuthorizationCodesJob < ApplicationJob
queue_as :default
def perform
RsAuthorizationCode.where(expires_at: ..1.hour.ago).delete_all
end
end
+1 -1
View File
@@ -11,5 +11,5 @@
production:
purge_rs_authorization_codes:
command: "RsAuthorizationCode.where(expires_at: ..1.hour.ago).delete_all"
class: PurgeRsAuthorizationCodesJob
schedule: every hour
@@ -0,0 +1,39 @@
require 'rails_helper'
RSpec.describe PurgeRsAuthorizationCodesJob, type: :job do
let(:user) { create :user }
def create_code(**attrs)
RsAuthorizationCode.create!(
user: user,
client_id: "example.com",
redirect_uri: "https://example.com",
permissions: %w(documents:rw),
code_challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
code_challenge_method: "S256",
**attrs
)
end
it "deletes codes expired beyond the grace period" do
expired = create_code(expires_at: 2.hours.ago)
expect {
perform_enqueued_jobs { described_class.perform_later }
}.to change(RsAuthorizationCode, :count).by(-1)
expect(RsAuthorizationCode.exists?(expired.id)).to be(false)
end
it "keeps recently expired and still active codes" do
recently_expired = create_code(expires_at: 30.minutes.ago)
active = create_code(expires_at: 10.minutes.from_now)
expect {
perform_enqueued_jobs { described_class.perform_later }
}.not_to change(RsAuthorizationCode, :count)
expect(RsAuthorizationCode.exists?(recently_expired.id)).to be(true)
expect(RsAuthorizationCode.exists?(active.id)).to be(true)
end
end