Fix Sentry 165 : api crash when siret is unknown

This commit is contained in:
Mathieu Magnin 2019-03-12 17:36:16 +01:00
parent 2b93c8903a
commit ff0356e698
3 changed files with 71 additions and 51 deletions

View file

@ -7,9 +7,9 @@ class ApiEntreprise::EntrepriseAdapter < ApiEntreprise::Adapter
end
def process_params
params = data_source[:entreprise].slice(*attr_to_fetch)
params = data_source[:entreprise]&.slice(*attr_to_fetch)
if valid_params?(params)
if params.present? && valid_params?(params)
params[:date_creation] = Time.zone.at(params[:date_creation]).to_datetime
params.transform_keys { |k| :"entreprise_#{k}" }
else

View file

@ -0,0 +1,6 @@
{
"errors": [
"Le siret ou siren indiqué n'existe pas, n'est pas connu ou ne comporte aucune information pour cet appel"
],
"gateway_error": true
}

View file

@ -8,9 +8,13 @@ describe ApiEntreprise::EntrepriseAdapter do
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=/)
.to_return(body: File.read('spec/fixtures/files/api_entreprise/entreprises.json', status: 200))
.to_return(body: body, status: status)
end
context "when SIRET is OK" do
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises.json') }
let(:status) { 200 }
it '#to_params class est une Hash ?' do
expect(subject).to be_an_instance_of(Hash)
end
@ -64,4 +68,14 @@ describe ApiEntreprise::EntrepriseAdapter do
expect(subject[:entreprise_prenom]).to eq('test_prenom')
end
end
end
context "when SIRET is KO" do
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_not_found.json') }
let(:status) { 206 }
it '#to_params class est une Hash ?' do
expect(subject).to eq({})
end
end
end