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

Commit

Permalink
Allow a banned user to register for a competition which starts after …
Browse files Browse the repository at this point in the history
…their ban ends (#589)

* added test case for ban end date

* refactored ban end date check

* rubocop changes
  • Loading branch information
dunkOnIT authored May 24, 2024
1 parent 0b9baa3 commit 4140bd2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
4 changes: 4 additions & 0 deletions app/helpers/competition_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def initialize(competition_json)
@competition_id = competition_json['id']
end

def start_date
@competition_json['start_date']
end

def within_event_change_deadline?
return true if @competition_json['event_change_deadline_date'].nil?
Time.now < @competition_json['event_change_deadline_date']
Expand Down
9 changes: 7 additions & 2 deletions app/helpers/user_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ def self.get_user_info_pii(user_ids)
HTTParty.post(competitor_info_path, headers: { WCA_API_HEADER => self.wca_token }, body: { ids: user_ids.to_a })
end

def self.can_compete?(user_id)
def self.can_compete?(user_id, competition_start_date)
# 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?
permissions = Rails.cache.fetch("#{user_id}-permissions", expires_in: 5.minutes) do
self.get_permissions(user_id)
end
permissions['can_attend_competitions']['scope'] == '*'

competition_permissions = permissions['can_attend_competitions']
competition_start = DateTime.parse(competition_start_date)
ban_end = DateTime.parse(permissions['can_attend_competitions']['until'] || '3099-09-09')

competition_permissions['scope'] == '*' || ban_end < competition_start
end

def self.can_administer?(user_id, competition_id)
Expand Down
2 changes: 1 addition & 1 deletion app/services/registration_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def user_can_create_registration!
# Only organizers can register when registration is closed, and they can only register for themselves - not for other users
raise RegistrationError.new(:forbidden, ErrorCodes::REGISTRATION_CLOSED) unless @competition_info.registration_open? || organizer_modifying_own_registration?

can_compete = UserApi.can_compete?(@requestee_user_id)
can_compete = UserApi.can_compete?(@requestee_user_id, @competition_info.start_date)
raise RegistrationError.new(:unauthorized, ErrorCodes::USER_CANNOT_COMPETE) unless can_compete

# Users cannot sign up for multiple competitions in a series
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 @@ -39,6 +39,10 @@
user_id { 209943 }
end

trait :unbanned_soon do
user_id { 209944 }
end

trait :incomplete do
user_id { 999999 }
end
Expand Down
8 changes: 8 additions & 0 deletions spec/factories/response_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

FactoryBot.define do
factory :permissions_response, class: Hash do
transient do
ban_end_date { nil } # Specify as a string ISO-formatted date
end

organized_competitions { [] }

can_attend_competitions { { 'scope' => '*' } }
Expand All @@ -18,6 +22,10 @@
can_attend_competitions { { 'scope' => [] } }
end

trait :unbanned_soon do
can_attend_competitions { { 'scope' => [], 'until' => ban_end_date } }
end

initialize_with { attributes.stringify_keys }
end
end
19 changes: 17 additions & 2 deletions spec/services/registration_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,25 @@
end
end

it 'banned user cant register' do
it 'can register if ban ends before competition starts', :focus do
registration_request = FactoryBot.build(:registration_request, :unbanned_soon)
competition_info = CompetitionInfo.new(FactoryBot.build(:competition))
stub_request(:get, permissions_path(registration_request['user_id'])).to_return(
status: 200,
body: FactoryBot.build(:permissions_response, :unbanned_soon, ban_end_date: DateTime.parse(competition_info.start_date)-1).to_json,
headers: { content_type: 'application/json' },
)

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

it 'cant register if ban ends after competition starts', :focus do
registration_request = FactoryBot.build(:registration_request, :banned)
competition_info = CompetitionInfo.new(FactoryBot.build(:competition))
stub_request(:get, permissions_path(registration_request['user_id'])).to_return(status: 200, body: FactoryBot.build(:permissions_response, :banned).to_json, headers: { content_type: 'application/json' })
stub_request(:get, permissions_path(registration_request['user_id'])).to_return(
status: 200, body: FactoryBot.build(:permissions_response, :banned).to_json, headers: { content_type: 'application/json' },
)

expect {
RegistrationChecker.create_registration_allowed!(registration_request, competition_info, registration_request['submitted_by'])
Expand Down

0 comments on commit 4140bd2

Please sign in to comment.