summaryrefslogtreecommitdiffstats
path: root/spec/unit/provider/user/ldap.rb
blob: f1c57177915ff39c9bafea2ae6b5b9bd28beef84 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/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(:user).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 :posixAccount and :person objectclasses" do
        provider_class.manager.objectclasses.should == [:posixAccount, :person]
    end

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

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

    it "should be able to manage passwords" do
        provider_class.should be_manages_passwords
    end
    
    it "should use the ldap group provider to convert group names to numbers" do
        provider = provider_class.new(:name => "foo")
        Puppet::Type.type(:group).provider(:ldap).expects(:name2id).with("bar").returns 10

        provider.gid = 'bar'
        provider.gid.should == 10
    end

    {:name => "uid",
        :password => "userPassword",
        :comment => "cn",
        :uid => "uidNumber",
        :gid => "gidNumber",
        :home => "homeDirectory",
        :shell => "loginShell"
    }.each do |puppet, ldap|
        it "should map :#{puppet.to_s} to '#{ldap}'" do
            provider_class.manager.ldap_name(puppet).should == ldap
        end
    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

        it "should generate the sn as the last field of the cn" do
            resource = stub 'resource', :should => %w{whatever}
            resource.stubs(:should).with(:comment).returns ["Luke Kanies"]
            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["sn"] == ["Kanies"] }

            instance.create
            instance.flush
        end

        describe "with no uid specified" do
            it "should pick the first available UID after the largest existing UID" do
                low = {:name=>["luke"], :shell=>:absent, :uid=>["600"], :home=>["/h"], :gid=>["1000"], :password=>["blah"], :comment=>["l k"]}
                high = {:name=>["testing"], :shell=>:absent, :uid=>["640"], :home=>["/h"], :gid=>["1000"], :password=>["blah"], :comment=>["t u"]}
                provider_class.manager.expects(:search).returns([low, high])

                resource = stub 'resource', :should => %w{whatever}
                resource.stubs(:should).with(:uid).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["uidNumber"] == ["641"] }

                instance.create
                instance.flush
            end

            it "should pick 501 of no users exist" do
                provider_class.manager.expects(:search).returns nil

                resource = stub 'resource', :should => %w{whatever}
                resource.stubs(:should).with(:uid).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["uidNumber"] == ["501"] }

                instance.create
                instance.flush
            end
        end
    end

    describe "when flushing" do
        before do
            provider_class.stubs(:suitable?).returns true

            @instance = provider_class.new(:name => "myname", :groups => %w{whatever}, :uid => "400")
        end

        it "should remove the :groups value before updating" do
            @instance.class.manager.expects(:update).with { |name, ldap, puppet| puppet[:groups].nil? }

            @instance.flush
        end

        it "should empty the property hash" do
            @instance.class.manager.stubs(:update)

            @instance.flush

            @instance.uid.should == :absent
        end

        it "should empty the ldap property hash" do
            @instance.class.manager.stubs(:update)

            @instance.flush

            @instance.ldap_properties[:uid].should be_nil
        end
    end

    describe "when checking group membership" do
        before do
            @groups = Puppet::Type.type(:group).provider(:ldap)
            @group_manager = @groups.manager
            provider_class.stubs(:suitable?).returns true

            @instance = provider_class.new(:name => "myname")
        end

        it "should show its group membership as the list of all groups returned by an ldap query of group memberships" do
            one = {:name => "one"}
            two = {:name => "two"}
            @group_manager.expects(:search).with("memberUid=myname").returns([one, two])

            @instance.groups.should == "one,two"
        end

        it "should show its group membership as :absent if no matching groups are found in ldap" do
            @group_manager.expects(:search).with("memberUid=myname").returns(nil)

            @instance.groups.should == :absent
        end

        it "should cache the group value" do
            @group_manager.expects(:search).with("memberUid=myname").once.returns nil

            @instance.groups
            @instance.groups.should == :absent
        end
    end

    describe "when modifying group membership" do
        before do
            @groups = Puppet::Type.type(:group).provider(:ldap)
            @group_manager = @groups.manager
            provider_class.stubs(:suitable?).returns true

            @one = {:name => "one", :gid => "500"}
            @group_manager.stubs(:find).with("one").returns(@one)

            @two = {:name => "one", :gid => "600"}
            @group_manager.stubs(:find).with("two").returns(@two)

            @instance = provider_class.new(:name => "myname")

            @instance.stubs(:groups).returns :absent
        end

        it "should fail if the group does not exist" do
            @group_manager.expects(:find).with("mygroup").returns nil

            lambda { @instance.groups = "mygroup" }.should raise_error(Puppet::Error)
        end

        it "should only pass the attributes it cares about to the group manager" do
            @group_manager.expects(:update).with { |name, attrs| attrs[:gid].nil? }

            @instance.groups = "one"
        end

        it "should always include :ensure => :present in the current values" do
            @group_manager.expects(:update).with { |name, is, should| is[:ensure] == :present }

            @instance.groups = "one"
        end

        it "should always include :ensure => :present in the desired values" do
            @group_manager.expects(:update).with { |name, is, should| should[:ensure] == :present }

            @instance.groups = "one"
        end

        it "should always pass the group's original member list" do
            @one[:members] = %w{yay ness}
            @group_manager.expects(:update).with { |name, is, should| is[:members] == %w{yay ness} }

            @instance.groups = "one"
        end

        it "should find the group again when resetting its member list, so it has the full member list" do
            @group_manager.expects(:find).with("one").returns(@one)

            @group_manager.stubs(:update)

            @instance.groups = "one"
        end

        describe "for groups that have no members" do
            it "should create a new members attribute with its value being the user's name" do
                @group_manager.expects(:update).with { |name, is, should| should[:members] == %w{myname} }

                @instance.groups = "one"
            end
        end

        describe "for groups it is being removed from" do
            it "should replace the group's member list with one missing the user's name" do
                @one[:members] = %w{myname a}
                @two[:members] = %w{myname b}

                @group_manager.expects(:update).with { |name, is, should| name == "two" and should[:members] == %w{b} }

                @instance.stubs(:groups).returns "one,two"
                @instance.groups = "one"
            end

            it "should mark the member list as empty if there are no remaining members" do
                @one[:members] = %w{myname}
                @two[:members] = %w{myname b}

                @group_manager.expects(:update).with { |name, is, should| name == "one" and should[:members] == :absent }

                @instance.stubs(:groups).returns "one,two"
                @instance.groups = "two"
            end
        end

        describe "for groups that already have members" do
            it "should replace each group's member list with a new list including the user's name" do
                @one[:members] = %w{a b}
                @group_manager.expects(:update).with { |name, is, should| should[:members] == %w{a b myname} }
                @two[:members] = %w{b c}
                @group_manager.expects(:update).with { |name, is, should| should[:members] == %w{b c myname} }

                @instance.groups = "one,two"
            end
        end

        describe "for groups of which it is a member" do
            it "should do nothing" do
                @one[:members] = %w{a b}
                @group_manager.expects(:update).with { |name, is, should| should[:members] == %w{a b myname} }

                @two[:members] = %w{c myname}
                @group_manager.expects(:update).with { |name, *other| name == "two" }.never

                @instance.stubs(:groups).returns "two"

                @instance.groups = "one,two"
            end
        end
    end
end