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 'stripe_mock'
|
|
|
|
|
|
|
|
describe CancelBillingSubscription do
|
|
|
|
let(:stripe_helper) { StripeMock.create_test_helper }
|
2019-07-30 21:29:24 +00:00
|
|
|
before(:each) do
|
2018-03-25 17:30:42 +00:00
|
|
|
StripeMock.start
|
2019-07-30 21:29:24 +00:00
|
|
|
@card_token = StripeMock.generate_card_token(last4: '9191', exp_year: 2011)
|
2018-03-25 17:30:42 +00:00
|
|
|
@np = force_create(:nonprofit)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
|
|
|
after { StripeMock.stop }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
describe 'parameter validation' do
|
|
|
|
it 'without db' do
|
|
|
|
result = CancelBillingSubscription.with_stripe(nil)
|
|
|
|
errors = result[:json][:errors]
|
|
|
|
expect(errors.length).to eq(2)
|
|
|
|
expect(result[:status]).to eq :unprocessable_entity
|
|
|
|
expect_validation_errors(errors, [
|
2019-07-30 21:29:24 +00:00
|
|
|
{ key: :nonprofit, name: :required },
|
|
|
|
{ key: :nonprofit, name: :is_a }
|
|
|
|
])
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with db' do
|
2019-07-30 21:29:24 +00:00
|
|
|
before(:each) do
|
2018-03-25 17:30:42 +00:00
|
|
|
@np = create(:nonprofit)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
it 'nonprofit valid but no card or billing_subscription' do
|
|
|
|
result = CancelBillingSubscription.with_stripe(@np)
|
|
|
|
expect_proper_failure(result)
|
|
|
|
end
|
|
|
|
it 'nonprofit valid but no card' do
|
2019-07-30 21:29:24 +00:00
|
|
|
force_create(:billing_subscription, nonprofit: @np)
|
2018-03-25 17:30:42 +00:00
|
|
|
result = CancelBillingSubscription.with_stripe(@np)
|
|
|
|
expect_proper_failure(result)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'nonprofit valid but no billings subscription' do
|
|
|
|
@np.active_card = build(:card)
|
|
|
|
result = CancelBillingSubscription.with_stripe(@np)
|
|
|
|
expect_proper_failure(result)
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_proper_failure(result)
|
|
|
|
expect(result[:status]).to eq(:unprocessable_entity)
|
|
|
|
expect(result[:json][:error]).to start_with("We don\'t have a subscription for your non-profit. Please contact support.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
context 'processing the billing subscription' do
|
2019-07-30 21:29:24 +00:00
|
|
|
before(:each) do
|
2019-11-08 17:30:48 +00:00
|
|
|
bp = create(:billing_plan, amount: 133_333, percentage_fee: 0.33, name: 'fake plan')
|
2019-07-30 21:29:24 +00:00
|
|
|
@stripe_customer = Stripe::Customer.create
|
|
|
|
@plan = Stripe::Plan.create(id: 'test_str_plan', amount: 0, currency: 'USD', interval: 'year', name: 'test PLan')
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
@original_str_subscription = @stripe_customer.subscriptions.create(plan: @plan.id)
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
create(:card, holder: @np, stripe_customer_id: @stripe_customer.id)
|
2018-03-25 17:30:42 +00:00
|
|
|
@np.billing_subscription = build(:billing_subscription, billing_plan: bp, stripe_subscription_id: @original_str_subscription.id)
|
2019-07-30 21:29:24 +00:00
|
|
|
@default_plan = create(:billing_plan, id: Settings.default_bp.id)
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
it 'handles failure of stripe properly' do
|
|
|
|
StripeMock.prepare_error(Stripe::StripeError.new('some failure'), :retrieve_customer_subscription)
|
|
|
|
original_bs = @np.billing_subscription
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
result = CancelBillingSubscription.with_stripe(@np)
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
expect(result[:status]).to eq :unprocessable_entity
|
2019-07-30 21:29:24 +00:00
|
|
|
expect(result[:json][:error]).to start_with('Oops')
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
expect(@np.billing_subscription).to eq(original_bs)
|
|
|
|
|
|
|
|
str_customer_reloaded = Stripe::Customer.retrieve(@stripe_customer.id)
|
2019-07-30 21:29:24 +00:00
|
|
|
expect(str_customer_reloaded.subscriptions.data).to eq([@original_str_subscription])
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should succeed' do
|
2019-07-30 21:29:24 +00:00
|
|
|
result = CancelBillingSubscription.with_stripe(@np)
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
expect(result[:status]).to eq :ok
|
2019-07-30 21:29:24 +00:00
|
|
|
expect(result[:json]).to eq({})
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
expect(@np.billing_subscription.status).to eq 'active'
|
|
|
|
expect(@np.billing_subscription.billing_plan).to eq @default_plan
|
|
|
|
str_customer_reloaded = Stripe::Customer.retrieve(@stripe_customer.id)
|
|
|
|
expect(str_customer_reloaded.subscriptions.data.length).to eq 0
|
|
|
|
end
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|