Add spring and fix controller specs

This commit is contained in:
2014-07-14 14:09:35 +02:00
parent 96841116c5
commit 6420b428a7
13 changed files with 147 additions and 185 deletions
@@ -1,25 +1,27 @@
# encoding: UTF-8
require 'spec_helper'
describe Suppliers::ListsController do
describe Suppliers::ListsController, type: :controller do
before :each do
@supplier = Supplier.find_by_email('supplier@qwaiter.com') || 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
#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 "populates an array of lists" do
list = create :list, supplier: @supplier
get :index
assigns(:lists).should eq([list])
end
it "has pagination options if show_all is given" do
list = create :list, supplier: @supplier
get :index, show_all: 'yes'
@@ -81,32 +83,28 @@ describe Suppliers::ListsController do
context "with valid attributes" do
it "creates a new list" do
expect{
post :create, list: attributes_for(:list, supplier: @supplier)
post :create, list: valid_attributes
}.to change(List, :count).by(1)
end
it "redirects to the new list" do
post :create, list: attributes_for(:list, supplier: @supplier)
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
post :create, list: attributes_for(:list, price: '6.66', supplier: supplier2)
List.find_by_price(6.66).supplier_id.should == @supplier.id
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: {table_id: ''}
}.to_not change(List, :count)
end
it "re-renders the new method" do
post :create, list: {table_id: ''}
response.should render_template :new
post :create, list: invalid_params
}.to raise_error
List.count.should be_zero
end
end
end
@@ -118,24 +116,25 @@ describe Suppliers::ListsController do
context "valid attributes" do
it "located the requested list" do
put :update, id: @list, list: attributes_for(:list, supplier: @supplier)
put :update, id: @list, list: valid_attributes
@list.reload
assigns(:list).should eq(@list)
end
it "changes @list's attributes" do
put :update, id: @list, list: attributes_for(:list, price: "7.22", supplier: @supplier)
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: attributes_for(:list, supplier: @supplier)
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
put :update, id: @list, list: attributes_for(:list, supplier: supplier2)
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
@@ -149,14 +148,8 @@ describe Suppliers::ListsController do
end
context "invalid attributes" do
it "locates the requested list" do
put :update, id: @list, list: {table_id: ''}
assigns(:list).should eq(@list)
end
it "re-renders the edit method" do
put :update, id: @list, list: {table_id: ''}
response.should render_template :edit
it "raises on invalid params" do
expect{ put :update, id: @list, list: invalid_params }.to raise_error
end
end
end
@@ -168,7 +161,7 @@ describe Suppliers::ListsController do
it "deletes the list" do
expect{
delete :destroy, id: @list
delete :destroy, id: @list
}.to change(List, :count).by(-1)
end
@@ -1,20 +1,21 @@
# encoding: UTF-8
require 'spec_helper'
describe Suppliers::ProductCategoriesController do
describe Suppliers::ProductCategoriesController, type: :controller do
before :each do
@supplier = Supplier.find_by_email('supplier@qwaiter.com') || create(:supplier, :confirmed)
controller.stub(:product_category_params){ controller.params.require(:product_category).permit! } # allow all parameters since cross parameter injection is tested
sign_in @supplier
end
describe "GET #index" do
it "populates an array of product_categories" do
product_category = create :product_category, supplier: @supplier
get :index
assigns(:product_categories).should eq([product_category])
end
def valid_attributes
{name: 'Lunch'}
end
def invalid_attributes
{name: ''}
end
describe "GET #index" do
it "does not include product_categories from another supplier" do
product_category1 = create :product_category, supplier: @supplier
product_category2 = create :product_category
@@ -26,11 +27,6 @@ describe Suppliers::ProductCategoriesController 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
@@ -45,12 +41,6 @@ describe Suppliers::ProductCategoriesController do
get :show, id: product_category
response.status.should == 404
end
it "renders the #show view" do
product_category = create :product_category, supplier: @supplier
get :show, id: product_category
response.should render_template :show
end
end
describe "GET #new" do
@@ -58,43 +48,33 @@ describe Suppliers::ProductCategoriesController do
get :new
assigns(:product_category).should be_a ProductCategory
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 product_category" do
expect{
post :create, product_category: attributes_for(:product_category, supplier: @supplier)
post :create, product_category: valid_attributes
}.to change(ProductCategory, :count).by(1)
end
it "redirects to the new product_category" do
post :create, product_category: attributes_for(:product_category, supplier: @supplier)
post :create, product_category: valid_attributes
response.should redirect_to [:suppliers, :product_categories]
end
it "should not be possible to create a product category for another supplier" do
supplier2 = create :supplier
post :create, product_category: attributes_for(:product_category, name: 'Trying to hack', supplier: supplier2)
ProductCategory.find_by_name('Trying to hack').supplier_id.should == @supplier.id
expect{ post :create, product_category: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id) }.to raise_error
ProductCategory.find_by_name('Trying to hack').should_not be_present
end
end
context "with invalid attributes" do
it "does not save the new product_category" do
expect{
post :create, product_category: {name: ''}
}.to_not change(ProductCategory, :count)
end
it "re-renders the new method" do
post :create, product_category: {name: ''}
response.should render_template :new
post :create, product_category: invalid_attributes
}.not_to change{ ProductCategory.count }
end
end
end
@@ -106,46 +86,33 @@ describe Suppliers::ProductCategoriesController do
context "valid attributes" do
it "located the requested product_category" do
put :update, id: @product_category, product_category: attributes_for(:product_category, supplier: @supplier)
put :update, id: @product_category, product_category: valid_attributes
@product_category.reload
assigns(:product_category).should eq(@product_category)
end
it "changes @product_category's attributes" do
put :update, id: @product_category, product_category: attributes_for(:product_category, name: "ChangedByTest", supplier: @supplier)
put :update, id: @product_category, product_category: valid_attributes.merge(name: "ChangedByTest")
@product_category.reload
@product_category.name.should eq("ChangedByTest")
end
it "redirects to the updated product_category" do
put :update, id: @product_category, product_category: attributes_for(:product_category, supplier: @supplier)
response.should redirect_to [:suppliers, :product_categories]
end
it "should not be possible to update a product category to another supplier" do
supplier2 = create :supplier
put :update, id: @product_category, product_category: attributes_for(:product_category, name: "Trying to hack", supplier: supplier2)
ProductCategory.find_by_name('Trying to hack').supplier_id.should == @supplier.id
expect{
put :update, id: @product_category, product_category: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id)
}.to raise_error
ProductCategory.find_by_name('Trying to hack').should_not be_present
end
it "should not be possible to update a product_category of another supplier" do
product_category = create :product_category, name: 'Other supplier product_category'
put :update, id: product_category, product_category: {name: "Trying to hack"}
expect(response.status).to eq 404
product_category.reload
product_category.name.should == 'Other supplier product_category'
end
end
context "invalid attributes" do
it "locates the requested product_category" do
put :update, id: @product_category, product_category: {name: ''}
assigns(:product_category).should eq(@product_category)
end
it "re-renders the edit method" do
put :update, id: @product_category, product_category: {name: ''}
response.should render_template :edit
end
end
end
describe 'DELETE destroy' do
@@ -155,20 +122,15 @@ describe Suppliers::ProductCategoriesController do
it "deletes the product_category" do
expect{
delete :destroy, id: @product_category
delete :destroy, id: @product_category
}.to change(ProductCategory, :count).by(-1)
end
it "redirects to product_categories#index" do
delete :destroy, id: @product_category
response.should redirect_to [:suppliers, :product_categories]
end
it "should not be possible to delete a product category of another supplier" do
product_category = create :product_category
expect{
delete :destroy, id: product_category
}.to_not change(ProductCategory, :count)
}.to_not change{ProductCategory.count }
end
end
end
@@ -1,20 +1,21 @@
# encoding: UTF-8
require 'spec_helper'
describe Suppliers::ProductsController do
describe Suppliers::ProductsController, type: :controller do
def valid_attributes
{name: 'Lemon cake', price: '4.2'}
end
def invalid_attributes
{name: ''}
end
before :each do
@supplier = Supplier.find_by_email('supplier@qwaiter.com') || create(:supplier, :confirmed)
controller.stub(:product_params){ controller.params.require(:product).permit! } # allow all parameters since cross parameter injection is tested
sign_in @supplier
end
describe "GET #index" do
it "populates an array of products" do
product = create :product, supplier: @supplier
get :index
assigns(:products).should eq([product])
end
it "does not include products from another supplier" do
product1 = create :product, supplier: @supplier
product2 = create :product
@@ -26,11 +27,6 @@ describe Suppliers::ProductsController 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
@@ -43,22 +39,11 @@ describe Suppliers::ProductsController do
it "should not display a product of another supplier" do
product = create :product
get :show, id: product
response.status.should == 404
end
it "renders the #show view" do
product = create :product, supplier: @supplier
get :show, id: product
response.should render_template :show
expect(response.status).to eq 404
end
end
describe "GET #new" do
it "assigns a new product to @product" do
get :new
assigns(:product).should be_a Product
end
it "renders the #show view" do
get :new
response.should render_template :new
@@ -69,31 +54,31 @@ describe Suppliers::ProductsController do
context "with valid attributes" do
it "creates a new product" do
expect{
post :create, product: attributes_for(:product, supplier: @supplier)
post :create, product: valid_attributes
}.to change(Product, :count).by(1)
end
it "redirects to the new product" do
post :create, product: attributes_for(:product, supplier: @supplier)
post :create, product: valid_attributes
response.should redirect_to [:suppliers, :products]
end
it "should not be possible to create a product for another supplier" do
supplier2 = create :supplier
post :create, product: attributes_for(:product, name: 'Trying to hack', supplier: supplier2)
Product.find_by_name('Trying to hack').supplier_id.should == @supplier.id
expect{ post :create, product: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id) }.to raise_error
Product.find_by_name('Trying to hack').should_not be_present
end
end
context "with invalid attributes" do
it "does not save the new product" do
expect{
post :create, product: {name: ''}
post :create, product: invalid_attributes
}.to_not change(Product, :count)
end
it "re-renders the new method" do
post :create, product: {name: ''}
post :create, product: invalid_attributes
response.should render_template :new
end
end
@@ -106,25 +91,21 @@ describe Suppliers::ProductsController do
context "valid attributes" do
it "located the requested product" do
put :update, id: @product, product: attributes_for(:product, supplier: @supplier)
put :update, id: @product, product: valid_attributes
@product.reload
assigns(:product).should eq(@product)
end
it "changes @product's attributes" do
put :update, id: @product, product: attributes_for(:product, name: "ChangedByTest", supplier: @supplier)
put :update, id: @product, product: valid_attributes.merge(name: "ChangedByTest")
@product.reload
@product.name.should eq("ChangedByTest")
end
it "redirects to the updated product" do
put :update, id: @product, product: attributes_for(:product, supplier: @supplier)
response.should redirect_to [:suppliers, :products]
end
it "should not be possible to update a product to another supplier" do
supplier2 = create :supplier
put :update, id: @product, product: attributes_for(:product, name: "Trying to hack", supplier: supplier2)
Product.find_by_name('Trying to hack').supplier_id.should == @supplier.id
expect{ put :update, id: @product, product: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id) }.to raise_error
expect( Product.find_by_name 'Trying to hack' ).not_to be_present
end
it "should not be possible to update a product of another supplier" do
@@ -137,12 +118,12 @@ describe Suppliers::ProductsController do
context "invalid attributes" do
it "locates the requested product" do
put :update, id: @product, product: {name: ''}
put :update, id: @product, product: invalid_attributes
assigns(:product).should eq(@product)
end
it "re-renders the edit method" do
put :update, id: @product, product: {name: ''}
put :update, id: @product, product: invalid_attributes
response.should render_template :edit
end
end
@@ -1,27 +1,27 @@
# encoding: UTF-8
require 'spec_helper'
describe Suppliers::SectionsController do
describe Suppliers::SectionsController, type: :controller do
before :each do
@supplier = Supplier.find_by_email('supplier@qwaiter.com') || create(:supplier, :confirmed)
controller.stub(:section_params){ controller.params.require(:section).permit! } # allow all parameters since cross parameter injection is tested
sign_in @supplier
end
describe "GET #index" do
it "populates an array of sections" do
base_section = @supplier.sections.first
section = create :section, supplier: @supplier
get :index
assigns(:sections).should =~[base_section, section].compact
end
def valid_attributes
{title: 'Terrace'}
end
def invalid_attributes
{title: ''}
end
describe "GET #index" do
it "does not include sections from another supplier" do
base_section = @supplier.sections.first
section1 = create :section, supplier: @supplier
section2 = create :section
get :index
assigns(:sections).should =~[base_section, section1].compact
assigns(:sections).map(&:id).sort.should == [base_section.id, section1.id].sort
end
it "should render without errors when no objects are present" do
@@ -71,31 +71,31 @@ describe Suppliers::SectionsController do
context "with valid attributes" do
it "creates a new section" do
expect{
post :create, section: attributes_for(:section, supplier: @supplier)
}.to change(Section, :count).by(2)
post :create, section: valid_attributes
}.to change(Section, :count).by(1)
end
it "redirects to the new section" do
post :create, section: attributes_for(:section, title: 'Created section 45', supplier: @supplier)
post :create, section: valid_attributes.merge(title: 'Created section 45')
response.should redirect_to [:suppliers, Section.find_by_title('Created section 45')]
end
it "should not be possible to create a section for another supplier" do
supplier2 = create :supplier
post :create, section: attributes_for(:section, title: 'Trying to hack', supplier: supplier2)
Section.find_by_title('Trying to hack').supplier_id.should == @supplier.id
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
end
end
context "with invalid attributes" do
it "does not save the new section" do
expect{
post :create, section: {title: ''}
post :create, section: invalid_attributes
}.to_not change(Section, :count)
end
it "re-renders the new method" do
post :create, section: {title: ''}
post :create, section: invalid_attributes
response.should render_template :new
end
end
@@ -108,25 +108,27 @@ describe Suppliers::SectionsController do
context "valid attributes" do
it "located the requested section" do
put :update, id: @section, section: attributes_for(:section, supplier: @supplier)
put :update, id: @section, section: valid_attributes
@section.reload
assigns(:section).should eq(@section)
end
it "changes @section's attributes" do
put :update, id: @section, section: attributes_for(:section, title: "ChangedByTest", supplier: @supplier)
put :update, id: @section, section: valid_attributes.merge(title: "ChangedByTest")
@section.reload
@section.title.should eq("ChangedByTest")
expect( @section.title ).to eq "ChangedByTest"
end
it "redirects to the updated section" do
put :update, id: @section, section: attributes_for(:section, supplier: @supplier)
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
put :update, id: @section, section: attributes_for(:section, title: "Trying to hack", supplier: supplier2)
Section.find_by_title('Trying to hack').supplier_id.should == @supplier.id
expect{ put :update, id: @section, section: valid_attributes.merge(title: "Trying to hack", supplier_id: supplier2.id) }.to raise_error
@section.reload
@section.supplier_id.should == @supplier.id
end
it "should not be possible to update a section of another supplier" do
@@ -139,12 +141,12 @@ describe Suppliers::SectionsController do
context "invalid attributes" do
it "locates the requested section" do
put :update, id: @section, section: {title: ''}
put :update, id: @section, section: invalid_attributes
assigns(:section).should eq(@section)
end
it "re-renders the edit method" do
put :update, id: @section, section: {title: ''}
put :update, id: @section, section: invalid_attributes
response.should render_template :edit
end
end
@@ -1,10 +1,10 @@
# encoding: UTF-8
require 'spec_helper'
describe Suppliers::TablesController do
describe Suppliers::TablesController, type: :controller do
before :each do
@supplier = Supplier.find_by_email('supplier@qwaiter.com') || create(:supplier, :confirmed)
controller.stub(:table_params){ controller.params.require(:table).permit! } # allow all parameters since cross parameter injection is tested
#controller.stub(:table_params){ controller.params.require(:table).permit! } # allow all parameters since cross parameter injection is tested
sign_in @supplier
end
@@ -69,19 +69,19 @@ describe Suppliers::TablesController do
context "with valid attributes" do
it "creates a new table" do
expect{
post :create, table: attributes_for(:table, supplier: @supplier)
post :create, table: {number: 22}
}.to change(Table, :count).by(1)
end
it "redirects to the new table" do
post :create, table: attributes_for(:table, supplier: @supplier)
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
post :create, table: attributes_for(:table, number: 6, supplier: supplier2)
Table.find_by_number(6).supplier_id.should == @supplier.id
expect { post :create, table: {number: 6, supplier_id: supplier2.id} }.to raise_error
Table.find_by_number(6).should_not be_present
end
end
@@ -106,25 +106,29 @@ describe Suppliers::TablesController do
context "valid attributes" do
it "located the requested table" do
put :update, id: @table, table: attributes_for(:table, supplier: @supplier)
put :update, id: @table, table: {number: 22}
@table.reload
assigns(:table).should eq(@table)
end
it "changes @table's attributes" do
put :update, id: @table, table: attributes_for(:table, number: "14", supplier: @supplier)
it "changes @table's number attribute" do
put :update, id: @table, table: {number: "14"}
@table.reload
@table.number.should eq(14)
end
it "redirects to the updated table" do
put :update, id: @table, table: attributes_for(:table, supplier: @supplier)
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
put :update, id: @table, table: attributes_for(:table, number: 6, supplier: supplier2)
Table.find_by_number(6).supplier_id.should == @supplier.id
expect{
put :update, id: @table.id, table: {number: 6, supplier_id: supplier2.id}
}.to raise_error
@table.reload
@table.supplier_id.should == @supplier.id
end
it "should not be possible to update a table of another supplier" do