# encoding: UTF-8
require 'spec_helper'
describe Suppliers::ListsController, type: :controller do
  #before :all do
    #c = List
    #class List
      #attr_accessor :foo
      #validates :foo, format: {with: /\Aqqq\z/, if: ->(l){ l.foo.present? }}
    #end
  #end
  before :each do
    setup_supplier_for_controller
    ActionController::Parameters.permit_all_parameters = false
    #controller.stub(:list_params){ controller.params.require(:list).permit! } # allow all parameters since cross parameter injection is tested
  end

  def valid_attributes
    {state: 'active'}
  end

  def invalid_params
    {}
  end

  #after :each do
    #ActionController::Parameters.permit_all_parameters = true
  #end

  describe "GET #index" do
    it "does not include lists from another supplier" do
      list1 = create :list, supplier: @supplier
      list2 = create :list
      get :index, format: :json
      assigns(:lists).should eq([list1])
    end
  end

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

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

    it "renders the #show view" do
      list = create :list, supplier: @supplier
      get :show, id: list
      response.body.should include 'orders'
    end
  end

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

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

      it "should not be possible to create a list for another supplier" do
        supplier2 = create :supplier
        post :create, list: valid_attributes.merge(price: '6.66', supplier_id: supplier2.id)
        expect( List.find_by_price(6.66).supplier_id ).to eq @supplier.id
      end
    end

    context "with invalid attributes", broken: true do
      it "does not save the new list" do
        expect{
          post :create, format: :json, list: invalid_params
        }.not_to change{ List.count }
        List.count.should be_zero
      end

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

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

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

      it "changes @list's price attribute" do
        put :update, id: @list, list: valid_attributes.merge(price: '7.22')
        @list.reload
        @list.price.should eq(7.22)
      end

      it "should not be possible to update a list to another supplier" do
        supplier2 = create :supplier
        put :update, id: @list, list: valid_attributes.merge(supplier_id: supplier2.id)
        @list.reload
        @list.supplier_id.should == @supplier.id
      end

      it "should not be possible to update a list of another supplier" do
        list = create :list, price: '7.22'
        put :update, id: list, list: {price: '6.66'}
        list.reload
        list.price.should == 7.22
      end
    end

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

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

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

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