2022-07-05 16:11:45 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-07 17:55:36 +02:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class SubscriptionExtensionAfterReservationTest < ActiveSupport::TestCase
|
|
|
|
setup do
|
|
|
|
@machine = Machine.find(6)
|
|
|
|
@training = Training.find(2)
|
|
|
|
|
|
|
|
@plan = Plan.find(3)
|
|
|
|
@plan.update!(is_rolling: true)
|
|
|
|
|
2019-06-04 16:50:23 +02:00
|
|
|
@user = User.joins(statistic_profile: [:subscriptions]).find_by(subscriptions: { plan_id: @plan.id })
|
2016-04-07 17:55:36 +02:00
|
|
|
|
|
|
|
@user.reservations.destroy_all # ensure no reservations
|
|
|
|
|
2022-07-18 17:17:21 +02:00
|
|
|
@slot_reservation_machine = SlotsReservation.new({ slot_id: @machine.availabilities.first.slots.first.id })
|
|
|
|
@slot_reservation_training = SlotsReservation.new({ slot_id: @training.availabilities.first.slots.first.id })
|
|
|
|
|
2022-07-13 16:28:43 +02:00
|
|
|
@reservation_machine = Reservation.new(
|
|
|
|
statistic_profile: @user.statistic_profile,
|
|
|
|
reservable: @machine,
|
2022-07-18 17:17:21 +02:00
|
|
|
slots_reservations: [@slot_reservation_machine]
|
2022-07-13 16:28:43 +02:00
|
|
|
)
|
|
|
|
@reservation_training = Reservation.new(
|
|
|
|
statistic_profile: @user.statistic_profile,
|
|
|
|
reservable: @training,
|
2022-07-18 17:17:21 +02:00
|
|
|
slots_reservations: [@slot_reservation_training]
|
2022-07-13 16:28:43 +02:00
|
|
|
)
|
2016-04-07 17:55:36 +02:00
|
|
|
@reservation_training.save!
|
|
|
|
end
|
|
|
|
|
2018-12-10 17:20:23 +01:00
|
|
|
test 'is eligible for extension because all conditions are met by default (test setup)' do
|
2016-04-07 17:55:36 +02:00
|
|
|
assert SubscriptionExtensionAfterReservation.new(@reservation_training).eligible_to_extension?
|
|
|
|
end
|
|
|
|
|
2018-12-10 17:20:23 +01:00
|
|
|
test 'not eligible if reservable is a machine' do
|
2016-04-07 17:55:36 +02:00
|
|
|
@reservation_machine.save!
|
|
|
|
refute SubscriptionExtensionAfterReservation.new(@reservation_machine).eligible_to_extension?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "not eligible if user doesn't have subscription" do
|
|
|
|
@user.subscriptions.destroy_all
|
|
|
|
refute SubscriptionExtensionAfterReservation.new(@reservation_training).eligible_to_extension?
|
|
|
|
end
|
|
|
|
|
2018-12-10 17:20:23 +01:00
|
|
|
test 'not eligible if subscription is expired' do
|
|
|
|
@user.subscription.update!(expiration_date: 10.years.ago)
|
2016-04-07 17:55:36 +02:00
|
|
|
refute SubscriptionExtensionAfterReservation.new(@reservation_training).eligible_to_extension?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "not eligible if plan attribute 'is_rolling' is false/nil" do
|
|
|
|
@plan.update!(is_rolling: false)
|
|
|
|
refute SubscriptionExtensionAfterReservation.new(@reservation_training).eligible_to_extension?
|
|
|
|
end
|
|
|
|
|
2018-12-10 17:20:23 +01:00
|
|
|
test 'method extend_subscription' do
|
2016-04-07 17:55:36 +02:00
|
|
|
SubscriptionExtensionAfterReservation.new(@reservation_training).extend_subscription
|
2022-07-18 17:17:21 +02:00
|
|
|
assert_equal @reservation_training.slots_reservations.first.slot.start_at + @plan.duration, @user.subscription.expired_at
|
2016-04-07 17:55:36 +02:00
|
|
|
end
|
|
|
|
end
|