Add TeamsController and corresponding specs
This commit is contained in:
parent
3744bf6858
commit
0308dc121f
|
|
@ -0,0 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class TeamsController < ApplicationController
|
||||
before_action :set_team, only: %i[show update]
|
||||
before_action :authenticate_user!, only: %i[update]
|
||||
before_action -> { require_owner! @team.owner }, only: %i[update]
|
||||
|
||||
# GET /teams/1
|
||||
def show
|
||||
render json: @team
|
||||
end
|
||||
|
||||
# PATCH/PUT /teams/1
|
||||
def update
|
||||
if @team.update(team_params)
|
||||
render json: @team
|
||||
else
|
||||
render json: @team.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_team
|
||||
@team = Team.find(params[:id])
|
||||
end
|
||||
|
||||
def team_params
|
||||
ActiveModelSerializers::Deserialization.jsonapi_parse(params, only: [:name])
|
||||
end
|
||||
end
|
||||
|
|
@ -4,4 +4,5 @@ Rails.application.routes.draw do
|
|||
mount_devise_token_auth_for 'User', at: 'users'
|
||||
|
||||
resources :matches, only: %i[show]
|
||||
resources :teams, only: %i[show update]
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,68 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe TeamsController, type: :controller do
|
||||
before do
|
||||
@team = create(:team)
|
||||
@owner = @team.owner
|
||||
end
|
||||
|
||||
describe 'GET #show' do
|
||||
it 'returns a success response' do
|
||||
get :show, params: { id: @team.to_param }
|
||||
expect(response).to be_successful
|
||||
end
|
||||
|
||||
it 'should return the correct team' do
|
||||
get :show, params: { id: @team.to_param }
|
||||
body = ActiveModelSerializers::Deserialization.jsonapi_parse(JSON.parse(response.body))
|
||||
expect(body[:name]).to eq(@team.name)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT #update' do
|
||||
let(:valid_update) do
|
||||
{
|
||||
data: {
|
||||
id: @team.id,
|
||||
type: 'teams',
|
||||
attributes: {
|
||||
name: Faker::Dog.name
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
context 'with valid params as owner' do
|
||||
before(:each) do
|
||||
apply_authentication_headers_for @owner
|
||||
end
|
||||
|
||||
it 'updates the requested team' do
|
||||
put :update, params: { id: @team.to_param }.merge(valid_update)
|
||||
@team.reload
|
||||
expect(response).to be_successful
|
||||
expect(@team.name).to eq(valid_update[:data][:attributes][:name])
|
||||
end
|
||||
|
||||
it 'renders a response with the updated team' do
|
||||
put :update, params: { id: @team.to_param }.merge(valid_update)
|
||||
expect(response).to be_successful
|
||||
body = ActiveModelSerializers::Deserialization.jsonapi_parse(JSON.parse(response.body))
|
||||
expect(body[:name]).to eq(valid_update[:data][:attributes][:name])
|
||||
end
|
||||
end
|
||||
|
||||
context 'with valid params 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: @team.to_param }.merge(valid_update)
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe TeamsController, type: :routing do
|
||||
describe 'routing' do
|
||||
it 'routes to #show' do
|
||||
expect(get: '/teams/1').to route_to('teams#show', id: '1')
|
||||
end
|
||||
|
||||
it 'routes to #update via PUT' do
|
||||
expect(put: '/teams/1').to route_to('teams#update', id: '1')
|
||||
end
|
||||
|
||||
it 'routes to #update via PATCH' do
|
||||
expect(patch: '/teams/1').to route_to('teams#update', id: '1')
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue