2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe UpdatePayouts do
|
|
|
|
describe '.reverse_with_stripe' do
|
|
|
|
describe 'param validation' do
|
|
|
|
it 'basic param_validation' do
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { UpdatePayouts.reverse_with_stripe(nil, nil, nil) }.to(raise_error do |error|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error).to be_a(ParamValidation::ValidationError)
|
2019-07-30 21:29:24 +00:00
|
|
|
expect_validation_errors(error.data, [{ key: :payout_id, name: :required },
|
|
|
|
{ key: :payout_id, name: :is_integer },
|
|
|
|
{ key: :status, name: :required },
|
|
|
|
{ key: :status, name: :included_in },
|
|
|
|
{ key: :failure_message, name: :required },
|
|
|
|
{ key: :failure_message, name: :not_blank }])
|
|
|
|
end)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'reject non-existent payouts' do
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { UpdatePayouts.reverse_with_stripe(5_555_555, 'failed', 'failure') }.to(raise_error do |error|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error).to be_a(ParamValidation::ValidationError)
|
2019-07-30 21:29:24 +00:00
|
|
|
expect_validation_errors(error.data, [{ key: :payout_id }])
|
|
|
|
end)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'reject payouts with no payments' do
|
|
|
|
payout = force_create(:payout)
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { UpdatePayouts.reverse_with_stripe(payout.id, 'failed', 'failure') }.to(raise_error do |error|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error).to be_a(ArgumentError)
|
2019-07-30 21:29:24 +00:00
|
|
|
end)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validate proper function' do
|
|
|
|
let(:payment_to_reverse) { force_create(:payment) }
|
|
|
|
let(:payment_to_ignore) { force_create(:payment) }
|
|
|
|
let(:payment_to_reverse_2) { force_create(:payment) }
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:payment_to_reverse_with_refund) { force_create(:payment) }
|
|
|
|
let(:reverse_payment_for_refund) { force_create(:payment) }
|
|
|
|
|
|
|
|
let!(:charges) do
|
|
|
|
[force_create(:charge, payment: payment_to_reverse, status: 'disbursed'),
|
|
|
|
force_create(:charge, payment: payment_to_reverse_2, status: 'disbursed'),
|
|
|
|
force_create(:charge, payment: payment_to_ignore, status: 'disbursed'),
|
|
|
|
force_create(:charge, payment: payment_to_reverse_with_refund, status: 'disbursed')]
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let!(:refunds) { [force_create(:refund, charge: charges.last, payment: reverse_payment_for_refund, disbursed: true)] }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2020-04-16 20:50:03 +00:00
|
|
|
let(:np) { force_create(:nm_justice) }
|
2019-07-30 21:29:24 +00:00
|
|
|
let!(:bank_account) { force_create(:bank_account, nonprofit: np) }
|
|
|
|
let!(:payout) do
|
|
|
|
force_create(:payout, status: 'paid', failure_message: 'all good',
|
|
|
|
nonprofit: np, stripe_transfer_id: 'transfer_id', email: 'no one cares', net_amount: 500)
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:bad_status) { 'failed' }
|
|
|
|
let(:bad_failure_message) { 'so terrible' }
|
|
|
|
let(:available) { 'available' }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
before(:each) do
|
2018-03-25 17:30:42 +00:00
|
|
|
payout.payments.push(payment_to_reverse)
|
|
|
|
payout.payments.push(payment_to_reverse_2)
|
|
|
|
payout.payments.push(payment_to_reverse_with_refund)
|
|
|
|
payout.payments.push(reverse_payment_for_refund)
|
|
|
|
UpdatePayouts.reverse_with_stripe(payout.id, bad_status, bad_failure_message)
|
2019-01-28 21:52:37 +00:00
|
|
|
payment_to_reverse.reload
|
|
|
|
payment_to_reverse_2.reload
|
|
|
|
payment_to_reverse_with_refund.reload
|
|
|
|
reverse_payment_for_refund.reload
|
|
|
|
payment_to_ignore.reload
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
it 'reverses proper payments' do
|
|
|
|
expect(payment_to_reverse.charge.status).to eq available
|
|
|
|
expect(payment_to_reverse_2.charge.status).to eq available
|
|
|
|
expect(payment_to_reverse_with_refund.charge.status).to eq available
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reverses proper refunds' do
|
|
|
|
refund = refunds.first
|
|
|
|
refund.reload
|
|
|
|
expect(refund.disbursed).to eq false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reverses disputes', pending: 'disputes aren\'t properly modeled to safely do this' do
|
2019-07-30 21:29:24 +00:00
|
|
|
raise
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores irrelevant payments' do
|
|
|
|
expect(payment_to_ignore.charge.status).to eq 'disbursed'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'changes payout status and failure' do
|
|
|
|
payout.reload
|
|
|
|
expect(payout.status).to eq bad_status
|
|
|
|
expect(payout.failure_message).to eq bad_failure_message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|