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_admin_test.rb

150 lines
6.0 KiB
Ruby
Raw Normal View History

2016-04-12 15:09:47 +02:00
class Subscriptions::RenewAsAdminTest < ActionDispatch::IntegrationTest
setup do
2016-11-23 16:30:19 +01:00
@admin = User.find_by(username: 'admin')
2016-04-12 15:09:47 +02:00
login_as(@admin, scope: :user)
end
test 'admin successfully renew a subscription before it has ended' do
2016-11-23 16:30:19 +01:00
user = User.find_by(username: 'kdumas')
2016-04-12 15:09:47 +02:00
plan = Plan.find_by(base_name: 'Mensuel tarif réduit')
VCR.use_cassette('subscriptions_admin_renew_success') do
2016-04-12 15:09:47 +02:00
post '/api/subscriptions',
{
subscription: {
plan_id: plan.id,
user_id: user.id
}
2016-04-12 15:09:47 +02:00
}.to_json, default_headers
end
# Check response format & status
assert_equal 201, response.status, response.body
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'
# Check that the user has the correct subscription
assert_not_nil user.subscription, "user's subscription was not found"
2018-12-12 15:27:12 +01:00
assert_not_nil user.subscribed_plan, "user's subscribed plan was not found"
2016-04-12 15:09:47 +02:00
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"
2018-12-11 17:27:25 +01:00
# Check the expiration date
assert_equal (user.subscription.created_at + plan.interval_count.send(plan.interval)).iso8601,
subscription[:expired_at],
'subscription expiration date 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-12 15:09:47 +02:00
# 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')
assert_equal 10,
(printer.prices.find_by(group_id: user.group_id, plan_id: user.subscription.plan_id).amount / 100),
'machine hourly price does not match'
2016-04-12 15:09:47 +02:00
# Check notification was sent to the user
notification = Notification.find_by(
notification_type_id: NotificationType.find_by_name('notify_member_subscribed_plan'),
attached_object_type: 'Subscription',
attached_object_id: subscription[:id]
)
2016-04-12 15:09:47 +02:00
assert_not_nil notification, 'user notification was not created'
assert_equal user.id, notification.receiver_id, 'wrong user notified'
# Check generated invoice
invoice = Invoice.find_by(invoiced_type: 'Subscription', invoiced_id: subscription[:id])
assert_invoice_pdf invoice
assert_equal plan.amount, invoice.total, 'Invoice total price does not match the bought subscription'
end
test 'admin successfully offer free days' do
user = User.find_by(username: 'pdurand')
2018-12-11 17:27:25 +01:00
subscription = user.subscription.clone
new_date = (1.month.from_now - 4.days).utc
VCR.use_cassette('subscriptions_admin_offer_free_days') do
put "/api/subscriptions/#{subscription.id}",
{
subscription: {
expired_at: new_date.strftime('%Y-%m-%d %H:%M:%S.%9N Z'),
free: true
}
}.to_json, default_headers
end
2016-04-12 15:09:47 +02:00
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime::JSON, response.content_type
# Check that the subscribed plan was not altered
res_subscription = json_response(response.body)
assert_equal subscription.id, res_subscription[:id], 'subscription id has changed'
assert_equal subscription.plan_id, res_subscription[:plan_id], 'subscribed plan does not match'
assert_dates_equal new_date, res_subscription[:expired_at], 'subscription end date was not updated'
2018-12-12 15:27:12 +01:00
# Check the subscription was correctly saved
assert_equal 1, user.subscriptions.count
# Check notification was sent to the user
notification = Notification.find_by(
notification_type_id: NotificationType.find_by_name('notify_member_subscription_extended'),
attached_object_type: 'Subscription',
attached_object_id: subscription[:id]
)
assert_not_nil notification, 'user notification was not created'
2018-12-11 17:27:25 +01:00
assert_not_nil notification.get_meta_data(:free_days),
"notification didn't says to the user that her extent was for free"
assert_equal user.id, notification.receiver_id, 'wrong user notified'
end
test 'admin successfully extends a subscription' do
user = User.find_by(username: 'pdurand')
subscription = user.subscription.clone
new_date = (1.month.from_now - 4.days).utc
VCR.use_cassette('subscriptions_admin_extends_subscription') do
put "/api/subscriptions/#{subscription.id}",
{
subscription: {
expired_at: new_date.strftime('%Y-%m-%d %H:%M:%S.%9N Z')
}
}.to_json, default_headers
end
# Check response format & status
assert_equal 201, response.status, response.body
assert_equal Mime::JSON, response.content_type
# Check that the subscribed plan is still the same
res_subscription = json_response(response.body)
assert_equal subscription.plan_id, res_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
2018-12-11 17:27:25 +01:00
# Check that the subscription is new
assert_not_equal subscription.id, res_subscription[:id], 'subscription id has not changed'
assert_dates_equal new_date, res_subscription[:expired_at], 'subscription end date does not match'
# Check notification was sent to the user
notification = Notification.find_by(
notification_type_id: NotificationType.find_by_name('notify_member_subscribed_plan'),
attached_object_type: 'Subscription',
attached_object_id: subscription[:id]
)
assert_not_nil notification, 'user notification was not created'
assert_equal user.id, notification.receiver_id, 'wrong user notified'
2016-04-12 15:09:47 +02:00
end
end