Skip to content

Instantly share code, notes, and snippets.

@mwhagedorn
Created August 4, 2016 15:42
Show Gist options
  • Save mwhagedorn/b3aebb5ed051de96acb485449894cb0d to your computer and use it in GitHub Desktop.
Save mwhagedorn/b3aebb5ed051de96acb485449894cb0d to your computer and use it in GitHub Desktop.
spec to investigate dunning issues
require 'spec_helper'
describe "Dunning" do
let(:site) { subscription.site }
let(:processing_time) { Chronic.parse("August 1, 2016 12:00 pm EST") }
let(:renewal_time) { subscription.current_period_ends_at + 1.minute }
let(:subscription) { create(:active_subscription) }
let!(:strategy) { create(:dunning_strategy, site: site, name:"my awesome test") }
let!(:day_1) { create(:strategy_step, strategy: strategy, action: 'retry', send_email: true, day_threshold: 1) }
let!(:day_3) { create(:strategy_step, strategy: strategy, action: 'retry', send_email: true, day_threshold: 3) }
let!(:day_7) { create(:strategy_step, strategy: strategy, action: 'cancel', send_email: true, day_threshold: 7) }
let(:dunner) { subscription.dunners.active }
before { site.update_attribute(:dunning_strategy_id, strategy.id) }
it "does something" do
# weirdness with the test setup
StrategyStep.last.destroy
expect(StrategyStep.count).to eql(3)
#Timecop.freeze(processing_time) { subscription.process }
expect(subscription.state).to eql("active")
# remove payment profile, force into `past_due`
subscription.update_column(:payment_profile_id, nil)
# Renewal, which will become Day 1 of dunning
Timecop.freeze(renewal_time) do
expect(CustomerMailer).to receive(:conditionally_deliver_dunning_notice).and_call_original
expect(subscription).to receive(:renew!).twice.and_call_original
#failed payments in active, cause two process method calls, one in active, one in past_due
expect { subscription.process }.to \
change {
{
state: subscription.reload.state,
dunner_count: Dunner.count,
}
}
.from(
state: "active",
dunner_count: 0,
).to(
state: "past_due",
dunner_count: 1,
)
expect(dunner.days_of_existence).to eql(1)
end
# Day 2
Timecop.freeze(subscription.next_assessment_at) do
expect(CustomerMailer).to_not receive(:conditionally_deliver_dunning_notice)
expect(subscription).to_not receive(:renew!)
expect(dunner.days_of_existence).to eql(2)
subscription.process
end
#
# # Day 3
# Timecop.freeze(subscription.next_assessment_at) do
# expect(CustomerMailer).to receive(:conditionally_deliver_dunning_notice).and_call_original
# expect(subscription).to receive(:renew!).and_call_original
# expect(dunner.days_of_existence).to eql(3)
# subscription.process
# end
#
# # Day 4
# Timecop.freeze(subscription.next_assessment_at) do
# expect(CustomerMailer).to_not receive(:conditionally_deliver_dunning_notice)
# expect(subscription).to_not receive(:renew!)
# expect(dunner.days_of_existence).to eql(4)
# subscription.process
# end
#
# # Day 5
# Timecop.freeze(subscription.next_assessment_at) do
# expect(CustomerMailer).to_not receive(:conditionally_deliver_dunning_notice)
# expect(subscription).to_not receive(:renew!)
# expect(dunner.days_of_existence).to eql(5)
# subscription.process
# end
#
# # Day 6
# Timecop.freeze(subscription.next_assessment_at) do
# expect(CustomerMailer).to_not receive(:conditionally_deliver_dunning_notice)
# expect(subscription).to_not receive(:renew!)
# expect(dunner.days_of_existence).to eql(6)
# subscription.process
# end
#
# # # Day 7
# # Timecop.freeze(subscription.next_assessment_at) do
# # expect(CustomerMailer).to receive(:conditionally_deliver_dunning_notice).and_call_original
# # expect(subscription).to receive(:renew!).and_call_original
# # expect(dunner.days_of_existence).to eql(7)
# # expect { subscription.process }.to change { subscription.reload.state }.to("canceled")
# # end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment