From 4b4542ab8e6053291a1575d926672804d093de7b Mon Sep 17 00:00:00 2001 From: Duncan Date: Fri, 20 Sep 2024 16:52:00 +0200 Subject: [PATCH 1/4] added test for organizer to edit series reg --- app/services/registration_checker.rb | 8 +++- .../registration_controller_spec.rb | 2 +- spec/services/registration_checker_spec.rb | 38 ++++++++++++++++++- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/app/services/registration_checker.rb b/app/services/registration_checker.rb index 639589a5..ffb0e7a8 100644 --- a/app/services/registration_checker.rb +++ b/app/services/registration_checker.rb @@ -74,7 +74,8 @@ def user_can_modify_registration! raise RegistrationError.new(:unauthorized, ErrorCodes::USER_INSUFFICIENT_PERMISSIONS) unless can_administer_or_current_user? raise RegistrationError.new(:forbidden, ErrorCodes::USER_EDITS_NOT_ALLOWED) unless @competition_info.registration_edits_allowed? || UserApi.can_administer?(@requester_user_id, @competition_info.id) raise RegistrationError.new(:unauthorized, ErrorCodes::REGISTRATION_IS_REJECTED) if user_is_rejected? - raise RegistrationError.new(:forbidden, ErrorCodes::ALREADY_REGISTERED_IN_SERIES) if existing_registration_in_series? + raise RegistrationError.new(:forbidden, ErrorCodes::ALREADY_REGISTERED_IN_SERIES) if + existing_registration_in_series? && !UserApi.can_administer?(@requester_user_id, @competition_info.id) end def user_is_rejected? @@ -173,6 +174,8 @@ 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 + raise RegistrationError.new(:forbidden, ErrorCodes::ALREADY_REGISTERED_IN_SERIES) if + new_status == 'accepted' && existing_registration_in_series? # Otherwise, organizers can make any status change they want to return if UserApi.can_administer?(@requester_user_id, @competition_info.id) @@ -211,7 +214,8 @@ def existing_registration_in_series? return false if @competition_info.other_series_ids.nil? @competition_info.other_series_ids.each do |comp_id| - return Registration.find("#{comp_id}-#{@requestee_user_id}").competing_status != 'cancelled' + series_reg = Registration.find("#{comp_id}-#{@requestee_user_id}") + return !%w[cancelled rejected].include?(series_reg.competing_status) rescue Dynamoid::Errors::RecordNotFound next end 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 339cf133..bcc8619b 100644 --- a/spec/services/registration_checker_spec.rb +++ b/spec/services/registration_checker_spec.rb @@ -1386,7 +1386,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) @@ -1557,6 +1557,42 @@ it_behaves_like 'update succeed: qualification enforced', 'can register when minx average exists for ranking-average', ['minx'] end end + + describe '#update_registration_allowed!.organizer updates series reg' do + it 'organizer cant set status to accepted if attendee is accepted for another series comp' do + cancelled_registration = FactoryBot.create(:registration, registration_status: 'cancelled') + FactoryBot.create(:registration, user_id: cancelled_registration['user_id'], registration_status: 'accepted', competition_id: 'CubingZAWarmup2023') + + series_competition_info = CompetitionInfo.new(FactoryBot.build(:competition, :series)) + + update_request = FactoryBot.build(:update_request, :organizer_for_user, user_id: cancelled_registration[:user_id], competing: { 'status' => 'accepted' }) + + expect { + RegistrationChecker.update_registration_allowed!(update_request, series_competition_info, update_request['submitted_by']) + }.to raise_error(RegistrationError) do |error| + expect(error.error).to eq(ErrorCodes::ALREADY_REGISTERED_IN_SERIES) + expect(error.http_status).to eq(:forbidden) + end + end + + it 'organizer can update admin comment in attendees non-accepted series comp registration' do + cancelled_registration = FactoryBot.create(:registration, registration_status: 'cancelled') + FactoryBot.create(:registration, user_id: cancelled_registration['user_id'], registration_status: 'accepted', competition_id: 'CubingZAWarmup2023') + + series_competition_info = CompetitionInfo.new(FactoryBot.build(:competition, :series)) + + update_request = FactoryBot.build( + :update_request, + :organizer_for_user, + user_id: cancelled_registration[:user_id], + competing: { 'admin_comment' => 'why they were cancelled' } + ) + + expect { + RegistrationChecker.update_registration_allowed!(update_request, series_competition_info, update_request['submitted_by']) + }.not_to raise_error + end + end end describe '#bulk_update' do From e7a64c5cc8a34db566f983aeacb41daeb1f58443 Mon Sep 17 00:00:00 2001 From: Duncan Date: Fri, 20 Sep 2024 16:55:51 +0200 Subject: [PATCH 2/4] rubocop --- app/services/registration_checker.rb | 2 +- spec/services/registration_checker_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/services/registration_checker.rb b/app/services/registration_checker.rb index ffb0e7a8..6123af3f 100644 --- a/app/services/registration_checker.rb +++ b/app/services/registration_checker.rb @@ -215,7 +215,7 @@ def existing_registration_in_series? @competition_info.other_series_ids.each do |comp_id| series_reg = Registration.find("#{comp_id}-#{@requestee_user_id}") - return !%w[cancelled rejected].include?(series_reg.competing_status) + return %w[cancelled rejected].exclude?(series_reg.competing_status) rescue Dynamoid::Errors::RecordNotFound next end diff --git a/spec/services/registration_checker_spec.rb b/spec/services/registration_checker_spec.rb index bcc8619b..fa8ad68d 100644 --- a/spec/services/registration_checker_spec.rb +++ b/spec/services/registration_checker_spec.rb @@ -1585,7 +1585,7 @@ :update_request, :organizer_for_user, user_id: cancelled_registration[:user_id], - competing: { 'admin_comment' => 'why they were cancelled' } + competing: { 'admin_comment' => 'why they were cancelled' }, ) expect { From 768dd4b4f5a697a6444e7723031226da2b32f418 Mon Sep 17 00:00:00 2001 From: Duncan Date: Fri, 20 Sep 2024 17:13:02 +0200 Subject: [PATCH 3/4] added might_attend status --- app/models/registration.rb | 5 +++++ app/services/registration_checker.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/models/registration.rb b/app/models/registration.rb index ddfbca22..56c59c30 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -10,6 +10,7 @@ class Registration REGISTRATION_STATES = %w[pending waiting_list accepted cancelled rejected].freeze ADMIN_ONLY_STATES = %w[pending waiting_list accepted rejected].freeze # Only admins are allowed to change registration state to one of these states + MIGHT_ATTEND_STATES = %w[pending waiting_list accepted] # Pre-validations before_validation :set_competing_status @@ -171,6 +172,10 @@ def update_payment_lane(id, iso_amount, currency_iso, status) update_attributes!(lanes: updated_lanes) end + def might_attend? + MIGHT_ATTEND_STATES.include?(self.competing_status) + end + def update_waiting_list(update_params, waiting_list) raise ArgumentError.new('Can only accept waiting list leader') if update_params[:status] == 'accepted' && waiting_list_position(waiting_list) != 1 diff --git a/app/services/registration_checker.rb b/app/services/registration_checker.rb index 6123af3f..ed06ce90 100644 --- a/app/services/registration_checker.rb +++ b/app/services/registration_checker.rb @@ -215,7 +215,7 @@ def existing_registration_in_series? @competition_info.other_series_ids.each do |comp_id| series_reg = Registration.find("#{comp_id}-#{@requestee_user_id}") - return %w[cancelled rejected].exclude?(series_reg.competing_status) + return series_reg.might_attend? rescue Dynamoid::Errors::RecordNotFound next end From de2d5d322b909c3dd53059363097edbc6299f604 Mon Sep 17 00:00:00 2001 From: Duncan Date: Fri, 20 Sep 2024 17:16:30 +0200 Subject: [PATCH 4/4] 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 56c59c30..e56f275a 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -10,7 +10,7 @@ class Registration REGISTRATION_STATES = %w[pending waiting_list accepted cancelled rejected].freeze ADMIN_ONLY_STATES = %w[pending waiting_list accepted rejected].freeze # Only admins are allowed to change registration state to one of these states - MIGHT_ATTEND_STATES = %w[pending waiting_list accepted] + MIGHT_ATTEND_STATES = %w[pending waiting_list accepted].freeze # Pre-validations before_validation :set_competing_status