<commit_before># frozen_string_literal: true

require 'spec_helper'

describe SplitCheckoutController, type: :controller do
  let(:user) { order.user }
  let(:address) { create(:address) }
  let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
  let(:order_cycle) { create(:order_cycle, distributors: [distributor]) }
  let(:exchange) { order_cycle.exchanges.outgoing.first }
  let(:order) {
    create(:order_with_line_items, line_items_count: 1, distributor: distributor,
                                   order_cycle: order_cycle)
  }
  let(:payment_method) { distributor.payment_methods.first }
  let(:shipping_method) { distributor.shipping_methods.first }

  before do
    allow(Flipper).to receive(:enabled?).with(:split_checkout) { true }
    allow(Flipper).to receive(:enabled?).with(:split_checkout, anything) { true }

    exchange.variants << order.line_items.first.variant
    allow(controller).to receive(:current_order) { order }
    allow(controller).to receive(:spree_current_user) { user }
  end

  describe "#edit" do
    it "renders the checkout" do
      get :edit, params: { step: "details" }
      expect(response.status).to eq 200
    end

    it "redirects to current step if no step is given" do
      get :edit
      expect(response).to redirect_to checkout_step_path(:details)
    end

    context "when line items in the cart are not valid" do
      before { allow(controller).to receive(:valid_order_line_items?) { false } }

      it "redirects to cart" do
        get :edit
        expect(response).to redirect_to cart_path
      end
    end

    context "when the given `step` params is inconsistent with the current order state" do
      context "when order state is `cart`" do
        before do
          order.update!(state: "cart")
        end

        it "redirects to the valid step if params is `payment`" do
          get :edit, params: { step: "payment" }
          expect(response).to redirect_to checkout_step_path(:details)
        end
        it "redirects to the valid step if params is `summary`" do
          get :edit, params: { step: "summary" }
          expect(response).to redirect_to checkout_step_path(:details)
        end
      end

      context "when order state is `payment`" do
        before do
          order.update!(state: "payment")
        end

        it "redirects to the valid step if params is `summary`" do
          get :edit, params: { step: "summary" }
          expect(response).to redirect_to checkout_step_path(:payment)
        end
      end
    end
  end

  describe "#update" do
    let(:checkout_params) { {} }
    let(:params) { { step: step }.merge(checkout_params) }

    context "details step" do
      let(:step) { "details" }

      context "with incomplete data" do
        let(:checkout_params) { { order: { email: user.email } } }

        it "returns 422 and some feedback" do
          put :update, params: params

          expect(response.status).to eq 422
          expect(flash[:error]).to eq "Saving failed, please update the highlighted fields."
          expect(order.reload.state).to eq "cart"
        end
      end

      context "with complete data" do
        let(:checkout_params) do
          {
            order: {
              email: user.email,
              bill_address_attributes: address.to_param,
              ship_address_attributes: address.to_param
            },
            shipping_method_id: shipping_method.id
          }
        end

        it "updates and redirects to payment step" do
          put :update, params: params

          expect(response).to redirect_to checkout_step_path(:payment)
          expect(order.reload.state).to eq "payment"
        end

        describe "saving default addresses" do
          it "don't updates default bill address on user" do
            expect {
              put :update, params: params.merge({ order: { save_bill_address: "0" } })
            }.to_not change {
              order.user.reload.bill_address
            }
          end

          it "updates default bill address on user and customer" do
            put :update, params: params.merge({ order: { save_bill_address: "1" } })

            expect(order.customer.bill_address).to eq(order.bill_address)
            expect(order.user.bill_address).to eq(order.bill_address)
          end

          it "don't updates default ship address on user" do
            expect {
              put :update, params: params.merge({ order: { save_bill_address: "0" } })
            }.to_not change {
              order.user.reload.ship_address
            }
          end

          it "updates default ship address on user and customer" do
            put :update, params: params.merge({ order: { save_ship_address: "1" } })

            expect(order.customer.ship_address).to eq(order.ship_address)
            expect(order.user.ship_address).to eq(order.ship_address)
          end
        end
      end
    end

    context "payment step" do
      let(:step) { "payment" }

      before do
        order.bill_address = address
        order.ship_address = address
        order.select_shipping_method shipping_method.id
        OrderWorkflow.new(order).advance_to_payment
      end

      context "with incomplete data" do
        let(:checkout_params) { { order: { email: user.email } } }

        it "returns 422 and some feedback" do<commit_msg>Correct typo in spec which lead to flakiness<commit_after># frozen_string_literal: true

require 'spec_helper'

