From 2f4648342fdf291ba5c32cd30e692f2c95a1f444 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 10 Jul 2020 11:50:20 +0200 Subject: [PATCH] Merge decorator specs with Spree's ones They are now isolated from each other. --- spec/models/spree/payment_original_spec.rb | 86 +++++++++++++++++++++ spec/models/spree/payment_spec.rb | 90 ---------------------- 2 files changed, 86 insertions(+), 90 deletions(-) delete mode 100644 spec/models/spree/payment_spec.rb diff --git a/spec/models/spree/payment_original_spec.rb b/spec/models/spree/payment_original_spec.rb index 418c09882c..07559f5314 100644 --- a/spec/models/spree/payment_original_spec.rb +++ b/spec/models/spree/payment_original_spec.rb @@ -820,4 +820,90 @@ describe Spree::Payment do end end end + + context 'OFN specs from previously decorated model' do + describe "applying transaction fees" do + let!(:order) { create(:order) } + let!(:line_item) { create(:line_item, order: order, quantity: 3, price: 5.00) } + + before do + order.reload.update! + end + + context "to Stripe payments" do + let(:shop) { create(:enterprise) } + let(:payment_method) { create(:stripe_payment_method, distributor_ids: [create(:distributor_enterprise).id], preferred_enterprise_id: shop.id) } + let(:payment) { create(:payment, order: order, payment_method: payment_method, amount: order.total) } + let(:calculator) { Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) } + + before do + payment_method.calculator = calculator + payment_method.save! + + allow(order).to receive(:pending_payments) { [payment] } + end + + context "when the payment fails" do + let(:failed_response) { ActiveMerchant::Billing::Response.new(false, "This is an error message") } + + before do + allow(payment_method).to receive(:purchase) { failed_response } + end + + it "makes the transaction fee ineligible and finalizes it" do + # Decided to wrap the save process in order.process_payments! + # since that is the context it is usually performed in + order.process_payments! + expect(order.payments.count).to eq 1 + expect(order.payments).to include payment + expect(payment.state).to eq "failed" + expect(payment.adjustment.eligible?).to be false + expect(payment.adjustment.finalized?).to be true + expect(order.adjustments.payment_fee.count).to eq 1 + expect(order.adjustments.payment_fee.eligible).to_not include payment.adjustment + end + end + + context "when the payment information is invalid" do + before do + allow(payment_method).to receive(:supports?) { false } + end + + it "makes the transaction fee ineligible and finalizes it" do + # Decided to wrap the save process in order.process_payments! + # since that is the context it is usually performed in + order.process_payments! + expect(order.payments.count).to eq 1 + expect(order.payments).to include payment + expect(payment.state).to eq "invalid" + expect(payment.adjustment.eligible?).to be false + expect(payment.adjustment.finalized?).to be true + expect(order.adjustments.payment_fee.count).to eq 1 + expect(order.adjustments.payment_fee.eligible).to_not include payment.adjustment + end + end + + context "when the payment is processed successfully" do + let(:successful_response) { ActiveMerchant::Billing::Response.new(true, "Yay!") } + + before do + allow(payment_method).to receive(:purchase) { successful_response } + end + + it "creates an appropriate adjustment" do + # Decided to wrap the save process in order.process_payments! + # since that is the context it is usually performed in + order.process_payments! + expect(order.payments.count).to eq 1 + expect(order.payments).to include payment + expect(payment.state).to eq "completed" + expect(payment.adjustment.eligible?).to be true + expect(order.adjustments.payment_fee.count).to eq 1 + expect(order.adjustments.payment_fee.eligible).to include payment.adjustment + expect(payment.adjustment.amount).to eq 1.5 + end + end + end + end + end end diff --git a/spec/models/spree/payment_spec.rb b/spec/models/spree/payment_spec.rb deleted file mode 100644 index 434ce3a2a3..0000000000 --- a/spec/models/spree/payment_spec.rb +++ /dev/null @@ -1,90 +0,0 @@ -require 'spec_helper' - -module Spree - describe Payment do - - describe "applying transaction fees" do - let!(:order) { create(:order) } - let!(:line_item) { create(:line_item, order: order, quantity: 3, price: 5.00) } - - before do - order.reload.update! - end - - context "to Stripe payments" do - let(:shop) { create(:enterprise) } - let(:payment_method) { create(:stripe_payment_method, distributor_ids: [create(:distributor_enterprise).id], preferred_enterprise_id: shop.id) } - let(:payment) { create(:payment, order: order, payment_method: payment_method, amount: order.total) } - let(:calculator) { Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) } - - before do - payment_method.calculator = calculator - payment_method.save! - - allow(order).to receive(:pending_payments) { [payment] } - end - - context "when the payment fails" do - let(:failed_response) { ActiveMerchant::Billing::Response.new(false, "This is an error message") } - - before do - allow(payment_method).to receive(:purchase) { failed_response } - end - - it "makes the transaction fee ineligible and finalizes it" do - # Decided to wrap the save process in order.process_payments! - # since that is the context it is usually performed in - order.process_payments! - expect(order.payments.count).to eq 1 - expect(order.payments).to include payment - expect(payment.state).to eq "failed" - expect(payment.adjustment.eligible?).to be false - expect(payment.adjustment.finalized?).to be true - expect(order.adjustments.payment_fee.count).to eq 1 - expect(order.adjustments.payment_fee.eligible).to_not include payment.adjustment - end - end - - context "when the payment information is invalid" do - before do - allow(payment_method).to receive(:supports?) { false } - end - - it "makes the transaction fee ineligible and finalizes it" do - # Decided to wrap the save process in order.process_payments! - # since that is the context it is usually performed in - order.process_payments! - expect(order.payments.count).to eq 1 - expect(order.payments).to include payment - expect(payment.state).to eq "invalid" - expect(payment.adjustment.eligible?).to be false - expect(payment.adjustment.finalized?).to be true - expect(order.adjustments.payment_fee.count).to eq 1 - expect(order.adjustments.payment_fee.eligible).to_not include payment.adjustment - end - end - - context "when the payment is processed successfully" do - let(:successful_response) { ActiveMerchant::Billing::Response.new(true, "Yay!") } - - before do - allow(payment_method).to receive(:purchase) { successful_response } - end - - it "creates an appropriate adjustment" do - # Decided to wrap the save process in order.process_payments! - # since that is the context it is usually performed in - order.process_payments! - expect(order.payments.count).to eq 1 - expect(order.payments).to include payment - expect(payment.state).to eq "completed" - expect(payment.adjustment.eligible?).to be true - expect(order.adjustments.payment_fee.count).to eq 1 - expect(order.adjustments.payment_fee.eligible).to include payment.adjustment - expect(payment.adjustment.amount).to eq 1.5 - end - end - end - end - end -end