From 94216ea0872ea81d8886114309637f81744ddb39 Mon Sep 17 00:00:00 2001 From: Duncan Date: Tue, 4 Feb 2025 15:42:46 +0200 Subject: [PATCH] added tests and validations --- app/models/competition.rb | 13 +++++++- config/locales/en.yml | 3 ++ spec/models/competition_spec.rb | 57 +++++++++++++++++++++++++++------ 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/app/models/competition.rb b/app/models/competition.rb index 03997339a4..40884ec56d 100644 --- a/app/models/competition.rb +++ b/app/models/competition.rb @@ -318,6 +318,14 @@ def has_administrative_notes? end end + validate :auto_close_threshold_validations + private def auto_close_threshold_validations + errors.add(:auto_close_registrations_threshold, I18n.t('competitions.errors.auto_close_not_negative')) if auto_close_registrations_threshold < 0 + errors.add(:auto_close_registrations_threshold, I18n.t('competitions.errors.must_exceed_competitor_limit')) if + (competitor_limit.present? && auto_close_registrations_threshold <= competitor_limit) && auto_close_registrations_threshold != 0 + errors.add(:auto_close_registrations_threshold, I18n.t('competitions.errors.use_wca_registration')) unless use_wca_registration + end + # Only validate on update: nobody can confirm competition on creation. # The only exception to this is within tests, in which case we actually don't want to run this validation. validate :schedule_must_match_rounds, if: :confirmed_at_changed?, on: :update @@ -925,6 +933,8 @@ def receive_registration_emails=(r) update_column :external_website, nil end + after_save :auto_close, if: -> { saved_change_to_auto_close_registrations_threshold? } + def website generate_website ? internal_website : external_website end @@ -2885,6 +2895,7 @@ def self.form_data_json_schema end def auto_close - update!(closing_full_registration: true, registration_close: Time.now) + threshold_reached = registrations.with_payments.count >= auto_close_registrations_threshold && auto_close_registrations_threshold > 0 + update!(closing_full_registration: true, registration_close: Time.now) if threshold_reached end end diff --git a/config/locales/en.yml b/config/locales/en.yml index c3fbd31caa..ef3f400c40 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1934,6 +1934,9 @@ en: -6002: "You need to finish your registration before you can pay" #context: and when an error occured errors: + auto_close_not_negative: "Auto-close threshold must be a positive number" + must_exceed_competitor_limit: "Auto-close threshold must be greater than the competitor limit" + use_wca_registration: "Competition must use WCA registration" invalid_name_message: "must end with a year and must contain only alphanumeric characters, dashes(-), ampersands(&), periods(.), colons(:), apostrophes('), and spaces( )" cannot_manage: "Cannot manage competition." cannot_delete_public: "Cannot delete a competition that is publicly visible." diff --git a/spec/models/competition_spec.rb b/spec/models/competition_spec.rb index 1687a872ac..32e5eb3c45 100644 --- a/spec/models/competition_spec.rb +++ b/spec/models/competition_spec.rb @@ -1587,29 +1587,68 @@ def change_and_check_activities(new_start_date, new_end_date) context 'auto-close registrations' do let(:auto_close_comp) { FactoryBot.create(:competition, :registration_open, auto_close_registrations_threshold: 5)} + let(:comp) { FactoryBot.create(:competition, :registration_open, :with_competitor_limit, competitor_limit: 3) } - it 'closes registrations when the close threshold is reached', :tag do - FactoryBot.create_list(:registration, 5, :paid, competition: auto_close_comp) - expect(auto_close_comp.registration_past?).to eq(true) + it 'doesnt auto-close if threshold not reached' do + FactoryBot.create(:registration, :paid, competition: auto_close_comp) + expect(auto_close_comp.registration_past?).to eq(false) end - it 'doesnt auto-close if threshold not reached' do + it 'doesnt auto-close if threshold is 0' do + FactoryBot.create(:registration, :paid, competition: comp) + expect(comp.registration_past?).to eq(false) end - it 'only auto-closes if the registrations are paid registrations' do + it 'closes registrations when the close threshold is reached' do + FactoryBot.create_list(:registration, 5, :paid, competition: auto_close_comp) + expect(auto_close_comp.registration_past?).to eq(true) end it 'closes registrations when the close threshold is exceeded' do - comp = FactoryBot.create(:competition, :registration_open) - FactoryBot.create_list(:registration, 5, competition: comp) + FactoryBot.create_list(:registration, 5, :paid, competition: comp) comp.auto_close_registrations_threshold = 5 - FactoryBot.create(:registration, competition: comp) + FactoryBot.create(:registration, :paid, competition: comp) - expect(auto_close_comp.registration_past?).to eq(true) + expect(comp.registration_past?).to eq(true) end it 'if close threshold is added == number of registrations, registration will close' do + FactoryBot.create_list(:registration, 5, :paid, competition: comp) + expect(comp.registration_past?).to eq(false) + + comp.update!(auto_close_registrations_threshold: 5) + expect(comp.registration_past?).to eq(true) + end + + it 'only auto-closes if the registrations are paid registrations' do + FactoryBot.create_list(:registration, 5, competition: auto_close_comp) + expect(auto_close_comp.registration_past?).to eq(false) + end + + context 'validations' do + it 'auto-close threshold must be positive' do + comp.auto_close_registrations_threshold = -1 + expect(comp).not_to be_valid + expect(comp.errors[:auto_close_registrations_threshold]).to include("Auto-close threshold must be a positive number") + end + + it 'must be greater than competitor limit' do + comp.auto_close_registrations_threshold = comp.competitor_limit + expect(comp).not_to be_valid + expect(comp.errors[:auto_close_registrations_threshold]).to include("Auto-close threshold must be greater than the competitor limit") + + comp.auto_close_registrations_threshold = comp.competitor_limit - 1 + expect(comp).not_to be_valid + expect(comp.errors[:auto_close_registrations_threshold]).to include("Auto-close threshold must be greater than the competitor limit") + end + + it 'comp must use wca registration' do + comp.use_wca_registration = false + comp.auto_close_registrations_threshold = 1 + expect(comp).not_to be_valid + expect(comp.errors[:auto_close_registrations_threshold]).to include("Competition must use WCA registration") + end end end end