From 55c9dfc001d8b6ad6eb90ae4295805f7dcd63e9c Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 12 Jun 2019 09:39:49 +0000 Subject: [PATCH] api_entreprise: display better error message when SIRET is private Previously we blamed the network. Now we say that the entreprise infos cannot be retrieved. Which is not ideal, but better. --- app/lib/api_entreprise/api.rb | 2 +- .../files/api_entreprise/entreprises_private.json | 6 ++++++ spec/lib/api_entreprise/api_spec.rb | 12 +++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/files/api_entreprise/entreprises_private.json diff --git a/app/lib/api_entreprise/api.rb b/app/lib/api_entreprise/api.rb index a84dd8c36..f90abd6e9 100644 --- a/app/lib/api_entreprise/api.rb +++ b/app/lib/api_entreprise/api.rb @@ -34,7 +34,7 @@ class ApiEntreprise::API if response.success? JSON.parse(response.body, symbolize_names: true) - elsif response.code == 404 || response.code == 422 + elsif response.code&.between?(401, 499) raise RestClient::ResourceNotFound else raise RestClient::RequestFailed diff --git a/spec/fixtures/files/api_entreprise/entreprises_private.json b/spec/fixtures/files/api_entreprise/entreprises_private.json new file mode 100644 index 000000000..0b77b10ee --- /dev/null +++ b/spec/fixtures/files/api_entreprise/entreprises_private.json @@ -0,0 +1,6 @@ +{ + "errors": [ + "Le SIREN ou SIRET est non diffusable" + ], + "gateway_error": true +} diff --git a/spec/lib/api_entreprise/api_spec.rb b/spec/lib/api_entreprise/api_spec.rb index f2beb720f..abd7c07c2 100644 --- a/spec/lib/api_entreprise/api_spec.rb +++ b/spec/lib/api_entreprise/api_spec.rb @@ -31,7 +31,17 @@ describe ApiEntreprise::API do end end - context 'when siret exist' do + context 'when siren infos are private' do + let(:siren) { '111111111' } + let(:status) { 403 } + let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_private.json') } + + it 'raises RestClient::ResourceNotFound' do + expect { subject }.to raise_error(RestClient::ResourceNotFound) + end + end + + context 'when siren exist' do let(:siren) { '418166096' } let(:status) { 200 } let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises.json') }