\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: solaris_spec.rb

#
# Author:: Adam Jacob (<adam@chef.io>)
# Author:: Daniel DeLeo (<dan@chef.io>)
# Author:: Dave Eddy (<dave@daveeddy.com>)
# Copyright:: Copyright (c) Chef Software Inc.
# Copyright:: Copyright 2015-2016, Dave Eddy
#
# 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::Solaris do

  let(:shellcmdresult) do
    Struct.new(:stdout, :stderr, :exitstatus)
  end

  let(:node) do
    Chef::Node.new.tap do |node|
      node.automatic["platform"] = "solaris2"
    end
  end
  let(:events) { Chef::EventDispatch::Dispatcher.new }
  let(:run_context) { Chef::RunContext.new(node, {}, events) }
  let(:new_resource) do
    Chef::Resource::User::SolarisUser.new("adam", @run_context)
  end
  let(:current_resource) do
    Chef::Resource::User::SolarisUser.new("adam", @run_context)
  end

  subject(:provider) do
    described_class.new(new_resource, run_context).tap do |p|
      p.current_resource = current_resource
      # Prevent the useradd-based provider tests from trying to write /etc/shadow
      allow(p).to receive(:write_shadow_file)
    end
  end

  describe "when we want to set a password" do
    before(:each) do
      new_resource.password "hocus-pocus"
    end

    it "should use its own shadow file writer to set the password" do
      expect(provider).to receive(:write_shadow_file)
      allow(provider).to receive(:shell_out_compacted!).and_return(true)
      provider.manage_user
    end

    it "should write out a modified version of the password file" do
      # Let this test run #write_shadow_file
      allow(provider).to receive(:write_shadow_file).and_call_original
      password_file = Tempfile.new("shadow")
      password_file.puts "adam:existingpassword:15441::::::"
      password_file.close
      stub_const("Chef::Provider::User::Solaris::PASSWORD_FILE", password_file.path)
      allow(provider).to receive(:shell_out_compacted!).and_return(true)
      # may not be able to write to /etc for tests...
      temp_file = Tempfile.new("shadow")
      allow(Tempfile).to receive(:new).with("shadow", "/etc").and_return(temp_file)
      new_resource.password "verysecurepassword"
      provider.manage_user
      expect(::File.open(password_file.path, "r").read).to match(/adam:verysecurepassword:/)
      password_file.unlink
    end
  end

  describe "#create_user" do
    context "with a system user" do
      before { new_resource.system(true) }
      it "should not pass -r" do
        expect(provider).to receive(:shell_out_compacted!).with( "useradd", "adam")
        provider.create_user
      end
    end

    context "with manage_home" do
      before { new_resource.manage_home(true) }
      it "should not pass -r" do
        expect(provider).to receive(:shell_out_compacted!).with( "useradd", "-m", "adam")
        provider.create_user
      end
    end
  end

  describe "when managing user locked status" do
    let(:user_lock) { "adam:FOO:::::::" }
    let(:shadow_file_contents) do
      %W{
        user1:LK:::::::
        #{user_lock}
        user2:NP:::::::
      }
    end

    describe "when determining if the user is locked" do
      before do
        allow(IO).to receive(:read).and_return(shadow_file_contents.join("\n"))
      end

      context "when user does not exist" do
        let(:user_lock) { "other_user:FOO:::::::" }

        it "should raise a sensible error" do
          expect { provider.check_lock }.to raise_error(Chef::Exceptions::User)
        end
      end

      # locked shadow lines
      [
        "adam:*LK*:::::::",
        "adam:*LK*foobar:::::::",
        "adam:*LK*bahamas10:::::::",
        "adam:*LK*goonawaLK:::::::",
        "adam:*LK*LKgir:::::::",
        "adam:*LK*L....:::::::",
      ].each do |shadow|
        context "for user 'adam' with entry '#{shadow}'" do
          let(:user_lock) { shadow }

          it "should return true" do
            expect(provider.check_lock).to eql(true)
          end
        end
      end

      # unlocked shadow lines
      [
        "adam:NP:::::::",
        "adam:*NP*:::::::",
        "adam:foobar:::::::",
        "adam:bahamas10:::::::",
        "adam:goonawaLK:::::::",
        "adam:LKgir:::::::",
        "adam:L...:::::::",
      ].each do |shadow|
        context "for user 'adam' with entry '#{shadow}'" do
          let(:user_lock) { shadow }

          it "should return false" do
            expect(provider.check_lock).to eql(false)
          end
        end
      end
    end

    describe "when locking the user" do
      it "should run passwd -l with the new resources username" do
        shell_return = shellcmdresult.new("", "", 0)
        expect(provider).to receive(:shell_out_compacted!).with("passwd", "-l", "adam").and_return(shell_return)
        provider.lock_user
      end
    end

    describe "when unlocking the user" do
      it "should run passwd -u with the new resources username" do
        shell_return = shellcmdresult.new("", "", 0)
        expect(provider).to receive(:shell_out_compacted!).with("passwd", "-u", "adam").and_return(shell_return)
        provider.unlock_user
      end
    end
  end
end