From 256cf4ac71afb3925140503763cb7e3fc10dcf44 Mon Sep 17 00:00:00 2001 From: Melissa Carbone Date: Tue, 1 Aug 2017 21:45:14 -0400 Subject: [PATCH] Ensure validation error is returned when creating a refund without a payment --- core/app/models/spree/refund.rb | 2 +- core/spec/models/spree/refund_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/core/app/models/spree/refund.rb b/core/app/models/spree/refund.rb index 42ed650072b..7ef1abdb518 100644 --- a/core/app/models/spree/refund.rb +++ b/core/app/models/spree/refund.rb @@ -76,7 +76,7 @@ def create_log_entry end def amount_is_less_than_or_equal_to_allowed_amount - if amount > payment.credit_allowed + if payment && amount > payment.credit_allowed errors.add(:amount, :greater_than_allowed) end end diff --git a/core/spec/models/spree/refund_spec.rb b/core/spec/models/spree/refund_spec.rb index d91d23748e7..ca95f9fe4bd 100644 --- a/core/spec/models/spree/refund_spec.rb +++ b/core/spec/models/spree/refund_spec.rb @@ -160,6 +160,14 @@ } end end + + context 'when payment is not present' do + let(:refund) { build(:refund, payment: nil) } + + it 'returns a validation error' do + expect { refund.save! }.to raise_error 'Validation failed: Payment can\'t be blank' + end + end end describe 'total_amount_reimbursed_for' do