describe SplitCheckoutController, type: :controller do
  let(:user) { order.user }
  let(:address) { create(:address) }
  let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
  let(:order_cycle) { create(:order_cycle, distributors: [distributor]) }
  let(:exchange) { order_cycle.exchanges.outgoing.first }
  let(:order) {
    create(:order_with_line_items, line_items_count: 1, distributor: distributor,
                                   order_cycle: order_cycle)
  }
  let(:payment_method) { distributor.payment_methods.first }
  let(:shipping_method) { distributor.shipping_methods.first }

  before do
    allow(Flipper).to receive(:enabled?).with(:split_checkout) { true }
    allow(Flipper).to receive(:enabled?).with(:split_checkout, anything) { true }

    exchange.variants << order.line_items.first.variant
    allow(controller).to receive(:current_order) { order }
    allow(controller).to receive(:spree_current_user) { user }
  end

  describe "#edit" do
    it "renders the checkout" do
      get :edit, params: { step: "details" }
      expect(response.status).to eq 200
    end

    it "redirects to current step if no step is given" do
      get :edit
      expect(response).to redirect_to checkout_step_path(:details)
    end

    context "when line items in the cart are not valid" do
      before { allow(controller).to receive(:valid_order_line_items?) { false } }

      it "redirects to cart" do
        get :edit
        expect(response).to redirect_to cart_path
      end
    end

    context "when the given `step` params is inconsistent with the current order state" do
      context "when order state is `cart`" do
        before do
          order.update!(state: "cart")
        end

        it "redirects to the valid step if params is `payment`" do
          get :edit, params: { step: "payment" }
          expect(response).to redirect_to checkout_step_path(:details)
        end
        it "redirects to the valid step if params is `summary`" do
          get :edit, params: { step: "summary" }
          expect(response).to redirect_to checkout_step_path(:details)
        end
      end

      context "when order state is `payment`" do
        before do
          order.update!(state: "payment")
        end

        it "redirects to the valid step if params is `summary`" do
          get :edit, params: { step: "summary" }
          expect(response).to redirect_to checkout_step_path(:payment)
        end
      end
    end
  end

  describe "#update" do
    let(:checkout_params) { {} }
    let(:params) { { step: step }.merge(checkout_params) }

    context "details step" do
      let(:step) { "details" }

      context "with incomplete data" do
        let(:checkout_params) { { order: { email: user.email } } }

        it "returns 422 and some feedback" do
          put :update, params: params

          expect(response.status).to eq 422
          expect(flash[:error]).to eq "Saving failed, please update the highlighted fields."
          expect(order.reload.state).to eq "cart"
        end
      end

      context "with complete data" do
        let(:checkout_params) do
          {
            order: {
              email: user.email,
              bill_address_attributes: address.to_param,
              ship_address_attributes: address.to_param
            },
            shipping_method_id: shipping_method.id
          }
        end

        it "updates and redirects to payment step" do
          put :update, params: params

          expect(response).to redirect_to checkout_step_path(:payment)
          expect(order.reload.state).to eq "payment"
        end

        describe "saving default addresses" do
          it "don't updates default bill address on user" do
            expect {
              put :update, params: params.merge({ order: { save_bill_address: "0" } })
            }.to_not change {
              order.user.reload.bill_address
            }
          end

          it "updates default bill address on user and customer" do
            put :update, params: params.merge({ order: { save_bill_address: "1" } })

            expect(order.customer.bill_address).to eq(order.bill_address)
            expect(order.user.bill_address).to eq(order.bill_address)
          end

          it "don't updates default ship address on user" do
            expect {
              put :update, params: params.merge({ order: { save_ship_address: "0" } })
            }.to_not change {
              order.user.reload.ship_address
            }
          end

          it "updates default ship address on user and customer" do
            put :update, params: params.merge({ order: { save_ship_address: "1" } })

            expect(order.customer.ship_address).to eq(order.ship_address)
            expect(order.user.ship_address).to eq(order.ship_address)
          end
        end
      end
    end

    context "payment step" do
      let(:step) { "payment" }

      before do
        order.bill_address = address
        order.ship_address = address
        order.select_shipping_method shipping_method.id
        OrderWorkflow.new(order).advance_to_payment
      end

      context "with incomplete data" do
        let(:checkout_params) { { order: { email: user.email } } }

        it "returns 422 and some feedback" do
{'fraction_non_alphanumeric': 0.09510644180259883, 'fraction_numerical': 0.003133351764814303, 'mean_word_length': 2.330877839165132, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5622720', 'n_tokens_mistral': 3129, 'n_tokens_neox': 2978, 'n_words': 866}