2020-08-05 18:40:47 +02:00
|
|
|
describe APIEntrepriseService do
|
2022-09-16 00:32:17 +02:00
|
|
|
shared_examples 'schedule fetch of all etablissement params' do
|
|
|
|
[
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-15 17:15:52 +02:00
|
|
|
describe '#create_etablissement' do
|
2019-05-03 17:24:42 +02:00
|
|
|
before do
|
2020-12-10 15:28:39 +01:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/)
|
2019-05-03 17:24:42 +02:00
|
|
|
.to_return(body: etablissements_body, status: etablissements_status)
|
2023-05-25 11:15:26 +02:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siret[0..8]}/)
|
2023-02-02 09:59:59 +01:00
|
|
|
.to_return(body: entreprises_body, status: entreprises_status)
|
2019-05-03 17:24:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:siret) { '41816609600051' }
|
2023-05-25 11:15:26 +02:00
|
|
|
let(:raison_sociale) { "DIRECTION INTERMINISTERIELLE DU NUMERIQUE" }
|
2019-05-03 17:24:42 +02:00
|
|
|
let(:etablissements_status) { 200 }
|
|
|
|
let(:etablissements_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') }
|
2023-02-02 09:59:59 +01:00
|
|
|
let(:entreprises_status) { 200 }
|
|
|
|
let(:entreprises_body) { File.read('spec/fixtures/files/api_entreprise/entreprises.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) }
|
2020-08-05 18:40:47 +02:00
|
|
|
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-08-05 18:40:47 +02:00
|
|
|
allow_any_instance_of(APIEntrepriseToken).to receive(:roles).and_return([])
|
|
|
|
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
|
|
|
|
|
2023-02-02 09:59:59 +01:00
|
|
|
it 'should fetch entreprise params' do
|
|
|
|
expect(subject[:entreprise_raison_sociale]).to eq(raison_sociale)
|
|
|
|
end
|
|
|
|
|
2022-09-16 00:32:17 +02:00
|
|
|
it_behaves_like 'schedule fetch of all etablissement params'
|
2019-05-03 17:24:42 +02:00
|
|
|
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-08-05 18:40:47 +02: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
|
2022-09-15 17:49:57 +02:00
|
|
|
|
2022-09-16 00:32:17 +02:00
|
|
|
describe '#create_etablissement_as_degraded_mode' do
|
|
|
|
let(:siret) { '41816609600051' }
|
|
|
|
let(:procedure) { create(:procedure) }
|
|
|
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
|
|
|
let(:user_id) { 12 }
|
|
|
|
|
|
|
|
subject(:etablissement) { APIEntrepriseService.create_etablissement_as_degraded_mode(dossier, siret, user_id) }
|
|
|
|
|
|
|
|
it 'should create an etablissement with minimumal attributes' do
|
|
|
|
etablissement = subject
|
|
|
|
|
|
|
|
expect(etablissement.siret).to eq(siret)
|
|
|
|
expect(etablissement).to be_as_degraded_mode
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'schedule fetch of all etablissement params'
|
|
|
|
end
|
|
|
|
|
2022-09-15 17:49:57 +02:00
|
|
|
describe "#api_up?" do
|
|
|
|
subject { described_class.api_up? }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read }
|
2022-09-15 17:49:57 +02:00
|
|
|
let(:status) { 200 }
|
|
|
|
|
|
|
|
before do
|
2023-04-27 17:17:38 +02:00
|
|
|
stub_request(:get, "https://status.entreprise.api.gouv.fr/summary.json")
|
2022-09-15 17:49:57 +02:00
|
|
|
.to_return(body: body, status: status)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when api etablissement is up" do
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when api entreprise is down" do
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read.gsub('UP', 'HASISSUES') }
|
2022-09-15 17:49:57 +02:00
|
|
|
|
|
|
|
it "returns false" do
|
2023-04-27 17:17:38 +02:00
|
|
|
expect(subject).to be_falsey
|
2022-09-15 17:49:57 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when api entreprise status is unknown" do
|
|
|
|
let(:body) { "" }
|
|
|
|
let(:status) { 0 }
|
|
|
|
|
|
|
|
it "returns nil" do
|
2023-04-27 17:17:38 +02:00
|
|
|
expect(subject).to be_falsey
|
2022-09-15 17:49:57 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-05-03 17:24:42 +02:00
|
|
|
end
|