diff options
author | Steven Danna <steve@opscode.com> | 2013-01-28 06:49:57 -0800 |
---|---|---|
committer | Bryan McLellan <btm@opscode.com> | 2013-02-06 09:51:03 -0800 |
commit | 8f1acf35b0c7a6d365c88a42abbe8e6f4fd86998 (patch) | |
tree | db29fe2f39ad1a6307cb0dccdbf197b23334867f | |
parent | c0f6187e3e5c29a7b72679ba4f3158c6ba79d3b9 (diff) | |
download | chef-8f1acf35b0c7a6d365c88a42abbe8e6f4fd86998.tar.gz |
Add validation of password length to the user-create plugin.
Ideally this type of validation would be done somewhere in the model.
Currently our use of the set_or_return method doesn't easily allow for
these types of validations.
-rw-r--r-- | lib/chef/knife/user_create.rb | 6 | ||||
-rw-r--r-- | spec/unit/knife/user_create_spec.rb | 9 |
2 files changed, 15 insertions, 0 deletions
diff --git a/lib/chef/knife/user_create.rb b/lib/chef/knife/user_create.rb index 9aebee3b62..fa889f29ec 100644 --- a/lib/chef/knife/user_create.rb +++ b/lib/chef/knife/user_create.rb @@ -59,6 +59,12 @@ class Chef exit 1 end + if config[:user_password].length == 0 + show_usage + ui.fatal("You must specify a non-blank password") + exit 1 + end + user = Chef::User.new user.name(@user_name) user.admin(config[:admin]) diff --git a/spec/unit/knife/user_create_spec.rb b/spec/unit/knife/user_create_spec.rb index fd5d76acf8..5bc9738313 100644 --- a/spec/unit/knife/user_create_spec.rb +++ b/spec/unit/knife/user_create_spec.rb @@ -24,6 +24,7 @@ describe Chef::Knife::UserCreate do before(:each) do @knife = Chef::Knife::UserCreate.new @knife.name_args = [ 'a_user' ] + @knife.config[:user_password] = "foobar" @user = Chef::User.new @user.name "a_user" @user_with_private_key = Chef::User.new @@ -34,7 +35,9 @@ describe Chef::Knife::UserCreate do Chef::User.stub!(:from_hash).and_return(@user) @knife.stub!(:edit_data).and_return(@user.to_hash) @stdout = StringIO.new + @stderr = StringIO.new @knife.ui.stub!(:stdout).and_return(@stdout) + @knife.ui.stub!(:stderr).and_return(@stderr) end it "creates a new user" do @@ -50,6 +53,12 @@ describe Chef::Knife::UserCreate do @knife.run end + it "exits with an error if password is blank" do + @knife.config[:user_password] = '' + lambda { @knife.run }.should raise_error SystemExit + @stderr.string.should match /You must specify a non-blank password/ + end + it "sets the user name" do @user.should_receive(:name).with("a_user") @knife.run |