Skip to content

Commit

Permalink
added tests and validations
Browse files Browse the repository at this point in the history
  • Loading branch information
dunkOnIT committed Feb 4, 2025
1 parent d387a96 commit 94216ea
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
13 changes: 12 additions & 1 deletion app/models/competition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
57 changes: 48 additions & 9 deletions spec/models/competition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 94216ea

Please sign in to comment.