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)
|
|
|
|
|
|
|
|
@user = User.joins(:subscriptions).find_by(subscriptions: { plan: @plan })
|
|
|
|
|
|
|
|
@user.reservations.destroy_all # ensure no reservations
|
|
|
|
|
|
|
|
@availability = @machine.availabilities.first
|
|
|
|
slot = Slot.new(start_at: @availability.start_at, end_at: @availability.end_at, availability_id: @availability.id)
|
|
|
|
@reservation_machine = Reservation.new(user: @user, reservable: @machine, slots: [slot])
|
|
|
|
@reservation_training = Reservation.new(user: @user, reservable: @training, slots: [slot])
|
|
|
|
@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
|
|
|
|
assert_equal @reservation_training.slots.first.start_at + @plan.duration, @user.subscription.expired_at
|
|
|
|
end
|
|
|
|
end
|