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
|
etablissement = begin
|
||||||
APIEntrepriseService.create_etablissement(@dossier, sanitized_siret, current_user.id)
|
APIEntrepriseService.create_etablissement(@dossier, sanitized_siret, current_user.id)
|
||||||
rescue => error
|
rescue => error
|
||||||
if error.try(:network_error?) && !APIEntrepriseService.api_up?
|
if error.try(:network_error?) && !APIEntrepriseService.api_insee_up?
|
||||||
# TODO: notify ops
|
# TODO: notify ops
|
||||||
APIEntrepriseService.create_etablissement_as_degraded_mode(@dossier, sanitized_siret, current_user.id)
|
APIEntrepriseService.create_etablissement_as_degraded_mode(@dossier, sanitized_siret, current_user.id)
|
||||||
else
|
else
|
||||||
|
|
|
@ -83,13 +83,6 @@ class APIEntreprise::API
|
||||||
call(url)
|
call(url)
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def recipient_for(siret_or_siren)
|
def recipient_for(siret_or_siren)
|
||||||
|
|
|
@ -12,7 +12,7 @@ module RNAChampAssociationFetchableConcern
|
||||||
|
|
||||||
update!(data: data)
|
update!(data: data)
|
||||||
rescue APIEntreprise::API::Error => error
|
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)
|
clear_association!(error_key)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ module SiretChampEtablissementFetchableConcern
|
||||||
|
|
||||||
update!(etablissement: etablissement)
|
update!(etablissement: etablissement)
|
||||||
rescue => error
|
rescue => error
|
||||||
if error.try(:network_error?) && !APIEntrepriseService.api_up?
|
if error.try(:network_error?) && !APIEntrepriseService.api_insee_up?
|
||||||
# TODO: notify ops
|
# TODO: notify ops
|
||||||
update!(
|
update!(
|
||||||
etablissement: APIEntrepriseService.create_etablissement_as_degraded_mode(self, siret, user.id)
|
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)
|
APIEntreprise::AttestationFiscaleJob.set(wait:).perform_later(etablissement.id, procedure_id, user_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def api_up?
|
# See: https://entreprise.api.gouv.fr/developpeurs#surveillance-etat-fournisseurs
|
||||||
APIEntreprise::API.new.current_status.fetch(:page).fetch(:status) == 'UP'
|
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
|
rescue => e
|
||||||
Sentry.capture_exception(e)
|
Sentry.capture_exception(e)
|
||||||
false
|
false
|
||||||
|
|
|
@ -85,7 +85,7 @@ describe Champs::RNAController, type: :controller do
|
||||||
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
expect(APIEntrepriseService).to receive(:api_up?).and_return(false)
|
expect(APIEntrepriseService).to receive(:api_djepva_up?).and_return(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
subject! { get :show, params: params, format: :turbo_stream }
|
subject! { get :show, params: params, format: :turbo_stream }
|
||||||
|
|
|
@ -83,7 +83,7 @@ describe Champs::SiretController, type: :controller do
|
||||||
let(:api_etablissement_status) { 503 }
|
let(:api_etablissement_status) { 503 }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
expect(APIEntrepriseService).to receive(:api_up?).and_return(true)
|
expect(APIEntrepriseService).to receive(:api_insee_up?).and_return(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
subject! { get :show, params: params, format: :turbo_stream }
|
subject! { get :show, params: params, format: :turbo_stream }
|
||||||
|
@ -102,7 +102,7 @@ describe Champs::SiretController, type: :controller do
|
||||||
let(:api_etablissement_status) { 502 }
|
let(:api_etablissement_status) { 502 }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
expect(APIEntrepriseService).to receive(:api_up?).and_return(false)
|
expect(APIEntrepriseService).to receive(:api_insee_up?).and_return(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
subject! { get :show, params: params, format: :turbo_stream }
|
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_status) { 200 }
|
||||||
let(:api_etablissement_body) { Rails.root.join('spec/fixtures/files/api_entreprise/etablissements.json').read }
|
let(:api_etablissement_body) { Rails.root.join('spec/fixtures/files/api_entreprise/etablissements.json').read }
|
||||||
let(:token_expired) { false }
|
let(:token_expired) { false }
|
||||||
let(:api_current_status_response) { nil }
|
let(:api_insee_status_response) { nil }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
sign_in(user)
|
sign_in(user)
|
||||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/etablissements\/#{siret}/)
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/etablissements\/#{siret}/)
|
||||||
.to_return(status: api_etablissement_status, body: api_etablissement_body)
|
.to_return(status: api_etablissement_status, body: api_etablissement_body)
|
||||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/)
|
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)
|
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)
|
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(token_expired)
|
||||||
|
|
||||||
if api_current_status_response
|
if api_insee_status_response
|
||||||
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: api_current_status_response)
|
.to_return(body: api_insee_status_response)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -304,14 +304,14 @@ describe Users::DossiersController, type: :controller do
|
||||||
|
|
||||||
context 'When API-Entreprise is ponctually down' do
|
context 'When API-Entreprise is ponctually down' do
|
||||||
let(:api_etablissement_status) { 502 }
|
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')
|
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_network_error')
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'When API-Entreprise is globally down' do
|
context 'When API-Entreprise is globally down' do
|
||||||
let(:api_etablissement_status) { 502 }
|
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
|
it "create an etablissement only with SIRET as degraded mode" do
|
||||||
dossier.reload
|
dossier.reload
|
||||||
|
@ -328,7 +328,7 @@ describe Users::DossiersController, type: :controller do
|
||||||
|
|
||||||
context 'when default token has expired' do
|
context 'when default token has expired' do
|
||||||
let(:api_etablissement_status) { 200 }
|
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 }
|
let(:token_expired) { true }
|
||||||
|
|
||||||
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_network_error')
|
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}/)
|
expect(WebMock).not_to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}/)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -52,7 +52,7 @@ RSpec.describe RNAChampAssociationFetchableConcern do
|
||||||
let(:status) { 503 }
|
let(:status) { 503 }
|
||||||
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
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
|
it_behaves_like "an association fetcher", false, :network_error, 'W595001988', nil
|
||||||
end
|
end
|
||||||
|
|
|
@ -52,7 +52,7 @@ RSpec.describe SiretChampEtablissementFetchableConcern do
|
||||||
let(:siret) { '82161143100015' }
|
let(:siret) { '82161143100015' }
|
||||||
let(:api_etablissement_status) { 503 }
|
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
|
it_behaves_like 'an error occured', :network_error
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ RSpec.describe SiretChampEtablissementFetchableConcern do
|
||||||
let(:siret) { '82161143100015' }
|
let(:siret) { '82161143100015' }
|
||||||
let(:api_etablissement_status) { 502 }
|
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 } }
|
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'
|
it_behaves_like 'schedule fetch of all etablissement params'
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#api_up?" do
|
describe "#api_insee_up?" do
|
||||||
subject { described_class.api_up? }
|
subject { described_class.api_insee_up? }
|
||||||
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read }
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/ping.json').read }
|
||||||
let(:status) { 200 }
|
let(:status) { 200 }
|
||||||
|
|
||||||
before do
|
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)
|
.to_return(body: body, status: status)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ describe APIEntrepriseService do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when api entreprise is down" do
|
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
|
it "returns false" do
|
||||||
expect(subject).to be_falsey
|
expect(subject).to be_falsey
|
||||||
|
|
Loading…
Reference in a new issue