Welcome to mirror list, hosted at ThFree Co, Russian Federation.

admin_constrainer_spec.rb « constraints « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6e8909ca129bca7ad0b73da06d60a0ef1c821e62 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true
#
require 'spec_helper'

RSpec.describe Constraints::AdminConstrainer do
  let(:user) { create(:user) }

  let(:session) { {} }
  let(:env) { { 'warden' => double(:warden, authenticate?: true, user: user) } }
  let(:request) { double(:request, session: session, env: env) }

  around do |example|
    Gitlab::Session.with_session(session) do
      example.run
    end
  end

  describe '#matches' do
    context 'application setting :admin_mode is enabled' do
      context 'when user is a regular user' do
        it 'forbids access' do
          expect(subject.matches?(request)).to be(false)
        end
      end

      context 'when user is an admin' do
        let(:user) { create(:admin) }

        context 'admin mode is disabled' do
          it 'forbids access' do
            expect(subject.matches?(request)).to be(false)
          end
        end

        context 'admin mode is enabled' do
          before do
            current_user_mode = Gitlab::Auth::CurrentUserMode.new(user)
            current_user_mode.request_admin_mode!
            current_user_mode.enable_admin_mode!(password: user.password)
          end

          it 'allows access' do
            expect(subject.matches?(request)).to be(true)
          end
        end
      end
    end

    context 'application setting :admin_mode is disabled' do
      before do
        stub_application_setting(admin_mode: false)
      end

      context 'when user is a regular user' do
        it 'forbids access' do
          expect(subject.matches?(request)).to be(false)
        end
      end

      context 'when user is an admin' do
        let(:user) { create(:admin) }

        it 'allows access' do
          expect(subject.matches?(request)).to be(true)
        end
      end
    end
  end
end