2020-04-15 11:09:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
#
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-12-17 13:10:14 -05:00
|
|
|
RSpec.describe Constraints::AdminConstrainer do
|
2020-04-15 11:09:17 -04:00
|
|
|
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
|
2021-03-17 20:08:58 -04:00
|
|
|
context 'application setting :admin_mode is enabled' do
|
2020-04-15 11:09:17 -04:00
|
|
|
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
|
|
|
|
|
2021-03-17 20:08:58 -04:00
|
|
|
context 'application setting :admin_mode is disabled' do
|
2020-04-15 11:09:17 -04:00
|
|
|
before do
|
2021-03-17 20:08:58 -04:00
|
|
|
stub_application_setting(admin_mode: false)
|
2020-04-15 11:09:17 -04:00
|
|
|
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
|