chef 11.0.0 → 11.2.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/distro/common/html/chef-client.8.html +3 -3
  2. data/distro/common/html/chef-expander.8.html +3 -3
  3. data/distro/common/html/chef-expanderctl.8.html +3 -3
  4. data/distro/common/html/chef-server-webui.8.html +3 -3
  5. data/distro/common/html/chef-server.8.html +3 -3
  6. data/distro/common/html/chef-shell.1.html +7 -7
  7. data/distro/common/html/chef-solo.8.html +3 -3
  8. data/distro/common/html/chef-solr.8.html +3 -3
  9. data/distro/common/html/knife-bootstrap.1.html +3 -3
  10. data/distro/common/html/knife-client.1.html +4 -4
  11. data/distro/common/html/knife-configure.1.html +3 -3
  12. data/distro/common/html/knife-cookbook-site.1.html +6 -6
  13. data/distro/common/html/knife-cookbook.1.html +7 -7
  14. data/distro/common/html/knife-data-bag.1.html +3 -3
  15. data/distro/common/html/knife-environment.1.html +3 -3
  16. data/distro/common/html/knife-exec.1.html +4 -4
  17. data/distro/common/html/knife-index.1.html +4 -4
  18. data/distro/common/html/knife-node.1.html +4 -4
  19. data/distro/common/html/knife-role.1.html +6 -6
  20. data/distro/common/html/knife-search.1.html +5 -5
  21. data/distro/common/html/knife-ssh.1.html +3 -3
  22. data/distro/common/html/knife-status.1.html +4 -4
  23. data/distro/common/html/knife-tag.1.html +3 -3
  24. data/distro/common/html/knife.1.html +11 -11
  25. data/distro/common/man/man1/chef-shell.1 +1 -1
  26. data/distro/common/man/man1/knife-bootstrap.1 +1 -1
  27. data/distro/common/man/man1/knife-client.1 +1 -1
  28. data/distro/common/man/man1/knife-configure.1 +1 -1
  29. data/distro/common/man/man1/knife-cookbook-site.1 +1 -1
  30. data/distro/common/man/man1/knife-cookbook.1 +1 -1
  31. data/distro/common/man/man1/knife-data-bag.1 +1 -1
  32. data/distro/common/man/man1/knife-environment.1 +1 -1
  33. data/distro/common/man/man1/knife-exec.1 +1 -1
  34. data/distro/common/man/man1/knife-index.1 +1 -1
  35. data/distro/common/man/man1/knife-node.1 +1 -1
  36. data/distro/common/man/man1/knife-role.1 +1 -1
  37. data/distro/common/man/man1/knife-search.1 +1 -1
  38. data/distro/common/man/man1/knife-ssh.1 +1 -1
  39. data/distro/common/man/man1/knife-status.1 +1 -1
  40. data/distro/common/man/man1/knife-tag.1 +1 -1
  41. data/distro/common/man/man1/knife.1 +1 -1
  42. data/distro/common/man/man8/chef-client.8 +1 -1
  43. data/distro/common/man/man8/chef-expander.8 +1 -1
  44. data/distro/common/man/man8/chef-expanderctl.8 +1 -1
  45. data/distro/common/man/man8/chef-server-webui.8 +1 -1
  46. data/distro/common/man/man8/chef-server.8 +1 -1
  47. data/distro/common/man/man8/chef-solo.8 +1 -1
  48. data/distro/common/man/man8/chef-solr.8 +1 -1
  49. data/lib/chef/knife/configure.rb +16 -13
  50. data/lib/chef/knife/user_create.rb +93 -0
  51. data/lib/chef/knife/user_delete.rb +46 -0
  52. data/lib/chef/knife/user_edit.rb +53 -0
  53. data/lib/chef/knife/user_list.rb +42 -0
  54. data/lib/chef/knife/user_reregister.rb +59 -0
  55. data/lib/chef/knife/user_show.rb +52 -0
  56. data/lib/chef/user.rb +182 -0
  57. data/lib/chef/version.rb +1 -1
  58. data/spec/unit/knife/configure_spec.rb +23 -26
  59. data/spec/unit/knife/user_create_spec.rb +86 -0
  60. data/spec/unit/knife/user_delete_spec.rb +39 -0
  61. data/spec/unit/knife/user_edit_spec.rb +42 -0
  62. data/spec/unit/knife/user_list_spec.rb +32 -0
  63. data/spec/unit/knife/user_reregister_spec.rb +53 -0
  64. data/spec/unit/knife/user_show_spec.rb +41 -0
  65. data/spec/unit/user_spec.rb +255 -0
  66. metadata +1246 -1227
