From becf4da70a487b7673f5768d867632773bfd2983 Mon Sep 17 00:00:00 2001 From: sebastiencarceles Date: Thu, 2 Feb 2023 09:59:59 +0100 Subject: [PATCH] fix raison social fetch --- app/services/api_entreprise_service.rb | 3 +++ spec/controllers/champs/siret_controller_spec.rb | 2 ++ spec/controllers/users/dossiers_controller_spec.rb | 2 ++ .../siret_champ_etablissement_fetchable_concern_spec.rb | 7 +++++++ spec/services/api_entreprise_service_spec.rb | 9 +++++++++ spec/system/users/dossier_prefill_get_spec.rb | 3 +++ spec/system/users/dossier_prefill_post_spec.rb | 3 +++ 7 files changed, 29 insertions(+) diff --git a/app/services/api_entreprise_service.rb b/app/services/api_entreprise_service.rb index 8eb5f4ed8..5f429d629 100644 --- a/app/services/api_entreprise_service.rb +++ b/app/services/api_entreprise_service.rb @@ -15,6 +15,9 @@ class APIEntrepriseService etablissement_params = APIEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params return nil if etablissement_params.empty? + entreprise_params = APIEntreprise::EntrepriseAdapter.new(siret, procedure_id).to_params + etablissement_params.merge!(entreprise_params) if entreprise_params.any? + etablissement = dossier_or_champ.build_etablissement(etablissement_params) etablissement.save! diff --git a/spec/controllers/champs/siret_controller_spec.rb b/spec/controllers/champs/siret_controller_spec.rb index 81a9b6dbd..2b1f5f42a 100644 --- a/spec/controllers/champs/siret_controller_spec.rb +++ b/spec/controllers/champs/siret_controller_spec.rb @@ -31,6 +31,8 @@ describe Champs::SiretController, type: :controller do sign_in user 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\/#{siret[0..8]}/) + .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/entreprises.json')) 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) diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 3f760ff38..57398f090 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -196,6 +196,8 @@ describe Users::DossiersController, type: :controller do sign_in(user) 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) 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) diff --git a/spec/models/concern/siret_champ_etablissement_fetchable_concern_spec.rb b/spec/models/concern/siret_champ_etablissement_fetchable_concern_spec.rb index 19151c31a..6a2c11120 100644 --- a/spec/models/concern/siret_champ_etablissement_fetchable_concern_spec.rb +++ b/spec/models/concern/siret_champ_etablissement_fetchable_concern_spec.rb @@ -8,6 +8,8 @@ RSpec.describe SiretChampEtablissementFetchableConcern do before 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\/#{siret[0..8]}/) + .to_return(body: File.read('spec/fixtures/files/api_entreprise/entreprises.json'), 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) @@ -103,6 +105,11 @@ RSpec.describe SiretChampEtablissementFetchableConcern do it { expect { fetch_etablissement! }.to change { Etablissement.count }.by(1) } it { expect(fetch_etablissement!).to eq(true) } + + it "fetches the entreprise raison sociale" do + fetch_etablissement! + expect(champ.reload.etablissement.entreprise_raison_sociale).to eq("OCTO-TECHNOLOGY") + end end end end diff --git a/spec/services/api_entreprise_service_spec.rb b/spec/services/api_entreprise_service_spec.rb index 93ec39539..fd7cc7ada 100644 --- a/spec/services/api_entreprise_service_spec.rb +++ b/spec/services/api_entreprise_service_spec.rb @@ -15,11 +15,16 @@ describe APIEntrepriseService do before do stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/) .to_return(body: etablissements_body, status: etablissements_status) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siret[0..8]}/) + .to_return(body: entreprises_body, status: entreprises_status) end let(:siret) { '41816609600051' } + let(:raison_sociale) { "OCTO-TECHNOLOGY" } let(:etablissements_status) { 200 } let(:etablissements_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') } + let(:entreprises_status) { 200 } + let(:entreprises_body) { File.read('spec/fixtures/files/api_entreprise/entreprises.json') } let(:valid_token) { "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" } let(:procedure) { create(:procedure, api_entreprise_token: valid_token) } let(:dossier) { create(:dossier, procedure: procedure) } @@ -35,6 +40,10 @@ describe APIEntrepriseService do expect(subject[:siret]).to eq(siret) end + it 'should fetch entreprise params' do + expect(subject[:entreprise_raison_sociale]).to eq(raison_sociale) + end + it_behaves_like 'schedule fetch of all etablissement params' end diff --git a/spec/system/users/dossier_prefill_get_spec.rb b/spec/system/users/dossier_prefill_get_spec.rb index 21b40fa1f..ada1cf6bc 100644 --- a/spec/system/users/dossier_prefill_get_spec.rb +++ b/spec/system/users/dossier_prefill_get_spec.rb @@ -59,6 +59,9 @@ describe 'Prefilling a dossier (with a GET request):', js: true do stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\//) .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/etablissements.json')) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siret_value[0..8]}/) + .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/entreprises.json')) + VCR.insert_cassette('api_geo_departements') VCR.insert_cassette('api_geo_communes') VCR.insert_cassette('api_geo_epcis') diff --git a/spec/system/users/dossier_prefill_post_spec.rb b/spec/system/users/dossier_prefill_post_spec.rb index eebb5462d..a9b4eae82 100644 --- a/spec/system/users/dossier_prefill_post_spec.rb +++ b/spec/system/users/dossier_prefill_post_spec.rb @@ -40,6 +40,9 @@ describe 'Prefilling a dossier (with a POST request):', js: true do stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret_value}/) .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/etablissements.json')) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siret_value[0..8]}/) + .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/entreprises.json')) + VCR.insert_cassette('api_geo_departements') VCR.insert_cassette('api_geo_communes') VCR.insert_cassette('api_geo_epcis')