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

#
# Author:: Doug MacEachern (<dougm@vmware.com>)
# Copyright:: Copyright 2010-2016, VMware, 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"

class Chef
  class Util
    class Windows
      class NetUser
      end
    end
  end
end

describe Chef::Provider::User::Windows do
  let(:logger) { double("Mixlib::Log::Child").as_null_object }

  before(:each) do
    @node = Chef::Node.new
    @new_resource = Chef::Resource::User::WindowsUser.new("monkey")
    @events = Chef::EventDispatch::Dispatcher.new
    @run_context = Chef::RunContext.new(@node, {}, @events)
    allow(@run_context).to receive(:logger).and_return(logger)
    @current_resource = Chef::Resource::User::WindowsUser.new("monkey")

    @net_user = double("Chef::Util::Windows::NetUser")
    allow(Chef::Util::Windows::NetUser).to receive(:new).and_return(@net_user)

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

  it "creates a net_user object with the provided username" do
    @new_resource.username "not-monkey"
    expect(Chef::Util::Windows::NetUser).to receive(:new).with("not-monkey")
    @provider = Chef::Provider::User::Windows.new(@new_resource, @run_context)
  end

  describe "when comparing the user's current properties to the desired properties" do
    before do
      @new_resource.full_name "Adam Jacob"
      @new_resource.comment   "Some comments"
      @new_resource.uid       1000
      @new_resource.gid       1000
      @new_resource.home      "/home/adam"
      @new_resource.shell     "/usr/bin/zsh"
      @new_resource.password  "abracadabra"

      @provider.current_resource = @new_resource.clone
    end

    describe "and the properties match" do
      it "doesn't set the comment field to be updated" do
        expect(@provider.set_options).not_to have_key(:comment)
      end

      it "doesn't set the home directory to be updated" do
        expect(@provider.set_options).not_to have_key(:home_dir)
      end

      it "doesn't set the group id to be updated" do
        expect(@provider.set_options).not_to have_key(:primary_group_id)
      end

      it "doesn't set the user id to be updated" do
        expect(@provider.set_options).not_to have_key(:user_id)
      end

      it "doesn't set the shell to be updated" do
        expect(@provider.set_options).not_to have_key(:script_path)
      end

      it "doesn't set the password to be updated" do
        expect(@provider.set_options).not_to have_key(:password)
      end

      it "doesn't set the full_name to be updated" do
        expect(@provider.set_options).not_to have_key(:full_name)
      end
    end

    describe "and the properties do not match" do
      before do
        @current_resource = Chef::Resource::User::WindowsUser.new("adam")
        @current_resource.full_name "Adam Jacob-foo"
        @current_resource.comment   "some comments"
        @current_resource.uid       1111
        @current_resource.gid       1111
        @current_resource.home      "/home/adam-foo"
        @current_resource.shell     "/usr/bin/tcsh"
        @current_resource.password  "foobarbaz"
        @provider.current_resource = @current_resource
      end

      it "marks the full_name field to be updated" do
        expect(@provider.set_options[:full_name]).to eq("Adam Jacob")
      end

      it "marks the comment field to be updated" do
        expect(@provider.set_options[:comment]).to eq("Some comments")
      end

      it "marks the home_dir property to be updated" do
        expect(@provider.set_options[:home_dir]).to eq("/home/adam")
      end

      it "ignores the primary_group_id property" do
        expect(@provider.set_options[:primary_group_id]).to eq(nil)
      end

      it "marks the user_id property to be updated" do
        expect(@provider.set_options[:user_id]).to eq(1000)
      end

      it "marks the script_path property to be updated" do
        expect(@provider.set_options[:script_path]).to eq("/usr/bin/zsh")
      end

      it "marks the password property to be updated" do
        expect(@provider.set_options[:password]).to eq("abracadabra")
      end
    end
  end

  describe "when creating the user" do
    it "should call @net_user.add with the return of set_options" do
      allow(@provider).to receive(:set_options).and_return(name: "monkey")
      expect(@net_user).to receive(:add).with(name: "monkey")
      @provider.create_user
    end
  end

  describe "manage_user" do
    before(:each) do
      allow(@provider).to receive(:set_options).and_return(name: "monkey")
    end

    it "should call @net_user.update with the return of set_options" do
      expect(@net_user).to receive(:update).with(name: "monkey")
      @provider.manage_user
    end
  end

  describe "when removing the user" do
    it "should call @net_user.delete" do
      expect(@net_user).to receive(:delete)
      @provider.remove_user
    end
  end

  describe "when checking if the user is locked" do
    before(:each) do
      @current_resource.password "abracadabra"
    end

    it "should return true if user is locked" do
      allow(@net_user).to receive(:check_enabled).and_return(true)
      expect(@provider.check_lock).to eql(true)
    end

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

  describe "locking the user" do
    it "should call @net_user.disable_account" do
      allow(@net_user).to receive(:check_enabled).and_return(true)
      expect(@net_user).to receive(:disable_account)
      @provider.lock_user
    end
  end

  describe "unlocking the user" do
    it "should call @net_user.enable_account" do
      allow(@net_user).to receive(:check_enabled).and_return(false)
      expect(@net_user).to receive(:enable_account)
      @provider.unlock_user
    end
  end
end