5
require 'puppet/util/adsi'
7
describe Puppet::Util::ADSI do
8
let(:connection) { stub 'connection' }
11
Puppet::Util::ADSI.instance_variable_set(:@computer_name, 'testcomputername')
12
Puppet::Util::ADSI.stubs(:connect).returns connection
16
Puppet::Util::ADSI.instance_variable_set(:@computer_name, nil)
19
it "should generate the correct URI for a resource" do
20
Puppet::Util::ADSI.uri('test', 'user').should == "WinNT://testcomputername/test,user"
23
it "should be able to get the name of the computer" do
24
Puppet::Util::ADSI.computer_name.should == 'testcomputername'
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"
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)
36
Puppet::Util::ADSI.sid_for_account('joe').should == 'S-1-1-50'
39
it "should return nil if the account does not exist" do
40
connection.expects(:execquery).returns([])
42
Puppet::Util::ADSI.sid_for_account('foobar').should be_nil
46
describe Puppet::Util::ADSI::User do
47
let(:username) { 'testuser' }
49
it "should generate the correct URI" do
50
Puppet::Util::ADSI::User.uri(username).should == "WinNT://testcomputername/#{username},user"
53
it "should be able to create a user" do
54
adsi_user = stub('adsi')
56
connection.expects(:Create).with('user', username).returns(adsi_user)
58
user = Puppet::Util::ADSI::User.create(username)
60
user.should be_a(Puppet::Util::ADSI::User)
61
user.native_user.should == adsi_user
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
69
it "should be able to delete a user" do
70
connection.expects(:Delete).with('user', username)
72
Puppet::Util::ADSI::User.delete(username)
75
describe "an instance" do
76
let(:adsi_user) { stub 'user' }
77
let(:user) { Puppet::Util::ADSI::User.new(username, adsi_user) }
79
it "should provide its groups as a list of names" do
80
names = ["group1", "group2"]
82
groups = names.map { |name| mock('group', :Name => name) }
84
adsi_user.expects(:Groups).returns(groups)
86
user.groups.should =~ names
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)
93
user.password_is?('pwdwrong').should be_false
94
user.password_is?('pwdright').should be_true
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
101
flagname = "UserFlags"
102
fADS_UF_DONT_EXPIRE_PASSWD = 0x10000
104
adsi_user.expects(:Get).with(flagname).returns(0)
105
adsi_user.expects(:Put).with(flagname, fADS_UF_DONT_EXPIRE_PASSWD)
107
user.password = 'pwd'
110
it "should generate the correct URI" do
111
user.uri.should == "WinNT://testcomputername/#{username},user"
114
describe "when given a set of groups to which to add the user" do
115
let(:groups_to_set) { 'group1,group2' }
118
user.expects(:groups).returns ['group2', 'group3']
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")
126
group3 = stub 'group1'
127
group3.expects(:Remove).with("WinNT://testcomputername/#{username},user")
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
132
user.set_groups(groups_to_set, false)
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")
141
Puppet::Util::ADSI.expects(:connect).with('WinNT://testcomputername/group1,group').returns group1
143
user.set_groups(groups_to_set, true)
150
describe Puppet::Util::ADSI::Group do
151
let(:groupname) { 'testgroup' }
153
describe "an instance" do
154
let(:adsi_group) { stub 'group' }
155
let(:group) { Puppet::Util::ADSI::Group.new(groupname, adsi_group) }
157
it "should be able to add a member" do
158
adsi_group.expects(:Add).with("WinNT://testcomputername/someone,user")
160
group.add_member('someone')
163
it "should be able to remove a member" do
164
adsi_group.expects(:Remove).with("WinNT://testcomputername/someone,user")
166
group.remove_member('someone')
169
it "should provide its groups as a list of names" do
170
names = ['user1', 'user2']
172
users = names.map { |name| mock('user', :Name => name) }
174
adsi_group.expects(:Members).returns(users)
176
group.members.should =~ names
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)}
183
adsi_group.expects(:Remove).with('WinNT://testcomputername/user1,user')
184
adsi_group.expects(:Add).with('WinNT://testcomputername/user3,user')
186
group.set_members(['user2', 'user3'])
189
it "should generate the correct URI" do
190
group.uri.should == "WinNT://testcomputername/#{groupname},group"
194
it "should generate the correct URI" do
195
Puppet::Util::ADSI::Group.uri("people").should == "WinNT://testcomputername/people,group"
198
it "should be able to create a group" do
199
adsi_group = stub("adsi")
201
connection.expects(:Create).with('group', groupname).returns(adsi_group)
203
group = Puppet::Util::ADSI::Group.create(groupname)
205
group.should be_a(Puppet::Util::ADSI::Group)
206
group.native_group.should == adsi_group
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
212
Puppet::Util::ADSI::Group.exists?(groupname).should be_true
215
it "should be able to delete a group" do
216
connection.expects(:Delete).with('group', groupname)
218
Puppet::Util::ADSI::Group.delete(groupname)