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

Commit

Permalink
updated user allowed status changes tests in registration checker
Browse files Browse the repository at this point in the history
  • Loading branch information
dunkOnIT committed Sep 2, 2024
1 parent 05bdf24 commit 7351ed3
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 78 deletions.
49 changes: 24 additions & 25 deletions spec/models/registration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

require 'rails_helper'

ALLOWED_COMPETING_STATUS_UPDATES = [
{ initial_status: 'pending', new_status: 'accepted' },
{ initial_status: 'pending', new_status: 'cancelled' },
{ initial_status: 'pending', new_status: 'waiting_list' },
{ initial_status: 'pending', new_status: 'pending' },
{ initial_status: 'accepted', new_status: 'cancelled' },
{ initial_status: 'accepted', new_status: 'pending' },
{ initial_status: 'accepted', new_status: 'waiting_list' },
{ initial_status: 'accepted', new_status: 'accepted' },
{ initial_status: 'waiting_list', new_status: 'cancelled' },
{ initial_status: 'waiting_list', new_status: 'pending' },
{ initial_status: 'waiting_list', new_status: 'waiting_list' },
{ initial_status: 'waiting_list', new_status: 'accepted' },
{ initial_status: 'cancelled', new_status: 'cancelled' },
{ initial_status: 'cancelled', new_status: 'pending' },
{ initial_status: 'cancelled', new_status: 'waiting_list' },
{ initial_status: 'cancelled', new_status: 'accepted' },
].freeze

describe Registration do
describe 'validations#competing_status_consistency' do
it 'passes if competing_status and competing lane status match' do
Expand All @@ -18,42 +37,22 @@
end
end

describe '#update_competing_lane.competing_status', :tag do
SUCCESSFUL_COMBINATIONS = [
{ initial_status: 'pending', new_status: 'accepted' },
{ initial_status: 'pending', new_status: 'cancelled' },
{ initial_status: 'pending', new_status: 'waiting_list' },
{ initial_status: 'pending', new_status: 'pending' },
{ initial_status: 'accepted', new_status: 'cancelled' },
{ initial_status: 'accepted', new_status: 'pending' },
{ initial_status: 'accepted', new_status: 'waiting_list' },
{ initial_status: 'accepted', new_status: 'accepted' },
{ initial_status: 'waiting_list', new_status: 'cancelled' },
{ initial_status: 'waiting_list', new_status: 'pending' },
{ initial_status: 'waiting_list', new_status: 'waiting_list' },
{ initial_status: 'waiting_list', new_status: 'accepted' },
{ initial_status: 'cancelled', new_status: 'cancelled' },
{ initial_status: 'cancelled', new_status: 'pending' },
{ initial_status: 'cancelled', new_status: 'waiting_list' },
{ initial_status: 'cancelled', new_status: 'accepted' },
]

describe '#update_competing_lane.competing_status' do
it 'tests cover all possible status combinations' do
expect(REGISTRATION_TRANSITIONS).to match_array(SUCCESSFUL_COMBINATIONS)
end

RSpec.shared_examples 'competing_status updates' do |old_status, new_status|
it "given #{new_status}, #{old_status} updates as expected" do
registration = FactoryBot.create(:registration, registration_status: old_status)
RSpec.shared_examples 'competing_status updates' do |initial_status, new_status|
it "given #{new_status}, #{initial_status} updates as expected" do
registration = FactoryBot.create(:registration, registration_status: new_status)
registration.update_competing_lane!({ status: new_status })
expect(registration.competing_status).to eq(new_status)
end
end

SUCCESSFUL_COMBINATIONS.each do |params|
it_behaves_like 'competing_status updates', params[:old_status], params[:new_status]
it_behaves_like 'competing_status updates', params[:initial_status], params[:new_status]
end

end

describe '#update_competing_lane!.waiting_list' do
Expand Down
116 changes: 67 additions & 49 deletions spec/services/registration_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@
# TODO: Add a test where one comp has a lot of competitors and another doesnt but you can still accept, to ensure that we're checking the reg count
# for the COMPETITION, not all registrations

