Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Added newcomers_allowed check #558

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/helpers/competition_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,8 @@ def user_can_cancel?
def other_series_ids
@competition_json['competition_series_ids']&.reject { |id| id == competition_id }
end

def newcomers_allowed?
@competition_json['newcomers_allowed']
end
end
1 change: 1 addition & 0 deletions app/helpers/error_codes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module ErrorCodes
ORGANIZER_MUST_CANCEL_REGISTRATION = -4009
INVALID_WAITING_LIST_POSITION = -4010
MUST_ACCEPT_WAITING_LIST_LEADER = -4011
WCA_ID_REQUIRED = -4013

# Payment Errors
PAYMENT_NOT_ENABLED = -3001
Expand Down
97 changes: 66 additions & 31 deletions app/helpers/mocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,72 @@ def self.pii_mock(user_ids)
end

def self.user_info_mock(user_ids)
{
'users' => user_ids.map do |u|
iso = %w[AD AE AI AL BW BY BZ CA CC NU NZ OM PA PE PN PR PS PTF TG TH TJ WS ZW].sample
wca_id = "2023TEST#{u % 99}"
{
'id' => u,
'created_at' => Time.now.to_s,
'updated_at' => Time.now.to_s,
'name' => "Name #{u}",
'wca_id' => wca_id,
'delegate_status' => nil,
'gender' => 'm',
'country_iso2' => iso,
'url' => "https://#{EnvConfig.WCA_HOST}/persons/#{wca_id}",
'country' => {
'id' => 'Test Country',
'name' => 'Test Country',
'continentId' => '_Europe',
'iso2' => iso,
},
'class' => 'user',
'teams' => [],
'avatar' => {
'url' => '',
'pending_url' => '',
'thumb_url' => '',
'is_default' => false,
},
}
end,
}
case user_ids
when [50]
{
'users' => user_ids.map do |u|
iso = %w[AD AE AI AL BW BY BZ CA CC NU NZ OM PA PE PN PR PS PTF TG TH TJ WS ZW].sample
wca_id = nil
{
'id' => u,
'created_at' => Time.now.to_s,
'updated_at' => Time.now.to_s,
'name' => "Name #{u}",
'wca_id' => wca_id,
'delegate_status' => nil,
'gender' => 'm',
'country_iso2' => iso,
'url' => nil,
'country' => {
'id' => 'Test Country',
'name' => 'Test Country',
'continentId' => '_Europe',
'iso2' => iso,
},
'class' => 'user',
'teams' => [],
'avatar' => {
'url' => '',
'pending_url' => '',
'thumb_url' => '',
'is_default' => false,
},
}
end,
}
else
{
'users' => user_ids.map do |u|
iso = %w[AD AE AI AL BW BY BZ CA CC NU NZ OM PA PE PN PR PS PTF TG TH TJ WS ZW].sample
wca_id = "2023TEST#{u % 99}"
{
'id' => u,
'created_at' => Time.now.to_s,
'updated_at' => Time.now.to_s,
'name' => "Name #{u}",
'wca_id' => wca_id,
'delegate_status' => nil,
'gender' => 'm',
'country_iso2' => iso,
'url' => "https://#{EnvConfig.WCA_HOST}/persons/#{wca_id}",
'country' => {
'id' => 'Test Country',
'name' => 'Test Country',
'continentId' => '_Europe',
'iso2' => iso,
},
'class' => 'user',
'teams' => [],
'avatar' => {
'url' => '',
'pending_url' => '',
'thumb_url' => '',
'is_default' => false,
},
}
end,
}
end
end

def self.permissions_mock(user_id)
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/user_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def self.get_user_info(user_ids)
end
end

def self.is_newcomer?(user_id)
self.get_user_info([user_id])['users'].first['wca_id'].nil?
end

def self.can_compete?(user_id)
# All User Related cache Keys should start with the UserID, so we could invalidate them on user update
# TODO: Move this to it's own cache helper class so this is guaranteed?
Expand Down
3 changes: 3 additions & 0 deletions app/services/registration_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def user_can_create_registration!
can_compete = UserApi.can_compete?(@requestee_user_id)
raise RegistrationError.new(:unauthorized, ErrorCodes::USER_CANNOT_COMPETE) unless can_compete

# Newcomers cannot sign up if no newcomers are allowed
raise RegistrationError.new(:forbidden, ErrorCodes::WCA_ID_REQUIRED) if !@competition_info.newcomers_allowed? && UserApi.is_newcomer?(@requestee_user_id)

# Users cannot sign up for multiple competitions in a series
raise RegistrationError.new(:forbidden, ErrorCodes::ALREADY_REGISTERED_IN_SERIES) if existing_registration_in_series?
end
Expand Down
1 change: 1 addition & 0 deletions spec/factories/competition_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
venue_address { 'South Africa, 28 Droste Cres, Droste Park, Johannesburg, 2094' }
venue_details { '' }
latitude_degrees { -26.21117 }
newcomers_allowed { true }
longitude_degrees { 28.06449 }
country_iso2 { 'ZA' }
guest_entry_status { 'restricted' }
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/request_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
competing { { 'event_ids' => events, 'comment' => raw_comment, 'lane_state' => 'pending' } }
end

trait :newcomer do
user_id { 50 }
end

trait :organizer do
user_id { 1306 }
jwt_token { fetch_jwt_token(user_id) }
Expand Down
21 changes: 21 additions & 0 deletions spec/services/registration_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,27 @@
.not_to raise_error
end

it 'user with wca id can register when wca id required' do
registration_request = FactoryBot.build(:registration_request)
competition_info = CompetitionInfo.new(FactoryBot.build(:competition, newcomers_allowed: false))

expect {
RegistrationChecker.create_registration_allowed!(registration_request, competition_info, registration_request['submitted_by'])
}.not_to raise_error
end

it 'newcomer cant register when wca id required' do
registration_request = FactoryBot.build(:registration_request, :newcomer)
competition_info = CompetitionInfo.new(FactoryBot.build(:competition, newcomers_allowed: false))

expect {
RegistrationChecker.create_registration_allowed!(registration_request, competition_info, registration_request['submitted_by'])
}.to raise_error(RegistrationError) do |error|
expect(error.http_status).to eq(:forbidden)
expect(error.error).to eq(ErrorCodes::WCA_ID_REQUIRED)
end
end

it 'users can only register for themselves' do
registration_request = FactoryBot.build(:registration_request, :impersonation)
competition_info = CompetitionInfo.new(FactoryBot.build(:competition))
Expand Down