2017-11-13 01:54:48 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 18:50:53 +02:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-05-04 05:49:53 +02:00
|
|
|
RSpec.describe SessionActivation do
|
2017-11-13 01:54:48 +01: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 03:24:16 +01:00
|
|
|
expect(session_activation.detection).to be_a Browser::Chrome
|
2017-11-13 01:54:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#browser' do
|
|
|
|
before do
|
|
|
|
allow(session_activation).to receive(:detection).and_return(detection)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:detection) { double(id: 1) }
|
|
|
|
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) }
|
|
|
|
let(:detection) { double(platform: double(id: 1)) }
|
|
|
|
|
|
|
|
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 05:49:08 +02:00
|
|
|
context 'when id is absent' do
|
2017-11-13 01:54:48 +01:00
|
|
|
let(:id) { nil }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
2023-02-20 05:00:48 +01:00
|
|
|
expect(subject).to be_nil
|
2017-11-13 01:54:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 05:49:08 +02:00
|
|
|
context 'when id is present' do
|
2017-11-13 01:54:48 +01:00
|
|
|
let(:id) { '1' }
|
|
|
|
let!(:session_activation) { Fabricate(:session_activation, session_id: id) }
|
|
|
|
|
2023-05-04 05:49:08 +02:00
|
|
|
context 'when id exists as session_id' do
|
2017-11-13 01:54:48 +01:00
|
|
|
it 'returns true' do
|
2023-02-20 05:00:48 +01:00
|
|
|
expect(subject).to be true
|
2017-11-13 01:54:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 05:49:08 +02:00
|
|
|
context 'when id does not exist as session_id' do
|
2017-11-13 01:54:48 +01:00
|
|
|
before do
|
|
|
|
session_activation.update!(session_id: '2')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
2023-02-20 05:00:48 +01:00
|
|
|
expect(subject).to be false
|
2017-11-13 01:54:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.activate' do
|
|
|
|
let(:options) { { user: Fabricate(:user), session_id: '1' } }
|
|
|
|
|
|
|
|
it 'calls create! and purge_old' do
|
2021-05-06 14:22:54 +02:00
|
|
|
expect(described_class).to receive(:create!).with(**options)
|
2017-11-13 01:54:48 +01:00
|
|
|
expect(described_class).to receive(:purge_old)
|
2021-05-06 14:22:54 +02:00
|
|
|
described_class.activate(**options)
|
2017-11-13 01:54:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an instance of SessionActivation' do
|
2023-06-06 13:58:33 +02:00
|
|
|
expect(described_class.activate(**options)).to be_a described_class
|
2017-11-13 01:54:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.deactivate' do
|
2023-05-04 05:49:08 +02:00
|
|
|
context 'when id is absent' do
|
2017-11-13 01:54:48 +01:00
|
|
|
let(:id) { nil }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
2023-02-17 13:45:27 +01:00
|
|
|
expect(described_class.deactivate(id)).to be_nil
|
2017-11-13 01:54:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 05:49:08 +02:00
|
|
|
context 'when id exists' do
|
2017-11-13 01:54:48 +01:00
|
|
|
let(:id) { '1' }
|
|
|
|
|
|
|
|
it 'calls where.destroy_all' do
|
|
|
|
expect(described_class).to receive_message_chain(:where, :destroy_all)
|
|
|
|
.with(session_id: id).with(no_args)
|
|
|
|
|
|
|
|
described_class.deactivate(id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.purge_old' do
|
|
|
|
it 'calls order.offset.destroy_all' do
|
|
|
|
expect(described_class).to receive_message_chain(:order, :offset, :destroy_all)
|
|
|
|
.with('created_at desc').with(Rails.configuration.x.max_session_activations).with(no_args)
|
|
|
|
|
|
|
|
described_class.purge_old
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.exclusive' do
|
|
|
|
let(:id) { '1' }
|
|
|
|
|
|
|
|
it 'calls where.destroy_all' do
|
2023-02-08 10:39:57 +01:00
|
|
|
expect(described_class).to receive_message_chain(:where, :not, :destroy_all)
|
|
|
|
.with(session_id: id).with(no_args)
|
2017-11-13 01:54:48 +01:00
|
|
|
|
|
|
|
described_class.exclusive(id)
|
|
|
|
end
|
|
|
|
end
|
2017-06-23 18:50:53 +02:00
|
|
|
end
|