refactor procedure_controller

This commit is contained in:
simon lehericey 2023-09-20 09:31:14 +02:00
parent 40a15b9be4
commit 4a17dec87b
2 changed files with 5 additions and 12 deletions

View file

@ -1,5 +1,6 @@
class API::V1::ProceduresController < APIController class API::V1::ProceduresController < APIController
before_action :fetch_procedure_and_check_token before_action :check_api_token
before_action :fetch_procedure
def show def show
render json: { procedure: ProcedureSerializer.new(@procedure).as_json } render json: { procedure: ProcedureSerializer.new(@procedure).as_json }
@ -7,17 +8,9 @@ class API::V1::ProceduresController < APIController
private private
def fetch_procedure_and_check_token def fetch_procedure
@procedure = Procedure.for_api.find(params[:id]) @procedure = @api_token.procedures.for_api.find(params[:id])
administrateur = find_administrateur_for_token(@procedure)
if administrateur.nil?
render json: {}, status: :unauthorized
else
# allow BaseController append_info_to_payload
# to log info on current_user
@current_user = administrateur.user
end
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
render json: {}, status: :not_found render json: {}, status: :not_found
end end

View file

@ -16,7 +16,7 @@ describe API::V1::ProceduresController, type: :controller do
context 'when procedure belongs to administrateur without token' do context 'when procedure belongs to administrateur without token' do
let(:procedure_id) { create(:procedure).id } let(:procedure_id) { create(:procedure).id }
it { is_expected.to have_http_status(401) } it { is_expected.to have_http_status(404) }
end end
context 'when procedure exist but bad token' do context 'when procedure exist but bad token' do