Make the products controller specs green

This commit is contained in:
2025-09-23 12:15:09 -05:00
parent 54f86078f5
commit 6a100555bc
6 changed files with 24 additions and 19 deletions
+1
View File
@@ -140,6 +140,7 @@ group :test do
gem 'timecop' gem 'timecop'
gem 'turnip' gem 'turnip'
gem 'webmock' gem 'webmock'
gem "rails-controller-testing"
#gem 'rails-controller-testing' #gem 'rails-controller-testing'
#gem 'rb-fsevent', require: false #if RUBY_PLATFORM =~ /darwin/i #gem 'rb-fsevent', require: false #if RUBY_PLATFORM =~ /darwin/i
end end
+5
View File
@@ -445,6 +445,10 @@ GEM
activesupport (= 8.0.2.1) activesupport (= 8.0.2.1)
bundler (>= 1.15.0) bundler (>= 1.15.0)
railties (= 8.0.2.1) railties (= 8.0.2.1)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
activesupport (>= 5.0.1.rc1)
rails-dom-testing (2.3.0) rails-dom-testing (2.3.0)
activesupport (>= 5.0.0) activesupport (>= 5.0.0)
minitest minitest
@@ -640,6 +644,7 @@ DEPENDENCIES
puma (>= 5.0) puma (>= 5.0)
rack-cors rack-cors
rails (~> 8.0.2.1) rails (~> 8.0.2.1)
rails-controller-testing
rqrcode rqrcode
rspec-its rspec-its
rspec-rails rspec-rails
+3 -3
View File
@@ -125,9 +125,9 @@ private
end end
alias json_notice js_notice alias json_notice js_notice
def show_404(options = {}) def show_404(error)
respond_to do |format| respond_to do |format|
format.html { render 'dashboard/404', options.reverse_merge(layout: true, status: 404)} format.html { render 'dashboard/404', {layout: true, status: 404} }
format.json { render json: {ok: false}, status: 404 } format.json { render json: {ok: false}, status: 404 }
end end
end end
@@ -138,7 +138,7 @@ private
if obj.is_a?(SimplyStored::Couch) if obj.is_a?(SimplyStored::Couch)
if obj.errors.present? if obj.errors.present?
#json_api_errors = obj.errors.details.map{|key, ers| {id: key, status: '422', title: ers.map{|e| e[:error].to_s}.join(', '), source: {parameter: key}}} #json_api_errors = obj.errors.details.map{|key, ers| {id: key, status: '422', title: ers.map{|e| e[:error].to_s}.join(', '), source: {parameter: key}}}
json_api_errors = obj.errors.map{|error| {id: error.attribute, status: '422', title: error.message}} json_api_errors = obj.errors.map{|error| {id: error.attribute.to_s, status: '422', title: error.message}}
options.reverse_merge(json: {errors: json_api_errors, ok: false}, status: :unprocessable_entity) options.reverse_merge(json: {errors: json_api_errors, ok: false}, status: :unprocessable_entity)
else else
options.reverse_merge(json: obj) options.reverse_merge(json: obj)
@@ -19,7 +19,6 @@ module Suppliers
# GET /products/1 # GET /products/1
# GET /products/1.json # GET /products/1.json
def show def show
respond_to do |format| respond_to do |format|
format.html # show.html.erb format.html # show.html.erb
format.json { render json: @product } format.json { render json: @product }
+1 -1
View File
@@ -3,7 +3,7 @@ class CustomFormBuilder < ActionView::Helpers::FormBuilder
t = @template t = @template
elements = [] elements = []
elements << submit(nil, class: 'form-action-submit') elements << submit(nil, class: 'form-action-submit')
plural_subject = object.class.name.underscore.pluralize plural_subject = object.class.name.underscore.pluralize.to_sym
elements << t.link_to( elements << t.link_to(
t.content_tag(:span, nil, data: {t: 'helpers.links.cancel'}), t.content_tag(:span, nil, data: {t: 'helpers.links.cancel'}),
[:suppliers, plural_subject], [:suppliers, plural_subject],
@@ -17,13 +17,13 @@ describe Suppliers::ProductsController, type: :controller do
describe "GET #show" do describe "GET #show" do
it "assigns the requested product to @product" do it "assigns the requested product to @product" do
product = create :product, supplier: @supplier product = create :product, supplier: @supplier
get :show, id: product get :show, params: {id: product}
assigns(:product).should eq(product) assigns(:product).should eq(product)
end end
it "should not display a product of another supplier" do it "should not display a product of another supplier" do
product = create :product product = create :product
get :show, id: product get :show, params: {id: product}
expect(response.status).to eq 404 expect(response.status).to eq 404
end end
end end
@@ -39,18 +39,18 @@ describe Suppliers::ProductsController, type: :controller do
context "with valid attributes" do context "with valid attributes" do
it "creates a new product" do it "creates a new product" do
expect{ expect{
post :create, product: valid_attributes post :create, params: {product: valid_attributes}
}.to change(Product, :count).by(1) }.to change(Product, :count).by(1)
end end
it "redirects to the new product" do it "redirects to the new product" do
post :create, product: valid_attributes post :create, params: {product: valid_attributes}
response.should redirect_to [:suppliers, :products] response.should redirect_to [:suppliers, :products]
end end
it "should not be possible to create a product for another supplier" do it "should not be possible to create a product for another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
post :create, product: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id) post :create, params: {product: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id)}
expect( Product.find_by_name('Trying to hack').supplier_id ).to eq @supplier.id expect( Product.find_by_name('Trying to hack').supplier_id ).to eq @supplier.id
end end
end end
@@ -58,12 +58,12 @@ describe Suppliers::ProductsController, type: :controller do
context "with invalid attributes" do context "with invalid attributes" do
it "does not save the new product" do it "does not save the new product" do
expect{ expect{
post :create, product: invalid_attributes post :create, params: {product: invalid_attributes}
}.to_not change(Product, :count) }.to_not change(Product, :count)
end end
it 'returns an error object' do it 'returns an error object' do
post :create, format: :json, product: invalid_attributes post :create, params: {format: :json, product: invalid_attributes}
expect( JSON.parse(response.body)['errors'] ).to be_present expect( JSON.parse(response.body)['errors'] ).to be_present
end end
end end
@@ -76,26 +76,26 @@ describe Suppliers::ProductsController, type: :controller do
context "valid attributes" do context "valid attributes" do
it "located the requested product" do it "located the requested product" do
put :update, id: @product, product: valid_attributes put :update, params: {id: @product, product: valid_attributes}
@product.reload @product.reload
assigns(:product).should eq(@product) assigns(:product).should eq(@product)
end end
it "changes @product's attributes" do it "changes @product's attributes" do
put :update, id: @product, product: valid_attributes.merge(name: "ChangedByTest") put :update, params: {id: @product, product: valid_attributes.merge(name: "ChangedByTest")}
@product.reload @product.reload
@product.name.should eq("ChangedByTest") @product.name.should eq("ChangedByTest")
end end
it "should not be possible to update a product to another supplier" do it "should not be possible to update a product to another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
put :update, id: @product, product: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id) put :update, params: {id: @product, product: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id)}
expect( Product.find_by_name('Trying to hack' ).supplier_id ).to eq @supplier.id expect( Product.find_by_name('Trying to hack' ).supplier_id ).to eq @supplier.id
end end
it "should not be possible to update a product of another supplier" do it "should not be possible to update a product of another supplier" do
product = create :product, name: 'Other supplier product' product = create :product, name: 'Other supplier product'
put :update, id: product, product: {name: "Trying to hack"} put :update, params: {id: product, product: {name: "Trying to hack"}}
product.reload product.reload
expect( product.name ).to eq 'Other supplier product' expect( product.name ).to eq 'Other supplier product'
end end
@@ -103,7 +103,7 @@ describe Suppliers::ProductsController, type: :controller do
context "with invalid attributes" do context "with invalid attributes" do
it "returns an error response" do it "returns an error response" do
put :update, id: @product, format: :json, product: invalid_attributes put :update, params: {id: @product, format: :json, product: invalid_attributes}
expect( JSON.parse( response.body )['errors']).to be_present expect( JSON.parse( response.body )['errors']).to be_present
end end
end end
@@ -116,14 +116,14 @@ describe Suppliers::ProductsController, type: :controller do
it "deletes the product" do it "deletes the product" do
expect{ expect{
delete :destroy, id: @product delete :destroy, params: {id: @product}
}.to change(Product, :count).by(-1) }.to change(Product, :count).by(-1)
end end
it "should not be possible to delete a product of another supplier" do it "should not be possible to delete a product of another supplier" do
product = create :product product = create :product
expect{ expect{
delete :destroy, id: product.id delete :destroy, params: {id: product.id}
}.to_not change(Product, :count) }.to_not change(Product, :count)
end end
end end