<commit_before># Copyright: Copyright (c) 2012 Opscode, Inc.
# License: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'pedant/rspec/cookbook_util'
require 'pedant/rspec/validations'

describe "Cookbooks API endpoint", :cookbooks do
  include Pedant::RSpec::CookbookUtil

  def self.ruby?
    Pedant::Config.ruby_cookbook_endpoint?
  end

  context "PUT /cookbooks/<name>/<version> [create]" do
    include Pedant::RSpec::Validations::Create
    let(:request_method){:PUT}
    let(:request_url){api_url("/cookbooks/#{cookbook_name}/#{cookbook_version}")}
    shared(:requestor){admin_user}

    let(:default_resource_attributes){ new_cookbook(cookbook_name, cookbook_version)}

    context 'with a basic cookbook', :smoke do
      after(:each) { delete_cookbook(admin_user, cookbook_name, cookbook_version) }

      let(:request_payload) { default_resource_attributes }
      let(:cookbook_name) { "pedant_basic" }
      let(:cookbook_version) { "1.0.0" }
      let(:created_resource) { default_resource_attributes }
      if ruby?
        it { should look_like http_200_response }
      else
        it { should look_like created_exact_response }
      end
    end

    # Start using the new validation macros
    context "when validating", :pending => ruby? do

      let(:cookbook_name) { "cookbook_name" }
      let(:cookbook_version) { "1.2.3" }

      let(:resource_url){api_url("/cookbooks/#{cookbook_name}/#{cookbook_version}")}
      let(:persisted_resource_response){ get(resource_url, requestor) }

      after(:each){ delete_cookbook(requestor, cookbook_name, cookbook_version)}

      context "the 'json_class' field" do
        let(:validate_attribute){"json_class"}
        accepts_valid_value "Chef::CookbookVersion"
        rejects_invalid_value "Chef::Node"
      end

      rejects_invalid_keys

    end

    context "creating broken cookbooks to test validation and defaults", :validation do
      let(:cookbook_name) { "cookbook_name" }
      let(:cookbook_version) { "1.2.3" }

      malformed_constraint = "s395dss@#"

      context "basic tests" do
        after(:each) do
          delete_cookbook(admin_user, cookbook_name, cookbook_version)
        end

        if (ruby?)
          should_fail_to_create('json_class', :delete, 400, "You didn't pass me a valid object!")
          create_should_crash_server('json_class', 'Chef::Role')
          should_fail_to_create('metadata', {}, 400,
                                "You said the cookbook was version 0.0.0, " +
                                 "but the URL says it should be 1.2.3.")
        else
          should_create('json_class', :delete, true, 'Chef::CookbookVersion')
          should_fail_to_create('json_class', 'Chef::Role', 400, "Field 'json_class' invalid")
          should_fail_to_create('metadata', {}, 400, "Field 'metadata.version' missing")
        end
      end # context basic tests

      context "checking segments" do
        %w{resources providers recipes definitions libraries attributes
           files templates root_files}.each do |segment|

          if (ruby?)
            create_should_crash_server(segment, "foo")
            should_fail_to_create(segment, [ {} ], 400,
                                  "Manifest has checksum  (path ) but " +
                                   "it hasn't yet been uploaded")
          else
            should_fail_to_create(segment, "foo", 400,
                                  "Field '#{segment}' invalid")
            should_fail_to_create(segment, [ {} ], 400,
                                  "Invalid element in array value of '#{segment}'.")
          end
        end
      end # context checking segments
<commit_msg>Change create broken cookbook tests to expect 400 instead of 500.<commit_after># Copyright: Copyright (c) 2012 Opscode, Inc.
# License: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'pedant/rspec/cookbook_util'
require 'pedant/rspec/validations'

describe "Cookbooks API endpoint", :cookbooks do
  include Pedant::RSpec::CookbookUtil

  def self.ruby?
    Pedant::Config.ruby_cookbook_endpoint?
  end

  context "PUT /cookbooks/<name>/<version> [create]" do
    include Pedant::RSpec::Validations::Create
    let(:request_method){:PUT}
    let(:request_url){api_url("/cookbooks/#{cookbook_name}/#{cookbook_version}")}
    shared(:requestor){admin_user}

    let(:default_resource_attributes){ new_cookbook(cookbook_name, cookbook_version)}

    context 'with a basic cookbook', :smoke do
      after(:each) { delete_cookbook(admin_user, cookbook_name, cookbook_version) }

      let(:request_payload) { default_resource_attributes }
      let(:cookbook_name) { "pedant_basic" }
      let(:cookbook_version) { "1.0.0" }
      let(:created_resource) { default_resource_attributes }
      if ruby?
        it { should look_like http_200_response }
      else
        it { should look_like created_exact_response }
      end
    end

    # Start using the new validation macros
    context "when validating", :pending => ruby? do

      let(:cookbook_name) { "cookbook_name" }
      let(:cookbook_version) { "1.2.3" }

      let(:resource_url){api_url("/cookbooks/#{cookbook_name}/#{cookbook_version}")}
      let(:persisted_resource_response){ get(resource_url, requestor) }

      after(:each){ delete_cookbook(requestor, cookbook_name, cookbook_version)}

      context "the 'json_class' field" do
        let(:validate_attribute){"json_class"}
        accepts_valid_value "Chef::CookbookVersion"
        rejects_invalid_value "Chef::Node"
      end

      rejects_invalid_keys

    end

    context "creating broken cookbooks to test validation and defaults", :validation do
      let(:cookbook_name) { "cookbook_name" }
      let(:cookbook_version) { "1.2.3" }

      malformed_constraint = "s395dss@#"

      context "basic tests" do
        after(:each) do
          delete_cookbook(admin_user, cookbook_name, cookbook_version)
        end

        if (ruby?)
          should_fail_to_create('json_class', :delete, 400, "You didn't pass me a valid object!")
          should_fail_to_create('json_class', 'Chef::Role', 400, "You didn't pass me a valid object!")
          should_fail_to_create('metadata', {}, 400,
                                "You said the cookbook was version 0.0.0, " +
                                 "but the URL says it should be 1.2.3.")
        else
          should_create('json_class', :delete, true, 'Chef::CookbookVersion')
          should_fail_to_create('json_class', 'Chef::Role', 400, "Field 'json_class' invalid")
          should_fail_to_create('metadata', {}, 400, "Field 'metadata.version' missing")
        end
      end # context basic tests

      context "checking segments" do
        %w{resources providers recipes definitions libraries attributes
           files templates root_files}.each do |segment|

          if (ruby?)
            should_fail_to_create(segment, "foo", 400,
                                  "Manifest has checksum  (path ) but " +
                                  "it hasn't yet been uploaded")
            should_fail_to_create(segment, [ {} ], 400,
                                  "Manifest has checksum  (path ) but " +
                                   "it hasn't yet been uploaded")
          else
            should_fail_to_create(segment, "foo", 400,
                                  "Field '#{segment}' invalid")
            should_fail_to_create(segment, [ {} ], 400,
                                  "Invalid element in array value of '#{segment}'.")
          end
        end
      end # context checking segments

{'fraction_non_alphanumeric': 0.10197482388266543, 'fraction_numerical': 0.01339646610463102, 'mean_word_length': 2.780008729812309, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2470805', 'n_tokens_mistral': 2570, 'n_tokens_neox': 2417, 'n_words': 669}