Merge pull request #10275 from demarches-simplifiees/fix_api_entreprise_status
Tech: corrige le mécanisme de surveillance des fournisseurs d entreprise.api.gouv.fr
This commit is contained in:
commit
22830fd7f6
14 changed files with 45 additions and 52 deletions
|
@ -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
|
||||
|
|
|
@ -83,13 +83,6 @@ class APIEntreprise::API
|
|||
call(url)
|
||||
end
|
||||
|
||||
def current_status
|
||||
status_url = "https://status.entreprise.api.gouv.fr/summary.json"
|
||||
response = Typhoeus.get(status_url, timeout: 1)
|
||||
|
||||
handle_response(response)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def recipient_for(siret_or_siren)
|
||||
|
|
|
@ -12,7 +12,7 @@ module RNAChampAssociationFetchableConcern
|
|||
|
||||
update!(data: data)
|
||||
rescue APIEntreprise::API::Error => error
|
||||
error_key = :network_error if error.try(:network_error?) && !APIEntrepriseService.api_up?
|
||||
error_key = :network_error if error.try(:network_error?) && !APIEntrepriseService.api_djepva_up?
|
||||
clear_association!(error_key)
|
||||
end
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -58,8 +58,24 @@ class APIEntrepriseService
|
|||
APIEntreprise::AttestationFiscaleJob.set(wait:).perform_later(etablissement.id, procedure_id, user_id)
|
||||
end
|
||||
|
||||
def api_up?
|
||||
APIEntreprise::API.new.current_status.fetch(:page).fetch(:status) == 'UP'
|
||||
# See: https://entreprise.api.gouv.fr/developpeurs#surveillance-etat-fournisseurs
|
||||
def api_insee_up?
|
||||
api_up?("https://entreprise.api.gouv.fr/ping/insee/sirene")
|
||||
end
|
||||
|
||||
def api_djepva_up?
|
||||
api_up?("https://entreprise.api.gouv.fr/ping/djepva/api-association")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def api_up?(url)
|
||||
response = Typhoeus.get(url, timeout: 1)
|
||||
if response.success?
|
||||
JSON.parse(response.body).fetch('status') == 'ok'
|
||||
else
|
||||
false
|
||||
end
|
||||
rescue => e
|
||||
Sentry.capture_exception(e)
|
||||
false
|
||||
|
|
|
@ -85,7 +85,7 @@ describe Champs::RNAController, type: :controller do
|
|||
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
||||
|
||||
before do
|
||||
expect(APIEntrepriseService).to receive(:api_up?).and_return(false)
|
||||
expect(APIEntrepriseService).to receive(:api_djepva_up?).and_return(false)
|
||||
end
|
||||
|
||||
subject! { get :show, params: params, format: :turbo_stream }
|
||||
|
|
|
@ -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 }
|
||||
|
|
|
@ -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')
|
||||
|
|
5
spec/fixtures/files/api_entreprise/ping.json
vendored
Normal file
5
spec/fixtures/files/api_entreprise/ping.json
vendored
Normal 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"
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
{
|
||||
"page": {
|
||||
"name": "API Entreprise",
|
||||
"url": "https://status.entreprise.api.gouv.fr",
|
||||
"status": "UP"
|
||||
}
|
||||
}
|
|
@ -323,18 +323,4 @@ describe APIEntreprise::API do
|
|||
expect(WebMock).not_to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}/)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'current_status' do
|
||||
subject { described_class.new.current_status }
|
||||
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read }
|
||||
|
||||
before do
|
||||
stub_request(:get, "https://status.entreprise.api.gouv.fr/summary.json")
|
||||
.to_return(body: body)
|
||||
end
|
||||
|
||||
it "returns the current status response" do
|
||||
expect(subject).to eq(JSON.parse(body, symbolize_names: true))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -52,7 +52,7 @@ RSpec.describe RNAChampAssociationFetchableConcern do
|
|||
let(:status) { 503 }
|
||||
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
||||
|
||||
before { expect(APIEntrepriseService).to receive(:api_up?).and_return(false) }
|
||||
before { expect(APIEntrepriseService).to receive(:api_djepva_up?).and_return(false) }
|
||||
|
||||
it_behaves_like "an association fetcher", false, :network_error, 'W595001988', nil
|
||||
end
|
||||
|
|
|
@ -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 } }
|
||||
|
||||
|
|
|
@ -85,13 +85,13 @@ describe APIEntrepriseService do
|
|||
it_behaves_like 'schedule fetch of all etablissement params'
|
||||
end
|
||||
|
||||
describe "#api_up?" do
|
||||
subject { described_class.api_up? }
|
||||
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read }
|
||||
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://status.entreprise.api.gouv.fr/summary.json")
|
||||
stub_request(:get, "https://entreprise.api.gouv.fr/ping/insee/sirene")
|
||||
.to_return(body: body, status: status)
|
||||
end
|
||||
|
||||
|
@ -100,7 +100,7 @@ describe APIEntrepriseService do
|
|||
end
|
||||
|
||||
context "when api entreprise is down" do
|
||||
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read.gsub('UP', 'HASISSUES') }
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue