2020-11-13 14:34:53 +01:00
|
|
|
RSpec.describe Cron::AutoArchiveProcedureJob, type: :job do
|
2019-08-06 11:02:54 +02:00
|
|
|
let!(:procedure) { create(:procedure, :published, :with_instructeur, auto_archive_on: nil) }
|
2023-06-01 14:47:06 +02:00
|
|
|
let!(:procedure_hier) { create(:procedure, :published, :with_instructeur) }
|
|
|
|
let!(:procedure_aujourdhui) { create(:procedure, :published, :with_instructeur) }
|
2019-12-02 21:27:05 +01:00
|
|
|
let!(:procedure_demain) { create(:procedure, :published, :with_instructeur, auto_archive_on: 1.day.from_now.to_date) }
|
2021-01-05 10:03:35 +01:00
|
|
|
let!(:job) { Cron::AutoArchiveProcedureJob.new }
|
2017-03-15 12:40:59 +01:00
|
|
|
|
2023-06-01 14:47:06 +02:00
|
|
|
before do
|
|
|
|
procedure_hier.auto_archive_on = 1.day.ago.to_date
|
|
|
|
procedure_hier.save!(validate: false)
|
|
|
|
procedure_aujourdhui.auto_archive_on = Date.current
|
|
|
|
procedure_aujourdhui.save!(validate: false)
|
|
|
|
end
|
|
|
|
|
2021-01-05 10:03:35 +01:00
|
|
|
subject { job.perform }
|
2017-03-15 12:40:59 +01:00
|
|
|
|
|
|
|
context "when procedures have no auto_archive_on" do
|
|
|
|
before do
|
|
|
|
subject
|
|
|
|
procedure.reload
|
|
|
|
end
|
|
|
|
|
2019-11-14 09:43:45 +01:00
|
|
|
it { expect(procedure.close?).to eq false }
|
2017-03-15 12:40:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when procedures have auto_archive_on set on yesterday or today" do
|
2022-12-14 18:15:57 +01:00
|
|
|
let!(:dossier1) { create(:dossier, procedure: procedure_hier) }
|
|
|
|
let!(:dossier2) { create(:dossier, :en_construction, procedure: procedure_hier) }
|
|
|
|
let!(:dossier3) { create(:dossier, :en_construction, procedure: procedure_hier) }
|
|
|
|
let!(:dossier4) { create(:dossier, :en_construction, procedure: procedure_hier) }
|
|
|
|
let!(:dossier5) { create(:dossier, :en_instruction, procedure: procedure_hier) }
|
|
|
|
let!(:dossier6) { create(:dossier, :accepte, procedure: procedure_hier) }
|
|
|
|
let!(:dossier7) { create(:dossier, :refuse, procedure: procedure_hier) }
|
|
|
|
let!(:dossier8) { create(:dossier, :sans_suite, procedure: procedure_hier) }
|
|
|
|
let!(:dossier9) { create(:dossier, :en_construction, procedure: procedure_aujourdhui) }
|
2019-05-02 16:22:16 +02:00
|
|
|
let(:last_operation) { dossier2.dossier_operation_logs.last }
|
2017-03-15 12:40:59 +01:00
|
|
|
|
2017-06-29 12:20:27 +02:00
|
|
|
before do
|
|
|
|
subject
|
2017-06-29 15:07:33 +02:00
|
|
|
|
2018-03-06 11:53:26 +01:00
|
|
|
[dossier1, dossier2, dossier3, dossier4, dossier5, dossier6, dossier7, dossier8, dossier9].each(&:reload)
|
2017-06-29 15:07:33 +02:00
|
|
|
|
2017-06-29 12:20:27 +02:00
|
|
|
procedure_hier.reload
|
|
|
|
procedure_aujourdhui.reload
|
2017-03-15 12:40:59 +01:00
|
|
|
end
|
2017-06-29 12:20:27 +02:00
|
|
|
|
2018-11-27 11:13:37 +01:00
|
|
|
it {
|
|
|
|
expect(dossier1.state).to eq Dossier.states.fetch(:brouillon)
|
|
|
|
expect(dossier2.state).to eq Dossier.states.fetch(:en_instruction)
|
2019-05-02 16:22:16 +02:00
|
|
|
expect(last_operation.operation).to eq('passer_en_instruction')
|
|
|
|
expect(last_operation.automatic_operation?).to be_truthy
|
2018-11-27 11:13:37 +01:00
|
|
|
expect(dossier3.state).to eq Dossier.states.fetch(:en_instruction)
|
|
|
|
expect(dossier4.state).to eq Dossier.states.fetch(:en_instruction)
|
|
|
|
expect(dossier5.state).to eq Dossier.states.fetch(:en_instruction)
|
|
|
|
expect(dossier6.state).to eq Dossier.states.fetch(:accepte)
|
|
|
|
expect(dossier7.state).to eq Dossier.states.fetch(:refuse)
|
|
|
|
expect(dossier8.state).to eq Dossier.states.fetch(:sans_suite)
|
|
|
|
expect(dossier9.state).to eq Dossier.states.fetch(:en_instruction)
|
|
|
|
}
|
|
|
|
|
|
|
|
it {
|
2019-11-14 09:43:45 +01:00
|
|
|
expect(procedure_hier.close?).to eq true
|
|
|
|
expect(procedure_aujourdhui.close?).to eq true
|
2018-11-27 11:13:37 +01:00
|
|
|
}
|
2017-03-15 12:40:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when procedures have auto_archive_on set on future" do
|
|
|
|
before do
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2019-11-14 09:43:45 +01:00
|
|
|
it { expect(procedure_demain.close?).to eq false }
|
2017-03-15 12:40:59 +01:00
|
|
|
end
|
2021-01-05 10:03:35 +01:00
|
|
|
|
|
|
|
context 'when an error occurs' do
|
2023-06-01 14:47:06 +02:00
|
|
|
let!(:buggy_procedure) { create(:procedure, :published, :with_instructeur) }
|
2021-01-05 10:03:35 +01:00
|
|
|
|
|
|
|
before do
|
2023-06-01 14:47:06 +02:00
|
|
|
buggy_procedure.auto_archive_on = 1.day.ago.to_date
|
|
|
|
buggy_procedure.save!(validate: false)
|
|
|
|
|
2021-01-05 10:03:35 +01:00
|
|
|
error = StandardError.new('nop')
|
|
|
|
expect(buggy_procedure).to receive(:close!).and_raise(error)
|
|
|
|
expect(job).to receive(:procedures_to_close).and_return([buggy_procedure, procedure_hier])
|
2021-01-28 14:49:22 +01:00
|
|
|
expect(Sentry).to receive(:capture_exception).with(error, extra: { procedure_id: buggy_procedure.id })
|
2021-01-05 10:03:35 +01:00
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should close all the procedure" do
|
|
|
|
expect(procedure_hier.reload.close?).to eq true
|
|
|
|
end
|
|
|
|
end
|
2017-03-15 12:40:59 +01:00
|
|
|
end
|