From 8b3f5d9a45e9e18b56f760f29eed58cfed63d9e3 Mon Sep 17 00:00:00 2001 From: Duncan <52967253+dunkOnIT@users.noreply.github.com> Date: Tue, 1 Oct 2024 18:53:44 +0200 Subject: [PATCH] Request qualification data at the current date, not the future qualification date (#653) * fixed qualifications with future dates * rubocop * Update registration_checker.rb * Update competition_factory.rb * Update stub_helper.rb * changed time determination * fixed dates to use utc timezone --- app/services/registration_checker.rb | 3 +- spec/factories/competition_factory.rb | 36 +++++++++++++++---- spec/services/registration_checker_spec.rb | 38 ++++++++++++++++++--- spec/support/qualification_results_faker.rb | 2 +- spec/support/stub_helper.rb | 4 ++- 5 files changed, 68 insertions(+), 15 deletions(-) diff --git a/app/services/registration_checker.rb b/app/services/registration_checker.rb index ed06ce90..586506c8 100644 --- a/app/services/registration_checker.rb +++ b/app/services/registration_checker.rb @@ -223,7 +223,8 @@ def existing_registration_in_series? end def competitor_qualifies_for_event?(event, qualification) - competitor_qualification_results = UserApi.qualifications(@requestee_user_id, qualification['whenDate']) + target_date = Date.parse(qualification['whenDate']) > Time.now.utc ? Time.now.utc.iso8601 : qualification['whenDate'] + competitor_qualification_results = UserApi.qualifications(@requestee_user_id, target_date) result_type = qualification['resultType'] competitor_pr = competitor_qualification_results.find { |result| result['eventId'] == event && result['type'] == result_type } diff --git a/spec/factories/competition_factory.rb b/spec/factories/competition_factory.rb index 031d07f9..298cefa4 100644 --- a/spec/factories/competition_factory.rb +++ b/spec/factories/competition_factory.rb @@ -48,7 +48,7 @@ end trait :has_qualifications do - today = Time.zone.today.iso8601 + today = Time.now.utc.iso8601 transient do extra_qualifications { {} } @@ -56,7 +56,7 @@ { '333' => { 'type' => 'attemptResult', 'resultType' => 'single', 'whenDate' => today, 'level' => 1000 }, '555' => { 'type' => 'attemptResult', 'resultType' => 'average', 'whenDate' => today, 'level' => 6000 }, - 'pyram' => { 'type' => 'ranking', 'resultType' => 'single', 'whenDate' => (Time.zone.today-2).iso8601, 'level' => 100 }, + 'pyram' => { 'type' => 'ranking', 'resultType' => 'single', 'whenDate' => (Time.now.utc-2).iso8601, 'level' => 100 }, 'minx' => { 'type' => 'ranking', 'resultType' => 'average', 'whenDate' => today, 'level' => 200 }, '222' => { 'type' => 'anyResult', 'resultType' => 'single', 'whenDate' => today, 'level' => 0 }, '555bf' => { 'type' => 'anyResult', 'resultType' => 'average', 'whenDate' => today, 'level' => 0 }, @@ -69,8 +69,30 @@ allow_registration_without_qualification { false } end + trait :has_future_qualifications do + tomorrow = (Time.now.utc+1).iso8601 + + transient do + extra_qualifications { {} } + standard_qualifications { + { + '333' => { 'type' => 'attemptResult', 'resultType' => 'single', 'whenDate' => tomorrow, 'level' => 1000 }, + '555' => { 'type' => 'attemptResult', 'resultType' => 'average', 'whenDate' => tomorrow, 'level' => 6000 }, + 'pyram' => { 'type' => 'ranking', 'resultType' => 'single', 'whenDate' => tomorrow, 'level' => 100 }, + 'minx' => { 'type' => 'ranking', 'resultType' => 'average', 'whenDate' => tomorrow, 'level' => 200 }, + '222' => { 'type' => 'anyResult', 'resultType' => 'single', 'whenDate' => tomorrow, 'level' => 0 }, + '555bf' => { 'type' => 'anyResult', 'resultType' => 'average', 'whenDate' => tomorrow, 'level' => 0 }, + } + } + end + + qualifications { standard_qualifications.merge(extra_qualifications) } + qualification_results { true } + allow_registration_without_qualification { false } + end + trait :has_hard_qualifications do - today = Time.zone.today.iso8601 + today = Time.now.utc.iso8601 transient do extra_qualifications { {} } @@ -78,10 +100,10 @@ { '333' => { 'type' => 'attemptResult', 'resultType' => 'single', 'whenDate' => today, 'level' => 10 }, '555' => { 'type' => 'attemptResult', 'resultType' => 'average', 'whenDate' => today, 'level' => 60 }, - 'pyram' => { 'type' => 'ranking', 'resultType' => 'single', 'whenDate' => (Time.zone.today-3).iso8601, 'level' => 10 }, - 'minx' => { 'type' => 'ranking', 'resultType' => 'average', 'whenDate' => (Time.zone.today-3).iso8601, 'level' => 20 }, - '222' => { 'type' => 'anyResult', 'resultType' => 'single', 'whenDate' => (Time.zone.today-3).iso8601, 'level' => 0 }, - '555bf' => { 'type' => 'anyResult', 'resultType' => 'average', 'whenDate' => (Time.zone.today-3).iso8601, 'level' => 0 }, + 'pyram' => { 'type' => 'ranking', 'resultType' => 'single', 'whenDate' => (Time.now.utc-3.days).iso8601, 'level' => 10 }, + 'minx' => { 'type' => 'ranking', 'resultType' => 'average', 'whenDate' => (Time.now.utc-3.days).iso8601, 'level' => 20 }, + '222' => { 'type' => 'anyResult', 'resultType' => 'single', 'whenDate' => (Time.now.utc-3.days).iso8601, 'level' => 0 }, + '555bf' => { 'type' => 'anyResult', 'resultType' => 'average', 'whenDate' => (Time.now.utc-3.days).iso8601, 'level' => 0 }, } } end diff --git a/spec/services/registration_checker_spec.rb b/spec/services/registration_checker_spec.rb index fa8ad68d..f1262875 100644 --- a/spec/services/registration_checker_spec.rb +++ b/spec/services/registration_checker_spec.rb @@ -254,7 +254,7 @@ end it 'smoketest - all qualifications unmet' do - stub_qualifications(nil, (Time.zone.today-1).iso8601) + stub_qualifications(nil, (Time.now.utc-1).iso8601) competition = FactoryBot.build(:competition, :has_hard_qualifications) stub_json(CompetitionApi.url("#{competition['id']}/qualifications"), 200, competition['qualifications']) @@ -301,11 +301,25 @@ RegistrationChecker.create_registration_allowed!(registration_request, competition_info, registration_request['submitted_by']) }.not_to raise_error end + + it "succeeds given future qualification and #{description}" do + stub_qualifications + + competition = FactoryBot.build(:competition, :has_future_qualifications) + stub_json(CompetitionApi.url("#{competition['id']}/qualifications"), 200, competition['qualifications']) + competition_info = CompetitionInfo.new(competition.except('qualifications')) + + registration_request = FactoryBot.build(:registration_request, events: event_ids) + + expect { + RegistrationChecker.create_registration_allowed!(registration_request, competition_info, registration_request['submitted_by']) + }.not_to raise_error + end end RSpec.shared_examples 'fail: qualification enforced' do |description, event_ids, extra_qualifications| it "fails given #{description}" do - stub_qualifications(nil, (Time.zone.today-1).iso8601) + stub_qualifications(nil, (Time.now.utc-1).iso8601) competition = FactoryBot.build(:competition, :has_qualifications, extra_qualifications: extra_qualifications) stub_json(CompetitionApi.url("#{competition['id']}/qualifications"), 200, competition['qualifications']) @@ -336,8 +350,8 @@ end context 'fail: qualification enforced' do - today = Time.zone.today.iso8601 - last_year = (Time.zone.today - 365).iso8601 + today = Time.now.utc.iso8601 + last_year = (Time.now.utc - 365.days).iso8601 it_behaves_like 'fail: qualification enforced', 'no qualifying result for attemptResult-single', ['666'], { '666' => { 'type' => 'attemptResult', 'resultType' => 'single', 'whenDate' => today, 'level' => 10000 }, @@ -1463,7 +1477,7 @@ end RSpec.shared_examples 'update succeed: qualification enforced' do |description, event_ids| - it "succeeds given given #{description}" do + it "succeeds given #{description}" do competition = FactoryBot.build(:competition, :has_qualifications) stub_json(CompetitionApi.url("#{competition['id']}/qualifications"), 200, competition['qualifications']) competition_info = CompetitionInfo.new(competition.except('qualifications')) @@ -1476,6 +1490,20 @@ RegistrationChecker.update_registration_allowed!(update_request, competition_info, update_request['submitted_by']) }.not_to raise_error end + + it "succeeds given future qualification and #{description}" do + competition = FactoryBot.build(:competition, :has_future_qualifications) + stub_json(CompetitionApi.url("#{competition['id']}/qualifications"), 200, competition['qualifications']) + competition_info = CompetitionInfo.new(competition.except('qualifications')) + + update_request = FactoryBot.build(:update_request, competing: { 'event_ids' => event_ids }) + + FactoryBot.create(:registration, user_id: update_request['user_id']) + + expect { + RegistrationChecker.update_registration_allowed!(update_request, competition_info, update_request['submitted_by']) + }.not_to raise_error + end end RSpec.shared_examples 'update fail: qualification enforced' do |description, event_ids, extra_qualifications| diff --git a/spec/support/qualification_results_faker.rb b/spec/support/qualification_results_faker.rb index 5ba83900..a1202fd9 100644 --- a/spec/support/qualification_results_faker.rb +++ b/spec/support/qualification_results_faker.rb @@ -4,7 +4,7 @@ class QualificationResultsFaker attr_accessor :qualification_results def initialize( - date = (Time.zone.today-1).iso8601, + date = (Time.now.utc-1).iso8601, results_inputs = [ ['222', 'single', '200'], ['333', 'single', '900'], diff --git a/spec/support/stub_helper.rb b/spec/support/stub_helper.rb index 79e8eace..25d7481c 100644 --- a/spec/support/stub_helper.rb +++ b/spec/support/stub_helper.rb @@ -28,7 +28,9 @@ def stub_qualifications(payload = nil, qualification_data_date = nil) date = params['date'] payload_date = qualification_data_date || date - if !payload.nil? # Present doesnt work because [].present? == false + if Date.parse(date) > Time.now.utc + { status: 200, body: { error: 'You cannot request qualification data for a future date.' }.to_json, headers: { 'Content-Type' => 'application/json' } } + elsif !payload.nil? # Present doesnt work because [].present? == false { status: 200, body: payload.to_json, headers: { 'Content-Type' => 'application/json' } } elsif payload_date.present? { status: 200, body: QualificationResultsFaker.new(payload_date).qualification_results.to_json, headers: { 'Content-Type' => 'application/json' } }