# encoding: UTF-8
require 'spec_helper'

describe Suppliers::ProductsController, type: :controller do
  def valid_attributes
    {name: 'Lemon cake', price: '4.2'}
  end

  def invalid_attributes
    {name: ''}
  end

  before :each do
    setup_supplier_for_controller
  end

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

    it "should not display a product of another supplier" do
      product = create :product
      get :show, id: product
      expect(response.status).to eq 404
    end
  end

  describe "GET #new" do
    it "renders the #show view" do
      get :new
      response.should render_template :new
    end
  end

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

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

      it "should not be possible to create a product for another supplier" do
        supplier2 = create :supplier
        post :create, product: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id)
        expect( Product.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" do
        expect{
          post :create, product: invalid_attributes
        }.to_not change(Product, :count)
      end

      it 'returns an error object' do
        post :create, format: :json, product: invalid_attributes
        expect( JSON.parse(response.body)['errors'] ).to be_present
      end
    end
  end

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

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

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

      it "should not be possible to update a product to another supplier" do
        supplier2 = create :supplier
        put :update, id: @product, product: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id)
        expect( Product.find_by_name('Trying to hack' ).supplier_id ).to eq @supplier.id
      end

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

    context "with invalid attributes" do
      it "returns an error response" do
        put :update, id: @product, format: :json, product: invalid_attributes
        expect( JSON.parse( response.body )['errors']).to be_present
      end
    end
  end

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

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

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