RSpec.shared_examples 'invalid user status updates' do |old_status, new_status|
it "user cant change 'status' => #{old_status} to: #{new_status}" do
registration = FactoryBot.create(:registration, registration_status: old_status)
competition_info = CompetitionInfo.new(FactoryBot.build(:competition))
update_request = FactoryBot.build(:update_request, user_id: registration[:user_id], competing: { 'status' => new_status })
stub_request(:get, UserApi.permissions_path(registration[:user_id])).to_return(status: 200, body: FactoryBot.build(:permissions_response).to_json, headers: { content_type: 'application/json' })

expect {
RegistrationChecker.update_registration_allowed!(update_request, competition_info, update_request['submitted_by'])
}.to raise_error(RegistrationError) do |error|
expect(error.http_status).to eq(:unauthorized)
expect(error.error).to eq(ErrorCodes::USER_INSUFFICIENT_PERMISSIONS)
end
end
end
USER_ALLOWED_STATUS_UPDATES = [
{ initial_status: 'pending', new_status: 'cancelled' },
{ initial_status: 'waiting_list', new_status: 'cancelled' },
{ initial_status: 'accepted', new_status: 'cancelled' },
{ initial_status: 'cancelled', new_status: 'pending' },
{ initial_status: 'cancelled', new_status: 'cancelled' },
].freeze

USER_FORBIDDEN_STATUS_UPDATES = [
{ initial_status: 'pending', new_status: 'accepted' },
{ initial_status: 'pending', new_status: 'waiting_list' },
{ initial_status: 'pending', new_status: 'pending' },
{ initial_status: 'waiting_list', new_status: 'pending' },
{ initial_status: 'waiting_list', new_status: 'waiting_list' },
{ initial_status: 'waiting_list', new_status: 'accepted' },
{ initial_status: 'accepted', new_status: 'pending' },
{ initial_status: 'accepted', new_status: 'waiting_list' },
{ initial_status: 'accepted', new_status: 'accepted' },
{ initial_status: 'cancelled', new_status: 'accepted' },
{ initial_status: 'cancelled', new_status: 'waiting_list' },
].freeze

RSpec.shared_examples 'valid organizer status updates' do |old_status, new_status|
it "organizer can change 'status' => #{old_status} to: #{new_status} before close" do
Expand Down Expand Up @@ -999,7 +1005,51 @@
end

describe '#update_registration_allowed!.validate_update_status!' do
it 'user cant submit an invalid status' do
describe 'user status combinations' do
it 'tests cover all possible user status update combinations' do
combined_updates = (USER_ALLOWED_STATUS_UPDATES << USER_FORBIDDEN_STATUS_UPDATES).flatten
expect(combined_updates).to match_array(REGISTRATION_TRANSITIONS)
end

RSpec.shared_examples 'forbidden user status updates' do |initial_status, new_status|
it "user cant change status '#{initial_status}' to: #{new_status}" do
override_registration = FactoryBot.create(:registration, registration_status: initial_status)
update_request = FactoryBot.build(:update_request, user_id: override_registration[:user_id], competing: { 'status' => new_status })
stub_request(:get, UserApi.permissions_path(override_registration[:user_id])).to_return(
status: 200,
body: FactoryBot.build(:permissions_response).to_json,
headers: { content_type: 'application/json' },
)

expect {
RegistrationChecker.update_registration_allowed!(update_request, @competition_info, update_request['submitted_by'])
}.to raise_error(RegistrationError) do |error|
expect(error.http_status).to eq(:unauthorized)
expect(error.error).to eq(ErrorCodes::USER_INSUFFICIENT_PERMISSIONS)
end
end
end

USER_FORBIDDEN_STATUS_UPDATES.each do |params|
it_behaves_like 'forbidden user status updates', params[:initial_status], params[:new_status]
end

