spec/unit/policyfile/uploader_spec.rb in chef-dk-0.3.5 vs spec/unit/policyfile/uploader_spec.rb in chef-dk-0.4.0

- old
+ new

@@ -50,12 +50,19 @@ let(:policy_group) { "unit-test" } let(:http_client) { instance_double("ChefDK::AuthenticatedHTTP") } - let(:uploader) { described_class.new(policyfile_lock, policy_group, http_client: http_client) } + let(:policy_document_native_api) { false } + let(:uploader) do + described_class.new(policyfile_lock, + policy_group, + http_client: http_client, + policy_document_native_api: policy_document_native_api) + end + let(:policyfile_as_data_bag_item) do policyfile_as_data_bag_item = { "id" => "example-unit-test", "name" => "data_bag_item_policyfiles_example-unit-test", @@ -77,11 +84,11 @@ it "has an HTTP client" do expect(uploader.http_client).to eq(http_client) end - describe "creating uploading documents in compat mode" do + describe "uploading documents in compat mode" do let(:cookbook_locks) { {} } let(:cookbook_versions) { {} } let(:existing_cookbook_on_remote) do @@ -139,38 +146,67 @@ cookbook_locks[name] = lock lock end - it "ensures a data bag named 'policyfiles' exists" do - expect(http_client).to receive(:post).with('data', {"name" => "policyfiles"}) - uploader.data_bag_create - end + context "when configured for policy document compat mode" do - it "does not error when the 'policyfiles' data bag exists" do - response = double("Net::HTTP response", code: "409") - error = Net::HTTPServerException.new("conflict", response) - expect(http_client).to receive(:post).with('data', {"name" => "policyfiles"}).and_raise(error) - expect { uploader.data_bag_create }.to_not raise_error - end + let(:policyfiles_data_bag) { {"name" => "policyfiles" } } - it "uploads the policyfile as a data bag item" do - response = double("Net::HTTP response", code: "404") - error = Net::HTTPServerException.new("Not Found", response) - expect(http_client).to receive(:put). - with('data/policyfiles/example-unit-test', policyfile_as_data_bag_item). - and_raise(error) - expect(http_client).to receive(:post). - with('data/policyfiles', policyfile_as_data_bag_item) + it "ensures a data bag named 'policyfiles' exists" do + expect(http_client).to receive(:post).with('data', policyfiles_data_bag) + uploader.data_bag_create + end - uploader.data_bag_item_create + it "does not error when the 'policyfiles' data bag exists" do + response = double("Net::HTTP response", code: "409") + error = Net::HTTPServerException.new("conflict", response) + expect(http_client).to receive(:post).with('data', {"name" => "policyfiles"}).and_raise(error) + expect { uploader.data_bag_create }.to_not raise_error + end + + it "uploads the policyfile as a data bag item" do + response = double("Net::HTTP response", code: "404") + error = Net::HTTPServerException.new("Not Found", response) + expect(http_client).to receive(:put). + with('data/policyfiles/example-unit-test', policyfile_as_data_bag_item). + and_raise(error) + expect(http_client).to receive(:post). + with('data/policyfiles', policyfile_as_data_bag_item) + + uploader.data_bag_item_create + end + + it "replaces an existing policyfile on the server if it exists" do + expect(http_client).to receive(:put). + with('data/policyfiles/example-unit-test', policyfile_as_data_bag_item) + uploader.data_bag_item_create + end + + it "creates the data bag and item to upload the policy" do + expect(http_client).to receive(:post).with('data', policyfiles_data_bag) + expect(http_client).to receive(:put). + with('data/policyfiles/example-unit-test', policyfile_as_data_bag_item) + uploader.upload_policy + end + end - it "replaces an existing policyfile on the server if it exists" do - expect(http_client).to receive(:put). - with('data/policyfiles/example-unit-test', policyfile_as_data_bag_item) + context "when configured for policy document native mode" do - uploader.data_bag_item_create + let(:policy_document_native_api) { true } + + it "enables native document mode for policyfiles" do + expect(uploader.using_policy_document_native_api?).to be(true) + end + + it "uploads the policyfile to the native API" do + expect(http_client).to receive(:put). + with('/policies/unit-test/example', policyfile_lock_data) + + uploader.upload_policy + end + end it "enumerates the cookbooks already on the server" do expect(http_client).to receive(:get).with('cookbooks?num_versions=all').and_return(existing_cookbook_on_remote) expect(uploader.existing_cookbook_on_remote).to eq(existing_cookbook_on_remote)