From 02b56ed6a7b8ebd07b3ccb4471f87175e1652d33 Mon Sep 17 00:00:00 2001 From: Nicolas Florentin Date: Thu, 7 Apr 2016 16:01:31 +0200 Subject: [PATCH] users_credits/manager service, handle the case if user does not have a plan --- app/services/users_credits/manager.rb | 4 +++ test/services/users_credits_manager_test.rb | 28 +++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/services/users_credits/manager.rb b/app/services/users_credits/manager.rb index 451b825ab..35a1d2dca 100644 --- a/app/services/users_credits/manager.rb +++ b/app/services/users_credits/manager.rb @@ -89,6 +89,8 @@ module UsersCredits private def _will_use_credits? + return false, 0 unless plan + if machine_credit = plan.machine_credits.find_by(creditable_id: reservation.reservable_id) users_credit = user.users_credits.find_by(credit_id: machine_credit.id) already_used_hours = users_credit ? users_credit.hours_used : 0 @@ -122,6 +124,8 @@ module UsersCredits private def _will_use_credits? + return false, nil unless plan + # if there is a training_credit defined for this plan and this training if training_credit = plan.training_credits.find_by(creditable_id: reservation.reservable_id) # if user has not used all the plan credits diff --git a/test/services/users_credits_manager_test.rb b/test/services/users_credits_manager_test.rb index aa69eab83..f8a3c72c0 100644 --- a/test/services/users_credits_manager_test.rb +++ b/test/services/users_credits_manager_test.rb @@ -6,12 +6,28 @@ class UsersCreditsManagerTest < ActiveSupport::TestCase @training = Training.find(2) @plan = Plan.find(3) @user = User.joins(:subscriptions).find_by(subscriptions: { plan: @plan }) + @user.users_credits.destroy_all @availability = @machine.availabilities.first @reservation_machine = Reservation.new(user: @user, reservable: @machine) @reservation_training = Reservation.new(user: @user, reservable: @training) end ## context machine reservation + test "machine reservation from user without subscribed plan" do + @user.subscriptions.destroy_all + + @reservation_machine.assign_attributes(slots_attributes: [{ + start_at: @availability.start_at, end_at: @availability.start_at + 1.hour, availability_id: @availability.id + }]) + manager = UsersCredits::Manager.new(reservation: @reservation_machine) + + assert_equal false, manager.will_use_credits? + assert_equal 0, manager.free_hours_count + + assert_no_difference 'UsersCredit.count' do + manager.update_credits + end + end test "machine reservation without credit associated" do Credit.where(creditable: @machine).destroy_all @@ -101,6 +117,18 @@ class UsersCreditsManagerTest < ActiveSupport::TestCase # context training reservation + test "training reservation from user without subscribed plan" do + @user.subscriptions.destroy_all + + manager = UsersCredits::Manager.new(reservation: @reservation_training) + + assert_equal false, manager.will_use_credits? + + assert_no_difference 'UsersCredit.count' do + manager.update_credits + end + end + test "training reservation without credit associated" do Credit.where(creditable: @training).destroy_all