1be2ec2d04
An LDAP sign-in request results in a different request parameter than a standard GitLab sign-in. Since Warden doesn't pass us the user that was blocked, we first search for a `username` in the request parameters and then look for `user.login`. Closes #46307
63 lines
1.8 KiB
Ruby
63 lines
1.8 KiB
Ruby
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
|
|
let(:base_env) { { 'warden.options' => { message: User::BLOCKED_MESSAGE } } }
|
|
let(:env) { base_env.merge(request_env) }
|
|
|
|
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
|
|
|
|
context 'via GitLab login' do
|
|
let(:request_env) { { described_class::ACTIVE_RECORD_REQUEST_PARAMS => { 'user' => { 'login' => user.username } } } }
|
|
|
|
it 'logs a blocked user' do
|
|
user.block!
|
|
|
|
expect(subject).to be_truthy
|
|
end
|
|
|
|
it 'logs a blocked user by e-mail' do
|
|
user.block!
|
|
env[described_class::ACTIVE_RECORD_REQUEST_PARAMS]['user']['login'] = user.email
|
|
|
|
expect(subject).to be_truthy
|
|
end
|
|
end
|
|
|
|
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!
|
|
|
|
expect(subject).to be_truthy
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|