RSpec.shared_examples 'allowed user status updates' do |initial_status, new_status|
it "user may change status '#{initial_status}' to: #{new_status}" do
override_registration = FactoryBot.create(:registration, user_id: 188000, registration_status: 'waiting_list')
update_request = FactoryBot.build(:update_request, user_id: override_registration[:user_id], competing: { 'status' => 'cancelled' })

expect { RegistrationChecker.update_registration_allowed!(update_request, @competition_info, update_request['submitted_by']) }
.not_to raise_error
end
end

USER_ALLOWED_STATUS_UPDATES.each do |params|
it_behaves_like 'allowed user status updates', params[:initial_status], params[:new_status]
end
end

it 'user cant submit a non-existent status' do
override_registration = FactoryBot.create(:registration, user_id: 188000, registration_status: 'waiting_list')
update_request = FactoryBot.build(:update_request, user_id: override_registration[:user_id], competing: { 'status' => 'random_status' })

Expand All @@ -1011,7 +1061,7 @@
end
end

it 'organizer cant submit an invalid status' do
it 'organizer cant submit a non-existent status' do
override_registration = FactoryBot.create(:registration, user_id: 188000, registration_status: 'waiting_list')
update_request = FactoryBot.build(:update_request, :organizer_as_user, user_id: override_registration[:user_id], competing: { 'status' => 'random_status' })

Expand Down Expand Up @@ -1047,14 +1097,6 @@
.not_to raise_error
end

it 'user can change state to cancelled' do
override_registration = FactoryBot.create(:registration, user_id: 188000, registration_status: 'waiting_list')
update_request = FactoryBot.build(:update_request, user_id: override_registration[:user_id], competing: { 'status' => 'cancelled' })

expect { RegistrationChecker.update_registration_allowed!(update_request, @competition_info, update_request['submitted_by']) }
.not_to raise_error
end

it 'user cant change events when cancelling' do
override_registration = FactoryBot.create(:registration, user_id: 188000, registration_status: 'waiting_list')
update_request = FactoryBot.build(
Expand All @@ -1069,30 +1111,6 @@
end
end

it 'user can change state from cancelled to pending' do
override_registration = FactoryBot.create(:registration, user_id: 188000, registration_status: 'cancelled')
update_request = FactoryBot.build(:update_request, user_id: override_registration[:user_id], competing: { 'status' => 'pending' })

expect { RegistrationChecker.update_registration_allowed!(update_request, @competition_info, update_request['submitted_by']) }
.not_to raise_error
end

[
{ old_status: 'pending', new_status: 'accepted' },
{ old_status: 'pending', new_status: 'waiting_list' },
{ old_status: 'pending', new_status: 'pending' },
{ old_status: 'waiting_list', new_status: 'pending' },
{ old_status: 'waiting_list', new_status: 'waiting_list' },
{ old_status: 'waiting_list', new_status: 'accepted' },
{ old_status: 'accepted', new_status: 'pending' },
{ old_status: 'accepted', new_status: 'waiting_list' },
{ old_status: 'accepted', new_status: 'accepted' },
{ old_status: 'cancelled', new_status: 'accepted' },
{ old_status: 'cancelled', new_status: 'waiting_list' },
].each do |params|
it_behaves_like 'invalid user status updates', params[:old_status], params[:new_status]
end

it 'user cant cancel accepted registration if competition requires organizers to cancel registration' do
override_registration = FactoryBot.create(:registration, user_id: 188000, registration_status: 'accepted')
override_competition_info = CompetitionInfo.new(FactoryBot.build(:competition, allow_registration_self_delete_after_acceptance: false))
Expand Down
5 changes: 1 addition & 4 deletions spec/support/registration_states_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# frozen_string_literal: true

REGISTRATION_TRANSITIONS = Registration::REGISTRATION_STATES.flat_map do |initial_status|
Registration::REGISTRATION_STATES.map do |new_status|
Registration::REGISTRATION_STATES.map do |new_status|
{ initial_status: initial_status, new_status: new_status }
end
end



0 comments on commit 7351ed3

Please sign in to comment.