From 58c3c49cfbc2c6f1a42222f9a51cd7f11c406d57 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Fri, 12 Mar 2021 22:14:02 +0000 Subject: [PATCH 1/4] Adapt to new rspec syntax --- spec/controllers/admin/matomo_settings_controller_spec.rb | 2 +- spec/controllers/api/orders_controller_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/controllers/admin/matomo_settings_controller_spec.rb b/spec/controllers/admin/matomo_settings_controller_spec.rb index 8644557a58..0bc9b35a16 100644 --- a/spec/controllers/admin/matomo_settings_controller_spec.rb +++ b/spec/controllers/admin/matomo_settings_controller_spec.rb @@ -20,7 +20,7 @@ describe Admin::MatomoSettingsController, type: :controller do it "changes Matomo settings" do expect { - post :update, params + post :update, params: params }.to change { [ Spree::Config[:matomo_url], diff --git a/spec/controllers/api/orders_controller_spec.rb b/spec/controllers/api/orders_controller_spec.rb index 2a5421e9cb..2dfd2dd035 100644 --- a/spec/controllers/api/orders_controller_spec.rb +++ b/spec/controllers/api/orders_controller_spec.rb @@ -133,7 +133,7 @@ module Api end it 'can show only completed orders' do - get :index, format: :json, q: { completed_at_not_null: true, s: 'created_at desc' } + get :index, params: { format: :json, q: { completed_at_not_null: true, s: 'created_at desc' } } expect(json_response['orders']).to eq serialized_orders([order4, order3, order2, order1]) end @@ -145,7 +145,7 @@ module Api end it 'returns pagination data when query params contain :per_page]' do - get :index, per_page: 15, page: 1 + get :index, params: { per_page: 15, page: 1 } pagination_data = { 'results' => 2, From f710bbed3efd47795f0fa99026b321cc70bf77a3 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 13 Mar 2021 12:08:10 +0000 Subject: [PATCH 2/4] Convert calls to xhr to post or put with xhr: true --- .../admin/bulk_line_items_controller_spec.rb | 2 +- .../api/product_images_controller_spec.rb | 4 ++-- spec/controllers/cart_controller_spec.rb | 14 +++++++------- spec/controllers/enterprises_controller_spec.rb | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/spec/controllers/admin/bulk_line_items_controller_spec.rb b/spec/controllers/admin/bulk_line_items_controller_spec.rb index 4d94591a0e..cdc1d8c0cc 100644 --- a/spec/controllers/admin/bulk_line_items_controller_spec.rb +++ b/spec/controllers/admin/bulk_line_items_controller_spec.rb @@ -237,7 +237,7 @@ describe Admin::BulkLineItemsController, type: :controller do context "hub enterprise" do before do allow(controller).to receive_messages spree_current_user: distributor1.owner - xhr :put, :update, params + put :update, params: params, xhr: true end it "updates the line item" do diff --git a/spec/controllers/api/product_images_controller_spec.rb b/spec/controllers/api/product_images_controller_spec.rb index 95ee3bd3de..b4858ae49e 100644 --- a/spec/controllers/api/product_images_controller_spec.rb +++ b/spec/controllers/api/product_images_controller_spec.rb @@ -19,14 +19,14 @@ module Api let(:current_api_user) { create(:admin_user) } it "saves a new image when none is present" do - xhr :post, :update_product_image, product_id: product_without_image.id, file: image, use_route: :product_images + post :update_product_image, xhr: true, params: { product_id: product_without_image.id, file: image, use_route: :product_images } expect(response.status).to eq 201 expect(product_without_image.images.first.id).to eq json_response['id'] end it "updates an existing product image" do - xhr :post, :update_product_image, product_id: product_with_image.id, file: image, use_route: :product_images + post :update_product_image, xhr: true, params: { product_id: product_with_image.id, file: image, use_route: :product_images } expect(response.status).to eq 200 expect(product_with_image.images.first.id).to eq json_response['id'] diff --git a/spec/controllers/cart_controller_spec.rb b/spec/controllers/cart_controller_spec.rb index b3f6d46c8f..836f7fa5da 100644 --- a/spec/controllers/cart_controller_spec.rb +++ b/spec/controllers/cart_controller_spec.rb @@ -17,7 +17,7 @@ describe CartController, type: :controller do allow(cart_service).to receive(:populate) { true } allow(cart_service).to receive(:valid?) { true } allow(cart_service).to receive(:variants_h) { {} } - xhr :post, :populate, use_route: :spree, format: :json + post :populate, xhr: true, params: { use_route: :spree, format: :json } expect(response.status).to eq(200) end @@ -26,7 +26,7 @@ describe CartController, type: :controller do allow(cart_service).to receive(:valid?) { false } allow(cart_service).to receive(:errors) { errors } allow(errors).to receive(:full_messages).and_return(["Error: foo"]) - xhr :post, :populate, use_route: :spree, format: :json + post :populate, xhr: true, params: { use_route: :spree, format: :json } expect(response.status).to eq(412) end @@ -34,7 +34,7 @@ describe CartController, type: :controller do allow(cart_service).to receive(:variants_h) { {} } allow(cart_service).to receive(:valid?) { true } expect(cart_service).to receive(:populate).with({}, true) - xhr :post, :populate, use_route: :spree, format: :json + post :populate, xhr: true, params: { use_route: :spree, format: :json } end it "returns stock levels as JSON on success" do @@ -44,7 +44,7 @@ describe CartController, type: :controller do allow(cart_service).to receive(:valid?) { true } allow(cart_service).to receive(:variants_h) { {} } - xhr :post, :populate, use_route: :spree, format: :json + post :populate, xhr: true, params: { use_route: :spree, format: :json } data = JSON.parse(response.body) expect(data['stock_levels']).to eq('my_stock_levels') @@ -82,7 +82,7 @@ describe CartController, type: :controller do end it "returns the variant override stock levels of the variant in the order" do - spree_post :populate, variants: { variant_in_the_order.id => 1 } + spree_post :populate, params: { variants: { variant_in_the_order.id => 1 } } data = JSON.parse(response.body) expect(data['stock_levels'][variant_in_the_order.id.to_s]["on_hand"]).to eq 20 @@ -93,7 +93,7 @@ describe CartController, type: :controller do # If the variant was not added to the order, VariantsStockLevels alternative calculation would fail # See #3222 for more details # This indicates that the VariantsStockLevels alternative calculation is never reached - spree_post :populate, variants: { variant_not_in_the_order.id => 1 } + spree_post :populate, params: { variants: { variant_not_in_the_order.id => 1 } } data = JSON.parse(response.body) expect(data['stock_levels'][variant_not_in_the_order.id.to_s]["on_hand"]).to eq 7 @@ -114,7 +114,7 @@ describe CartController, type: :controller do allow(controller).to receive(:current_order).and_return(order) expect do - spree_post :populate, variants: { variant.id => 1 }, variant_attributes: { variant.id => { max_quantity: "3" } } + spree_post :populate, params: { variants: { variant.id => 1 }, variant_attributes: { variant.id => { max_quantity: "3" } } } end.to change(Spree::LineItem, :count).by(1) end end diff --git a/spec/controllers/enterprises_controller_spec.rb b/spec/controllers/enterprises_controller_spec.rb index 86ff995516..cdfa2ef964 100644 --- a/spec/controllers/enterprises_controller_spec.rb +++ b/spec/controllers/enterprises_controller_spec.rb @@ -150,7 +150,7 @@ describe EnterprisesController, type: :controller do # let(:enterprise) { create(:enterprise, permalink: 'enterprise_permalink') } it "responds with status of 200 when the route does not exist" do - xhr :get, :check_permalink, permalink: 'some_nonexistent_route', format: :js + get :check_permalink, xhr: true, params: { permalink: 'some_nonexistent_route', format: :js } expect(response.status).to be 200 end From 25e0262364648cb6a18ce0205fec17e701129d8d Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 13 Mar 2021 12:12:16 +0000 Subject: [PATCH 3/4] Adapt spec to new rspec syntax with params and no xhr --- .../enterprises_controller_spec.rb | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/spec/controllers/enterprises_controller_spec.rb b/spec/controllers/enterprises_controller_spec.rb index cdfa2ef964..1170b7de6e 100644 --- a/spec/controllers/enterprises_controller_spec.rb +++ b/spec/controllers/enterprises_controller_spec.rb @@ -18,7 +18,7 @@ describe EnterprisesController, type: :controller do end it "sets the shop as the distributor on the order when shopping for the distributor" do - get :shop, id: distributor + get :shop, params: { id: distributor } expect(controller.current_distributor).to eq(distributor) expect(controller.current_order.distributor).to eq(distributor) @@ -29,7 +29,7 @@ describe EnterprisesController, type: :controller do before { allow(controller).to receive(:spree_current_user) { user } } it "sets the shop as the distributor on the order when shopping for the distributor" do - get :shop, id: distributor + get :shop, params: { id: distributor } expect(controller.current_distributor).to eq(distributor) expect(controller.current_order.distributor).to eq(distributor) @@ -39,11 +39,11 @@ describe EnterprisesController, type: :controller do it "sorts order cycles by the distributor's preferred ordering attr" do distributor.update_attribute(:preferred_shopfront_order_cycle_order, 'orders_close_at') - get :shop, id: distributor + get :shop, params: { id: distributor } expect(assigns(:order_cycles)).to eq([order_cycle1, order_cycle2].sort_by(&:orders_close_at)) distributor.update_attribute(:preferred_shopfront_order_cycle_order, 'orders_open_at') - get :shop, id: distributor + get :shop, params: { id: distributor } expect(assigns(:order_cycles)).to eq([order_cycle1, order_cycle2].sort_by(&:orders_open_at)) end @@ -64,23 +64,23 @@ describe EnterprisesController, type: :controller do preferred_exchange_tags: "wholesale", preferred_matched_order_cycles_visibility: 'hidden') - get :shop, id: distributor + get :shop, params: { id: distributor } expect(assigns(:order_cycles)).to include order_cycle1, order_cycle2, order_cycle3 allow(controller).to receive(:spree_current_user) { user } - get :shop, id: distributor + get :shop, params: { id: distributor } expect(assigns(:order_cycles)).to include order_cycle1, order_cycle2, order_cycle3 oc3_exchange.update_attribute(:tag_list, "wholesale") - get :shop, id: distributor + get :shop, params: { id: distributor } expect(assigns(:order_cycles)).to include order_cycle1, order_cycle2 expect(assigns(:order_cycles)).not_to include order_cycle3 customer.update_attribute(:tag_list, ["wholesale"]) - get :shop, id: distributor + get :shop, params: { id: distributor } expect(assigns(:order_cycles)).to include order_cycle1, order_cycle2, order_cycle3 end end @@ -89,7 +89,7 @@ describe EnterprisesController, type: :controller do line_item = create(:line_item) controller.current_order.line_items << line_item - get :shop, id: distributor + get :shop, params: { id: distributor } expect(controller.current_order.distributor).to eq(distributor) expect(controller.current_order.order_cycle).to be_nil @@ -97,7 +97,7 @@ describe EnterprisesController, type: :controller do end it "should not empty an order if returning to the same distributor" do - get :shop, id: current_distributor + get :shop, params: { id: current_distributor } expect(controller.current_order.distributor).to eq current_distributor expect(controller.current_order.line_items.first.variant).to eq line_item.variant @@ -117,7 +117,7 @@ describe EnterprisesController, type: :controller do end it "redirects to the cart" do - get :shop, id: current_distributor + get :shop, params: { id: current_distributor } expect(response).to redirect_to cart_path end @@ -129,7 +129,7 @@ describe EnterprisesController, type: :controller do order.save order_cycle1.update_attribute :orders_close_at, Time.zone.now - get :shop, id: distributor + get :shop, params: { id: distributor } expect(controller.current_order.distributor).to eq(distributor) expect(controller.current_order.order_cycle).to eq(order_cycle2) @@ -139,7 +139,7 @@ describe EnterprisesController, type: :controller do it "sets order cycle if only one is available at the chosen distributor" do order_cycle2.destroy - get :shop, id: distributor + get :shop, params: { id: distributor } expect(controller.current_order.distributor).to eq(distributor) expect(controller.current_order.order_cycle).to eq(order_cycle1) @@ -157,16 +157,16 @@ describe EnterprisesController, type: :controller do it "responds with status of 409 when the permalink matches an existing route" do # get :check_permalink, { permalink: 'enterprise_permalink', format: :js } # expect(response.status).to be 409 - xhr :get, :check_permalink, permalink: 'map', format: :js + get :check_permalink, xhr: true, params: { permalink: 'map', format: :js } expect(response.status).to be 409 - xhr :get, :check_permalink, permalink: '', format: :js + get :check_permalink, xhr: true, params: { permalink: '', format: :js } expect(response.status).to be 409 end end context "checking access on nonexistent enterprise" do before do - get :shop, id: "some_nonexistent_enterprise" + get :shop, params: { id: "some_nonexistent_enterprise" } end it "redirects to shops_path" do From e2ce6634df39909b73258cfd6f835edde7e1fe84 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sat, 13 Mar 2021 12:18:30 +0000 Subject: [PATCH 4/4] Adapt specs to new rspec syntax without xhr --- spec/controllers/cart_controller_spec.rb | 6 +++--- spec/controllers/user_passwords_controller_spec.rb | 4 ++-- spec/controllers/user_registrations_controller_spec.rb | 8 ++++---- spec/performance/shop_controller_spec.rb | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/spec/controllers/cart_controller_spec.rb b/spec/controllers/cart_controller_spec.rb index 836f7fa5da..83cbee70e5 100644 --- a/spec/controllers/cart_controller_spec.rb +++ b/spec/controllers/cart_controller_spec.rb @@ -82,7 +82,7 @@ describe CartController, type: :controller do end it "returns the variant override stock levels of the variant in the order" do - spree_post :populate, params: { variants: { variant_in_the_order.id => 1 } } + spree_post :populate, variants: { variant_in_the_order.id => 1 } data = JSON.parse(response.body) expect(data['stock_levels'][variant_in_the_order.id.to_s]["on_hand"]).to eq 20 @@ -93,7 +93,7 @@ describe CartController, type: :controller do # If the variant was not added to the order, VariantsStockLevels alternative calculation would fail # See #3222 for more details # This indicates that the VariantsStockLevels alternative calculation is never reached - spree_post :populate, params: { variants: { variant_not_in_the_order.id => 1 } } + spree_post :populate, variants: { variant_not_in_the_order.id => 1 } data = JSON.parse(response.body) expect(data['stock_levels'][variant_not_in_the_order.id.to_s]["on_hand"]).to eq 7 @@ -114,7 +114,7 @@ describe CartController, type: :controller do allow(controller).to receive(:current_order).and_return(order) expect do - spree_post :populate, params: { variants: { variant.id => 1 }, variant_attributes: { variant.id => { max_quantity: "3" } } } + spree_post :populate, variants: { variant.id => 1 }, variant_attributes: { variant.id => { max_quantity: "3" } } end.to change(Spree::LineItem, :count).by(1) end end diff --git a/spec/controllers/user_passwords_controller_spec.rb b/spec/controllers/user_passwords_controller_spec.rb index 72e399adf2..f66c7d1bf8 100644 --- a/spec/controllers/user_passwords_controller_spec.rb +++ b/spec/controllers/user_passwords_controller_spec.rb @@ -49,13 +49,13 @@ describe UserPasswordsController, type: :controller do describe "via ajax" do it "returns error when email not found" do - xhr :post, :create, spree_user: {}, use_route: :spree + post :create, xhr: true, params: { spree_user: {}, use_route: :spree } expect(response.status).to eq 404 expect(json_response).to eq 'error' => I18n.t('email_not_found') end it "returns error when user is unconfirmed" do - xhr :post, :create, spree_user: { email: unconfirmed_user.email }, use_route: :spree + post :create, xhr: true, params: { spree_user: { email: unconfirmed_user.email }, use_route: :spree } expect(response.status).to eq 401 expect(json_response).to eq 'error' => I18n.t('email_unconfirmed') end diff --git a/spec/controllers/user_registrations_controller_spec.rb b/spec/controllers/user_registrations_controller_spec.rb index b659266c64..9e77be34f2 100644 --- a/spec/controllers/user_registrations_controller_spec.rb +++ b/spec/controllers/user_registrations_controller_spec.rb @@ -25,7 +25,7 @@ describe UserRegistrationsController, type: :controller do end it "returns validation errors" do - xhr :post, :create, spree_user: {}, use_route: :spree + post :create, xhr: true, params: { spree_user: {}, use_route: :spree } expect(response.status).to eq(401) json = JSON.parse(response.body) expect(json).to eq("email" => ["can't be blank"], "password" => ["can't be blank"]) @@ -35,7 +35,7 @@ describe UserRegistrationsController, type: :controller do allow(Spree::UserMailer).to receive(:confirmation_instructions).and_raise("Some error") expect(OpenFoodNetwork::ErrorLogger).to receive(:notify) - xhr :post, :create, spree_user: user_params, use_route: :spree + post :create, xhr: true, params: { spree_user: user_params, use_route: :spree } expect(response.status).to eq(401) json = JSON.parse(response.body) @@ -43,7 +43,7 @@ describe UserRegistrationsController, type: :controller do end it "returns 200 when registration succeeds" do - xhr :post, :create, spree_user: user_params, use_route: :spree + post :create, xhr: true, params: { spree_user: user_params, use_route: :spree } expect(response.status).to eq(200) json = JSON.parse(response.body) expect(json).to eq("email" => "test@test.com") @@ -55,7 +55,7 @@ describe UserRegistrationsController, type: :controller do original_locale_cookie = cookies[:locale] cookies[:locale] = "pt" - xhr :post, :create, spree_user: user_params, use_route: :spree + post :create, xhr: true, params: { spree_user: user_params, use_route: :spree } expect(assigns[:user].locale).to eq("pt") I18n.locale = original_i18n_locale diff --git a/spec/performance/shop_controller_spec.rb b/spec/performance/shop_controller_spec.rb index 6a052e67dc..ba116dbc66 100644 --- a/spec/performance/shop_controller_spec.rb +++ b/spec/performance/shop_controller_spec.rb @@ -37,7 +37,7 @@ describe ShopController, type: :controller, performance: true do it "returns products via json" do results = multi_benchmark(3, cache_key_patterns: cache_key_patterns) do - xhr :get, :products + get :products, xhr: true expect(response).to be_success end end