debian-mirror-gitlab/spec/lib/gitlab/auth/ldap/access_spec.rb

216 lines
5.7 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Auth::Ldap::Access do
2018-05-09 12:01:36 +05:30
include LdapHelpers
2015-04-26 12:48:37 +05:30
let(:user) { create(:omniauth_user) }
2018-11-20 20:47:30 +05:30
subject(:access) { described_class.new(user) }
2017-09-10 17:25:29 +05:30
describe '.allowed?' do
2018-11-20 20:47:30 +05:30
before do
2018-11-08 19:23:39 +05:30
allow(access).to receive(:update_user)
2018-11-20 20:47:30 +05:30
allow(access).to receive(:allowed?).and_return(true)
allow(described_class).to receive(:open).and_yield(access)
end
2017-09-10 17:25:29 +05:30
2018-11-20 20:47:30 +05:30
it "updates the user's `last_credential_check_at`" do
2017-09-10 17:25:29 +05:30
expect { described_class.allowed?(user) }
.to change { user.last_credential_check_at }
end
2018-11-20 20:47:30 +05:30
it "does not update user's `last_credential_check_at` when in a read-only GitLab instance" do
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
2018-11-08 19:23:39 +05:30
2018-11-20 20:47:30 +05:30
expect { described_class.allowed?(user) }
.not_to change { user.last_credential_check_at }
2018-11-08 19:23:39 +05:30
end
end
2016-08-24 12:49:21 +05:30
describe '#allowed?' do
2015-04-26 12:48:37 +05:30
context 'when the user cannot be found' do
2015-09-11 14:41:01 +05:30
before do
2018-11-20 20:47:30 +05:30
stub_ldap_person_find_by_dn(nil)
stub_ldap_person_find_by_email(user.email, nil)
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
2018-11-20 20:47:30 +05:30
it 'returns false' do
expect(access.allowed?).to be_falsey
end
2017-08-17 22:00:37 +05:30
it 'blocks user in GitLab' do
2018-11-20 20:47:30 +05:30
access.allowed?
expect(user).to be_blocked
expect(user).to be_ldap_blocked
end
it 'logs the reason' do
expect(Gitlab::AppLogger).to receive(:info).with(
"LDAP account \"123456\" does not exist anymore, " \
2018-12-05 23:21:45 +05:30
"blocking GitLab user \"#{user.name}\" (#{user.email})"
2018-11-20 20:47:30 +05:30
)
2017-08-17 22:00:37 +05:30
2015-12-23 02:04:40 +05:30
access.allowed?
end
2015-04-26 12:48:37 +05:30
end
context 'when the user is found' do
2015-09-11 14:41:01 +05:30
before do
2018-11-20 20:47:30 +05:30
stub_ldap_person_find_by_dn(Net::LDAP::Entry.new)
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
context 'and the user is disabled via active directory' do
before do
2020-04-08 14:13:33 +05:30
allow(Gitlab::Auth::Ldap::Person).to receive(:disabled_via_active_directory?).and_return(true)
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
2018-11-20 20:47:30 +05:30
it 'returns false' do
expect(access.allowed?).to be_falsey
end
2015-04-26 12:48:37 +05:30
2016-06-02 11:05:42 +05:30
it 'blocks user in GitLab' do
2018-11-20 20:47:30 +05:30
access.allowed?
expect(user).to be_blocked
expect(user).to be_ldap_blocked
end
it 'logs the reason' do
expect(Gitlab::AppLogger).to receive(:info).with(
"LDAP account \"123456\" is disabled in Active Directory, " \
2018-12-05 23:21:45 +05:30
"blocking GitLab user \"#{user.name}\" (#{user.email})"
2018-11-20 20:47:30 +05:30
)
2017-08-17 22:00:37 +05:30
2015-04-26 12:48:37 +05:30
access.allowed?
end
end
2018-11-08 19:23:39 +05:30
context 'and has no disabled flag in active directory' do
2015-04-26 12:48:37 +05:30
before do
2020-04-08 14:13:33 +05:30
allow(Gitlab::Auth::Ldap::Person).to receive(:disabled_via_active_directory?).and_return(false)
2015-04-26 12:48:37 +05:30
end
it { is_expected.to be_truthy }
2015-09-11 14:41:01 +05:30
context 'when auto-created users are blocked' do
before do
user.block
2015-09-11 14:41:01 +05:30
end
it 'does not unblock user in GitLab' do
2017-08-17 22:00:37 +05:30
expect(access).not_to receive(:unblock_user)
2015-09-11 14:41:01 +05:30
access.allowed?
2017-08-17 22:00:37 +05:30
2015-09-11 14:41:01 +05:30
expect(user).to be_blocked
expect(user).not_to be_ldap_blocked # this block is handled by omniauth not by our internal logic
2015-09-11 14:41:01 +05:30
end
end
context 'when auto-created users are not blocked' do
2015-09-11 14:41:01 +05:30
before do
user.ldap_block
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
it 'unblocks user in GitLab' do
2018-11-20 20:47:30 +05:30
access.allowed?
expect(user).not_to be_blocked
expect(user).not_to be_ldap_blocked
end
it 'logs the reason' do
expect(Gitlab::AppLogger).to receive(:info).with(
"LDAP account \"123456\" is not disabled anymore, " \
2018-12-05 23:21:45 +05:30
"unblocking GitLab user \"#{user.name}\" (#{user.email})"
2018-11-20 20:47:30 +05:30
)
2017-08-17 22:00:37 +05:30
2015-09-11 14:41:01 +05:30
access.allowed?
end
2015-04-26 12:48:37 +05:30
end
end
context 'without ActiveDirectory enabled' do
before do
2020-04-08 14:13:33 +05:30
allow(Gitlab::Auth::Ldap::Config).to receive(:enabled?).and_return(true)
allow_next_instance_of(Gitlab::Auth::Ldap::Config) do |instance|
2020-01-01 13:55:28 +05:30
allow(instance).to receive(:active_directory).and_return(false)
end
2015-04-26 12:48:37 +05:30
end
2018-11-20 20:47:30 +05:30
it 'returns true' do
expect(access.allowed?).to be_truthy
end
2016-06-02 11:05:42 +05:30
context 'when user cannot be found' do
before do
2018-11-20 20:47:30 +05:30
stub_ldap_person_find_by_dn(nil)
stub_ldap_person_find_by_email(user.email, nil)
2016-06-02 11:05:42 +05:30
end
2018-11-20 20:47:30 +05:30
it 'returns false' do
expect(access.allowed?).to be_falsey
end
2016-06-02 11:05:42 +05:30
it 'blocks user in GitLab' do
2018-11-20 20:47:30 +05:30
access.allowed?
expect(user).to be_blocked
expect(user).to be_ldap_blocked
end
it 'logs the reason' do
expect(Gitlab::AppLogger).to receive(:info).with(
"LDAP account \"123456\" does not exist anymore, " \
2018-12-05 23:21:45 +05:30
"blocking GitLab user \"#{user.name}\" (#{user.email})"
2018-11-20 20:47:30 +05:30
)
2017-08-17 22:00:37 +05:30
2016-06-02 11:05:42 +05:30
access.allowed?
end
end
context 'when user was previously ldap_blocked' do
before do
user.ldap_block
end
it 'unblocks the user if it exists' do
2018-11-20 20:47:30 +05:30
access.allowed?
expect(user).not_to be_blocked
expect(user).not_to be_ldap_blocked
end
it 'logs the reason' do
expect(Gitlab::AppLogger).to receive(:info).with(
"LDAP account \"123456\" is available again, " \
2018-12-05 23:21:45 +05:30
"unblocking GitLab user \"#{user.name}\" (#{user.email})"
2018-11-20 20:47:30 +05:30
)
2017-08-17 22:00:37 +05:30
2016-06-02 11:05:42 +05:30
access.allowed?
end
end
2015-04-26 12:48:37 +05:30
end
end
2018-05-09 12:01:36 +05:30
context 'when the connection fails' do
before do
raise_ldap_connection_error
end
it 'does not block the user' do
access.allowed?
expect(user.ldap_blocked?).to be_falsey
end
it 'denies access' do
expect(access.allowed?).to be_falsey
end
end
2015-04-26 12:48:37 +05:30
end
end