make api entreprise call only if token not expired

This commit is contained in:
Christophe Robillard 2020-05-05 16:06:18 +02:00
parent f587e6600a
commit dbf04dd0d8
3 changed files with 34 additions and 1 deletions

View file

@ -230,6 +230,8 @@ describe Users::DossiersController, type: :controller do
let(:api_entreprise_bilans_bdf_status) { 200 }
let(:api_entreprise_bilans_bdf_body) { File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json') }
let(:token_expired) { false }
def stub_api_entreprise_requests
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*token=/)
.to_return(status: api_etablissement_status, body: api_etablissement_body)
@ -258,8 +260,9 @@ describe Users::DossiersController, type: :controller do
before do
sign_in(user)
stub_api_entreprise_requests
allow_any_instance_of(Procedure).to receive(:api_entreprise_roles)
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles)
.and_return(["attestations_fiscales", "attestations_sociales", "bilans_entreprise_bdf"])
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(token_expired)
end
before { Timecop.freeze(Time.zone.local(2020, 3, 14)) }
after { Timecop.return }
@ -316,6 +319,14 @@ describe Users::DossiersController, type: :controller do
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown')
end
context 'when default token has expired' do
let(:api_etablissement_status) { 200 }
let(:api_body_status) { '' }
let(:token_expired) { true }
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown')
end
context 'when the API returns no Entreprise' do
let(:api_entreprise_status) { 404 }
let(:api_entreprise_body) { '' }