Simplify test by re-using helper stripe mock

This commit is contained in:
Luis Ramos
2020-10-02 19:12:56 +01:00
parent ff9f374950
commit 83456f94e3
2 changed files with 6 additions and 9 deletions

View File

@@ -3,6 +3,8 @@
require 'spec_helper'
describe Spree::Admin::PaymentsController, type: :controller do
include StripeHelper
let!(:shop) { create(:enterprise) }
let!(:user) { shop.owner }
let!(:order) { create(:order, distributor: shop, state: 'complete') }
@@ -152,19 +154,13 @@ describe Spree::Admin::PaymentsController, type: :controller do
allow(Stripe).to receive(:api_key) { "sk_test_12345" }
allow(StripeAccount).to receive(:find_by) { stripe_account }
# Retrieves payment intent info
stub_request(:get, "https://api.stripe.com/v1/payment_intents/pi_123")
.with(headers: { 'Stripe-Account' => 'abc123' })
.to_return({ status: 200, body: JSON.generate(
amount_received: 2000,
charges: { data: [{ id: "ch_1a2b3c" }] }
) })
stub_payment_intent_get_request
end
context "where the request succeeds" do
before do
# Issues the refund
stub_request(:post, "https://api.stripe.com/v1/charges/ch_1a2b3c/refunds").
stub_request(:post, "https://api.stripe.com/v1/charges/ch_1234/refunds").
with(basic_auth: ["sk_test_12345", ""]).
to_return(status: 200,
body: JSON.generate(id: 're_123', object: 'refund', status: 'succeeded') )
@@ -184,7 +180,7 @@ describe Spree::Admin::PaymentsController, type: :controller do
context "where the request fails" do
before do
stub_request(:post, "https://api.stripe.com/v1/charges/ch_1a2b3c/refunds").
stub_request(:post, "https://api.stripe.com/v1/charges/ch_1234/refunds").
with(basic_auth: ["sk_test_12345", ""]).
to_return(status: 200, body: JSON.generate(error: { message: "Bup-bow!" }) )
end

View File

@@ -55,6 +55,7 @@ module StripeHelper
body: JSON.generate(id: "pi_123",
object: "payment_intent",
amount: 2000,
amount_received: 2000,
status: options[:intent_status] || "requires_capture",
last_payment_error: nil,
charges: { data: [{ id: "ch_1234", amount: 2000 }] }) }