summaryrefslogtreecommitdiff
path: root/spec/unit
diff options
context:
space:
mode:
authorSteven Danna <steve@opscode.com>2012-12-16 23:49:37 -0800
committerBryan McLellan <btm@opscode.com>2013-02-06 09:49:04 -0800
commitcd7178d0ed7b7eb1df1063ee03d2946ff7d482f1 (patch)
tree7db72f1d17ecf89f6a6339e53dadc035bf4c12fd /spec/unit
parent1afeec4c3275d2bdff87f2321c40f7968cf51fc6 (diff)
downloadchef-cd7178d0ed7b7eb1df1063ee03d2946ff7d482f1.tar.gz
Add user commands to knife.
This commit adds the following commands to knife: knife user list knife user show USERNAME knife user create USERNAME knife user delete USERNAME knife user reregister USERNAME Note that since Chef::User objects do not contain a json_class, the edit object method will not work as expecated.
Diffstat (limited to 'spec/unit')
-rw-r--r--spec/unit/knife/user_create_spec.rb77
-rw-r--r--spec/unit/knife/user_delete_spec.rb39
-rw-r--r--spec/unit/knife/user_edit_spec.rb42
-rw-r--r--spec/unit/knife/user_list_spec.rb32
-rw-r--r--spec/unit/knife/user_reregister_spec.rb53
-rw-r--r--spec/unit/knife/user_show_spec.rb41
6 files changed, 284 insertions, 0 deletions
diff --git a/spec/unit/knife/user_create_spec.rb b/spec/unit/knife/user_create_spec.rb
new file mode 100644
index 0000000000..fd5d76acf8
--- /dev/null
+++ b/spec/unit/knife/user_create_spec.rb
@@ -0,0 +1,77 @@
+#
+# Author:: Steven Danna (<steve@opscode.com>)
+# Copyright:: Copyright (c) 2012 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'spec_helper'
+
+Chef::Knife::UserCreate.load_deps
+
+describe Chef::Knife::UserCreate do
+ before(:each) do
+ @knife = Chef::Knife::UserCreate.new
+ @knife.name_args = [ 'a_user' ]
+ @user = Chef::User.new
+ @user.name "a_user"
+ @user_with_private_key = Chef::User.new
+ @user_with_private_key.name "a_user"
+ @user_with_private_key.private_key 'private_key'
+ @user.stub!(:create).and_return(@user_with_private_key)
+ Chef::User.stub!(:new).and_return(@user)
+ Chef::User.stub!(:from_hash).and_return(@user)
+ @knife.stub!(:edit_data).and_return(@user.to_hash)
+ @stdout = StringIO.new
+ @knife.ui.stub!(:stdout).and_return(@stdout)
+ end
+
+ it "creates a new user" do
+ Chef::User.should_receive(:new).and_return(@user)
+ @user.should_receive(:create)
+ @knife.run
+ @stdout.string.should match /created user.+a_user/i
+ end
+
+ it "sets the password" do
+ @knife.config[:user_password] = "a_password"
+ @user.should_receive(:password).with("a_password")
+ @knife.run
+ end
+
+ it "sets the user name" do
+ @user.should_receive(:name).with("a_user")
+ @knife.run
+ end
+
+ it "sets the public key if given" do
+ @knife.config[:user_key] = "/a/filename"
+ File.stub(:read).with("/a/filename").and_return("a_key")
+ @user.should_receive(:public_key).with("a_key")
+ @knife.run
+ end
+
+ it "allows you to edit the data" do
+ @knife.should_receive(:edit_data).with(@user)
+ @knife.run
+ end
+
+ it "writes the private key to a file when --file is specified" do
+ @knife.config[:file] = "/tmp/a_file"
+ filehandle = mock("filehandle")
+ filehandle.should_receive(:print).with('private_key')
+ File.should_receive(:open).with("/tmp/a_file", "w").and_yield(filehandle)
+ @knife.run
+ end
+end
diff --git a/spec/unit/knife/user_delete_spec.rb b/spec/unit/knife/user_delete_spec.rb
new file mode 100644
index 0000000000..be027e5128
--- /dev/null
+++ b/spec/unit/knife/user_delete_spec.rb
@@ -0,0 +1,39 @@
+#
+# Author:: Steven Danna (<steve@opscode.com>)
+# Copyright:: Copyright (c) 2012 Opscode, Inc
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'spec_helper'
+
+describe Chef::Knife::UserDelete do
+ before(:each) do
+ Chef::Knife::UserDelete.load_deps
+ @knife = Chef::Knife::UserDelete.new
+ @knife.name_args = [ 'my_user' ]
+ end
+
+ it 'deletes the user' do
+ @knife.should_receive(:delete_object).with(Chef::User, 'my_user')
+ @knife.run
+ end
+
+ it 'prints usage and exits when a user name is not provided' do
+ @knife.name_args = []
+ @knife.should_receive(:show_usage)
+ @knife.ui.should_receive(:fatal)
+ lambda { @knife.run }.should raise_error(SystemExit)
+ end
+end
diff --git a/spec/unit/knife/user_edit_spec.rb b/spec/unit/knife/user_edit_spec.rb
new file mode 100644
index 0000000000..d5b380a12f
--- /dev/null
+++ b/spec/unit/knife/user_edit_spec.rb
@@ -0,0 +1,42 @@
+#
+# Author:: Steven Danna (<steve@opscode.com>)
+# Copyright:: Copyright (c) 2012 Opscode, Inc
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'spec_helper'
+
+describe Chef::Knife::UserEdit do
+ before(:each) do
+ Chef::Knife::UserEdit.load_deps
+ @knife = Chef::Knife::UserEdit.new
+ @knife.name_args = [ 'my_user' ]
+ @knife.config[:disable_editing] = true
+ end
+
+ it 'loads and edits the user' do
+ data = { :name => "my_user" }
+ Chef::User.stub(:load).with("my_user").and_return(data)
+ @knife.should_receive(:edit_data).with(data).and_return(data)
+ @knife.run
+ end
+
+ it 'prints usage and exits when a user name is not provided' do
+ @knife.name_args = []
+ @knife.should_receive(:show_usage)
+ @knife.ui.should_receive(:fatal)
+ lambda { @knife.run }.should raise_error(SystemExit)
+ end
+end
diff --git a/spec/unit/knife/user_list_spec.rb b/spec/unit/knife/user_list_spec.rb
new file mode 100644
index 0000000000..7a47f9ddba
--- /dev/null
+++ b/spec/unit/knife/user_list_spec.rb
@@ -0,0 +1,32 @@
+#
+# Author:: Steven Danna
+# Copyright:: Copyright (c) 2012 Opscode, Inc
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'spec_helper'
+
+describe Chef::Knife::UserList do
+ before(:each) do
+ Chef::Knife::UserList.load_deps
+ @knife = Chef::Knife::UserList.new
+ end
+
+ it 'lists the users' do
+ Chef::User.should_receive(:list)
+ @knife.should_receive(:format_list_for_display)
+ @knife.run
+ end
+end
diff --git a/spec/unit/knife/user_reregister_spec.rb b/spec/unit/knife/user_reregister_spec.rb
new file mode 100644
index 0000000000..fddab57467
--- /dev/null
+++ b/spec/unit/knife/user_reregister_spec.rb
@@ -0,0 +1,53 @@
+#
+# Author:: Steven Danna (<steve@opscode.com>)
+# Copyright:: Copyright (c) 2012 Opscode, Inc
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'spec_helper'
+
+describe Chef::Knife::UserReregister do
+ before(:each) do
+ Chef::Knife::UserReregister.load_deps
+ @knife = Chef::Knife::UserReregister.new
+ @knife.name_args = [ 'a_user' ]
+ @user_mock = mock('user_mock', :private_key => "private_key")
+ Chef::User.stub!(:load).and_return(@user_mock)
+ @stdout = StringIO.new
+ @knife.ui.stub!(:stdout).and_return(@stdout)
+ end
+
+ it 'prints usage and exits when a user name is not provided' do
+ @knife.name_args = []
+ @knife.should_receive(:show_usage)
+ @knife.ui.should_receive(:fatal)
+ lambda { @knife.run }.should raise_error(SystemExit)
+ end
+
+ it 'reregisters the user and prints the key' do
+ @user_mock.should_receive(:reregister).and_return(@user_mock)
+ @knife.run
+ @stdout.string.should match( /private_key/ )
+ end
+
+ it 'writes the private key to a file when --file is specified' do
+ @user_mock.should_receive(:reregister).and_return(@user_mock)
+ @knife.config[:file] = '/tmp/a_file'
+ filehandle = StringIO.new
+ File.should_receive(:open).with('/tmp/a_file', 'w').and_yield(filehandle)
+ @knife.run
+ filehandle.string.should == "private_key"
+ end
+end
diff --git a/spec/unit/knife/user_show_spec.rb b/spec/unit/knife/user_show_spec.rb
new file mode 100644
index 0000000000..f2bd959d15
--- /dev/null
+++ b/spec/unit/knife/user_show_spec.rb
@@ -0,0 +1,41 @@
+#
+# Author:: Steven Danna (<steve@opscode.com>)
+# Copyright:: Copyright (c) 2012 Opscode, Inc
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'spec_helper'
+
+describe Chef::Knife::UserShow do
+ before(:each) do
+ Chef::Knife::UserShow.load_deps
+ @knife = Chef::Knife::UserShow.new
+ @knife.name_args = [ 'my_user' ]
+ @user_mock = mock('user_mock')
+ end
+
+ it 'loads and displays the user' do
+ Chef::User.should_receive(:load).with('my_user').and_return(@user_mock)
+ @knife.should_receive(:format_for_display).with(@user_mock)
+ @knife.run
+ end
+
+ it 'prints usage and exits when a user name is not provided' do
+ @knife.name_args = []
+ @knife.should_receive(:show_usage)
+ @knife.ui.should_receive(:fatal)
+ lambda { @knife.run }.should raise_error(SystemExit)
+ end
+end