Prevent matches from being stopped without a winner in playoffs

This commit is contained in:
Daniel Schädler 2019-05-29 15:57:25 +02:00
parent 3cf2828dae
commit df0b2bccb5
2 changed files with 60 additions and 22 deletions

View File

@ -13,6 +13,11 @@ class MatchesController < ApplicationController
# PATCH/PUT /matches/1 # PATCH/PUT /matches/1
def update def update
new_state = match_params['state'] new_state = match_params['state']
if new_state == 'finished' && @match.current_leading_team.nil?
render json: { error: 'Stopping undecided Matches isn\'t allowed in playoff stage' },
status: :unprocessable_entity
return
end
if @match.update(match_params) if @match.update(match_params)
if new_state == 'finished' if new_state == 'finished'
result = PopulateMatchBelowAndSave.call(match: @match) unless @match.group_match? result = PopulateMatchBelowAndSave.call(match: @match) unless @match.group_match?

View File

@ -70,6 +70,7 @@ RSpec.describe MatchesController, type: :controller do
apply_authentication_headers_for @running_playoff_match.owner apply_authentication_headers_for @running_playoff_match.owner
end end
context 'on a decided match' do
before do before do
@running_playoff_match.match_scores.each_with_index do |ms, i| @running_playoff_match.match_scores.each_with_index do |ms, i|
ms.points = i ms.points = i
@ -100,6 +101,38 @@ RSpec.describe MatchesController, type: :controller do
end end
end end
end end
context 'on a undecided match' do
before do
@running_playoff_match.match_scores.each do |ms|
ms.points = 42
ms.save!
end
put :update, params: { id: @running_playoff_match.to_param }.merge(finished)
@running_playoff_match.reload
end
it 'returns unprocessable entity' do
expect(response).to have_http_status(:unprocessable_entity)
expect(@running_playoff_match.state).to eq('in_progress')
end
describe 'doesn\'t update the match below' do
before do
@match_below = @tournament.stages.find_by(level: @amount_of_stages - 1).matches
.find_by(position: @running_playoff_match.position / 2).reload
end
it 'teams' do
expect(@match_below.teams.empty?).to be(true)
end
it 'status' do
expect(@match_below.state).to eq('not_ready')
end
end
end
end
end end
context 'with invalid params' do context 'with invalid params' do