Controller spec api improvements

This commit is contained in:
2014-12-02 19:24:09 +01:00
parent 60e6b2a648
commit a0774caeff
7 changed files with 61 additions and 164 deletions
@@ -104,7 +104,7 @@ module Suppliers
@tables = current_supplier.active_tables @tables = current_supplier.active_tables
render action: "new" render action: "new"
end end
format.json { render json: @list.errors, status: :unprocessable_entity } format.json { render json: {errors: @list.errors}, status: :unprocessable_entity }
end end
end end
end end
@@ -123,13 +123,10 @@ module Suppliers
respond_to do |format| respond_to do |format|
if @list.update_attributes(list_params) if @list.update_attributes(list_params)
format.html { redirect_to [:suppliers, @list], notice: t('action.update.successfull', model: List.model_name.human) } format.html { redirect_to [:suppliers, @list], notice: t('action.update.successfull', model: List.model_name.human) }
format.json { head :no_content } format.json { render json: @list }
format.js { head :no_content }
else else
@tables = current_supplier.active_tables
format.html { render action: "edit" } format.html { render action: "edit" }
format.json { render json: @list.errors, status: :unprocessable_entity } format.json { render json: {errors: @list.errors}, status: :unprocessable_entity }
format.js { head :no_content }
end end
end end
end end
@@ -149,7 +146,7 @@ module Suppliers
private private
def list_params 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 end
end end
@@ -61,7 +61,7 @@ module Suppliers
format.json { render json: @section, serializer: SupplierSectionSerializer, status: :created } format.json { render json: @section, serializer: SupplierSectionSerializer, status: :created }
else else
format.html { render action: "new" } 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 end
end end
@@ -77,7 +77,7 @@ module Suppliers
format.json { head :no_content } format.json { head :no_content }
else else
format.html { render action: "edit" } 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 end
end end
+1 -1
View File
@@ -17,7 +17,7 @@ class Table
validates :supplier_id, presence: true validates :supplier_id, presence: true
#validates :list_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_id, key: [:supplier_id, :_id]
view :by_supplier_id_and_number, key: [:supplier_id, :number] view :by_supplier_id_and_number, key: [:supplier_id, :number]
@@ -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 = ProductCategory.find_by_name 'New product category'
#@product_category.week_days.should == [0, 0, 1, 0, 0, 0, 0] #@product_category.week_days.should == [0, 0, 1, 0, 0, 0, 0]
@product_category.full_day.should be true @product_category.full_day.should be true
expect( @product_category.active_on_monday ).to be false expect( @product_category.active_on_monday ).to be true
expect( @product_category.active_on_tuesday ).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] #@product_category.product_ids.should == [@products.first.id]
end end
@@ -1,7 +1,13 @@
# encoding: UTF-8 # encoding: UTF-8
require 'spec_helper' require 'spec_helper'
describe Suppliers::ListsController, type: :controller do 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 before :each do
@supplier = Supplier.find_by_email('supplier@mozo.bar') || create(:supplier, :confirmed) @supplier = Supplier.find_by_email('supplier@mozo.bar') || create(:supplier, :confirmed)
ActionController::Parameters.permit_all_parameters = false ActionController::Parameters.permit_all_parameters = false
@@ -82,11 +88,6 @@ describe Suppliers::ListsController, type: :controller do
}.to change(List, :count).by(1) }.to change(List, :count).by(1)
end 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 it "should not be possible to create a list for another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
expect{ post :create, list: valid_attributes.merge(price: '6.66', supplier_id: supplier2.id) }.to raise_error 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 context "with invalid attributes" do
it "does not save the new list" do it "does not save the new list" do
expect{ expect{
post :create, list: invalid_params post :create, format: :json, list: invalid_params
}.to raise_error }.not_to change{ List.count }
List.count.should be_zero List.count.should be_zero
end 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
end end
@@ -122,11 +128,6 @@ describe Suppliers::ListsController, type: :controller do
@list.price.should eq(7.22) @list.price.should eq(7.22)
end 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 it "should not be possible to update a list to another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
expect{ put :update, id: @list, list: valid_attributes.merge(supplier_id: supplier2.id) }.to raise_error 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 end
context "invalid attributes" do context "invalid attributes" do
it "raises on invalid params" do it "returns an error response" do
expect{ put :update, id: @list, list: invalid_params }.to raise_error put :update, id: @list, format: :json, list: invalid_params
expect( JSON.parse( response.body )['errors']).to be_present
end end
end end
end end
@@ -160,11 +162,6 @@ describe Suppliers::ListsController, type: :controller do
}.to change(List, :count).by(-1) }.to change(List, :count).by(-1)
end 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 it "should not be possible to delete a list of another supplier" do
list = create :list list = create :list
expect{ expect{
@@ -20,46 +20,23 @@ describe Suppliers::SectionsController, type: :controller do
base_section = @supplier.sections.first base_section = @supplier.sections.first
section1 = create :section, supplier: @supplier section1 = create :section, supplier: @supplier
section2 = create :section section2 = create :section
get :index get :index, format: :json
assigns(:sections).map(&:id).sort.should == [base_section.id, section1.id].sort assigns(:sections).should =~ [base_section, section1]
end
it "should render without errors when no objects are present" do
get :index
expect{ render_template :index }.not_to raise_error
end end
end end
describe "GET #show" do describe "GET #show" do
it "assigns the requested section to @section" do it "assigns the requested section to @section" do
section = create :section, supplier: @supplier section = create :section, supplier: @supplier
get :show, id: section get :show, id: section.id, format: :json
assigns(:section).should eq(section) assigns(:section).should eq(section)
end end
it "should not display a section of another supplier" do it "should not display a section of another supplier" do
section = create :section section = create :section
get :show, id: section get :show, id: section, format: :json
response.status.should == 404 response.status.should == 404
end 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 end
describe "POST #create" do describe "POST #create" do
@@ -68,30 +45,26 @@ describe Suppliers::SectionsController, type: :controller do
expect{ expect{
post :create, section: valid_attributes post :create, section: valid_attributes
}.to change(Section, :count).by(1) }.to change(Section, :count).by(1)
end expect( Section.last.supplier_id ).to eq @supplier.id
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')]
end end
it "should not be possible to create a section for another supplier" do it "should not be possible to create a section for another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
expect{ post :create, section: valid_attributes.merge(title: 'Trying to hack', supplier_id: supplier2.id) }.to raise_error post :create, section: valid_attributes.merge(title: 'Trying to hack', supplier_id: supplier2.id)
expect( Section.find_by_title 'Trying to hack' ).not_to be_present expect( Section.find_by_title('Trying to hack').supplier_id ).to eq @supplier.id
end end
end end
context "with invalid attributes" do context "with invalid attributes" do
it "does not save the new section" do it "does not save the new section" do
expect{ expect{
post :create, section: invalid_attributes post :create, format: :json, section: invalid_attributes
}.to_not change(Section, :count) }.to_not change(Section, :count)
end end
it "re-renders the new method" do it "returns an error response" do
post :create, section: invalid_attributes post :create, format: :json, section: invalid_attributes
response.should render_template :new expect( JSON.parse(response.body )['errors'] ).to be_present
end end
end end
end end
@@ -103,32 +76,28 @@ describe Suppliers::SectionsController, type: :controller do
context "valid attributes" do context "valid attributes" do
it "located the requested section" 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 @section.reload
assigns(:section).should eq(@section) assigns(:section).should eq(@section)
end end
it "changes @section's attributes" do 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 @section.reload
expect( @section.title ).to eq "ChangedByTest" expect( @section.title ).to eq "ChangedByTest"
end 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 it "should not be possible to update a section to another supplier" do
supplier2 = create :supplier 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.reload
@section.supplier_id.should == @supplier.id @section.supplier_id.should == @supplier.id
end end
it "should not be possible to update a section of another supplier" do it "should not be possible to update a section of another supplier" do
section = create :section, title: 'Other supplier section' 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.reload
section.title.should == 'Other supplier section' section.title.should == 'Other supplier section'
end end
@@ -136,13 +105,13 @@ describe Suppliers::SectionsController, type: :controller do
context "invalid attributes" do context "invalid attributes" do
it "locates the requested section" 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) assigns(:section).should eq(@section)
end end
it "re-renders the edit method" do it "returns an error response" do
put :update, id: @section, section: invalid_attributes put :update, id: @section.id, format: :json, section: invalid_attributes
response.should render_template :edit expect( JSON.parse(response.body )['errors'] ).to be_present
end end
end end
end end
@@ -158,11 +127,6 @@ describe Suppliers::SectionsController, type: :controller do
}.to change(Section, :count).by(-1) }.to change(Section, :count).by(-1)
end 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 it "should not be possible to delete a section of another supplier" do
section = create :section section = create :section
expect{ expect{
@@ -21,48 +21,20 @@ describe Suppliers::TablesController, type: :controller do
get :index get :index
assigns(:tables).should eq([table1]) assigns(:tables).should eq([table1])
end 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 end
describe "GET #show" do describe "GET #show" do
it "assigns the requested table to @table" do it "assigns the requested table to @table" do
table = create :table, supplier: @supplier table = create :table, supplier: @supplier
get :show, id: table get :show, id: table, format: :json
assigns(:table).should eq(table) assigns(:table).should eq(table)
end end
it "should not display a table of another supplier" do it "should not display a table of another supplier" do
table = create :table table = create :table
get :show, id: table get :show, id: table, format: :json
response.status.should == 404 response.status.should == 404
end 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 end
describe "POST #create" do describe "POST #create" do
@@ -73,28 +45,11 @@ describe Suppliers::TablesController, type: :controller do
}.to change(Table, :count).by(1) }.to change(Table, :count).by(1)
end end
it "redirects to the new table" do it "should not be possible to create a table for another supplier, linked to signed in supplier instead" 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
supplier2 = create :supplier supplier2 = create :supplier
expect { post :create, table: {number: 6, supplier_id: supplier2.id} }.to raise_error expect { post :create, table: {number: 6, supplier_id: supplier2.id} }.to change{ Table.count }.by(1)
Table.find_by_number(6).should_not be_present created_table = Table.find_by_number(6)
end expect( created_table.supplier_id ).to eq @supplier.id
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
end end
end end
end end
@@ -106,7 +61,7 @@ describe Suppliers::TablesController, type: :controller do
context "valid attributes" do context "valid attributes" do
it "located the requested table" do it "located the requested table" do
put :update, id: @table, table: {number: 22} put :update, id: @table.id, table: {number: 22}
@table.reload @table.reload
assigns(:table).should eq(@table) assigns(:table).should eq(@table)
end end
@@ -117,37 +72,25 @@ describe Suppliers::TablesController, type: :controller do
@table.number.should eq(14) @table.number.should eq(14)
end 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 it "should not be possible to update a table to another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
expect{
put :update, id: @table.id, table: {number: 6, supplier_id: supplier2.id} put :update, id: @table.id, table: {number: 6, supplier_id: supplier2.id}
}.to raise_error
@table.reload @table.reload
@table.supplier_id.should == @supplier.id expect( @table.supplier_id ).to eq @supplier.id
end end
it "should not be possible to update a table of another supplier" do it "should not be possible to update a table of another supplier" do
table = create :table, number: 11 table = create :table, number: 11
put :update, id: table, table: {number: 6} put :update, id: table.id, table: {number: 6}
table.reload table.reload
table.number.should == 11 table.number.should == 11
end end
end end
context "invalid attributes" do context "invalid attributes" do
it "locates the requested table" do it "returns an error response" do
put :update, id: @table, table: {number: '-6'} #put :update, id: @table, format: :json, table: {number: 'aaa'}
assigns(:table).should eq(@table) #TODO: when proper invalid tables exist expect( JSON.parse(response.body)['errors'] ).to be_present
end
it "re-renders the edit method" do
put :update, id: @table, table: {number: '-6'}
response.should render_template :edit
end end
end end
end end
@@ -163,11 +106,6 @@ describe Suppliers::TablesController, type: :controller do
}.to change(Table, :count).by(-1) }.to change(Table, :count).by(-1)
end 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 it "should not be possible to delete a table of another supplier" do
table = create :table table = create :table
expect{ expect{