From a224c53200ddbd3e99f1ed165edee6acee842f18 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Sun, 9 Feb 2020 12:43:45 +0000 Subject: [PATCH] Add spec to test receiving a redirect from stripe with a valid payment intent id --- spec/controllers/checkout_controller_spec.rb | 38 +++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/spec/controllers/checkout_controller_spec.rb b/spec/controllers/checkout_controller_spec.rb index a4ba67715b..b11ca1ab78 100644 --- a/spec/controllers/checkout_controller_spec.rb +++ b/spec/controllers/checkout_controller_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe CheckoutController, type: :controller do - let(:distributor) { double(:distributor) } + let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) } let(:order_cycle) { create(:simple_order_cycle) } let(:order) { create(:order) } let(:reset_order_service) { double(ResetOrderService) } @@ -36,7 +36,7 @@ describe CheckoutController, type: :controller do expect(flash[:info]).to eq("The hub you have selected is temporarily closed for orders. Please try again later.") end - describe "redirection to the cart" do + describe "redirection to cart and stripe" do let(:order_cycle_distributed_variants) { double(:order_cycle_distributed_variants) } before do @@ -44,7 +44,7 @@ describe CheckoutController, type: :controller do allow(order).to receive(:distributor).and_return(distributor) order.order_cycle = order_cycle - allow(OrderCycleDistributedVariants).to receive(:new).with(order_cycle, distributor).and_return(order_cycle_distributed_variants) + allow(OrderCycleDistributedVariants).to receive(:new).and_return(order_cycle_distributed_variants) end it "redirects when some items are out of stock" do @@ -62,12 +62,34 @@ describe CheckoutController, type: :controller do expect(response).to redirect_to cart_path end - it "does not redirect when items are available and in stock" do - allow(order).to receive_message_chain(:insufficient_stock_lines, :empty?).and_return true - expect(order_cycle_distributed_variants).to receive(:distributes_order_variants?).with(order).and_return(true) + describe "when items are available and in stock" do + before do + allow(order).to receive_message_chain(:insufficient_stock_lines, :empty?).and_return true + end - get :edit - expect(response).to be_success + it "does not redirect" do + expect(order_cycle_distributed_variants).to receive(:distributes_order_variants?).with(order).and_return(true) + get :edit + expect(response).to be_success + end + + describe "when the order is in payment state and a stripe payment intent is provided" do + before do + order.update_attribute :state, "payment" + order.ship_address = create(:address) + order.save! + order.payments << create(:payment, state: "pending", response_code: "pi_123") + + # this is called a 2nd time after order completion from the reset_order_service + expect(order_cycle_distributed_variants).to receive(:distributes_order_variants?).twice.and_return(true) + end + + it "completes the order and redirects to the order confirmation page" do + get :edit, { payment_intent: "pi_123" } + expect(order.completed?).to be true + expect(response).to redirect_to spree.order_path(order) + end + end end end