From 99abdf45e7044ea9cbc00cae0a1f68e642ba1294 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 27 Apr 2023 17:17:38 +0200 Subject: [PATCH] fix(api-entreprise): use new status endpoint --- app/lib/api_entreprise/api.rb | 2 +- app/services/api_entreprise_service.rb | 14 +- .../users/dossiers_controller_spec.rb | 12 +- .../files/api_entreprise/current_status.json | 265 ------------------ .../fixtures/files/api_entreprise/status.json | 7 + spec/lib/api_entreprise/api_spec.rb | 28 +- spec/services/api_entreprise_service_spec.rb | 18 +- 7 files changed, 37 insertions(+), 309 deletions(-) delete mode 100644 spec/fixtures/files/api_entreprise/current_status.json create mode 100644 spec/fixtures/files/api_entreprise/status.json diff --git a/app/lib/api_entreprise/api.rb b/app/lib/api_entreprise/api.rb index 20b07099e..80dd654ef 100644 --- a/app/lib/api_entreprise/api.rb +++ b/app/lib/api_entreprise/api.rb @@ -73,7 +73,7 @@ class APIEntreprise::API end def current_status - status_url = "https://entreprise.api.gouv.fr/watchdoge/dashboard/current_status" + status_url = "https://status.entreprise.api.gouv.fr/summary.json" response = Typhoeus.get(status_url, timeout: 1) handle_response(response) diff --git a/app/services/api_entreprise_service.rb b/app/services/api_entreprise_service.rb index 5f429d629..1999dc980 100644 --- a/app/services/api_entreprise_service.rb +++ b/app/services/api_entreprise_service.rb @@ -57,17 +57,11 @@ class APIEntrepriseService APIEntreprise::AttestationFiscaleJob.set(wait:).perform_later(etablissement.id, procedure_id, user_id) end - def api_up?(uname = "apie_2_etablissements") - statuses = APIEntreprise::API.new.current_status.fetch(:results) - - # find results having uname = apie_2_etablissements - status = statuses.find { |result| result[:uname] == uname } - - status.fetch(:code) == 200 + def api_up? + APIEntreprise::API.new.current_status.fetch(:page).fetch(:status) == 'UP' rescue => e - Sentry.capture_exception(e, extra: { uname: uname }) - - nil + Sentry.capture_exception(e) + false end end end diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 2cd90fb78..798526e71 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -188,7 +188,7 @@ describe Users::DossiersController, type: :controller do let(:siret) { params_siret.delete(' ') } let(:siren) { siret[0..8] } let(:api_etablissement_status) { 200 } - let(:api_etablissement_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') } + 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 } @@ -197,13 +197,13 @@ describe Users::DossiersController, type: :controller do stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/) .to_return(status: api_etablissement_status, body: api_etablissement_body) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}/) - .to_return(body: File.read('spec/fixtures/files/api_entreprise/entreprises.json'), status: 200) + .to_return(body: Rails.root.join('spec/fixtures/files/api_entreprise/status.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://entreprise.api.gouv.fr/watchdoge/dashboard/current_status") + stub_request(:get, "https://status.entreprise.api.gouv.fr/summary.json") .to_return(body: api_current_status_response) end end @@ -249,14 +249,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) { File.read('spec/fixtures/files/api_entreprise/current_status.json') } + let(:api_current_status_response) { Rails.root.join('spec/fixtures/files/api_entreprise/status.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) { File.read('spec/fixtures/files/api_entreprise/current_status.json').gsub('200', '502') } + let(:api_current_status_response) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read.gsub('UP', 'HASISSUES') } it "create an etablissement only with SIRET as degraded mode" do dossier.reload @@ -273,7 +273,7 @@ describe Users::DossiersController, type: :controller do context 'when default token has expired' do let(:api_etablissement_status) { 200 } - let(:api_current_status_response) { File.read('spec/fixtures/files/api_entreprise/current_status.json') } + let(:api_current_status_response) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read } let(:token_expired) { true } it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_network_error') diff --git a/spec/fixtures/files/api_entreprise/current_status.json b/spec/fixtures/files/api_entreprise/current_status.json deleted file mode 100644 index 8271a9d6b..000000000 --- a/spec/fixtures/files/api_entreprise/current_status.json +++ /dev/null @@ -1,265 +0,0 @@ -{ - "results": [ - { - "uname": "apie_2_etablissements", - "name": "Etablissements", - "provider": "insee", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:31:40.442Z", - "endpoint": "api_entreprise/v2/etablissements_restored" - }, - { - "uname": "apie_2_certificats_qualibat", - "name": "Certificats Qualibat", - "provider": "qualibat", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:30:28.848Z", - "endpoint": "api_entreprise/v2/certificats_qualibat" - }, - { - "uname": "apie_2_effectifs_annuels_acoss", - "name": "Effectifs annuels", - "provider": "acoss", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:30:41.415Z", - "endpoint": "api_entreprise/v2/effectifs_annuels_entreprise_acoss_covid" - }, - { - "uname": "apie_2_effectifs_mensuels_entreprise_acoss", - "name": "Effectifs mensuels (entreprise)", - "provider": "acoss", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:31:17.437Z", - "endpoint": "api_entreprise/v2/effectifs_mensuels_entreprise_acoss_covid" - }, - { - "uname": "apie_2_effectifs_mensuels_etablissement_acoss", - "name": "Effectifs mensuels (établissement)", - "provider": "acoss", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:31:52.412Z", - "endpoint": "api_entreprise/v2/effectifs_mensuels_etablissement_acoss_covid" - }, - { - "uname": "apie_2_conventions_collectives", - "name": "Conventions Collectives", - "provider": "fabsocial", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:30:03.003Z", - "endpoint": "api_entreprise/v2/conventions_collectives" - }, - { - "uname": "apie_2_agence_bio", - "name": "Certifications BIO", - "provider": "agencebio", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:30:03.042Z", - "endpoint": "api_entreprise/v2/certificats_agence_bio" - }, - { - "uname": "apie_2_entreprises", - "name": "Entreprise", - "provider": "insee", - "api_version": 2, - "code": 206, - "timestamp": "2022-09-15T14:33:27.934Z", - "endpoint": "api_entreprise/v2/entreprises_restored" - }, - { - "uname": "apie_2_eori", - "name": "Immatriculation EORI Douanes", - "provider": "douanes", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:33:46.047Z", - "endpoint": "api_entreprise/v2/eori_douanes" - }, - { - "uname": "apie_2_attestations_fiscales_dgfip", - "name": "Attestations fiscales", - "provider": "dgfip", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:31:32.503Z", - "endpoint": "api_entreprise/v2/attestations_fiscales_dgfip" - }, - { - "uname": "apie_2_cma_france", - "name": "Entreprises Artisanales CMA France", - "provider": "cmafrance", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:30:55.431Z", - "endpoint": "api_entreprise/v2/entreprises_artisanales" - }, - { - "uname": "apie_2_liasses_fiscales_dgfip_complete", - "name": "Liasses fiscales (complete)", - "provider": "dgfip", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:32:55.595Z", - "endpoint": "api_entreprise/v2/liasses_fiscales_dgfip" - }, - { - "uname": "apie_2_exercices_dgfip", - "name": "Exercices", - "provider": "dgfip", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:19:15.501Z", - "endpoint": "api_entreprise/v2/exercices" - }, - { - "uname": "apie_2_cotisations_msa", - "name": "Cotisations MSA", - "provider": "msa", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:31:57.747Z", - "endpoint": "api_entreprise/v2/cotisations_msa" - }, - { - "uname": "apie_2_eligibilites_cotisation_retraite_probtp", - "name": "Eligibilité cotisation retraite ProBTP", - "provider": "probtp", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:30:36.979Z", - "endpoint": "api_entreprise/v2/eligibilites_cotisation_retraite_probtp" - }, - { - "uname": "apie_2_documents_associations_rna", - "name": "Documents associations", - "provider": "rna", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:31:57.924Z", - "endpoint": "api_entreprise/v2/documents_associations" - }, - { - "uname": "apie_2_cartes_professionnelles_fntp", - "name": "Cartes professionnelles FNTP", - "provider": "fntp", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:00:06.107Z", - "endpoint": "api_entreprise/v2/cartes_professionnelles_fntp" - }, - { - "uname": "apie_2_associations_rna", - "name": "Associations", - "provider": "rna", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:33:12.993Z", - "endpoint": "api_entreprise/v2/associations" - }, - { - "uname": "apie_2_attestations_agefiph", - "name": "Attestations agefiph", - "provider": "agefiph", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:32:15.345Z", - "endpoint": "api_entreprise/v2/attestations_agefiph" - }, - { - "uname": "apie_2_extraits_rcs_infogreffe", - "name": "Extraits RCS (Infogreffe)", - "provider": "infogreffe", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:33:48.762Z", - "endpoint": "api_entreprise/v2/extraits_rcs_infogreffe" - }, - { - "uname": "apie_2_liasses_fiscales_dgfip_dictionnaire", - "name": "Liasses fiscales (dictionnaire)", - "provider": "dgfip", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:31:36.557Z", - "endpoint": "api_entreprise/v2/liasses_fiscales_dgfip" - }, - { - "uname": "apie_2_certificats_cnetp", - "name": "Certificats CNETP", - "provider": "cnetp", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:32:23.801Z", - "endpoint": "api_entreprise/v2/certificats_cnetp" - }, - { - "uname": "apie_2_certificats_opqibi", - "name": "Certificats OPQIBI", - "provider": "opqibi", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:00:03.725Z", - "endpoint": "api_entreprise/v2/certificats_opqibi" - }, - { - "uname": "apie_2_certificats_rge_ademe", - "name": "Certificats RGE ADEME", - "provider": "ademe", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:00:04.174Z", - "endpoint": "api_entreprise/v2/certificats_rge_ademe" - }, - { - "uname": "apie_2_liasses_fiscales_dgfip_declaration", - "name": "Liasses fiscales (déclaration)", - "provider": "dgfip", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:00:03.654Z", - "endpoint": "api_entreprise/v2/liasses_fiscales_dgfip" - }, - { - "uname": "apie_2_extraits_courts_inpi", - "name": "Extraits courts INPI", - "provider": "inpi", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:00:04.660Z", - "endpoint": "api_entreprise/v2/extraits_courts_inpi" - }, - { - "uname": "apie_2_actes_inpi", - "name": "Actes INPI", - "provider": "inpi", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:00:04.263Z", - "endpoint": "api_entreprise/v2/documents_inpi" - }, - { - "uname": "apie_2_bilans_inpi", - "name": "Bilans INPI", - "provider": "inpi", - "api_version": 2, - "code": 200, - "timestamp": "2022-09-15T14:00:04.446Z", - "endpoint": "api_entreprise/v2/documents_inpi" - }, - { - "uname": "apie_2_bilans_entreprises_bdf", - "name": "Bilans Entreprises Banque de France", - "provider": "bdf", - "api_version": 2, - "code": 404, - "timestamp": "2022-09-15T14:00:07.312Z", - "endpoint": "api_entreprise/v2/bilans_entreprises_bdf" - } - ] -} diff --git a/spec/fixtures/files/api_entreprise/status.json b/spec/fixtures/files/api_entreprise/status.json new file mode 100644 index 000000000..7da318e25 --- /dev/null +++ b/spec/fixtures/files/api_entreprise/status.json @@ -0,0 +1,7 @@ +{ + "page": { + "name": "API Entreprise", + "url": "https://status.entreprise.api.gouv.fr", + "status": "UP" + } +} diff --git a/spec/lib/api_entreprise/api_spec.rb b/spec/lib/api_entreprise/api_spec.rb index 5eb3f5647..02ea02e29 100644 --- a/spec/lib/api_entreprise/api_spec.rb +++ b/spec/lib/api_entreprise/api_spec.rb @@ -15,7 +15,7 @@ describe APIEntreprise::API do context 'when the service throws a bad gateaway exception' do let(:siren) { '111111111' } let(:status) { 502 } - let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_unavailable.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/entreprises_unavailable.json').read } it 'raises APIEntreprise::API::Error::RequestFailed' do expect { subject }.to raise_error(APIEntreprise::API::Error::BadGateway) @@ -25,7 +25,7 @@ describe APIEntreprise::API do context 'when siren does not exist' do let(:siren) { '111111111' } let(:status) { 404 } - let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_not_found.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/entreprises_not_found.json').read } it 'raises APIEntreprise::API::Error::ResourceNotFound' do expect { subject }.to raise_error(APIEntreprise::API::Error::ResourceNotFound) @@ -35,7 +35,7 @@ describe APIEntreprise::API do context 'when request has bad format' do let(:siren) { '111111111' } let(:status) { 400 } - let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_not_found.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/entreprises_not_found.json').read } it 'raises APIEntreprise::API::Error::BadFormatRequest' do expect { subject }.to raise_error(APIEntreprise::API::Error::BadFormatRequest) @@ -45,7 +45,7 @@ describe APIEntreprise::API do context 'when siren infos are private' do let(:siren) { '111111111' } let(:status) { 403 } - let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_private.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/entreprises_private.json').read } it 'raises APIEntreprise::API::Error::ResourceNotFound' do expect { subject }.to raise_error(APIEntreprise::API::Error::ResourceNotFound) @@ -55,7 +55,7 @@ describe APIEntreprise::API do context 'when siren exist' do let(:siren) { '418166096' } let(:status) { 200 } - let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/entreprises.json').read } it 'returns response body' do expect(subject).to eq(JSON.parse(body, symbolize_names: true)) @@ -105,7 +105,7 @@ describe APIEntreprise::API do context 'when siret exists' do let(:siret) { '41816609600051' } let(:status) { 200 } - let(:body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/etablissements.json').read } it 'returns body' do expect(subject).to eq(JSON.parse(body, symbolize_names: true)) @@ -137,7 +137,7 @@ describe APIEntreprise::API do let(:siret) { '41816609600051' } let(:status) { 200 } - let(:body) { File.read('spec/fixtures/files/api_entreprise/exercices.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/exercices.json').read } it 'success' do expect(subject).to eq(JSON.parse(body, symbolize_names: true)) @@ -167,7 +167,7 @@ describe APIEntreprise::API do context 'when siren exists' do let(:siren) { '418166096' } let(:status) { 200 } - let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/associations.json').read } it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) } end @@ -177,7 +177,7 @@ describe APIEntreprise::API do let(:procedure) { create(:procedure, api_entreprise_token: token) } let(:siren) { '418166096' } let(:status) { 200 } - let(:body) { File.read('spec/fixtures/files/api_entreprise/attestation_sociale.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/attestation_sociale.json').read } before do allow_any_instance_of(APIEntrepriseToken).to receive(:roles).and_return(roles) @@ -206,7 +206,7 @@ describe APIEntreprise::API do let(:siren) { '418166096' } let(:user_id) { 1 } let(:status) { 200 } - let(:body) { File.read('spec/fixtures/files/api_entreprise/attestation_fiscale.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/attestation_fiscale.json').read } before do allow_any_instance_of(APIEntrepriseToken).to receive(:roles).and_return(roles) @@ -234,7 +234,7 @@ describe APIEntreprise::API do let(:procedure) { create(:procedure, api_entreprise_token: token) } let(:siren) { '418166096' } let(:status) { 200 } - let(:body) { File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json').read } before do allow_any_instance_of(APIEntrepriseToken).to receive(:roles).and_return(roles) @@ -261,7 +261,7 @@ describe APIEntreprise::API do describe '.privileges' do let(:api) { described_class.new } let(:status) { 200 } - let(:body) { File.read('spec/fixtures/files/api_entreprise/privileges.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/privileges.json').read } subject { api.privileges } before do @@ -292,10 +292,10 @@ describe APIEntreprise::API do describe 'current_status' do subject { described_class.new.current_status } - let(:body) { File.read('spec/fixtures/files/api_entreprise/current_status.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read } before do - stub_request(:get, "https://entreprise.api.gouv.fr/watchdoge/dashboard/current_status") + stub_request(:get, "https://status.entreprise.api.gouv.fr/summary.json") .to_return(body: body) end diff --git a/spec/services/api_entreprise_service_spec.rb b/spec/services/api_entreprise_service_spec.rb index fd7cc7ada..5e8a165cf 100644 --- a/spec/services/api_entreprise_service_spec.rb +++ b/spec/services/api_entreprise_service_spec.rb @@ -86,11 +86,11 @@ describe APIEntrepriseService do describe "#api_up?" do subject { described_class.api_up? } - let(:body) { File.read('spec/fixtures/files/api_entreprise/current_status.json') } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read } let(:status) { 200 } before do - stub_request(:get, "https://entreprise.api.gouv.fr/watchdoge/dashboard/current_status") + stub_request(:get, "https://status.entreprise.api.gouv.fr/summary.json") .to_return(body: body, status: status) end @@ -99,18 +99,10 @@ describe APIEntrepriseService do end context "when api entreprise is down" do - let(:body) do - original_body = super() - - json = JSON.parse(original_body) - # API etablissements is the first listed - json["results"][0]["code"] = 502 - - JSON.generate(json) - end + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read.gsub('UP', 'HASISSUES') } it "returns false" do - expect(subject).to be_falsy + expect(subject).to be_falsey end end @@ -119,7 +111,7 @@ describe APIEntrepriseService do let(:status) { 0 } it "returns nil" do - expect(subject).to be_nil + expect(subject).to be_falsey end end end