~ubuntu-branches/ubuntu/trusty/puppet/trusty

« back to all changes in this revision

Viewing changes to spec/unit/util/adsi_spec.rb

  • Committer: Package Import Robot
  • Author(s): Stig Sandbeck Mathisen
  • Date: 2011-10-22 14:08:22 UTC
  • mfrom: (1.1.25) (3.1.32 sid)
  • Revision ID: package-import@ubuntu.com-20111022140822-odxde5lohc45yhuz
Tags: 2.7.6-1
* New upstream release (CVE-2011-3872)
* Remove cherry-picked "groupadd_aix_warning" patch
* Install all new manpages

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
require 'spec_helper'
 
4
 
 
5
require 'puppet/util/adsi'
 
6
 
 
7
describe Puppet::Util::ADSI do
 
8
  let(:connection) { stub 'connection' }
 
9
 
 
10
  before(:each) do
 
11
    Puppet::Util::ADSI.instance_variable_set(:@computer_name, 'testcomputername')
 
12
    Puppet::Util::ADSI.stubs(:connect).returns connection
 
13
  end
 
14
 
 
15
  after(:each) do
 
16
    Puppet::Util::ADSI.instance_variable_set(:@computer_name, nil)
 
17
  end
 
18
 
 
19
  it "should generate the correct URI for a resource" do
 
20
    Puppet::Util::ADSI.uri('test', 'user').should == "WinNT://testcomputername/test,user"
 
21
  end
 
22
 
 
23
  it "should be able to get the name of the computer" do
 
24
    Puppet::Util::ADSI.computer_name.should == 'testcomputername'
 
25
  end
 
26
 
 
27
  it "should be able to provide the correct WinNT base URI for the computer" do
 
28
    Puppet::Util::ADSI.computer_uri.should == "WinNT://testcomputername"
 
29
  end
 
30
 
 
31
  describe ".sid_for_account" do
 
32
    it "should return the SID" do
 
33
      result = [stub('account', :Sid => 'S-1-1-50')]
 
34
      connection.expects(:execquery).returns(result)
 
35
 
 
36
      Puppet::Util::ADSI.sid_for_account('joe').should == 'S-1-1-50'
 
37
    end
 
38
 
 
39
    it "should return nil if the account does not exist" do
 
40
        connection.expects(:execquery).returns([])
 
41
 
 
42
      Puppet::Util::ADSI.sid_for_account('foobar').should be_nil
 
43
    end
 
44
  end
 
45
 
 
46
  describe Puppet::Util::ADSI::User do
 
47
    let(:username)  { 'testuser' }
 
48
 
 
49
    it "should generate the correct URI" do
 
50
      Puppet::Util::ADSI::User.uri(username).should == "WinNT://testcomputername/#{username},user"
 
51
    end
 
52
 
 
53
    it "should be able to create a user" do
 
54
      adsi_user = stub('adsi')
 
55
 
 
56
      connection.expects(:Create).with('user', username).returns(adsi_user)
 
57
 
 
58
      user = Puppet::Util::ADSI::User.create(username)
 
59
 
 
60
      user.should be_a(Puppet::Util::ADSI::User)
 
61
      user.native_user.should == adsi_user
 
62
    end
 
63
 
 
64
    it "should be able to check the existence of a user" do
 
65
      Puppet::Util::ADSI.expects(:connect).with("WinNT://testcomputername/#{username},user").returns connection
 
66
      Puppet::Util::ADSI::User.exists?(username).should be_true
 
67
    end
 
68
 
 
69
    it "should be able to delete a user" do
 
70
      connection.expects(:Delete).with('user', username)
 
71
 
 
72
      Puppet::Util::ADSI::User.delete(username)
 
73
    end
 
74
 
 
75
    describe "an instance" do
 
76
      let(:adsi_user) { stub 'user' }
 
77
      let(:user)      { Puppet::Util::ADSI::User.new(username, adsi_user) }
 
78
 
 
79
      it "should provide its groups as a list of names" do
 
80
        names = ["group1", "group2"]
 
81
 
 
82
        groups = names.map { |name| mock('group', :Name => name) }
 
83
 
 
84
        adsi_user.expects(:Groups).returns(groups)
 
85
 
 
86
        user.groups.should =~ names
 
87
      end
 
88
 
 
89
      it "should be able to test whether a given password is correct" do
 
90
        Puppet::Util::ADSI::User.expects(:logon).with(username, 'pwdwrong').returns(false)
 
91
        Puppet::Util::ADSI::User.expects(:logon).with(username, 'pwdright').returns(true)
 
92
 
 
93
        user.password_is?('pwdwrong').should be_false
 
94
        user.password_is?('pwdright').should be_true
 
95
      end
 
96
 
 
97
      it "should be able to set a password" do
 
98
        adsi_user.expects(:SetPassword).with('pwd')
 
99
        adsi_user.expects(:SetInfo).at_least_once
 
100
 
 
101
        flagname = "UserFlags"
 
102
        fADS_UF_DONT_EXPIRE_PASSWD = 0x10000
 
