From 56361c4f7794e97a3ba6b9d9f33381245f354dec Mon Sep 17 00:00:00 2001 From: Duncan Date: Mon, 12 Aug 2024 08:13:31 +0200 Subject: [PATCH] waiting list controller tests --- app/services/registration_checker.rb | 14 +++++- .../registration_controller_spec.rb | 46 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/app/services/registration_checker.rb b/app/services/registration_checker.rb index 09aaea5c..cffe47a2 100644 --- a/app/services/registration_checker.rb +++ b/app/services/registration_checker.rb @@ -36,6 +36,8 @@ def self.update_registration_allowed!(update_request, competition_info, requesti end def self.bulk_update_allowed!(bulk_update_request, competition_info, requesting_user) + @competition_info = competition_info + raise BulkUpdateError.new(:unauthorized, [ErrorCodes::USER_INSUFFICIENT_PERMISSIONS]) unless UserApi.can_administer?(requesting_user, competition_info.id) @@ -45,7 +47,8 @@ def self.bulk_update_allowed!(bulk_update_request, competition_info, requesting_ rescue RegistrationError => e errors[update_request['user_id']] = e.error end - raise BulkUpdateError.new(:unprocessable_entity, errors) unless errors == {} + + raise BulkUpdateError.new(:unprocessable_entity, errors) unless errors.empty? || sequential_waiting_list_updates?(bulk_update_request, errors) end class << self @@ -202,5 +205,14 @@ def existing_registration_in_series? end false end + + def sequential_waiting_list_updates?(bulk_update_request, errors) + return false unless errors.values.uniq == [-4011] + + accepted_users = bulk_update_request['requests'].map { |r| r['user_id'] if r['competing']['status'] == 'accepted' } + waiting_list_users = WaitingList.find(@competition_info.id).entries.take(accepted_users.count) + return false unless accepted_users == waiting_list_users + true + end end end diff --git a/spec/controllers/registration_controller_spec.rb b/spec/controllers/registration_controller_spec.rb index 5271b74f..f4dffb40 100644 --- a/spec/controllers/registration_controller_spec.rb +++ b/spec/controllers/registration_controller_spec.rb @@ -102,7 +102,51 @@ headers: { content_type: 'application/json' }, ) end - # TODO: Consider refactor into separate contexts with one expect() per it-block + + describe 'waiting list bulk updates' do + before do + @registration = FactoryBot.create(:registration, :waiting_list) + @update = FactoryBot.build(:update_request, user_id: @registration[:user_id], competing: { 'status' => 'accepted' }) + @registration2 = FactoryBot.create(:registration, :waiting_list) + @update2 = FactoryBot.build(:update_request, user_id: @registration2[:user_id], competing: { 'status' => 'accepted' }) + @registration3 = FactoryBot.create(:registration, :waiting_list) + @update3 = FactoryBot.build(:update_request, user_id: @registration3[:user_id], competing: { 'status' => 'accepted' }) + + @waiting_list = FactoryBot.create(:waiting_list, entries: [@registration.user_id, @registration2.user_id, @registration3.user_id]) + + end + + it 'accepts competitors from the waiting list in the order they appear' do + updates = [@update, @update2, @update3] + bulk_update_request = FactoryBot.build(:bulk_update_request, requests: updates) + + request.headers['Authorization'] = bulk_update_request['jwt_token'] + patch :bulk_update, params: bulk_update_request, as: :json + + expect(response.code).to eq('200') + + @updated_registration = Registration.find("#{@competition['id']}-#{@registration[:user_id]}") + expect(@updated_registration.competing_status).to eq('accepted') + @updated_registration2 = Registration.find("#{@competition['id']}-#{@registration2[:user_id]}") + expect(@updated_registration2.competing_status).to eq('accepted') + @updated_registration3 = Registration.find("#{@competition['id']}-#{@registration3[:user_id]}") + expect(@updated_registration3.competing_status).to eq('accepted') + + expect(@waiting_list.reload.entries.empty?).to eq(true) + end + + it 'cant accept competitors from waiting list without accepting leader', :tag do + updates = [@update2, @update3] + bulk_update_request = FactoryBot.build(:bulk_update_request, requests: updates) + + request.headers['Authorization'] = bulk_update_request['jwt_token'] + patch :bulk_update, params: bulk_update_request, as: :json + + expect(response.code).to eq('422') + expect(JSON.parse(response.body)['error'].values).to eq([-4011, -4011]) + end + end + it 'returns a 422 if there are validation errors' do registration = FactoryBot.create(:registration) update = FactoryBot.build(:update_request, user_id: registration[:user_id])