2019-06-04 16:50:23 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-12-15 17:01:54 +01:00
|
|
|
require 'test_helper'
|
|
|
|
|
2021-05-31 11:52:53 +02:00
|
|
|
module Subscriptions; end
|
|
|
|
|
2020-12-15 17:01:54 +01:00
|
|
|
class Subscriptions::CreateAsAdminTest < ActionDispatch::IntegrationTest
|
|
|
|
setup do
|
|
|
|
@admin = User.find_by(username: 'admin')
|
|
|
|
login_as(@admin, scope: :user)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'admin successfully takes a subscription for a user' do
|
|
|
|
user = User.find_by(username: 'jdupond')
|
|
|
|
plan = Plan.find_by(group_id: user.group.id, type: 'Plan', base_name: 'Mensuel')
|
|
|
|
|
|
|
|
VCR.use_cassette('subscriptions_admin_create_success') do
|
2021-05-19 18:12:52 +02:00
|
|
|
post '/api/local_payment/confirm_payment',
|
2020-12-15 17:01:54 +01:00
|
|
|
params: {
|
2021-04-26 11:41:02 +02:00
|
|
|
customer_id: user.id,
|
2021-05-19 18:12:52 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: plan.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2020-12-15 17:01:54 +01:00
|
|
|
}.to_json, headers: default_headers
|
2016-04-06 18:17:33 +02:00
|
|
|
end
|
|
|
|
|
2020-12-15 17:01:54 +01:00
|
|
|
# Check response format & status
|
|
|
|
assert_equal 201, response.status, response.body
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2016-04-07 17:46:23 +02:00
|
|
|
|
2020-12-15 17:01:54 +01: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'
|
|
|
|
|
2020-12-15 17:01:54 +01:00
|
|
|
# Check that the user has only one subscription
|
|
|
|
assert_equal 1, user.subscriptions.count
|
|
|
|
|
|
|
|
# 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
|
|
|
|
printer = Machine.find_by(slug: 'imprimante-3d')
|
2023-02-16 16:08:24 +01:00
|
|
|
assert_equal 15,
|
|
|
|
(printer.prices.find_by(group_id: user.group_id, plan_id: user.subscription.plan_id).amount / 100.00),
|
|
|
|
'machine hourly price does not match'
|
2020-12-15 17:01:54 +01:00
|
|
|
|
|
|
|
# Check notification was sent to the user
|
2023-02-16 16:08:24 +01:00
|
|
|
notification = Notification.find_by(notification_type_id: NotificationType.find_by(name: 'notify_member_subscribed_plan'),
|
2021-05-31 11:52:53 +02:00
|
|
|
attached_object_type: 'Subscription', attached_object_id: subscription.id)
|
2020-12-15 17:01:54 +01: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-31 11:52:53 +02:00
|
|
|
item = InvoiceItem.find_by(object_type: 'Subscription', object_id: subscription.id)
|
2021-05-27 15:58:55 +02:00
|
|
|
invoice = item.invoice
|
2020-12-15 17:01:54 +01:00
|
|
|
assert_invoice_pdf invoice
|
|
|
|
assert_equal plan.amount, invoice.total, 'Invoice total price does not match the bought subscription'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'admin takes a subscription with a payment schedule' do
|
|
|
|
user = User.find_by(username: 'jdupond')
|
|
|
|
plan = Plan.find_by(group_id: user.group.id, type: 'Plan', base_name: 'Abonnement mensualisable')
|
2020-12-16 18:33:43 +01:00
|
|
|
invoice_count = Invoice.count
|
2020-12-15 17:01:54 +01:00
|
|
|
payment_schedule_count = PaymentSchedule.count
|
|
|
|
payment_schedule_items_count = PaymentScheduleItem.count
|
|
|
|
|
|
|
|
VCR.use_cassette('subscriptions_admin_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: {
|
|
|
|
customer_id: user.id,
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: plan.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
payment_schedule: true,
|
|
|
|
payment_method: 'cart'
|
|
|
|
}
|
|
|
|
}.to_json, headers: default_headers
|
2016-04-07 17:46:23 +02:00
|
|
|
|
|
|
|
# Check response format & status
|
2021-10-14 18:20:10 +02:00
|
|
|
assert_equal 201, response.status, response.body
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2016-04-07 17:46:23 +02:00
|
|
|
|
2020-12-15 17:01:54 +01:00
|
|
|
# Check the response
|
2021-09-13 14:25:30 +02:00
|
|
|
res = json_response(response.body)
|
|
|
|
assert_not_nil res[:id]
|
2020-12-15 17:01:54 +01:00
|
|
|
end
|
2016-04-07 17:46:23 +02:00
|
|
|
|
2020-12-15 17:01:54 +01:00
|
|
|
# Check generalities
|
2020-12-16 18:33:43 +01:00
|
|
|
assert_equal invoice_count, Invoice.count, "an invoice was generated but it shouldn't"
|
2020-12-15 17:01:54 +01:00
|
|
|
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'
|
2016-04-07 17:46:23 +02:00
|
|
|
|
2020-12-15 17:01:54 +01:00
|
|
|
# Check the correct plan was subscribed
|
2021-05-31 11:52:53 +02:00
|
|
|
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'
|
2020-12-15 17:01:54 +01: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"
|
2016-04-06 18:17:33 +02:00
|
|
|
end
|
2018-12-12 15:27:12 +01:00
|
|
|
end
|