2020-03-13 17:10:38 +01:00
|
|
|
# frozen_string_literal: true
|
2016-04-07 17:46:23 +02:00
|
|
|
|
2021-01-04 16:26:44 +01:00
|
|
|
require 'test_helper'
|
|
|
|
|
2020-03-13 17:10:38 +01:00
|
|
|
class Subscriptions::RenewAsUserTest < ActionDispatch::IntegrationTest
|
2016-04-07 17:46:23 +02:00
|
|
|
setup do
|
2021-01-04 16:26:44 +01:00
|
|
|
@user = User.find_by(username: 'atiermoulin')
|
2016-04-07 17:46:23 +02:00
|
|
|
login_as(@user, scope: :user)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'user successfully renew a subscription after it has ended' do
|
|
|
|
plan = Plan.find_by(group_id: @user.group.id, type: 'Plan', base_name: 'Mensuel')
|
|
|
|
|
2018-12-06 18:26:01 +01:00
|
|
|
VCR.use_cassette('subscriptions_user_renew_success', erb: true) 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-07 17:46:23 +02:00
|
|
|
}
|
2020-03-13 17:10:38 +01:00
|
|
|
}.to_json, headers: default_headers
|
2016-04-07 17:46:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Check response format & status
|
2018-12-06 18:26:01 +01:00
|
|
|
assert_equal 201, response.status, "API does not return the expected status. #{response.body}"
|
2020-03-13 17:10:38 +01:00
|
|
|
assert_equal Mime[:json], response.content_type
|
2016-04-07 17:46:23 +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-07 17:46:23 +02:00
|
|
|
|
2018-12-12 15:27:12 +01:00
|
|
|
# Check the subscription was correctly saved
|
|
|
|
assert_equal 2, @user.subscriptions.count
|
|
|
|
|
2016-04-07 17:46:23 +02:00
|
|
|
# Check that the user has the correct subscription
|
|
|
|
assert_not_nil @user.subscription, "user's subscription was not found"
|
2018-12-11 17:27:25 +01:00
|
|
|
|
|
|
|
# Check the expiration date
|
2023-02-14 13:10:58 +01:00
|
|
|
assert @user.subscription.expired_at > Time.current,
|
2018-12-06 18:26:01 +01:00
|
|
|
"user's subscription expiration was not updated ... VCR cassettes may be outdated, please check the gitlab wiki"
|
2018-12-11 17:27:25 +01:00
|
|
|
assert_equal @user.subscription.expired_at.iso8601,
|
2019-09-11 16:19:24 +02:00
|
|
|
(@user.subscription.created_at + plan.duration).iso8601,
|
2018-12-11 17:27:25 +01:00
|
|
|
'subscription expiration date does not match'
|
|
|
|
|
2018-12-06 18:26:01 +01:00
|
|
|
assert_in_delta 5,
|
2023-02-14 13:10:58 +01:00
|
|
|
(Time.current.to_i - @user.subscription.updated_at.to_i),
|
2018-12-06 18:26:01 +01:00
|
|
|
10,
|
|
|
|
"user's subscription was not updated recently"
|
2016-04-07 17:46:23 +02:00
|
|
|
|
|
|
|
# Check that the credits were reset correctly
|
|
|
|
assert_empty @user.users_credits, 'credits were not reset'
|
|
|
|
|
|
|
|
# Check notifications were sent for every admins
|
2018-12-06 18:26:01 +01:00
|
|
|
notifications = Notification.where(
|
|
|
|
notification_type_id: NotificationType.find_by_name('notify_admin_subscribed_plan'),
|
|
|
|
attached_object_type: 'Subscription',
|
|
|
|
attached_object_id: subscription[:id]
|
|
|
|
)
|
2016-04-07 17:46:23 +02:00
|
|
|
assert_not_empty notifications, 'no notifications were created'
|
2018-12-06 18:26:01 +01:00
|
|
|
notified_users_ids = notifications.map(&:receiver_id)
|
2016-04-07 17:46:23 +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-07 17:46:23 +02:00
|
|
|
assert_equal plan.amount, invoice.total, 'Invoice total price does not match the bought subscription'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'user fails to renew a subscription' do
|
|
|
|
plan = Plan.find_by(group_id: @user.group.id, type: 'Plan', base_name: 'Mensuel')
|
|
|
|
|
|
|
|
previous_expiration = @user.subscription.expired_at.to_i
|
|
|
|
|
2018-12-06 18:26:01 +01:00
|
|
|
VCR.use_cassette('subscriptions_user_renew_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(error: :card_declined),
|
|
|
|
cart_items: {
|
2021-05-19 18:12:52 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: plan.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2018-12-06 18:26:01 +01:00
|
|
|
}
|
2020-03-13 17:10:38 +01:00
|
|
|
}.to_json, headers: default_headers
|
2016-04-07 17:46:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Check response format & status
|
2019-09-11 16:19:24 +02:00
|
|
|
assert_equal 200, response.status, "API does not return the expected status. #{response.body}"
|
2020-03-13 17:10:38 +01:00
|
|
|
assert_equal Mime[:json], response.content_type
|
2016-04-07 17:46:23 +02:00
|
|
|
|
|
|
|
# Check the error was handled
|
2022-07-19 12:13:50 +02:00
|
|
|
assert_match(/Your card was declined/, response.body)
|
2016-04-07 17:46:23 +02:00
|
|
|
|
|
|
|
# Check that the user's subscription has not changed
|
2016-04-11 18:37:06 +02:00
|
|
|
assert_equal previous_expiration, @user.subscription.expired_at.to_i, "user's subscription has changed"
|
2018-12-12 15:27:12 +01:00
|
|
|
|
|
|
|
# Check the subscription was not saved
|
|
|
|
assert_equal 1, @user.subscriptions.count
|
2016-04-07 17:46:23 +02:00
|
|
|
end
|
2016-06-03 10:10:13 +02:00
|
|
|
end
|