From c895e4e8f61884fe4974b8f290224c58fb96d536 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Wed, 29 May 2019 15:57:25 +0200 Subject: [PATCH] Prevent matches from being stopped without a winner in playoffs --- app/controllers/matches_controller.rb | 5 ++ spec/controllers/matches_controller_spec.rb | 77 +++++++++++++++------ 2 files changed, 60 insertions(+), 22 deletions(-) diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index d64c60d..b978852 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -13,6 +13,11 @@ class MatchesController < ApplicationController # PATCH/PUT /matches/1 def update 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 new_state == 'finished' result = PopulateMatchBelowAndSave.call(match: @match) unless @match.group_match? diff --git a/spec/controllers/matches_controller_spec.rb b/spec/controllers/matches_controller_spec.rb index 7606be1..48c73a2 100644 --- a/spec/controllers/matches_controller_spec.rb +++ b/spec/controllers/matches_controller_spec.rb @@ -70,33 +70,66 @@ RSpec.describe MatchesController, type: :controller do apply_authentication_headers_for @running_playoff_match.owner end - before do - @running_playoff_match.match_scores.each_with_index do |ms, i| - ms.points = i - ms.save! - end - put :update, params: { id: @running_playoff_match.to_param }.merge(finished) - @running_playoff_match.reload - end - - it 'updates the matches status' do - expect(response).to be_successful - expect(@running_playoff_match.state).to eq(finished[:state]) - end - - describe 'updates the match below' do + context 'on a decided match' do before do - @match_below = @tournament.stages.find_by(level: @amount_of_stages - 1).matches - .find_by(position: @running_playoff_match.position / 2).reload + @running_playoff_match.match_scores.each_with_index do |ms, i| + ms.points = i + ms.save! + end + put :update, params: { id: @running_playoff_match.to_param }.merge(finished) + @running_playoff_match.reload end - it 'with the right teams' do - expect(@running_playoff_match.winner).to be_a(Team) - expect(@match_below.teams).to include(@running_playoff_match.winner) + it 'updates the matches status' do + expect(response).to be_successful + expect(@running_playoff_match.state).to eq(finished[:state]) end - it 'with the right status' do - expect(@match_below.state).to eq('not_ready') + describe 'updates 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 'with the right teams' do + expect(@running_playoff_match.winner).to be_a(Team) + expect(@match_below.teams).to include(@running_playoff_match.winner) + end + + it 'with the right status' do + expect(@match_below.state).to eq('not_ready') + 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