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

@ -9,6 +9,7 @@ describe ApiEntreprise::API do
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=#{token}/)
.to_return(status: status, body: body)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end
context 'when the service is unavailable' do
@ -78,6 +79,7 @@ describe ApiEntreprise::API do
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*non_diffusables=true&.*token=/)
.to_return(status: status, body: body)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end
context 'when siret does not exist' do
@ -105,6 +107,7 @@ describe ApiEntreprise::API do
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\/.*token=/)
.to_return(status: status, body: body)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end
context 'when siret does not exist' do
@ -136,6 +139,7 @@ describe ApiEntreprise::API do
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\/.*token=/)
.to_return(status: status, body: body)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end
subject { described_class.rna(siren, procedure_id) }
@ -167,6 +171,7 @@ describe ApiEntreprise::API do
before do
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(roles)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_sociales_acoss\/#{siren}?.*token=/)
.to_return(body: body, status: status)
end
@ -195,6 +200,7 @@ describe ApiEntreprise::API do
before do
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(roles)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_fiscales_dgfip\/#{siren}?.*token=#{token}&user_id=#{user_id}/)
.to_return(body: body, status: status)
end
@ -222,6 +228,7 @@ describe ApiEntreprise::API do
before do
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(roles)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/bilans_entreprises_bdf\/#{siren}?.*token=#{token}/)
.to_return(body: body, status: status)
end
@ -240,4 +247,18 @@ describe ApiEntreprise::API do
it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) }
end
end
describe 'with expired token' do
let(:siren) { '111111111' }
subject { described_class.entreprise(siren, procedure_id) }
before do
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(true)
end
it 'makes no call to api-entreprise' do
subject
expect(WebMock).not_to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=#{token}/)
end
end
end