session_activation_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe SessionActivation do
  4. include_examples 'BrowserDetection'
  5. describe '.active?' do
  6. subject { described_class.active?(id) }
  7. context 'when id is absent' do
  8. let(:id) { nil }
  9. it 'returns nil' do
  10. expect(subject).to be_nil
  11. end
  12. end
  13. context 'when id is present' do
  14. let(:id) { '1' }
  15. let!(:session_activation) { Fabricate(:session_activation, session_id: id) }
  16. context 'when id exists as session_id' do
  17. it 'returns true' do
  18. expect(subject).to be true
  19. end
  20. end
  21. context 'when id does not exist as session_id' do
  22. before do
  23. session_activation.update!(session_id: '2')
  24. end
  25. it 'returns false' do
  26. expect(subject).to be false
  27. end
  28. end
  29. end
  30. end
  31. describe '.activate' do
  32. let(:options) { { user: Fabricate(:user), session_id: '1' } }
  33. it 'calls create! and purge_old' do
  34. allow(described_class).to receive(:create!).with(**options)
  35. allow(described_class).to receive(:purge_old)
  36. described_class.activate(**options)
  37. expect(described_class).to have_received(:create!).with(**options)
  38. expect(described_class).to have_received(:purge_old)
  39. end
  40. it 'returns an instance of SessionActivation' do
  41. expect(described_class.activate(**options)).to be_a described_class
  42. end
  43. end
  44. describe '.deactivate' do
  45. context 'when id is absent' do
  46. let(:id) { nil }
  47. it 'returns nil' do
  48. expect(described_class.deactivate(id)).to be_nil
  49. end
  50. end
  51. context 'when id exists' do
  52. let!(:session_activation) { Fabricate(:session_activation) }
  53. it 'destroys the record' do
  54. described_class.deactivate(session_activation.session_id)
  55. expect { session_activation.reload }.to raise_error(ActiveRecord::RecordNotFound)
  56. end
  57. end
  58. end
  59. describe '.purge_old' do
  60. around do |example|
  61. before = Rails.configuration.x.max_session_activations
  62. Rails.configuration.x.max_session_activations = 1
  63. example.run
  64. Rails.configuration.x.max_session_activations = before
  65. end
  66. let!(:oldest_session_activation) { Fabricate(:session_activation, created_at: 10.days.ago) }
  67. let!(:newest_session_activation) { Fabricate(:session_activation, created_at: 5.days.ago) }
  68. it 'preserves the newest X records based on config' do
  69. described_class.purge_old
  70. expect { oldest_session_activation.reload }.to raise_error(ActiveRecord::RecordNotFound)
  71. expect { newest_session_activation.reload }.to_not raise_error
  72. end
  73. end
  74. describe '.exclusive' do
  75. let!(:unwanted_session_activation) { Fabricate(:session_activation) }
  76. let!(:wanted_session_activation) { Fabricate(:session_activation) }
  77. it 'preserves supplied record and destroys all others' do
  78. described_class.exclusive(wanted_session_activation.session_id)
  79. expect { unwanted_session_activation.reload }.to raise_error(ActiveRecord::RecordNotFound)
  80. expect { wanted_session_activation.reload }.to_not raise_error
  81. end
  82. end
  83. end