2019-07-25 01:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-02 06:52:13 -04:00
|
|
|
module LdapHelpers
|
|
|
|
def ldap_adapter(provider = 'ldapmain', ldap = double(:ldap))
|
2020-03-12 11:09:39 -04:00
|
|
|
::Gitlab::Auth::Ldap::Adapter.new(provider, ldap)
|
2016-09-02 06:52:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_dn(uid)
|
|
|
|
"uid=#{uid},ou=users,dc=example,dc=com"
|
|
|
|
end
|
|
|
|
|
2020-03-12 11:09:39 -04:00
|
|
|
# Accepts a hash of Gitlab::Auth::Ldap::Config keys and values.
|
2016-09-02 06:52:13 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# stub_ldap_config(
|
|
|
|
# group_base: 'ou=groups,dc=example,dc=com',
|
|
|
|
# admin_group: 'my-admin-group'
|
|
|
|
# )
|
|
|
|
def stub_ldap_config(messages)
|
2020-03-12 11:09:39 -04:00
|
|
|
allow_any_instance_of(::Gitlab::Auth::Ldap::Config).to receive_messages(messages)
|
2016-09-02 06:52:13 -04:00
|
|
|
end
|
|
|
|
|
2018-04-18 10:03:27 -04:00
|
|
|
def stub_ldap_setting(messages)
|
|
|
|
allow(Gitlab.config.ldap).to receive_messages(to_settings(messages))
|
|
|
|
end
|
|
|
|
|
2016-09-02 06:52:13 -04:00
|
|
|
# Stub an LDAP person search and provide the return entry. Specify `nil` for
|
|
|
|
# `entry` to simulate when an LDAP person is not found
|
|
|
|
#
|
|
|
|
# Example:
|
2020-03-12 11:09:39 -04:00
|
|
|
# adapter = ::Gitlab::Auth::Ldap::Adapter.new('ldapmain', double(:ldap))
|
2016-09-02 06:52:13 -04:00
|
|
|
# ldap_user_entry = ldap_user_entry('john_doe')
|
|
|
|
#
|
|
|
|
# stub_ldap_person_find_by_uid('john_doe', ldap_user_entry, adapter)
|
|
|
|
def stub_ldap_person_find_by_uid(uid, entry, provider = 'ldapmain')
|
2020-03-12 11:09:39 -04:00
|
|
|
return_value = ::Gitlab::Auth::Ldap::Person.new(entry, provider) if entry.present?
|
2016-09-02 06:52:13 -04:00
|
|
|
|
2020-03-12 11:09:39 -04:00
|
|
|
allow(::Gitlab::Auth::Ldap::Person)
|
2016-09-02 06:52:13 -04:00
|
|
|
.to receive(:find_by_uid).with(uid, any_args).and_return(return_value)
|
|
|
|
end
|
|
|
|
|
2018-08-23 09:46:45 -04:00
|
|
|
def stub_ldap_person_find_by_dn(entry, provider = 'ldapmain')
|
2020-03-12 11:09:39 -04:00
|
|
|
person = ::Gitlab::Auth::Ldap::Person.new(entry, provider) if entry.present?
|
2018-08-23 09:46:45 -04:00
|
|
|
|
2020-03-12 11:09:39 -04:00
|
|
|
allow(::Gitlab::Auth::Ldap::Person)
|
2018-08-23 09:46:45 -04:00
|
|
|
.to receive(:find_by_dn)
|
|
|
|
.and_return(person)
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_ldap_person_find_by_email(email, entry, provider = 'ldapmain')
|
2020-03-12 11:09:39 -04:00
|
|
|
person = ::Gitlab::Auth::Ldap::Person.new(entry, provider) if entry.present?
|
2018-08-23 09:46:45 -04:00
|
|
|
|
2020-03-12 11:09:39 -04:00
|
|
|
allow(::Gitlab::Auth::Ldap::Person)
|
2018-08-23 09:46:45 -04:00
|
|
|
.to receive(:find_by_email)
|
|
|
|
.with(email, anything)
|
|
|
|
.and_return(person)
|
|
|
|
end
|
|
|
|
|
2016-09-02 06:52:13 -04:00
|
|
|
# Create a simple LDAP user entry.
|
|
|
|
def ldap_user_entry(uid)
|
|
|
|
entry = Net::LDAP::Entry.new
|
|
|
|
entry['dn'] = user_dn(uid)
|
|
|
|
entry['uid'] = uid
|
|
|
|
|
|
|
|
entry
|
|
|
|
end
|
2018-04-04 05:07:28 -04:00
|
|
|
|
|
|
|
def raise_ldap_connection_error
|
2020-03-12 11:09:39 -04:00
|
|
|
allow_any_instance_of(Gitlab::Auth::Ldap::Adapter)
|
|
|
|
.to receive(:ldap_search).and_raise(Gitlab::Auth::Ldap::LdapConnectionError)
|
2018-04-04 05:07:28 -04:00
|
|
|
end
|
2016-09-02 06:52:13 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
LdapHelpers.include_if_ee('EE::LdapHelpers')
|