fix raison social fetch

This commit is contained in:
sebastiencarceles 2023-02-02 09:59:59 +01:00
parent 498e5d7487
commit becf4da70a
7 changed files with 29 additions and 0 deletions

View file

@ -15,6 +15,9 @@ class APIEntrepriseService
etablissement_params = APIEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params etablissement_params = APIEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params
return nil if etablissement_params.empty? 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 = dossier_or_champ.build_etablissement(etablissement_params)
etablissement.save! etablissement.save!

View file

@ -31,6 +31,8 @@ describe Champs::SiretController, type: :controller do
sign_in user sign_in user
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/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\/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) 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)

View file

@ -196,6 +196,8 @@ describe Users::DossiersController, type: :controller do
sign_in(user) sign_in(user)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/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\/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) 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)

View file

@ -8,6 +8,8 @@ RSpec.describe SiretChampEtablissementFetchableConcern do
before do before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/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\/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) 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)
@ -103,6 +105,11 @@ RSpec.describe SiretChampEtablissementFetchableConcern do
it { expect { fetch_etablissement! }.to change { Etablissement.count }.by(1) } it { expect { fetch_etablissement! }.to change { Etablissement.count }.by(1) }
it { expect(fetch_etablissement!).to eq(true) } 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 end
end end

View file

@ -15,11 +15,16 @@ describe APIEntrepriseService do
before do before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/)
.to_return(body: etablissements_body, status: etablissements_status) .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 end
let(:siret) { '41816609600051' } let(:siret) { '41816609600051' }
let(:raison_sociale) { "OCTO-TECHNOLOGY" }
let(:etablissements_status) { 200 } let(:etablissements_status) { 200 }
let(:etablissements_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') } 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(:valid_token) { "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" }
let(:procedure) { create(:procedure, api_entreprise_token: valid_token) } let(:procedure) { create(:procedure, api_entreprise_token: valid_token) }
let(:dossier) { create(:dossier, procedure: procedure) } let(:dossier) { create(:dossier, procedure: procedure) }
@ -35,6 +40,10 @@ describe APIEntrepriseService do
expect(subject[:siret]).to eq(siret) expect(subject[:siret]).to eq(siret)
end 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' it_behaves_like 'schedule fetch of all etablissement params'
end end

View file

@ -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\//) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\//)
.to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/etablissements.json')) .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_departements')
VCR.insert_cassette('api_geo_communes') VCR.insert_cassette('api_geo_communes')
VCR.insert_cassette('api_geo_epcis') VCR.insert_cassette('api_geo_epcis')

View file

@ -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}/) 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')) .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_departements')
VCR.insert_cassette('api_geo_communes') VCR.insert_cassette('api_geo_communes')
VCR.insert_cassette('api_geo_epcis') VCR.insert_cassette('api_geo_epcis')