summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/api_client.rb7
-rw-r--r--spec/unit/api_client_spec.rb35
2 files changed, 23 insertions, 19 deletions
diff --git a/lib/chef/api_client.rb b/lib/chef/api_client.rb
index efb855b27a..b316e14f99 100644
--- a/lib/chef/api_client.rb
+++ b/lib/chef/api_client.rb
@@ -86,8 +86,8 @@ class Chef
)
end
- # The hash representation of the object. Includes the name and public_key,
- # but never the private key.
+ # The hash representation of the object. Includes the name and public_key.
+ # Private key is included if available.
#
# @return [Hash]
def to_hash
@@ -98,6 +98,7 @@ class Chef
'json_class' => self.class.name,
"chef_type" => "client"
}
+ result["private_key"] = @private_key if @private_key
result
end
@@ -111,7 +112,7 @@ class Chef
def self.json_create(o)
client = Chef::ApiClient.new
client.name(o["name"] || o["clientname"])
- client.private_key(o["private_key"])
+ client.private_key(o["private_key"]) if o.key?("private_key")
client.public_key(o["public_key"])
client.admin(o["admin"])
client
diff --git a/spec/unit/api_client_spec.rb b/spec/unit/api_client_spec.rb
index 6b41f1b211..4364bc1f16 100644
--- a/spec/unit/api_client_spec.rb
+++ b/spec/unit/api_client_spec.rb
@@ -100,33 +100,36 @@ describe Chef::ApiClient do
end
end
- describe "serialize" do
+ describe "when serializing to JSON" do
before(:each) do
@client.name("black")
@client.public_key("crowes")
- @client.private_key("monkeypants")
- @serial = @client.to_json
+ @json = @client.to_json
end
- it "should serialize to a json hash" do
- @client.to_json.should match(/^\{.+\}$/)
+ it "serializes as a JSON object" do
+ @json.should match(/^\{.+\}$/)
end
- %w{
- name
- public_key
- }.each do |t|
- it "should include '#{t}'" do
- @serial.should =~ /"#{t}":"#{@client.send(t.to_sym)}"/
- end
+ it "includes the name value" do
+ @json.should include(%q{"name":"black"})
+ end
+
+ it "includes the public key value" do
+ @json.should include(%{"public_key":"crowes"})
end
- it "should include 'admin'" do
- @serial.should =~ /"admin":false/
+ it "includes the 'admin' flag" do
+ @json.should include(%q{"admin":false})
+ end
+
+ it "includes the private key when present" do
+ @client.private_key("monkeypants")
+ @client.to_json.should include(%q{"private_key":"monkeypants"})
end
- it "should not include the private key" do
- @serial.should_not =~ /"private_key":/
+ it "does not include the private key if not present" do
+ @json.should_not include("private_key")
end
end