# encoding: UTF-8
require 'spec_helper'

describe Suppliers::ProductCategoriesController, type: :controller do
  before :each do
    @supplier = Supplier.find_by_email('supplier@mozo.bar') || create(:supplier, :confirmed)
    sign_in @supplier
  end

  def valid_attributes
    {name: 'Lunch'}
  end

  def invalid_attributes
    {name: ''}
  end

  describe "GET #index" do
    it "does not include product_categories from another supplier" do
      product_category1 = create :product_category, supplier: @supplier
      product_category2 = create :product_category
      get :index
      assigns(:product_categories).should eq([product_category1])
    end

    it "should render without errors when no objects are present" do
      get :index
      expect{ render_template :index }.not_to raise_error
    end
  end

  describe "GET #show" do
    it "assigns the requested product_category to @product_category" do
      product_category = create :product_category, supplier: @supplier
      get :show, id: product_category
      assigns(:product_category).should eq(product_category)
    end

    it "should not display a product_category of another supplier" do
      product_category = create :product_category
      get :show, id: product_category
      response.status.should == 404
    end
  end

  describe "GET #new" do
    it "assigns a new product_category to @product_category" do
      get :new
      assigns(:product_category).should be_a ProductCategory
    end
  end

  describe "POST #create" do
    context "with valid attributes" do
      it "creates a new product_category" do
        expect{
          post :create, product_category: valid_attributes
        }.to change(ProductCategory, :count).by(1)
      end

      it "redirects to the new product_category" do
        post :create, product_category: valid_attributes
        response.should redirect_to [:suppliers, :product_categories]
      end

      it "should not be possible to create a product category for another supplier" do
        supplier2 = create :supplier
        expect{ post :create, product_category: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id) }.to raise_error
        ProductCategory.find_by_name('Trying to hack').should_not be_present
      end
    end

    context "with invalid attributes" do
      it "does not save the new product_category" do
        expect{
          post :create, product_category: invalid_attributes
        }.not_to change{ ProductCategory.count }
      end
    end
  end

  describe 'PUT update' do
    before :each do
      @product_category = create :product_category, supplier: @supplier
    end

    context "valid attributes" do
      it "located the requested product_category" do
        put :update, id: @product_category, product_category: valid_attributes
        @product_category.reload
        assigns(:product_category).should eq(@product_category)
      end

      it "changes @product_category's attributes" do
        put :update, id: @product_category, product_category: valid_attributes.merge(name: "ChangedByTest")
        @product_category.reload
        @product_category.name.should eq("ChangedByTest")
      end

      it "should not be possible to update a product category to another supplier" do
        supplier2 = create :supplier
        expect{
          put :update, id: @product_category, product_category: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id)
        }.to raise_error
        ProductCategory.find_by_name('Trying to hack').should_not be_present
      end

      it "should not be possible to update a product_category of another supplier" do
        product_category = create :product_category, name: 'Other supplier product_category'
        put :update, id: product_category, product_category: {name: "Trying to hack"}
        expect(response.status).to eq 404
        product_category.reload
        product_category.name.should == 'Other supplier product_category'
      end
    end
  end

  describe 'DELETE destroy' do
    before :each do
      @product_category = create :product_category, supplier: @supplier
    end

    it "deletes the product_category" do
      expect{
        delete :destroy, id: @product_category
      }.to change(ProductCategory, :count).by(-1)
    end

    it "should not be possible to delete a product category of another supplier" do
      product_category = create :product_category
      expect{
        delete :destroy, id: product_category
      }.to_not change{ProductCategory.count }
    end
  end
end
