2019-09-26 08:06:00 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-02-04 13:08:50 -05:00
|
|
|
describe Gitlab::Auth::CurrentUserMode, :do_not_mock_admin_mode do
|
2019-09-26 08:06:00 -04:00
|
|
|
include_context 'custom session'
|
|
|
|
|
2020-02-04 13:08:50 -05:00
|
|
|
let(:user) { build(:user) }
|
2019-09-26 08:06:00 -04:00
|
|
|
|
|
|
|
subject { described_class.new(user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(ActiveSession).to receive(:list_sessions).with(user).and_return([session])
|
|
|
|
end
|
|
|
|
|
2020-02-04 13:08:50 -05:00
|
|
|
describe '#admin_mode?', :request_store do
|
|
|
|
context 'when the user is a regular user' do
|
|
|
|
it 'is false by default' do
|
|
|
|
expect(subject.admin_mode?).to be(false)
|
|
|
|
end
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2020-02-04 13:08:50 -05:00
|
|
|
it 'cannot be enabled with a valid password' do
|
|
|
|
subject.enable_admin_mode!(password: user.password)
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2020-02-04 13:08:50 -05:00
|
|
|
expect(subject.admin_mode?).to be(false)
|
|
|
|
end
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2020-02-04 13:08:50 -05:00
|
|
|
it 'cannot be enabled with an invalid password' do
|
|
|
|
subject.enable_admin_mode!(password: nil)
|
2020-02-04 07:09:00 -05:00
|
|
|
|
2020-02-04 13:08:50 -05:00
|
|
|
expect(subject.admin_mode?).to be(false)
|
|
|
|
end
|
2020-02-04 07:09:00 -05:00
|
|
|
|
2020-02-04 13:08:50 -05:00
|
|
|
it 'cannot be enabled with empty params' do
|
|
|
|
subject.enable_admin_mode!
|
2019-09-26 08:06:00 -04:00
|
|
|
|
|
|
|
expect(subject.admin_mode?).to be(false)
|
|
|
|
end
|
|
|
|
|
2020-02-04 13:08:50 -05:00
|
|
|
it 'disable has no effect' do
|
|
|
|
subject.enable_admin_mode!
|
|
|
|
subject.disable_admin_mode!
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2020-02-04 07:09:00 -05:00
|
|
|
expect(subject.admin_mode?).to be(false)
|
|
|
|
end
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2020-02-04 13:08:50 -05:00
|
|
|
context 'skipping password validation' do
|
|
|
|
it 'cannot be enabled with a valid password' do
|
|
|
|
subject.enable_admin_mode!(password: user.password, skip_password_validation: true)
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2020-02-04 13:08:50 -05:00
|
|
|
expect(subject.admin_mode?).to be(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cannot be enabled with an invalid password' do
|
|
|
|
subject.enable_admin_mode!(skip_password_validation: true)
|
|
|
|
|
|
|
|
expect(subject.admin_mode?).to be(false)
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user is an admin' do
|
2020-02-04 13:08:50 -05:00
|
|
|
let(:user) { build(:user, :admin) }
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
context 'when admin mode not requested' do
|
|
|
|
it 'is false by default' do
|
|
|
|
expect(subject.admin_mode?).to be(false)
|
|
|
|
end
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
it 'raises exception if we try to enable it' do
|
|
|
|
expect do
|
|
|
|
subject.enable_admin_mode!(password: user.password)
|
|
|
|
end.to raise_error(::Gitlab::Auth::CurrentUserMode::NotRequestedError)
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
expect(subject.admin_mode?).to be(false)
|
|
|
|
end
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
context 'when admin mode requested first' do
|
|
|
|
before do
|
|
|
|
subject.request_admin_mode!
|
|
|
|
end
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
it 'is false by default' do
|
|
|
|
expect(subject.admin_mode?).to be(false)
|
|
|
|
end
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
it 'cannot be enabled with an invalid password' do
|
|
|
|
subject.enable_admin_mode!(password: nil)
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
expect(subject.admin_mode?).to be(false)
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be enabled with a valid password' do
|
2019-12-11 07:08:10 -05:00
|
|
|
subject.enable_admin_mode!(password: user.password)
|
2019-09-26 08:06:00 -04:00
|
|
|
|
|
|
|
expect(subject.admin_mode?).to be(true)
|
|
|
|
end
|
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
it 'can be disabled' do
|
|
|
|
subject.enable_admin_mode!(password: user.password)
|
|
|
|
subject.disable_admin_mode!
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
expect(subject.admin_mode?).to be(false)
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
it 'will expire in the future' do
|
|
|
|
subject.enable_admin_mode!(password: user.password)
|
|
|
|
expect(subject.admin_mode?).to be(true), 'admin mode is not active in the present'
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
Timecop.freeze(Gitlab::Auth::CurrentUserMode::MAX_ADMIN_MODE_TIME.from_now) do
|
|
|
|
# in the future this will be a new request, simulate by clearing the RequestStore
|
|
|
|
Gitlab::SafeRequestStore.clear!
|
|
|
|
|
|
|
|
expect(subject.admin_mode?).to be(false), 'admin mode did not expire in the future'
|
|
|
|
end
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
context 'skipping password validation' do
|
|
|
|
it 'can be enabled with a valid password' do
|
|
|
|
subject.enable_admin_mode!(password: user.password, skip_password_validation: true)
|
|
|
|
|
|
|
|
expect(subject.admin_mode?).to be(true)
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
it 'can be enabled with an invalid password' do
|
|
|
|
subject.enable_admin_mode!(skip_password_validation: true)
|
|
|
|
|
|
|
|
expect(subject.admin_mode?).to be(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with two independent sessions' do
|
|
|
|
let(:another_session) { {} }
|
|
|
|
let(:another_subject) { described_class.new(user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(ActiveSession).to receive(:list_sessions).with(user).and_return([session, another_session])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be enabled in one and seen in the other' do
|
|
|
|
Gitlab::Session.with_session(another_session) do
|
|
|
|
another_subject.request_admin_mode!
|
|
|
|
another_subject.enable_admin_mode!(password: user.password)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(subject.admin_mode?).to be(true)
|
|
|
|
end
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#enable_admin_mode!' do
|
2020-02-04 13:08:50 -05:00
|
|
|
let(:user) { build(:user, :admin) }
|
2019-09-26 08:06:00 -04:00
|
|
|
|
|
|
|
it 'creates a timestamp in the session' do
|
2019-12-11 07:08:10 -05:00
|
|
|
subject.request_admin_mode!
|
2019-09-26 08:06:00 -04:00
|
|
|
subject.enable_admin_mode!(password: user.password)
|
|
|
|
|
|
|
|
expect(session).to include(expected_session_entry(be_within(1.second).of Time.now))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
describe '#enable_sessionless_admin_mode!' do
|
2020-02-04 13:08:50 -05:00
|
|
|
let(:user) { build(:user, :admin) }
|
2019-12-11 07:08:10 -05:00
|
|
|
|
|
|
|
it 'enabled admin mode without password' do
|
|
|
|
subject.enable_sessionless_admin_mode!
|
|
|
|
|
|
|
|
expect(subject.admin_mode?).to be(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-26 08:06:00 -04:00
|
|
|
describe '#disable_admin_mode!' do
|
2020-02-04 13:08:50 -05:00
|
|
|
let(:user) { build(:user, :admin) }
|
2019-09-26 08:06:00 -04:00
|
|
|
|
|
|
|
it 'sets the session timestamp to nil' do
|
2019-12-11 07:08:10 -05:00
|
|
|
subject.request_admin_mode!
|
2019-09-26 08:06:00 -04:00
|
|
|
subject.disable_admin_mode!
|
|
|
|
|
|
|
|
expect(session).to include(expected_session_entry(be_nil))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def expected_session_entry(value_matcher)
|
|
|
|
{
|
|
|
|
Gitlab::Auth::CurrentUserMode::SESSION_STORE_KEY => a_hash_including(
|
|
|
|
Gitlab::Auth::CurrentUserMode::ADMIN_MODE_START_TIME_KEY => value_matcher)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|