summaryrefslogtreecommitdiff
path: root/spec/unit/provider/user/pw_spec.rb
blob: fb7c9211a1b29d276894cdbd0d1a017ca4cf6977 (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
#
# Author:: Stephen Haynes (<sh@nomitor.com>)
# Copyright:: Copyright 2008-2016, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"

describe Chef::Provider::User::Pw do
  before(:each) do
    @node = Chef::Node.new
    @events = Chef::EventDispatch::Dispatcher.new
    @run_context = Chef::RunContext.new(@node, {}, @events)

    @new_resource = Chef::Resource::User::PwUser.new("adam")
    @new_resource.comment   "Adam Jacob"
    @new_resource.uid       1000
    @new_resource.gid       1000
    @new_resource.home      "/home/adam"
    @new_resource.shell     "/usr/bin/zsh"
    @new_resource.password  "abracadabra"

    # XXX: rip out in Chef-13
    Chef::Config[:treat_deprecation_warnings_as_errors] = false
    @new_resource.supports :manage_home => true

    @current_resource = Chef::Resource::User::PwUser.new("adam")
    @current_resource.comment  "Adam Jacob"
    @current_resource.uid      1000
    @current_resource.gid      1000
    @current_resource.home     "/home/adam"
    @current_resource.shell    "/usr/bin/zsh"
    @current_resource.password "abracadabra"

    @provider = Chef::Provider::User::Pw.new(@new_resource, @run_context)
    @provider.current_resource = @current_resource
  end

  describe "setting options to the pw command" do
    field_list = {
      "comment" => "-c",
      "home" => "-d",
      "gid" => "-g",
      "uid" => "-u",
      "shell" => "-s",
    }
    field_list.each do |attribute, option|
      it "should check for differences in #{attribute} between the new and current resources" do
        expect(@current_resource).to receive(attribute)
        expect(@new_resource).to receive(attribute)
        @provider.set_options
      end

      it "should set the option for #{attribute} if the new resources #{attribute} is not null" do
        allow(@new_resource).to receive(attribute).and_return("hola")
        expect(@provider.set_options).to eql(" #{@new_resource.username} #{option} '#{@new_resource.send(attribute)}' -m")
      end

      it "should set the option for #{attribute} if the new resources #{attribute} is not null, without homedir management" do
        allow(@new_resource).to receive(:supports).and_return({ :manage_home => false })
        allow(@new_resource).to receive(attribute).and_return("hola")
        expect(@provider.set_options).to eql(" #{@new_resource.username} #{option} '#{@new_resource.send(attribute)}'")
      end
    end

    it "should combine all the possible options" do
      match_string = " adam"
      field_list.sort { |a, b| a[0] <=> b[0] }.each do |attribute, option|
        allow(@new_resource).to receive(attribute).and_return("hola")
        match_string << " #{option} 'hola'"
      end
      match_string << " -m"
      expect(@provider.set_options).to eql(match_string)
    end
  end

  describe "create_user" do
    before(:each) do
      allow(@provider).to receive(:run_command).and_return(true)
      allow(@provider).to receive(:modify_password).and_return(true)
    end

    it "should run pw useradd with the return of set_options" do
      expect(@provider).to receive(:run_command).with({ :command => "pw useradd adam -m" }).and_return(true)
      @provider.create_user
    end

    it "should modify the password" do
      expect(@provider).to receive(:modify_password).and_return(true)
      @provider.create_user
    end
  end

  describe "manage_user" do
    before(:each) do
      allow(@provider).to receive(:run_command).and_return(true)
      allow(@provider).to receive(:modify_password).and_return(true)
    end

    it "should run pw usermod with the return of set_options" do
      expect(@provider).to receive(:run_command).with({ :command => "pw usermod adam -m" }).and_return(true)
      @provider.manage_user
    end

    it "should modify the password" do
      expect(@provider).to receive(:modify_password).and_return(true)
      @provider.create_user
    end
  end

  describe "remove_user" do
    it "should run pw userdel with the new resources user name" do
      @new_resource.supports :manage_home => false
      expect(@provider).to receive(:run_command).with({ :command => "pw userdel #{@new_resource.username}" }).and_return(true)
      @provider.remove_user
    end

    it "should run pw userdel with the new resources user name and -r if manage_home is true" do
      expect(@provider).to receive(:run_command).with({ :command => "pw userdel #{@new_resource.username} -r" }).and_return(true)
      @provider.remove_user
    end
  end

  describe "determining if the user is locked" do
    it "should return true if user is locked" do
      allow(@current_resource).to receive(:password).and_return("*LOCKED*abracadabra")
      expect(@provider.check_lock).to eql(true)
    end

    it "should return false if user is not locked" do
      allow(@current_resource).to receive(:password).and_return("abracadabra")
      expect(@provider.check_lock).to eql(false)
    end
  end

  describe "when locking the user" do
    it "should run pw lock with the new resources username" do
      expect(@provider).to receive(:run_command).with({ :command => "pw lock #{@new_resource.username}" })
      @provider.lock_user
    end
  end

  describe "when unlocking the user" do
    it "should run pw unlock with the new resources username" do
      expect(@provider).to receive(:run_command).with({ :command => "pw unlock #{@new_resource.username}" })
      @provider.unlock_user
    end
  end

  describe "when modifying the password" do
    before(:each) do
      @status = double("Status", :exitstatus => 0)
      allow(@provider).to receive(:popen4).and_return(@status)
      @pid, @stdin, @stdout, @stderr = nil, nil, nil, nil
    end

    describe "and the new password has not been specified" do
      before(:each) do
        allow(@new_resource).to receive(:password).and_return(nil)
      end

      it "logs an appropriate message" do
        @provider.modify_password
      end
    end

    describe "and the new password has been specified" do
      before(:each) do
        allow(@new_resource).to receive(:password).and_return("abracadabra")
      end

      it "should check for differences in password between the new and current resources" do
        expect(@current_resource).to receive(:password)
        expect(@new_resource).to receive(:password)
        @provider.modify_password
      end
    end

    describe "and the passwords are identical" do
      before(:each) do
        allow(@new_resource).to receive(:password).and_return("abracadabra")
        allow(@current_resource).to receive(:password).and_return("abracadabra")
      end

      it "logs an appropriate message" do
        @provider.modify_password
      end
    end

    describe "and the passwords are different" do
      before(:each) do
        allow(@new_resource).to receive(:password).and_return("abracadabra")
        allow(@current_resource).to receive(:password).and_return("sesame")
      end

      it "should log an appropriate message" do
        @provider.modify_password
      end

      it "should run pw usermod with the username and the option -H 0" do
        expect(@provider).to receive(:popen4).with("pw usermod adam -H 0", :waitlast => true).and_return(@status)
        @provider.modify_password
      end

      it "should send the new password to the stdin of pw usermod" do
        @stdin = StringIO.new
        allow(@provider).to receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
        @provider.modify_password
        expect(@stdin.string).to eq("abracadabra\n")
      end

      it "should raise an exception if pw usermod fails" do
        expect(@status).to receive(:exitstatus).and_return(1)
        expect { @provider.modify_password }.to raise_error(Chef::Exceptions::User)
      end

      it "should not raise an exception if pw usermod succeeds" do
        expect(@status).to receive(:exitstatus).and_return(0)
        expect { @provider.modify_password }.not_to raise_error
      end
    end
  end

  describe "when loading the current state" do
    before do
      @provider.new_resource = Chef::Resource::User::PwUser.new("adam")
    end

    it "should raise an error if the required binary /usr/sbin/pw doesn't exist" do
      expect(File).to receive(:exists?).with("/usr/sbin/pw").and_return(false)
      expect { @provider.load_current_resource }.to raise_error(Chef::Exceptions::User)
    end

    it "shouldn't raise an error if /usr/sbin/pw exists" do
      allow(File).to receive(:exists?).and_return(true)
      expect { @provider.load_current_resource }.not_to raise_error
    end
  end
end