diff --git a/Gemfile b/Gemfile index db355118..bcea423f 100644 --- a/Gemfile +++ b/Gemfile @@ -140,6 +140,7 @@ group :test do gem 'timecop' gem 'turnip' gem 'webmock' + gem "rails-controller-testing" #gem 'rails-controller-testing' #gem 'rb-fsevent', require: false #if RUBY_PLATFORM =~ /darwin/i end diff --git a/Gemfile.lock b/Gemfile.lock index 3802db06..380f1d98 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -445,6 +445,10 @@ GEM activesupport (= 8.0.2.1) bundler (>= 1.15.0) 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) activesupport (>= 5.0.0) minitest @@ -640,6 +644,7 @@ DEPENDENCIES puma (>= 5.0) rack-cors rails (~> 8.0.2.1) + rails-controller-testing rqrcode rspec-its rspec-rails diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8493844a..801e57cd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -125,9 +125,9 @@ private end alias json_notice js_notice - def show_404(options = {}) + def show_404(error) 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 } end end @@ -138,7 +138,7 @@ private if obj.is_a?(SimplyStored::Couch) 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.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) else options.reverse_merge(json: obj) diff --git a/app/controllers/suppliers/products_controller.rb b/app/controllers/suppliers/products_controller.rb index 6635ac90..d667463f 100644 --- a/app/controllers/suppliers/products_controller.rb +++ b/app/controllers/suppliers/products_controller.rb @@ -19,7 +19,6 @@ module Suppliers # GET /products/1 # GET /products/1.json def show - respond_to do |format| format.html # show.html.erb format.json { render json: @product } diff --git a/config/initializers/custom_form_builder.rb b/config/initializers/custom_form_builder.rb index 24234b94..c1f3409f 100644 --- a/config/initializers/custom_form_builder.rb +++ b/config/initializers/custom_form_builder.rb @@ -3,7 +3,7 @@ class CustomFormBuilder < ActionView::Helpers::FormBuilder t = @template elements = [] 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( t.content_tag(:span, nil, data: {t: 'helpers.links.cancel'}), [:suppliers, plural_subject], diff --git a/spec/controllers/suppliers/products_controller_spec.rb b/spec/controllers/suppliers/products_controller_spec.rb index edbda994..70269a90 100644 --- a/spec/controllers/suppliers/products_controller_spec.rb +++ b/spec/controllers/suppliers/products_controller_spec.rb @@ -17,13 +17,13 @@ describe Suppliers::ProductsController, type: :controller do describe "GET #show" do it "assigns the requested product to @product" do product = create :product, supplier: @supplier - get :show, id: product + get :show, params: {id: product} assigns(:product).should eq(product) end it "should not display a product of another supplier" do product = create :product - get :show, id: product + get :show, params: {id: product} expect(response.status).to eq 404 end end @@ -39,18 +39,18 @@ describe Suppliers::ProductsController, type: :controller do context "with valid attributes" do it "creates a new product" do expect{ - post :create, product: valid_attributes + post :create, params: {product: valid_attributes} }.to change(Product, :count).by(1) end it "redirects to the new product" do - post :create, product: valid_attributes + post :create, params: {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: 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 end end @@ -58,12 +58,12 @@ describe Suppliers::ProductsController, type: :controller do context "with invalid attributes" do it "does not save the new product" do expect{ - post :create, product: invalid_attributes + post :create, params: {product: invalid_attributes} }.to_not change(Product, :count) end 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 end end @@ -76,26 +76,26 @@ describe Suppliers::ProductsController, type: :controller do context "valid attributes" do it "located the requested product" do - put :update, id: @product, product: valid_attributes + put :update, params: {id: @product, product: valid_attributes} @product.reload assigns(:product).should eq(@product) end 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.name.should eq("ChangedByTest") end it "should not be possible to update a product to another supplier" do 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 end it "should not be possible to update a product of another supplier" do 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 expect( product.name ).to eq 'Other supplier product' end @@ -103,7 +103,7 @@ describe Suppliers::ProductsController, type: :controller do context "with invalid attributes" 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 end end @@ -116,14 +116,14 @@ describe Suppliers::ProductsController, type: :controller do it "deletes the product" do expect{ - delete :destroy, id: @product + delete :destroy, params: {id: @product} }.to change(Product, :count).by(-1) end it "should not be possible to delete a product of another supplier" do product = create :product expect{ - delete :destroy, id: product.id + delete :destroy, params: {id: product.id} }.to_not change(Product, :count) end end