2017-11-13 00:54:48 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 16:50:53 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-05-04 03:49:53 +00:00
|
|
|
RSpec.describe SessionActivation do
|
2017-11-13 00:54:48 +00:00
|
|
|
describe '#detection' do
|
|
|
|
let(:session_activation) { Fabricate(:session_activation, user_agent: 'Chrome/62.0.3202.89') }
|
|
|
|
|
|
|
|
it 'sets a Browser instance as detection' do
|
2023-02-18 02:24:16 +00:00
|
|
|
expect(session_activation.detection).to be_a Browser::Chrome
|
2017-11-13 00:54:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#browser' do
|
|
|
|
before do
|
|
|
|
allow(session_activation).to receive(:detection).and_return(detection)
|
|
|
|
end
|
|
|
|
|
2023-06-22 12:55:22 +00:00
|
|
|
let(:detection) { instance_double(Browser::Chrome, id: 1) }
|
2017-11-13 00:54:48 +00:00
|
|
|
let(:session_activation) { Fabricate(:session_activation) }
|
|
|
|
|
|
|
|
it 'returns detection.id' do
|
|
|
|
expect(session_activation.browser).to be 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#platform' do
|
|
|
|
before do
|
|
|
|
allow(session_activation).to receive(:detection).and_return(detection)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:session_activation) { Fabricate(:session_activation) }
|
2023-06-22 12:55:22 +00:00
|
|
|
let(:detection) { instance_double(Browser::Chrome, platform: instance_double(Browser::Platform, id: 1)) }
|
2017-11-13 00:54:48 +00:00
|
|
|
|
|
|
|
it 'returns detection.platform.id' do
|
|
|
|
expect(session_activation.platform).to be 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.active?' do
|
|
|
|
subject { described_class.active?(id) }
|
|
|
|
|
2023-05-04 03:49:08 +00:00
|
|
|
context 'when id is absent' do
|
2017-11-13 00:54:48 +00:00
|
|
|
let(:id) { nil }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
2023-02-20 04:00:48 +00:00
|
|
|
expect(subject).to be_nil
|
2017-11-13 00:54:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 03:49:08 +00:00
|
|
|
context 'when id is present' do
|
2017-11-13 00:54:48 +00:00
|
|
|
let(:id) { '1' }
|
|
|
|
let!(:session_activation) { Fabricate(:session_activation, session_id: id) }
|
|
|
|
|
2023-05-04 03:49:08 +00:00
|
|
|
context 'when id exists as session_id' do
|
2017-11-13 00:54:48 +00:00
|
|
|
it 'returns true' do
|
2023-02-20 04:00:48 +00:00
|
|
|
expect(subject).to be true
|
2017-11-13 00:54:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 03:49:08 +00:00
|
|
|
context 'when id does not exist as session_id' do
|
2017-11-13 00:54:48 +00:00
|
|
|
before do
|
|
|
|
session_activation.update!(session_id: '2')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
2023-02-20 04:00:48 +00:00
|
|
|
expect(subject).to be false
|
2017-11-13 00:54:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.activate' do
|
|
|
|
let(:options) { { user: Fabricate(:user), session_id: '1' } }
|
|
|
|
|
|
|
|
it 'calls create! and purge_old' do
|
2023-11-07 09:46:28 +00:00
|
|
|
allow(described_class).to receive(:create!).with(**options)
|
|
|
|
allow(described_class).to receive(:purge_old)
|
|
|
|
|
2021-05-06 12:22:54 +00:00
|
|
|
described_class.activate(**options)
|
2023-11-07 09:46:28 +00:00
|
|
|
|
|
|
|
expect(described_class).to have_received(:create!).with(**options)
|
|
|
|
expect(described_class).to have_received(:purge_old)
|
2017-11-13 00:54:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an instance of SessionActivation' do
|
2023-06-06 11:58:33 +00:00
|
|
|
expect(described_class.activate(**options)).to be_a described_class
|
2017-11-13 00:54:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.deactivate' do
|
2023-05-04 03:49:08 +00:00
|
|
|
context 'when id is absent' do
|
2017-11-13 00:54:48 +00:00
|
|
|
let(:id) { nil }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
2023-02-17 12:45:27 +00:00
|
|
|
expect(described_class.deactivate(id)).to be_nil
|
2017-11-13 00:54:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 03:49:08 +00:00
|
|
|
context 'when id exists' do
|
2023-11-09 12:57:23 +00:00
|
|
|
let!(:session_activation) { Fabricate(:session_activation) }
|
2017-11-13 00:54:48 +00:00
|
|
|
|
2023-11-09 12:57:23 +00:00
|
|
|
it 'destroys the record' do
|
|
|
|
described_class.deactivate(session_activation.session_id)
|
2017-11-13 00:54:48 +00:00
|
|
|
|
2023-11-09 12:57:23 +00:00
|
|
|
expect { session_activation.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
2017-11-13 00:54:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.purge_old' do
|
2023-11-09 12:57:23 +00:00
|
|
|
around do |example|
|
|
|
|
before = Rails.configuration.x.max_session_activations
|
|
|
|
Rails.configuration.x.max_session_activations = 1
|
|
|
|
example.run
|
|
|
|
Rails.configuration.x.max_session_activations = before
|
|
|
|
end
|
2017-11-13 00:54:48 +00:00
|
|
|
|
2023-11-09 12:57:23 +00:00
|
|
|
let!(:oldest_session_activation) { Fabricate(:session_activation, created_at: 10.days.ago) }
|
|
|
|
let!(:newest_session_activation) { Fabricate(:session_activation, created_at: 5.days.ago) }
|
|
|
|
|
|
|
|
it 'preserves the newest X records based on config' do
|
2017-11-13 00:54:48 +00:00
|
|
|
described_class.purge_old
|
2023-11-09 12:57:23 +00:00
|
|
|
|
|
|
|
expect { oldest_session_activation.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
expect { newest_session_activation.reload }.to_not raise_error
|
2017-11-13 00:54:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.exclusive' do
|
2023-11-09 12:57:23 +00:00
|
|
|
let!(:unwanted_session_activation) { Fabricate(:session_activation) }
|
|
|
|
let!(:wanted_session_activation) { Fabricate(:session_activation) }
|
2017-11-13 00:54:48 +00:00
|
|
|
|
2023-11-09 12:57:23 +00:00
|
|
|
it 'preserves supplied record and destroys all others' do
|
|
|
|
described_class.exclusive(wanted_session_activation.session_id)
|
2017-11-13 00:54:48 +00:00
|
|
|
|
2023-11-09 12:57:23 +00:00
|
|
|
expect { unwanted_session_activation.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
expect { wanted_session_activation.reload }.to_not raise_error
|
2017-11-13 00:54:48 +00:00
|
|
|
end
|
|
|
|
end
|
2017-06-23 16:50:53 +00:00
|
|
|
end
|