chore(services): fetch etablissement adresse from API Entreprise
This commit is contained in:
parent
7d39cc6b3e
commit
8762acc0b5
3 changed files with 119 additions and 1 deletions
|
@ -15,6 +15,7 @@ class APIEntreprise::API
|
|||
|
||||
attr_reader :procedure
|
||||
attr_accessor :token
|
||||
attr_accessor :api_object
|
||||
|
||||
def initialize(procedure_id = nil)
|
||||
return if procedure_id.blank?
|
||||
|
@ -125,7 +126,12 @@ class APIEntreprise::API
|
|||
def build_params(user_id)
|
||||
params = base_params
|
||||
|
||||
params[:object] = "procedure_id: #{procedure.id}" if procedure.present?
|
||||
params[:object] = if api_object.present?
|
||||
api_object
|
||||
elsif procedure.present?
|
||||
"procedure_id: #{procedure.id}"
|
||||
end
|
||||
|
||||
params[:user_id] = user_id if user_id.present?
|
||||
|
||||
params
|
||||
|
|
23
app/lib/api_entreprise/service_adapter.rb
Normal file
23
app/lib/api_entreprise/service_adapter.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
class APIEntreprise::ServiceAdapter < APIEntreprise::EtablissementAdapter
|
||||
def initialize(siret, service_id)
|
||||
@siret = siret
|
||||
@service_id = service_id
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get_resource
|
||||
api_instance = api
|
||||
# TODO: reuse instead a token from an administrateur's procedure?
|
||||
api_instance.token = Rails.application.secrets.api_entreprise[:key]
|
||||
api_instance.api_object = "service_id: #{@service_id}"
|
||||
api_instance.etablissement(@siret)
|
||||
end
|
||||
|
||||
def attr_to_fetch
|
||||
[
|
||||
:adresse,
|
||||
:siret
|
||||
]
|
||||
end
|
||||
end
|
89
spec/lib/api_entreprise/service_adapter_spec.rb
Normal file
89
spec/lib/api_entreprise/service_adapter_spec.rb
Normal file
|
@ -0,0 +1,89 @@
|
|||
describe APIEntreprise::ServiceAdapter do
|
||||
before do
|
||||
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
||||
end
|
||||
|
||||
let(:siret) { '41816609600051' }
|
||||
let(:service) { create(:service, siret: siret) }
|
||||
|
||||
context 'SIRET valide avec infos diffusables' do
|
||||
let(:fixture) { 'spec/fixtures/files/api_entreprise/etablissements.json' }
|
||||
subject { described_class.new(siret, service.id).to_params }
|
||||
|
||||
before do
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/)
|
||||
.with(query: hash_including({ 'object' => "service_id: #{service.id}" }))
|
||||
.to_return(body: File.read(fixture, status: 200))
|
||||
end
|
||||
|
||||
it '#to_params class est une Hash ?' do
|
||||
expect(subject).to be_a_instance_of(Hash)
|
||||
end
|
||||
|
||||
context 'Attributs Etablissements' do
|
||||
it 'should contains a SIRET' do
|
||||
expect(subject[:siret]).to eq(siret)
|
||||
end
|
||||
|
||||
it 'should not return siege_social information' do
|
||||
expect(subject[:siege_social]).to be_nil
|
||||
end
|
||||
|
||||
context 'Concaténation lignes adresse' do
|
||||
it 'service contains a multi lines adress' do
|
||||
expect(subject[:adresse]).to eq("OCTO TECHNOLOGY\r\n50 AVENUE DES CHAMPS ELYSEES\r\n75008 PARIS\r\nFRANCE")
|
||||
end
|
||||
end
|
||||
|
||||
context 'adress details' do
|
||||
it 'service contains a numero_voie' do
|
||||
expect(subject[:numero_voie]).to eq('50')
|
||||
end
|
||||
|
||||
it 'service contains a type_voie' do
|
||||
expect(subject[:type_voie]).to eq('AV')
|
||||
end
|
||||
|
||||
it 'service contains a nom_voie' do
|
||||
expect(subject[:nom_voie]).to eq('DES CHAMPS ELYSEES')
|
||||
end
|
||||
it 'service contains a complement_adresse' do
|
||||
expect(subject[:complement_adresse]).to eq('complement_adresse')
|
||||
end
|
||||
|
||||
it 'service contains a code_postal' do
|
||||
expect(subject[:code_postal]).to eq('75008')
|
||||
end
|
||||
|
||||
it 'service contains a localite' do
|
||||
expect(subject[:localite]).to eq('PARIS 8')
|
||||
end
|
||||
|
||||
it 'service contains a code_insee_localite' do
|
||||
expect(subject[:code_insee_localite]).to eq('75108')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'Attributs Etablissements pour etablissement non siege' do
|
||||
let(:siret) { '17310120500719' }
|
||||
let(:fixture) { 'spec/fixtures/files/api_entreprise/etablissements-non-siege.json' }
|
||||
it 'service contains a siret' do
|
||||
expect(subject[:siret]).to eq(siret)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when siret is not found' do
|
||||
let(:bad_siret) { 11_111_111_111_111 }
|
||||
subject { described_class.new(bad_siret, service.id).to_params }
|
||||
|
||||
before do
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{bad_siret}/)
|
||||
.with(query: hash_including({ 'object' => "service_id: #{service.id}" }))
|
||||
.to_return(body: 'Fake body', status: 404)
|
||||
end
|
||||
|
||||
it { expect(subject).to eq({}) }
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue