2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:15:39 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
require 'timecop'
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
describe PayRecurringDonation do
|
2018-03-25 17:30:42 +00:00
|
|
|
before(:all) do
|
2019-07-30 21:29:24 +00:00
|
|
|
# @data = PsqlFixtures.init
|
|
|
|
# @result = @data['recurring_donation']
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:today) { Time.current.to_date }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
describe '.with_donation', pending: true do
|
2018-03-25 17:30:42 +00:00
|
|
|
# describe 'create a valid charge and payment when due' do
|
|
|
|
#
|
|
|
|
# end
|
|
|
|
# it 'creates a new valid charge and payment when due' do
|
|
|
|
# h = Timecop.freeze(Time.parse("2020-02-02").utc) do
|
|
|
|
# VCR.use_cassette('PayRecurringDonation/h1') do
|
|
|
|
# PayRecurringDonation.with_stripe(@result['recurring_donation']['id'])
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# expect(h['charge']['stripe_charge_id']).to be_present
|
|
|
|
# expect(h['charge']['card_id']).to eq(@data['donor']['card']['id'])
|
|
|
|
# expect(h['charge']['supporter_id']).to eq(@data['donor']['supporter']['id'])
|
|
|
|
# expect(h['charge']['amount']).to eq(@data['recurring_donation']['recurring_donation']['amount'])
|
|
|
|
# expect(h['charge']['donation_id']).to eq(@result['donation']['id'])
|
|
|
|
# expect(h['charge']['nonprofit_id']).to eq(@data['np']['id'])
|
|
|
|
# expect(h['charge']['status']).to eq('pending')
|
|
|
|
# expect(h['charge']['payment_id']).to eq(h['payment']['id'])
|
|
|
|
# expect(h['payment']['nonprofit_id']).to eq(@data['np']['id'])
|
|
|
|
# expect(h['payment']['date']).to eq(h['payment']['created_at'])
|
|
|
|
# expect(h['payment']['supporter_id']).to eq(@data['donor']['supporter']['id'])
|
|
|
|
# expect(h['payment']['donation_id']).to eq(@result['donation']['id'])
|
|
|
|
# expect(h['payment']['gross_amount']).to eq(@data['recurring_donation']['recurring_donation']['amount'])
|
|
|
|
# expect(h['payment']['kind']).to eq('RecurringDonation')
|
|
|
|
# # Wipe out data for other tests
|
|
|
|
# Psql.execute("DELETE FROM charges WHERE id = #{h['charge']['id']}")
|
|
|
|
# Psql.execute("DELETE FROM payments WHERE id = #{h['payment']['id']}")
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# it 'returns a failed charge without a payment when the card is declined, increments n_failures, and is still due' do
|
|
|
|
# Psql.execute("UPDATE donations SET card_id=#{@data['donor']['bad_card']['id']} WHERE id=#{@result['donation']['id']}")
|
|
|
|
# h = Timecop.freeze(Time.parse("2020-02-01").utc) do
|
|
|
|
# VCR.use_cassette('PayRecurringDonation/bad_card') do
|
|
|
|
# PayRecurringDonation.with_stripe(@result['recurring_donation']['id'])
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# expect(h['charge']['status']).to eq 'failed'
|
|
|
|
# expect(h['recurring_donation']['n_failures']).to eq 1
|
|
|
|
# Timecop.freeze(Time.parse("2020-02-01").utc) do
|
|
|
|
# expect(QueryRecurringDonations.is_due?(h['recurring_donation']['id'])).to be true
|
|
|
|
# end
|
|
|
|
# Psql.execute("UPDATE donations SET card_id=#{@data['donor']['card']['id']} WHERE id=#{@result['donation']['id']}")
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# it 'returns false when not due' do
|
|
|
|
# Timecop.freeze(Time.parse("2020-01-01").utc) do
|
|
|
|
# expect(PayRecurringDonation.with_stripe(@result['recurring_donation']['id'])).to be false
|
|
|
|
# end
|
|
|
|
# end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
describe '.pay_all_due_with_stripe', pending: true do
|
2018-03-25 17:30:42 +00:00
|
|
|
# it 'queues a job to pay each due recurring donation' do
|
|
|
|
# Timecop.freeze(Time.parse("2020-02-01").utc) do
|
|
|
|
# VCR.use_cassette('PayRecurringDonation/pay_all_due_with_stripe') do
|
|
|
|
# PayRecurringDonation.pay_all_due_with_stripe
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# jerbz = Psql.execute("SELECT * FROM delayed_jobs WHERE queue='rec-don-payments'")
|
|
|
|
# handlers = jerbz.map{|j| YAML.load(j['handler'])}
|
|
|
|
# expect(handlers.count).to eq(handlers.select{|h| h.method_name == :with_stripe}.count)
|
|
|
|
# expect(handlers.count).to eq(handlers.select{|h| h.object == PayRecurringDonation}.count)
|
|
|
|
# expect(handlers.map{|h| h.args}.flatten).to include(@result['recurring_donation']['id'])
|
|
|
|
# Psql.execute("DELETE FROM delayed_jobs WHERE queue='rec-don-payments'")
|
|
|
|
# end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.ULTIMATE_VERIFICATION' do
|
|
|
|
it 'returns false' do
|
|
|
|
Timecop.freeze(Time.parse('2020-02-01').utc) do
|
|
|
|
expect(PayRecurringDonation.ULTIMATE_VERIFICATION('2020-02-02', true, true, false, 'run_dangerously')).to be_falsey
|
|
|
|
expect(PayRecurringDonation.ULTIMATE_VERIFICATION('2020-02-01', false, true, false, 'run_dangerously')).to be_falsey
|
|
|
|
expect(PayRecurringDonation.ULTIMATE_VERIFICATION('2020-02-01', true, false, false, 'run_dangerously')).to be_falsey
|
|
|
|
expect(PayRecurringDonation.ULTIMATE_VERIFICATION('2020-02-01', true, true, true, 'run_dangerously')).to be_falsey
|
|
|
|
expect(PayRecurringDonation.ULTIMATE_VERIFICATION('2020-02-01', true, true, false, 'rd')).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
it 'returns true' do
|
|
|
|
Timecop.freeze(Time.parse('2020-02-01').utc) do
|
|
|
|
expect(PayRecurringDonation.ULTIMATE_VERIFICATION('2020-02-01', true, true, false, 'run dangerously')).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|