\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef/embedded/lib/ruby/gems/2.7.0/gems/chef-16.3.45/spec/unit/provider/user/

Viewing File: pw_spec.rb

#
# Author:: Stephen Haynes (<sh@nomitor.com>)
# Copyright:: Copyright (c) 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"

    @new_resource.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 |property, option|
      it "should check for differences in #{property} between the new and current resources" do
        expect(@current_resource).to receive(property)
        expect(@new_resource).to receive(property)
        @provider.set_options
      end

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

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

  describe "create_user" do
    before(:each) do
      allow(@provider).to receive(:shell_out_compacted!).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(:shell_out_compacted!).with( "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(:shell_out_compacted!).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(:shell_out_compacted!).with( "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.manage_home false
      expect(@provider).to receive(:shell_out_compacted!).with( "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(:shell_out_compacted!).with( "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(:shell_out_compacted!).with( "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(:shell_out_compacted!).with( "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(:shell_out_compacted!).and_return(@status)
    end

    describe "and the new password has not been specified" do
      before(:each) do
        @new_resource.password(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
        @new_resource.password("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).and_call_original.at_least(:once)
        @provider.modify_password
      end
    end

    describe "and the passwords are identical" do
      before(:each) do
        @new_resource.password("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
        @new_resource.password("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(:shell_out_compacted!).with( "pw usermod adam -H 0", { input: "abracadabra" }).and_return(@status)
        @provider.modify_password
      end

      it "should raise an exception if pw usermod fails" do
        expect(@provider).to receive(:shell_out_compacted!).and_raise(Mixlib::ShellOut::ShellCommandFailed)
        expect { @provider.modify_password }.to raise_error(Mixlib::ShellOut::ShellCommandFailed)
      end

      it "should not raise an exception if pw usermod succeeds" do
        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(:exist?).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(:exist?).and_return(true)
      expect { @provider.load_current_resource }.not_to raise_error
    end
  end
end