From e29af4842074a635ca892736bdaad2a13ee53b5f Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Mon, 27 Apr 2020 15:58:04 +0200 Subject: [PATCH] call api_entreprise with specific token if exist --- app/lib/api_entreprise/api.rb | 7 ++++--- spec/lib/api_entreprise/api_spec.rb | 27 +++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/app/lib/api_entreprise/api.rb b/app/lib/api_entreprise/api.rb index 5d8443fea..119d65495 100644 --- a/app/lib/api_entreprise/api.rb +++ b/app/lib/api_entreprise/api.rb @@ -74,11 +74,12 @@ class ApiEntreprise::API recipient: siret_or_siren, object: "procedure_id: #{procedure_id}", non_diffusables: true, - token: token + token: token_for_procedure(procedure_id) } end - def self.token - Rails.application.secrets.api_entreprise[:key] + def self.token_for_procedure(procedure_id) + procedure = Procedure.find(procedure_id) + procedure.api_entreprise_token.presence || Rails.application.secrets.api_entreprise[:key] end end diff --git a/spec/lib/api_entreprise/api_spec.rb b/spec/lib/api_entreprise/api_spec.rb index 403cd460c..77c7966b8 100644 --- a/spec/lib/api_entreprise/api_spec.rb +++ b/spec/lib/api_entreprise/api_spec.rb @@ -1,11 +1,13 @@ describe ApiEntreprise::API do - let(:procedure_id) { 12 } + let(:procedure) { create(:procedure) } + let(:procedure_id) { procedure.id } + let(:token) { Rails.application.secrets.api_entreprise[:key] } describe '.entreprise' do subject { described_class.entreprise(siren, procedure_id) } before do - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=/) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=#{token}/) .to_return(status: status, body: body) end @@ -47,6 +49,27 @@ describe ApiEntreprise::API do it 'returns response body' do expect(subject).to eq(JSON.parse(body, symbolize_names: true)) end + + context 'with specific token for procedure' do + let(:token) { 'token-for-demarche' } + let(:procedure) { create(:procedure, api_entreprise_token: token) } + let(:procedure_id) { procedure.id } + + it 'call api-entreprise with specfic token' do + subject + expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=token-for-demarche/) + end + end + + context 'without specific token for procedure' do + let(:procedure) { create(:procedure, api_entreprise_token: nil) } + let(:procedure_id) { procedure.id } + + it 'call api-entreprise with specfic token' do + subject + expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=#{token}/) + end + end end end