add and use api_insee_up?

This commit is contained in:
simon lehericey 2024-04-05 12:07:40 +02:00
parent 63052ea063
commit e994738a2e
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
8 changed files with 61 additions and 14 deletions

View file

@ -175,7 +175,7 @@ module Users
etablissement = begin
APIEntrepriseService.create_etablissement(@dossier, sanitized_siret, current_user.id)
rescue => error
if error.try(:network_error?) && !APIEntrepriseService.api_up?
if error.try(:network_error?) && !APIEntrepriseService.api_insee_up?
# TODO: notify ops
APIEntrepriseService.create_etablissement_as_degraded_mode(@dossier, sanitized_siret, current_user.id)
else

View file

@ -11,7 +11,7 @@ module SiretChampEtablissementFetchableConcern
update!(etablissement: etablissement)
rescue => error
if error.try(:network_error?) && !APIEntrepriseService.api_up?
if error.try(:network_error?) && !APIEntrepriseService.api_insee_up?
# TODO: notify ops
update!(
etablissement: APIEntrepriseService.create_etablissement_as_degraded_mode(self, siret, user.id)

View file

@ -64,5 +64,15 @@ class APIEntrepriseService
Sentry.capture_exception(e)
false
end
# See: https://entreprise.api.gouv.fr/developpeurs#surveillance-etat-fournisseurs
def api_insee_up?
response = Typhoeus.get("https://entreprise.api.gouv.fr/ping/insee/sirene", timeout: 1)
if response.success?
JSON.parse(response.body).fetch('status') == 'ok'
else
false
end
end
end
end

View file

@ -83,7 +83,7 @@ describe Champs::SiretController, type: :controller do
let(:api_etablissement_status) { 503 }
before do
expect(APIEntrepriseService).to receive(:api_up?).and_return(true)
expect(APIEntrepriseService).to receive(:api_insee_up?).and_return(true)
end
subject! { get :show, params: params, format: :turbo_stream }
@ -102,7 +102,7 @@ describe Champs::SiretController, type: :controller do
let(:api_etablissement_status) { 502 }
before do
expect(APIEntrepriseService).to receive(:api_up?).and_return(false)
expect(APIEntrepriseService).to receive(:api_insee_up?).and_return(false)
end
subject! { get :show, params: params, format: :turbo_stream }

View file

@ -245,21 +245,21 @@ describe Users::DossiersController, type: :controller do
let(:api_etablissement_status) { 200 }
let(:api_etablissement_body) { Rails.root.join('spec/fixtures/files/api_entreprise/etablissements.json').read }
let(:token_expired) { false }
let(:api_current_status_response) { nil }
let(:api_insee_status_response) { nil }
before do
sign_in(user)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/etablissements\/#{siret}/)
.to_return(status: api_etablissement_status, body: api_etablissement_body)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/)
.to_return(body: Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read, status: 200)
.to_return(body: Rails.root.join('spec/fixtures/files/api_entreprise/ping.json').read, status: 200)
allow_any_instance_of(APIEntrepriseToken).to receive(:roles)
.and_return(["attestations_fiscales", "attestations_sociales", "bilans_entreprise_bdf"])
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(token_expired)
if api_current_status_response
stub_request(:get, "https://status.entreprise.api.gouv.fr/summary.json")
.to_return(body: api_current_status_response)
if api_insee_status_response
stub_request(:get, "https://entreprise.api.gouv.fr/ping/insee/sirene")
.to_return(body: api_insee_status_response)
end
end
@ -304,14 +304,14 @@ describe Users::DossiersController, type: :controller do
context 'When API-Entreprise is ponctually down' do
let(:api_etablissement_status) { 502 }
let(:api_current_status_response) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read }
let(:api_insee_status_response) { Rails.root.join('spec/fixtures/files/api_entreprise/ping.json').read }
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_network_error')
end
context 'When API-Entreprise is globally down' do
let(:api_etablissement_status) { 502 }
let(:api_current_status_response) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read.gsub('UP', 'HASISSUES') }
let(:api_insee_status_response) { Rails.root.join('spec/fixtures/files/api_entreprise/ping.json').read.gsub('ok', 'HASISSUES') }
it "create an etablissement only with SIRET as degraded mode" do
dossier.reload
@ -328,7 +328,7 @@ describe Users::DossiersController, type: :controller do
context 'when default token has expired' do
let(:api_etablissement_status) { 200 }
let(:api_current_status_response) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read }
let(:api_insee_status_response) { Rails.root.join('spec/fixtures/files/api_entreprise/ping.json').read }
let(:token_expired) { true }
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_network_error')

View file

@ -0,0 +1,5 @@
{
"status": "ok",
"last_update": "2024-04-05T11:15:25.483+02:00",
"last_ok_status": "2024-04-05T11:15:25.483+02:00"
}

View file

@ -52,7 +52,7 @@ RSpec.describe SiretChampEtablissementFetchableConcern do
let(:siret) { '82161143100015' }
let(:api_etablissement_status) { 503 }
before { expect(APIEntrepriseService).to receive(:api_up?).and_return(true) }
before { expect(APIEntrepriseService).to receive(:api_insee_up?).and_return(true) }
it_behaves_like 'an error occured', :network_error
@ -66,7 +66,7 @@ RSpec.describe SiretChampEtablissementFetchableConcern do
let(:siret) { '82161143100015' }
let(:api_etablissement_status) { 502 }
before { expect(APIEntrepriseService).to receive(:api_up?).and_return(false) }
before { expect(APIEntrepriseService).to receive(:api_insee_up?).and_return(false) }
it { expect { fetch_etablissement! }.to change { champ.reload.etablissement } }

View file

@ -116,4 +116,36 @@ describe APIEntrepriseService do
end
end
end
describe "#api_insee_up?" do
subject { described_class.api_insee_up? }
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/ping.json').read }
let(:status) { 200 }
before do
stub_request(:get, "https://entreprise.api.gouv.fr/ping/insee/sirene")
.to_return(body: body, status: status)
end
it "returns true when api etablissement is up" do
expect(subject).to be_truthy
end
context "when api entreprise is down" do
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/ping.json').read.gsub('ok', 'HASISSUES') }
it "returns false" do
expect(subject).to be_falsey
end
end
context "when api entreprise status is unknown" do
let(:body) { "" }
let(:status) { 0 }
it "returns nil" do
expect(subject).to be_falsey
end
end
end
end