From ef7a02004e68109df6f48ce7dbf7b343b781d118 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 6 Dec 2021 17:44:04 +0000 Subject: [PATCH] Update specs --- spec/controllers/checkout_controller_spec.rb | 24 ++----- spec/models/spree/gateway/stripe_sca_spec.rb | 63 ++++++++++++------- .../services/checkout/paypal_redirect_spec.rb | 47 -------------- .../services/checkout/stripe_redirect_spec.rb | 57 +++++++---------- 4 files changed, 66 insertions(+), 125 deletions(-) delete mode 100644 spec/services/checkout/paypal_redirect_spec.rb diff --git a/spec/controllers/checkout_controller_spec.rb b/spec/controllers/checkout_controller_spec.rb index 4c413727d4..a5be72e102 100644 --- a/spec/controllers/checkout_controller_spec.rb +++ b/spec/controllers/checkout_controller_spec.rb @@ -418,28 +418,12 @@ describe CheckoutController, type: :controller do allow(order).to receive(:state) { "payment" } end - describe "paypal redirect" do - let(:payment_method) { create(:payment_method, type: "Spree::Gateway::PayPalExpress") } - let(:paypal_redirect) { instance_double(Checkout::PaypalRedirect) } - - it "should call Paypal redirect and redirect if a path is provided" do - expect(Checkout::PaypalRedirect).to receive(:new).and_return(paypal_redirect) - expect(paypal_redirect).to receive(:path).and_return("test_path") - - spree_post :update, - order: { payments_attributes: [{ payment_method_id: payment_method.id }] } - - expect(response.body).to eq({ path: "test_path" }.to_json) - end - end - - describe "stripe redirect" do - let(:payment_method) { create(:payment_method, type: "Spree::Gateway::StripeSCA") } - let(:stripe_redirect) { instance_double(Checkout::StripeRedirect) } + describe "redirecting to an external payment gateway" do + let(:payment_method) { create(:payment_method) } it "should call Stripe redirect and redirect if a path is provided" do - expect(Checkout::StripeRedirect).to receive(:new).and_return(stripe_redirect) - expect(stripe_redirect).to receive(:path).and_return("test_path") + expect(Spree::PaymentMethod).to receive(:find).and_return(payment_method) + expect(payment_method).to receive(:external_payment_url).and_return("test_path") spree_post :update, order: { payments_attributes: [{ payment_method_id: payment_method.id }] } diff --git a/spec/models/spree/gateway/stripe_sca_spec.rb b/spec/models/spree/gateway/stripe_sca_spec.rb index bf8b29d9b8..e9c7042233 100644 --- a/spec/models/spree/gateway/stripe_sca_spec.rb +++ b/spec/models/spree/gateway/stripe_sca_spec.rb @@ -5,30 +5,30 @@ require 'spec_helper' describe Spree::Gateway::StripeSCA, type: :model do before { Stripe.api_key = "sk_test_12345" } - describe "#purchase" do - let(:order) { create(:order_with_totals_and_distribution) } - let(:credit_card) { create(:credit_card) } - let(:payment) { - create( - :payment, - state: "checkout", - order: order, - amount: order.total, - payment_method: subject, - source: credit_card, - response_code: "12345" - ) - } - let(:gateway_options) { - { order_id: order.number } - } - let(:payment_authorised) { - payment_intent(payment.amount, "requires_capture") - } - let(:capture_successful) { - payment_intent(payment.amount, "succeeded") - } + let(:order) { create(:order_with_totals_and_distribution) } + let(:credit_card) { create(:credit_card) } + let(:payment) { + create( + :payment, + state: "checkout", + order: order, + amount: order.total, + payment_method: subject, + source: credit_card, + response_code: "12345" + ) + } + let(:gateway_options) { + { order_id: order.number } + } + let(:payment_authorised) { + payment_intent(payment.amount, "requires_capture") + } + let(:capture_successful) { + payment_intent(payment.amount, "succeeded") + } + describe "#purchase" do it "captures the payment" do stub_request(:get, "https://api.stripe.com/v1/payment_intents/12345"). to_return(status: 200, body: payment_authorised) @@ -81,6 +81,23 @@ describe Spree::Gateway::StripeSCA, type: :model do end end + describe "#external_payment_url" do + let(:redirect_double) { instance_double(Checkout::StripeRedirect) } + + it "returns nil when an order is not supplied" do + expect(subject.external_payment_url({})).to eq nil + end + + it "calls Checkout::StripeRedirect" do + expect(Checkout::StripeRedirect).to receive(:new).with(subject, order) { redirect_double } + expect(redirect_double).to receive(:path).and_return("http://stripe-test.org") + + expect(subject.external_payment_url(order: order)).to eq "http://stripe-test.org" + end + end + + private + def payment_intent(amount, status) JSON.generate( object: "payment_intent", diff --git a/spec/services/checkout/paypal_redirect_spec.rb b/spec/services/checkout/paypal_redirect_spec.rb deleted file mode 100644 index e028be4486..0000000000 --- a/spec/services/checkout/paypal_redirect_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe Checkout::PaypalRedirect do - describe '#path' do - let(:params) { { order: { order_id: "123" } } } - - let(:redirect) { Checkout::PaypalRedirect.new(params) } - - it "returns nil if payment_attributes are not provided" do - expect(redirect.path).to be nil - end - - describe "when payment_attributes are provided" do - it "raises an error if payment method does not exist" do - params[:order][:payments_attributes] = [{ payment_method_id: "123" }] - - expect { redirect.path }.to raise_error ActiveRecord::RecordNotFound - end - - describe "when payment method provided exists" do - before { params[:order][:payments_attributes] = [{ payment_method_id: payment_method.id }] } - - describe "and the payment method is not a paypal payment method" do - let(:payment_method) { create(:payment_method) } - - it "returns nil" do - expect(redirect.path).to be nil - end - end - - describe "and the payment method is a paypal method" do - let(:distributor) { create(:distributor_enterprise) } - let(:payment_method) do - Spree::Gateway::PayPalExpress.create!(name: "PayPalExpress", - distributor_ids: [distributor.id]) - end - - it "returns the redirect path" do - expect(redirect.path).to include payment_method.id.to_s - end - end - end - end - end -end diff --git a/spec/services/checkout/stripe_redirect_spec.rb b/spec/services/checkout/stripe_redirect_spec.rb index 6d5830e3c9..164151e145 100644 --- a/spec/services/checkout/stripe_redirect_spec.rb +++ b/spec/services/checkout/stripe_redirect_spec.rb @@ -5,53 +5,40 @@ require 'spec_helper' describe Checkout::StripeRedirect do describe '#path' do let(:order) { create(:order) } - let(:params) { { order: { order_id: order.id } } } + let(:service) { Checkout::StripeRedirect.new(payment_method, order) } - let(:redirect) { Checkout::StripeRedirect.new(params, order) } + context "when the given payment method is not Stripe SCA" do + let(:payment_method) { build(:payment_method) } - it "returns nil if payment_attributes are not provided" do - expect(redirect.path).to be nil + it "returns nil" do + expect(service.path).to be nil + end end - describe "when payment_attributes are provided" do - it "raises an error if payment method does not exist" do - params[:order][:payments_attributes] = [{ payment_method_id: "123" }] + context "when the payment method is a Stripe method" do + let(:payment_method) { create(:stripe_sca_payment_method) } + let(:stripe_payment) { create(:payment, payment_method_id: payment_method.id) } + let(:test_redirect_url) { "http://stripe_auth_url/" } - expect { redirect.path }.to raise_error ActiveRecord::RecordNotFound + before do + order.payments << stripe_payment end - describe "when payment method provided exists" do - before { params[:order][:payments_attributes] = [{ payment_method_id: payment_method.id }] } + it "authorizes the payment and returns the redirect path" do + expect(OrderPaymentFinder).to receive_message_chain(:new, :last_pending_payment). + and_return(stripe_payment) - describe "and the payment method is not a stripe payment method" do - let(:payment_method) { create(:payment_method) } + expect(OrderManagement::Order::StripeScaPaymentAuthorize).to receive(:new).and_call_original - it "returns nil" do - expect(redirect.path).to be nil - end + expect(stripe_payment).to receive(:authorize!) do + # Authorization moves the payment state from checkout/processing to pending + stripe_payment.state = 'pending' + true end - describe "and the payment method is a stripe method" do - let(:distributor) { create(:distributor_enterprise) } - let(:payment_method) { create(:stripe_sca_payment_method) } + expect(stripe_payment).to receive(:cvv_response_message).and_return(test_redirect_url) - it "returns the redirect path" do - stripe_payment = create(:payment, payment_method_id: payment_method.id) - order.payments << stripe_payment - allow(OrderPaymentFinder).to receive_message_chain(:new, :last_pending_payment). - and_return(stripe_payment) - allow(stripe_payment).to receive(:authorize!) do - # Authorization moves the payment state from checkout/processing to pending - stripe_payment.state = 'pending' - true - end - allow(stripe_payment.order).to receive(:distributor) { distributor } - test_redirect_url = "http://stripe_auth_url/" - allow(stripe_payment).to receive(:cvv_response_message).and_return(test_redirect_url) - - expect(redirect.path).to eq test_redirect_url - end - end + expect(service.path).to eq test_redirect_url end end end