eceb6c9b43
* Add self-service account deletion * Disable access to cancelled accounts & implement Stripe interactions * Add tests * Fix failing tests * Remove cancelled accounts from lists * Fix incorrect redirect * Use _path instead of _url for consistency * Don't track how far the inicieration job got We still want the step tracking so that the job can be interrupted. But, since the scope is idempotent, we don't need to track how far it got. * Specify the exact time when the data will be deleted * Fix crash due to unadvancable cursor * Rename up_for_incineration to due_for_incineration * Regenrate the schema * Fix incorrect path check * Migrate the SQLite schema * Only show the cancel button on cancellable accounts * Check that a subscirption method exists before calling it * Ignore job failures due to missing records when an account gets deleted * Skip sending notifications on cancelled accounts * Use collbacks to integrate * Add a blank line between queue_as and discard_on * Break checks into methods * Inline methods * Rename Account::IncinerateJob * Run migrations * Migrate SQLite
121 lines
3.3 KiB
Ruby
121 lines
3.3 KiB
Ruby
require "test_helper"
|
|
|
|
class Account::SubscriptionTest < ActiveSupport::TestCase
|
|
test "get the account plan" do
|
|
subscription = Account::Subscription.new(plan_key: "free_v1")
|
|
assert_equal Plan[:free_v1], subscription.plan
|
|
end
|
|
|
|
test "check if account is active" do
|
|
subscription = Account::Subscription.new(status: "active")
|
|
assert subscription.active?
|
|
end
|
|
|
|
test "check if account is paid" do
|
|
assert Account::Subscription.new(plan_key: "monthly_v1", status: "active").paid?
|
|
assert_not Account::Subscription.new(plan_key: "free_v1", status: "active").paid?
|
|
end
|
|
|
|
test "pause pauses Stripe subscription with void behavior" do
|
|
subscription = Account::Subscription.new(stripe_subscription_id: "sub_123")
|
|
|
|
Stripe::Subscription.expects(:update).with(
|
|
"sub_123",
|
|
pause_collection: { behavior: "void" }
|
|
).returns(true)
|
|
|
|
subscription.pause
|
|
end
|
|
|
|
test "pause does nothing when no stripe_subscription_id" do
|
|
subscription = Account::Subscription.new(stripe_subscription_id: nil)
|
|
|
|
Stripe::Subscription.expects(:update).never
|
|
|
|
subscription.pause
|
|
end
|
|
|
|
test "pause raises on Stripe errors" do
|
|
subscription = Account::Subscription.new(stripe_subscription_id: "sub_123")
|
|
|
|
Stripe::Subscription.stubs(:update).raises(
|
|
Stripe::APIConnectionError.new("Network error")
|
|
)
|
|
|
|
assert_raises Stripe::APIConnectionError do
|
|
subscription.pause
|
|
end
|
|
end
|
|
|
|
test "resume resumes Stripe subscription" do
|
|
subscription = Account::Subscription.new(stripe_subscription_id: "sub_123")
|
|
|
|
Stripe::Subscription.expects(:update).with(
|
|
"sub_123",
|
|
pause_collection: ""
|
|
).returns(true)
|
|
|
|
subscription.resume
|
|
end
|
|
|
|
test "resume does nothing when no stripe_subscription_id" do
|
|
subscription = Account::Subscription.new(stripe_subscription_id: nil)
|
|
|
|
Stripe::Subscription.expects(:update).never
|
|
|
|
subscription.resume
|
|
end
|
|
|
|
test "resume raises on Stripe errors" do
|
|
subscription = Account::Subscription.new(stripe_subscription_id: "sub_123")
|
|
|
|
Stripe::Subscription.stubs(:update).raises(
|
|
Stripe::AuthenticationError.new("Invalid API key")
|
|
)
|
|
|
|
assert_raises Stripe::AuthenticationError do
|
|
subscription.resume
|
|
end
|
|
end
|
|
|
|
test "cancel cancels Stripe subscription" do
|
|
subscription = Account::Subscription.new(stripe_subscription_id: "sub_123")
|
|
|
|
Stripe::Subscription.expects(:cancel).with("sub_123").returns(true)
|
|
|
|
subscription.cancel
|
|
end
|
|
|
|
test "cancel does nothing when no stripe_subscription_id" do
|
|
subscription = Account::Subscription.new(stripe_subscription_id: nil)
|
|
|
|
Stripe::Subscription.expects(:cancel).never
|
|
|
|
subscription.cancel
|
|
end
|
|
|
|
test "cancel treats 404 as success" do
|
|
subscription = Account::Subscription.new(stripe_subscription_id: "sub_deleted")
|
|
|
|
Stripe::Subscription.stubs(:cancel).raises(
|
|
Stripe::InvalidRequestError.new("No such subscription", {})
|
|
)
|
|
|
|
assert_nothing_raised do
|
|
subscription.cancel
|
|
end
|
|
end
|
|
|
|
test "cancel raises on other Stripe errors" do
|
|
subscription = Account::Subscription.new(stripe_subscription_id: "sub_123")
|
|
|
|
Stripe::Subscription.stubs(:cancel).raises(
|
|
Stripe::RateLimitError.new("Rate limit exceeded")
|
|
)
|
|
|
|
assert_raises Stripe::RateLimitError do
|
|
subscription.cancel
|
|
end
|
|
end
|
|
end
|