responder con un error cuando la barca no esta abordada closes #13

This commit is contained in:
f 2020-02-29 18:30:28 -03:00
parent 9018c30e90
commit e5fdacc03e
No known key found for this signature in database
GPG key ID: 2AE5A13E321F953D
4 changed files with 24 additions and 19 deletions

View file

@ -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

View file

@ -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!'

View file

@ -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!'

View file

@ -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']