summaryrefslogtreecommitdiff
path: root/spec/unit/type/sudoers.rb
blob: 39c4e0b31a4c74039a99e5a2810072eb7a476d45 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }

describe Puppet::Type.type(:sudoers) do
  before(:each) do
    @type = Puppet::Type.type(:sudoers)
    stub_default_provider!(:parsed)
    # these are the initial params used for testing
    @init_params = {
      :ensure => :present,
      :name => :name,
      :comment => :mycomment,
      :type => 'default',
      :parameters => ['1']
      # :target => '/etc/sudoers'
    }
    # these are all of the attributes that exist
    @attributes=[
      :ensure, :name, :comment, :target, :type, 
      :sudo_alias, :items,
      :parameters,
      :users, :hosts, :commands
    ]
    @valid_params = @init_params
  end

  it "should exist" do
    @type.should_not be_nil
  end
  it "should not have valid attributes that are nil" do
    @attributes.each do |attr|
      @type.attrclass(attr).should_not be_nil
    end
  end

  describe 'shared attributes' do
    describe 'ensure' do
      it 'should only accept absent/present' do
        restricted_params(:ensure, [:absent, :present], @valid_params)
      end
    end
    describe 'comment attribute' do
      it 'should accept a value' do
        should_accept(:comment, 'foo')
      end
      it 'should default to empty string' do
        should_default_to(:comment, '')
      end
    end
    describe 'name attribute' do
      it 'should accept a value' do
        should_accept(:name, 'foo')
      end
      it 'should be required' do
        should_require(:name)
      end
    end
  end

  describe "the user alias" do
    before(:each) do
      @alias_params = @init_params.merge({
        :type => 'alias',
        :sudo_alias => 'Cmnd_Alias',
        :items => 'item1'
      })
      # set what your valid params are
      @valid_params = @alias_params
    end
    describe 'require attributes' do
      # isrequired in puppet is broken
      #self.should_require([:sudo_alias, :items])
    end
    describe "sudo_alias" do
      it "should only accept certain aliases" do 
        valid= [
          :Cmnd_Alias, :Host_Alias, :User_Alias, :Runas_Alias,
          :Cmnd, :Host, :User, :Runas
        ]
        restricted_params(:sudo_alias, valid, @valid_params)
      end
    end
    describe 'items' do
      it 'should be required' do
        should_require(:items)
      end
      it 'should take a single element' do
        with(valid_params_with({:items => 'one'}))[:items]    .should == ['one']
      end
      it 'should take a single element array' do
        should_accept(:items, ['one'])
      end
      it 'should take an array' do
        should_accept(:items, ['one', 'two'])
      end
    end
    describe 'type' do
      it 'should not accept other type' do
        lambda { with(valid_params_with({:type => 'bad_type'}))}.should raise_error
      end
      it 'should not accept other type' do
        lambda { with(valid_params_with({:type => 'user_spec'}))}.should raise_error
      end
    end
  end

  describe 'sudo defaults' do
    before do
      @default_params = @init_params.merge({
        :type => 'default',
        :parameters => 'params'
      })
      # set what your valid params are
      @valid_params = @default_params
    end
    describe 'parameters' do
      it 'should take a single element' do
        with(valid_params_with({:parameters => 'one'}))[:parameters].should == ['one']
      end
      it 'should take a single element array' do
        should_accept(:parameters, ['one'])
      end
      it 'should take an array' do
        should_accept(:parameters, ['one', 'two'])
      end
      it 'should require a parameter' do
        should_require(:parameters)
      end
    end
  end

  describe 'user specs' do
    before do
      # user spec setup
      @spec_params = @init_params.merge({
        :type => 'user_spec',
        :users => 'danbode',
        :hosts => 'coolmachine@awesomeocorp.org', 
        :commands => '/bin/true',
      })
      @valid_params = @spec_params
    end
    describe 'users' do
      it 'should accept an array' do
        should_accept_array(:users, ['alice', 'bob'])
      end
      it 'should not accept Defaults' do
        should_not_accept(:usrs, 'Defaults')
      end
      it 'should be required' do
        should_require(:users)
      end
    end
    describe 'hosts' do
      it 'should accept an array' do
        should_accept_array(:hosts, ['alice', 'bob'])
      end
      it 'should be required' do
        should_require(:hosts)
      end
    end
    describe 'commands' do
      it 'should accept an array' do
        should_accept_array(:commands, ['alice', 'bob'])
      end
      it 'should be required' do
        should_require(:commands)
      end
    end
  end
end