Files
mozo-backend/spec/controllers/suppliers/lists_controller_spec.rb
T
2025-09-23 17:45:00 -05:00

155 lines
4.3 KiB
Ruby

# 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
setup_supplier_for_controller
ActionController::Parameters.permit_all_parameters = false
#controller.stub(:list_params){ controller.params.require(:list).permit! } # allow all parameters since cross parameter injection is tested
end
def valid_attributes
{state: 'active'}
end
def invalid_params
{}
end
#after :each do
#ActionController::Parameters.permit_all_parameters = true
#end
describe "GET #index" do
it "does not include lists from another supplier" do
list1 = create :list, supplier: @supplier
list2 = create :list
get :index, format: :json
assigns(:lists).should eq([list1])
end
end
describe "GET #show" do
it "assigns the requested list to @list" do
list = create :list, supplier: @supplier
get :show, params: {id: list.id}
assigns(:list).should eq(list)
end
it "should not display a list of another supplier" do
list = create :list
get :show, params: {id: list.id}
response.status.should == 404
end
it "renders the #show view" do
list = create :list, supplier: @supplier
get :show, params: {id: list.id}
response.body.should include 'orders'
end
end
describe "GET #new" do
it "assigns a new list to @list" do
get :new
assigns(:list).should be_a List
end
end
describe "POST #create" do
context "with valid attributes" do
it "creates a new list" do
expect{
post :create, params: {list: valid_attributes}
}.to change(List, :count).by(1)
end
it "should not be possible to create a list for another supplier" do
supplier2 = create :supplier
post :create, params: {list: valid_attributes.merge(price: '6.66', supplier_id: supplier2.id)}
expect( List.find_by_price(6.66).supplier_id ).to eq @supplier.id
end
end
context "with invalid attributes", broken: true do
it "does not save the new list" do
expect{
post :create, params: {list: invalid_params}, format: :json
}.not_to change{ List.count }
List.count.should be_zero
end
it 'returns an error object' do
post :create, params: {list: invalid_params}, format: :json
expect( JSON.parse(response.body)['errors'] ).to be_present
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, params: {id: @list.id, list: valid_attributes}
@list.reload
assigns(:list).should eq(@list)
end
it "changes @list's price attribute" do
put :update, params: {id: @list.id, list: valid_attributes.merge(price: '7.22')}
@list.reload
@list.price.should eq(7.22)
end
it "should not be possible to update a list to another supplier" do
supplier2 = create :supplier
put :update, params: {id: @list.id, list: valid_attributes.merge(supplier_id: supplier2.id)}
@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, params: {id: list.id, list: {price: '6.66'}}
list.reload
list.price.should == 7.22
end
end
context "invalid attributes", broken: true do
it "returns an error response" do
put :update, params: {id: @list, list: invalid_params}, format: :json
expect( JSON.parse( response.body )['errors']).to be_present
end
end
end
describe 'DELETE destroy' do
before :each do
@list = create :list, supplier: @supplier
end
it "deletes the list" do
expect{
delete :destroy, params: {id: @list.id}
}.to change(List, :count).by(-1)
end
it "should not be possible to delete a list of another supplier" do
list = create :list
expect{
delete :destroy, params: {id: list.id}
}.to_not change(List, :count)
end
end
end