|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'solidus_stripe_spec_helper' |
| 4 | + |
| 5 | +RSpec.describe SolidusStripe::Customer, type: :model do |
| 6 | + describe ".retrieve_or_create_stripe_customer_id" do |
| 7 | + context 'with an existing customer' do |
| 8 | + it 'returns the customer_id' do |
| 9 | + user = create(:user) |
| 10 | + order = create(:order, user: user) |
| 11 | + customer = create(:stripe_customer, stripe_id: 'cus_123', source: user) |
| 12 | + |
| 13 | + expect(customer.source).to be_a(Spree::User) |
| 14 | + expect( |
| 15 | + described_class.retrieve_or_create_stripe_customer_id(order: order, payment_method: customer.payment_method) |
| 16 | + ).to eq('cus_123') |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + context 'without an existing customer' do |
| 21 | + it 'creates the customer from a user' do |
| 22 | + user = create(:user, email: 'registered@example.com') |
| 23 | + order = create(:order, user: user) |
| 24 | + payment_method = create(:stripe_payment_method) |
| 25 | + |
| 26 | + stripe_customer = Stripe::Customer.construct_from(id: 'cus_123') |
| 27 | + allow(Stripe::Customer).to receive(:create).with(email: 'registered@example.com').and_return(stripe_customer) |
| 28 | + |
| 29 | + expect( |
| 30 | + described_class.retrieve_or_create_stripe_customer_id(order: order, payment_method: payment_method) |
| 31 | + ).to eq('cus_123') |
| 32 | + end |
| 33 | + |
| 34 | + it 'creates the customer from a guest order' do |
| 35 | + payment_method = create(:stripe_payment_method) |
| 36 | + order = create(:order, user: nil, email: 'guest@example.com') |
| 37 | + |
| 38 | + stripe_customer = Stripe::Customer.construct_from(id: 'cus_123') |
| 39 | + allow(Stripe::Customer).to receive(:create).with(email: 'guest@example.com').and_return(stripe_customer) |
| 40 | + |
| 41 | + expect( |
| 42 | + described_class.retrieve_or_create_stripe_customer_id(order: order, payment_method: payment_method) |
| 43 | + ).to eq('cus_123') |
| 44 | + end |
| 45 | + end |
| 46 | + end |
| 47 | +end |
0 commit comments