From a5e48fa379a524d04af9f6c09b5208e9b2e18cc2 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 15 Dec 2020 17:01:54 +0100 Subject: [PATCH] test subscription with schedule by admin --- .rubocop.yml | 1 + .../subscriptions/create_as_admin_test.rb | 148 +++++++++++++----- 2 files changed, 106 insertions(+), 43 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index fa5d4d57e..2eca3bfc2 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -16,6 +16,7 @@ Metrics/BlockLength: - 'lib/tasks/**/*.rake' - 'config/routes.rb' - 'app/pdfs/pdf/*.rb' + - 'test/**/*.rb' Metrics/ParameterLists: CountKeywordArgs: false Style/BracesAroundHashParameters: diff --git a/test/integration/subscriptions/create_as_admin_test.rb b/test/integration/subscriptions/create_as_admin_test.rb index 534ece8bf..e959403fa 100644 --- a/test/integration/subscriptions/create_as_admin_test.rb +++ b/test/integration/subscriptions/create_as_admin_test.rb @@ -1,59 +1,121 @@ # frozen_string_literal: true -module Subscriptions - class CreateAsAdminTest < ActionDispatch::IntegrationTest - setup do - @admin = User.find_by(username: 'admin') - login_as(@admin, scope: :user) +require 'test_helper' + +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 + post '/api/subscriptions', + params: { + subscription: { + plan_id: plan.id, + user_id: user.id + } + }.to_json, headers: default_headers 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') + # Check response format & status + assert_equal 201, response.status, response.body + assert_equal Mime[:json], response.content_type - VCR.use_cassette('subscriptions_admin_create_success') do - post '/api/subscriptions', - params: { - subscription: { - plan_id: plan.id, - user_id: user.id - } - }.to_json, headers: default_headers - end + # 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 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') + 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' + + # 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]) + 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') + payment_schedule_count = PaymentSchedule.count + payment_schedule_items_count = PaymentScheduleItem.count + + VCR.use_cassette('subscriptions_admin_create_with_payment_schedule') do + get "/api/payments/setup_intent/#{user.id}" # Check response format & status - assert_equal 201, response.status, response.body + assert_equal 200, 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 the response + setup_intent = json_response(response.body) + assert_not_nil setup_intent[:client_secret] + assert_not_nil setup_intent[:id] + assert_match /^#{setup_intent[:id]}_secret_/, setup_intent[:client_secret] - # Check that the user has only one subscription - assert_equal 1, user.subscriptions.count + # Confirm the intent + stripe_res = Stripe::SetupIntent.confirm( + setup_intent[:id], + { payment_method: stripe_payment_method }, + { api_key: Setting.get('stripe_secret_key') } + ) - # 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 the confirmation + assert_equal setup_intent[:id], stripe_res.id + assert_equal 'succeeded', stripe_res.status + assert_equal 'off_session', stripe_res.usage - # 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') - 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' - - # 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]) - assert_invoice_pdf invoice - assert_equal plan.amount, invoice.total, 'Invoice total price does not match the bought subscription' + post '/api/payments/confirm_payment_schedule', + params: { + setup_intent_id: setup_intent[:id], + cart_items: { + subscription: { + plan_id: plan.id, + payment_schedule: true, + user_id: user.id, + payment_method: 'stripe' + } + } + }.to_json, headers: default_headers end + + # Check generalities + assert_equal 201, response.status, response.body + assert_equal Mime[:json], response.content_type + 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' + + # 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" + 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" end end