feat(api_token): api v1 use new token

This commit is contained in:
Paul Chavard 2022-11-30 10:13:55 +01:00
parent 3c21090a88
commit a47a056ee8
4 changed files with 6 additions and 9 deletions

View file

@ -4,8 +4,9 @@ class APIController < ApplicationController
protected
def find_administrateur_for_token(procedure)
procedure.administrateurs.find do |administrateur|
administrateur.valid_api_token?(api_token.token)
api_token = APIToken.find_and_verify(authorization_bearer_token, procedure.administrateurs)
if api_token.present? && procedure.administrateurs.include?(api_token.administrateur)
api_token.administrateur
end
end
@ -15,10 +16,6 @@ class APIController < ApplicationController
request.format = "json" if !request.params[:format]
end
def api_token
@api_token ||= APIToken.new(authorization_bearer_token)
end
def authorization_bearer_token
params_token.presence || header_token
end

View file

@ -1,6 +1,6 @@
describe API::V1::DossiersController do
let(:admin) { create(:administrateur) }
let(:token) { admin.renew_api_token }
let(:token) { APIToken.generate(admin)[1] }
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, administrateur: admin) }
let(:wrong_procedure) { create(:procedure) }

View file

@ -1,6 +1,6 @@
describe API::V1::ProceduresController, type: :controller do
let!(:admin) { create(:administrateur, :with_api_token) }
let!(:token) { admin.renew_api_token }
let!(:token) { APIToken.generate(admin)[1] }
it { expect(described_class).to be < APIController }

View file

@ -12,7 +12,7 @@ describe APIController, type: :controller do
end
context 'when the admin has a token' do
let!(:token) { admin.renew_api_token }
let!(:token) { APIToken.generate(admin)[1] }
context 'and the token is given by params' do
before { controller.params[:token] = token }