summaryrefslogtreecommitdiff
path: root/spec/unit/knife/user_create_spec.rb
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/knife/user_create_spec.rb
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/knife/user_create_spec.rb')
-rw-r--r--spec/unit/knife/user_create_spec.rb77
1 files changed, 77 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