diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-04 21:08:50 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-04 21:08:50 +0300 |
commit | ca05512007cea51e05d3431b2c8bd7228c754370 (patch) | |
tree | 5202d429acd68c071445aff9e352379173ec9c0b /spec/lib/gitlab/auth | |
parent | 6b833f1e0340e00fdee074da9c42c0d4e07a46d2 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/auth')
-rw-r--r-- | spec/lib/gitlab/auth/current_user_mode_spec.rb | 172 |
1 files changed, 34 insertions, 138 deletions
diff --git a/spec/lib/gitlab/auth/current_user_mode_spec.rb b/spec/lib/gitlab/auth/current_user_mode_spec.rb index 7c2fdac6c25..3b3db0f7315 100644 --- a/spec/lib/gitlab/auth/current_user_mode_spec.rb +++ b/spec/lib/gitlab/auth/current_user_mode_spec.rb @@ -2,10 +2,10 @@ require 'spec_helper' -describe Gitlab::Auth::CurrentUserMode, :do_not_mock_admin_mode, :request_store do +describe Gitlab::Auth::CurrentUserMode, :do_not_mock_admin_mode do include_context 'custom session' - let(:user) { build_stubbed(:user) } + let(:user) { build(:user) } subject { described_class.new(user) } @@ -13,66 +13,54 @@ describe Gitlab::Auth::CurrentUserMode, :do_not_mock_admin_mode, :request_store allow(ActiveSession).to receive(:list_sessions).with(user).and_return([session]) end - shared_examples 'admin mode cannot be enabled' do - it 'is false by default' do - expect(subject.admin_mode?).to be(false) - end - - it 'cannot be enabled with a valid password' do - subject.enable_admin_mode!(password: user.password) - - expect(subject.admin_mode?).to be(false) - end - - it 'cannot be enabled with an invalid password' do - subject.enable_admin_mode!(password: nil) - - expect(subject.admin_mode?).to be(false) - end + 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 - it 'cannot be enabled with empty params' do - subject.enable_admin_mode! + it 'cannot be enabled with a valid password' do + subject.enable_admin_mode!(password: user.password) - expect(subject.admin_mode?).to be(false) - end + expect(subject.admin_mode?).to be(false) + end - it 'disable has no effect' do - subject.enable_admin_mode! - subject.disable_admin_mode! + it 'cannot be enabled with an invalid password' do + subject.enable_admin_mode!(password: nil) - expect(subject.admin_mode?).to be(false) - end + expect(subject.admin_mode?).to be(false) + end - 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) + it 'cannot be enabled with empty params' do + subject.enable_admin_mode! 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) + it 'disable has no effect' do + subject.enable_admin_mode! + subject.disable_admin_mode! expect(subject.admin_mode?).to be(false) end - end - end - describe '#admin_mode?' do - context 'when the user is a regular user' do - it_behaves_like 'admin mode cannot be enabled' + 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) - context 'bypassing session' do - it_behaves_like 'admin mode cannot be enabled' do - around do |example| - described_class.bypass_session!(user.id) { example.run } - end + 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) end end end context 'when the user is an admin' do - let(:user) { build_stubbed(:user, :admin) } + let(:user) { build(:user, :admin) } context 'when admin mode not requested' do it 'is false by default' do @@ -160,36 +148,11 @@ describe Gitlab::Auth::CurrentUserMode, :do_not_mock_admin_mode, :request_store end end end - - context 'bypassing session' do - it 'is active by default' do - described_class.bypass_session!(user.id) do - expect(subject.admin_mode?).to be(true) - end - end - - it 'enable has no effect' do - described_class.bypass_session!(user.id) do - subject.request_admin_mode! - subject.enable_admin_mode!(password: user.password) - - expect(subject.admin_mode?).to be(true) - end - end - - it 'disable has no effect' do - described_class.bypass_session!(user.id) do - subject.disable_admin_mode! - - expect(subject.admin_mode?).to be(true) - end - end - end end end describe '#enable_admin_mode!' do - let(:user) { build_stubbed(:user, :admin) } + let(:user) { build(:user, :admin) } it 'creates a timestamp in the session' do subject.request_admin_mode! @@ -200,7 +163,7 @@ describe Gitlab::Auth::CurrentUserMode, :do_not_mock_admin_mode, :request_store end describe '#enable_sessionless_admin_mode!' do - let(:user) { build_stubbed(:user, :admin) } + let(:user) { build(:user, :admin) } it 'enabled admin mode without password' do subject.enable_sessionless_admin_mode! @@ -210,7 +173,7 @@ describe Gitlab::Auth::CurrentUserMode, :do_not_mock_admin_mode, :request_store end describe '#disable_admin_mode!' do - let(:user) { build_stubbed(:user, :admin) } + let(:user) { build(:user, :admin) } it 'sets the session timestamp to nil' do subject.request_admin_mode! @@ -220,73 +183,6 @@ describe Gitlab::Auth::CurrentUserMode, :do_not_mock_admin_mode, :request_store end end - describe '.bypass_session!' do - context 'with a regular user' do - it 'admin mode is false' do - described_class.bypass_session!(user.id) do - expect(subject.admin_mode?).to be(false) - expect(described_class.bypass_session_admin_id).to be(user.id) - end - - expect(described_class.bypass_session_admin_id).to be_nil - end - end - - context 'with an admin user' do - let(:user) { build_stubbed(:user, :admin) } - - it 'admin mode is true' do - described_class.bypass_session!(user.id) do - expect(subject.admin_mode?).to be(true) - expect(described_class.bypass_session_admin_id).to be(user.id) - end - - expect(described_class.bypass_session_admin_id).to be_nil - end - end - end - - describe '.with_current_request_admin_mode' do - context 'with a regular user' do - it 'user is not available inside nor outside the yielded block' do - described_class.with_current_admin(user) do - expect(described_class.current_admin).to be_nil - end - - expect(described_class.bypass_session_admin_id).to be_nil - end - end - - context 'with an admin user' do - let(:user) { build_stubbed(:user, :admin) } - - context 'admin mode is disabled' do - it 'user is not available inside nor outside the yielded block' do - described_class.with_current_admin(user) do - expect(described_class.current_admin).to be_nil - end - - expect(described_class.bypass_session_admin_id).to be_nil - end - end - - context 'admin mode is enabled' do - before do - subject.request_admin_mode! - subject.enable_admin_mode!(password: user.password) - end - - it 'user is available only inside the yielded block' do - described_class.with_current_admin(user) do - expect(described_class.current_admin).to be(user) - end - - expect(described_class.current_admin).to be_nil - end - end - end - end - def expected_session_entry(value_matcher) { Gitlab::Auth::CurrentUserMode::SESSION_STORE_KEY => a_hash_including( |