Only allow positive powers of two for playoff_teams_amount

This commit is contained in:
Daniel Schädler 2019-06-09 17:23:06 +02:00
parent dffe708933
commit 37cddd839f
3 changed files with 43 additions and 4 deletions

View File

@ -10,6 +10,8 @@ class Tournament < ApplicationRecord
validates :name, presence: true
validates :code, presence: true, uniqueness: true
validate :playoff_teams_amount_is_positive_power_of_two
alias_attribute :owner, :user
after_initialize :generate_code
@ -24,4 +26,11 @@ class Tournament < ApplicationRecord
break if errors['code'].blank?
end
end
def playoff_teams_amount_is_positive_power_of_two
return if (Utils.po2?(playoff_teams_amount) && playoff_teams_amount.positive?) || playoff_teams_amount.zero?
errors.add(:playoff_teams_amount_isnt_positive_power_of_two,
'playoff_teams_amount needs to be a positive power of two')
end
end

View File

@ -51,7 +51,7 @@ class CreateSchema < ActiveRecord::Migration[5.2]
t.string :code, null: false, index: { unique: true }
t.string :description
t.boolean :public, default: true
t.integer :playoff_teams_amount
t.integer :playoff_teams_amount, default: 0
t.integer :instant_finalists_amount, default: 0
t.integer :intermediate_round_participants_amount, default: 0

View File

@ -194,6 +194,29 @@ RSpec.describe TournamentsController, type: :controller do
expect(@group_stage_tournament.playoff_teams_amount)
.to eq(create_group_tournament_data[:playoff_teams_amount])
end
context 'playoff_teams_amount unacceptable' do
it 'is not a power of two' do
post :create, params: create_group_tournament_data.merge(playoff_teams_amount: 18)
expect(response).to have_http_status(:unprocessable_entity)
expect(deserialize_response(response).values.first.first)
.to eq('playoff_teams_amount needs to be a positive power of two')
end
it 'isn\'t positive' do
post :create, params: create_group_tournament_data.merge(playoff_teams_amount: -16)
expect(response).to have_http_status(:unprocessable_entity)
expect(deserialize_response(response).values.first.first)
.to eq('playoff_teams_amount needs to be a positive power of two')
end
it 'isn\'t positive nor a power of two' do
post :create, params: create_group_tournament_data.merge(playoff_teams_amount: -42)
expect(response).to have_http_status(:unprocessable_entity)
expect(deserialize_response(response).values.first.first)
.to eq('playoff_teams_amount needs to be a positive power of two')
end
end
end
it 'renders a JSON response with the new tournament' do
@ -234,10 +257,17 @@ RSpec.describe TournamentsController, type: :controller do
end
context 'with invalid parameters' do
it 'renders an unprocessable entity response' do
put :create, params: { teams: [1, 2, 3] }
expect(response).to have_http_status(:unprocessable_entity)
shared_examples_for 'renders unprocessable entity response' do
it 'renders an unprocessable entity response' do
expect(response).to have_http_status(:unprocessable_entity)
end
end
before do
put :create, params: context
end
context ''
end
context 'with empty team objects' do