From 0308dc121fa0034affc04b98a0a15476d8cef714 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Sat, 24 Nov 2018 19:17:23 +0100 Subject: [PATCH] Add TeamsController and corresponding specs --- app/controllers/teams_controller.rb | 31 +++++++++++ config/routes.rb | 1 + spec/controllers/teams_controller_spec.rb | 68 +++++++++++++++++++++++ spec/routing/teams_routing_spec.rb | 19 +++++++ 4 files changed, 119 insertions(+) create mode 100644 app/controllers/teams_controller.rb create mode 100644 spec/controllers/teams_controller_spec.rb create mode 100644 spec/routing/teams_routing_spec.rb diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb new file mode 100644 index 0000000..fde8269 --- /dev/null +++ b/app/controllers/teams_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index b10df31..b52f9d6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/controllers/teams_controller_spec.rb b/spec/controllers/teams_controller_spec.rb new file mode 100644 index 0000000..823fa3f --- /dev/null +++ b/spec/controllers/teams_controller_spec.rb @@ -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 diff --git a/spec/routing/teams_routing_spec.rb b/spec/routing/teams_routing_spec.rb new file mode 100644 index 0000000..b5f5a40 --- /dev/null +++ b/spec/routing/teams_routing_spec.rb @@ -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