1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/test/integration/subscriptions/renew_as_user_test.rb

106 lines
3.9 KiB
Ruby
Raw Normal View History

2016-04-07 17:46:23 +02:00
class Subscriptions::RenewAsUserTest < ActionDispatch::IntegrationTest
setup do
2016-11-23 16:30:19 +01:00
@user = User.find_by(username: 'lseguin')
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-11 17:27:25 +01:00
stripe_subscription = nil
2016-04-07 17:46:23 +02:00
VCR.use_cassette('subscriptions_user_renew_success', erb: true) do
2016-04-07 17:46:23 +02:00
post '/api/subscriptions',
{
subscription: {
plan_id: plan.id,
user_id: @user.id,
card_token: stripe_card_token
}
}.to_json, default_headers
2018-12-11 17:27:25 +01:00
stripe_subscription = Stripe::Customer.retrieve(@user.stp_customer_id).subscriptions.retrieve(@user.subscription.stp_subscription_id)
2016-04-07 17:46:23 +02:00
end
# Check response format & status
assert_equal 201, response.status, "API does not return the expected status. #{response.body}"
2016-04-07 17:46:23 +02:00
assert_equal Mime::JSON, response.content_type
# Check the correct plan was subscribed
subscription = json_response(response.body)
assert_equal plan.id, subscription[:plan_id], 'subscribed plan does not match'
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
assert (@user.subscription.expired_at > DateTime.now),
"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,
Time.at(stripe_subscription.current_period_end).iso8601,
'subscription expiration date does not match'
assert_in_delta 5,
(DateTime.now.to_i - @user.subscription.updated_at.to_i),
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
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'
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
invoice = Invoice.find_by(invoiced_type: 'Subscription', invoiced_id: subscription[:id])
assert_invoice_pdf invoice
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
VCR.use_cassette('subscriptions_user_renew_failed') do
2016-04-07 17:46:23 +02:00
post '/api/subscriptions',
{
subscription: {
plan_id: plan.id,
user_id: @user.id,
card_token: 'invalid_card_token'
}
2016-04-07 17:46:23 +02:00
}.to_json, default_headers
end
# Check response format & status
assert_equal 422, response.status, "API does not return the expected status. #{response.body}"
2016-04-07 17:46:23 +02:00
assert_equal Mime::JSON, response.content_type
# Check the error was handled
assert_match /No such token/, response.body
2016-04-07 17:46:23 +02:00
# Check that the user's subscription has not changed
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
end