make api entreprise call only if token not expired
This commit is contained in:
parent
f587e6600a
commit
dbf04dd0d8
3 changed files with 34 additions and 1 deletions
|
@ -60,6 +60,7 @@ class ApiEntreprise::API
|
||||||
private
|
private
|
||||||
|
|
||||||
def self.call(resource_name, siret_or_siren, procedure_id, user_id = nil)
|
def self.call(resource_name, siret_or_siren, procedure_id, user_id = nil)
|
||||||
|
return if ApiEntrepriseToken.new(token_for_procedure(procedure_id)).expired?
|
||||||
url = url(resource_name, siret_or_siren)
|
url = url(resource_name, siret_or_siren)
|
||||||
params = params(siret_or_siren, procedure_id, user_id)
|
params = params(siret_or_siren, procedure_id, user_id)
|
||||||
|
|
||||||
|
|
|
@ -230,6 +230,8 @@ describe Users::DossiersController, type: :controller do
|
||||||
let(:api_entreprise_bilans_bdf_status) { 200 }
|
let(:api_entreprise_bilans_bdf_status) { 200 }
|
||||||
let(:api_entreprise_bilans_bdf_body) { File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json') }
|
let(:api_entreprise_bilans_bdf_body) { File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json') }
|
||||||
|
|
||||||
|
let(:token_expired) { false }
|
||||||
|
|
||||||
def stub_api_entreprise_requests
|
def stub_api_entreprise_requests
|
||||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*token=/)
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*token=/)
|
||||||
.to_return(status: api_etablissement_status, body: api_etablissement_body)
|
.to_return(status: api_etablissement_status, body: api_etablissement_body)
|
||||||
|
@ -258,8 +260,9 @@ describe Users::DossiersController, type: :controller do
|
||||||
before do
|
before do
|
||||||
sign_in(user)
|
sign_in(user)
|
||||||
stub_api_entreprise_requests
|
stub_api_entreprise_requests
|
||||||
allow_any_instance_of(Procedure).to receive(:api_entreprise_roles)
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles)
|
||||||
.and_return(["attestations_fiscales", "attestations_sociales", "bilans_entreprise_bdf"])
|
.and_return(["attestations_fiscales", "attestations_sociales", "bilans_entreprise_bdf"])
|
||||||
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(token_expired)
|
||||||
end
|
end
|
||||||
before { Timecop.freeze(Time.zone.local(2020, 3, 14)) }
|
before { Timecop.freeze(Time.zone.local(2020, 3, 14)) }
|
||||||
after { Timecop.return }
|
after { Timecop.return }
|
||||||
|
@ -316,6 +319,14 @@ describe Users::DossiersController, type: :controller do
|
||||||
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown')
|
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when default token has expired' do
|
||||||
|
let(:api_etablissement_status) { 200 }
|
||||||
|
let(:api_body_status) { '' }
|
||||||
|
let(:token_expired) { true }
|
||||||
|
|
||||||
|
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown')
|
||||||
|
end
|
||||||
|
|
||||||
context 'when the API returns no Entreprise' do
|
context 'when the API returns no Entreprise' do
|
||||||
let(:api_entreprise_status) { 404 }
|
let(:api_entreprise_status) { 404 }
|
||||||
let(:api_entreprise_body) { '' }
|
let(:api_entreprise_body) { '' }
|
||||||
|
|
|
@ -9,6 +9,7 @@ describe ApiEntreprise::API do
|
||||||
before do
|
before do
|
||||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=#{token}/)
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=#{token}/)
|
||||||
.to_return(status: status, body: body)
|
.to_return(status: status, body: body)
|
||||||
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the service is unavailable' do
|
context 'when the service is unavailable' do
|
||||||
|
@ -78,6 +79,7 @@ describe ApiEntreprise::API do
|
||||||
before do
|
before do
|
||||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*non_diffusables=true&.*token=/)
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*non_diffusables=true&.*token=/)
|
||||||
.to_return(status: status, body: body)
|
.to_return(status: status, body: body)
|
||||||
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when siret does not exist' do
|
context 'when siret does not exist' do
|
||||||
|
@ -105,6 +107,7 @@ describe ApiEntreprise::API do
|
||||||
before do
|
before do
|
||||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\/.*token=/)
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\/.*token=/)
|
||||||
.to_return(status: status, body: body)
|
.to_return(status: status, body: body)
|
||||||
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when siret does not exist' do
|
context 'when siret does not exist' do
|
||||||
|
@ -136,6 +139,7 @@ describe ApiEntreprise::API do
|
||||||
before do
|
before do
|
||||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\/.*token=/)
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\/.*token=/)
|
||||||
.to_return(status: status, body: body)
|
.to_return(status: status, body: body)
|
||||||
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
subject { described_class.rna(siren, procedure_id) }
|
subject { described_class.rna(siren, procedure_id) }
|
||||||
|
@ -167,6 +171,7 @@ describe ApiEntreprise::API do
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(roles)
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(roles)
|
||||||
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
||||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_sociales_acoss\/#{siren}?.*token=/)
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_sociales_acoss\/#{siren}?.*token=/)
|
||||||
.to_return(body: body, status: status)
|
.to_return(body: body, status: status)
|
||||||
end
|
end
|
||||||
|
@ -195,6 +200,7 @@ describe ApiEntreprise::API do
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(roles)
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(roles)
|
||||||
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
||||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_fiscales_dgfip\/#{siren}?.*token=#{token}&user_id=#{user_id}/)
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_fiscales_dgfip\/#{siren}?.*token=#{token}&user_id=#{user_id}/)
|
||||||
.to_return(body: body, status: status)
|
.to_return(body: body, status: status)
|
||||||
end
|
end
|
||||||
|
@ -222,6 +228,7 @@ describe ApiEntreprise::API do
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(roles)
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(roles)
|
||||||
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
||||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/bilans_entreprises_bdf\/#{siren}?.*token=#{token}/)
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/bilans_entreprises_bdf\/#{siren}?.*token=#{token}/)
|
||||||
.to_return(body: body, status: status)
|
.to_return(body: body, status: status)
|
||||||
end
|
end
|
||||||
|
@ -240,4 +247,18 @@ describe ApiEntreprise::API do
|
||||||
it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) }
|
it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'with expired token' do
|
||||||
|
let(:siren) { '111111111' }
|
||||||
|
subject { described_class.entreprise(siren, procedure_id) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'makes no call to api-entreprise' do
|
||||||
|
subject
|
||||||
|
expect(WebMock).not_to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=#{token}/)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue