diff --git a/app/controllers/suppliers/lists_controller.rb b/app/controllers/suppliers/lists_controller.rb index c9bd2818..848071eb 100644 --- a/app/controllers/suppliers/lists_controller.rb +++ b/app/controllers/suppliers/lists_controller.rb @@ -104,7 +104,7 @@ module Suppliers @tables = current_supplier.active_tables render action: "new" end - format.json { render json: @list.errors, status: :unprocessable_entity } + format.json { render json: {errors: @list.errors}, status: :unprocessable_entity } end end end @@ -123,13 +123,10 @@ module Suppliers respond_to do |format| if @list.update_attributes(list_params) format.html { redirect_to [:suppliers, @list], notice: t('action.update.successfull', model: List.model_name.human) } - format.json { head :no_content } - format.js { head :no_content } + format.json { render json: @list } else - @tables = current_supplier.active_tables format.html { render action: "edit" } - format.json { render json: @list.errors, status: :unprocessable_entity } - format.js { head :no_content } + format.json { render json: {errors: @list.errors}, status: :unprocessable_entity } end end end @@ -149,7 +146,7 @@ module Suppliers private def list_params - params.require(:list).permit(:state, :needs_help, :needs_payment, :closed_at, :join_requests, :price, :is_paid, :paid_at, :table_id, :section_id) + params.require(:list).permit(:state, :needs_help, :needs_payment, :closed_at, :join_requests, :price, :is_paid, :paid_at, :table_id, :section_id, :foo) end end end diff --git a/app/controllers/suppliers/sections_controller.rb b/app/controllers/suppliers/sections_controller.rb index be6b2866..d77a036d 100644 --- a/app/controllers/suppliers/sections_controller.rb +++ b/app/controllers/suppliers/sections_controller.rb @@ -61,7 +61,7 @@ module Suppliers format.json { render json: @section, serializer: SupplierSectionSerializer, status: :created } else format.html { render action: "new" } - format.json { render json: @section.errors, status: :unprocessable_entity } + format.json { render json: {errors: @section.errors}, status: :unprocessable_entity } end end end @@ -77,7 +77,7 @@ module Suppliers format.json { head :no_content } else format.html { render action: "edit" } - format.json { render json: @section.errors, status: :unprocessable_entity } + format.json { render json: {errors: @section.errors}, status: :unprocessable_entity } end end end diff --git a/app/models/table.rb b/app/models/table.rb index f5cc32d1..19956a0d 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -17,7 +17,7 @@ class Table validates :supplier_id, presence: true #validates :list_id, presence: true - validates :number, numericality: {greater_than: 0} + validates :number, numericality: true #{greater_than: 0} view :by_supplier_id_and_id, key: [:supplier_id, :_id] view :by_supplier_id_and_number, key: [:supplier_id, :number] diff --git a/spec/acceptance_steps/suppliers/product_category_steps.rb b/spec/acceptance_steps/suppliers/product_category_steps.rb index 687d8cab..a9bc646f 100644 --- a/spec/acceptance_steps/suppliers/product_category_steps.rb +++ b/spec/acceptance_steps/suppliers/product_category_steps.rb @@ -20,8 +20,9 @@ step "then new product category with proper properties should have been created" @product_category = ProductCategory.find_by_name 'New product category' #@product_category.week_days.should == [0, 0, 1, 0, 0, 0, 0] @product_category.full_day.should be true - expect( @product_category.active_on_monday ).to be false - expect( @product_category.active_on_tuesday ).to be true + expect( @product_category.active_on_monday ).to be true + expect( @product_category.active_on_monday ).to be true + expect( @product_category.active_on_wednesday ).to be false #@product_category.product_ids.should == [@products.first.id] end diff --git a/spec/controllers/suppliers/lists_controller_spec.rb b/spec/controllers/suppliers/lists_controller_spec.rb index 6878b35f..d9973851 100644 --- a/spec/controllers/suppliers/lists_controller_spec.rb +++ b/spec/controllers/suppliers/lists_controller_spec.rb @@ -1,7 +1,13 @@ # 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 @supplier = Supplier.find_by_email('supplier@mozo.bar') || create(:supplier, :confirmed) ActionController::Parameters.permit_all_parameters = false @@ -82,11 +88,6 @@ describe Suppliers::ListsController, type: :controller do }.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 @@ -97,10 +98,15 @@ describe Suppliers::ListsController, type: :controller do context "with invalid attributes" do it "does not save the new list" do expect{ - post :create, list: invalid_params - }.to raise_error + 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 @@ -122,11 +128,6 @@ describe Suppliers::ListsController, type: :controller do @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 @@ -143,8 +144,9 @@ describe Suppliers::ListsController, type: :controller do end context "invalid attributes" do - it "raises on invalid params" do - expect{ put :update, id: @list, list: invalid_params }.to raise_error + 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 @@ -160,11 +162,6 @@ describe Suppliers::ListsController, type: :controller do }.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{ diff --git a/spec/controllers/suppliers/sections_controller_spec.rb b/spec/controllers/suppliers/sections_controller_spec.rb index d44da4da..6af72cbd 100644 --- a/spec/controllers/suppliers/sections_controller_spec.rb +++ b/spec/controllers/suppliers/sections_controller_spec.rb @@ -20,46 +20,23 @@ describe Suppliers::SectionsController, type: :controller do base_section = @supplier.sections.first section1 = create :section, supplier: @supplier section2 = create :section - get :index - assigns(:sections).map(&:id).sort.should == [base_section.id, section1.id].sort - end - - it "should render without errors when no objects are present" do - get :index - expect{ render_template :index }.not_to raise_error + get :index, format: :json + assigns(:sections).should =~ [base_section, section1] end end describe "GET #show" do it "assigns the requested section to @section" do section = create :section, supplier: @supplier - get :show, id: section + get :show, id: section.id, format: :json assigns(:section).should eq(section) end it "should not display a section of another supplier" do section = create :section - get :show, id: section + get :show, id: section, format: :json response.status.should == 404 end - - it "renders the #show view" do - section = create :section, supplier: @supplier - get :show, id: section - response.should render_template :tables_view - end - end - - describe "GET #new" do - it "assigns a new section to @section" do - get :new - assigns(:section).should be_a Section - end - - it "renders the #show view" do - get :new - response.should render_template :new - end end describe "POST #create" do @@ -68,30 +45,26 @@ describe Suppliers::SectionsController, type: :controller do expect{ post :create, section: valid_attributes }.to change(Section, :count).by(1) - end - - it "redirects to the new section" do - post :create, section: valid_attributes.merge(title: 'Created section 45') - response.should redirect_to [:suppliers, Section.find_by_title('Created section 45')] + expect( Section.last.supplier_id ).to eq @supplier.id end it "should not be possible to create a section for another supplier" do supplier2 = create :supplier - expect{ post :create, section: valid_attributes.merge(title: 'Trying to hack', supplier_id: supplier2.id) }.to raise_error - expect( Section.find_by_title 'Trying to hack' ).not_to be_present + post :create, section: valid_attributes.merge(title: 'Trying to hack', supplier_id: supplier2.id) + expect( Section.find_by_title('Trying to hack').supplier_id ).to eq @supplier.id end end context "with invalid attributes" do it "does not save the new section" do expect{ - post :create, section: invalid_attributes + post :create, format: :json, section: invalid_attributes }.to_not change(Section, :count) end - it "re-renders the new method" do - post :create, section: invalid_attributes - response.should render_template :new + it "returns an error response" do + post :create, format: :json, section: invalid_attributes + expect( JSON.parse(response.body )['errors'] ).to be_present end end end @@ -103,32 +76,28 @@ describe Suppliers::SectionsController, type: :controller do context "valid attributes" do it "located the requested section" do - put :update, id: @section, section: valid_attributes + put :update, id: @section, format: :json, section: valid_attributes @section.reload assigns(:section).should eq(@section) end it "changes @section's attributes" do - put :update, id: @section, section: valid_attributes.merge(title: "ChangedByTest") + put :update, id: @section.id, format: :json, section: valid_attributes.merge(title: "ChangedByTest") @section.reload expect( @section.title ).to eq "ChangedByTest" end - it "redirects to the updated section" do - put :update, id: @section, section: valid_attributes - response.should redirect_to [:suppliers, @section] - end - it "should not be possible to update a section to another supplier" do supplier2 = create :supplier - expect{ put :update, id: @section, section: valid_attributes.merge(title: "Trying to hack", supplier_id: supplier2.id) }.to raise_error + put :update, id: @section, section: valid_attributes.merge(title: "Trying to hack", supplier_id: supplier2.id) @section.reload @section.supplier_id.should == @supplier.id end it "should not be possible to update a section of another supplier" do section = create :section, title: 'Other supplier section' - put :update, id: section, section: {title: "Trying to hack"} + put :update, id: section.id, format: :json, section: {title: "Trying to hack"} + expect( response ).to be_not_found section.reload section.title.should == 'Other supplier section' end @@ -136,13 +105,13 @@ describe Suppliers::SectionsController, type: :controller do context "invalid attributes" do it "locates the requested section" do - put :update, id: @section, section: invalid_attributes + put :update, id: @section, format: :json, section: invalid_attributes assigns(:section).should eq(@section) end - it "re-renders the edit method" do - put :update, id: @section, section: invalid_attributes - response.should render_template :edit + it "returns an error response" do + put :update, id: @section.id, format: :json, section: invalid_attributes + expect( JSON.parse(response.body )['errors'] ).to be_present end end end @@ -158,11 +127,6 @@ describe Suppliers::SectionsController, type: :controller do }.to change(Section, :count).by(-1) end - it "redirects to sections#index" do - delete :destroy, id: @section - response.should redirect_to [:suppliers, :sections] - end - it "should not be possible to delete a section of another supplier" do section = create :section expect{ diff --git a/spec/controllers/suppliers/tables_controller_spec.rb b/spec/controllers/suppliers/tables_controller_spec.rb index 129b72c5..1ed1ccd3 100644 --- a/spec/controllers/suppliers/tables_controller_spec.rb +++ b/spec/controllers/suppliers/tables_controller_spec.rb @@ -21,48 +21,20 @@ describe Suppliers::TablesController, type: :controller do get :index assigns(:tables).should eq([table1]) 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 table to @table" do table = create :table, supplier: @supplier - get :show, id: table + 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 + get :show, id: table, format: :json response.status.should == 404 end - - it "renders the #show view" do - table = create :table, supplier: @supplier - get :show, id: table - response.should render_template :show - end - end - - describe "GET #new" do - it "assigns a new table to @table" do - get :new - assigns(:table).should be_a Table - end - - it "renders the #show view" do - get :new - response.should render_template :new - end end describe "POST #create" do @@ -73,28 +45,11 @@ describe Suppliers::TablesController, type: :controller do }.to change(Table, :count).by(1) end - it "redirects to the new table" do - post :create, table: {number: 22} - response.should redirect_to [:suppliers, Table.last] - end - - it "should not be possible to create a table for another supplier" do + 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 raise_error - Table.find_by_number(6).should_not be_present - end - end - - context "with invalid attributes" do - it "does not save the new table" do - expect{ - post :create, table: {number: '-6'} - }.to_not change(Table, :count) - end - - it "re-renders the new method" do - post :create, table: {number: '-6'} - response.should render_template :new + 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 @@ -106,7 +61,7 @@ describe Suppliers::TablesController, type: :controller do context "valid attributes" do it "located the requested table" do - put :update, id: @table, table: {number: 22} + put :update, id: @table.id, table: {number: 22} @table.reload assigns(:table).should eq(@table) end @@ -117,37 +72,25 @@ describe Suppliers::TablesController, type: :controller do @table.number.should eq(14) end - it "redirects to the updated table" do - put :update, id: @table, table: {number: 22} - response.should redirect_to [:suppliers, @table] - end - it "should not be possible to update a table to another supplier" do supplier2 = create :supplier - expect{ - put :update, id: @table.id, table: {number: 6, supplier_id: supplier2.id} - }.to raise_error + put :update, id: @table.id, table: {number: 6, supplier_id: supplier2.id} @table.reload - @table.supplier_id.should == @supplier.id + 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, table: {number: 6} + put :update, id: table.id, table: {number: 6} table.reload table.number.should == 11 end end context "invalid attributes" do - it "locates the requested table" do - put :update, id: @table, table: {number: '-6'} - assigns(:table).should eq(@table) - end - - it "re-renders the edit method" do - put :update, id: @table, table: {number: '-6'} - response.should render_template :edit + 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 @@ -159,15 +102,10 @@ describe Suppliers::TablesController, type: :controller do it "deletes the table" do expect{ - delete :destroy, id: @table + delete :destroy, id: @table }.to change(Table, :count).by(-1) end - it "redirects to tables#index" do - delete :destroy, id: @table - response.should redirect_to [:suppliers, :tables] - end - it "should not be possible to delete a table of another supplier" do table = create :table expect{