Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport new features in v2 registrations to v3 #10162

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
4 changes: 0 additions & 4 deletions app/models/competition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1112,10 +1112,6 @@ def entry_fee_required?
)
end

def competitor_limit_enabled?
competitor_limit_enabled
end

def competitor_limit_required?
confirmed? && created_at.present? && created_at > Date.new(2018, 9, 1)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/registrations/registration_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def user_can_create_registration!(competition, current_user, target_user)
def user_can_modify_registration!(competition, current_user, target_user, registration)
raise WcaExceptions::RegistrationError.new(:unauthorized, Registrations::ErrorCodes::USER_INSUFFICIENT_PERMISSIONS) unless can_administer_or_current_user?(competition, current_user, target_user)
raise WcaExceptions::RegistrationError.new(:forbidden, Registrations::ErrorCodes::USER_EDITS_NOT_ALLOWED) unless competition.registration_edits_allowed? || current_user.can_manage_competition?(competition)
raise WcaExceptions::RegistrationError.new(:unauthorized, Registrations::ErrorCodes::REGISTRATION_IS_REJECTED) if user_is_rejected?(current_user, target_user, registration)
raise WcaExceptions::RegistrationError.new(:unauthorized, Registrations::ErrorCodes::REGISTRATION_IS_REJECTED) if user_is_rejected?(current_user, target_user, registration) && !organizer_modifying_own_registration?
raise WcaExceptions::RegistrationError.new(:forbidden, Registrations::ErrorCodes::ALREADY_REGISTERED_IN_SERIES) if
existing_registration_in_series?(competition, target_user) && !current_user.can_manage_competition?(competition)
end
Expand Down Expand Up @@ -167,7 +167,7 @@ def contains_organizer_fields?(request, organizer_fields)
def validate_update_status!(new_status, competition, current_user, target_user, registration)
raise WcaExceptions::RegistrationError.new(:unprocessable_entity, Registrations::ErrorCodes::INVALID_REQUEST_DATA) unless Registrations::Helper::REGISTRATION_STATES.include?(new_status)
raise WcaExceptions::RegistrationError.new(:forbidden, Registrations::ErrorCodes::COMPETITOR_LIMIT_REACHED) if
new_status == 'accepted' && Registration.accepted.count == competition.competitor_limit
new_status == 'accepted' && competition.competitor_limit_enabled? && Registration.accepted.count >= competition.competitor_limit
raise WcaExceptions::RegistrationError.new(:forbidden, Registrations::ErrorCodes::ALREADY_REGISTERED_IN_SERIES) if
new_status == 'accepted' && existing_registration_in_series?(competition, target_user)

Expand Down
39 changes: 38 additions & 1 deletion spec/lib/registrations/registration_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,23 @@
end
end

it 'organizer cant accept a user when registration list is full' do
it 'organizer can accept registrations when there is no competitor limit' do
no_competitor_limit = FactoryBot.create(:competition, :with_organizer)
registration = FactoryBot.create(:registration, competition: no_competitor_limit)

update_request = FactoryBot.build(
:update_request,
user_id: registration.user_id,
competition_id: registration.competition.id,
submitted_by: no_competitor_limit.organizers.first.id,
competing: { 'status' => 'accepted' },
)

expect { Registrations::RegistrationChecker.update_registration_allowed!(update_request, Competition.find(update_request['competition_id']), User.find(update_request['submitted_by'])) }
.not_to raise_error
end

it 'organizer cant accept a user when registration list is exactly full' do
competitor_limit = FactoryBot.create(:competition, :with_competitor_limit, :with_organizer, competitor_limit: 3)
limited_reg = FactoryBot.create(:registration, competition: competitor_limit)
FactoryBot.create_list(:registration, 3, :accepted, competition: competitor_limit)
Expand All @@ -1276,6 +1292,27 @@
end
end

it 'organizer cant accept a user when registration list is over full' do
competitor_limit = FactoryBot.create(:competition, :with_competitor_limit, :with_organizer, competitor_limit: 3)
limited_reg = FactoryBot.create(:registration, competition: competitor_limit)
FactoryBot.create_list(:registration, 4, :accepted, competition: competitor_limit)

update_request = FactoryBot.build(
:update_request,
user_id: limited_reg.user_id,
competition_id: limited_reg.competition.id,
submitted_by: competitor_limit.organizers.first.id,
competing: { 'status' => 'accepted' },
)

expect {
Registrations::RegistrationChecker.update_registration_allowed!(update_request, User.find(update_request['submitted_by']))
}.to raise_error(WcaExceptions::RegistrationError) do |error|
expect(error.error).to eq(Registrations::ErrorCodes::COMPETITOR_LIMIT_REACHED)
expect(error.status).to eq(:forbidden)
end
end

it 'organizer can accept registrations up to the limit' do
competitor_limit = FactoryBot.create(:competition, :with_competitor_limit, :with_organizer, competitor_limit: 3)
limited_reg = FactoryBot.create(:registration, competition: competitor_limit)
Expand Down
Loading