diff --git a/app/controllers/champs/siret_controller.rb b/app/controllers/champs/siret_controller.rb index 1b1b33edd..fbf8d2495 100644 --- a/app/controllers/champs/siret_controller.rb +++ b/app/controllers/champs/siret_controller.rb @@ -17,7 +17,7 @@ class Champs::SiretController < ApplicationController begin etablissement = find_etablissement_with_siret - rescue APIEntreprise::API::Error::RequestFailed, APIEntreprise::API::Error::BadGateway, APIEntreprise::API::Error::TimedOut, APIEntreprise::API::Error::ServiceUnavailable + rescue APIEntreprise::API::Error::RequestFailed, APIEntreprise::API::Error::BadGateway, APIEntreprise::API::Error::TimedOut, APIEntreprise::API::Error::ServiceUnavailable, APIEntrepriseToken::TokenError # i18n-tasks-use t('errors.messages.siret_network_error') return siret_error(:network_error) end diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 170ebac13..a0e7b8f13 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -114,7 +114,7 @@ module Users sanitized_siret = siret_model.siret begin etablissement = APIEntrepriseService.create_etablissement(@dossier, sanitized_siret, current_user.id) - rescue APIEntreprise::API::Error::RequestFailed, APIEntreprise::API::Error::BadGateway, APIEntreprise::API::Error::TimedOut, APIEntreprise::API::Error::ServiceUnavailable + rescue APIEntreprise::API::Error::RequestFailed, APIEntreprise::API::Error::BadGateway, APIEntreprise::API::Error::TimedOut, APIEntreprise::API::Error::ServiceUnavailable, APIEntrepriseToken::TokenError return render_siret_error(t('errors.messages.siret_network_error')) end if etablissement.nil? diff --git a/app/lib/api_entreprise/api.rb b/app/lib/api_entreprise/api.rb index 297ad0669..9f92f5cad 100644 --- a/app/lib/api_entreprise/api.rb +++ b/app/lib/api_entreprise/api.rb @@ -82,7 +82,10 @@ class APIEntreprise::API end def self.call_with_siret(resource_name, siret_or_siren, procedure_id, user_id = nil) - return if APIEntrepriseToken.new(token_for_procedure(procedure_id)).expired? + if APIEntrepriseToken.new(token_for_procedure(procedure_id)).expired? + raise APIEntrepriseToken::TokenError, I18n.t("api_entreprise.errors.token_expired") + end + url = url(resource_name, siret_or_siren) params = params(siret_or_siren, procedure_id, user_id) diff --git a/config/locales/api_entreprise.en.yml b/config/locales/api_entreprise.en.yml index ee815c16e..e6c2fe6fa 100644 --- a/config/locales/api_entreprise.en.yml +++ b/config/locales/api_entreprise.en.yml @@ -2,3 +2,4 @@ en: api_entreprise: errors: missing_token: "the API Entreprise token cannot be blank" + token_expired: "API Entreprise token has expired." diff --git a/config/locales/api_entreprise.fr.yml b/config/locales/api_entreprise.fr.yml index f8d81a523..8c997879d 100644 --- a/config/locales/api_entreprise.fr.yml +++ b/config/locales/api_entreprise.fr.yml @@ -2,3 +2,4 @@ fr: api_entreprise: errors: missing_token: "le jeton API Entreprise ne peut ĂȘtre vide" + token_expired: "Le jeton API Entreprise a expirĂ©." diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 4fb36ec88..958fc04f4 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -271,7 +271,7 @@ describe Users::DossiersController, type: :controller do let(:api_etablissement_status) { 200 } let(:token_expired) { true } - it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown') + it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_network_error') end context 'when all API informations available' do diff --git a/spec/lib/api_entreprise/api_spec.rb b/spec/lib/api_entreprise/api_spec.rb index bb6d6aff1..03021f6f0 100644 --- a/spec/lib/api_entreprise/api_spec.rb +++ b/spec/lib/api_entreprise/api_spec.rb @@ -267,7 +267,7 @@ describe APIEntreprise::API do end it 'makes no call to api-entreprise' do - subject + expect { subject }.to raise_error(APIEntrepriseToken::TokenError) expect(WebMock).not_to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}/) end end