Update specs

This commit is contained in:
Matt-Yorkley
2021-12-06 17:44:04 +00:00
parent b77ff346a3
commit ef7a02004e
4 changed files with 66 additions and 125 deletions

View File

@@ -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 }] }

View File

@@ -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",

View File

@@ -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

View File

@@ -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