summaryrefslogtreecommitdiffstats
path: root/spec/unit/provider/group/ldap.rb
blob: ab2bd72aa794860c3b201cc16888e4640f9e4107 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env ruby
#
#  Created by Luke Kanies on 2008-3-10.
#  Copyright (c) 2006. All rights reserved.

require File.dirname(__FILE__) + '/../../../spec_helper'

provider_class = Puppet::Type.type(:group).provider(:ldap)

describe provider_class do
    it "should have the Ldap provider class as its baseclass" do
        provider_class.superclass.should equal(Puppet::Provider::Ldap)
    end

    it "should manage :posixGroup objectclass" do
        provider_class.manager.objectclasses.should == [:posixGroup]
    end

    it "should use 'ou=Groups' as its relative base" do
        provider_class.manager.location.should == "ou=Groups"
    end

    it "should use :cn as its rdn" do
        provider_class.manager.rdn.should == :cn
    end

    it "should map :name to 'cn'" do
        provider_class.manager.ldap_name(:name).should == 'cn'
    end

    it "should map :gid to 'gidNumber'" do
        provider_class.manager.ldap_name(:gid).should == 'gidNumber'
    end

    it "should map :members to 'memberUid', to be used by the user ldap provider" do
        provider_class.manager.ldap_name(:members).should == 'memberUid'
    end

    describe "when being created" do
        before do
            # So we don't try to actually talk to ldap
            @connection = mock 'connection'
            provider_class.manager.stubs(:connect).yields @connection
        end

        describe "with no gid specified" do
            it "should pick the first available GID after the largest existing GID" do
                low = {:name=>["luke"], :gid=>["600"]}
                high = {:name=>["testing"], :gid=>["640"]}
                provider_class.manager.expects(:search).returns([low, high])

                resource = stub 'resource', :should => %w{whatever}
                resource.stubs(:should).with(:gid).returns nil
                resource.stubs(:should).with(:ensure).returns :present
                instance = provider_class.new(:name => "luke", :ensure => :absent)
                instance.stubs(:resource).returns resource

                @connection.expects(:add).with { |dn, attrs| attrs["gidNumber"] == ["641"] }

                instance.create
                instance.flush
            end

            it "should pick '501' as its GID if no groups are found" do
                provider_class.manager.expects(:search).returns nil

                resource = stub 'resource', :should => %w{whatever}
                resource.stubs(:should).with(:gid).returns nil
                resource.stubs(:should).with(:ensure).returns :present
                instance = provider_class.new(:name => "luke", :ensure => :absent)
                instance.stubs(:resource).returns resource

                @connection.expects(:add).with { |dn, attrs| attrs["gidNumber"] == ["501"] }

                instance.create
                instance.flush
            end
        end
    end

    it "should have a method for converting group names to GIDs" do
        provider_class.should respond_to(:name2id)
    end

    describe "when converting from a group name to GID" do
        it "should use the ldap manager to look up the GID" do
            provider_class.manager.expects(:search).with("cn=foo")
            provider_class.name2id("foo")
        end

        it "should return nil if no group is found" do
            provider_class.manager.expects(:search).with("cn=foo").returns nil
            provider_class.name2id("foo").should be_nil
            provider_class.manager.expects(:search).with("cn=bar").returns []
            provider_class.name2id("bar").should be_nil
        end

        # We shouldn't ever actually have more than one gid, but it doesn't hurt
        # to test for the possibility.
        it "should return the first gid from the first returned group" do
            provider_class.manager.expects(:search).with("cn=foo").returns [{:name => "foo", :gid => [10, 11]}, {:name => :bar, :gid => [20, 21]}]
            provider_class.name2id("foo").should == 10
        end
    end
end