2014-05-14 12:14:06 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Gitlab::LDAP::Access do
|
2017-07-25 13:09:00 -04:00
|
|
|
let(:access) { described_class.new user }
|
2014-12-04 06:43:08 -05:00
|
|
|
let(:user) { create(:omniauth_user) }
|
2014-05-14 12:14:06 -04:00
|
|
|
|
2017-06-28 03:30:18 -04:00
|
|
|
describe '.allowed?' do
|
|
|
|
it 'updates the users `last_credential_check_at' do
|
|
|
|
expect(access).to receive(:allowed?) { true }
|
|
|
|
expect(described_class).to receive(:open).and_yield(access)
|
|
|
|
|
|
|
|
expect { described_class.allowed?(user) }
|
|
|
|
.to change { user.last_credential_check_at }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-11 18:12:31 -04:00
|
|
|
describe '#allowed?' do
|
2014-10-13 11:24:05 -04:00
|
|
|
subject { access.allowed? }
|
2014-05-14 12:14:06 -04:00
|
|
|
|
|
|
|
context 'when the user cannot be found' do
|
2015-05-21 17:49:06 -04:00
|
|
|
before do
|
|
|
|
allow(Gitlab::LDAP::Person).to receive(:find_by_dn).and_return(nil)
|
|
|
|
end
|
2014-05-14 12:14:06 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to be_falsey }
|
2015-12-30 17:56:26 -05:00
|
|
|
|
2016-08-11 11:30:18 -04:00
|
|
|
it 'blocks user in GitLab' do
|
2016-12-12 09:13:23 -05:00
|
|
|
expect(access).to receive(:block_user).with(user, 'does not exist anymore')
|
|
|
|
|
2015-12-08 10:47:42 -05:00
|
|
|
access.allowed?
|
|
|
|
end
|
2014-05-14 12:14:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user is found' do
|
2015-05-21 17:49:06 -04:00
|
|
|
before do
|
2015-12-30 17:56:26 -05:00
|
|
|
allow(Gitlab::LDAP::Person).to receive(:find_by_dn).and_return(:ldap_user)
|
2015-05-21 17:49:06 -04:00
|
|
|
end
|
2014-05-14 12:14:06 -04:00
|
|
|
|
2015-05-12 05:21:56 -04:00
|
|
|
context 'and the user is disabled via active directory' do
|
2015-05-21 17:49:06 -04:00
|
|
|
before do
|
2015-12-30 17:56:26 -05:00
|
|
|
allow(Gitlab::LDAP::Person).to receive(:disabled_via_active_directory?).and_return(true)
|
2015-05-21 17:49:06 -04:00
|
|
|
end
|
2014-05-14 12:14:06 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to be_falsey }
|
2015-03-12 14:53:21 -04:00
|
|
|
|
2016-04-05 15:33:37 -04:00
|
|
|
it 'blocks user in GitLab' do
|
2016-12-12 09:13:23 -05:00
|
|
|
expect(access).to receive(:block_user).with(user, 'is disabled in Active Directory')
|
|
|
|
|
2015-03-12 14:53:21 -04:00
|
|
|
access.allowed?
|
|
|
|
end
|
2014-05-14 12:14:06 -04:00
|
|
|
end
|
|
|
|
|
2014-09-01 10:35:18 -04:00
|
|
|
context 'and has no disabled flag in active diretory' do
|
2015-03-13 11:40:15 -04:00
|
|
|
before do
|
2015-12-30 17:56:26 -05:00
|
|
|
allow(Gitlab::LDAP::Person).to receive(:disabled_via_active_directory?).and_return(false)
|
2015-03-13 11:40:15 -04:00
|
|
|
end
|
2014-05-14 12:14:06 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to be_truthy }
|
2015-03-13 11:40:15 -04:00
|
|
|
|
2015-05-12 05:21:56 -04:00
|
|
|
context 'when auto-created users are blocked' do
|
|
|
|
before do
|
2016-01-14 00:31:27 -05:00
|
|
|
user.block
|
2015-05-12 05:21:56 -04:00
|
|
|
end
|
|
|
|
|
2015-12-30 17:56:26 -05:00
|
|
|
it 'does not unblock user in GitLab' do
|
2016-12-12 09:13:23 -05:00
|
|
|
expect(access).not_to receive(:unblock_user)
|
|
|
|
|
2015-05-12 05:21:56 -04:00
|
|
|
access.allowed?
|
2016-12-12 09:13:23 -05:00
|
|
|
|
2015-06-13 18:24:51 -04:00
|
|
|
expect(user).to be_blocked
|
2015-12-30 17:56:26 -05:00
|
|
|
expect(user).not_to be_ldap_blocked # this block is handled by omniauth not by our internal logic
|
2015-05-12 05:21:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-30 17:56:26 -05:00
|
|
|
context 'when auto-created users are not blocked' do
|
2015-05-12 05:21:56 -04:00
|
|
|
before do
|
2016-01-14 00:31:27 -05:00
|
|
|
user.ldap_block
|
2015-05-12 05:21:56 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'unblocks user in GitLab' do
|
2016-12-12 09:13:23 -05:00
|
|
|
expect(access).to receive(:unblock_user).with(user, 'is not disabled anymore')
|
|
|
|
|
2015-05-12 05:21:56 -04:00
|
|
|
access.allowed?
|
|
|
|
end
|
2015-03-13 11:40:15 -04:00
|
|
|
end
|
2014-05-14 12:14:06 -04:00
|
|
|
end
|
2014-09-30 06:07:31 -04:00
|
|
|
|
2014-10-13 11:24:05 -04:00
|
|
|
context 'without ActiveDirectory enabled' do
|
|
|
|
before do
|
2015-05-21 17:49:06 -04:00
|
|
|
allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(true)
|
2015-12-30 17:56:26 -05:00
|
|
|
allow_any_instance_of(Gitlab::LDAP::Config).to receive(:active_directory).and_return(false)
|
2014-10-13 11:24:05 -04:00
|
|
|
end
|
2014-09-30 06:07:31 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to be_truthy }
|
2016-04-05 15:33:37 -04:00
|
|
|
|
|
|
|
context 'when user cannot be found' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::LDAP::Person).to receive(:find_by_dn).and_return(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
|
|
|
|
it 'blocks user in GitLab' do
|
2016-12-12 09:13:23 -05:00
|
|
|
expect(access).to receive(:block_user).with(user, 'does not exist anymore')
|
|
|
|
|
2016-04-05 15:33:37 -04:00
|
|
|
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
|
2016-12-20 13:54:37 -05:00
|
|
|
expect(access).to receive(:unblock_user).with(user, 'is available again')
|
2016-12-12 09:13:23 -05:00
|
|
|
|
2016-04-05 15:33:37 -04:00
|
|
|
access.allowed?
|
|
|
|
end
|
|
|
|
end
|
2014-09-30 06:07:31 -04:00
|
|
|
end
|
2014-05-14 12:14:06 -04:00
|
|
|
end
|
|
|
|
end
|
2016-12-12 09:13:23 -05:00
|
|
|
|
|
|
|
describe '#block_user' do
|
|
|
|
before do
|
|
|
|
user.activate
|
|
|
|
allow(Gitlab::AppLogger).to receive(:info)
|
|
|
|
|
|
|
|
access.block_user user, 'reason'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'blocks the user' do
|
|
|
|
expect(user).to be_blocked
|
|
|
|
expect(user).to be_ldap_blocked
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs the reason' do
|
|
|
|
expect(Gitlab::AppLogger).to have_received(:info).with(
|
2017-01-10 08:31:21 -05:00
|
|
|
"LDAP account \"123456\" reason, " \
|
2016-12-12 09:13:23 -05:00
|
|
|
"blocking Gitlab user \"#{user.name}\" (#{user.email})"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#unblock_user' do
|
|
|
|
before do
|
|
|
|
user.ldap_block
|
|
|
|
allow(Gitlab::AppLogger).to receive(:info)
|
|
|
|
|
|
|
|
access.unblock_user user, 'reason'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'activates the user' do
|
|
|
|
expect(user).not_to be_blocked
|
|
|
|
expect(user).not_to be_ldap_blocked
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs the reason' do
|
|
|
|
Gitlab::AppLogger.info(
|
2017-01-10 08:31:21 -05:00
|
|
|
"LDAP account \"123456\" reason, " \
|
2016-12-12 09:13:23 -05:00
|
|
|
"unblocking Gitlab user \"#{user.name}\" (#{user.email})"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2015-03-12 14:53:21 -04:00
|
|
|
end
|