From b2db63178f4c2edce4ba3ebd5735e975c37e6aa8 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Tue, 20 Dec 2022 11:09:34 +0100 Subject: [PATCH] Redirect `/checkout/whatever` to `/checkout` when split_checkout is activate + constraint the legacy checkout routes when `split_checkout` feature is disabled + add specs --- config/routes.rb | 12 +++-- lib/open_food_network/feature_toggle.rb | 4 ++ spec/requests/checkout/routes_spec.rb | 64 +++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 spec/requests/checkout/routes_spec.rb diff --git a/config/routes.rb b/config/routes.rb index e1bc3394d1..9512b55f33 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -91,11 +91,17 @@ Openfoodnetwork::Application.routes.draw do get '/checkout/:step', to: 'split_checkout#edit', as: :checkout_step put '/checkout/:step', to: 'split_checkout#update', as: :checkout_update end + + # Redirects to the new checkout for any other 'step' (ie. /checkout/cart from the legacy checkout) + get '/checkout/:other', to: redirect('/checkout') end - get '/checkout', to: 'checkout#edit' - put '/checkout', to: 'checkout#update', as: :update_checkout - get '/checkout/:state', to: 'checkout#edit', as: :checkout_state + # When the split_checkout feature is disabled for the current user, use the legacy checkout + constraints ->(request) { OpenFoodNetwork::FeatureToggle.disabled? :split_checkout, request.env['warden']&.user } do + get '/checkout', to: 'checkout#edit' + put '/checkout', to: 'checkout#update', as: :update_checkout + get '/checkout/:state', to: 'checkout#edit', as: :checkout_state + end get 'embedded_shopfront/shopfront_session', to: 'application#shopfront_session' post 'embedded_shopfront/enable', to: 'application#enable_embedded_styles' diff --git a/lib/open_food_network/feature_toggle.rb b/lib/open_food_network/feature_toggle.rb index dd45753b16..888342fab3 100644 --- a/lib/open_food_network/feature_toggle.rb +++ b/lib/open_food_network/feature_toggle.rb @@ -12,5 +12,9 @@ module OpenFoodNetwork feature.add unless feature.exist? feature.enabled?(user) end + + def self.disabled?(feature_name, user = nil) + !enabled?(feature_name, user) + end end end diff --git a/spec/requests/checkout/routes_spec.rb b/spec/requests/checkout/routes_spec.rb new file mode 100644 index 0000000000..d0f37c5b6a --- /dev/null +++ b/spec/requests/checkout/routes_spec.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'checkout endpoints', type: :request do + include ShopWorkflow + + let!(:shop) { create(:enterprise) } + let!(:order_cycle) { create(:simple_order_cycle) } + let!(:exchange) { + create(:exchange, order_cycle: order_cycle, sender: order_cycle.coordinator, receiver: shop, + incoming: false, pickup_time: "Monday") + } + let!(:line_item) { create(:line_item, order: order, quantity: 3, price: 5.00) } + let!(:payment_method) { + create(:bogus_payment_method, distributor_ids: [shop.id], environment: Rails.env) + } + let!(:check_payment_method) { + create(:payment_method, distributor_ids: [shop.id], environment: Rails.env) + } + let!(:shipping_method) { create(:shipping_method, distributor_ids: [shop.id]) } + let!(:shipment) { create(:shipment_with, :shipping_method, shipping_method: shipping_method) } + let!(:order) { + create(:order, shipments: [shipment], distributor: shop, order_cycle: order_cycle) + } + + before do + order_cycle_distributed_variants = double(:order_cycle_distributed_variants) + allow(OrderCycleDistributedVariants).to receive(:new) + .and_return(order_cycle_distributed_variants) + allow(order_cycle_distributed_variants).to receive(:distributes_order_variants?) + .and_return(true) + + set_order order + end + + context "when getting the cart `/checkout/cart`" do + let(:path) { "/checkout/cart" } + + context "using the legacy checkout" do + it "do not redirect" do + get path + puts response.redirect_url + expect(response.status).to eq(200) + end + end + + context "using the split checkout" do + before do + # feature toggle is enabled + Flipper.enable(:split_checkout) + end + + it "redirect to the split checkout" do + get path + expect(response.status).to redirect_to("/checkout") + + # follow the redirect + get response.redirect_url + expect(response.status).to redirect_to("/checkout/details") + end + end + end +end