From e5fdacc03e29420ab18ec409b5c9bd36ba0c9e6d Mon Sep 17 00:00:00 2001 From: f Date: Sat, 29 Feb 2020 18:30:28 -0300 Subject: [PATCH] responder con un error cuando la barca no esta abordada closes #13 --- app/controllers/barcas_controller.rb | 14 +++++------ config/locales/en.yml | 1 + config/locales/es.yml | 1 + test/controllers/barcas_controller_test.rb | 27 ++++++++++++---------- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/app/controllers/barcas_controller.rb b/app/controllers/barcas_controller.rb index 95b60a4..50392fc 100644 --- a/app/controllers/barcas_controller.rb +++ b/app/controllers/barcas_controller.rb @@ -3,6 +3,7 @@ # API de Barcas class BarcasController < ApplicationController before_action :authenticate! + before_action :find_barca, only: %i[abordar abandonar] # GET /barcas # @@ -98,8 +99,6 @@ class BarcasController < ApplicationController # # La pirata actual aborda la barca def abordar - find_barca - @barca.piratas << current_pirata if @barca.save @@ -115,15 +114,16 @@ class BarcasController < ApplicationController # # La pirata actual abandona la barca! def abandonar - find_barca - - if @barca.tripulaciones.find_by(pirata: current_pirata).try :destroy + if @barca.tripulaciones.find_by!(pirata: current_pirata).destroy notify(subject: :abandonar, urgency: :'very-low', ttl: 1.day) render status: :no_content else - render json: { errors: @barca.errors.messages }, - status: :unprocessable_entity + render status: :unprocessable_entity, + json: { errors: @barca.errors.messages } end + rescue ActiveRecord::RecordNotFound + render status: :unprocessable_entity, + json: { errors: [I18n.t('barcas.abandonar.no_tripulada')] } end private diff --git a/config/locales/en.yml b/config/locales/en.yml index ca962ef..5a62954 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -23,6 +23,7 @@ en: abandonar: subject: '%{barca}' message: '%{nick} left the ship!' + no_tripulada: "You're not on this ship" destroy: subject: '%{barca}' message: '%{nick} sunk the ship!' diff --git a/config/locales/es.yml b/config/locales/es.yml index 196de51..56ea818 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -23,6 +23,7 @@ es: abandonar: subject: '%{barca}' message: '¡%{nick} abandonó la barca!' + no_tripulada: 'No estás en esa barca' destroy: subject: '%{barca}' message: '¡%{nick} hundió la barca!' diff --git a/test/controllers/barcas_controller_test.rb b/test/controllers/barcas_controller_test.rb index d02ef2d..3d9d7ae 100644 --- a/test/controllers/barcas_controller_test.rb +++ b/test/controllers/barcas_controller_test.rb @@ -16,9 +16,9 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest get barcas_todas_url, as: :json, headers: @auth - body = JSON.parse(@response.body) + body = JSON.parse(response.body) - assert_equal 200, @response.status + assert_equal 200, response.status assert_equal 3, body['barcas'].size end @@ -31,9 +31,9 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest get barcas_url, as: :json, headers: @auth - body = JSON.parse(@response.body) + body = JSON.parse(response.body) - assert_equal 200, @response.status + assert_equal 200, response.status assert_equal 2, body['barcas'].size end @@ -42,9 +42,9 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest post barcas_url, as: :json, headers: @auth, params: { barca: { nombre: nombre, descripcion: nombre } } - body = JSON.parse(@response.body) + body = JSON.parse(response.body) - assert_equal 201, @response.status + assert_equal 201, response.status assert_equal nombre, body['nombre'] assert_equal nombre, body['descripcion'] end @@ -61,7 +61,7 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest } } - assert_equal 204, @response.status + assert_equal 204, response.status end test 'se pueden eliminar' do @@ -69,7 +69,7 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest delete barca_url(barca), as: :json, headers: @auth - assert_equal 204, @response.status + assert_equal 204, response.status assert_raise ActiveRecord::RecordNotFound do barca.reload end @@ -81,7 +81,7 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest delete barca_url(barca), as: :json, headers: @auth - assert_equal 422, @response.status + assert_equal 422, response.status assert barca.reload end @@ -90,7 +90,7 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest delete barca_abandonar_url(barca), as: :json, headers: @auth - assert_equal 204, @response.status + assert_equal 204, response.status assert_not barca.reload.piratas.include?(@pirata) end @@ -99,7 +99,10 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest delete barca_abandonar_url(barca), as: :json, headers: @auth + body = JSON.parse(response.body) + assert_response :unprocessable_entity + assert body['errors'].include?(I18n.t('barcas.abandonar.no_tripulada')) end test 'se pueden abordar' do @@ -107,7 +110,7 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest put barca_abordar_url(barca), as: :json, headers: @auth - assert_equal 204, @response.status + assert_equal 204, response.status assert barca.reload.piratas.include?(@pirata) end @@ -117,7 +120,7 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest get barcas_todas_url, as: :json, headers: @auth - body = JSON.parse @response.body + body = JSON.parse response.body assert body['barcas'].first['abordada'] assert_equal 1, body['barcas'].first['tripulacion'] assert_not body['barcas'].last['abordada']