summaryrefslogtreecommitdiffstats
path: root/spec/unit/type/user_spec.rb
blob: 590b35a946c3350e3f14ba4ed184f15f82fbda69 (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
280
281
282
283
#!/usr/bin/env ruby

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

user = Puppet::Type.type(:user)

describe user do
    before do
        unless ENV["PATH"].split(File::PATH_SEPARATOR).include?("/usr/sbin")
            ENV["PATH"] += File::PATH_SEPARATOR + "/usr/sbin"
        end
        @provider = stub 'provider'
        @resource = stub 'resource', :resource => nil, :provider => @provider, :line => nil, :file => nil
    end

    it "should have a default provider inheriting from Puppet::Provider" do
        user.defaultprovider.ancestors.should be_include(Puppet::Provider)
    end

    it "should be able to create a instance" do
        user.new(:name => "foo").should_not be_nil
    end

    it "should have an allows_duplicates feature" do
        user.provider_feature(:allows_duplicates).should_not be_nil
    end

    it "should have an manages_homedir feature" do
        user.provider_feature(:manages_homedir).should_not be_nil
    end

    it "should have an manages_passwords feature" do
        user.provider_feature(:manages_passwords).should_not be_nil
    end

    it "should have a manages_solaris_rbac feature" do
        user.provider_feature(:manages_solaris_rbac).should_not be_nil
    end

    describe "instances" do
        it "should have a valid provider" do
            user.new(:name => "foo").provider.class.ancestors.should be_include(Puppet::Provider)
        end

        it "should delegate existence questions to its provider" do
            instance = user.new(:name => "foo")
            instance.provider.expects(:exists?).returns "eh"
            instance.exists?.should == "eh"
        end
    end

    properties = [:ensure, :uid, :gid, :home, :comment, :shell, :password, :groups, :roles, :auths, :profiles, :project, :keys]

    properties.each do |property|
        it "should have a %s property" % property do
            user.attrclass(property).ancestors.should be_include(Puppet::Property)
        end

        it "should have documentation for its %s property" % property do
            user.attrclass(property).doc.should be_instance_of(String)
        end
    end

    list_properties = [:groups, :roles, :auths]

    list_properties.each do |property|
        it "should have a list '%s'" % property do
            user.attrclass(property).ancestors.should be_include(Puppet::Property::List)
        end
    end

    it "should have an ordered list 'profiles'" do
        user.attrclass(:profiles).ancestors.should be_include(Puppet::Property::OrderedList)
    end

    it "should have key values 'keys'" do
        user.attrclass(:keys).ancestors.should be_include(Puppet::Property::KeyValue)
    end

    describe "when retrieving all current values" do
        before do
            @user = user.new(:name => "foo", :uid => 10)
        end

        it "should return a hash containing values for all set properties" do
            @user[:gid] = 10
            @user.property(:ensure).expects(:retrieve).returns :present
            @user.property(:uid).expects(:retrieve).returns 15
            @user.property(:gid).expects(:retrieve).returns 15
            values = @user.retrieve
            [@user.property(:uid), @user.property(:gid)].each { |property| values.should be_include(property) }
        end

        it "should set all values to :absent if the user is absent" do
            @user.property(:ensure).expects(:retrieve).returns :absent
            @user.property(:uid).expects(:retrieve).never
            @user.retrieve[@user.property(:uid)].should == :absent
        end

        it "should include the result of retrieving each property's current value if the user is present" do
            @user.property(:ensure).expects(:retrieve).returns :present
            @user.property(:uid).expects(:retrieve).returns 15
            @user.retrieve[@user.property(:uid)].should == 15
        end
    end

    describe "when managing the ensure property" do
        before do
            @ensure = user.attrclass(:ensure).new(:resource => @resource)
        end

        it "should support a :present value" do
            lambda { @ensure.should = :present }.should_not raise_error
        end

        it "should support an :absent value" do
            lambda { @ensure.should = :absent }.should_not raise_error
        end

        it "should call :create on the provider when asked to sync to the :present state" do
            @provider.expects(:create)
            @ensure.should = :present
            @ensure.sync
        end

        it "should call :delete on the provider when asked to sync to the :absent state" do
            @provider.expects(:delete)
            @ensure.should = :absent
            @ensure.sync
        end

        describe "and determining the current state" do
            it "should return :present when the provider indicates the user exists" do
                @provider.expects(:exists?).returns true
                @ensure.retrieve.should == :present
            end

            it "should return :absent when the provider indicates the user does not exist" do
                @provider.expects(:exists?).returns false
                @ensure.retrieve.should == :absent
            end
        end
    end

    describe "when managing the uid property" do
        it "should convert number-looking strings into actual numbers" do
            uid = user.attrclass(:uid).new(:resource => @resource)
            uid.should = "50"
            uid.should.must == 50
        end

        it "should support UIDs as numbers" do
            uid = user.attrclass(:uid).new(:resource => @resource)
            uid.should = 50
            uid.should.must == 50
        end

        it "should :absent as a value" do
            uid = user.attrclass(:uid).new(:resource => @resource)
            uid.should = :absent
            uid.should.must == :absent
        end
    end

    describe "when managing the gid" do
        it "should :absent as a value" do
            gid = user.attrclass(:gid).new(:resource => @resource)
            gid.should = :absent
            gid.should.must == :absent
        end

        it "should convert number-looking strings into actual numbers" do
            gid = user.attrclass(:gid).new(:resource => @resource)
            gid.should = "50"
            gid.should.must == 50
        end

        it "should support GIDs specified as integers" do
            gid = user.attrclass(:gid).new(:resource => @resource)
            gid.should = 50
            gid.should.must == 50
        end

        it "should support groups specified by name" do
            gid = user.attrclass(:gid).new(:resource => @resource)
            gid.should = "foo"
            gid.should.must == "foo"
        end

        describe "when testing whether in sync" do
            before do
                @gid = user.attrclass(:gid).new(:resource => @resource, :should => %w{foo bar})
            end

            it "should return true if no 'should' values are set" do
                @gid = user.attrclass(:gid).new(:resource => @resource)

                @gid.must be_insync(500)
            end

            it "should return true if any of the specified groups are equal to the current integer" do
                Puppet::Util.expects(:gid).with("foo").returns 300
                Puppet::Util.expects(:gid).with("bar").returns 500

                @gid.must be_insync(500)
            end

            it "should return false if none of the specified groups are equal to the current integer" do
                Puppet::Util.expects(:gid).with("foo").returns 300
                Puppet::Util.expects(:gid).with("bar").returns 500

                @gid.should_not be_insync(700)
            end
        end

        describe "when syncing" do
            before do
                @gid = user.attrclass(:gid).new(:resource => @resource, :should => %w{foo bar})
            end

            it "should use the first found, specified group as the desired value and send it to the provider" do
                Puppet::Util.expects(:gid).with("foo").returns nil
                Puppet::Util.expects(:gid).with("bar").returns 500

                @provider.expects(:gid=).with 500

                @gid.sync
            end
        end
    end

    describe "when managing passwords" do
        before do
            @password = user.attrclass(:password).new(:resource => @resource, :should => "mypass")
        end

        it "should not include the password in the change log when adding the password" do
            @password.change_to_s(:absent, "mypass").should_not be_include("mypass")
        end

        it "should not include the password in the change log when changing the password" do
            @password.change_to_s("other", "mypass").should_not be_include("mypass")
        end

        it "should fail if a ':' is included in the password" do
            lambda { @password.should = "some:thing" }.should raise_error(Puppet::Error)
        end

        it "should allow the value to be set to :absent" do
            lambda { @password.should = :absent }.should_not raise_error
        end
    end

    describe "when manages_solaris_rbac is enabled" do
        before do
            @provider.stubs(:satisfies?).returns(false)
            @provider.expects(:satisfies?).with([:manages_solaris_rbac]).returns(true)
        end

        it "should support a :role value for ensure" do
            @ensure = user.attrclass(:ensure).new(:resource => @resource)
            lambda { @ensure.should = :role }.should_not raise_error
        end
    end

    describe "when user has roles" do
        it "should autorequire roles" do
            testuser = Puppet::Type.type(:user).new(:name => "testuser")
            testuser[:roles] = "testrole"

            testrole = Puppet::Type.type(:user).new(:name => "testrole")

            config = Puppet::Resource::Catalog.new :testing do |conf|
                [testuser, testrole].each { |resource| conf.add_resource resource }
            end
            Puppet::Type::User::ProviderDirectoryservice.stubs(:get_macosx_version_major).returns "10.5"

            rel = testuser.autorequire[0]
            rel.source.ref.should == testrole.ref
            rel.target.ref.should == testuser.ref
        end
    end
end