1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-15 00:24:09 +01:00
fab-manager/test/integration/subscriptions/renew_as_admin_test.rb

129 lines
5.2 KiB
Ruby
Raw Normal View History

2020-03-13 17:10:38 +01:00
# frozen_string_literal: true
2016-04-12 15:09:47 +02:00
require 'test_helper'
2021-10-18 11:47:24 +02:00
module Subscriptions; end
2020-03-13 17:10:38 +01:00
class Subscriptions::RenewAsAdminTest < ActionDispatch::IntegrationTest
2016-04-12 15:09:47 +02:00
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
post '/api/local_payment/confirm_payment',
2020-03-13 17:10:38 +01:00
params: {
2021-04-26 11:40:26 +02:00
customer_id: user.id,
items: [
{
subscription: {
plan_id: plan.id
}
}
]
2020-03-13 17:10:38 +01:00
}.to_json, headers: default_headers
2016-04-12 15:09:47 +02:00
end
# 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-12 15:09:47 +02:00
# Check the correct plan was subscribed
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-12 15:09:47 +02:00
# 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)).to_i,
subscription.expiration_date.to_i,
2018-12-11 17:27:25 +01:00
'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(
2022-12-28 11:01:05 +01:00
notification_type_id: NotificationType.find_by_name('notify_member_subscribed_plan'), # rubocop:disable Rails/DynamicFindBy
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
2021-05-27 15:58:55 +02:00
item = InvoiceItem.find_by(object_type: 'Subscription', object_id: subscription[:id])
invoice = item.invoice
2016-04-12 15:09:47 +02:00
assert_invoice_pdf invoice
2021-05-31 12:19:28 +02:00
assert_not_nil invoice.debug_footprint
2016-04-12 15:09:47 +02:00
assert_equal plan.amount, invoice.total, 'Invoice total price does not match the bought subscription'
end
2018-12-11 17:27:25 +01:00
test 'admin successfully extends a subscription' do
user = User.find_by(username: 'pdurand')
subscription = user.subscription.clone
2021-10-18 11:47:24 +02:00
new_date = subscription.expired_at + subscription.plan.interval_count.send(subscription.plan.interval)
2018-12-11 17:27:25 +01:00
VCR.use_cassette('subscriptions_admin_extends_subscription') do
2021-10-18 11:47:24 +02:00
post '/api/local_payment/confirm_payment',
params: {
customer_id: user.id,
payment_method: 'check',
payment_schedule: false,
items: [
{
subscription: {
start_at: subscription.expired_at.strftime('%Y-%m-%d %H:%M:%S.%9N Z'),
plan_id: subscription.plan_id
}
}
]
}.to_json, headers: default_headers
2018-12-11 17:27:25 +01:00
end
# 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
2018-12-11 17:27:25 +01:00
res_subscription = json_response(response.body)
2021-10-18 11:47:24 +02:00
assert_equal 'Subscription', res_subscription[:main_object][:type]
assert_equal subscription.plan.amount / 100.0, res_subscription[:items][0][:amount]
2018-12-11 17:27:25 +01:00
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
2021-10-18 11:47:24 +02:00
assert_not_equal subscription.id, user.subscription.id, 'subscription id has not changed'
assert_dates_equal new_date, user.subscription.expired_at, 'subscription end date does not match'
2018-12-11 17:27:25 +01:00
# Check notification was sent to the user
notification = Notification.find_by(
2022-12-28 11:01:05 +01:00
notification_type_id: NotificationType.find_by_name('notify_member_subscribed_plan'), # rubocop:disable Rails/DynamicFindBy
2018-12-11 17:27:25 +01:00
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