mirror of
https://github.com/philippdieter/puppet-ldapquery.git
synced 2026-05-05 15:32:47 +00:00
Rewrite for testability
Testing the methods inside of the function is a complete pain. This moves the code out to use the PuppetX pattern and adds some basic unit tests that validate the logic.
This commit is contained in:
4
spec/fixtures/entries_multivalue.obj
vendored
Normal file
4
spec/fixtures/entries_multivalue.obj
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
[Iu:Net::LDAP::Entry<01>dn: uid=zach,ou=users,dc=puppetlabs,dc=com
|
||||
sshpublickey:: c3NoLXJzYSBBQUFBQi4uLjE9PSB1c2VyQHNvbWV3aGVyZQo=
|
||||
sshpublickey:: c3NoLXJzYSBBQUFBQi4uLjI9PSB1c2VyQHNvbWV3aGVyZWVsc2UK
|
||||
:ET
|
||||
8
spec/fixtures/entries_objectClass.obj
vendored
Normal file
8
spec/fixtures/entries_objectClass.obj
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
[Iu:Net::LDAP::Entry<01>dn: uid=zach,ou=users,dc=puppetlabs,dc=com
|
||||
objectclass: posixAccount
|
||||
objectclass: shadowAccount
|
||||
objectclass: inetOrgPerson
|
||||
objectclass: puppetPerson
|
||||
objectclass: ldapPublicKey
|
||||
objectclass: top
|
||||
:ET
|
||||
3
spec/fixtures/entries_single.obj
vendored
Normal file
3
spec/fixtures/entries_single.obj
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
[Iu:Net::LDAP::Entry:dn: uid=zach,ou=users,dc=puppetlabs,dc=com
|
||||
uid: zach
|
||||
:ET
|
||||
10
spec/spec_helper.rb
Normal file
10
spec/spec_helper.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
require 'puppetlabs_spec_helper/module_spec_helper'
|
||||
require 'rspec-puppet'
|
||||
|
||||
fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
|
||||
|
||||
RSpec.configure do |c|
|
||||
c.module_path = File.join(fixture_path, 'modules')
|
||||
c.manifest_dir = File.join(fixture_path, 'manifests')
|
||||
c.mock_framework = :rspec
|
||||
end
|
||||
77
spec/unit/ldapquery_spec.rb
Normal file
77
spec/unit/ldapquery_spec.rb
Normal file
@@ -0,0 +1,77 @@
|
||||
require 'spec_helper'
|
||||
require 'puppet_x/ldapquery'
|
||||
require 'net/ldap'
|
||||
|
||||
describe 'PuppetX::LDAPquery' do
|
||||
describe 'results' do
|
||||
let (:conf) { {
|
||||
:host => 'ldap.example.com',
|
||||
:port => 9009,
|
||||
} }
|
||||
|
||||
let (:base) { 'dc=example,dc=com' }
|
||||
|
||||
it "should fail with no filter" do
|
||||
filter = ''
|
||||
attributes = ['uid']
|
||||
expect { PuppetX::LDAPquery.new(filter, attributes).results }.to raise_error
|
||||
end
|
||||
|
||||
it "should not fail when using defaults in puppet.conf" do
|
||||
filter = '(uid=zach)'
|
||||
attributes = ['uid']
|
||||
l = PuppetX::LDAPquery.new(filter, attributes, base)
|
||||
expect { l.get_config }.to_not raise_error
|
||||
end
|
||||
|
||||
it 'should return the desired results' do
|
||||
filter = '(uid=zach)'
|
||||
attributes = ['uid']
|
||||
|
||||
wanted = [{"dn"=>"uid=zach,ou=users,dc=puppetlabs,dc=com", "uid"=>"zach"}]
|
||||
entries = Marshal.load(File.read("spec/fixtures/entries_single.obj"))
|
||||
|
||||
l = PuppetX::LDAPquery.new(filter, attributes, base)
|
||||
|
||||
expect(l).to receive(:get_entries).and_return(entries)
|
||||
expect(l.results).to match(wanted)
|
||||
end
|
||||
|
||||
context "a multivalued attribute is requested" do
|
||||
it 'should return the attribute values as an array to the attribute' do
|
||||
filter = '(uid=zach)'
|
||||
attributes = ['objectClass']
|
||||
|
||||
wanted = [{"dn"=>"uid=zach,ou=users,dc=puppetlabs,dc=com",
|
||||
"objectclass"=> [
|
||||
"posixAccount",
|
||||
"shadowAccount",
|
||||
"inetOrgPerson",
|
||||
"puppetPerson",
|
||||
"ldapPublicKey",
|
||||
"top"]}]
|
||||
|
||||
entries = Marshal.load(File.read("spec/fixtures/entries_objectClass.obj"))
|
||||
|
||||
l = PuppetX::LDAPquery.new(filter, attributes, base)
|
||||
expect(l).to receive(:get_entries).and_return(entries)
|
||||
expect(l.results).to match(wanted)
|
||||
end
|
||||
it 'should return the attributes without new lines' do
|
||||
filter = '(uid=zach)'
|
||||
attributes = ['sshPublicKey']
|
||||
|
||||
wanted = [{"dn"=>"uid=zach,ou=users,dc=puppetlabs,dc=com",
|
||||
"sshpublickey"=>
|
||||
["ssh-rsa AAAAB...1== user@somewhere",
|
||||
"ssh-rsa AAAAB...2== user@somewhereelse"]}]
|
||||
|
||||
entries = Marshal.load(File.read("spec/fixtures/entries_multivalue.obj"))
|
||||
|
||||
l = PuppetX::LDAPquery.new(filter, attributes, base)
|
||||
expect(l).to receive(:get_entries).and_return(entries)
|
||||
expect(l.results).to match(wanted)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user