From 0025baee476f7886a1e595db2eef0afd9f14682a Mon Sep 17 00:00:00 2001 From: Duncan Date: Fri, 30 Aug 2024 16:19:46 +0200 Subject: [PATCH 1/6] added check for registrations > competitor limit to also fail --- app/services/registration_checker.rb | 2 +- spec/services/registration_checker_spec.rb | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/services/registration_checker.rb b/app/services/registration_checker.rb index 3338e27f..7589b58f 100644 --- a/app/services/registration_checker.rb +++ b/app/services/registration_checker.rb @@ -165,7 +165,7 @@ def validate_update_status! raise RegistrationError.new(:unprocessable_entity, ErrorCodes::INVALID_REQUEST_DATA) unless Registration::REGISTRATION_STATES.include?(new_status) raise RegistrationError.new(:forbidden, ErrorCodes::COMPETITOR_LIMIT_REACHED) if - new_status == 'accepted' && Registration.accepted_competitors_count(@competition_info.competition_id) == @competition_info.competitor_limit + new_status == 'accepted' && Registration.accepted_competitors_count(@competition_info.competition_id) >= @competition_info.competitor_limit # Organizers cant accept someone from the waiting list who isn't in the leading position min_waiting_list_position = @registration.competing_lane.get_waiting_list_boundaries(@registration.competition_id)['waiting_list_position_min'] diff --git a/spec/services/registration_checker_spec.rb b/spec/services/registration_checker_spec.rb index b0166dea..98b7ef8e 100644 --- a/spec/services/registration_checker_spec.rb +++ b/spec/services/registration_checker_spec.rb @@ -1023,7 +1023,7 @@ end end - it 'organizer cant accept a user when registration list is full' do + it 'organizer cant accept a user when registration list is exactly full' do FactoryBot.create_list(:registration, 3, registration_status: 'accepted') override_registration = FactoryBot.create(:registration, user_id: 188000, registration_status: 'waiting_list') override_competition_info = CompetitionInfo.new(FactoryBot.build(:competition, competitor_limit: 3)) @@ -1037,6 +1037,21 @@ end end + it 'organizer cant accept a user when registration list is over full' do + FactoryBot.create_list(:registration, 4, registration_status: 'accepted') + override_registration = FactoryBot.create(:registration, user_id: 188000, registration_status: 'waiting_list') + override_competition_info = CompetitionInfo.new(FactoryBot.build(:competition, competitor_limit: 3)) + update_request = FactoryBot.build(:update_request, :organizer_for_user, user_id: override_registration[:user_id], competing: { 'status' => 'accepted' }) + + expect { + RegistrationChecker.update_registration_allowed!(update_request, override_competition_info, update_request['submitted_by']) + }.to raise_error(RegistrationError) do |error| + expect(error.error).to eq(ErrorCodes::COMPETITOR_LIMIT_REACHED) + expect(error.http_status).to eq(:forbidden) + end + end + + it 'organizer can accept registrations up to the limit' do FactoryBot.create_list(:registration, 2, registration_status: 'accepted') registration = FactoryBot.create(:registration, registration_status: 'waiting_list') From 06c834162872be9d5d8dc1d169e723fa9a99b9e9 Mon Sep 17 00:00:00 2001 From: Duncan Date: Mon, 2 Sep 2024 13:54:50 +0200 Subject: [PATCH 2/6] rubocop --- spec/services/registration_checker_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/services/registration_checker_spec.rb b/spec/services/registration_checker_spec.rb index 98b7ef8e..b33041b1 100644 --- a/spec/services/registration_checker_spec.rb +++ b/spec/services/registration_checker_spec.rb @@ -1051,7 +1051,6 @@ end end - it 'organizer can accept registrations up to the limit' do FactoryBot.create_list(:registration, 2, registration_status: 'accepted') registration = FactoryBot.create(:registration, registration_status: 'waiting_list') From 880d4d920ee62a61c6de73411145e24a5208d6dd Mon Sep 17 00:00:00 2001 From: Duncan Date: Wed, 4 Sep 2024 13:16:43 +0200 Subject: [PATCH 3/6] added integer conversion on redis retrieval --- app/models/registration.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/registration.rb b/app/models/registration.rb index cfcf6950..410f0f8d 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -32,7 +32,7 @@ def self.get_registrations_by_status(competition_id, status) end def self.accepted_competitors_count(competition_id) - Rails.cache.fetch("#{competition_id}-accepted-count", expires_in: 60.minutes, raw: true) do + Integer(Rails.cache.fetch("#{competition_id}-accepted-count", expires_in: 60.minutes, raw: true)) do self.accepted_competitors(competition_id) end end From b56c4f1d3d3ee38a57431f046e28e48246993c62 Mon Sep 17 00:00:00 2001 From: Duncan Date: Thu, 5 Sep 2024 13:50:08 +0200 Subject: [PATCH 4/6] wrapped entire cache call in integer conversion --- app/models/registration.rb | 8 +++++--- spec/controllers/registration_controller_spec.rb | 2 +- spec/services/registration_checker_spec.rb | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/models/registration.rb b/app/models/registration.rb index 27a63511..78ed1360 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -32,9 +32,11 @@ def self.get_registrations_by_status(competition_id, status) end def self.accepted_competitors_count(competition_id) - Integer(Rails.cache.fetch("#{competition_id}-accepted-count", expires_in: 60.minutes, raw: true)) do - self.accepted_competitors(competition_id) - end + Integer( + Rails.cache.fetch("#{competition_id}-accepted-count", expires_in: 60.minutes, raw: true) do + self.accepted_competitors(competition_id) + end + ) end def self.decrement_competitors_count(competition_id) diff --git a/spec/controllers/registration_controller_spec.rb b/spec/controllers/registration_controller_spec.rb index 5c3dfb3b..aa3d3e17 100644 --- a/spec/controllers/registration_controller_spec.rb +++ b/spec/controllers/registration_controller_spec.rb @@ -265,7 +265,7 @@ end end - describe '#list_admin', :tag do + describe '#list_admin' do before do @competition = FactoryBot.build(:competition) stub_json(CompetitionApi.url(@competition['id']), 200, @competition.except('qualifications')) diff --git a/spec/services/registration_checker_spec.rb b/spec/services/registration_checker_spec.rb index 93d41a0f..f73a37e9 100644 --- a/spec/services/registration_checker_spec.rb +++ b/spec/services/registration_checker_spec.rb @@ -1400,7 +1400,7 @@ end end - it 'cannot move to less than position 1', :tag2 do + it 'cannot move to less than position 1' do @waiting_list.add(FactoryBot.create(:registration, registration_status: 'waiting_list').user_id) @waiting_list.add(FactoryBot.create(:registration, registration_status: 'waiting_list').user_id) @waiting_list.add(FactoryBot.create(:registration, registration_status: 'waiting_list').user_id) From b892dc1fda50aad810502885e249f241e1539cbf Mon Sep 17 00:00:00 2001 From: Duncan Date: Thu, 5 Sep 2024 13:51:21 +0200 Subject: [PATCH 5/6] rubocop --- app/models/registration.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/registration.rb b/app/models/registration.rb index 78ed1360..ba05d53b 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -35,7 +35,7 @@ def self.accepted_competitors_count(competition_id) Integer( Rails.cache.fetch("#{competition_id}-accepted-count", expires_in: 60.minutes, raw: true) do self.accepted_competitors(competition_id) - end + end, ) end From 782a472b7bda45a9aea1b23dcb1dff815416e8e0 Mon Sep 17 00:00:00 2001 From: Duncan Date: Tue, 29 Oct 2024 13:56:32 +0200 Subject: [PATCH 6/6] finn comments --- app/controllers/registration_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/registration_controller.rb b/app/controllers/registration_controller.rb index 82d3fc06..9fa2a2cd 100644 --- a/app/controllers/registration_controller.rb +++ b/app/controllers/registration_controller.rb @@ -28,7 +28,7 @@ def show def count competition_id = params.require(:competition_id) - render json: { count: Registration.accepted_competitors_count(competition_id).to_i } + render json: { count: Registration.accepted_competitors_count(competition_id) } end def create