diff options
author | tylercloke <tylercloke@gmail.com> | 2015-06-01 15:09:40 -0700 |
---|---|---|
committer | tylercloke <tylercloke@gmail.com> | 2015-06-05 10:38:49 -0700 |
commit | fb73d4ae3a0c20a3c0dfa5a85b652b8f0cba5a32 (patch) | |
tree | 55c4821657ad8a8ab106e767e3c52b543dc0b6c4 | |
parent | aa569ebb6fe0c6263bcb93fae80cc65ddfaff481 (diff) | |
download | chef-fb73d4ae3a0c20a3c0dfa5a85b652b8f0cba5a32.tar.gz |
Fixed bug where boolean fields would not be populated in Chef::User/ApiClient.
-rw-r--r-- | lib/chef/api_client.rb | 8 | ||||
-rw-r--r-- | lib/chef/user.rb | 43 | ||||
-rw-r--r-- | spec/unit/user_spec.rb | 11 |
3 files changed, 34 insertions, 28 deletions
diff --git a/lib/chef/api_client.rb b/lib/chef/api_client.rb index f3a83d7bcb..9e964081ec 100644 --- a/lib/chef/api_client.rb +++ b/lib/chef/api_client.rb @@ -141,9 +141,9 @@ class Chef 'json_class' => self.class.name, "chef_type" => "client" } - result["private_key"] = @private_key if @private_key - result["public_key"] = @public_key if @public_key - result["create_key"] = @create_key if @create_key + result["private_key"] = @private_key unless @private_key.nil? + result["public_key"] = @public_key unless @public_key.nil? + result["create_key"] = @create_key unless @create_key.nil? result end @@ -269,7 +269,7 @@ class Chef } begin # try API V1 - raise Chef::Exceptions::InvalidClientAttribute, "You cannot set both public_key and create_key for create." if create_key && public_key + raise Chef::Exceptions::InvalidClientAttribute, "You cannot set both public_key and create_key for create." if !create_key.nil? && !public_key.nil? payload[:public_key] = public_key unless public_key.nil? payload[:create_key] = create_key unless create_key.nil? diff --git a/lib/chef/user.rb b/lib/chef/user.rb index 9159d162f4..6758ed671d 100644 --- a/lib/chef/user.rb +++ b/lib/chef/user.rb @@ -116,14 +116,15 @@ class Chef result = { "username" => @username } - result["display_name"] = @display_name if @display_name - result["first_name"] = @first_name if @first_name - result["middle_name"] = @middle_name if @middle_name - result["last_name"] = @last_name if @last_name - result["email"] = @email if @email - result["password"] = @password if @password - result["public_key"] = @public_key if @public_key - result["private_key"] = @private_key if @private_key + result["display_name"] = @display_name unless @display_name.nil? + result["first_name"] = @first_name unless @first_name.nil? + result["middle_name"] = @middle_name unless @middle_name.nil? + result["last_name"] = @last_name unless @last_name.nil? + result["email"] = @email unless @email.nil? + result["password"] = @password unless @password.nil? + result["public_key"] = @public_key unless @public_key.nil? + result["private_key"] = @private_key unless @private_key.nil? + result["create_key"] = @create_key unless @create_key.nil? result end @@ -146,10 +147,10 @@ class Chef :email => @email, :password => @password } - payload[:public_key] = @public_key if @public_key - payload[:create_key] = @create_key if @create_key - payload[:middle_name] = @middle_name if @middle_name - raise Chef::Exceptions::InvalidUserAttribute, "You cannot set both public_key and create_key for create." if @create_key && @public_key + payload[:public_key] = @public_key unless @public_key.nil? + payload[:create_key] = @create_key unless @create_key.nil? + payload[:middle_name] = @middle_name unless @middle_name.nil? + raise Chef::Exceptions::InvalidUserAttribute, "You cannot set both public_key and create_key for create." if !@create_key.nil? && !@public_key.nil? new_user = chef_root_rest_v1.post("users", payload) # get the private_key out of the chef_key hash if it exists @@ -171,8 +172,8 @@ class Chef :email => @email, :password => @password } - payload[:middle_name] = @middle_name if @middle_name - payload[:public_key] = @public_key if @public_key + payload[:middle_name] = @middle_name unless @middle_name.nil? + payload[:public_key] = @public_key unless @public_key.nil? # under API V0, the server will create a key pair if public_key isn't passed new_user = chef_root_rest_v0.post("users", payload) end @@ -183,15 +184,15 @@ class Chef def update(new_key=false) begin payload = {:username => username} - payload[:display_name] = display_name if display_name - payload[:first_name] = first_name if first_name - payload[:middle_name] = middle_name if middle_name - payload[:last_name] = last_name if last_name - payload[:email] = email if email - payload[:password] = password if password + payload[:display_name] = display_name unless display_name.nil? + payload[:first_name] = first_name unless first_name.nil? + payload[:middle_name] = middle_name unless middle_name.nil? + payload[:last_name] = last_name unless last_name.nil? + payload[:email] = email unless email.nil? + payload[:password] = password unless password.nil? # API V1 will fail if these key fields are defined, and try V0 below if relevant 400 is returned - payload[:public_key] = public_key if public_key + payload[:public_key] = public_key unless public_key.nil? payload[:private_key] = new_key if new_key updated_user = chef_root_rest_v1.put("users/#{username}", payload) diff --git a/spec/unit/user_spec.rb b/spec/unit/user_spec.rb index 394378e86c..d9872ef5a8 100644 --- a/spec/unit/user_spec.rb +++ b/spec/unit/user_spec.rb @@ -51,6 +51,11 @@ describe Chef::User do expect(@user.send(method)).to eq(true) end + it "should return the false value when false" do + @user.send(method, false) + expect(@user.send(method)).to eq(false) + end + it "should throw an ArgumentError if you feed it anything but true or false" do expect { @user.send(method, Hash.new) }.to raise_error(ArgumentError) end @@ -256,7 +261,7 @@ describe Chef::User do "password" => "password", "public_key" => "turtles", "private_key" => "pandas", - "create_key" => true + "create_key" => false } @user = Chef::User.from_json(Chef::JSONCompat.to_json(user)) end @@ -301,8 +306,8 @@ describe Chef::User do expect(@user.private_key).to eq("pandas") end - it "includes the create key status if present" do - expect(@user.create_key).to be_truthy + it "includes the create key status if not nil" do + expect(@user.create_key).to be_falsey end end |