2019-05-03 17:24:42 +02:00
|
|
|
describe ApiEntrepriseService do
|
2020-05-15 17:15:52 +02:00
|
|
|
describe '#create_etablissement' do
|
2019-05-03 17:24:42 +02:00
|
|
|
before do
|
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*token=/)
|
|
|
|
.to_return(body: etablissements_body, status: etablissements_status)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:siret) { '41816609600051' }
|
|
|
|
let(:etablissements_status) { 200 }
|
|
|
|
let(:etablissements_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') }
|
2020-07-08 17:00:21 +02:00
|
|
|
let(:valid_token) { "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" }
|
|
|
|
let(:procedure) { create(:procedure, api_entreprise_token: valid_token) }
|
2020-05-15 17:15:52 +02:00
|
|
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
|
|
|
let(:subject) { ApiEntrepriseService.create_etablissement(dossier, siret, procedure.id) }
|
2019-05-03 17:24:42 +02:00
|
|
|
|
2020-04-29 10:23:35 +02:00
|
|
|
before do
|
2020-05-15 17:15:52 +02:00
|
|
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return([])
|
2020-05-05 16:58:37 +02:00
|
|
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
2020-04-29 10:23:35 +02:00
|
|
|
end
|
|
|
|
|
2019-05-03 17:24:42 +02:00
|
|
|
context 'when service is up' do
|
|
|
|
it 'should fetch etablissement params' do
|
2020-05-15 17:15:52 +02:00
|
|
|
expect(subject[:siret]).to eq(siret)
|
2019-05-03 17:24:42 +02:00
|
|
|
end
|
|
|
|
|
2020-05-15 17:15:52 +02:00
|
|
|
[
|
|
|
|
ApiEntreprise::EntrepriseJob, ApiEntreprise::AssociationJob, ApiEntreprise::ExercicesJob,
|
|
|
|
ApiEntreprise::EffectifsJob, ApiEntreprise::EffectifsAnnuelsJob, ApiEntreprise::AttestationSocialeJob,
|
|
|
|
ApiEntreprise::BilansBdfJob
|
|
|
|
].each do |job|
|
|
|
|
it "should enqueue #{job.class.name}" do
|
|
|
|
expect { subject }.to have_enqueued_job(job)
|
|
|
|
end
|
2019-05-03 17:24:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when etablissement api down' do
|
2020-05-15 17:15:52 +02:00
|
|
|
let(:etablissements_status) { 504 }
|
2019-05-03 17:24:42 +02:00
|
|
|
let(:etablissements_body) { '' }
|
|
|
|
|
2020-12-03 16:52:58 +01:00
|
|
|
it 'should raise ApiEntreprise::API::Error::RequestFailed' do
|
|
|
|
expect { subject }.to raise_error(ApiEntreprise::API::Error::RequestFailed)
|
2019-05-03 17:24:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when etablissement not found' do
|
|
|
|
let(:etablissements_status) { 404 }
|
|
|
|
let(:etablissements_body) { '' }
|
|
|
|
|
|
|
|
it 'should return nil' do
|
2020-05-15 17:15:52 +02:00
|
|
|
expect(subject).to be_nil
|
2019-05-03 17:24:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|