summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/auth/otp/session_enforcer_spec.rb
blob: 928aade4008114e8611765d3a62d894fb1896dc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Auth::Otp::SessionEnforcer, :clean_gitlab_redis_shared_state do
  let_it_be(:key) { create(:key)}

  describe '#update_session' do
    it 'registers a session in Redis' do
      redis = double(:redis)
      expect(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis)

      expect(redis).to(
        receive(:setex)
          .with("#{described_class::OTP_SESSIONS_NAMESPACE}:#{key.id}",
                described_class::DEFAULT_EXPIRATION,
                true)
          .once)

      described_class.new(key).update_session
    end
  end

  describe '#access_restricted?' do
    subject { described_class.new(key).access_restricted? }

    context 'with existing session' do
      before do
        Gitlab::Redis::SharedState.with do |redis|
          redis.set("#{described_class::OTP_SESSIONS_NAMESPACE}:#{key.id}", true )
        end
      end

      it { is_expected.to be_falsey }
    end

    context 'without an existing session' do
      it { is_expected.to be_truthy }
    end
  end
end