diff options
author | tylercloke <tylercloke@gmail.com> | 2015-04-27 16:15:28 -0700 |
---|---|---|
committer | tylercloke <tylercloke@gmail.com> | 2015-04-28 13:20:01 -0700 |
commit | afa4b9e4034628bf5a4f337cf57cfa657df6ec3d (patch) | |
tree | 2a0f64e3f356c908aeca0793f90cf39cc7169c43 | |
parent | e60111f2773b60309db41581a98a66b5ed7cdad6 (diff) | |
download | chef-afa4b9e4034628bf5a4f337cf57cfa657df6ec3d.tar.gz |
Fixed bug in Chef::Key where create failed to return the full key.
Code assumed POST to keys endpoint returned the key body like other parts of the API.
Now, it simply generated the new key returned by create from the original key plus the private_key returned by the API if any.
Specs also updated.
-rw-r--r-- | lib/chef/key.rb | 6 | ||||
-rw-r--r-- | spec/unit/key_spec.rb | 42 |
2 files changed, 37 insertions, 11 deletions
diff --git a/lib/chef/key.rb b/lib/chef/key.rb index 1828713386..1ba0ab49c9 100644 --- a/lib/chef/key.rb +++ b/lib/chef/key.rb @@ -147,7 +147,11 @@ class Chef payload['public_key'] = @public_key unless @public_key.nil? payload['create_key'] = @create_key if @create_key payload['expiration_date'] = @expiration_date unless @expiration_date.nil? - new_key = chef_rest.post_rest("#{api_base}/#{@actor}/keys", payload) + result = chef_rest.post_rest("#{api_base}/#{@actor}/keys", payload) + # append the private key to the current key if the server returned one, + # since the POST endpoint just returns uri and private_key if needed. + new_key = self.to_hash + new_key["private_key"] = result["private_key"] if result["private_key"] Chef::Key.from_hash(new_key) end diff --git a/spec/unit/key_spec.rb b/spec/unit/key_spec.rb index 9f37b8aa12..a2bc6dcdd4 100644 --- a/spec/unit/key_spec.rb +++ b/spec/unit/key_spec.rb @@ -435,17 +435,37 @@ EOS end context "when create_key is true and public_key is nil" do + before do key.delete_public_key key.create_key true + $expected_output = { + actor_type => "foobar", + "name" => key.name, + "create_key" => true, + "expiration_date" => key.expiration_date + } + $expected_input = { + "name" => key.name, + "create_key" => true, + "expiration_date" => key.expiration_date + } end + it "should create a new key via the API" do - expect(rest).to receive(:post_rest).with(url, - {"name" => key.name, - "create_key" => true, - "expiration_date" => key.expiration_date}).and_return({}) + expect(rest).to receive(:post_rest).with(url, $expected_input).and_return({}) key.create end + + context "when the server returns the private_key via key.create" do + before do + allow(rest).to receive(:post_rest).with(url, $expected_input).and_return({"private_key" => "this_private_key"}) + end + + it "key.create returns the original key plus the private_key" do + expect(key.create.to_hash).to eq($expected_output.merge({"private_key" => "this_private_key"})) + end + end end context "when create_key is false and public_key is nil" do @@ -464,6 +484,7 @@ EOS it_should_behave_like "create key" do let(:url) { "users/#{key.actor}/keys" } let(:key) { user_key } + let(:actor_type) { "user" } end end @@ -471,6 +492,7 @@ EOS it_should_behave_like "create key" do let(:url) { "clients/#{client_key.actor}/keys" } let(:key) { client_key } + let(:actor_type) { "client" } end end end # create @@ -496,17 +518,17 @@ EOS end end - context "when creating a user key" do + context "when updating a user key" do it_should_behave_like "update key" do - let(:url) { "users/#{key.actor}/keys/#{key.name}" } - let(:key) { user_key } + let(:url) { "users/#{key.actor}/keys/#{key.name}" } + let(:key) { user_key } end end - context "when creating a client key" do + context "when updating a client key" do it_should_behave_like "update key" do - let(:url) { "clients/#{client_key.actor}/keys/#{key.name}" } - let(:key) { client_key } + let(:url) { "clients/#{client_key.actor}/keys/#{key.name}" } + let(:key) { client_key } end end |