103
 
 
104
        adsi_user.expects(:Get).with(flagname).returns(0)
 
105
        adsi_user.expects(:Put).with(flagname, fADS_UF_DONT_EXPIRE_PASSWD)
 
106
 
 
107
        user.password = 'pwd'
 
108
      end
 
109
 
 
110
      it "should generate the correct URI" do
 
111
        user.uri.should == "WinNT://testcomputername/#{username},user"
 
112
      end
 
113
 
 
114
      describe "when given a set of groups to which to add the user" do
 
115
        let(:groups_to_set) { 'group1,group2' }
 
116
 
 
117
        before(:each) do
 
118
          user.expects(:groups).returns ['group2', 'group3']
 
119
        end
 
120
 
 
121
        describe "if membership is specified as inclusive" do
 
122
          it "should add the user to those groups, and remove it from groups not in the list" do
 
123
            group1 = stub 'group1'
 
124
            group1.expects(:Add).with("WinNT://testcomputername/#{username},user")
 
125
 
 
126
            group3 = stub 'group1'
 
127
            group3.expects(:Remove).with("WinNT://testcomputername/#{username},user")
 
128
 
 
129
            Puppet::Util::ADSI.expects(:connect).with('WinNT://testcomputername/group1,group').returns group1
 
130
            Puppet::Util::ADSI.expects(:connect).with('WinNT://testcomputername/group3,group').returns group3
 
131
 
 
132
            user.set_groups(groups_to_set, false)
 
133
          end
 
134
        end
 
135
 
 
136
        describe "if membership is specified as minimum" do
 
137
          it "should add the user to the specified groups without affecting its other memberships" do
 
138
            group1 = stub 'group1'
 
139
            group1.expects(:Add).with("WinNT://testcomputername/#{username},user")
 
140
 
 
141
            Puppet::Util::ADSI.expects(:connect).with('WinNT://testcomputername/group1,group').returns group1
 
142
 
 
143
            user.set_groups(groups_to_set, true)
 
144
          end
 
145
        end
 
146
      end
 
147
    end
 
148
  end
 
149
 
 
150
  describe Puppet::Util::ADSI::Group do
 
151
    let(:groupname)  { 'testgroup' }
 
152
 
 
153
    describe "an instance" do
 
154
      let(:adsi_group) { stub 'group' }
 
155
      let(:group)      { Puppet::Util::ADSI::Group.new(groupname, adsi_group) }
 
156
 
 
157
      it "should be able to add a member" do
 
158
        adsi_group.expects(:Add).with("WinNT://testcomputername/someone,user")
 
159
 
 
160
        group.add_member('someone')
 
161
      end
 
162
 
 
163
      it "should be able to remove a member" do
 
164
        adsi_group.expects(:Remove).with("WinNT://testcomputername/someone,user")
 
165
 
 
166
        group.remove_member('someone')
 
167
      end
 
168
 
 
169
      it "should provide its groups as a list of names" do
 
170
        names = ['user1', 'user2']
 
171
 
 
172
        users = names.map { |name| mock('user', :Name => name) }
 
173
 
 
174
        adsi_group.expects(:Members).returns(users)
 
175
 
 
176
        group.members.should =~ names
 
177
      end
 
178
 
 
179
      it "should be able to add a list of users to a group" do
 
180
        names = ['user1', 'user2']
 
181
        adsi_group.expects(:Members).returns names.map{|n| stub(:Name => n)}
 
182
 
 
183
        adsi_group.expects(:Remove).with('WinNT://testcomputername/user1,user')
 
184
        adsi_group.expects(:Add).with('WinNT://testcomputername/user3,user')
 
185
 
 
186
        group.set_members(['user2', 'user3'])
 
187
      end
 
188
 
 
189
      it "should generate the correct URI" do
 
190
        group.uri.should == "WinNT://testcomputername/#{groupname},group"
 
191
      end
 
192
    end
 
193
 
 
194
    it "should generate the correct URI" do
 
195
      Puppet::Util::ADSI::Group.uri("people").should == "WinNT://testcomputername/people,group"
 
196
    end
 
197
 
 
198
    it "should be able to create a group" do
 
199
      adsi_group = stub("adsi")
 
200
 
 
201
      connection.expects(:Create).with('group', groupname).returns(adsi_group)
 
202
 
 
203
      group = Puppet::Util::ADSI::Group.create(groupname)
 
204
 
 
205
      group.should be_a(Puppet::Util::ADSI::Group)
 
206
      group.native_group.should == adsi_group
 
207
    end
 
208
 
 
209
    it "should be able to confirm the existence of a group" do
 
210
      Puppet::Util::ADSI.expects(:connect).with("WinNT://testcomputername/#{groupname},group").returns connection
 
211
 
 
212
      Puppet::Util::ADSI::Group.exists?(groupname).should be_true
 
213
    end
 
214
 
 
215
    it "should be able to delete a group" do
 
216
      connection.expects(:Delete).with('group', groupname)
 
217
 
 
218
      Puppet::Util::ADSI::Group.delete(groupname)
 
219
    end
 
220
  end
 
221
end