# encoding: UTF-8 require 'spec_helper' describe Suppliers::TablesController, type: :controller do before :each do @supplier = Supplier.find_by_email('supplier@mozo.bar') || create(:supplier, :confirmed) #controller.stub(:table_params){ controller.params.require(:table).permit! } # allow all parameters since cross parameter injection is tested sign_in @supplier end describe "GET #index" do it "populates an array of tables" do table = create :table, supplier: @supplier get :index assigns(:tables).should eq([table]) end it "does not include tables from another supplier" do table1 = create :table, supplier: @supplier table2 = create :table get :index assigns(:tables).should eq([table1]) end end describe "GET #show" do it "assigns the requested table to @table" do table = create :table, supplier: @supplier get :show, id: table, format: :json assigns(:table).should eq(table) end it "should not display a table of another supplier" do table = create :table get :show, id: table, format: :json response.status.should == 404 end end describe "POST #create" do context "with valid attributes" do it "creates a new table" do expect{ post :create, table: {number: 22} }.to change(Table, :count).by(1) end it "should not be possible to create a table for another supplier, linked to signed in supplier instead" do supplier2 = create :supplier expect { post :create, table: {number: 6, supplier_id: supplier2.id} }.to change{ Table.count }.by(1) created_table = Table.find_by_number(6) expect( created_table.supplier_id ).to eq @supplier.id end end end describe 'PUT update' do before :each do @table = create :table, supplier: @supplier end context "valid attributes" do it "located the requested table" do put :update, id: @table.id, table: {number: 22} @table.reload assigns(:table).should eq(@table) end it "changes @table's number attribute" do put :update, id: @table, table: {number: "14"} @table.reload @table.number.should eq(14) end it "should not be possible to update a table to another supplier" do supplier2 = create :supplier put :update, id: @table.id, table: {number: 6, supplier_id: supplier2.id} @table.reload expect( @table.supplier_id ).to eq @supplier.id end it "should not be possible to update a table of another supplier" do table = create :table, number: 11 put :update, id: table.id, table: {number: 6} table.reload table.number.should == 11 end end context "invalid attributes" do it "returns an error response" do #put :update, id: @table, format: :json, table: {number: 'aaa'} #TODO: when proper invalid tables exist expect( JSON.parse(response.body)['errors'] ).to be_present end end end describe 'DELETE destroy' do before :each do @table = create :table, supplier: @supplier end it "deletes the table" do expect{ delete :destroy, id: @table }.to change(Table, :count).by(-1) end it "should not be possible to delete a table of another supplier" do table = create :table expect{ delete :destroy, id: table }.to_not change(Table, :count) end end end