Improve SessionActivation.activate spec (#36983)

This commit is contained in:
Matt Jankowski 2025-11-26 05:26:39 -05:00 committed by GitHub
parent 00163e89bf
commit cb4f1cc89c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 14 deletions

View File

@ -38,9 +38,7 @@ class SessionActivation < ApplicationRecord
end end
def activate(**) def activate(**)
activation = create!(**) create!(**).tap { purge_old }
purge_old
activation
end end
def deactivate(id) def deactivate(id)

View File

@ -39,20 +39,24 @@ RSpec.describe SessionActivation do
end end
describe '.activate' do describe '.activate' do
let(:options) { { user: Fabricate(:user), session_id: '1' } } let(:user) { Fabricate :user }
let!(:session_activation) { Fabricate :session_activation, user: }
it 'calls create! and purge_old' do around do |example|
allow(described_class).to receive(:create!).with(**options) original = Rails.configuration.x.max_session_activations
allow(described_class).to receive(:purge_old) Rails.configuration.x.max_session_activations = 1
example.run
described_class.activate(**options) Rails.configuration.x.max_session_activations = original
expect(described_class).to have_received(:create!).with(**options)
expect(described_class).to have_received(:purge_old)
end end
it 'returns an instance of SessionActivation' do it 'creates a new activation and purges older ones' do
expect(described_class.activate(**options)).to be_a described_class result = described_class.activate(user: user, session_id: '123')
expect(result)
.to be_a(described_class)
.and have_attributes(session_id: '123', user:)
expect { session_activation.reload }
.to raise_error(ActiveRecord::RecordNotFound)
end end
end end