1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-19 13:54:25 +01:00

test reservation with schedule by user

This commit is contained in:
Sylvain 2020-12-15 16:53:11 +01:00
parent ccff463165
commit e3a81f6dbe
2 changed files with 91 additions and 4 deletions

View File

@ -650,5 +650,90 @@ module Reservations
assert_equal 0, @user_without_subscription.subscriptions.count
assert_nil @user_without_subscription.subscribed_plan
end
test 'user reserves a training and a subscription with payment schedule' do
login_as(@user_without_subscription, scope: :user)
reservations_count = Reservation.count
invoice_count = Invoice.count
invoice_items_count = InvoiceItem.count
subscriptions_count = Subscription.count
users_credit_count = UsersCredit.count
payment_schedule_count = PaymentSchedule.count
payment_schedule_items_count = PaymentScheduleItem.count
training = Training.find(1)
availability = training.availabilities.first
plan = Plan.find_by(group_id: @user_without_subscription.group.id, type: 'Plan', base_name: 'Abonnement mensualisable')
VCR.use_cassette('reservations_training_subscription_with_payment_schedule') do
get "/api/payments/setup_intent/#{@user_without_subscription.id}"
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# 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]
# Confirm the intent
stripe_res = Stripe::SetupIntent.confirm(
setup_intent[:id],
{ payment_method: stripe_payment_method },
{ api_key: Setting.get('stripe_secret_key') }
)
# check the confirmation
assert_equal setup_intent[:id], stripe_res.id
assert_equal 'succeeded', stripe_res.status
assert_equal 'off_session', stripe_res.usage
post '/api/payments/confirm_payment_schedule',
params: {
setup_intent_id: setup_intent[:id],
cart_items: {
reservation: {
reservable_id: training.id,
reservable_type: training.class.name,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
],
plan_id: plan.id,
payment_schedule: true
}
}
}.to_json, headers: default_headers
end
# Check response format & status
assert_equal 201, response.status, response.body
assert_equal Mime[:json], response.content_type
assert_equal reservations_count + 1, Reservation.count, 'missing the reservation'
assert_equal invoice_count, Invoice.count, "an invoice was generated but it shouldn't"
assert_equal invoice_items_count, InvoiceItem.count, "some invoice items were generated but they shouldn't"
assert_equal users_credit_count, UsersCredit.count, "user's credits count has changed but it shouldn't"
assert_equal subscriptions_count + 1, Subscription.count, 'missing the subscription'
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'
# subscription assertions
assert_equal 1, @user_without_subscription.subscriptions.count
assert_not_nil @user_without_subscription.subscribed_plan, "user's subscribed plan was not found"
assert_not_nil @user_without_subscription.subscription, "user's subscription was not found"
assert_equal plan.id, @user_without_subscription.subscribed_plan.id, "user's plan does not match"
# Check the answer
reservation = json_response(response.body)
assert_equal plan.id, reservation[:user][:subscribed_plan][:id], 'subscribed plan does not match'
end
end
end

View File

@ -171,6 +171,8 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest
test 'user takes a subscription with payment schedule' do
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_user_create_with_payment_schedule') do
get "/api/payments/setup_intent/#{@user.id}"
@ -204,16 +206,17 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest
cart_items: {
subscription: {
plan_id: plan.id,
payment_schedule: true,
payment_method: 'stripe'
payment_schedule: true
}
}
}.to_json, headers: default_headers
end
# Check response format & status
# 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)
@ -223,6 +226,5 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest
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