diff --git a/test/integration/reservations/create_as_admin_test.rb b/test/integration/reservations/create_as_admin_test.rb index c70713a3e..96871896d 100644 --- a/test/integration/reservations/create_as_admin_test.rb +++ b/test/integration/reservations/create_as_admin_test.rb @@ -38,6 +38,7 @@ module Reservations assert_equal users_credit_count, UsersCredit.count # subscription assertions + assert_equal 0, @user_without_subscription.subscriptions.count assert_nil @user_without_subscription.subscribed_plan # reservation assertions @@ -80,6 +81,7 @@ module Reservations assert_equal users_credit_count, UsersCredit.count # subscription assertions + assert_equal 0, @user_without_subscription.subscriptions.count assert_nil @user_without_subscription.subscribed_plan # reservation assertions @@ -137,6 +139,7 @@ module Reservations assert_equal invoice_items_count + 1, InvoiceItem.count # subscription assertions + assert_equal 0, @user_without_subscription.subscriptions.count assert_nil @user_without_subscription.subscribed_plan # reservation assertions @@ -201,6 +204,7 @@ module Reservations assert_equal users_credit_count + 1, UsersCredit.count # subscription assertions + assert_equal 1, @user_with_subscription.subscriptions.count assert_not_nil @user_with_subscription.subscribed_plan assert_equal plan.id, @user_with_subscription.subscribed_plan.id @@ -270,6 +274,7 @@ module Reservations assert_equal users_credit_count, UsersCredit.count # subscription assertions + assert_equal 0, @user_without_subscription.subscriptions.count assert_nil @user_without_subscription.subscribed_plan # reservation assertions @@ -343,6 +348,7 @@ module Reservations assert_equal wallet_transactions_count + 1, WalletTransaction.count # subscription assertions + assert_equal 1, @vlonchamp.subscriptions.count assert_not_nil @vlonchamp.subscribed_plan assert_equal plan.id, @vlonchamp.subscribed_plan.id @@ -409,7 +415,8 @@ module Reservations assert_equal users_credit_count, UsersCredit.count # subscription assertions - assert_nil @user_without_subscription.subscribed_plan + assert_equal 0, @vlonchamp.subscriptions.count + assert_nil @vlonchamp.subscribed_plan # reservation assertions reservation = Reservation.last @@ -458,6 +465,7 @@ module Reservations assert_equal users_credit_count + 1, UsersCredit.count # subscription assertions + assert_equal 1, @user_without_subscription.subscriptions.count assert_not_nil @user_without_subscription.subscribed_plan assert_equal plan.id, @user_without_subscription.subscribed_plan.id diff --git a/test/integration/reservations/create_test.rb b/test/integration/reservations/create_test.rb index b4856b637..52673061b 100644 --- a/test/integration/reservations/create_test.rb +++ b/test/integration/reservations/create_test.rb @@ -42,6 +42,7 @@ module Reservations assert_equal subscriptions_count, Subscription.count # subscription assertions + assert_equal 0, @user_without_subscription.subscriptions.count assert_nil @user_without_subscription.subscribed_plan # reservation assertions @@ -104,6 +105,10 @@ module Reservations assert_equal invoice_count, Invoice.count assert_equal invoice_items_count, InvoiceItem.count assert_equal notifications_count, Notification.count + + # subscription assertions + assert_equal 0, @user_without_subscription.subscriptions.count + assert_nil @user_without_subscription.subscribed_plan end test 'user without subscription reserves a training with success' do @@ -138,6 +143,10 @@ module Reservations assert_equal invoice_count + 1, Invoice.count assert_equal invoice_items_count + 1, InvoiceItem.count + # subscription assertions + assert_equal 0, @user_without_subscription.subscriptions.count + assert_nil @user_without_subscription.subscribed_plan + # reservation assertions reservation = Reservation.last @@ -204,6 +213,11 @@ module Reservations assert_equal invoice_items_count + 2, InvoiceItem.count assert_equal users_credit_count + 1, UsersCredit.count + # subscription assertions + assert_equal 1, @user_with_subscription.subscriptions.count + assert_not_nil @user_with_subscription.subscribed_plan + assert_equal plan.id, @user_with_subscription.subscribed_plan.id + # reservation assertions reservation = Reservation.last @@ -273,6 +287,11 @@ module Reservations assert_equal invoice_count + 1, Invoice.count assert_equal invoice_items_count + 1, InvoiceItem.count + # subscription assertions + assert_equal 1, @user_with_subscription.subscriptions.count + assert_not_nil @user_with_subscription.subscribed_plan + assert_equal plan.id, @user_with_subscription.subscribed_plan.id + # reservation assertions reservation = Reservation.last @@ -339,6 +358,10 @@ module Reservations assert_equal users_credit_count, UsersCredit.count assert_equal wallet_transactions_count + 1, WalletTransaction.count + # subscription assertions + assert_equal 0, @vlonchamp.subscriptions.count + assert_nil @vlonchamp.subscribed_plan + # reservation assertions reservation = Reservation.last @@ -374,7 +397,7 @@ module Reservations assert_equal transaction.amount, invoice.wallet_amount / 100.0 end - test 'user reserves a training and plan by wallet with success' do + test 'user reserves a training and a subscription by wallet with success' do @vlonchamp = User.find_by(username: 'vlonchamp') login_as(@vlonchamp, scope: :user) @@ -411,6 +434,11 @@ module Reservations assert_equal invoice_items_count + 2, InvoiceItem.count assert_equal wallet_transactions_count + 1, WalletTransaction.count + # subscription assertions + assert_equal 1, @vlonchamp.subscriptions.count + assert_not_nil @vlonchamp.subscribed_plan + assert_equal plan.id, @vlonchamp.subscribed_plan.id + # reservation assertions reservation = Reservation.last @@ -482,6 +510,11 @@ module Reservations assert_equal users_credit_count, UsersCredit.count assert_equal subscriptions_count + 1, Subscription.count + # subscription assertions + assert_equal 1, @user_without_subscription.subscriptions.count + assert_not_nil @user_without_subscription.subscribed_plan + assert_equal plan.id, @user_without_subscription.subscribed_plan.id + # reservation assertions reservation = Reservation.last @@ -563,6 +596,10 @@ module Reservations assert_equal invoice_count, Invoice.count assert_equal invoice_items_count, InvoiceItem.count assert_equal notifications_count, Notification.count + + # subscription assertions + assert_equal 0, @user_without_subscription.subscriptions.count + assert_nil @user_without_subscription.subscribed_plan end end end diff --git a/test/integration/subscriptions/create_as_admin_test.rb b/test/integration/subscriptions/create_as_admin_test.rb index c0cee90c9..13e0f8057 100644 --- a/test/integration/subscriptions/create_as_admin_test.rb +++ b/test/integration/subscriptions/create_as_admin_test.rb @@ -30,6 +30,9 @@ module Subscriptions 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" @@ -55,4 +58,4 @@ module Subscriptions end end -end \ No newline at end of file +end diff --git a/test/integration/subscriptions/create_as_user_test.rb b/test/integration/subscriptions/create_as_user_test.rb index 5060eeb3d..fa8729649 100644 --- a/test/integration/subscriptions/create_as_user_test.rb +++ b/test/integration/subscriptions/create_as_user_test.rb @@ -9,7 +9,7 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest test 'user successfully takes a subscription' do plan = Plan.find_by(group_id: @user.group.id, type: 'Plan', base_name: 'Mensuel') - VCR.use_cassette("subscriptions_user_create_success") do + VCR.use_cassette('subscriptions_user_create_success') do post '/api/subscriptions', { subscription: { @@ -28,6 +28,9 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest 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" @@ -39,12 +42,18 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest # 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), 'machine hourly price does not match' + 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 notifications were sent for every admins - notifications = Notification.where(notification_type_id: NotificationType.find_by_name('notify_admin_subscribed_plan'), attached_object_type: 'Subscription', attached_object_id: subscription[:id]) + notifications = Notification.where( + notification_type_id: NotificationType.find_by_name('notify_admin_subscribed_plan'), + attached_object_type: 'Subscription', + attached_object_id: subscription[:id] + ) assert_not_empty notifications, 'no notifications were created' - notified_users_ids = notifications.map {|n| n.receiver_id } + notified_users_ids = notifications.map(&:receiver_id) User.admins.each do |adm| assert_includes notified_users_ids, adm.id, "Admin #{adm.id} was not notified" end @@ -61,14 +70,14 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest # get plan for wrong group plan = Plan.where.not(group_id: @user.group.id).first - VCR.use_cassette("subscriptions_user_create_failed") do + VCR.use_cassette('subscriptions_user_create_failed') do post '/api/subscriptions', { - subscription: { - plan_id: plan.id, - user_id: @user.id, - card_token: stripe_card_token - } + subscription: { + plan_id: plan.id, + user_id: @user.id, + card_token: stripe_card_token + } }.to_json, default_headers end @@ -77,7 +86,7 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest assert_equal Mime::JSON, response.content_type # Check the error was handled - assert_match /plan is not compatible/, response.body + assert_match /plan is not compatible/, response.body # Check that the user has no subscription assert_nil @user.subscription, "user's subscription was found" @@ -89,7 +98,7 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest login_as(@vlonchamp, scope: :user) plan = Plan.find_by(group_id: @vlonchamp.group.id, type: 'Plan', base_name: 'Mensuel tarif réduit') - VCR.use_cassette("subscriptions_user_create_success_with_wallet") do + VCR.use_cassette('subscriptions_user_create_success_with_wallet') do post '/api/subscriptions', { subscription: { @@ -115,16 +124,27 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest # Check that the training credits were set correctly assert_empty @vlonchamp.training_credits, 'training credits were not reset' - assert_equal @vlonchamp.subscription.plan.training_credit_nb, plan.training_credit_nb, 'trainings credits were not allocated' + assert_equal @vlonchamp.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 10, (printer.prices.find_by(group_id: @vlonchamp.group_id, plan_id: @vlonchamp.subscription.plan_id).amount / 100), 'machine hourly price does not match' + assert_equal 10, + (printer.prices.find_by( + group_id: @vlonchamp.group_id, + plan_id: @vlonchamp.subscription.plan_id).amount / 100 + ), + 'machine hourly price does not match' # Check notifications were sent for every admins - notifications = Notification.where(notification_type_id: NotificationType.find_by_name('notify_admin_subscribed_plan'), attached_object_type: 'Subscription', attached_object_id: subscription[:id]) + notifications = Notification.where( + notification_type_id: NotificationType.find_by_name('notify_admin_subscribed_plan'), + attached_object_type: 'Subscription', + attached_object_id: subscription[:id] + ) assert_not_empty notifications, 'no notifications were created' - notified_users_ids = notifications.map {|n| n.receiver_id } + notified_users_ids = notifications.map(&:receiver_id) User.admins.each do |adm| assert_includes notified_users_ids, adm.id, "Admin #{adm.id} was not notified" end diff --git a/test/integration/subscriptions/renew_as_admin_test.rb b/test/integration/subscriptions/renew_as_admin_test.rb index 05027c956..b2ba7883b 100644 --- a/test/integration/subscriptions/renew_as_admin_test.rb +++ b/test/integration/subscriptions/renew_as_admin_test.rb @@ -30,6 +30,7 @@ class Subscriptions::RenewAsAdminTest < ActionDispatch::IntegrationTest # Check that the user has the correct subscription assert_not_nil user.subscription, "user's subscription was not found" + assert_not_nil user.subscribed_plan, "user's subscribed plan 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" @@ -38,6 +39,9 @@ class Subscriptions::RenewAsAdminTest < ActionDispatch::IntegrationTest subscription[:expired_at], 'subscription expiration date does not match' + # Check the subscription was correctly saved + assert_equal 2, user.subscriptions.count + # 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' @@ -88,6 +92,9 @@ class Subscriptions::RenewAsAdminTest < ActionDispatch::IntegrationTest 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' + # 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'), @@ -122,6 +129,9 @@ class Subscriptions::RenewAsAdminTest < ActionDispatch::IntegrationTest res_subscription = json_response(response.body) assert_equal subscription.plan_id, res_subscription[:plan_id], 'subscribed plan does not match' + # Check the subscription was correctly saved + assert_equal 2, user.subscriptions.count + # 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' diff --git a/test/integration/subscriptions/renew_as_user_test.rb b/test/integration/subscriptions/renew_as_user_test.rb index cd440b7e0..b221c589a 100644 --- a/test/integration/subscriptions/renew_as_user_test.rb +++ b/test/integration/subscriptions/renew_as_user_test.rb @@ -30,6 +30,9 @@ class Subscriptions::RenewAsUserTest < ActionDispatch::IntegrationTest subscription = json_response(response.body) assert_equal plan.id, subscription[:plan_id], 'subscribed plan does not match' + # Check the subscription was correctly saved + assert_equal 2, @user.subscriptions.count + # Check that the user has the correct subscription assert_not_nil @user.subscription, "user's subscription was not found" @@ -93,6 +96,10 @@ class Subscriptions::RenewAsUserTest < ActionDispatch::IntegrationTest # Check that the user's subscription has not changed assert_equal previous_expiration, @user.subscription.expired_at.to_i, "user's subscription has changed" + + # Check the subscription was not saved + assert_equal 1, @user.subscriptions.count + end end