diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index cb10c08b7..10c590a14 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -111,6 +111,9 @@ FactoryBot.define do end end + trait :brouillon do + end + trait :en_construction do after(:create) do |dossier, _evaluator| dossier.state = Dossier.states.fetch(:en_construction) diff --git a/spec/jobs/cron/discarded_dossiers_deletion_job_spec.rb b/spec/jobs/cron/discarded_dossiers_deletion_job_spec.rb index 95e358427..61078f168 100644 --- a/spec/jobs/cron/discarded_dossiers_deletion_job_spec.rb +++ b/spec/jobs/cron/discarded_dossiers_deletion_job_spec.rb @@ -1,33 +1,68 @@ RSpec.describe Cron::DiscardedDossiersDeletionJob, type: :job do - include ActiveJob::TestHelper + describe '#perform' do + let(:instructeur) { create(:instructeur) } + let(:dossier) { create(:dossier, state, hidden_at: hidden_at) } - let(:instructeur) { create(:instructeur) } - let!(:dossier_brouillon) { create(:dossier) } - let!(:dossier) { create(:dossier, :en_construction) } - - let!(:discarded_dossier_brouillon) { create(:dossier, hidden_at: 2.weeks.ago) } - let!(:discarded_dossier_en_construction) { create(:dossier, :en_construction, hidden_at: 2.weeks.ago) } - let!(:discarded_dossier_termine) { create(:dossier, :accepte, hidden_at: 2.weeks.ago) } - let!(:discarded_dossier_termine_today) { create(:dossier, :accepte, hidden_at: 1.hour.ago) } - - before do - discarded_dossier_en_construction.send(:log_dossier_operation, instructeur, :passer_en_instruction, discarded_dossier_en_construction) - discarded_dossier_termine.send(:log_dossier_operation, instructeur, :passer_en_instruction, discarded_dossier_termine) - discarded_dossier_termine_today.send(:log_dossier_operation, instructeur, :passer_en_instruction, discarded_dossier_termine_today) - discarded_dossier_en_construction.send(:log_dossier_operation, instructeur, :supprimer, discarded_dossier_en_construction) - discarded_dossier_termine.send(:log_dossier_operation, instructeur, :supprimer, discarded_dossier_termine) - discarded_dossier_termine_today.send(:log_dossier_operation, instructeur, :supprimer, discarded_dossier_termine_today) - end - - context 'cleanup discared dossiers' do - it 'delete dossiers and operation logs' do - expect(Dossier.with_discarded.count).to eq(6) - expect(DossierOperationLog.count).to eq(6) + before do + # hack to add passer_en_instruction and supprimer to dossier.dossier_operation_logs + dossier.send(:log_dossier_operation, instructeur, :passer_en_instruction, dossier) + dossier.send(:log_dossier_operation, instructeur, :supprimer, dossier) Cron::DiscardedDossiersDeletionJob.perform_now + end - expect(Dossier.with_discarded.count).to eq(3) - expect(DossierOperationLog.count).to eq(4) + def operations_left + DossierOperationLog.where(dossier_id: dossier.id).pluck(:operation) + end + + RSpec.shared_examples "does not delete" do + it 'does not delete it' do + expect { dossier.reload }.not_to raise_error + end + + it 'does not delete its operations logs' do + expect(operations_left).to match_array(["passer_en_instruction", "supprimer"]) + end + end + + RSpec.shared_examples "does delete" do + it 'does delete it' do + expect { dossier.reload }.to raise_error(ActiveRecord::RecordNotFound) + end + + it 'deletes its operations logs except supprimer' do + expect(operations_left).to eq(["supprimer"]) + end + end + + [:brouillon, :en_construction, :en_instruction, :accepte, :refuse, :sans_suite].each do |state| + context "with a dossier #{state}" do + let(:state) { state } + + context 'not hidden' do + let(:hidden_at) { nil } + + include_examples "does not delete" + end + + context 'hidden not so long ago' do + let(:hidden_at) { 1.week.ago + 1.hour } + + include_examples "does not delete" + end + end + end + + [:en_construction, :accepte, :refuse, :sans_suite].each do |state| + context "with a dossier #{state}" do + let(:state) { state } + + context 'hidden long ago' do + let(:hidden_at) { 1.week.ago - 1.hour } + + include_examples "does delete" + end + end end end end