2022-07-25 17:37:02 +02:00
RSpec . describe APIEntreprise :: ServiceJob , type : :job do
2023-05-30 16:27:20 +02:00
let ( :siret ) { '30613890001294' }
2022-07-25 17:37:02 +02:00
let ( :service ) { create ( :service , siret : siret ) }
let ( :entreprise_body ) { File . read ( 'spec/fixtures/files/api_entreprise/etablissements.json' ) }
let ( :geocoder_body ) { File . read ( 'spec/fixtures/files/api_address/address.json' ) }
let ( :status ) { 200 }
2023-05-30 16:27:20 +02:00
let ( :adresse ) { " 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 17:37:02 +02:00
before do
2023-05-30 16:27:20 +02:00
stub_request ( :get , %r{ https://entreprise.api.gouv.fr/v3 \ /insee \ /sirene \ /etablissements \ / #{ siret } } )
2022-07-25 17:37:02 +02:00
. to_return ( body : entreprise_body , status : status )
allow_any_instance_of ( APIEntrepriseToken ) . to receive ( :expired? ) . and_return ( false )
Geocoder . configure ( lookup : :ban_data_gouv_fr , use_https : true )
stub_request ( :get , " https://api-adresse.data.gouv.fr/search/?q= #{ adresse } " )
. to_return ( body : geocoder_body , status : status )
end
after do
Geocoder . configure ( lookup : :test )
end
subject { described_class . new . perform ( service . id ) }
it " update service with address " do
subject
infos = service . reload . etablissement_infos
expect ( infos ) . not_to be_empty
expect ( infos [ " adresse " ] ) . to eq ( adresse )
2023-05-30 16:27:20 +02:00
expect ( infos [ " numero_voie " ] ) . to eq ( " 22 " )
expect ( infos [ " code_postal " ] ) . to eq ( " 75016 " )
expect ( infos [ " localite " ] ) . to eq ( " PARIS 12 " )
2022-07-25 17:37:02 +02:00
end
it " geocode address " do
subject
service . reload
expect ( service . etablissement_lat ) . to eq ( 48 . 859 )
expect ( service . etablissement_lng ) . to eq ( 2 . 347 )
end
context " errors responses " do
it " clear attributes when no address match " do
2023-05-30 16:27:20 +02:00
stub_request ( :get , %r{ https://entreprise.api.gouv.fr/v3 \ /insee \ /sirene \ /etablissements \ / #{ siret } } )
2022-07-25 17:37:02 +02:00
. to_return ( body : " {} " , status : 404 )
subject
service . reload
expect ( service . etablissement_infos ) . to be_empty
expect ( service . etablissement_lat ) . to be_nil
expect ( service . etablissement_lng ) . to be_nil
end
it " supports empty geocode result " do
geocoder_response = JSON . parse ( geocoder_body )
geocoder_response [ " features " ] = [ ]
stub_request ( :get , " https://api-adresse.data.gouv.fr/search/?q= #{ adresse } " )
. to_return ( body : JSON . generate ( geocoder_response ) , status : status )
subject
service . reload
expect ( service . etablissement_lat ) . to be_nil
expect ( service . etablissement_lng ) . to be_nil
end
end
end