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

Commit

Permalink
waiting list controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dunkOnIT committed Aug 12, 2024
1 parent aec32f1 commit 56361c4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
14 changes: 13 additions & 1 deletion app/services/registration_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def self.update_registration_allowed!(update_request, competition_info, requesti
end

def self.bulk_update_allowed!(bulk_update_request, competition_info, requesting_user)
@competition_info = competition_info

raise BulkUpdateError.new(:unauthorized, [ErrorCodes::USER_INSUFFICIENT_PERMISSIONS]) unless
UserApi.can_administer?(requesting_user, competition_info.id)

Expand All @@ -45,7 +47,8 @@ def self.bulk_update_allowed!(bulk_update_request, competition_info, requesting_
rescue RegistrationError => e
errors[update_request['user_id']] = e.error
end
raise BulkUpdateError.new(:unprocessable_entity, errors) unless errors == {}

raise BulkUpdateError.new(:unprocessable_entity, errors) unless errors.empty? || sequential_waiting_list_updates?(bulk_update_request, errors)
end

class << self
Expand Down Expand Up @@ -202,5 +205,14 @@ def existing_registration_in_series?
end
false
end

def sequential_waiting_list_updates?(bulk_update_request, errors)
return false unless errors.values.uniq == [-4011]

accepted_users = bulk_update_request['requests'].map { |r| r['user_id'] if r['competing']['status'] == 'accepted' }
waiting_list_users = WaitingList.find(@competition_info.id).entries.take(accepted_users.count)
return false unless accepted_users == waiting_list_users
true
end
end
end
46 changes: 45 additions & 1 deletion spec/controllers/registration_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,51 @@
headers: { content_type: 'application/json' },
)
end
# TODO: Consider refactor into separate contexts with one expect() per it-block

describe 'waiting list bulk updates' do
before do
@registration = FactoryBot.create(:registration, :waiting_list)
@update = FactoryBot.build(:update_request, user_id: @registration[:user_id], competing: { 'status' => 'accepted' })
@registration2 = FactoryBot.create(:registration, :waiting_list)
@update2 = FactoryBot.build(:update_request, user_id: @registration2[:user_id], competing: { 'status' => 'accepted' })
@registration3 = FactoryBot.create(:registration, :waiting_list)
@update3 = FactoryBot.build(:update_request, user_id: @registration3[:user_id], competing: { 'status' => 'accepted' })

@waiting_list = FactoryBot.create(:waiting_list, entries: [@registration.user_id, @registration2.user_id, @registration3.user_id])

end

it 'accepts competitors from the waiting list in the order they appear' do
updates = [@update, @update2, @update3]
bulk_update_request = FactoryBot.build(:bulk_update_request, requests: updates)

request.headers['Authorization'] = bulk_update_request['jwt_token']
patch :bulk_update, params: bulk_update_request, as: :json

expect(response.code).to eq('200')

@updated_registration = Registration.find("#{@competition['id']}-#{@registration[:user_id]}")
expect(@updated_registration.competing_status).to eq('accepted')
@updated_registration2 = Registration.find("#{@competition['id']}-#{@registration2[:user_id]}")
expect(@updated_registration2.competing_status).to eq('accepted')
@updated_registration3 = Registration.find("#{@competition['id']}-#{@registration3[:user_id]}")
expect(@updated_registration3.competing_status).to eq('accepted')

expect(@waiting_list.reload.entries.empty?).to eq(true)
end

it 'cant accept competitors from waiting list without accepting leader', :tag do
updates = [@update2, @update3]
bulk_update_request = FactoryBot.build(:bulk_update_request, requests: updates)

request.headers['Authorization'] = bulk_update_request['jwt_token']
patch :bulk_update, params: bulk_update_request, as: :json

expect(response.code).to eq('422')
expect(JSON.parse(response.body)['error'].values).to eq([-4011, -4011])
end
end

it 'returns a 422 if there are validation errors' do
registration = FactoryBot.create(:registration)
update = FactoryBot.build(:update_request, user_id: registration[:user_id])
Expand Down

0 comments on commit 56361c4

Please sign in to comment.