1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00

insertions on subscriptions count

This commit is contained in:
Sylvain 2018-12-12 15:27:12 +01:00
parent 811b7193f7
commit ae9caef8af
6 changed files with 104 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -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
end

View File

@ -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

View File

@ -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'

View File

@ -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