session_activation_spec.rb 4.1 KB

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