Merge pull request #9 from turniere/ticket/TURNIERE-97
Implement ScoreController
This commit is contained in:
commit
6355655d54
|
|
@ -0,0 +1,33 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class MatchScoresController < ApplicationController
|
||||
before_action :set_match_score, only: %i[show update]
|
||||
before_action :authenticate_user!, only: %i[update]
|
||||
before_action -> { require_owner! @match_score.owner }, only: %i[update]
|
||||
|
||||
# GET /scores/1
|
||||
def show
|
||||
render json: @match_score
|
||||
end
|
||||
|
||||
# PATCH/PUT /scores/1
|
||||
def update
|
||||
if @match_score.update(match_score_params)
|
||||
render json: @match_score
|
||||
else
|
||||
render json: @match_score.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_match_score
|
||||
@match_score = MatchScore.find(params[:id])
|
||||
end
|
||||
|
||||
# Only allow a trusted parameter "white list" through.
|
||||
def match_score_params
|
||||
deserialize_params only: %i[points]
|
||||
end
|
||||
end
|
||||
|
|
@ -3,4 +3,6 @@
|
|||
class MatchScore < ApplicationRecord
|
||||
belongs_to :match
|
||||
belongs_to :team
|
||||
|
||||
delegate :owner, to: :team
|
||||
end
|
||||
|
|
|
|||
|
|
@ -4,4 +4,5 @@ class MatchScoreSerializer < ApplicationSerializer
|
|||
attributes :points
|
||||
|
||||
has_one :team
|
||||
has_one :match
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,4 +6,5 @@ Rails.application.routes.draw do
|
|||
resources :matches, only: %i[show]
|
||||
resources :teams, only: %i[show update]
|
||||
resources :tournaments
|
||||
resources :match_scores, only: %i[show update]
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,71 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe MatchScoresController, type: :controller do
|
||||
before do
|
||||
@match_score = create(:match_score)
|
||||
@owner = @match_score.owner
|
||||
end
|
||||
|
||||
describe 'GET #show' do
|
||||
it 'returns a success response' do
|
||||
get :show, params: { id: @match_score.to_param }
|
||||
expect(response).to be_successful
|
||||
end
|
||||
|
||||
it 'should return the correct score' do
|
||||
get :show, params: { id: @match_score.to_param }
|
||||
body = deserialize_response response
|
||||
expect(body[:points]).to eq(@match_score.points)
|
||||
expect(body[:team_id]).to eq(@match_score.team.id.to_s)
|
||||
expect(body[:match_id]).to eq(@match_score.match.id.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT #update' do
|
||||
let(:valid_update) do
|
||||
{
|
||||
data: {
|
||||
id: @match_score.id,
|
||||
type: 'match_scores',
|
||||
attributes: {
|
||||
points: 42
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
context 'with valid params' do
|
||||
context 'as owner' do
|
||||
before(:each) do
|
||||
apply_authentication_headers_for @owner
|
||||
end
|
||||
|
||||
it 'updates the requested score' do
|
||||
put :update, params: { id: @match_score.to_param }.merge(valid_update)
|
||||
@match_score.reload
|
||||
expect(@match_score.points).to eq(valid_update[:data][:attributes][:points])
|
||||
end
|
||||
|
||||
it 'renders a response with the updated team' do
|
||||
put :update, params: { id: @match_score.to_param }.merge(valid_update)
|
||||
expect(response).to be_successful
|
||||
body = deserialize_response response
|
||||
expect(body[:points]).to eq(valid_update[:data][:attributes][:points])
|
||||
end
|
||||
end
|
||||
|
||||
context 'as another user' do
|
||||
before(:each) do
|
||||
apply_authentication_headers_for create(:user)
|
||||
end
|
||||
|
||||
it 'renders a forbidden error response' do
|
||||
put :update, params: { id: @match_score.to_param }.merge(valid_update)
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe MatchScoresController, type: :routing do
|
||||
describe 'routing' do
|
||||
it 'routes to #show' do
|
||||
expect(get: '/match_scores/1').to route_to('match_scores#show', id: '1')
|
||||
end
|
||||
|
||||
it 'routes to #update via PUT' do
|
||||
expect(put: '/match_scores/1').to route_to('match_scores#update', id: '1')
|
||||
end
|
||||
|
||||
it 'routes to #update via PATCH' do
|
||||
expect(patch: '/match_scores/1').to route_to('match_scores#update', id: '1')
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue