From 0f5aa11f055c6cd0e68c1ab4d09d555e492af70f Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 26 Oct 2020 16:50:29 +0000 Subject: [PATCH 1/4] Nest spec in module declaration --- .../admin/customers_controller_spec.rb | 258 +++++++++--------- 1 file changed, 130 insertions(+), 128 deletions(-) diff --git a/spec/controllers/admin/customers_controller_spec.rb b/spec/controllers/admin/customers_controller_spec.rb index b439cc7c9c..efdb436c3a 100644 --- a/spec/controllers/admin/customers_controller_spec.rb +++ b/spec/controllers/admin/customers_controller_spec.rb @@ -1,172 +1,174 @@ require 'spec_helper' -describe Admin::CustomersController, type: :controller do - include AuthenticationHelper +module Admin + describe CustomersController, type: :controller do + include AuthenticationHelper - describe "index" do - let(:enterprise) { create(:distributor_enterprise) } - let(:another_enterprise) { create(:distributor_enterprise) } + describe "index" do + let(:enterprise) { create(:distributor_enterprise) } + let(:another_enterprise) { create(:distributor_enterprise) } - context "html" do - before do - allow(controller).to receive(:spree_current_user) { enterprise.owner } - end - - it "returns an empty @collection" do - spree_get :index, format: :html - expect(assigns(:collection)).to eq [] - end - end - - context "json" do - let!(:customer) { create(:customer, enterprise: enterprise) } - - context "where I manage the enterprise" do + context "html" do before do allow(controller).to receive(:spree_current_user) { enterprise.owner } end - context "and enterprise_id is given in params" do - let(:params) { { format: :json, enterprise_id: enterprise.id } } + it "returns an empty @collection" do + spree_get :index, format: :html + expect(assigns(:collection)).to eq [] + end + end - it "scopes @collection to customers of that enterprise" do - spree_get :index, params - expect(assigns(:collection)).to eq [customer] + context "json" do + let!(:customer) { create(:customer, enterprise: enterprise) } + + context "where I manage the enterprise" do + before do + allow(controller).to receive(:spree_current_user) { enterprise.owner } end - it "serializes the data" do - expect(ActiveModel::ArraySerializer).to receive(:new) - spree_get :index, params + context "and enterprise_id is given in params" do + let(:params) { { format: :json, enterprise_id: enterprise.id } } + + it "scopes @collection to customers of that enterprise" do + spree_get :index, params + expect(assigns(:collection)).to eq [customer] + end + + it "serializes the data" do + expect(ActiveModel::ArraySerializer).to receive(:new) + spree_get :index, params + end + end + + context "and enterprise_id is not given in params" do + it "returns an empty collection" do + spree_get :index, format: :json + expect(assigns(:collection)).to eq [] + end end end - context "and enterprise_id is not given in params" do + context "and I do not manage the enterprise" do + before do + allow(controller).to receive(:spree_current_user) { another_enterprise.owner } + end + it "returns an empty collection" do spree_get :index, format: :json expect(assigns(:collection)).to eq [] end end end + end - context "and I do not manage the enterprise" do - before do - allow(controller).to receive(:spree_current_user) { another_enterprise.owner } + describe "update" do + let(:enterprise) { create(:distributor_enterprise) } + let(:another_enterprise) { create(:distributor_enterprise) } + + context "json" do + let!(:customer) { create(:customer, enterprise: enterprise) } + + context "where I manage the customer's enterprise" do + render_views + + before do + allow(controller).to receive(:spree_current_user) { enterprise.owner } + end + + it "allows me to update the customer" do + spree_put :update, format: :json, id: customer.id, customer: { email: 'new.email@gmail.com' } + expect(JSON.parse(response.body)["id"]).to eq customer.id + expect(assigns(:customer)).to eq customer + expect(customer.reload.email).to eq 'new.email@gmail.com' + end end - it "returns an empty collection" do - spree_get :index, format: :json - expect(assigns(:collection)).to eq [] + context "where I don't manage the customer's enterprise" do + before do + allow(controller).to receive(:spree_current_user) { another_enterprise.owner } + end + + it "prevents me from updating the customer" do + spree_put :update, format: :json, id: customer.id, customer: { email: 'new.email@gmail.com' } + expect(response).to redirect_to unauthorized_path + expect(assigns(:customer)).to eq nil + expect(customer.email).to_not eq 'new.email@gmail.com' + end end end end - end - describe "update" do - let(:enterprise) { create(:distributor_enterprise) } - let(:another_enterprise) { create(:distributor_enterprise) } + describe "create" do + let(:enterprise) { create(:distributor_enterprise) } + let(:another_enterprise) { create(:distributor_enterprise) } - context "json" do - let!(:customer) { create(:customer, enterprise: enterprise) } - - context "where I manage the customer's enterprise" do - render_views - - before do - allow(controller).to receive(:spree_current_user) { enterprise.owner } - end - - it "allows me to update the customer" do - spree_put :update, format: :json, id: customer.id, customer: { email: 'new.email@gmail.com' } - expect(JSON.parse(response.body)["id"]).to eq customer.id - expect(assigns(:customer)).to eq customer - expect(customer.reload.email).to eq 'new.email@gmail.com' - end + def create_customer(enterprise) + spree_put :create, format: :json, customer: { email: 'new@example.com', enterprise_id: enterprise.id } end - context "where I don't manage the customer's enterprise" do - before do - allow(controller).to receive(:spree_current_user) { another_enterprise.owner } + context "json" do + context "where I manage the customer's enterprise" do + before do + allow(controller).to receive(:spree_current_user) { enterprise.owner } + end + + it "allows me to create the customer" do + expect { create_customer enterprise }.to change(Customer, :count).by(1) + end end - it "prevents me from updating the customer" do - spree_put :update, format: :json, id: customer.id, customer: { email: 'new.email@gmail.com' } - expect(response).to redirect_to unauthorized_path - expect(assigns(:customer)).to eq nil - expect(customer.email).to_not eq 'new.email@gmail.com' + context "where I don't manage the customer's enterprise" do + before do + allow(controller).to receive(:spree_current_user) { another_enterprise.owner } + end + + it "prevents me from creating the customer" do + expect { create_customer enterprise }.to change(Customer, :count).by(0) + end + end + + context "where I am the admin user" do + before do + allow(controller).to receive(:spree_current_user) { create(:admin_user) } + end + + it "allows admins to create the customer" do + expect { create_customer enterprise }.to change(Customer, :count).by(1) + end end end end - end - describe "create" do - let(:enterprise) { create(:distributor_enterprise) } - let(:another_enterprise) { create(:distributor_enterprise) } + describe "show" do + let(:enterprise) { create(:distributor_enterprise) } + let(:another_enterprise) { create(:distributor_enterprise) } - def create_customer(enterprise) - spree_put :create, format: :json, customer: { email: 'new@example.com', enterprise_id: enterprise.id } - end + context "json" do + let!(:customer) { create(:customer, enterprise: enterprise) } - context "json" do - context "where I manage the customer's enterprise" do - before do - allow(controller).to receive(:spree_current_user) { enterprise.owner } + context "where I manage the customer's enterprise" do + render_views + + before do + allow(controller).to receive(:spree_current_user) { enterprise.owner } + end + + it "renders the customer as json" do + spree_get :show, format: :json, id: customer.id + expect(JSON.parse(response.body)["id"]).to eq customer.id + end end - it "allows me to create the customer" do - expect { create_customer enterprise }.to change(Customer, :count).by(1) - end - end + context "where I don't manage the customer's enterprise" do + before do + allow(controller).to receive(:spree_current_user) { another_enterprise.owner } + end - context "where I don't manage the customer's enterprise" do - before do - allow(controller).to receive(:spree_current_user) { another_enterprise.owner } - end - - it "prevents me from creating the customer" do - expect { create_customer enterprise }.to change(Customer, :count).by(0) - end - end - - context "where I am the admin user" do - before do - allow(controller).to receive(:spree_current_user) { create(:admin_user) } - end - - it "allows admins to create the customer" do - expect { create_customer enterprise }.to change(Customer, :count).by(1) - end - end - end - end - - describe "show" do - let(:enterprise) { create(:distributor_enterprise) } - let(:another_enterprise) { create(:distributor_enterprise) } - - context "json" do - let!(:customer) { create(:customer, enterprise: enterprise) } - - context "where I manage the customer's enterprise" do - render_views - - before do - allow(controller).to receive(:spree_current_user) { enterprise.owner } - end - - it "renders the customer as json" do - spree_get :show, format: :json, id: customer.id - expect(JSON.parse(response.body)["id"]).to eq customer.id - end - end - - context "where I don't manage the customer's enterprise" do - before do - allow(controller).to receive(:spree_current_user) { another_enterprise.owner } - end - - it "prevents me from updating the customer" do - spree_get :show, format: :json, id: customer.id - expect(response).to redirect_to unauthorized_path + it "prevents me from updating the customer" do + spree_get :show, format: :json, id: customer.id + expect(response).to redirect_to unauthorized_path + end end end end From 0b51d8b2975cc60fc7ae6051262bf85bbbce07db Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 26 Oct 2020 16:50:49 +0000 Subject: [PATCH 2/4] Add rubocop config to allow specs to be 300 lines long --- .rubocop_specs.yml | 7 +++++++ engines/catalog/spec/.rubocop.yml | 2 ++ engines/dfc_provider/spec/.rubocop.yml | 2 ++ engines/order_management/spec/.rubocop.yml | 2 ++ engines/web/spec/.rubocop.yml | 2 ++ spec/.rubocop.yml | 2 ++ 6 files changed, 17 insertions(+) create mode 100644 .rubocop_specs.yml create mode 100644 engines/catalog/spec/.rubocop.yml create mode 100644 engines/dfc_provider/spec/.rubocop.yml create mode 100644 engines/order_management/spec/.rubocop.yml create mode 100644 engines/web/spec/.rubocop.yml create mode 100644 spec/.rubocop.yml diff --git a/.rubocop_specs.yml b/.rubocop_specs.yml new file mode 100644 index 0000000000..8ea54cd55c --- /dev/null +++ b/.rubocop_specs.yml @@ -0,0 +1,7 @@ +inherit_from: + - .rubocop.yml + +# This rubocop config file is only used for specs +# Here we allow specs to be 300 lines long +Metrics/ModuleLength: + Max: 300 diff --git a/engines/catalog/spec/.rubocop.yml b/engines/catalog/spec/.rubocop.yml new file mode 100644 index 0000000000..5d20d5e3d1 --- /dev/null +++ b/engines/catalog/spec/.rubocop.yml @@ -0,0 +1,2 @@ +inherit_from: + - ../.rubocop_specs.yml diff --git a/engines/dfc_provider/spec/.rubocop.yml b/engines/dfc_provider/spec/.rubocop.yml new file mode 100644 index 0000000000..5d20d5e3d1 --- /dev/null +++ b/engines/dfc_provider/spec/.rubocop.yml @@ -0,0 +1,2 @@ +inherit_from: + - ../.rubocop_specs.yml diff --git a/engines/order_management/spec/.rubocop.yml b/engines/order_management/spec/.rubocop.yml new file mode 100644 index 0000000000..5d20d5e3d1 --- /dev/null +++ b/engines/order_management/spec/.rubocop.yml @@ -0,0 +1,2 @@ +inherit_from: + - ../.rubocop_specs.yml diff --git a/engines/web/spec/.rubocop.yml b/engines/web/spec/.rubocop.yml new file mode 100644 index 0000000000..5d20d5e3d1 --- /dev/null +++ b/engines/web/spec/.rubocop.yml @@ -0,0 +1,2 @@ +inherit_from: + - ../.rubocop_specs.yml diff --git a/spec/.rubocop.yml b/spec/.rubocop.yml new file mode 100644 index 0000000000..5d20d5e3d1 --- /dev/null +++ b/spec/.rubocop.yml @@ -0,0 +1,2 @@ +inherit_from: + - ../.rubocop_specs.yml From 52f56baa8cf72153c35f2ec6d591a8e06906801f Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 26 Oct 2020 16:56:27 +0000 Subject: [PATCH 3/4] Remove specs with more than 100 lines but less than 300 from rubocop exceptions list --- .rubocop_manual_todo.yml | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/.rubocop_manual_todo.yml b/.rubocop_manual_todo.yml index 4b1c3662d0..dba500e10f 100644 --- a/.rubocop_manual_todo.yml +++ b/.rubocop_manual_todo.yml @@ -811,40 +811,17 @@ Metrics/ModuleLength: - app/helpers/spree/admin/navigation_helper.rb - app/models/spree/order/checkout.rb - app/models/spree/payment/processing.rb - - engines/order_management/spec/services/order_management/order/updater_spec.rb - - engines/order_management/spec/services/order_management/stock/package_spec.rb - - engines/order_management/spec/services/order_management/subscriptions/estimator_spec.rb - - engines/order_management/spec/services/order_management/subscriptions/form_spec.rb - engines/order_management/spec/services/order_management/subscriptions/proxy_order_syncer_spec.rb - - engines/order_management/spec/services/order_management/subscriptions/summarizer_spec.rb - engines/order_management/spec/services/order_management/subscriptions/validator_spec.rb - - engines/order_management/spec/services/order_management/subscriptions/variants_list_spec.rb - lib/open_food_network/column_preference_defaults.rb - spec/controllers/admin/order_cycles_controller_spec.rb - - spec/controllers/api/order_cycles_controller_spec.rb - spec/controllers/api/orders_controller_spec.rb - - spec/controllers/spree/admin/payment_methods_controller_spec.rb - - spec/lib/open_food_network/address_finder_spec.rb - - spec/lib/open_food_network/customers_report_spec.rb - - spec/lib/open_food_network/enterprise_fee_calculator_spec.rb - - spec/lib/open_food_network/order_cycle_form_applicator_spec.rb - spec/lib/open_food_network/order_cycle_permissions_spec.rb - - spec/lib/open_food_network/order_grouper_spec.rb - - spec/lib/open_food_network/packing_report_spec.rb - - spec/lib/open_food_network/permissions_spec.rb - - spec/lib/open_food_network/products_and_inventory_report_spec.rb - - spec/lib/open_food_network/scope_variant_to_hub_spec.rb - - spec/lib/open_food_network/tag_rule_applicator_spec.rb - - spec/lib/open_food_network/user_balance_calculator_spec.rb - - spec/lib/open_food_network/users_and_enterprises_report_spec.rb - spec/models/spree/adjustment_spec.rb - spec/models/spree/credit_card_spec.rb - spec/models/spree/line_item_spec.rb - spec/models/spree/product_spec.rb - - spec/models/spree/shipping_method_spec.rb - spec/models/spree/variant_spec.rb - - spec/services/permissions/order_spec.rb - - spec/services/variant_units/option_value_namer_spec.rb Metrics/ParameterLists: Max: 5 From c5186c2412cf836662809be8ac1dd462c407cf8f Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 26 Oct 2020 17:27:38 +0000 Subject: [PATCH 4/4] Fix rubocop_specs file path --- engines/catalog/spec/.rubocop.yml | 2 +- engines/dfc_provider/spec/.rubocop.yml | 2 +- engines/order_management/spec/.rubocop.yml | 2 +- engines/web/spec/.rubocop.yml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/engines/catalog/spec/.rubocop.yml b/engines/catalog/spec/.rubocop.yml index 5d20d5e3d1..26e3e2c99a 100644 --- a/engines/catalog/spec/.rubocop.yml +++ b/engines/catalog/spec/.rubocop.yml @@ -1,2 +1,2 @@ inherit_from: - - ../.rubocop_specs.yml + - ../../../.rubocop_specs.yml diff --git a/engines/dfc_provider/spec/.rubocop.yml b/engines/dfc_provider/spec/.rubocop.yml index 5d20d5e3d1..26e3e2c99a 100644 --- a/engines/dfc_provider/spec/.rubocop.yml +++ b/engines/dfc_provider/spec/.rubocop.yml @@ -1,2 +1,2 @@ inherit_from: - - ../.rubocop_specs.yml + - ../../../.rubocop_specs.yml diff --git a/engines/order_management/spec/.rubocop.yml b/engines/order_management/spec/.rubocop.yml index 5d20d5e3d1..26e3e2c99a 100644 --- a/engines/order_management/spec/.rubocop.yml +++ b/engines/order_management/spec/.rubocop.yml @@ -1,2 +1,2 @@ inherit_from: - - ../.rubocop_specs.yml + - ../../../.rubocop_specs.yml diff --git a/engines/web/spec/.rubocop.yml b/engines/web/spec/.rubocop.yml index 5d20d5e3d1..26e3e2c99a 100644 --- a/engines/web/spec/.rubocop.yml +++ b/engines/web/spec/.rubocop.yml @@ -1,2 +1,2 @@ inherit_from: - - ../.rubocop_specs.yml + - ../../../.rubocop_specs.yml