@@ -17,7 +17,7 @@
17
17
 
18
18
  class Chef
19
19
  CHEF_ROOT = File.dirname(File.expand_path(File.dirname(__FILE__)))
20
- VERSION = '11.0.0'
20
+ VERSION = '11.2.0.rc.1'
21
21
  end
22
22
 
23
23
  # NOTE: the Chef::Version class is defined in version_class.rb
@@ -41,7 +41,7 @@ describe Chef::Knife::Configure do
41
41
  @knife.config[:initial] = true
42
42
  Etc.stub!(:getlogin).and_return("a-new-user")
43
43
  @knife.ask_user_for_config
44
- @out.string.should match(Regexp.escape("Please enter a clientname for the new client: [a-new-user]"))
44
+ @out.string.should match(Regexp.escape("Please enter a name for the new user: [a-new-user]"))
45
45
  @knife.new_client_name.should == Etc.getlogin
46
46
  end
47
47
 
@@ -50,7 +50,7 @@ describe Chef::Knife::Configure do
50
50
  @knife.config[:node_name] = 'testnode'
51
51
  Etc.stub!(:getlogin).and_return("a-new-user")
52
52
  @knife.ask_user_for_config
53
- @out.string.should_not match(Regexp.escape("Please enter a clientname for the new client"))
53
+ @out.string.should_not match(Regexp.escape("Please enter a name for the new user"))
54
54
  @knife.new_client_name.should == 'testnode'
55
55
  end
56
56
 
@@ -64,32 +64,32 @@ describe Chef::Knife::Configure do
64
64
  it "asks the user for the existing admin client's name if -i is specified" do
65
65
  @knife.config[:initial] = true
66
66
  @knife.ask_user_for_config
67
- @out.string.should match(Regexp.escape("Please enter the existing admin clientname: [chef-webui]"))
68
- @knife.admin_client_name.should == 'chef-webui'
67
+ @out.string.should match(Regexp.escape("Please enter the existing admin name: [admin]"))
68
+ @knife.admin_client_name.should == 'admin'
69
69
  end
70
70
 
71
71
  it "should not ask the user for the existing admin client's name if -i and --admin-client_name are specified" do
72
72
  @knife.config[:initial] = true
73
73
  @knife.config[:admin_client_name] = 'my-webui'
74
74
  @knife.ask_user_for_config
75
- @out.string.should_not match(Regexp.escape("Please enter the existing admin clientname:"))
75
+ @out.string.should_not match(Regexp.escape("Please enter the existing admin:"))
76
76
  @knife.admin_client_name.should == 'my-webui'
77
77
  end
78
78
 
79
79
  it "should not ask the user for the existing admin client's name if -i is not specified" do
80
80
  @knife.ask_user_for_config
81
- @out.string.should_not match(Regexp.escape("Please enter the existing admin clientname: [chef-webui]"))
82
- @knife.admin_client_name.should_not == 'chef-webui'
81
+ @out.string.should_not match(Regexp.escape("Please enter the existing admin: [admin]"))
82
+ @knife.admin_client_name.should_not == 'admin'
83
83
  end
84
84
 
85
85
  it "asks the user for the location of the existing admin key if -i is specified" do
86
86
  @knife.config[:initial] = true
87
87
  @knife.ask_user_for_config
88
- @out.string.should match(Regexp.escape("Please enter the location of the existing admin client's private key: [/etc/chef/webui.pem]"))
88
+ @out.string.should match(Regexp.escape("Please enter the location of the existing admin's private key: [/etc/chef/admin.pem]"))
89
89
  if windows?
