# encoding: UTF-8 require 'spec_helper' describe Suppliers::ListsController, type: :controller do before :each do @supplier = Supplier.find_by_email('supplier@mozo.bar') || create(:supplier, :confirmed) ActionController::Parameters.permit_all_parameters = false #controller.stub(:list_params){ controller.params.require(:list).permit! } # allow all parameters since cross parameter injection is tested sign_in @supplier end def valid_attributes {state: 'active'} end def invalid_params {foo: 'bar'} end #after :each do #ActionController::Parameters.permit_all_parameters = true #end describe "GET #index" do it "has pagination options if show_all is given" do list = create :list, supplier: @supplier get :index, show_all: 'yes' #assigns(:lists).should respond_to :num_pages controller.instance_variable_get('@lists').should respond_to :num_pages end it "does not include lists from another supplier" do list1 = create :list, supplier: @supplier list2 = create :list get :index assigns(:lists).should eq([list1]) end it "should render without errors when no objects are present" do get :index expect{ render_template :index }.not_to raise_error end it "renders the :index view" do get :index response.should render_template :index 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.should render_template :show end end describe "GET #new" do it "assigns a new list to @list" do get :new assigns(:list).should be_a List end 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 list" do expect{ post :create, list: valid_attributes }.to change(List, :count).by(1) end it "redirects to the new list" do post :create, list: valid_attributes response.should redirect_to [:suppliers, List.last] end it "should not be possible to create a list for another supplier" do supplier2 = create :supplier expect{ post :create, list: valid_attributes.merge(price: '6.66', supplier_id: supplier2.id) }.to raise_error List.find_by_price(6.66).should_not be_present end end context "with invalid attributes" do it "does not save the new list" do expect{ post :create, list: invalid_params }.to raise_error List.count.should be_zero 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 "redirects to the updated list" do put :update, id: @list, list: valid_attributes response.should redirect_to [:suppliers, @list] end it "should not be possible to update a list to another supplier" do supplier2 = create :supplier expect{ put :update, id: @list, list: valid_attributes.merge(supplier_id: supplier2.id) }.to raise_error @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" do it "raises on invalid params" do expect{ put :update, id: @list, list: invalid_params }.to raise_error 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 "redirects to lists#index" do delete :destroy, id: @list response.should redirect_to [:suppliers, :lists] 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