mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-23 15:19:38 +02:00
Subscription tests and domain (#2209)
* Save work * Subscriptions and trials domain * Store family ID on customer * Remove indirection of stripe calls * Test simplifications * Update brakeman * Fix stripe tests in CI * Update billing page to show subscription details * Remove legacy columns * Complete billing settings page * Fix hardcoded plan name * Handle subscriptions for self hosting mode * Lint fixes
This commit is contained in:
parent
8c10e87387
commit
5da4bb6dc3
40 changed files with 1041 additions and 233 deletions
|
@ -1,41 +1,78 @@
|
|||
require "test_helper"
|
||||
require "ostruct"
|
||||
|
||||
class SubscriptionsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
sign_in @user = users(:family_admin)
|
||||
sign_in @user = users(:empty)
|
||||
@family = @user.family
|
||||
|
||||
@mock_stripe = mock
|
||||
Provider::Registry.stubs(:get_provider).with(:stripe).returns(@mock_stripe)
|
||||
end
|
||||
|
||||
test "can start trial" do
|
||||
@user.update!(onboarded_at: nil)
|
||||
@user.family.update!(trial_started_at: nil, stripe_subscription_status: "incomplete")
|
||||
|
||||
assert_nil @user.onboarded_at
|
||||
assert_nil @user.family.trial_started_at
|
||||
|
||||
post start_trial_subscription_path
|
||||
assert_redirected_to root_path
|
||||
assert_equal "Welcome to Maybe!", flash[:notice]
|
||||
|
||||
assert @user.reload.onboarded?
|
||||
assert @user.family.reload.trial_started_at.present?
|
||||
end
|
||||
|
||||
test "if user re-enters onboarding, don't restart trial" do
|
||||
onboard_time = 1.day.ago
|
||||
trial_start_time = 1.day.ago
|
||||
|
||||
@user.update!(onboarded_at: onboard_time)
|
||||
@user.family.update!(trial_started_at: trial_start_time, stripe_subscription_status: "incomplete")
|
||||
|
||||
post start_trial_subscription_path
|
||||
assert_redirected_to root_path
|
||||
|
||||
assert @user.reload.family.trial_started_at < Date.current
|
||||
end
|
||||
|
||||
test "redirects to settings if self hosting" do
|
||||
test "disabled for self hosted users" do
|
||||
Rails.application.config.app_mode.stubs(:self_hosted?).returns(true)
|
||||
get subscription_path
|
||||
post subscription_path
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
# Trial subscriptions are managed internally and do NOT go through Stripe
|
||||
test "can create trial subscription" do
|
||||
@family.subscription.destroy
|
||||
@family.reload
|
||||
|
||||
assert_difference "Subscription.count", 1 do
|
||||
post subscription_path
|
||||
end
|
||||
|
||||
assert_redirected_to root_path
|
||||
assert_equal "Welcome to Maybe!", flash[:notice]
|
||||
assert_equal "trialing", @family.subscription.status
|
||||
assert_in_delta Subscription::TRIAL_DAYS.days.from_now, @family.subscription.trial_ends_at, 1.minute
|
||||
end
|
||||
|
||||
test "users who have already trialed cannot create a new subscription" do
|
||||
@family.start_trial_subscription!
|
||||
|
||||
assert_no_difference "Subscription.count" do
|
||||
post subscription_path
|
||||
end
|
||||
|
||||
assert_redirected_to root_path
|
||||
assert_equal "You have already started or completed a trial. Please upgrade to continue.", flash[:alert]
|
||||
end
|
||||
|
||||
test "creates new checkout session" do
|
||||
@mock_stripe.expects(:create_checkout_session).with(
|
||||
plan: "monthly",
|
||||
family_id: @family.id,
|
||||
family_email: @family.billing_email,
|
||||
success_url: success_subscription_url + "?session_id={CHECKOUT_SESSION_ID}",
|
||||
cancel_url: upgrade_subscription_url
|
||||
).returns(
|
||||
OpenStruct.new(
|
||||
url: "https://checkout.stripe.com/c/pay/test-session-id",
|
||||
customer_id: "test-customer-id"
|
||||
)
|
||||
)
|
||||
|
||||
get new_subscription_path(plan: "monthly")
|
||||
|
||||
assert_redirected_to "https://checkout.stripe.com/c/pay/test-session-id"
|
||||
assert_equal "test-customer-id", @family.reload.stripe_customer_id
|
||||
end
|
||||
|
||||
test "creates active subscription on checkout success" do
|
||||
@mock_stripe.expects(:get_checkout_result).with("test-session-id").returns(
|
||||
OpenStruct.new(
|
||||
success?: true,
|
||||
subscription_id: "test-subscription-id"
|
||||
)
|
||||
)
|
||||
|
||||
get success_subscription_url(session_id: "test-session-id")
|
||||
|
||||
assert @family.subscription.active?
|
||||
assert_equal "Welcome to Maybe! Your subscription has been created.", flash[:notice]
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue