2020-03-13 17:10:38 +01:00
|
|
|
# frozen_string_literal: true
|
2016-04-06 17:10:15 +02:00
|
|
|
|
2020-12-14 17:42:23 +01:00
|
|
|
require 'test_helper'
|
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
module Subscriptions; end
|
|
|
|
|
2020-03-13 17:10:38 +01:00
|
|
|
class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest
|
2016-04-06 17:10:15 +02:00
|
|
|
setup do
|
2016-11-23 16:30:19 +01:00
|
|
|
@user = User.find_by(username: 'jdupond')
|
2016-04-06 17:10:15 +02:00
|
|
|
login_as(@user, scope: :user)
|
|
|
|
end
|
|
|
|
|
2016-04-07 17:46:23 +02:00
|
|
|
test 'user successfully takes a subscription' do
|
2016-04-06 17:10:15 +02:00
|
|
|
plan = Plan.find_by(group_id: @user.group.id, type: 'Plan', base_name: 'Mensuel')
|
|
|
|
|
2018-12-12 15:27:12 +01:00
|
|
|
VCR.use_cassette('subscriptions_user_create_success') do
|
2021-04-12 10:45:41 +02:00
|
|
|
post '/api/stripe/confirm_payment',
|
2020-03-13 17:10:38 +01:00
|
|
|
params: {
|
2019-09-11 16:19:24 +02:00
|
|
|
payment_method_id: stripe_payment_method,
|
|
|
|
cart_items: {
|
2021-05-19 18:12:52 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: plan.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2016-04-06 17:10:15 +02:00
|
|
|
}
|
2020-03-13 17:10:38 +01:00
|
|
|
}.to_json, headers: default_headers
|
2016-04-06 18:17:33 +02:00
|
|
|
end
|
2016-04-06 17:10:15 +02:00
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 201, response.status, response.body
|
2020-03-13 17:10:38 +01:00
|
|
|
assert_equal Mime[:json], response.content_type
|
2016-04-06 17:10:15 +02:00
|
|
|
|
|
|
|
# Check the correct plan was subscribed
|
2021-05-31 11:52:53 +02:00
|
|
|
result = json_response(response.body)
|
|
|
|
assert_equal Invoice.last.id, result[:id], 'invoice id does not match'
|
|
|
|
subscription = Invoice.find(result[:id]).invoice_items.first.object
|
|
|
|
assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match'
|
2016-04-06 17:10:15 +02:00
|
|
|
|
2018-12-12 15:27:12 +01:00
|
|
|
# Check that the user has only one subscription
|
|
|
|
assert_equal 1, @user.subscriptions.count
|
|
|
|
|
2016-04-06 17:10:15 +02:00
|
|
|
# Check that the user has the correct subscription
|
|
|
|
assert_not_nil @user.subscription, "user's subscription was not found"
|
|
|
|
assert_not_nil @user.subscription.plan, "user's subscribed plan was not found"
|
|
|
|
assert_equal plan.id, @user.subscription.plan_id, "user's plan does not match"
|
|
|
|
|
|
|
|
# Check that the training credits were set correctly
|
|
|
|
assert_empty @user.training_credits, 'training credits were not reset'
|
|
|
|
assert_equal @user.subscription.plan.training_credit_nb, plan.training_credit_nb, 'trainings credits were not allocated'
|
|
|
|
|
|
|
|
# Check that the user benefit from prices of his plan
|
2016-11-23 16:30:19 +01:00
|
|
|
printer = Machine.find_by(slug: 'imprimante-3d')
|
2018-12-12 15:27:12 +01:00
|
|
|
assert_equal 15,
|
2019-08-14 10:54:19 +02:00
|
|
|
(printer.prices.find_by(group_id: @user.group_id, plan_id: @user.subscription.plan_id).amount / 100.00),
|
2018-12-12 15:27:12 +01:00
|
|
|
'machine hourly price does not match'
|
2016-04-06 17:10:15 +02:00
|
|
|
|
|
|
|
# Check notifications were sent for every admins
|
2018-12-12 15:27:12 +01:00
|
|
|
notifications = Notification.where(
|
2022-12-28 17:51:27 +01:00
|
|
|
notification_type_id: NotificationType.find_by_name('notify_admin_subscribed_plan'), # rubocop:disable Rails/DynamicFindBy
|
2018-12-12 15:27:12 +01:00
|
|
|
attached_object_type: 'Subscription',
|
|
|
|
attached_object_id: subscription[:id]
|
|
|
|
)
|
2016-04-06 17:10:15 +02:00
|
|
|
assert_not_empty notifications, 'no notifications were created'
|
2018-12-12 15:27:12 +01:00
|
|
|
notified_users_ids = notifications.map(&:receiver_id)
|
2016-04-06 17:10:15 +02:00
|
|
|
User.admins.each do |adm|
|
|
|
|
assert_includes notified_users_ids, adm.id, "Admin #{adm.id} was not notified"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check generated invoice
|
2021-05-27 15:58:55 +02:00
|
|
|
item = InvoiceItem.find_by(object_type: 'Subscription', object_id: subscription[:id])
|
|
|
|
invoice = item.invoice
|
2016-04-11 15:31:25 +02:00
|
|
|
assert_invoice_pdf invoice
|
2021-05-31 12:19:28 +02:00
|
|
|
assert_not_nil invoice.debug_footprint
|
2016-04-06 17:10:15 +02:00
|
|
|
assert_equal plan.amount, invoice.total, 'Invoice total price does not match the bought subscription'
|
|
|
|
end
|
|
|
|
|
2016-04-07 17:46:23 +02:00
|
|
|
test 'user fails to take a subscription' do
|
2016-04-07 12:28:25 +02:00
|
|
|
# get plan for wrong group
|
|
|
|
plan = Plan.where.not(group_id: @user.group.id).first
|
|
|
|
|
2018-12-12 15:27:12 +01:00
|
|
|
VCR.use_cassette('subscriptions_user_create_failed') do
|
2021-04-12 10:45:41 +02:00
|
|
|
post '/api/stripe/confirm_payment',
|
2020-03-13 17:10:38 +01:00
|
|
|
params: {
|
2019-09-11 16:19:24 +02:00
|
|
|
payment_method_id: stripe_payment_method,
|
|
|
|
cart_items: {
|
2021-05-19 18:12:52 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: plan.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2018-12-12 15:27:12 +01:00
|
|
|
}
|
2020-03-13 17:10:38 +01:00
|
|
|
}.to_json, headers: default_headers
|
2016-04-07 12:28:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 422, response.status, response.body
|
2020-03-13 17:10:38 +01:00
|
|
|
assert_equal Mime[:json], response.content_type
|
2016-04-07 12:28:25 +02:00
|
|
|
|
|
|
|
# Check the error was handled
|
2022-12-28 17:51:27 +01:00
|
|
|
assert_match(/plan is reserved for members of group/, response.body)
|
2016-04-07 12:28:25 +02:00
|
|
|
|
|
|
|
# Check that the user has no subscription
|
|
|
|
assert_nil @user.subscription, "user's subscription was found"
|
|
|
|
end
|
|
|
|
|
2016-07-11 15:32:42 +02:00
|
|
|
test 'user successfully takes a subscription with wallet' do
|
|
|
|
@vlonchamp = User.find_by(username: 'vlonchamp')
|
|
|
|
login_as(@vlonchamp, scope: :user)
|
|
|
|
plan = Plan.find_by(group_id: @vlonchamp.group.id, type: 'Plan', base_name: 'Mensuel tarif réduit')
|
|
|
|
|
2018-12-12 15:27:12 +01:00
|
|
|
VCR.use_cassette('subscriptions_user_create_success_with_wallet') do
|
2021-04-12 10:45:41 +02:00
|
|
|
post '/api/stripe/confirm_payment',
|
2020-03-13 17:10:38 +01:00
|
|
|
params: {
|
2019-09-11 16:19:24 +02:00
|
|
|
payment_method_id: stripe_payment_method,
|
|
|
|
cart_items: {
|
2021-05-19 18:12:52 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: plan.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2016-07-11 15:32:42 +02:00
|
|
|
}
|
2020-03-13 17:10:38 +01:00
|
|
|
}.to_json, headers: default_headers
|
2016-07-11 15:32:42 +02:00
|
|
|
end
|
|
|
|
|
2019-09-11 16:19:24 +02:00
|
|
|
@vlonchamp.wallet.reload
|
|
|
|
|
2016-07-11 15:32:42 +02:00
|
|
|
# Check response format & status
|
|
|
|
assert_equal 201, response.status, response.body
|
2020-03-13 17:10:38 +01:00
|
|
|
assert_equal Mime[:json], response.content_type
|
2016-07-11 15:32:42 +02:00
|
|
|
|
|
|
|
# Check the correct plan was subscribed
|
2021-05-31 11:52:53 +02:00
|
|
|
result = json_response(response.body)
|
|
|
|
assert_equal Invoice.last.id, result[:id], 'invoice id does not match'
|
|
|
|
subscription = Invoice.find(result[:id]).invoice_items.first.object
|
|
|
|
assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match'
|
2016-07-11 15:32:42 +02:00
|
|
|
|
|
|
|
# Check that the user has the correct subscription
|
|
|
|
assert_not_nil @vlonchamp.subscription, "user's subscription was not found"
|
|
|
|
assert_not_nil @vlonchamp.subscription.plan, "user's subscribed plan was not found"
|
|
|
|
assert_equal plan.id, @vlonchamp.subscription.plan_id, "user's plan does not match"
|
|
|
|
|
|
|
|
# Check that the training credits were set correctly
|
|
|
|
assert_empty @vlonchamp.training_credits, 'training credits were not reset'
|
2018-12-12 15:27:12 +01:00
|
|
|
assert_equal @vlonchamp.subscription.plan.training_credit_nb,
|
|
|
|
plan.training_credit_nb,
|
|
|
|
'trainings credits were not allocated'
|
2016-07-11 15:32:42 +02:00
|
|
|
|
|
|
|
# Check that the user benefit from prices of his plan
|
2016-11-23 16:30:19 +01:00
|
|
|
printer = Machine.find_by(slug: 'imprimante-3d')
|
2018-12-12 15:27:12 +01:00
|
|
|
assert_equal 10,
|
|
|
|
(printer.prices.find_by(
|
|
|
|
group_id: @vlonchamp.group_id,
|
2020-03-13 17:10:38 +01:00
|
|
|
plan_id: @vlonchamp.subscription.plan_id
|
|
|
|
).amount / 100.00
|
2018-12-12 15:27:12 +01:00
|
|
|
),
|
|
|
|
'machine hourly price does not match'
|
2016-07-11 15:32:42 +02:00
|
|
|
|
|
|
|
# Check notifications were sent for every admins
|
2018-12-12 15:27:12 +01:00
|
|
|
notifications = Notification.where(
|
2022-12-28 17:51:27 +01:00
|
|
|
notification_type_id: NotificationType.find_by_name('notify_admin_subscribed_plan'), # rubocop:disable Rails/DynamicFindBy
|
2018-12-12 15:27:12 +01:00
|
|
|
attached_object_type: 'Subscription',
|
|
|
|
attached_object_id: subscription[:id]
|
|
|
|
)
|
2016-07-11 15:32:42 +02:00
|
|
|
assert_not_empty notifications, 'no notifications were created'
|
2018-12-12 15:27:12 +01:00
|
|
|
notified_users_ids = notifications.map(&:receiver_id)
|
2016-07-11 15:32:42 +02:00
|
|
|
User.admins.each do |adm|
|
|
|
|
assert_includes notified_users_ids, adm.id, "Admin #{adm.id} was not notified"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check generated invoice
|
2021-05-27 15:58:55 +02:00
|
|
|
item = InvoiceItem.find_by(object_type: 'Subscription', object_id: subscription[:id])
|
|
|
|
invoice = item.invoice
|
2016-07-11 15:32:42 +02:00
|
|
|
assert_invoice_pdf invoice
|
2021-05-31 12:19:28 +02:00
|
|
|
assert_not_nil invoice.debug_footprint
|
2016-07-11 15:32:42 +02:00
|
|
|
assert_equal plan.amount, invoice.total, 'Invoice total price does not match the bought subscription'
|
|
|
|
|
|
|
|
# wallet
|
2019-09-11 16:19:24 +02:00
|
|
|
assert_equal 0, @vlonchamp.wallet.amount
|
|
|
|
assert_equal 2, @vlonchamp.wallet.wallet_transactions.count
|
2016-07-11 15:32:42 +02:00
|
|
|
transaction = @vlonchamp.wallet.wallet_transactions.last
|
2019-09-11 16:19:24 +02:00
|
|
|
assert_equal 'debit', transaction.transaction_type
|
|
|
|
assert_equal 10, transaction.amount
|
|
|
|
assert_equal invoice.wallet_amount / 100.0, transaction.amount
|
|
|
|
assert_equal invoice.wallet_transaction_id, transaction.id
|
2016-07-11 15:32:42 +02:00
|
|
|
end
|
2020-12-14 17:42:23 +01:00
|
|
|
|
|
|
|
test 'user takes a subscription with payment schedule' do
|
|
|
|
plan = Plan.find_by(group_id: @user.group.id, type: 'Plan', base_name: 'Abonnement mensualisable')
|
2020-12-15 16:53:11 +01:00
|
|
|
payment_schedule_count = PaymentSchedule.count
|
|
|
|
payment_schedule_items_count = PaymentScheduleItem.count
|
2020-12-14 17:42:23 +01:00
|
|
|
|
2020-12-15 15:48:13 +01:00
|
|
|
VCR.use_cassette('subscriptions_user_create_with_payment_schedule') do
|
2021-10-14 18:20:10 +02:00
|
|
|
post '/api/stripe/setup_subscription',
|
2021-09-13 14:25:30 +02:00
|
|
|
params: {
|
|
|
|
payment_method_id: stripe_payment_method,
|
|
|
|
cart_items: {
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: plan.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
payment_schedule: true,
|
|
|
|
payment_method: 'cart'
|
|
|
|
}
|
|
|
|
}.to_json, headers: default_headers
|
2020-12-14 17:42:23 +01:00
|
|
|
|
2020-12-15 15:48:13 +01:00
|
|
|
# Check response format & status
|
2021-10-14 18:20:10 +02:00
|
|
|
assert_equal 201, response.status, response.body
|
2020-12-15 15:48:13 +01:00
|
|
|
assert_equal Mime[:json], response.content_type
|
2020-12-14 17:42:23 +01:00
|
|
|
|
2020-12-15 15:48:13 +01:00
|
|
|
# Check the response
|
2021-09-13 14:25:30 +02:00
|
|
|
sub = json_response(response.body)
|
|
|
|
assert_not_nil sub[:id]
|
2021-10-14 18:20:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Check generalities
|
|
|
|
assert_equal payment_schedule_count + 1, PaymentSchedule.count, 'missing the payment schedule'
|
|
|
|
assert_equal payment_schedule_items_count + 12, PaymentScheduleItem.count, 'missing some payment schedule items'
|
|
|
|
|
|
|
|
# Check the correct plan was subscribed
|
|
|
|
result = json_response(response.body)
|
|
|
|
assert_equal PaymentSchedule.last.id, result[:id], 'payment schedule id does not match'
|
|
|
|
subscription = PaymentSchedule.find(result[:id]).payment_schedule_objects.first.object
|
|
|
|
assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match'
|
|
|
|
|
|
|
|
# Check that the user has the correct subscription
|
|
|
|
assert_not_nil @user.subscription, "user's subscription was not found"
|
|
|
|
assert_not_nil @user.subscription.plan, "user's subscribed plan was not found"
|
|
|
|
assert_equal plan.id, @user.subscription.plan_id, "user's plan does not match"
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'user takes a subscription but does not confirm 3DS' do
|
|
|
|
plan = Plan.find_by(group_id: @user.group.id, type: 'Plan', base_name: 'Abonnement mensualisable')
|
|
|
|
payment_schedule_count = PaymentSchedule.count
|
|
|
|
payment_schedule_items_count = PaymentScheduleItem.count
|
2020-12-15 15:48:13 +01:00
|
|
|
|
2021-10-14 18:20:10 +02:00
|
|
|
VCR.use_cassette('subscriptions_user_create_without_3ds_confirmation') do
|
|
|
|
post '/api/stripe/setup_subscription',
|
2020-12-14 17:42:23 +01:00
|
|
|
params: {
|
2021-10-14 18:20:10 +02:00
|
|
|
payment_method_id: stripe_payment_method(error: :require_3ds),
|
|
|
|
cart_items: {
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: plan.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
payment_schedule: true,
|
|
|
|
payment_method: 'cart'
|
|
|
|
}
|
|
|
|
}.to_json, headers: default_headers
|
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 200, response.status, response.body
|
|
|
|
assert_equal Mime[:json], response.content_type
|
|
|
|
|
|
|
|
# Check the response
|
|
|
|
res = json_response(response.body)
|
|
|
|
assert res[:requires_action]
|
|
|
|
assert_not_nil res[:payment_intent_client_secret]
|
|
|
|
assert_not_nil res[:subscription_id]
|
|
|
|
assert_equal 'subscription', res[:type]
|
|
|
|
|
|
|
|
# try to confirm the subscription
|
|
|
|
post '/api/stripe/confirm_subscription',
|
|
|
|
params: {
|
|
|
|
subscription_id: res[:subscription_id],
|
2020-12-14 17:42:23 +01:00
|
|
|
cart_items: {
|
2021-05-19 18:12:52 +02:00
|
|
|
payment_schedule: true,
|
2021-09-13 14:25:30 +02:00
|
|
|
payment_method: 'card',
|
2021-05-19 18:12:52 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: plan.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2020-12-15 15:48:13 +01:00
|
|
|
}
|
2020-12-14 17:42:23 +01:00
|
|
|
}.to_json, headers: default_headers
|
|
|
|
end
|
|
|
|
|
2020-12-15 16:53:11 +01:00
|
|
|
# Check generalities
|
2021-10-14 18:20:10 +02:00
|
|
|
assert_equal 200, response.status, response.body
|
2020-12-14 17:42:23 +01:00
|
|
|
assert_equal Mime[:json], response.content_type
|
|
|
|
|
2021-10-14 18:20:10 +02:00
|
|
|
res = json_response(response.body)
|
|
|
|
assert res[:requires_action]
|
|
|
|
assert_not_nil res[:payment_intent_client_secret]
|
|
|
|
assert_not_nil res[:subscription_id]
|
|
|
|
assert_equal 'subscription', res[:type]
|
2020-12-14 17:42:23 +01:00
|
|
|
|
2021-10-14 18:20:10 +02:00
|
|
|
assert_equal payment_schedule_count, PaymentSchedule.count, 'the payment schedule was created anyway'
|
|
|
|
assert_equal payment_schedule_items_count, PaymentScheduleItem.count, 'some payment schedule items were created anyway'
|
|
|
|
|
|
|
|
# Check that the user has no subscription
|
|
|
|
assert_nil @user.subscription, "user's subscription was not found"
|
2020-12-14 17:42:23 +01:00
|
|
|
end
|
2016-07-11 15:32:42 +02:00
|
|
|
end
|