From f3693d5dd41a8ea2686deba53964bb22216d9788 Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Fri, 22 Nov 2024 16:18:46 +0000 Subject: [PATCH 1/2] Add missing Pro InvoiceCollection specs --- .../alaveteli_pro/invoice_collection_spec.rb | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 spec/models/alaveteli_pro/invoice_collection_spec.rb diff --git a/spec/models/alaveteli_pro/invoice_collection_spec.rb b/spec/models/alaveteli_pro/invoice_collection_spec.rb new file mode 100644 index 0000000000..8d5b634560 --- /dev/null +++ b/spec/models/alaveteli_pro/invoice_collection_spec.rb @@ -0,0 +1,128 @@ +require 'spec_helper' +require 'stripe_mock' + +RSpec.describe AlaveteliPro::InvoiceCollection do + let(:collection) { described_class.new(customer) } + let(:customer) { double(:customer) } + + let(:open_invoice) { double(:invoice, status: 'open') } + let(:paid_invoice) { double(:invoice, status: 'paid', amount_paid: 10) } + + describe '.for_customer' do + it 'should return instance for customer' do + collection = described_class.for_customer(customer) + expect(collection).to be_a described_class + end + + it 'should pass customer to instance' do + expect(described_class).to receive(:new).with(customer) + described_class.for_customer(customer) + end + end + + describe '.new' do + it 'should store customer instance variable' do + expect(collection.instance_variable_get(:@customer)).to eq customer + end + end + + describe '#retrieve' do + context 'without customer' do + let(:customer) { nil } + + it 'returns nil' do + expect(collection.retrieve(123)).to eq nil + end + end + + context 'with Stripe invoices' do + let(:invoices) { Stripe::ListObject.new } + + before do + allow(Stripe::Invoice).to receive(:list).with(customer: customer). + and_return(invoices) + end + + it 'should retrieve wrapped invoice' do + invoice = double('Stripe::Invoice') + allow(invoices).to receive(:retrieve).with(123). + and_return(invoice) + expect(collection.retrieve(123)).to be_a AlaveteliPro::Invoice + expect(collection.retrieve(123)).to eq invoice + end + end + end + + describe '#open' do + before do + allow(Stripe::Invoice).to receive(:list).with(customer: customer). + and_return([open_invoice, paid_invoice]) + end + + it 'should return any open invoice' do + expect(collection.open).to match_array([open_invoice]) + end + end + + describe '#paid' do + before do + allow(Stripe::Invoice).to receive(:list).with(customer: customer). + and_return([open_invoice, paid_invoice]) + end + + it 'should return any paid invoice' do + expect(collection.paid).to match_array([paid_invoice]) + end + end + + describe '#each' do + context 'without customer' do + let(:customer) { nil } + + it 'returns no invoices' do + expect(collection.count).to eq 0 + end + end + + context 'with Stripe invoices' do + let(:invoices) { Stripe::ListObject.new } + + before do + allow(Stripe::Invoice).to receive(:list).with(customer: customer). + and_return(invoices) + allow(invoices).to receive(:auto_paging_each). + and_yield(open_invoice). + and_yield(paid_invoice) + end + + it 'returns to correct amount of objects' do + expect(collection.count).to eq 2 + end + + it 'wraps invoices as AlaveteliPro::Invoice objects' do + expect(collection.to_a).to all(be_a AlaveteliPro::Invoice) + end + end + + context 'without Stripe invoices' do + before do + allow(Stripe::Invoice).to receive(:list).with(customer: customer). + and_return([open_invoice, paid_invoice, open_invoice]) + end + + it 'returns to correct amount of objects' do + expect(collection.count).to eq 3 + end + + it 'wraps invoices as AlaveteliPro::Invoice objects' do + expect(collection.to_a).to all(be_a AlaveteliPro::Invoice) + end + end + + context 'without block' do + it 'should return a Enumerator' do + expect(collection.each).to be_a Enumerator + end + end + end +end From d42918a5292b21e79463fbe68c1b73f1c2f3609a Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Fri, 22 Nov 2024 16:20:03 +0000 Subject: [PATCH 2/2] Fix loading Stripe invoices Broken since the recent refactoring. Need to now call `id` on the Stripe Customer object and pass this into the list invoices API call. Fixes #8465 --- app/models/alaveteli_pro/invoice_collection.rb | 2 +- spec/models/alaveteli_pro/invoice_collection_spec.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/models/alaveteli_pro/invoice_collection.rb b/app/models/alaveteli_pro/invoice_collection.rb index b1f00882e3..8e60b107a3 100644 --- a/app/models/alaveteli_pro/invoice_collection.rb +++ b/app/models/alaveteli_pro/invoice_collection.rb @@ -52,7 +52,7 @@ def each(&block) def invoices return [] unless @customer - @invoices ||= Stripe::Invoice.list(customer: @customer) + @invoices ||= Stripe::Invoice.list(customer: @customer.id) end end end diff --git a/spec/models/alaveteli_pro/invoice_collection_spec.rb b/spec/models/alaveteli_pro/invoice_collection_spec.rb index 8d5b634560..dbfe81d226 100644 --- a/spec/models/alaveteli_pro/invoice_collection_spec.rb +++ b/spec/models/alaveteli_pro/invoice_collection_spec.rb @@ -3,7 +3,7 @@ RSpec.describe AlaveteliPro::InvoiceCollection do let(:collection) { described_class.new(customer) } - let(:customer) { double(:customer) } + let(:customer) { double(:customer, id: 'cus_123') } let(:open_invoice) { double(:invoice, status: 'open') } let(:paid_invoice) { double(:invoice, status: 'paid', amount_paid: 10) } @@ -39,7 +39,7 @@ let(:invoices) { Stripe::ListObject.new } before do - allow(Stripe::Invoice).to receive(:list).with(customer: customer). + allow(Stripe::Invoice).to receive(:list).with(customer: 'cus_123'). and_return(invoices) end @@ -55,7 +55,7 @@ describe '#open' do before do - allow(Stripe::Invoice).to receive(:list).with(customer: customer). + allow(Stripe::Invoice).to receive(:list).with(customer: 'cus_123'). and_return([open_invoice, paid_invoice]) end @@ -66,7 +66,7 @@ describe '#paid' do before do - allow(Stripe::Invoice).to receive(:list).with(customer: customer). + allow(Stripe::Invoice).to receive(:list).with(customer: 'cus_123'). and_return([open_invoice, paid_invoice]) end @@ -88,7 +88,7 @@ let(:invoices) { Stripe::ListObject.new } before do - allow(Stripe::Invoice).to receive(:list).with(customer: customer). + allow(Stripe::Invoice).to receive(:list).with(customer: 'cus_123'). and_return(invoices) allow(invoices).to receive(:auto_paging_each). and_yield(open_invoice). @@ -106,7 +106,7 @@ context 'without Stripe invoices' do before do - allow(Stripe::Invoice).to receive(:list).with(customer: customer). + allow(Stripe::Invoice).to receive(:list).with(customer: 'cus_123'). and_return([open_invoice, paid_invoice, open_invoice]) end