diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/support/shared/unit/api_versioning.rb | 44 | ||||
-rw-r--r-- | spec/support/shared/unit/user_and_client_shared.rb | 3 | ||||
-rw-r--r-- | spec/unit/api_client_spec.rb | 19 | ||||
-rw-r--r-- | spec/unit/user_spec.rb | 29 |
4 files changed, 94 insertions, 1 deletions
diff --git a/spec/support/shared/unit/api_versioning.rb b/spec/support/shared/unit/api_versioning.rb index ffa424a78d..61fdfe7faf 100644 --- a/spec/support/shared/unit/api_versioning.rb +++ b/spec/support/shared/unit/api_versioning.rb @@ -16,7 +16,12 @@ # limitations under the License. # +require "chef/exceptions" + shared_examples_for "version handling" do + let(:response_406) { OpenStruct.new(:code => '406') } + let(:exception_406) { Net::HTTPServerException.new("406 Not Acceptable", response_406) } + before do allow(rest_v1).to receive(http_verb).and_raise(exception_406) end @@ -31,3 +36,42 @@ shared_examples_for "version handling" do end end # when the server does not support the min or max server API version that Chef::User supports end # version handling + +shared_examples_for "user and client reregister" do + let(:response_406) { OpenStruct.new(:code => '406') } + let(:exception_406) { Net::HTTPServerException.new("406 Not Acceptable", response_406) } + let(:generic_exception) { Exception.new } + let(:min_version) { "2" } + let(:max_version) { "5" } + let(:return_hash_406) { + { + "min_version" => min_version, + "max_version" => max_version, + "request_version" => "30" + } + } + + context "when V0 is not supported by the server" do + context "when the exception is 406 and returns x-ops-server-api-version header" do + before do + allow(rest_v0).to receive(:put).and_raise(exception_406) + allow(response_406).to receive(:[]).with('x-ops-server-api-version').and_return(Chef::JSONCompat.to_json(return_hash_406)) + end + + it "raises an error about only V0 being supported" do + expect(object).to receive(:reregister_only_v0_supported_error_msg).with(max_version, min_version) + expect{ object.reregister }.to raise_error(Chef::Exceptions::OnlyApiVersion0SupportedForAction) + end + + end + context "when the exception is not versioning related" do + before do + allow(rest_v0).to receive(:put).and_raise(generic_exception) + end + + it "raises the original error" do + expect{ object.reregister }.to raise_error(generic_exception) + end + end + end +end diff --git a/spec/support/shared/unit/user_and_client_shared.rb b/spec/support/shared/unit/user_and_client_shared.rb index 36a7a7aeff..b0f3902922 100644 --- a/spec/support/shared/unit/user_and_client_shared.rb +++ b/spec/support/shared/unit/user_and_client_shared.rb @@ -111,4 +111,5 @@ shared_examples_for "user or client create" do end # when the server supports API V0 end # when server API V1 is not valid on the Chef Server receiving the request -end +end # user or client create + diff --git a/spec/unit/api_client_spec.rb b/spec/unit/api_client_spec.rb index b903c0e3c7..e1ffa9252a 100644 --- a/spec/unit/api_client_spec.rb +++ b/spec/unit/api_client_spec.rb @@ -490,5 +490,24 @@ describe Chef::ApiClient do end # when a valid client is defined end # update + # DEPRECATION + # This can be removed after API V0 support is gone + describe "reregister" do + context "when server API V0 is valid on the Chef Server receiving the request" do + it "creates a new object via the API" do + expect(@client.chef_rest_v0).to receive(:put).with("clients/#{@client.name}", payload.merge({:private_key => true})).and_return({}) + @client.reregister + end + end # when server API V0 is valid on the Chef Server receiving the request + + context "when server API V0 is not supported by the Chef Server" do + # from spec/support/shared/unit/api_versioning.rb + it_should_behave_like "user and client reregister" do + let(:object) { @client } + let(:rest_v0) { @client.chef_rest_v0 } + end + end # when server API V0 is not supported by the Chef Server + end # reregister + end end diff --git a/spec/unit/user_spec.rb b/spec/unit/user_spec.rb index d9872ef5a8..15d9591712 100644 --- a/spec/unit/user_spec.rb +++ b/spec/unit/user_spec.rb @@ -504,6 +504,35 @@ describe Chef::User do end # create + # DEPRECATION + # This can be removed after API V0 support is gone + describe "reregister" do + let(:payload) { + { + "username" => "some_username", + } + } + + before do + @user.username "some_username" + end + + context "when server API V0 is valid on the Chef Server receiving the request" do + it "creates a new object via the API" do + expect(@user.chef_root_rest_v0).to receive(:put).with("users/#{@user.username}", payload.merge({"private_key" => true})).and_return({}) + @user.reregister + end + end # when server API V0 is valid on the Chef Server receiving the request + + context "when server API V0 is not supported by the Chef Server" do + # from spec/support/shared/unit/api_versioning.rb + it_should_behave_like "user and client reregister" do + let(:object) { @user } + let(:rest_v0) { @user.chef_root_rest_v0 } + end + end # when server API V0 is not supported by the Chef Server + end # reregister + end # Versioned API Interactions describe "API Interactions" do |