90
- @knife.admin_client_key.should == 'C:/etc/chef/webui.pem'
90
+ @knife.admin_client_key.should == 'C:/etc/chef/admin.pem'
91
91
  else
92
- @knife.admin_client_key.should == '/etc/chef/webui.pem'
92
+ @knife.admin_client_key.should == '/etc/chef/admin.pem'
93
93
  end
94
94
  end
95
95
 
@@ -111,7 +111,7 @@ describe Chef::Knife::Configure do
111
111
  if windows?
112
112
  @knife.admin_client_key.should_not == 'C:/etc//chef/webui.pem'
113
113
  else
114
- @knife.admin_client_key.should_not == '/etc/chef/webui.pem'
114
+ @knife.admin_client_key.should_not == '/etc/chef/webui.pem'
115
115
  end
116
116
  end
117
117
 
@@ -140,7 +140,7 @@ describe Chef::Knife::Configure do
140
140
  if windows?
141
141
  @knife.validation_key.should == 'C:/etc/chef/validation.pem'
142
142
  else
143
- @knife.validation_key.should == '/etc/chef/validation.pem'
143
+ @knife.validation_key.should == '/etc/chef/validation.pem'
144
144
  end
145
145
  end
146
146
 
@@ -208,28 +208,25 @@ describe Chef::Knife::Configure do
208
208
  File.should_receive(:expand_path).with("/home/you/.chef/knife.rb").and_return("/home/you/.chef/knife.rb")
209
209
  File.should_receive(:expand_path).with("/home/you/.chef/a-new-user.pem").and_return("/home/you/.chef/a-new-user.pem")
210
210
  File.should_receive(:expand_path).with("/etc/chef/validation.pem").and_return("/etc/chef/validation.pem")
211
- File.should_receive(:expand_path).with("/etc/chef/webui.pem").and_return("/etc/chef/webui.pem")
211
+ File.should_receive(:expand_path).with("/etc/chef/admin.pem").and_return("/etc/chef/admin.pem")
212
212
  Chef::Config[:node_name] = "webmonkey.example.com"
213
213
 
214
-
215
- client_command_config = {}
216
-
217
- client_command = mock("knife client create command", :config => client_command_config)
218
- client_command.should_receive(:name_args=).with(["a-new-user"])
219
- client_command.stub!(:name_args).and_return(["a-new-user"])
220
- client_command.should_receive(:run)
214
+ user_command = Chef::Knife::UserCreate.new
215
+ user_command.should_receive(:run)
221
216
 
222
217
  Etc.stub!(:getlogin).and_return("a-new-user")
223
218
 
224
- Chef::Knife::ClientCreate.stub!(:new).and_return(client_command)
219
+ Chef::Knife::UserCreate.stub!(:new).and_return(user_command)
225
220
  FileUtils.should_receive(:mkdir_p).with("/home/you/.chef")
226
221
  ::File.should_receive(:open).with("/home/you/.chef/knife.rb", "w")
227
222
  @knife.config[:initial] = true
223
+ @knife.config[:user_password] = "blah"
228
224
  @knife.run
229
- client_command.name_args.should == Array("a-new-user")
230
- client_command.config[:admin].should be_true
231
- client_command.config[:file].should == "/home/you/.chef/a-new-user.pem"
232
- client_command.config[:yes].should be_true
233
- client_command.config[:disable_editing].should be_true
225
+ user_command.name_args.should == Array("a-new-user")
226
+ user_command.config[:user_password].should == "blah"
227
+ user_command.config[:admin].should be_true
228
+ user_command.config[:file].should == "/home/you/.chef/a-new-user.pem"
229
+ user_command.config[:yes].should be_true
230
+ user_command.config[:disable_editing].should be_true
234
231
  end
235
232
  end
