# encoding: UTF-8
require 'spec_helper'

describe Suppliers::ProductCategoriesController, type: :controller do
  before :each do
    setup_supplier_for_controller
  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, format: :json
      assigns(:product_categories).should eq([product_category1])
    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, format: :json
      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, format: :json
      response.status.should == 404
    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, format: :json
        }.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
        post :create, product_category: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id)
        expect( ProductCategory.find_by_name('Trying to hack').supplier_id ).to eq @supplier.id
      end
    end

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

      it 'returns an error object' do
        post :create, format: :json, product_category: invalid_attributes, format: :json
        expect( JSON.parse(response.body)['errors'] ).to be_present
      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
        put :update, id: @product_category, product_category: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id)
        @product_category.reload
        expect( @product_category.name ).to eq 'Trying to hack'
        expect( @product_category.supplier_id ).to eq @supplier.id
      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

    context "with invalid attributes" do
      it "returns an error response" do
        put :update, id: @product_category, format: :json, product_category: invalid_attributes
        expect( JSON.parse( response.body )['errors']).to be_present
      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
