summaryrefslogtreecommitdiffstats
path: root/spec/unit/provider/user/useradd_spec.rb
blob: 26367c5846a6630294afcaa1226a8b7f9a72dfd5 (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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../../spec_helper'

provider_class = Puppet::Type.type(:user).provider(:useradd)

describe provider_class do
  before do
    @resource = stub("resource", :name => "myuser", :managehome? => nil)
    @resource.stubs(:should).returns "fakeval"
    @resource.stubs(:[]).returns "fakeval"
    @provider = provider_class.new(@resource)
  end

  # #1360
  it "should add -o when allowdupe is enabled and the user is being created" do
    @resource.expects(:allowdupe?).returns true
    @provider.stubs(:execute)
    @provider.expects(:execute).with { |args| args.include?("-o") }
    @provider.create
  end

  it "should add -o when allowdupe is enabled and the uid is being modified" do
    @resource.expects(:allowdupe?).returns true
    @provider.expects(:execute).with { |args| args.include?("-o") }

    @provider.uid = 150
  end

  it "should set password age rules" do
    provider_class.has_feature :manages_password_age
    @resource = Puppet::Type.type(:user).new :name => "myuser", :password_min_age => 5, :password_max_age => 10, :provider => :useradd
    @provider = provider_class.new(@resource)
    @provider.stubs(:execute)
    @provider.expects(:execute).with { |cmd, *args| args == ["-m", 5, "-M", 10, "myuser"] }
    @provider.create
  end

  describe "when checking to add allow dup" do
    it "should check allow dup" do
      @resource.expects(:allowdupe?)
      @provider.check_allow_dup
    end

    it "should return an array with a flag if dup is allowed" do
      @resource.stubs(:allowdupe?).returns true
      @provider.check_allow_dup.must == ["-o"]
    end

    it "should return an empty array if no dup is allowed" do
      @resource.stubs(:allowdupe?).returns false
      @provider.check_allow_dup.must == []
    end
  end

  describe "when checking manage home" do
    it "should check manage home" do
      @resource.expects(:managehome?)
      @provider.check_manage_home
    end

    it "should return an array with -m flag if home is managed" do
      @resource.stubs(:managehome?).returns true
      @provider.check_manage_home.must == ["-m"]
    end

    it "should return an array with -M if home is not managed and on Redhat" do
      Facter.stubs(:value).with("operatingsystem").returns("RedHat")
      @resource.stubs(:managehome?).returns false
      @provider.check_manage_home.must == ["-M"]
    end

    it "should return an empty array if home is not managed and not on Redhat" do
      Facter.stubs(:value).with("operatingsystem").returns("some OS")
      @resource.stubs(:managehome?).returns false
      @provider.check_manage_home.must == []
    end
  end

  describe "when adding properties" do
    it "should get the valid properties"
    it "should not add the ensure property"
    it "should add the flag and value to an array"
    it "should return and array of flags and values"
  end

  describe "when calling addcmd" do
    before do
      @resource.stubs(:allowdupe?).returns true
      @resource.stubs(:managehome?).returns true
    end

    it "should call command with :add" do
      @provider.expects(:command).with(:add)
      @provider.addcmd
    end

    it "should add properties" do
      @provider.expects(:add_properties).returns([])
      @provider.addcmd
    end

    it "should check and add if dup allowed" do
      @provider.expects(:check_allow_dup).returns([])
      @provider.addcmd
    end

    it "should check and add if home is managed" do
      @provider.expects(:check_manage_home).returns([])
      @provider.addcmd
    end

    it "should add the resource :name" do
      @resource.expects(:[]).with(:name)
      @provider.addcmd
    end

    it "should return an array with full command" do
      @provider.stubs(:command).with(:add).returns("useradd")
      @provider.stubs(:add_properties).returns(["-G", "somegroup"])
      @resource.stubs(:[]).with(:name).returns("someuser")
      @resource.stubs(:[]).with(:expiry).returns("somedate")
      @provider.addcmd.must == ["useradd", "-G", "somegroup", "-o", "-m", '-e somedate', "someuser"]
    end

    it "should return an array without -e if expery is undefined full command" do
      @provider.stubs(:command).with(:add).returns("useradd")
      @provider.stubs(:add_properties).returns(["-G", "somegroup"])
      @resource.stubs(:[]).with(:name).returns("someuser")
      @resource.stubs(:[]).with(:expiry).returns nil
      @provider.addcmd.must == ["useradd", "-G", "somegroup", "-o", "-m", "someuser"]
    end
  end
end