You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.2 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe SessionActivation, type: :model 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_kind_of 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) { double(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) { double(platform: double(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 'id is absent' do
  33. let(:id) { nil }
  34. it 'returns nil' do
  35. is_expected.to be nil
  36. end
  37. end
  38. context 'id is present' do
  39. let(:id) { '1' }
  40. let!(:session_activation) { Fabricate(:session_activation, session_id: id) }
  41. context 'id exists as session_id' do
  42. it 'returns true' do
  43. is_expected.to be true
  44. end
  45. end
  46. context '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. is_expected.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. expect(described_class).to receive(:create!).with(options)
  60. expect(described_class).to receive(:purge_old)
  61. described_class.activate(options)
  62. end
  63. it 'returns an instance of SessionActivation' do
  64. expect(described_class.activate(options)).to be_kind_of SessionActivation
  65. end
  66. end
  67. describe '.deactivate' do
  68. context 'id is absent' do
  69. let(:id) { nil }
  70. it 'returns nil' do
  71. expect(described_class.deactivate(id)).to be nil
  72. end
  73. end
  74. context 'id exists' do
  75. let(:id) { '1' }
  76. it 'calls where.destroy_all' do
  77. expect(described_class).to receive_message_chain(:where, :destroy_all)
  78. .with(session_id: id).with(no_args)
  79. described_class.deactivate(id)
  80. end
  81. end
  82. end
  83. describe '.purge_old' do
  84. it 'calls order.offset.destroy_all' do
  85. expect(described_class).to receive_message_chain(:order, :offset, :destroy_all)
  86. .with('created_at desc').with(Rails.configuration.x.max_session_activations).with(no_args)
  87. described_class.purge_old
  88. end
  89. end
  90. describe '.exclusive' do
  91. let(:id) { '1' }
  92. it 'calls where.destroy_all' do
  93. expect(described_class).to receive_message_chain(:where, :destroy_all)
  94. .with('session_id != ?', id).with(no_args)
  95. described_class.exclusive(id)
  96. end
  97. end
  98. end