From 7351ed310a6ddef78d98515b94744d3acd6ad460 Mon Sep 17 00:00:00 2001 From: Duncan Date: Mon, 2 Sep 2024 09:32:12 +0200 Subject: [PATCH] updated user allowed status changes tests in registration checker --- spec/models/registration_spec.rb | 49 +++++---- spec/services/registration_checker_spec.rb | 116 ++++++++++++--------- spec/support/registration_states_helper.rb | 5 +- 3 files changed, 92 insertions(+), 78 deletions(-) diff --git a/spec/models/registration_spec.rb b/spec/models/registration_spec.rb index cf6786bc..5b3ea529 100644 --- a/spec/models/registration_spec.rb +++ b/spec/models/registration_spec.rb @@ -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 @@ -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 diff --git a/spec/services/registration_checker_spec.rb b/spec/services/registration_checker_spec.rb index b0166dea..216ad4da 100644 --- a/spec/services/registration_checker_spec.rb +++ b/spec/services/registration_checker_spec.rb @@ -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 @@ -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' }) @@ -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' }) @@ -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( @@ -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)) diff --git a/spec/support/registration_states_helper.rb b/spec/support/registration_states_helper.rb index 01ea14f9..929b922f 100644 --- a/spec/support/registration_states_helper.rb +++ b/spec/support/registration_states_helper.rb @@ -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 - - -