user upgrades: add UserUpgrade model.

Add a model to store the status of user upgrades.

* Store the upgrade purchaser and the upgrade receiver (these are
  different for a gifted upgrade, the same for a self upgrade).
* Store the upgrade type: gold, platinum, or gold-to-platinum upgrades.
* Store the upgrade status:
** pending: User is still on the Stripe checkout page, no payment
   received yet.
** processing: User has completed checkout, but the checkout status in
   Stripe is still 'unpaid'.
** complete: We've received notification from Stripe that the payment
   has gone through and the user has been upgraded.
* Store the Stripe checkout ID, to cross-reference the upgrade record on
  Danbooru with the checkout record on Stripe.

This is the upgrade flow:

* When the user clicks the upgrade button on the upgrade page, we call
  POST /user_upgrades and create a pending UserUpgrade.
* We redirect the user to the checkout page on Stripe.
* When the user completes checkout on Stripe, Stripe sends us a webhook
  notification at POST /webhooks/receive.
* When we receive the webhook, we check the payment status, and if it's
  paid we mark the UserUpgrade as complete and upgrade the user.
* After Stripe sees that we have successfully processed the webhook,
  they redirect the user to the /user_upgrades/:id page, where we show
  the user their upgrade receipt.
This commit is contained in:
evazion
2020-12-24 06:40:20 -06:00
parent 7762489d7d
commit 74ed2a8b96
13 changed files with 502 additions and 164 deletions

View File

@@ -3,37 +3,45 @@ require 'test_helper'
class UserUpgradeTest < ActiveSupport::TestCase
context "UserUpgrade:" do
context "the #process_upgrade! method" do
setup do
@user = create(:user)
@user_upgrade = UserUpgrade.new(recipient: @user, purchaser: @user, level: User::Levels::GOLD)
end
should "update the user's level" do
@user_upgrade.process_upgrade!
assert_equal(User::Levels::GOLD, @user.reload.level)
end
should "log an account upgrade modaction" do
assert_difference("ModAction.user_account_upgrade.count") do
@user_upgrade.process_upgrade!
end
end
should "send the user a dmail" do
assert_difference("@user.dmails.received.count") do
@user_upgrade.process_upgrade!
end
end
context "for an upgrade for a user above Platinum level" do
should "not demote the user" do
@user.update!(level: User::Levels::BUILDER)
assert_raise(User::PrivilegeError) do
@user_upgrade.process_upgrade!
context "for a self upgrade" do
context "to Gold" do
setup do
@user_upgrade = create(:self_gold_upgrade)
end
assert_equal(true, @user.reload.is_builder?)
should "update the user's level if the payment status is paid" do
@user_upgrade.process_upgrade!("paid")
assert_equal(User::Levels::GOLD, @user_upgrade.recipient.level)
assert_equal("complete", @user_upgrade.status)
end
should "not update the user's level if the payment is unpaid" do
@user_upgrade.process_upgrade!("unpaid")
assert_equal(User::Levels::MEMBER, @user_upgrade.recipient.level)
assert_equal("processing", @user_upgrade.status)
end
should "not update the user's level if the upgrade status is complete" do
@user_upgrade.update!(status: "complete")
@user_upgrade.process_upgrade!("paid")
assert_equal(User::Levels::MEMBER, @user_upgrade.recipient.level)
assert_equal("complete", @user_upgrade.status)
end
should "log an account upgrade modaction" do
assert_difference("ModAction.user_account_upgrade.count") do
@user_upgrade.process_upgrade!("paid")
end
end
should "send the recipient a dmail" do
assert_difference("@user_upgrade.recipient.dmails.received.count") do
@user_upgrade.process_upgrade!("paid")
end
end
end
end
end