<commit_before>#
# Author:: <NAME> (<<EMAIL>>)
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2008, 2010 Opscode, 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::Useradd do
  before(:each) do
    @node = Chef::Node.new
    @run_context = Chef::RunContext.new(@node, {})

    @new_resource = Chef::Resource::User.new("adam", @run_context)
    @new_resource.comment "<NAME>"
    @new_resource.uid 1000
    @new_resource.gid 1000
    @new_resource.home "/home/adam"
    @new_resource.shell "/usr/bin/zsh"
    @new_resource.password "<PASSWORD>"
    @new_resource.system false
    @new_resource.manage_home false
    @new_resource.non_unique false
    @current_resource = Chef::Resource::User.new("adam", @run_context)
    @current_resource.comment "<NAME>"
    @current_resource.uid 1000
    @current_resource.gid 1000
    @current_resource.home "/home/adam"
    @current_resource.shell "/usr/bin/zsh"
    @current_resource.password "<PASSWORD>"
    @current_resource.system false
    @current_resource.manage_home false
    @current_resource.non_unique false
    @current_resource.supports({:manage_home => false, :non_unique => false})
    @provider = Chef::Provider::User::Useradd.new(@new_resource, @run_context)
    @provider.current_resource = @current_resource
  end

  describe "when setting option" do
    field_list = {
      'comment' => "-c",
      'gid' => "-g",
      'uid' => "-u",
      'shell' => "-s",
      'password' => <PASSWORD>"
    }

    field_list.each do |attribute, option|
      it "should check for differences in #{attribute} between the new and current resources" do
        @current_resource.should_receive(attribute)
        @new_resource.should_receive(attribute)
        @provider.universal_options
      end

      it "should set the option for #{attribute} if the new resources #{attribute} is not nil" do
        @new_resource.stub!(attribute).and_return("hola")
        @provider.universal_options.should eql(" #{option} 'hola'")
      end

      it "should set the option for #{attribute} if the new resources #{attribute} is not nil, without homedir management" do
        @new_resource.stub!(:supports).and_return({:manage_home => false,
                                                    :non_unique => false})
        @new_resource.stub!(attribute).and_return("hola")
        @provider.universal_options.should eql(" #{option} 'hola'")
      end

      it "should set the option for #{attribute} if the new resources #{attribute} is not nil, without homedir management (using real attributes)" do
        @new_resource.stub!(:manage_home).and_return(false)
        @new_resource.stub!(:non_unique).and_return(false)
        @new_resource.stub!(attribute).and_return("hola")
        @provider.universal_options.should eql(" #{option} 'hola'")
      end
    end

    it "should combine all the possible options" do
      match_string = ""
      field_list.sort{ |a,b| a[0] <=> b[0] }.each do |attribute, option|
        @new_resource.stub!(attribute).and_return("hola")
        match_string << " #{option} 'hola'"
      end
      @provider.universal_options.should eql(match_string)
    end

    describe "when we want to create a system user" do
      before do
        @new_resource.manage_home(true)
        @new_resource.non_unique(false)
      end

      it "should set useradd -r" do
        @new_resource.system(true)
        @provider.useradd_options.should == " -r"
      end
    end

    describe "when the resource has a different home directory and supports home directory management" do
      before do
        @new_resource.stub!(:home).and_return("/wowaweea")
        @new_resource.stub!(:supports).and_return({:manage_home => true,
                                                   :non_unique => false})
      end

      it "should set -d /homedir -m" do
        @provider.universal_options.should == " -d '/wowaweea'"
        @provider.useradd_options.should == " -m"
      end
    end

    describe "when the resource has a different home directory and supports home directory management (using real attributes)" do
      before do
        @new_resource.stub!(:home).and_return("/wowaweea")
        @new_resource.stub!(:manage_home).and_return(true)
        @new_resource.stub!(:non_unique).and_return(false)
      end

      it "should set -d /homedir -m" do
        @provider.universal_options.should eql(" -d '/wowaweea'")
        @provider.useradd_options.should == " -m"
      end
    end

    describe "when the resource supports non_unique ids" do
      before do
        @new_resource.stub!(:supports).and_return({:manage_home => false,
                                                  :non_unique => true})
      end

      it "should set -m -o" do
        @provider.universal_options.should eql(" -o")
      end
    end

    describe "when the resource supports non_unique ids (using real attributes)" do
      before do
        @new_resource.stub!(:manage_home).and_return(false)
        @new_resource.stub!(:non_unique).and_return(true)
      end

      it "should set -m -o" do
        @provider.universal_options.should eql(" -o")
      end
    end
  end

  describe "when creating a user" do
    before(:each) do
      @current_resource = Chef::Resource::User.new(@new_resource.name, @run_context)
      @current_resource.username(@new_resource.username)
      @provider.current_resource = @current_resource
      @provider.new_resource.manage_home true
      @provider.new_resource.home "/Users/mud"
      @provider.new_resource.gid '23'
    end

    it "runs useradd with the computed command options" do
      command = "useradd -c '<NAME>' -g '23' -p 'abracadabra' -s '/usr/bin/zsh' -u '1000' -d '/Users/mud' -m adam"
      @provider.should_receive(:run_command).with({ :command => command }).and_return(true)
      @provider.create_user
    end

  end

  describe "when managing a user" do
    before(:each) do
      @provider.new_resource.manage_home true
      @provider.new_resource.home "/Users/mud"
      @provider.new_resource.gid '23'<commit_msg>add a spec for the user case when home for new system resource is not specified<commit_after>#
