2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-12-07 15:10:26 +01:00
|
|
|
RSpec.describe Cron::DiscardedDossiersDeletionJob, type: :job do
|
2020-12-16 14:35:55 +01:00
|
|
|
describe '#perform' do
|
|
|
|
let(:instructeur) { create(:instructeur) }
|
2021-01-28 13:53:18 +01:00
|
|
|
let(:dossier) { create(:dossier, :with_individual, state) }
|
2024-06-04 17:52:01 +02:00
|
|
|
let(:dossier_2) { create(:dossier, :with_individual, state) }
|
2020-11-26 15:13:32 +01:00
|
|
|
|
2020-12-16 14:35:55 +01:00
|
|
|
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)
|
2022-03-09 10:29:16 +01:00
|
|
|
dossier.update_columns(hidden_by_user_at: hidden_at, hidden_by_administration_at: hidden_at)
|
2022-02-08 11:19:50 +01:00
|
|
|
dossier.update_column(:hidden_by_reason, "user_request")
|
2024-07-17 15:42:26 +02:00
|
|
|
dossier_2.update_columns(hidden_by_expired_at: hidden_at)
|
2024-06-04 17:52:01 +02:00
|
|
|
dossier_2.update_column(:hidden_by_reason, "expired")
|
2024-01-17 13:15:42 +01:00
|
|
|
end
|
2020-11-26 15:13:32 +01:00
|
|
|
|
2024-01-17 13:15:42 +01:00
|
|
|
subject do
|
2020-12-07 15:10:26 +01:00
|
|
|
Cron::DiscardedDossiersDeletionJob.perform_now
|
2020-12-16 14:35:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def operations_left
|
|
|
|
DossierOperationLog.where(dossier_id: dossier.id).pluck(:operation)
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.shared_examples "does not delete" do
|
2024-01-17 13:15:42 +01:00
|
|
|
before { subject }
|
|
|
|
|
2020-12-16 14:35:55 +01:00
|
|
|
it 'does not delete it' do
|
|
|
|
expect { dossier.reload }.not_to raise_error
|
2024-06-04 17:52:01 +02:00
|
|
|
expect { dossier_2.reload }.not_to raise_error
|
2020-12-16 14:35:55 +01:00
|
|
|
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
|
2024-01-17 13:15:42 +01:00
|
|
|
before { subject }
|
|
|
|
|
2020-12-16 14:35:55 +01:00
|
|
|
it 'does delete it' do
|
|
|
|
expect { dossier.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
2024-06-04 17:52:01 +02:00
|
|
|
expect { dossier_2.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
2020-12-16 14:35:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes its operations logs except supprimer' do
|
|
|
|
expect(operations_left).to eq(["supprimer"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-28 13:53:18 +01:00
|
|
|
[:en_construction, :en_instruction, :accepte, :refuse, :sans_suite].each do |state|
|
2020-12-16 14:35:55 +01:00
|
|
|
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
|
2020-11-26 15:13:32 +01:00
|
|
|
end
|
2024-01-17 13:15:42 +01:00
|
|
|
|
|
|
|
context "error on error" do
|
|
|
|
let(:state) { :en_construction }
|
|
|
|
let(:hidden_at) { 1.month.ago }
|
|
|
|
let(:failing_dossier) { create(:dossier, :en_construction, hidden_by_user_at: 5.weeks.ago, hidden_by_reason: "user_request") }
|
|
|
|
|
|
|
|
before do
|
|
|
|
failing_dossier.update_column(:hidden_by_reason, nil) # recurrent error previously causing job to crash
|
|
|
|
expect(Sentry).to receive(:capture_exception).with(instance_of(KeyError), extra: { dossier: failing_dossier.id })
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "does delete"
|
|
|
|
end
|
2020-11-26 15:13:32 +01:00
|
|
|
end
|
|
|
|
end
|