@@ -0,0 +1,86 @@
1
+ #
2
+ # Author:: Steven Danna (<steve@opscode.com>)
3
+ # Copyright:: Copyright (c) 2012 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+
21
+ Chef::Knife::UserCreate.load_deps
22
+
23
+ describe Chef::Knife::UserCreate do
24
+ before(:each) do
25
+ @knife = Chef::Knife::UserCreate.new
26
+ @knife.name_args = [ 'a_user' ]
27
+ @knife.config[:user_password] = "foobar"
28
+ @user = Chef::User.new
29
+ @user.name "a_user"
30
+ @user_with_private_key = Chef::User.new
31
+ @user_with_private_key.name "a_user"
32
+ @user_with_private_key.private_key 'private_key'
33
+ @user.stub!(:create).and_return(@user_with_private_key)
34
+ Chef::User.stub!(:new).and_return(@user)
35
+ Chef::User.stub!(:from_hash).and_return(@user)
36
+ @knife.stub!(:edit_data).and_return(@user.to_hash)
37
+ @stdout = StringIO.new
38
+ @stderr = StringIO.new
39
+ @knife.ui.stub!(:stdout).and_return(@stdout)
40
+ @knife.ui.stub!(:stderr).and_return(@stderr)
41
+ end
42
+
43
+ it "creates a new user" do
44
+ Chef::User.should_receive(:new).and_return(@user)
45
+ @user.should_receive(:create)
46
+ @knife.run
47
+ @stdout.string.should match /created user.+a_user/i
48
+ end
49
+
50
+ it "sets the password" do
51
+ @knife.config[:user_password] = "a_password"
52
+ @user.should_receive(:password).with("a_password")
53
+ @knife.run
54
+ end
55
+
56
+ it "exits with an error if password is blank" do
57
+ @knife.config[:user_password] = ''
58
+ lambda { @knife.run }.should raise_error SystemExit
59
+ @stderr.string.should match /You must specify a non-blank password/
60
+ end
61
+
62
+ it "sets the user name" do
63
+ @user.should_receive(:name).with("a_user")
64
+ @knife.run
65
+ end
66
+
67
+ it "sets the public key if given" do
68
+ @knife.config[:user_key] = "/a/filename"
69
+ File.stub(:read).with("/a/filename").and_return("a_key")
70
+ @user.should_receive(:public_key).with("a_key")
71
+ @knife.run
72
+ end
73
+
74
+ it "allows you to edit the data" do
75
+ @knife.should_receive(:edit_data).with(@user)
76
+ @knife.run
77
+ end
78
+
79
+ it "writes the private key to a file when --file is specified" do
80
+ @knife.config[:file] = "/tmp/a_file"
81
+ filehandle = mock("filehandle")
82
+ filehandle.should_receive(:print).with('private_key')
83
+ File.should_receive(:open).with("/tmp/a_file", "w").and_yield(filehandle)
84
+ @knife.run
85
+ end
86
+ end
@@ -0,0 +1,39 @@
1
+ #
2
+ # Author:: Steven Danna (<steve@opscode.com>)
3
+ # Copyright:: Copyright (c) 2012 Opscode, Inc
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+
21
+ describe Chef::Knife::UserDelete do
22
+ before(:each) do
23
+ Chef::Knife::UserDelete.load_deps
24
+ @knife = Chef::Knife::UserDelete.new
25
+ @knife.name_args = [ 'my_user' ]
26
+ end
27
+
28
+ it 'deletes the user' do
29
+ @knife.should_receive(:delete_object).with(Chef::User, 'my_user')
30
+ @knife.run
31
+ end
32
+
33
+ it 'prints usage and exits when a user name is not provided' do
34
+ @knife.name_args = []
35
+ @knife.should_receive(:show_usage)
36
+ @knife.ui.should_receive(:fatal)
37
+ lambda { @knife.run }.should raise_error(SystemExit)
38
+ end
39
+ end
@@ -0,0 +1,42 @@
1
+ #
2
+ # Author:: Steven Danna (<steve@opscode.com>)
3
+ # Copyright:: Copyright (c) 2012 Opscode, Inc
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+
21
+ describe Chef::Knife::UserEdit do
22
+ before(:each) do
23
+ Chef::Knife::UserEdit.load_deps
24
+ @knife = Chef::Knife::UserEdit.new
25
+ @knife.name_args = [ 'my_user' ]
26
+ @knife.config[:disable_editing] = true
27
+ end
28
+
29
+ it 'loads and edits the user' do
30
+ data = { :name => "my_user" }
31
+ Chef::User.stub(:load).with("my_user").and_return(data)
32
+ @knife.should_receive(:edit_data).with(data).and_return(data)
33
+ @knife.run
34
+ end
35
+
36
+ it 'prints usage and exits when a user name is not provided' do
37
+ @knife.name_args = []
38
+ @knife.should_receive(:show_usage)
39
+ @knife.ui.should_receive(:fatal)
40
+ lambda { @knife.run }.should raise_error(SystemExit)
41
+ end
42
+ end
@@ -0,0 +1,32 @@
1
+ #
2
+ # Author:: Steven Danna
3
+ # Copyright:: Copyright (c) 2012 Opscode, Inc
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+
21
+ describe Chef::Knife::UserList do
22
+ before(:each) do
23
+ Chef::Knife::UserList.load_deps
24
+ @knife = Chef::Knife::UserList.new
25
+ end
26
+
27
+ it 'lists the users' do
28
+ Chef::User.should_receive(:list)
29
+ @knife.should_receive(:format_list_for_display)
30
+ @knife.run
31
+ end
32
+ end
@@ -0,0 +1,53 @@
1
+ #
2
+ # Author:: Steven Danna (<steve@opscode.com>)
3
+ # Copyright:: Copyright (c) 2012 Opscode, Inc
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+
21
+ describe Chef::Knife::UserReregister do
22
+ before(:each) do
23
+ Chef::Knife::UserReregister.load_deps
24
+ @knife = Chef::Knife::UserReregister.new
25
+ @knife.name_args = [ 'a_user' ]
26
+ @user_mock = mock('user_mock', :private_key => "private_key")
27
+ Chef::User.stub!(:load).and_return(@user_mock)
28
+ @stdout = StringIO.new
29
+ @knife.ui.stub!(:stdout).and_return(@stdout)
30
+ end
31
+
32
+ it 'prints usage and exits when a user name is not provided' do
33
+ @knife.name_args = []
34
+ @knife.should_receive(:show_usage)
35
+ @knife.ui.should_receive(:fatal)
36
+ lambda { @knife.run }.should raise_error(SystemExit)
37
+ end
38
+
39
+ it 'reregisters the user and prints the key' do
40
+ @user_mock.should_receive(:reregister).and_return(@user_mock)
41
+ @knife.run
42
+ @stdout.string.should match( /private_key/ )
43
+ end
44
+
45
+ it 'writes the private key to a file when --file is specified' do
46
+ @user_mock.should_receive(:reregister).and_return(@user_mock)
47
+ @knife.config[:file] = '/tmp/a_file'
48
+ filehandle = StringIO.new
49
+ File.should_receive(:open).with('/tmp/a_file', 'w').and_yield(filehandle)
50
+ @knife.run
51
+ filehandle.string.should == "private_key"
52
+ end
53
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Author:: Steven Danna (<steve@opscode.com>)
3
+ # Copyright:: Copyright (c) 2012 Opscode, Inc
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+
21
+ describe Chef::Knife::UserShow do
22
+ before(:each) do
23
+ Chef::Knife::UserShow.load_deps
24
+ @knife = Chef::Knife::UserShow.new
25
+ @knife.name_args = [ 'my_user' ]
26
+ @user_mock = mock('user_mock')
27
+ end
28
+
29
+ it 'loads and displays the user' do
30
+ Chef::User.should_receive(:load).with('my_user').and_return(@user_mock)
31
+ @knife.should_receive(:format_for_display).with(@user_mock)
32
+ @knife.run
33
+ end
34
+
35
+ it 'prints usage and exits when a user name is not provided' do
36
+ @knife.name_args = []
37
+ @knife.should_receive(:show_usage)
38
+ @knife.ui.should_receive(:fatal)
39
+ lambda { @knife.run }.should raise_error(SystemExit)
40
+ end
41
+ end
@@ -0,0 +1,255 @@
1
+ #
2
+ # Author:: Steven Danna (steve@opscode.com)
3
+ # Copyright:: Copyright (c) 2012 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+
21
+ require 'chef/user'
22
+ require 'tempfile'
23
+
24
+ describe Chef::User do
25
+ before(:each) do
26
+ @user = Chef::User.new
27
+ end
28
+
29
+ describe "initialize" do
30
+ it "should be a Chef::User" do
31
+ @user.should be_a_kind_of(Chef::User)
32
+ end
33
+ end
34
+
35
+ describe "name" do
36
+ it "should let you set the name to a string" do
37
+ @user.name("ops_master").should == "ops_master"
38
+ end
39
+
40
+ it "should return the current name" do
41
+ @user.name "ops_master"
42
+ @user.name.should == "ops_master"
43
+ end
44
+
45
+ # It is not feasible to check all invalid characters. Here are a few
46
+ # that we probably care about.
47
+ it "should not accept invalid characters" do
48
+ # capital letters
49
+ lambda { @user.name "Bar" }.should raise_error(ArgumentError)
50
+ # slashes
51
+ lambda { @user.name "foo/bar" }.should raise_error(ArgumentError)
52
+ # ?
53
+ lambda { @user.name "foo?" }.should raise_error(ArgumentError)
54
+ # &
55
+ lambda { @user.name "foo&" }.should raise_error(ArgumentError)
56
+ end
57
+
58
+
59
+ it "should not accept spaces" do
60
+ lambda { @user.name "ops master" }.should raise_error(ArgumentError)
61
+ end
62
+
63
+ it "should throw an ArgumentError if you feed it anything but a string" do
64
+ lambda { @user.name Hash.new }.should raise_error(ArgumentError)
65
+ end
66
+ end
67
+
68
+ describe "admin" do
69
+ it "should let you set the admin bit" do
70
+ @user.admin(true).should == true
71
+ end
72
+
73
+ it "should return the current admin value" do
74
+ @user.admin true
75
+ @user.admin.should == true
76
+ end
77
+
78
+ it "should default to false" do
79
+ @user.admin.should == false
80
+ end
81
+
82
+ it "should throw an ArgumentError if you feed it anything but true or false" do
83
+ lambda { @user.name Hash.new }.should raise_error(ArgumentError)
84
+ end
85
+ end
86
+
87
+ describe "public_key" do
88
+ it "should let you set the public key" do
89
+ @user.public_key("super public").should == "super public"
90
+ end
91
+
92
+ it "should return the current public key" do
93
+ @user.public_key("super public")
94
+ @user.public_key.should == "super public"
95
+ end
96
+
97
+ it "should throw an ArgumentError if you feed it something lame" do
98
+ lambda { @user.public_key Hash.new }.should raise_error(ArgumentError)
99
+ end
100
+ end
101
+
102
+ describe "private_key" do
103
+ it "should let you set the private key" do
104
+ @user.private_key("super private").should == "super private"
105
+ end
106
+
107
+ it "should return the private key" do
108
+ @user.private_key("super private")
109
+ @user.private_key.should == "super private"
110
+ end
111
+
112
+ it "should throw an ArgumentError if you feed it something lame" do
113
+ lambda { @user.private_key Hash.new }.should raise_error(ArgumentError)
114
+ end
115
+ end
116
+
117
+ describe "when serializing to JSON" do
118
+ before(:each) do
119
+ @user.name("black")
120
+ @user.public_key("crowes")
121
+ @json = @user.to_json
122
+ end
123
+
124
+ it "serializes as a JSON object" do
125
+ @json.should match(/^\{.+\}$/)
126
+ end
127
+
128
+ it "includes the name value" do
129
+ @json.should include(%q{"name":"black"})
130
+ end
131
+
132
+ it "includes the public key value" do
133
+ @json.should include(%{"public_key":"crowes"})
134
+ end
135
+
136
+ it "includes the 'admin' flag" do
137
+ @json.should include(%q{"admin":false})
138
+ end
139
+
140
+ it "includes the private key when present" do
141
+ @user.private_key("monkeypants")
142
+ @user.to_json.should include(%q{"private_key":"monkeypants"})
143
+ end
144
+
145
+ it "does not include the private key if not present" do
146
+ @json.should_not include("private_key")
147
+ end
148
+
149
+ it "includes the password if present" do
150
+ @user.password "password"
151
+ @user.to_json.should include(%q{"password":"password"})
152
+ end
153
+
154
+ it "does not include the password if not present" do
155
+ @json.should_not include("password")
156
+ end
157
+ end
158
+
159
+ describe "when deserializing from JSON" do
160
+ before(:each) do
161
+ user = { "name" => "mr_spinks",
162
+ "public_key" => "turtles",
163
+ "private_key" => "pandas",
164
+ "password" => "password",
165
+ "admin" => true }
166
+ @user = Chef::User.from_json(user.to_json)
167
+ end
168
+
169
+ it "should deserialize to a Chef::User object" do
170
+ @user.should be_a_kind_of(Chef::User)
171
+ end
172
+
173
+ it "preserves the name" do
174
+ @user.name.should == "mr_spinks"
175
+ end
176
+
177
+ it "preserves the public key" do
178
+ @user.public_key.should == "turtles"
179
+ end
180
+
181
+ it "preserves the admin status" do
182
+ @user.admin.should be_true
183
+ end
184
+
185
+ it "includes the private key if present" do
186
+ @user.private_key.should == "pandas"
187
+ end
188
+
189
+ it "includes the password if present" do
190
+ @user.password.should == "password"
191
+ end
192
+
193
+ end
194
+
195
+ describe "API Interactions" do
196
+ before (:each) do
197
+ @user = Chef::User.new
198
+ @user.name "foobar"
199
+ @http_client = mock("Chef::REST mock")
200
+ Chef::REST.stub!(:new).and_return(@http_client)
201
+ end
202
+
203
+ describe "list" do
204
+ before(:each) do
205
+ Chef::Config[:chef_server_url] = "http://www.example.com"
206
+ @osc_response = { "admin" => "http://www.example.com/users/admin"}
207
+ @ohc_response = [ { "user" => { "username" => "admin" }} ]
208
+ end
209
+
210
+ it "lists all clients on an OSC server" do
211
+ @http_client.stub!(:get_rest).with("users").and_return(@osc_response)
212
+ Chef::User.list.should == @osc_response
213
+ end
214
+
215
+ it "lists all clients on an OHC/OPC server" do
216
+ @http_client.stub!(:get_rest).with("users").and_return(@ohc_response)
217
+ # We expect that Chef::User.list will give a consistent response
218
+ # so OHC API responses should be transformed to OSC-style output.
219
+ Chef::User.list.should == @osc_response
220
+ end
221
+ end
222
+
223
+ describe "create" do
224
+ it "creates a new user via the API" do
225
+ @user.password "password"
226
+ @http_client.should_receive(:post_rest).with("users", {:name => "foobar", :admin => false, :password => "password"}).and_return({})
227
+ @user.create
228
+ end
229
+ end
230
+
231
+ describe "read" do
232
+ it "loads a named user from the API" do
233
+ @http_client.should_receive(:get_rest).with("users/foobar").and_return({"name" => "foobar", "admin" => true, "public_key" => "pubkey"})
234
+ user = Chef::User.load("foobar")
235
+ user.name.should == "foobar"
236
+ user.admin.should == true
237
+ user.public_key.should == "pubkey"
238
+ end
239
+ end
240
+
241
+ describe "update" do
242
+ it "updates an existing user on via the API" do
243
+ @http_client.should_receive(:put_rest).with("users/foobar", {:name => "foobar", :admin => false}).and_return({})
244
+ @user.update
245
+ end
246
+ end
247
+
248
+ describe "destroy" do
249
+ it "deletes the specified user via the API" do
250
+ @http_client.should_receive(:delete_rest).with("users/foobar")
251
+ @user.destroy
252
+ end
253
+ end
254
+ end
255
+ end