From d63228965502377ca5004f46c9c84acf21b369ec Mon Sep 17 00:00:00 2001 From: Duncan Date: Sat, 24 Aug 2024 07:39:56 +0200 Subject: [PATCH 1/7] fixed qualifications with future dates --- app/services/registration_checker.rb | 3 ++- spec/factories/competition_factory.rb | 22 +++++++++++++++ spec/services/registration_checker_spec.rb | 31 +++++++++++++++++++++- spec/support/stub_helper.rb | 4 ++- 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/app/services/registration_checker.rb b/app/services/registration_checker.rb index 3338e27f..1c3bdc78 100644 --- a/app/services/registration_checker.rb +++ b/app/services/registration_checker.rb @@ -217,7 +217,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.zone.today ? Time.zone.today.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..a3e05e16 100644 --- a/spec/factories/competition_factory.rb +++ b/spec/factories/competition_factory.rb @@ -69,6 +69,28 @@ allow_registration_without_qualification { false } end + trait :has_future_qualifications do + tomorrow = (Time.zone.today+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 diff --git a/spec/services/registration_checker_spec.rb b/spec/services/registration_checker_spec.rb index b0166dea..930e5c73 100644 --- a/spec/services/registration_checker_spec.rb +++ b/spec/services/registration_checker_spec.rb @@ -285,6 +285,20 @@ 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| @@ -1401,7 +1415,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')) @@ -1414,6 +1428,21 @@ 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/stub_helper.rb b/spec/support/stub_helper.rb index c6f46bce..2c762663 100644 --- a/spec/support/stub_helper.rb +++ b/spec/support/stub_helper.rb @@ -16,7 +16,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.zone.today + { 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' } } From 68ddae3e4f3b8f1ee27a163b964a3e18c1140217 Mon Sep 17 00:00:00 2001 From: Duncan Date: Sat, 24 Aug 2024 07:55:49 +0200 Subject: [PATCH 2/7] rubocop --- spec/services/registration_checker_spec.rb | 1 - spec/support/stub_helper.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/services/registration_checker_spec.rb b/spec/services/registration_checker_spec.rb index 930e5c73..f1caf7a9 100644 --- a/spec/services/registration_checker_spec.rb +++ b/spec/services/registration_checker_spec.rb @@ -1442,7 +1442,6 @@ 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/stub_helper.rb b/spec/support/stub_helper.rb index 2c762663..10b19543 100644 --- a/spec/support/stub_helper.rb +++ b/spec/support/stub_helper.rb @@ -17,7 +17,7 @@ def stub_qualifications(payload = nil, qualification_data_date = nil) payload_date = qualification_data_date || date if Date.parse(date) > Time.zone.today - { status: 200, body: {"error":"You cannot request qualification data for a future date."}.to_json, headers: { 'Content-Type' => 'application/json' } } + { 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? From 6f9abc3a0b49fd32630cf3d000fd5f1dab1c8ee9 Mon Sep 17 00:00:00 2001 From: Duncan <52967253+dunkOnIT@users.noreply.github.com> Date: Tue, 1 Oct 2024 17:53:27 +0200 Subject: [PATCH 3/7] Update registration_checker.rb --- app/services/registration_checker.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/registration_checker.rb b/app/services/registration_checker.rb index 1c3bdc78..1afc0681 100644 --- a/app/services/registration_checker.rb +++ b/app/services/registration_checker.rb @@ -217,7 +217,7 @@ def existing_registration_in_series? end def competitor_qualifies_for_event?(event, qualification) - target_date = Date.parse(qualification['whenDate']) > Time.zone.today ? Time.zone.today.iso8601 : qualification['whenDate'] + target_date = Date.parse(qualification['whenDate']) > Time.utc.today ? Time.utc.today.iso8601 : qualification['whenDate'] competitor_qualification_results = UserApi.qualifications(@requestee_user_id, target_date) result_type = qualification['resultType'] From 191a8ab4d4a6d1fc73f3ebaf542fa3d78a89d2c4 Mon Sep 17 00:00:00 2001 From: Duncan <52967253+dunkOnIT@users.noreply.github.com> Date: Tue, 1 Oct 2024 17:53:54 +0200 Subject: [PATCH 4/7] Update competition_factory.rb --- spec/factories/competition_factory.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/factories/competition_factory.rb b/spec/factories/competition_factory.rb index a3e05e16..3f56e293 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.utc.today.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.utc.today-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 }, @@ -70,7 +70,7 @@ end trait :has_future_qualifications do - tomorrow = (Time.zone.today+1).iso8601 + tomorrow = (Time.utc.today+1).iso8601 transient do extra_qualifications { {} } @@ -92,7 +92,7 @@ end trait :has_hard_qualifications do - today = Time.zone.today.iso8601 + today = Time.utc.today.iso8601 transient do extra_qualifications { {} } @@ -100,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.utc.today-3).iso8601, 'level' => 10 }, + 'minx' => { 'type' => 'ranking', 'resultType' => 'average', 'whenDate' => (Time.utc.today-3).iso8601, 'level' => 20 }, + '222' => { 'type' => 'anyResult', 'resultType' => 'single', 'whenDate' => (Time.utc.today-3).iso8601, 'level' => 0 }, + '555bf' => { 'type' => 'anyResult', 'resultType' => 'average', 'whenDate' => (Time.utc.today-3).iso8601, 'level' => 0 }, } } end From 73884a98563571adf49474bf48147b8151e7ab94 Mon Sep 17 00:00:00 2001 From: Duncan <52967253+dunkOnIT@users.noreply.github.com> Date: Tue, 1 Oct 2024 17:54:42 +0200 Subject: [PATCH 5/7] Update stub_helper.rb --- spec/support/stub_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/support/stub_helper.rb b/spec/support/stub_helper.rb index 10b19543..22bfd278 100644 --- a/spec/support/stub_helper.rb +++ b/spec/support/stub_helper.rb @@ -16,7 +16,7 @@ def stub_qualifications(payload = nil, qualification_data_date = nil) date = params['date'] payload_date = qualification_data_date || date - if Date.parse(date) > Time.zone.today + if Date.parse(date) > Time.utc.today { 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' } } From fa4302d6e99802ae951dd5e1cf5ef839243e7fda Mon Sep 17 00:00:00 2001 From: Duncan Date: Tue, 1 Oct 2024 18:03:34 +0200 Subject: [PATCH 6/7] changed time determination --- spec/factories/competition_factory.rb | 16 ++++++++-------- spec/services/registration_checker_spec.rb | 8 ++++---- spec/support/qualification_results_faker.rb | 2 +- spec/support/stub_helper.rb | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/spec/factories/competition_factory.rb b/spec/factories/competition_factory.rb index 3f56e293..cd6424ce 100644 --- a/spec/factories/competition_factory.rb +++ b/spec/factories/competition_factory.rb @@ -48,7 +48,7 @@ end trait :has_qualifications do - today = Time.utc.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.utc.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 }, @@ -70,7 +70,7 @@ end trait :has_future_qualifications do - tomorrow = (Time.utc.today+1).iso8601 + tomorrow = (Time.now.utc+1).iso8601 transient do extra_qualifications { {} } @@ -92,7 +92,7 @@ end trait :has_hard_qualifications do - today = Time.utc.today.iso8601 + today = Time.now.utc.iso8601 transient do extra_qualifications { {} } @@ -100,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.utc.today-3).iso8601, 'level' => 10 }, - 'minx' => { 'type' => 'ranking', 'resultType' => 'average', 'whenDate' => (Time.utc.today-3).iso8601, 'level' => 20 }, - '222' => { 'type' => 'anyResult', 'resultType' => 'single', 'whenDate' => (Time.utc.today-3).iso8601, 'level' => 0 }, - '555bf' => { 'type' => 'anyResult', 'resultType' => 'average', 'whenDate' => (Time.utc.today-3).iso8601, 'level' => 0 }, + 'pyram' => { 'type' => 'ranking', 'resultType' => 'single', 'whenDate' => (Time.now.utc-3).iso8601, 'level' => 10 }, + 'minx' => { 'type' => 'ranking', 'resultType' => 'average', 'whenDate' => (Time.now.utc-3).iso8601, 'level' => 20 }, + '222' => { 'type' => 'anyResult', 'resultType' => 'single', 'whenDate' => (Time.now.utc-3).iso8601, 'level' => 0 }, + '555bf' => { 'type' => 'anyResult', 'resultType' => 'average', 'whenDate' => (Time.now.utc-3).iso8601, 'level' => 0 }, } } end diff --git a/spec/services/registration_checker_spec.rb b/spec/services/registration_checker_spec.rb index f1caf7a9..846db3f1 100644 --- a/spec/services/registration_checker_spec.rb +++ b/spec/services/registration_checker_spec.rb @@ -238,7 +238,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']) @@ -303,7 +303,7 @@ 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']) @@ -334,8 +334,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).iso8601 it_behaves_like 'fail: qualification enforced', 'no qualifying result for attemptResult-single', ['666'], { '666' => { 'type' => 'attemptResult', 'resultType' => 'single', 'whenDate' => today, 'level' => 10000 }, 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 22bfd278..bbea0d34 100644 --- a/spec/support/stub_helper.rb +++ b/spec/support/stub_helper.rb @@ -16,7 +16,7 @@ def stub_qualifications(payload = nil, qualification_data_date = nil) date = params['date'] payload_date = qualification_data_date || date - if Date.parse(date) > Time.utc.today + if Date.parse(date) > Time.now.utc.today { 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' } } From a1ac423e934e38e2a7e35562f192775a29941449 Mon Sep 17 00:00:00 2001 From: Duncan Date: Tue, 1 Oct 2024 18:43:27 +0200 Subject: [PATCH 7/7] fixed dates to use utc timezone --- app/services/registration_checker.rb | 2 +- spec/factories/competition_factory.rb | 8 ++++---- spec/services/registration_checker_spec.rb | 2 +- spec/support/stub_helper.rb | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/services/registration_checker.rb b/app/services/registration_checker.rb index 6cbb109b..586506c8 100644 --- a/app/services/registration_checker.rb +++ b/app/services/registration_checker.rb @@ -223,7 +223,7 @@ def existing_registration_in_series? end def competitor_qualifies_for_event?(event, qualification) - target_date = Date.parse(qualification['whenDate']) > Time.utc.today ? Time.utc.today.iso8601 : 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'] diff --git a/spec/factories/competition_factory.rb b/spec/factories/competition_factory.rb index cd6424ce..298cefa4 100644 --- a/spec/factories/competition_factory.rb +++ b/spec/factories/competition_factory.rb @@ -100,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.now.utc-3).iso8601, 'level' => 10 }, - 'minx' => { 'type' => 'ranking', 'resultType' => 'average', 'whenDate' => (Time.now.utc-3).iso8601, 'level' => 20 }, - '222' => { 'type' => 'anyResult', 'resultType' => 'single', 'whenDate' => (Time.now.utc-3).iso8601, 'level' => 0 }, - '555bf' => { 'type' => 'anyResult', 'resultType' => 'average', 'whenDate' => (Time.now.utc-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 06c5b91c..f1262875 100644 --- a/spec/services/registration_checker_spec.rb +++ b/spec/services/registration_checker_spec.rb @@ -351,7 +351,7 @@ context 'fail: qualification enforced' do today = Time.now.utc.iso8601 - last_year = (Time.now.utc - 365).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 }, diff --git a/spec/support/stub_helper.rb b/spec/support/stub_helper.rb index 539786bd..25d7481c 100644 --- a/spec/support/stub_helper.rb +++ b/spec/support/stub_helper.rb @@ -28,7 +28,7 @@ def stub_qualifications(payload = nil, qualification_data_date = nil) date = params['date'] payload_date = qualification_data_date || date - if Date.parse(date) > Time.now.utc.today + 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' } }