From aac7a5e5590dad8298d8d6c402ca1c089fb01329 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Thu, 13 Feb 2020 12:24:47 +0000 Subject: [PATCH] Adapt PaymentSetup spec to new version and remove some private method testing (payment) as well as some tests testing order.pending_payments logic --- .rubocop_manual_todo.yml | 1 - .../subscriptions/payment_setup_spec.rb | 198 ++---------------- 2 files changed, 17 insertions(+), 182 deletions(-) diff --git a/.rubocop_manual_todo.yml b/.rubocop_manual_todo.yml index a1908cdc5d..da0470689e 100644 --- a/.rubocop_manual_todo.yml +++ b/.rubocop_manual_todo.yml @@ -681,7 +681,6 @@ Metrics/ModuleLength: - engines/order_management/spec/services/order_management/subscriptions/estimator_spec.rb - engines/order_management/spec/services/order_management/subscriptions/form_spec.rb - engines/order_management/spec/services/order_management/subscriptions/proxy_order_syncer_spec.rb - - engines/order_management/spec/services/order_management/subscriptions/payment_setup_spec.rb - engines/order_management/spec/services/order_management/subscriptions/summarizer_spec.rb - engines/order_management/spec/services/order_management/subscriptions/validator_spec.rb - engines/order_management/spec/services/order_management/subscriptions/variants_list_spec.rb diff --git a/engines/order_management/spec/services/order_management/subscriptions/payment_setup_spec.rb b/engines/order_management/spec/services/order_management/subscriptions/payment_setup_spec.rb index 442fb64cb5..32bf3874d0 100644 --- a/engines/order_management/spec/services/order_management/subscriptions/payment_setup_spec.rb +++ b/engines/order_management/spec/services/order_management/subscriptions/payment_setup_spec.rb @@ -8,44 +8,6 @@ module OrderManagement let(:order) { create(:order) } let(:payment_setup) { OrderManagement::Subscriptions::PaymentSetup.new(order) } - describe "#payment" do - context "when only one payment exists on the order" do - let!(:payment) { create(:payment, order: order) } - - context "where the payment is pending" do - it { expect(payment_setup.__send__(:payment)).to eq payment } - end - - context "where the payment is failed" do - before { payment.update_attribute(:state, 'failed') } - it { expect(payment_setup.__send__(:payment)).to be nil } - end - end - - context "when more that one payment exists on the order" do - let!(:payment1) { create(:payment, order: order) } - let!(:payment2) { create(:payment, order: order) } - - context "where more than one payment is pending" do - it { expect([payment1, payment2]).to include payment_setup.__send__(:payment) } - end - - context "where only one payment is pending" do - before { payment1.update_attribute(:state, 'failed') } - it { expect(payment_setup.__send__(:payment)).to eq payment2 } - end - - context "where no payments are pending" do - before do - payment1.update_attribute(:state, 'failed') - payment2.update_attribute(:state, 'failed') - end - - it { expect(payment_setup.__send__(:payment)).to be nil } - end - end - end - describe "#call!" do let!(:payment){ create(:payment, amount: 10) } @@ -68,161 +30,35 @@ module OrderManagement context "when a payment is present" do before { allow(order).to receive(:pending_payments).once { [payment] } } - context "when a credit card is not required" do - before do - allow(payment_setup).to receive(:card_required?) { false } - expect(payment_setup).to_not receive(:card_available?) - expect(payment_setup).to_not receive(:ensure_credit_card) - end - - context "when the payment total doesn't match the outstanding balance on the order" do - before { allow(order).to receive(:outstanding_balance) { 5 } } - it "updates the payment total to reflect the outstanding balance" do - expect{ payment_setup.call! }.to change(payment, :amount).from(10).to(5) - end - end - - context "when the payment total matches the outstanding balance on the order" do - before { allow(order).to receive(:outstanding_balance) { 10 } } - - it "does nothing" do - expect{ payment_setup.call! }.to_not change(payment, :amount).from(10) - end + context "when the payment total doesn't match the outstanding balance on the order" do + before { allow(order).to receive(:outstanding_balance) { 5 } } + it "updates the payment total to reflect the outstanding balance" do + expect{ payment_setup.call! }.to change(payment, :amount).from(10).to(5) end end - context "when a credit card is required" do - before do - expect(payment_setup).to receive(:card_required?) { true } - end + context "when the payment total matches the outstanding balance on the order" do + before { allow(order).to receive(:outstanding_balance) { 10 } } - context "and the payment source is not a credit card" do - before { expect(payment_setup).to receive(:card_set?) { false } } - - context "and no default credit card has been set by the customer" do - before do - allow(order).to receive(:user) { instance_double(Spree::User, default_card: nil) } - end - - it "adds an error to the order and does not update the payment" do - expect(payment).to_not receive(:update_attributes) - expect{ payment_setup.call! }.to change(order.errors[:base], :count).from(0).to(1) - end - end - - context "and the customer has not authorised the shop to charge to credit cards" do - before do - allow(order).to receive(:user) { - instance_double(Spree::User, default_card: create(:credit_card)) - } - allow(order).to receive(:customer) { - instance_double(Customer, allow_charges?: false) - } - end - - it "adds an error to the order and does not update the payment" do - expect(payment).to_not receive(:update_attributes) - expect{ payment_setup.call! }.to change(order.errors[:base], :count).from(0).to(1) - end - end - - context "and an authorised default credit card is available to charge" do - before do - allow(order).to receive(:user) { - instance_double(Spree::User, default_card: create(:credit_card)) - } - allow(order).to receive(:customer) { - instance_double(Customer, allow_charges?: true) - } - end - - context "when the payment total doesn't match the order's outstanding balance" do - before { allow(order).to receive(:outstanding_balance) { 5 } } - it "updates the payment total to reflect the outstanding balance" do - expect{ payment_setup.call! }.to change(payment, :amount).from(10).to(5) - end - end - - context "when the payment total matches the outstanding balance on the order" do - before { allow(order).to receive(:outstanding_balance) { 10 } } - - it "does nothing" do - expect{ payment_setup.call! }.to_not change(payment, :amount).from(10) - end - end - end - end - - context "and the payment source is already a credit card" do - before { expect(payment_setup).to receive(:card_set?) { true } } - - context "when the payment total doesn't match the outstanding balance on the order" do - before { allow(order).to receive(:outstanding_balance) { 5 } } - it "updates the payment total to reflect the outstanding balance" do - expect{ payment_setup.call! }.to change(payment, :amount).from(10).to(5) - end - end - - context "when the payment total matches the outstanding balance on the order" do - before { allow(order).to receive(:outstanding_balance) { 10 } } - - it "does nothing" do - expect{ payment_setup.call! }.to_not change(payment, :amount).from(10) - end - end + it "does nothing" do + expect{ payment_setup.call! }.to_not change(payment, :amount).from(10) end end end - end - describe "#ensure_credit_card" do - let!(:payment) { create(:payment, source: nil) } - before { allow(payment_setup).to receive(:payment) { payment } } + context "when more that one payment exists on the order" do + let!(:payment1) { create(:payment, order: order) } + let!(:payment2) { create(:payment, order: order) } - context "when no default credit card is found" do before do - allow(order).to receive(:user) { instance_double(Spree::User, default_card: nil) } + allow(order).to receive(:outstanding_balance) { 7 } + allow(order).to receive(:pending_payments).once { [payment1, payment2] } end - it "returns false and down not update the payment source" do - expect do - expect(payment_setup.__send__(:ensure_credit_card)).to be false - end.to_not change(payment, :source).from(nil) - end - end - - context "when a default credit card is found" do - let(:credit_card) { create(:credit_card) } - before do - allow(order).to receive(:user) { - instance_double(Spree::User, default_card: credit_card) - } - end - - context "and charge have not been authorised by the customer" do - before do - allow(order).to receive(:customer) { - instance_double(Customer, allow_charges?: false) - } - end - - it "returns false and does not update the payment source" do - expect do - expect(payment_setup.__send__(:ensure_credit_card)).to be false - end.to_not change(payment, :source).from(nil) - end - end - - context "and charges have been authorised by the customer" do - before do - allow(order).to receive(:customer) { instance_double(Customer, allow_charges?: true) } - end - - it "returns true and stores the credit card as the payment source" do - expect do - expect(payment_setup.__send__(:ensure_credit_card)).to be true - end.to change(payment, :source_id).from(nil).to(credit_card.id) - end + it "updates the amount of the last payment to reflect the outstanding balance" do + payment_setup.call! + expect(payment1.amount).to eq 45.75 + expect(payment2.amount).to eq 7 end end end