2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2017-04-19 10:09:54 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
# rails autoloading issue
|
|
|
|
require 'ldap'
|
|
|
|
require 'ldap/group'
|
|
|
|
|
|
|
|
RSpec.describe Ldap::Group do
|
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
# required as 'let' to perform test based
|
|
|
|
# expectations and reuse it in 'let' instance
|
|
|
|
# as additional parameter
|
2021-07-16 13:29:38 +00:00
|
|
|
let(:mocked_ldap) { double }
|
2019-04-15 01:41:17 +00:00
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '.uid_attribute' do
|
2017-04-19 10:09:54 +00:00
|
|
|
|
|
|
|
it 'responds to .uid_attribute' do
|
|
|
|
expect(described_class).to respond_to(:uid_attribute)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns uid attribute' do
|
|
|
|
expect(described_class.uid_attribute).to be_a(String)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-23 11:35:27 +00:00
|
|
|
describe 'initialization config parameters' do
|
2017-04-19 10:09:54 +00:00
|
|
|
|
|
|
|
it 'reuses given Ldap instance if given' do
|
|
|
|
config = {}
|
|
|
|
expect(Ldap).not_to receive(:new).with(config)
|
2019-04-15 01:41:17 +00:00
|
|
|
described_class.new(config, ldap: mocked_ldap)
|
2017-04-19 10:09:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'takes optional filter' do
|
|
|
|
|
|
|
|
filter = '(objectClass=custom)'
|
|
|
|
config = {
|
|
|
|
filter: filter
|
|
|
|
}
|
|
|
|
|
|
|
|
instance = described_class.new(config, ldap: mocked_ldap)
|
|
|
|
|
|
|
|
expect(instance.filter).to eq(filter)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'takes optional uid_attribute' do
|
|
|
|
|
|
|
|
uid_attribute = 'dn'
|
|
|
|
config = {
|
|
|
|
uid_attribute: uid_attribute
|
|
|
|
}
|
|
|
|
|
|
|
|
instance = described_class.new(config, ldap: mocked_ldap)
|
|
|
|
|
|
|
|
expect(instance.uid_attribute).to eq(uid_attribute)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates own Ldap instance if none given' do
|
|
|
|
expect(Ldap).to receive(:new)
|
2019-04-15 01:41:17 +00:00
|
|
|
|
|
|
|
described_class.new
|
2017-04-19 10:09:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-23 11:35:27 +00:00
|
|
|
describe 'instance methods' do
|
2017-04-19 10:09:54 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
let(:initialization_config) do
|
2017-04-19 10:09:54 +00:00
|
|
|
{
|
|
|
|
uid_attribute: 'dn',
|
|
|
|
filter: '(objectClass=group)',
|
|
|
|
}
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-04-19 10:09:54 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
let(:instance) do
|
2017-04-19 10:09:54 +00:00
|
|
|
described_class.new(initialization_config, ldap: mocked_ldap)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-04-19 10:09:54 +00:00
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#list' do
|
2017-04-19 10:09:54 +00:00
|
|
|
|
|
|
|
it 'responds to #list' do
|
|
|
|
expect(instance).to respond_to(:list)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a Hash of groups' do
|
|
|
|
ldap_entry = build(:ldap_entry)
|
2020-10-22 13:57:01 +00:00
|
|
|
allow(mocked_ldap).to receive(:search).and_return(ldap_entry)
|
2017-04-19 10:09:54 +00:00
|
|
|
expect(instance.list).to be_a(Hash)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#filter' do
|
2017-04-19 10:09:54 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
let(:initialization_config) do
|
2017-04-19 10:09:54 +00:00
|
|
|
{
|
|
|
|
uid_attribute: 'dn',
|
|
|
|
}
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-04-19 10:09:54 +00:00
|
|
|
|
|
|
|
it 'responds to #filter' do
|
|
|
|
expect(instance).to respond_to(:filter)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'tries filters and returns first one with entries' do
|
2020-10-22 13:57:01 +00:00
|
|
|
allow(mocked_ldap).to receive(:entries?).and_return(true)
|
2017-04-19 10:09:54 +00:00
|
|
|
expect(instance.filter).to be_a(String)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails if no filter found entries' do
|
|
|
|
allow(mocked_ldap).to receive(:entries?).and_return(false)
|
|
|
|
expect(instance.filter).to be nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#uid_attribute' do
|
2017-04-19 10:09:54 +00:00
|
|
|
|
|
|
|
it 'responds to #uid_attribute' do
|
|
|
|
expect(instance).to respond_to(:uid_attribute)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the uid attribute' do
|
|
|
|
expect(instance.uid_attribute).to be_a(String)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|