<commit_before>require 'spec_helper'

describe InventoriesController do
  let(:user) { FactoryGirl.create(:user) }

  before :each do
    sign_in user
  end

  describe "POST create" do

    shared_examples "a valid inventory file" do
      it "creates a new inventory" do
        post :create, inventory: { csv_file: inventory_file }, :locale => "es"
        assigns(:inventory).should be_kind_of(Inventory)
        assigns(:inventory).should be_persisted
      end

      it "assigns the inventory to the current organization" do
        post :create, inventory: { csv_file: inventory_file }, :locale => "es"
        assigns(:inventory).organization.should == user.organization
      end

      it "redirects to new inventory page" do
        post :create, inventory: { csv_file: inventory_file }, :locale => "es"
        response.should render_template("new")
      end
    end

    context "UTF-8 file" do
      it_behaves_like "a valid inventory file" do
        let(:inventory_file) { fixture_file_upload("files/inventory.csv") }
      end
    end

    context "ISO 8859-1 file" do
      it_behaves_like "a valid inventory file" do
        let(:inventory_file) { fixture_file_upload("files/inventory-latin-1.csv") }
      end
    end

    context "file with invalid encoding" do
      before do
        def controller.create
          raise Exceptions::UnknownEncodingError
        end
      end

      it "redirects to inventories_path" do
        post :create, inventory: { csv_file: "" }, :locale => "es"
        expect(response).to redirect_to(inventories_path)
      end

      it "has 302 status" do
        post :create, inventory: { csv_file: "" }, :locale => "es"
        expect(response.status).to eq(302)
      end

      it "shows invalid encoding message" do
        post :create, inventory: { csv_file: "" }, :locale => "es"
        expect(flash[:alert]).to eq(I18n.t("activerecord.errors.models.inventory.attributes.csv_file.encoding"))
      end

      it "raises Exceptions::UnknownEncodingError" do
        bypass_rescue
        expect { post :create, inventory: { csv_file: "" }, :locale => "es" }.to raise_error(Exceptions::UnknownEncodingError)
      end
    end

    context "empty inventory file" do
      it "renders the template again on error" do
        post :create, inventory: { csv_file: "" }, :locale => "es"
        response.should render_template("new")
      end
    end
  end
end<commit_msg>Adding test cases for malformed csv files<commit_after>require 'spec_helper'

describe InventoriesController do
  let(:user) { FactoryGirl.create(:user) }

  before :each do
    sign_in user
  end

  describe "POST create" do

    shared_examples "a valid inventory file" do
      it "creates a new inventory" do
        post :create, inventory: { csv_file: inventory_file }, :locale => "es"
        assigns(:inventory).should be_kind_of(Inventory)
        assigns(:inventory).should be_persisted
      end

      it "assigns the inventory to the current organization" do
        post :create, inventory: { csv_file: inventory_file }, :locale => "es"
        assigns(:inventory).organization.should == user.organization
      end

      it "redirects to new inventory page" do
        post :create, inventory: { csv_file: inventory_file }, :locale => "es"
        response.should render_template("new")
      end
    end

    context "UTF-8 file" do
      it_behaves_like "a valid inventory file" do
        let(:inventory_file) { fixture_file_upload("files/inventory.csv") }
      end
    end

    context "ISO 8859-1 file" do
      it_behaves_like "a valid inventory file" do
        let(:inventory_file) { fixture_file_upload("files/inventory-latin-1.csv") }
      end
    end

    shared_examples "an invalid inventory file" do
      it "redirects to inventories_path" do
        post :create, inventory: { csv_file: "" }, :locale => "es"
        expect(response).to redirect_to(inventories_path)
      end

      it "has 302 status" do
        post :create, inventory: { csv_file: "" }, :locale => "es"
        expect(response.status).to eq(302)
      end

      it "shows invalid encoding message" do
        post :create, inventory: { csv_file: "" }, :locale => "es"
        expect(flash[:alert]).to eq(error_message)
      end

      it "raises Exceptions::UnknownEncodingError" do
        bypass_rescue
        expect { post :create, inventory: { csv_file: "" }, :locale => "es" }.to raise_error
      end
    end

    context "file with invalid encoding" do
      before do
        def controller.create
          raise Exceptions::UnknownEncodingError
        end
      end
      it_behaves_like "an invalid inventory file" do
        let(:error_message) { I18n.t("activerecord.errors.models.inventory.attributes.csv_file.encoding") }
      end
    end

    context "malformed csv file" do
      before do
        def controller.create
          raise CSV::MalformedCSVError
        end
      end
      it_behaves_like "an invalid inventory file" do
        let(:error_message) { I18n.t("activerecord.errors.models.inventory.attributes.csv_file.malformed") }
      end
    end

    context "empty inventory file" do
      it "renders the template again on error" do
        post :create, inventory: { csv_file: "" }, :locale => "es"
        response.should render_template("new")
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.0983177570093458, 'fraction_numerical': 0.005981308411214953, 'mean_word_length': 2.6928916494133883, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3946981', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1534, 'n_words': 454}