From ebc794194f4fe6b68cbb39b2a4bd0e8b16c0db13 Mon Sep 17 00:00:00 2001 From: wandji20 Date: Tue, 23 Jul 2024 13:02:33 +0100 Subject: [PATCH] Add product spec when supplier is empty and remove error status code from product create action [OFN-12591] --- .rubocop_todo.yml | 25 +++---------------- .../spree/admin/products_controller.rb | 2 +- app/models/product_import/entry_processor.rb | 2 -- .../spree/admin/products_controller_spec.rb | 7 +++--- spec/system/admin/products_spec.rb | 17 +++++++++++++ 5 files changed, 26 insertions(+), 27 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 30998d7e4bd..4559b67da74 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -12,22 +12,6 @@ Layout/EmptyLines: Exclude: - 'app/services/products_renderer.rb' -# Offense count: 6 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, IndentationWidth. -# SupportedStyles: aligned, indented, indented_relative_to_receiver -Layout/MultilineMethodCallIndentation: - Exclude: - - 'app/services/products_renderer.rb' - -# Offense count: 2 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, IndentationWidth. -# SupportedStyles: aligned, indented -Layout/MultilineOperationIndentation: - Exclude: - - 'app/services/products_renderer.rb' - # Offense count: 16 # Configuration parameters: AllowComments, AllowEmptyLambdas. Lint/EmptyBlock: @@ -101,7 +85,7 @@ Lint/UselessMethodDefinition: Exclude: - 'app/models/spree/gateway.rb' -# Offense count: 23 +# Offense count: 24 # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes, Max. Metrics/AbcSize: Exclude: @@ -114,6 +98,7 @@ Metrics/AbcSize: - 'app/helpers/spree/admin/navigation_helper.rb' - 'app/models/enterprise_group.rb' - 'app/models/enterprise_relationship.rb' + - 'app/models/product_import/entry_processor.rb' - 'app/models/spree/ability.rb' - 'app/models/spree/address.rb' - 'app/models/spree/order/checkout.rb' @@ -142,7 +127,7 @@ Metrics/BlockNesting: Exclude: - 'app/models/spree/payment/processing.rb' -# Offense count: 47 +# Offense count: 46 # Configuration parameters: CountComments, Max, CountAsOne. Metrics/ClassLength: Exclude: @@ -178,7 +163,6 @@ Metrics/ClassLength: - 'app/models/spree/variant.rb' - 'app/models/spree/zone.rb' - 'app/reflexes/admin/orders_reflex.rb' - - 'app/reflexes/products_reflex.rb' - 'app/serializers/api/cached_enterprise_serializer.rb' - 'app/serializers/api/enterprise_shopfront_serializer.rb' - 'app/services/cart_service.rb' @@ -408,7 +392,6 @@ RSpecRails/HaveHttpStatus: - 'spec/controllers/stripe/webhooks_controller_spec.rb' - 'spec/controllers/user_passwords_controller_spec.rb' - 'spec/controllers/user_registrations_controller_spec.rb' - - 'spec/requests/admin/images_spec.rb' - 'spec/requests/api/routes_spec.rb' - 'spec/requests/checkout/stripe_sca_spec.rb' - 'spec/requests/home_controller_spec.rb' @@ -725,7 +708,7 @@ Style/ClassAndModuleChildren: - 'lib/open_food_network/locking.rb' - 'spec/models/spree/payment_method_spec.rb' -# Offense count: 2 +# Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle. # SupportedStyles: always, always_true, never diff --git a/app/controllers/spree/admin/products_controller.rb b/app/controllers/spree/admin/products_controller.rb index 2755f1b36d9..3ae92a38bd1 100644 --- a/app/controllers/spree/admin/products_controller.rb +++ b/app/controllers/spree/admin/products_controller.rb @@ -46,7 +46,7 @@ def create # Re-fill the form with deleted params on product @on_hand = request.params[:product][:on_hand] @on_demand = request.params[:product][:on_demand] - render :new, status: :unprocessable_entity + render :new end end end diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb index 6c7b71eff99..c7596cc1ae3 100644 --- a/app/models/product_import/entry_processor.rb +++ b/app/models/product_import/entry_processor.rb @@ -149,7 +149,6 @@ def save_existing_inventory_item(entry) end end - # rubocop:disable Metrics/AbcSize def save_new_product(entry) @already_created ||= {} # If we've already added a new product with these attributes @@ -178,7 +177,6 @@ def save_new_product(entry) @already_created.deep_merge! entry.enterprise_id => { entry.name => product.id } end - # rubocop:enable Metrics/AbcSize def save_variant(entry) variant = entry.product_object diff --git a/spec/controllers/spree/admin/products_controller_spec.rb b/spec/controllers/spree/admin/products_controller_spec.rb index 6752239a8c8..f7f81f2cdc0 100644 --- a/spec/controllers/spree/admin/products_controller_spec.rb +++ b/spec/controllers/spree/admin/products_controller_spec.rb @@ -168,17 +168,18 @@ spree_put :create, product: product_attrs_with_image - expect(response.status).to eq 422 + expect(response.status).to eq 200 end end describe "when variant attributes are missing" do - it 'renders 422 error' do + it 'renders form with errors' do spree_post :create, product: product_attrs.merge!( { supplier_id: nil, primary_taxon_id: nil } ), button: 'create' - expect(response.status).to eq 422 + expect(response.status).to eq 200 + expect(response).to render_template('spree/admin/products/new') end end end diff --git a/spec/system/admin/products_spec.rb b/spec/system/admin/products_spec.rb index bd19bdcbfdc..9fad202f793 100644 --- a/spec/system/admin/products_spec.rb +++ b/spec/system/admin/products_spec.rb @@ -187,6 +187,23 @@ expect(page).to have_content "Product Category must exist" end + it "creating product with empty product supplier fails" do + fill_in 'product_name', with: 'Hot Cakes' + select "Weight (kg)", from: 'product_variant_unit_with_scale' + fill_in "product_unit_value", with: '1' + fill_in 'product_price', with: '1.99' + select taxon.name, from: "product_primary_taxon_id" + fill_in 'product_on_hand', with: 0 + check 'product_on_demand' + select 'Test Tax Category', from: 'product_tax_category_id' + fill_in_trix_editor 'product_description', + with: 'In demand, and on_demand! The hottest cakes in town.' + click_button 'Create' + + expect(current_path).to eq spree.admin_products_path + expect(page).to have_content "Supplier must exist" + end + describe "localization settings" do shared_examples "with different price values" do |localized_number, price| context "when enable_localized_number is set to #{localized_number}" do