# Author:: <NAME> (<<EMAIL>>)
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2008, 2010 Opscode, 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::Useradd do
  before(:each) do
    @node = Chef::Node.new
    @run_context = Chef::RunContext.new(@node, {})

    @new_resource = Chef::Resource::User.new("adam", @run_context)
    @new_resource.comment "<NAME>"
    @new_resource.uid 1000
    @new_resource.gid 1000
    @new_resource.home "/home/adam"
    @new_resource.shell "/usr/bin/zsh"
    @new_resource.password "<PASSWORD>"
    @new_resource.system false
    @new_resource.manage_home false
    @new_resource.non_unique false
    @current_resource = Chef::Resource::User.new("adam", @run_context)
    @current_resource.comment "<NAME>"
    @current_resource.uid 1000
    @current_resource.gid 1000
    @current_resource.home "/home/adam"
    @current_resource.shell "/usr/bin/zsh"
    @current_resource.password "<PASSWORD>"
    @current_resource.system false
    @current_resource.manage_home false
    @current_resource.non_unique false
    @current_resource.supports({:manage_home => false, :non_unique => false})
    @provider = Chef::Provider::User::Useradd.new(@new_resource, @run_context)
    @provider.current_resource = @current_resource
  end

  describe "when setting option" do
    field_list = {
      'comment' => "-c",
      'gid' => "-g",
      'uid' => "-u",
      'shell' => "-s",
      'password' => <PASSWORD>"
    }

    field_list.each do |attribute, option|
      it "should check for differences in #{attribute} between the new and current resources" do
        @current_resource.should_receive(attribute)
        @new_resource.should_receive(attribute)
        @provider.universal_options
      end

      it "should set the option for #{attribute} if the new resources #{attribute} is not nil" do
        @new_resource.stub!(attribute).and_return("hola")
        @provider.universal_options.should eql(" #{option} 'hola'")
      end

      it "should set the option for #{attribute} if the new resources #{attribute} is not nil, without homedir management" do
        @new_resource.stub!(:supports).and_return({:manage_home => false,
                                                    :non_unique => false})
        @new_resource.stub!(attribute).and_return("hola")
        @provider.universal_options.should eql(" #{option} 'hola'")
      end

      it "should set the option for #{attribute} if the new resources #{attribute} is not nil, without homedir management (using real attributes)" do
        @new_resource.stub!(:manage_home).and_return(false)
        @new_resource.stub!(:non_unique).and_return(false)
        @new_resource.stub!(attribute).and_return("hola")
        @provider.universal_options.should eql(" #{option} 'hola'")
      end
    end

    it "should combine all the possible options" do
      match_string = ""
      field_list.sort{ |a,b| a[0] <=> b[0] }.each do |attribute, option|
        @new_resource.stub!(attribute).and_return("hola")
        match_string << " #{option} 'hola'"
      end
      @provider.universal_options.should eql(match_string)
    end

    describe "when we want to create a system user" do
      before do
        @new_resource.manage_home(true)
        @new_resource.non_unique(false)
      end

      it "should set useradd -r" do
        @new_resource.system(true)
        @provider.useradd_options.should == " -r"
      end
    end

    describe "when the resource has a different home directory and supports home directory management" do
      before do
        @new_resource.stub!(:home).and_return("/wowaweea")
        @new_resource.stub!(:supports).and_return({:manage_home => true,
                                                   :non_unique => false})
      end

      it "should set -d /homedir -m" do
        @provider.universal_options.should == " -d '/wowaweea'"
        @provider.useradd_options.should == " -m"
      end
    end

    describe "when the resource has a different home directory and supports home directory management (using real attributes)" do
      before do
        @new_resource.stub!(:home).and_return("/wowaweea")
        @new_resource.stub!(:manage_home).and_return(true)
        @new_resource.stub!(:non_unique).and_return(false)
      end

      it "should set -d /homedir -m" do
        @provider.universal_options.should eql(" -d '/wowaweea'")
        @provider.useradd_options.should == " -m"
      end
    end

    describe "when the resource supports non_unique ids" do
      before do
        @new_resource.stub!(:supports).and_return({:manage_home => false,
                                                  :non_unique => true})
      end

      it "should set -m -o" do
        @provider.universal_options.should eql(" -o")
      end
    end

    describe "when the resource supports non_unique ids (using real attributes)" do
      before do
        @new_resource.stub!(:manage_home).and_return(false)
        @new_resource.stub!(:non_unique).and_return(true)
      end

      it "should set -m -o" do
        @provider.universal_options.should eql(" -o")
      end
    end
  end

  describe "when creating a user" do
    before(:each) do
      @current_resource = Chef::Resource::User.new(@new_resource.name, @run_context)
      @current_resource.username(@new_resource.username)
      @provider.current_resource = @current_resource
      @provider.new_resource.manage_home true
      @provider.new_resource.home "/Users/mud"
      @provider.new_resource.gid '23'
    end

    it "runs useradd with the computed command options" do
      command = "useradd -c '<NAME>' -g '23' -p 'abracadabra' -s '/usr/bin/zsh' -u '1000' -d '/Users/mud' -m adam"
      @provider.should_receive(:run_command).with({ :command => command }).and_return(true)
      @provider.create_user
    end

    describe "and home is not specified for new system user resource" do

      before do
        @provider.new_resource.system true
        # there is no public API to set attribute's value to nil 
        @provider.new_resource.instance_variable_set("@home", nil)
      end

      it "should not include -d in the command options" do
        command = "useradd -c '<NAME>' -g '23' -p 'abracadabra' -s '/usr/bin/zsh' -u '1000' -r adam"
        @provider.should_receive(:run_command).with({ :command => command }).and_return(true)
        @provider.create_user
      end

    end

  end

  describe "when managing a user" do
    before(:each) do
      @provider.new_resource.manage_home true
      @provider.new_resource.home "/Users/mud"
      @provider.new_resource.gid '23'
{'fraction_non_alphanumeric': 0.11726314279566807, 'fraction_numerical': 0.006454851897009252, 'mean_word_length': 2.922362869198312, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2357173', 'n_tokens_mistral': 4277, 'n_tokens_neox': 4100, 'n_words': 1131}