Files
danbooru/test/functional/webhooks_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

146 lines
5.5 KiB
Ruby

require 'test_helper'
class WebhooksControllerTest < ActionDispatch::IntegrationTest
mock_stripe!
def post_webhook(*args, payment_status: "paid", **metadata)
event = StripeMock.mock_webhook_event(*args, payment_status: payment_status, metadata: metadata)
signature = generate_stripe_signature(event)
headers = { "Stripe-Signature": signature }
post receive_webhooks_path(source: "stripe"), headers: headers, params: event, as: :json
end
# https://github.com/stripe-ruby-mock/stripe-ruby-mock/issues/467#issuecomment-634674913
# https://stripe.com/docs/webhooks/signatures
def generate_stripe_signature(event)
time = Time.now
secret = UserUpgrade.stripe_webhook_secret
signature = Stripe::Webhook::Signature.compute_signature(time, event.to_json, secret)
Stripe::Webhook::Signature.generate_header(time, signature, scheme: Stripe::Webhook::Signature::EXPECTED_SCHEME)
end
context "The webhooks controller" do
context "receive action" do
context "for a request from an unrecognized source" do
should "fail" do
post receive_webhooks_path(source: "blah")
assert_response 400
end
end
context "for a Stripe webhook" do
context "with a missing signature" do
should "fail" do
event = StripeMock.mock_webhook_event("payment_intent.created")
post receive_webhooks_path(source: "stripe"), params: event, as: :json
assert_response 400
end
end
context "with an invalid signature" do
should "fail" do
event = StripeMock.mock_webhook_event("payment_intent.created")
headers = { "Stripe-Signature": "blah" }
post receive_webhooks_path(source: "stripe"), headers: headers, params: event, as: :json
assert_response 400
end
end
context "for a payment_intent.created event" do
should "work" do
post_webhook("payment_intent.created")
assert_response 200
end
end
context "for a checkout.session.completed event" do
context "for completed event with an unpaid payment status" do
should "not upgrade the user" do
@user_upgrade = create(:self_gold_upgrade)
post_webhook("checkout.session.completed", { user_upgrade_id: @user_upgrade.id, payment_status: "unpaid" })
assert_response 200
assert_equal("processing", @user_upgrade.reload.status)
assert_equal(User::Levels::MEMBER, @user_upgrade.recipient.reload.level)
end
end
context "for a self upgrade" do
context "to Gold" do
should "upgrade the user" do
@user_upgrade = create(:self_gold_upgrade)
post_webhook("checkout.session.completed", { user_upgrade_id: @user_upgrade.id })
assert_response 200
assert_equal("complete", @user_upgrade.reload.status)
assert_equal(User::Levels::GOLD, @user_upgrade.recipient.reload.level)
end
end
context "to Platinum" do
should "upgrade the user" do
@user_upgrade = create(:self_platinum_upgrade)
post_webhook("checkout.session.completed", { user_upgrade_id: @user_upgrade.id })
assert_response 200
assert_equal("complete", @user_upgrade.reload.status)
assert_equal(User::Levels::PLATINUM, @user_upgrade.recipient.reload.level)
end
end
context "from Gold to Platinum" do
should "upgrade the user" do
@user_upgrade = create(:self_gold_to_platinum_upgrade)
post_webhook("checkout.session.completed", { user_upgrade_id: @user_upgrade.id })
assert_response 200
assert_equal("complete", @user_upgrade.reload.status)
assert_equal(User::Levels::PLATINUM, @user_upgrade.recipient.reload.level)
end
end
end
context "for a gifted upgrade" do
context "to Gold" do
should "upgrade the user" do
@user_upgrade = create(:gift_gold_upgrade)
post_webhook("checkout.session.completed", { user_upgrade_id: @user_upgrade.id })
assert_response 200
assert_equal("complete", @user_upgrade.reload.status)
assert_equal(User::Levels::GOLD, @user_upgrade.recipient.reload.level)
end
end
context "to Platinum" do
should "upgrade the user" do
@user_upgrade = create(:gift_platinum_upgrade)
post_webhook("checkout.session.completed", { user_upgrade_id: @user_upgrade.id })
assert_response 200
assert_equal("complete", @user_upgrade.reload.status)
assert_equal(User::Levels::PLATINUM, @user_upgrade.recipient.reload.level)
end
end
context "from Gold to Platinum" do
should "upgrade the user" do
@user_upgrade = create(:gift_gold_to_platinum_upgrade)
post_webhook("checkout.session.completed", { user_upgrade_id: @user_upgrade.id })
assert_response 200
assert_equal("complete", @user_upgrade.reload.status)
assert_equal(User::Levels::PLATINUM, @user_upgrade.recipient.reload.level)
end
end
end
end
end
end
end
end