2018-01-15 00:10:48 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Auth::BlockedUserTracker do
|
|
|
|
set(:user) { create(:user) }
|
|
|
|
|
|
|
|
describe '.log_if_user_blocked' do
|
|
|
|
it 'does not log if user failed to login due to undefined reason' do
|
|
|
|
expect_any_instance_of(SystemHooksService).not_to receive(:execute_hooks_for)
|
|
|
|
|
|
|
|
expect(described_class.log_if_user_blocked({})).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'gracefully handles malformed environment variables' do
|
|
|
|
env = { 'warden.options' => 'test' }
|
|
|
|
|
|
|
|
expect(described_class.log_if_user_blocked(env)).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'failed login due to blocked user' do
|
2018-05-13 01:02:26 -04:00
|
|
|
let(:base_env) { { 'warden.options' => { message: User::BLOCKED_MESSAGE } } }
|
|
|
|
let(:env) { base_env.merge(request_env) }
|
2018-01-15 00:10:48 -05:00
|
|
|
|
|
|
|
subject { described_class.log_if_user_blocked(env) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
expect_any_instance_of(SystemHooksService).to receive(:execute_hooks_for).with(user, :failed_login)
|
|
|
|
end
|
|
|
|
|
2018-05-13 01:02:26 -04:00
|
|
|
context 'via GitLab login' do
|
|
|
|
let(:request_env) { { described_class::ACTIVE_RECORD_REQUEST_PARAMS => { 'user' => { 'login' => user.username } } } }
|
2018-01-15 00:10:48 -05:00
|
|
|
|
2018-05-13 01:02:26 -04:00
|
|
|
it 'logs a blocked user' do
|
|
|
|
user.block!
|
|
|
|
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
2018-01-15 00:10:48 -05:00
|
|
|
|
2018-05-13 01:02:26 -04:00
|
|
|
it 'logs a blocked user by e-mail' do
|
|
|
|
user.block!
|
|
|
|
env[described_class::ACTIVE_RECORD_REQUEST_PARAMS]['user']['login'] = user.email
|
2018-01-15 00:10:48 -05:00
|
|
|
|
2018-05-13 01:02:26 -04:00
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
2018-01-15 00:10:48 -05:00
|
|
|
end
|
|
|
|
|
2018-05-13 01:02:26 -04:00
|
|
|
context 'via LDAP login' do
|
|
|
|
let(:request_env) { { described_class::ACTIVE_RECORD_REQUEST_PARAMS => { 'username' => user.username } } }
|
|
|
|
|
|
|
|
it 'logs a blocked user' do
|
|
|
|
user.block!
|
|
|
|
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs a LDAP blocked user' do
|
|
|
|
user.ldap_block!
|
2018-01-15 00:10:48 -05:00
|
|
|
|
2018-05-13 01:02:26 -04:00
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
2018-01-15 00:10:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|