Add methods and specs to update a match
This commit is contained in:
parent
436adde706
commit
3876f57d32
|
|
@ -2,15 +2,43 @@
|
||||||
|
|
||||||
class MatchesController < ApplicationController
|
class MatchesController < ApplicationController
|
||||||
before_action :set_match, only: %i[show update]
|
before_action :set_match, only: %i[show update]
|
||||||
|
before_action :validate_params, only: %i[update]
|
||||||
|
before_action -> { require_owner! @match.owner }, only: %i[update]
|
||||||
|
|
||||||
# GET /matches/1
|
# GET /matches/1
|
||||||
def show
|
def show
|
||||||
render json: @match, include: ['match_scores.points', 'match_scores.team']
|
render json: @match, include: ['match_scores.points', 'match_scores.team']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# PATCH/PUT /matches/1
|
||||||
|
def update
|
||||||
|
if @match.update(match_params)
|
||||||
|
render json: @match
|
||||||
|
else
|
||||||
|
render json: @match.errors, status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def validate_params
|
||||||
|
case match_params['state']
|
||||||
|
when 'in_progress'
|
||||||
|
render json: { error: 'Match can\'t start in this state' }, status: :unprocessable_entity \
|
||||||
|
unless @match.not_started?
|
||||||
|
when 'finished'
|
||||||
|
render json: { error: 'Match can\'t finish in this state' }, status: :unprocessable_entity \
|
||||||
|
unless @match.in_progress?
|
||||||
|
else
|
||||||
|
render json: { error: 'Invalid target state' }, status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def set_match
|
def set_match
|
||||||
@match = Match.find(params[:id])
|
@match = Match.find(params[:id])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def match_params
|
||||||
|
params.slice(:state).permit!
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ Rails.application.routes.draw do
|
||||||
sessions: 'overrides/sessions'
|
sessions: 'overrides/sessions'
|
||||||
}
|
}
|
||||||
|
|
||||||
resources :matches, only: %i[show]
|
resources :matches, only: %i[show update]
|
||||||
resources :teams, only: %i[show update]
|
resources :teams, only: %i[show update]
|
||||||
resources :tournaments
|
resources :tournaments
|
||||||
resources :match_scores, only: %i[show update]
|
resources :match_scores, only: %i[show update]
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe MatchesController, type: :controller do
|
RSpec.describe MatchesController, type: :controller do
|
||||||
before do
|
before do
|
||||||
@match = create(:match)
|
@match = create(:match, state: :not_started)
|
||||||
@match.match_scores = create_pair(:match_score)
|
@match.match_scores = create_pair(:match_score)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -22,4 +22,60 @@ RSpec.describe MatchesController, type: :controller do
|
||||||
expect(body[:match_scores].map { |ms| ms[:id] }).to eq(@match.match_scores.map(&:id))
|
expect(body[:match_scores].map { |ms| ms[:id] }).to eq(@match.match_scores.map(&:id))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'POST #update' do
|
||||||
|
let(:valid_update) do
|
||||||
|
{
|
||||||
|
state: 'in_progress'
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:invalid_update) do
|
||||||
|
{
|
||||||
|
state: 'team1_won'
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'as owner' do
|
||||||
|
before(:each) do
|
||||||
|
apply_authentication_headers_for @match.owner
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with valid params' do
|
||||||
|
it 'updates the match' do
|
||||||
|
put :update, params: { id: @match.to_param }.merge(valid_update)
|
||||||
|
@match.reload
|
||||||
|
expect(response).to be_successful
|
||||||
|
expect(@match.state).to eq(valid_update[:state])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'renders a response with the updated match' do
|
||||||
|
put :update, params: { id: @match.to_param }.merge(valid_update)
|
||||||
|
expect(response).to be_successful
|
||||||
|
body = deserialize_response response
|
||||||
|
expect(body[:state]).to eq(valid_update[:state])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with invalid params' do
|
||||||
|
it 'renders an unprocessable entity response' do
|
||||||
|
put :update, params: { id: @match.to_param }.merge(invalid_update)
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'as another user' do
|
||||||
|
context 'with valid params' do
|
||||||
|
before(:each) do
|
||||||
|
apply_authentication_headers_for create(:user)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'renders a forbidden error response' do
|
||||||
|
put :update, params: { id: @match.to_param }.merge(valid_update)
|
||||||
|
expect(response).to have_http_status(:forbidden)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue