diff options
author | Steven Danna <steve@opscode.com> | 2015-01-22 15:53:15 +0000 |
---|---|---|
committer | Bryan McLellan <btm@opscode.com> | 2015-02-09 21:19:44 -0500 |
commit | 8ffa43844287691e2b88287cab6033b35b723b40 (patch) | |
tree | ae30c5235ced13749a51efcee54b99f3d01e7d56 | |
parent | 1a9681d4e65be2e194a5393d14102d696053bbff (diff) | |
download | chef-8ffa43844287691e2b88287cab6033b35b723b40.tar.gz |
Add ApiClient#from_hash and ApiClient#from_json
ApiClient#json_create confusingly takes a hash rather than a JSON
string. We need to preserve json_create for backwards compatibility.
from_hash is the same as json_create. from_json takes an actual
string of JSON.
-rw-r--r-- | lib/chef/api_client.rb | 23 | ||||
-rw-r--r-- | spec/unit/api_client_spec.rb | 76 |
2 files changed, 72 insertions, 27 deletions
diff --git a/lib/chef/api_client.rb b/lib/chef/api_client.rb index 334fb23f38..c27dfe82ba 100644 --- a/lib/chef/api_client.rb +++ b/lib/chef/api_client.rb @@ -124,14 +124,21 @@ class Chef Chef::JSONCompat.to_json(to_hash, *a) end - def self.json_create(o) - client = Chef::ApiClient.new - client.name(o["name"] || o["clientname"]) - client.private_key(o["private_key"]) if o.key?("private_key") - client.public_key(o["public_key"]) - client.admin(o["admin"]) - client.validator(o["validator"]) - client + class << self + def from_hash(o) + client = Chef::ApiClient.new + client.name(o["name"] || o["clientname"]) + client.private_key(o["private_key"]) if o.key?("private_key") + client.public_key(o["public_key"]) + client.admin(o["admin"]) + client.validator(o["validator"]) + client + end + alias :json_create :from_hash + + def from_json(j) + Chef::ApiClient.from_hash(Chef::JSONCompat.parse(j)) + end end def self.http_api diff --git a/spec/unit/api_client_spec.rb b/spec/unit/api_client_spec.rb index 7f8687e2b9..7668e31f5a 100644 --- a/spec/unit/api_client_spec.rb +++ b/spec/unit/api_client_spec.rb @@ -129,43 +129,83 @@ describe Chef::ApiClient do end end - describe "when deserializing from JSON" do - before(:each) do - client = { - "name" => "black", - "public_key" => "crowes", - "private_key" => "monkeypants", - "admin" => true, - "validator" => true, - "json_class" => "Chef::ApiClient" - } - @client = Chef::JSONCompat.from_json(Chef::JSONCompat.to_json(client)) + describe "when deserializing from JSON (string) using ApiClient#from_json" do + let(:client_string) do + "{\"name\":\"black\",\"public_key\":\"crowes\",\"private_key\":\"monkeypants\",\"admin\":true,\"validator\":true}" + end + + let(:client) do + Chef::ApiClient.from_json(client_string) + end + + it "does not require a 'json_class' string" do + expect(Chef::JSONCompat.parse(client_string)["json_class"]).to eq(nil) end it "should deserialize to a Chef::ApiClient object" do - expect(@client).to be_a_kind_of(Chef::ApiClient) + expect(client).to be_a_kind_of(Chef::ApiClient) end it "preserves the name" do - expect(@client.name).to eq("black") + expect(client.name).to eq("black") end it "preserves the public key" do - expect(@client.public_key).to eq("crowes") + expect(client.public_key).to eq("crowes") end it "preserves the admin status" do - expect(@client.admin).to be_truthy + expect(client.admin).to be_truthy end it "preserves the 'validator' status" do - expect(@client.validator).to be_truthy + expect(client.validator).to be_truthy end it "includes the private key if present" do - expect(@client.private_key).to eq("monkeypants") + expect(client.private_key).to eq("monkeypants") + end + end + + describe "when deserializing from JSON (hash) using JSONCompat#from_json" do + let(:client_hash) do + { + "name" => "black", + "public_key" => "crowes", + "private_key" => "monkeypants", + "admin" => true, + "validator" => true, + "json_class" => "Chef::ApiClient" + } end + let(:client) do + Chef::JSONCompat.from_json(Chef::JSONCompat.to_json(client_hash)) + end + + it "should deserialize to a Chef::ApiClient object" do + expect(client).to be_a_kind_of(Chef::ApiClient) + end + + it "preserves the name" do + expect(client.name).to eq("black") + end + + it "preserves the public key" do + expect(client.public_key).to eq("crowes") + end + + it "preserves the admin status" do + expect(client.admin).to be_truthy + end + + it "preserves the 'validator' status" do + expect(client.validator).to be_truthy + end + + it "includes the private key if present" do + expect(client.private_key).to eq("monkeypants") + end end describe "when loading from JSON" do @@ -306,5 +346,3 @@ describe Chef::ApiClient do end end end - - |