From 2233ae0b727f9930d35538c4843a617f026234fa Mon Sep 17 00:00:00 2001 From: Duncan <52967253+dunkOnIT@users.noreply.github.com> Date: Tue, 29 Oct 2024 14:01:31 +0200 Subject: [PATCH] Reimplment >= competitor limit check (#673) * added check for registrations > competitor limit to also fail * rubocop * added integer conversion on redis retrieval * wrapped entire cache call in integer conversion * rubocop * finn comments --- app/controllers/registration_controller.rb | 2 +- app/models/registration.rb | 8 +++++--- spec/services/registration_checker_spec.rb | 16 +++++++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) 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 diff --git a/app/models/registration.rb b/app/models/registration.rb index ff219030..19769870 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -37,9 +37,11 @@ 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 - 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/services/registration_checker_spec.rb b/spec/services/registration_checker_spec.rb index 0c02fc39..518fee1a 100644 --- a/spec/services/registration_checker_spec.rb +++ b/spec/services/registration_checker_spec.rb @@ -1080,7 +1080,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)) @@ -1094,6 +1094,20 @@ 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: 'pending')