2020-03-13 17:10:38 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-12-15 17:14:58 +01:00
|
|
|
require 'test_helper'
|
|
|
|
|
2021-05-21 18:25:18 +02:00
|
|
|
module Reservations; end
|
|
|
|
|
2020-12-15 17:14:58 +01:00
|
|
|
class Reservations::CreateAsAdminTest < ActionDispatch::IntegrationTest
|
|
|
|
setup do
|
|
|
|
@user_without_subscription = User.members.without_subscription.first
|
|
|
|
@user_with_subscription = User.members.with_subscription.second
|
|
|
|
@admin = User.with_role(:admin).first
|
|
|
|
login_as(@admin, scope: :user)
|
|
|
|
end
|
2016-04-07 16:02:09 +02:00
|
|
|
|
2020-12-15 17:14:58 +01:00
|
|
|
test 'user without subscription reserves a machine with success' do
|
|
|
|
machine = Machine.find(6)
|
|
|
|
availability = machine.availabilities.first
|
|
|
|
|
|
|
|
reservations_count = Reservation.count
|
|
|
|
invoice_count = Invoice.count
|
|
|
|
invoice_items_count = InvoiceItem.count
|
|
|
|
users_credit_count = UsersCredit.count
|
|
|
|
|
2021-05-19 18:12:52 +02:00
|
|
|
post '/api/local_payment/confirm_payment', params: {
|
2021-04-23 17:54:59 +02:00
|
|
|
customer_id: @user_without_subscription.id,
|
2021-05-19 18:12:52 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
reservation: {
|
|
|
|
reservable_id: machine.id,
|
|
|
|
reservable_type: machine.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
|
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}
|
2021-05-19 18:12:52 +02:00
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}.to_json, headers: default_headers
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# general assertions
|
|
|
|
assert_equal 201, response.status
|
|
|
|
assert_equal reservations_count + 1, Reservation.count
|
|
|
|
assert_equal invoice_count + 1, Invoice.count
|
|
|
|
assert_equal invoice_items_count + 1, InvoiceItem.count
|
|
|
|
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
|
|
|
|
reservation = Reservation.last
|
|
|
|
|
2021-05-28 17:34:20 +02:00
|
|
|
assert reservation.original_invoice
|
|
|
|
assert_equal 1, reservation.original_invoice.invoice_items.count
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# invoice assertions
|
2021-05-28 17:34:20 +02:00
|
|
|
invoice = reservation.original_invoice
|
2020-12-15 17:14:58 +01:00
|
|
|
|
2021-04-23 17:54:59 +02:00
|
|
|
assert invoice.payment_gateway_object.blank?
|
2020-12-15 17:14:58 +01:00
|
|
|
refute invoice.total.blank?
|
|
|
|
|
|
|
|
# invoice_items assertions
|
|
|
|
invoice_item = InvoiceItem.last
|
|
|
|
|
|
|
|
assert_equal machine.prices.find_by(group_id: @user_without_subscription.group_id, plan_id: nil).amount, invoice_item.amount
|
|
|
|
|
|
|
|
# invoice assertions
|
2021-05-27 15:58:55 +02:00
|
|
|
item = InvoiceItem.find_by(object: reservation)
|
|
|
|
invoice = item.invoice
|
2020-12-15 17:14:58 +01:00
|
|
|
assert_invoice_pdf invoice
|
|
|
|
|
|
|
|
# notification
|
|
|
|
assert_not_empty Notification.where(attached_object: reservation)
|
|
|
|
end
|
2016-04-07 16:02:09 +02:00
|
|
|
|
2020-12-15 17:14:58 +01:00
|
|
|
test 'user without subscription reserves a training with success' do
|
|
|
|
training = Training.first
|
|
|
|
availability = training.availabilities.first
|
|
|
|
|
|
|
|
reservations_count = Reservation.count
|
|
|
|
invoice_count = Invoice.count
|
|
|
|
invoice_items_count = InvoiceItem.count
|
|
|
|
|
2021-05-19 18:12:52 +02:00
|
|
|
post '/api/local_payment/confirm_payment', params: {
|
2021-04-23 17:54:59 +02:00
|
|
|
customer_id: @user_without_subscription.id,
|
2021-05-19 18:12:52 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}.to_json, headers: default_headers
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# general assertions
|
|
|
|
assert_equal 201, response.status
|
|
|
|
assert_equal reservations_count + 1, Reservation.count
|
|
|
|
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
|
|
|
|
|
2021-05-28 17:34:20 +02:00
|
|
|
assert reservation.original_invoice
|
|
|
|
assert_equal 1, reservation.original_invoice.invoice_items.count
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# invoice assertions
|
2021-05-28 17:34:20 +02:00
|
|
|
invoice = reservation.original_invoice
|
2020-12-15 17:14:58 +01:00
|
|
|
|
2021-04-23 17:54:59 +02:00
|
|
|
assert invoice.payment_gateway_object.blank?
|
2020-12-15 17:14:58 +01:00
|
|
|
refute invoice.total.blank?
|
|
|
|
# invoice_items
|
|
|
|
invoice_item = InvoiceItem.last
|
|
|
|
|
|
|
|
assert_equal invoice_item.amount, training.amount_by_group(@user_without_subscription.group_id).amount
|
|
|
|
|
|
|
|
# invoice assertions
|
2021-05-27 15:58:55 +02:00
|
|
|
item = InvoiceItem.find_by(object: reservation)
|
|
|
|
invoice = item.invoice
|
2020-12-15 17:14:58 +01:00
|
|
|
assert_invoice_pdf invoice
|
|
|
|
|
|
|
|
# notification
|
|
|
|
assert_not_empty Notification.where(attached_object: reservation)
|
|
|
|
end
|
2016-04-07 16:02:09 +02:00
|
|
|
|
2020-12-15 17:14:58 +01:00
|
|
|
test 'user with subscription reserves a machine with success' do
|
|
|
|
plan = @user_with_subscription.subscribed_plan
|
|
|
|
machine = Machine.find(6)
|
|
|
|
availability = machine.availabilities.first
|
|
|
|
|
|
|
|
reservations_count = Reservation.count
|
|
|
|
invoice_count = Invoice.count
|
|
|
|
invoice_items_count = InvoiceItem.count
|
|
|
|
users_credit_count = UsersCredit.count
|
|
|
|
|
2021-05-19 18:12:52 +02:00
|
|
|
post '/api/local_payment/confirm_payment', params: {
|
2021-04-23 17:54:59 +02:00
|
|
|
customer_id: @user_with_subscription.id,
|
2021-05-19 18:12:52 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
reservation: {
|
|
|
|
reservable_id: machine.id,
|
|
|
|
reservable_type: machine.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
|
|
|
|
},
|
|
|
|
{
|
|
|
|
start_at: (availability.start_at + 1.hour).to_s(:iso8601),
|
|
|
|
end_at: (availability.start_at + 2.hours).to_s(:iso8601),
|
|
|
|
availability_id: availability.id
|
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}
|
2021-05-19 18:12:52 +02:00
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}.to_json, headers: default_headers
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# general assertions
|
|
|
|
assert_equal 201, response.status
|
|
|
|
assert_equal reservations_count + 1, Reservation.count
|
|
|
|
assert_equal invoice_count + 1, Invoice.count
|
|
|
|
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
|
|
|
|
|
2021-05-28 17:34:20 +02:00
|
|
|
assert reservation.original_invoice
|
|
|
|
assert_equal 2, reservation.original_invoice.invoice_items.count
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# invoice assertions
|
2021-05-28 17:34:20 +02:00
|
|
|
invoice = reservation.original_invoice
|
2020-12-15 17:14:58 +01:00
|
|
|
|
2021-04-23 17:54:59 +02:00
|
|
|
assert invoice.payment_gateway_object.blank?
|
2020-12-15 17:14:58 +01:00
|
|
|
refute invoice.total.blank?
|
|
|
|
|
|
|
|
# invoice_items assertions
|
|
|
|
invoice_items = InvoiceItem.last(2)
|
|
|
|
machine_price = machine.prices.find_by(group_id: @user_with_subscription.group_id, plan_id: plan.id).amount
|
|
|
|
|
|
|
|
assert(invoice_items.any? { |ii| ii.amount.zero? })
|
|
|
|
assert(invoice_items.any? { |ii| ii.amount == machine_price })
|
|
|
|
|
|
|
|
# users_credits assertions
|
|
|
|
users_credit = UsersCredit.last
|
|
|
|
|
|
|
|
assert_equal @user_with_subscription, users_credit.user
|
|
|
|
assert_equal [reservation.slots.count, plan.machine_credits.find_by(creditable_id: machine.id).hours].min, users_credit.hours_used
|
|
|
|
|
|
|
|
# invoice assertions
|
2021-05-27 15:58:55 +02:00
|
|
|
item = InvoiceItem.find_by(object: reservation)
|
|
|
|
invoice = item.invoice
|
2020-12-15 17:14:58 +01:00
|
|
|
assert_invoice_pdf invoice
|
|
|
|
|
|
|
|
# notification
|
|
|
|
assert_not_empty Notification.where(attached_object: reservation)
|
|
|
|
end
|
2016-04-07 16:02:09 +02:00
|
|
|
|
2020-12-15 17:14:58 +01:00
|
|
|
test 'user without subscription reserves a machine and pay by wallet with success' do
|
|
|
|
@vlonchamp = User.find_by(username: 'vlonchamp')
|
|
|
|
machine = Machine.find(6)
|
|
|
|
availability = machine.availabilities.first
|
|
|
|
|
|
|
|
reservations_count = Reservation.count
|
|
|
|
invoice_count = Invoice.count
|
|
|
|
invoice_items_count = InvoiceItem.count
|
|
|
|
users_credit_count = UsersCredit.count
|
|
|
|
|
2021-05-19 18:12:52 +02:00
|
|
|
post '/api/local_payment/confirm_payment', params: {
|
2021-04-23 17:54:59 +02:00
|
|
|
customer_id: @vlonchamp.id,
|
2021-05-19 18:12:52 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
reservation: {
|
|
|
|
reservable_id: machine.id,
|
|
|
|
reservable_type: machine.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
|
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}
|
2021-05-19 18:12:52 +02:00
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}.to_json, headers: default_headers
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# general assertions
|
|
|
|
assert_equal 201, response.status
|
|
|
|
assert_equal reservations_count + 1, Reservation.count
|
|
|
|
assert_equal invoice_count + 1, Invoice.count
|
|
|
|
assert_equal invoice_items_count + 1, InvoiceItem.count
|
|
|
|
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
|
|
|
|
reservation = Reservation.last
|
|
|
|
|
2021-05-28 17:34:20 +02:00
|
|
|
assert reservation.original_invoice
|
|
|
|
assert_equal 1, reservation.original_invoice.invoice_items.count
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# invoice assertions
|
2021-05-28 17:34:20 +02:00
|
|
|
invoice = reservation.original_invoice
|
2020-12-15 17:14:58 +01:00
|
|
|
|
2021-04-23 17:54:59 +02:00
|
|
|
assert invoice.payment_gateway_object.blank?
|
2020-12-15 17:14:58 +01:00
|
|
|
refute invoice.total.blank?
|
|
|
|
|
|
|
|
# invoice_items assertions
|
|
|
|
invoice_item = InvoiceItem.last
|
|
|
|
|
|
|
|
assert_equal machine.prices.find_by(group_id: @vlonchamp.group_id, plan_id: nil).amount, invoice_item.amount
|
|
|
|
|
|
|
|
# invoice assertions
|
2021-05-27 15:58:55 +02:00
|
|
|
item = InvoiceItem.find_by(object: reservation)
|
|
|
|
invoice = item.invoice
|
2020-12-15 17:14:58 +01:00
|
|
|
assert_invoice_pdf invoice
|
|
|
|
|
|
|
|
# notification
|
|
|
|
assert_not_empty Notification.where(attached_object: reservation)
|
|
|
|
|
|
|
|
# wallet
|
|
|
|
assert_equal @vlonchamp.wallet.amount, 0
|
|
|
|
assert_equal @vlonchamp.wallet.wallet_transactions.count, 2
|
|
|
|
transaction = @vlonchamp.wallet.wallet_transactions.last
|
|
|
|
assert_equal transaction.transaction_type, 'debit'
|
|
|
|
assert_equal transaction.amount, 10
|
|
|
|
assert_equal transaction.amount, invoice.wallet_amount / 100.0
|
|
|
|
assert_equal transaction.id, invoice.wallet_transaction_id
|
|
|
|
end
|
2016-04-07 16:02:09 +02:00
|
|
|
|
2020-12-15 17:14:58 +01:00
|
|
|
test 'user reserves a machine and a subscription pay by wallet with success' do
|
|
|
|
@vlonchamp = User.find_by(username: 'vlonchamp')
|
|
|
|
machine = Machine.find(6)
|
|
|
|
availability = machine.availabilities.first
|
|
|
|
plan = Plan.find_by(group_id: @vlonchamp.group.id, type: 'Plan', base_name: 'Mensuel tarif réduit')
|
|
|
|
|
|
|
|
reservations_count = Reservation.count
|
|
|
|
invoice_count = Invoice.count
|
|
|
|
invoice_items_count = InvoiceItem.count
|
|
|
|
users_credit_count = UsersCredit.count
|
|
|
|
wallet_transactions_count = WalletTransaction.count
|
|
|
|
|
2021-05-19 18:12:52 +02:00
|
|
|
post '/api/local_payment/confirm_payment', params: {
|
2021-04-23 17:54:59 +02:00
|
|
|
customer_id: @vlonchamp.id,
|
2021-05-19 18:12:52 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
reservation: {
|
|
|
|
reservable_id: machine.id,
|
|
|
|
reservable_type: machine.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
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2021-05-28 17:34:20 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: plan.id
|
|
|
|
}
|
2021-05-19 18:12:52 +02:00
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}.to_json, headers: default_headers
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# general assertions
|
|
|
|
assert_equal 201, response.status
|
|
|
|
assert_equal reservations_count + 1, Reservation.count
|
|
|
|
assert_equal invoice_count + 1, Invoice.count
|
|
|
|
assert_equal invoice_items_count + 2, InvoiceItem.count
|
|
|
|
assert_equal users_credit_count + 1, UsersCredit.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
|
|
|
|
|
2021-05-28 17:34:20 +02:00
|
|
|
assert reservation.original_invoice
|
|
|
|
assert_equal 2, reservation.original_invoice.invoice_items.count
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# invoice assertions
|
2021-05-28 17:34:20 +02:00
|
|
|
invoice = reservation.original_invoice
|
2020-12-15 17:14:58 +01:00
|
|
|
|
2021-04-23 17:54:59 +02:00
|
|
|
assert invoice.payment_gateway_object.blank?
|
2020-12-15 17:14:58 +01:00
|
|
|
refute invoice.total.blank?
|
|
|
|
assert_equal invoice.total, 2000
|
|
|
|
|
|
|
|
# invoice assertions
|
2021-05-27 15:58:55 +02:00
|
|
|
item = InvoiceItem.find_by(object: reservation)
|
|
|
|
invoice = item.invoice
|
2020-12-15 17:14:58 +01:00
|
|
|
assert_invoice_pdf invoice
|
|
|
|
|
|
|
|
# notification
|
|
|
|
assert_not_empty Notification.where(attached_object: reservation)
|
|
|
|
|
|
|
|
# wallet
|
|
|
|
assert_equal @vlonchamp.wallet.amount, 0
|
|
|
|
assert_equal @vlonchamp.wallet.wallet_transactions.count, 2
|
|
|
|
transaction = @vlonchamp.wallet.wallet_transactions.last
|
|
|
|
assert_equal transaction.transaction_type, 'debit'
|
|
|
|
assert_equal transaction.amount, 10
|
|
|
|
assert_equal transaction.amount, invoice.wallet_amount / 100.0
|
|
|
|
assert_equal transaction.id, invoice.wallet_transaction_id
|
|
|
|
end
|
2016-04-07 16:02:09 +02:00
|
|
|
|
2020-12-15 17:14:58 +01:00
|
|
|
test 'user without subscription reserves a machine and pay wallet with success' do
|
|
|
|
@vlonchamp = User.find_by(username: 'vlonchamp')
|
|
|
|
machine = Machine.find(6)
|
|
|
|
availability = machine.availabilities.first
|
|
|
|
|
|
|
|
reservations_count = Reservation.count
|
|
|
|
invoice_count = Invoice.count
|
|
|
|
invoice_items_count = InvoiceItem.count
|
|
|
|
users_credit_count = UsersCredit.count
|
|
|
|
|
2021-05-19 18:12:52 +02:00
|
|
|
post '/api/local_payment/confirm_payment', params: {
|
2021-04-23 17:54:59 +02:00
|
|
|
customer_id: @vlonchamp.id,
|
2021-05-19 18:12:52 +02:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
reservation: {
|
|
|
|
reservable_id: machine.id,
|
|
|
|
reservable_type: machine.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
|
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}
|
2021-05-19 18:12:52 +02:00
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}.to_json, headers: default_headers
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# general assertions
|
|
|
|
assert_equal 201, response.status
|
|
|
|
assert_equal reservations_count + 1, Reservation.count
|
|
|
|
assert_equal invoice_count + 1, Invoice.count
|
|
|
|
assert_equal invoice_items_count + 1, InvoiceItem.count
|
|
|
|
assert_equal users_credit_count, UsersCredit.count
|
|
|
|
|
|
|
|
# subscription assertions
|
|
|
|
assert_equal 0, @vlonchamp.subscriptions.count
|
|
|
|
assert_nil @vlonchamp.subscribed_plan
|
|
|
|
|
|
|
|
# reservation assertions
|
|
|
|
reservation = Reservation.last
|
|
|
|
|
2021-05-28 17:34:20 +02:00
|
|
|
assert_not_nil reservation.original_invoice
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# notification
|
|
|
|
assert_not_empty Notification.where(attached_object: reservation)
|
|
|
|
end
|
2016-04-07 16:02:09 +02:00
|
|
|
|
2020-12-15 17:14:58 +01:00
|
|
|
test 'user reserves a training and a subscription with success' do
|
|
|
|
training = Training.first
|
|
|
|
availability = training.availabilities.first
|
|
|
|
plan = Plan.where(group_id: @user_without_subscription.group.id, type: 'Plan').first
|
|
|
|
|
|
|
|
reservations_count = Reservation.count
|
|
|
|
invoice_count = Invoice.count
|
|
|
|
invoice_items_count = InvoiceItem.count
|
|
|
|
users_credit_count = UsersCredit.count
|
|
|
|
|
2021-05-19 18:12:52 +02:00
|
|
|
post '/api/local_payment/confirm_payment', params: {
|
2021-04-23 17:54:59 +02:00
|
|
|
customer_id: @user_without_subscription.id,
|
2021-05-19 18:12:52 +02:00
|
|
|
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),
|
|
|
|
offered: false,
|
|
|
|
availability_id: availability.id
|
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}
|
2021-05-19 18:12:52 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
subscription: {
|
2021-05-28 17:34:20 +02:00
|
|
|
plan_id: plan.id
|
2021-05-19 18:12:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}.to_json, headers: default_headers
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# general assertions
|
|
|
|
assert_equal 201, response.status
|
|
|
|
assert_equal Mime[:json], response.content_type
|
|
|
|
result = json_response(response.body)
|
|
|
|
|
|
|
|
# Check the DB objects have been created as they should
|
|
|
|
assert_equal reservations_count + 1, Reservation.count
|
|
|
|
assert_equal invoice_count + 1, Invoice.count
|
|
|
|
assert_equal invoice_items_count + 2, InvoiceItem.count
|
|
|
|
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
|
|
|
|
|
|
|
|
# reservation assertions
|
2021-05-31 11:52:53 +02:00
|
|
|
invoice = Invoice.find(result[:id])
|
|
|
|
reservation = invoice.main_item.object
|
2020-12-15 17:14:58 +01:00
|
|
|
|
2021-05-28 17:34:20 +02:00
|
|
|
assert reservation.original_invoice
|
|
|
|
assert_equal 2, reservation.original_invoice.invoice_items.count
|
2020-12-15 17:14:58 +01:00
|
|
|
|
|
|
|
# credits assertions
|
|
|
|
assert_equal 1, @user_without_subscription.credits.count
|
|
|
|
assert_equal 'Training', @user_without_subscription.credits.last.creditable_type
|
|
|
|
assert_equal training.id, @user_without_subscription.credits.last.creditable_id
|
|
|
|
|
|
|
|
# invoice assertions
|
2021-05-28 17:34:20 +02:00
|
|
|
invoice = reservation.original_invoice
|
2020-12-15 17:14:58 +01:00
|
|
|
|
2021-04-23 17:54:59 +02:00
|
|
|
assert invoice.payment_gateway_object.blank?
|
2020-12-15 17:14:58 +01:00
|
|
|
refute invoice.total.blank?
|
|
|
|
assert_equal plan.amount, invoice.total
|
|
|
|
|
|
|
|
# invoice_items
|
|
|
|
invoice_items = InvoiceItem.last(2)
|
|
|
|
|
2021-05-31 11:52:53 +02:00
|
|
|
assert(invoice_items.any? { |ii| ii.amount == plan.amount && !ii.subscription.nil? })
|
2020-12-15 17:14:58 +01:00
|
|
|
assert(invoice_items.any? { |ii| ii.amount.zero? })
|
|
|
|
|
|
|
|
# invoice assertions
|
2021-05-27 15:58:55 +02:00
|
|
|
item = InvoiceItem.find_by(object: reservation)
|
|
|
|
invoice = item.invoice
|
2020-12-15 17:14:58 +01:00
|
|
|
assert_invoice_pdf invoice
|
|
|
|
|
|
|
|
# notification
|
|
|
|
assert_not_empty Notification.where(attached_object: reservation)
|
|
|
|
end
|
2016-04-07 16:02:09 +02:00
|
|
|
|
2020-12-15 17:14:58 +01:00
|
|
|
test 'user reserves a training and a subscription with payment schedule' do
|
|
|
|
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
|
2016-04-07 16:02:09 +02:00
|
|
|
|
2020-12-15 17:14:58 +01:00
|
|
|
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')
|
2016-04-07 16:02:09 +02:00
|
|
|
|
2020-12-15 17:14:58 +01:00
|
|
|
VCR.use_cassette('reservations_admin_training_subscription_with_payment_schedule') do
|
2021-05-19 18:12:52 +02:00
|
|
|
post '/api/local_payment/confirm_payment', params: {
|
2021-01-04 16:26:44 +01:00
|
|
|
payment_method: 'check',
|
2021-04-23 17:54:59 +02:00
|
|
|
payment_schedule: true,
|
|
|
|
customer_id: @user_without_subscription.id,
|
2021-05-19 18:12:52 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}
|
2021-05-19 18:12:52 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
subscription: {
|
|
|
|
plan_id: plan.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2021-04-23 17:54:59 +02:00
|
|
|
}.to_json, headers: default_headers
|
2016-07-11 15:32:42 +02:00
|
|
|
end
|
2018-12-12 13:49:14 +01:00
|
|
|
|
2021-01-04 16:26:44 +01:00
|
|
|
# get the objects
|
|
|
|
reservation = Reservation.last
|
|
|
|
payment_schedule = PaymentSchedule.last
|
|
|
|
|
2020-12-15 17:14:58 +01:00
|
|
|
# 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"
|
|
|
|
|
2021-01-04 16:26:44 +01:00
|
|
|
# payment schedule assertions
|
2021-05-28 17:34:20 +02:00
|
|
|
assert reservation.original_payment_schedule
|
|
|
|
assert_equal payment_schedule.id, reservation.original_payment_schedule.id
|
2021-01-04 16:26:44 +01:00
|
|
|
assert_not_nil payment_schedule.reference
|
|
|
|
assert_equal 'check', payment_schedule.payment_method
|
2021-04-23 17:54:59 +02:00
|
|
|
assert_empty payment_schedule.payment_gateway_objects
|
2021-01-04 16:26:44 +01:00
|
|
|
assert_nil payment_schedule.wallet_transaction
|
|
|
|
assert_nil payment_schedule.wallet_amount
|
|
|
|
assert_nil payment_schedule.coupon_id
|
|
|
|
assert_equal 'test', payment_schedule.environment
|
|
|
|
assert payment_schedule.check_footprint
|
|
|
|
assert_equal @user_without_subscription.invoicing_profile.id, payment_schedule.invoicing_profile_id
|
|
|
|
assert_equal @admin.invoicing_profile.id, payment_schedule.operator_profile_id
|
2021-05-28 17:34:20 +02:00
|
|
|
|
|
|
|
# Check the answer
|
|
|
|
result = json_response(response.body)
|
|
|
|
assert_equal reservation.original_payment_schedule.id, result[:id], 'payment schedule id does not match'
|
|
|
|
|
|
|
|
# reservation assertions
|
|
|
|
assert_equal result[:main_object][:id], reservation.id
|
|
|
|
assert_equal payment_schedule.main_object.object, reservation
|
2016-04-07 16:02:09 +02:00
|
|
|
end
|
|
|
|
end
|