Files
danbooru/test/functional/user_upgrades_controller_test.rb
evazion 74ed2a8b96 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.
2020-12-24 21:15:04 -06:00

189 lines
6.8 KiB
Ruby

require 'test_helper'
class UserUpgradesControllerTest < ActionDispatch::IntegrationTest
context "The user upgrades controller" do
context "new action" do
should "render for a self upgrade" do
@user = create(:user)
get_auth new_user_upgrade_path, @user
assert_response :success
end
should "render for a gift upgrade" do
@recipient = create(:user)
get_auth new_user_upgrade_path(user_id: @recipient.id), create(:user)
assert_response :success
end
should "render for an anonymous user" do
get new_user_upgrade_path
assert_response :success
end
end
context "show action" do
context "for a completed upgrade" do
should "render for a self upgrade" do
@user_upgrade = create(:self_gold_upgrade, status: "complete")
get_auth user_upgrade_path(@user_upgrade), @user_upgrade.purchaser
assert_response :success
end
should "render for a gift upgrade for the purchaser" do
@user_upgrade = create(:gift_gold_upgrade, status: "complete")
get_auth user_upgrade_path(@user_upgrade), @user_upgrade.purchaser
assert_response :success
end
should "render for a gift upgrade for the recipient" do
@user_upgrade = create(:gift_gold_upgrade, status: "complete")
get_auth user_upgrade_path(@user_upgrade), @user_upgrade.recipient
assert_response :success
end
should "render for the site owner" do
@user_upgrade = create(:self_gold_upgrade, status: "complete")
get_auth user_upgrade_path(@user_upgrade), create(:owner_user)
assert_response :success
end
should "be inaccessible to other users" do
@user_upgrade = create(:self_gold_upgrade, status: "complete")
get_auth user_upgrade_path(@user_upgrade), create(:user)
assert_response 403
end
end
context "for a pending upgrade" do
should "render" do
@user_upgrade = create(:self_gold_upgrade, status: "pending")
get_auth user_upgrade_path(@user_upgrade), @user_upgrade.purchaser
assert_response :success
end
end
end
context "create action" do
mock_stripe!
context "for a self upgrade" do
context "to Gold" do
should "create a pending upgrade" do
@user = create(:member_user)
post_auth user_upgrades_path(user_id: @user.id), @user, params: { upgrade_type: "gold" }, xhr: true
assert_response :success
@user_upgrade = @user.purchased_upgrades.last
assert_equal(@user, @user_upgrade.purchaser)
assert_equal(@user, @user_upgrade.recipient)
assert_equal("gold", @user_upgrade.upgrade_type)
assert_equal("pending", @user_upgrade.status)
assert_not_nil(@user_upgrade.stripe_id)
assert_match(/redirectToCheckout/, response.body)
end
end
context "to Platinum" do
should "create a pending upgrade" do
@user = create(:member_user)
post_auth user_upgrades_path(user_id: @user.id), @user, params: { upgrade_type: "platinum" }, xhr: true
assert_response :success
@user_upgrade = @user.purchased_upgrades.last
assert_equal(@user, @user_upgrade.purchaser)
assert_equal(@user, @user_upgrade.recipient)
assert_equal("platinum", @user_upgrade.upgrade_type)
assert_equal("pending", @user_upgrade.status)
assert_not_nil(@user_upgrade.stripe_id)
assert_match(/redirectToCheckout/, response.body)
end
end
context "from Gold to Platinum" do
should "create a pending upgrade" do
@user = create(:member_user)
post_auth user_upgrades_path(user_id: @user.id), @user, params: { upgrade_type: "gold_to_platinum" }, xhr: true
assert_response :success
@user_upgrade = @user.purchased_upgrades.last
assert_equal(@user, @user_upgrade.purchaser)
assert_equal(@user, @user_upgrade.recipient)
assert_equal("gold_to_platinum", @user_upgrade.upgrade_type)
assert_equal("pending", @user_upgrade.status)
assert_not_nil(@user_upgrade.stripe_id)
assert_match(/redirectToCheckout/, response.body)
end
end
end
context "for a gifted upgrade" do
context "to Gold" do
should "create a pending upgrade" do
@recipient = create(:member_user)
@purchaser = create(:member_user)
post_auth user_upgrades_path(user_id: @recipient.id), @purchaser, params: { upgrade_type: "gold" }, xhr: true
assert_response :success
@user_upgrade = @purchaser.purchased_upgrades.last
assert_equal(@purchaser, @user_upgrade.purchaser)
assert_equal(@recipient, @user_upgrade.recipient)
assert_equal("gold", @user_upgrade.upgrade_type)
assert_equal("pending", @user_upgrade.status)
assert_not_nil(@user_upgrade.stripe_id)
assert_match(/redirectToCheckout/, response.body)
end
end
context "to Platinum" do
should "create a pending upgrade" do
@recipient = create(:member_user)
@purchaser = create(:member_user)
post_auth user_upgrades_path(user_id: @recipient.id), @purchaser, params: { upgrade_type: "platinum" }, xhr: true
assert_response :success
@user_upgrade = @purchaser.purchased_upgrades.last
assert_equal(@purchaser, @user_upgrade.purchaser)
assert_equal(@recipient, @user_upgrade.recipient)
assert_equal("platinum", @user_upgrade.upgrade_type)
assert_equal("pending", @user_upgrade.status)
assert_not_nil(@user_upgrade.stripe_id)
assert_match(/redirectToCheckout/, response.body)
end
end
context "from Gold to Platinum" do
should "create a pending upgrade" do
@recipient = create(:gold_user)
@purchaser = create(:member_user)
post_auth user_upgrades_path(user_id: @recipient.id), @purchaser, params: { upgrade_type: "gold_to_platinum" }, xhr: true
assert_response :success
@user_upgrade = @purchaser.purchased_upgrades.last
assert_equal(@purchaser, @user_upgrade.purchaser)
assert_equal(@recipient, @user_upgrade.recipient)
assert_equal("gold_to_platinum", @user_upgrade.upgrade_type)
assert_equal("pending", @user_upgrade.status)
assert_not_nil(@user_upgrade.stripe_id)
assert_match(/redirectToCheckout/, response.body)
end
end
end
end
end
end