2022-07-25 16:27:26 +02:00
describe APIEntreprise :: ServiceAdapter do
before do
allow_any_instance_of ( APIEntrepriseToken ) . to receive ( :expired? ) . and_return ( false )
end
2023-05-30 16:27:20 +02:00
let ( :siret ) { '30613890001294' }
2022-07-25 16:27:26 +02:00
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
2023-05-30 16:27:20 +02:00
stub_request ( :get , / https: \/ \/ entreprise.api.gouv.fr \/ v3 \/ insee \/ sirene \/ etablissements \/ #{ siret } / )
2022-07-25 16:27:26 +02:00
. 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
2023-05-30 16:27:20 +02:00
expect ( subject [ :adresse ] ) . to eq ( " DIRECTION INTERMINISTERIELLE DU NUMERIQUE \r \n JEAN MARIE DURAND \r \n ZAE SAINT GUENAULT \r \n 51 BIS RUE DE LA PAIX \r \n CS 72809 \r \n 75256 PARIX CEDEX 12 \r \n FRANCE " )
2022-07-25 16:27:26 +02:00
end
end
context 'adress details' do
it 'service contains a numero_voie' do
2023-05-30 16:27:20 +02:00
expect ( subject [ :numero_voie ] ) . to eq ( '22' )
2022-07-25 16:27:26 +02:00
end
it 'service contains a type_voie' do
2023-05-30 16:27:20 +02:00
expect ( subject [ :type_voie ] ) . to eq ( 'RUE' )
2022-07-25 16:27:26 +02:00
end
it 'service contains a nom_voie' do
2023-05-30 16:27:20 +02:00
expect ( subject [ :nom_voie ] ) . to eq ( 'DE LA PAIX' )
2022-07-25 16:27:26 +02:00
end
it 'service contains a complement_adresse' do
2023-05-30 16:27:20 +02:00
expect ( subject [ :complement_adresse ] ) . to eq ( 'ZAE SAINT GUENAULT' )
2022-07-25 16:27:26 +02:00
end
it 'service contains a code_postal' do
2023-05-30 16:27:20 +02:00
expect ( subject [ :code_postal ] ) . to eq ( '75016' )
2022-07-25 16:27:26 +02:00
end
it 'service contains a localite' do
2023-05-30 16:27:20 +02:00
expect ( subject [ :localite ] ) . to eq ( 'PARIS 12' )
2022-07-25 16:27:26 +02:00
end
it 'service contains a code_insee_localite' do
2023-05-30 16:27:20 +02:00
expect ( subject [ :code_insee_localite ] ) . to eq ( '75112' )
2022-07-25 16:27:26 +02:00
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
2023-05-30 16:27:20 +02:00
stub_request ( :get , / https: \/ \/ entreprise.api.gouv.fr \/ v3 \/ insee \/ sirene \/ etablissements \/ #{ bad_siret } / )
2022-07-25 16:27:26 +02:00
. with ( query : hash_including ( { 'object' = > " service_id: #{ service . id } " } ) )
. to_return ( body : 'Fake body' , status : 404 )
end
it { expect ( subject ) . to eq ( { } ) }
end
end