2016-04-07 17:46:23 +02:00
|
|
|
module Subscriptions
|
|
|
|
class CreateAsAdminTest < ActionDispatch::IntegrationTest
|
2016-04-06 18:17:33 +02:00
|
|
|
|
|
|
|
|
2016-04-07 17:46:23 +02:00
|
|
|
setup do
|
2016-11-23 16:30:19 +01:00
|
|
|
@admin = User.find_by(username: 'admin')
|
2016-04-07 17:46:23 +02:00
|
|
|
login_as(@admin, scope: :user)
|
2016-04-06 18:17:33 +02:00
|
|
|
end
|
|
|
|
|
2016-04-07 17:46:23 +02:00
|
|
|
test "admin successfully takes a subscription for a user" do
|
2016-11-23 16:30:19 +01:00
|
|
|
user = User.find_by(username: 'jdupond')
|
2016-04-07 17:46:23 +02:00
|
|
|
plan = Plan.find_by(group_id: user.group.id, type: 'Plan', base_name: 'Mensuel')
|
|
|
|
|
|
|
|
VCR.use_cassette("subscriptions_admin_create_success") do
|
|
|
|
post '/api/subscriptions',
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: plan.id,
|
|
|
|
user_id: user.id
|
|
|
|
}
|
|
|
|
}.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'
|
|
|
|
|
2018-12-12 15:27:12 +01:00
|
|
|
# Check that the user has only one subscription
|
|
|
|
assert_equal 1, 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"
|
|
|
|
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
|
2016-11-23 16:30:19 +01:00
|
|
|
printer = Machine.find_by(slug: 'imprimante-3d')
|
2016-04-07 17:46:23 +02:00
|
|
|
assert_equal 15, (printer.prices.find_by(group_id: user.group_id, plan_id: user.subscription.plan_id).amount / 100), 'machine hourly price 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'
|
|
|
|
|
|
|
|
# Check generated invoice
|
|
|
|
invoice = Invoice.find_by(invoiced_type: 'Subscription', invoiced_id: subscription[:id])
|
2016-04-11 15:31:25 +02:00
|
|
|
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
|
2016-04-06 18:17:33 +02:00
|
|
|
|
|
|
|
end
|
2018-12-12 15:27:12 +01:00
|
|
|
end
|