2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-11-19 06:03:59 +01:00
|
|
|
describe BatchOperationProcessOneJob, type: :job do
|
2022-11-18 16:59:46 +01:00
|
|
|
describe 'perform' do
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :archiver,
|
|
|
|
options.merge(instructeur: create(:instructeur)))
|
|
|
|
end
|
|
|
|
let(:dossier_job) { batch_operation.dossiers.first }
|
2022-11-19 06:03:59 +01:00
|
|
|
subject { BatchOperationProcessOneJob.new(batch_operation, dossier_job) }
|
2022-11-18 16:59:46 +01:00
|
|
|
let(:options) { {} }
|
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
it 'when it works' do
|
|
|
|
allow_any_instance_of(BatchOperation).to receive(:process_one).with(dossier_job).and_return(true)
|
2022-11-18 16:59:46 +01:00
|
|
|
expect { subject.perform_now }
|
2022-12-22 22:23:47 +01:00
|
|
|
.to change { batch_operation.dossier_operations.success.pluck(:dossier_id) }
|
2022-11-25 15:43:00 +01:00
|
|
|
.from([])
|
|
|
|
.to([dossier_job.id])
|
2022-11-18 16:59:46 +01:00
|
|
|
end
|
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
it 'when it fails for an "unknown" reason' do
|
|
|
|
allow_any_instance_of(BatchOperation).to receive(:process_one).with(dossier_job).and_raise("boom")
|
|
|
|
expect { subject.perform_now }.to raise_error('boom')
|
2022-11-18 16:59:46 +01:00
|
|
|
|
2022-12-22 22:23:47 +01:00
|
|
|
expect(batch_operation.dossier_operations.error.pluck(:dossier_id)).to eq([dossier_job.id])
|
2022-11-18 16:59:46 +01:00
|
|
|
end
|
|
|
|
|
2022-12-12 10:02:33 +01:00
|
|
|
context 'when operation is "archiver"' do
|
|
|
|
it 'archives the dossier in the batch' do
|
|
|
|
expect { subject.perform_now }
|
2022-12-12 10:30:16 +01:00
|
|
|
.to change { dossier_job.reload.archived? }
|
|
|
|
.from(false)
|
|
|
|
.to(true)
|
2022-12-12 10:02:33 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-29 22:36:55 +01:00
|
|
|
context 'when operation is "desarchiver"' do
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :desarchiver,
|
|
|
|
options.merge(instructeur: create(:instructeur)))
|
|
|
|
end
|
|
|
|
it 'archives the dossier in the batch' do
|
|
|
|
expect { subject.perform_now }
|
|
|
|
.to change { dossier_job.reload.archived? }
|
|
|
|
.from(true)
|
|
|
|
.to(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-12 10:02:33 +01:00
|
|
|
context 'when operation is "passer_en_instruction"' do
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :passer_en_instruction,
|
|
|
|
options.merge(instructeur: create(:instructeur)))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'changes the dossier to en_instruction in the batch' do
|
|
|
|
expect { subject.perform_now }
|
2022-12-12 10:30:16 +01:00
|
|
|
.to change { dossier_job.reload.en_instruction? }
|
|
|
|
.from(false)
|
|
|
|
.to(true)
|
2022-12-12 10:02:33 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-30 16:11:04 +01:00
|
|
|
context 'when operation is "repousser_expiration"' do
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :repousser_expiration,
|
|
|
|
options.merge(instructeur: create(:instructeur)))
|
|
|
|
end
|
|
|
|
it 'archives the dossier in the batch' do
|
|
|
|
expect { subject.perform_now }
|
|
|
|
.to change { dossier_job.reload.conservation_extension }
|
|
|
|
.from(dossier_job.conservation_extension)
|
|
|
|
.to(dossier_job.conservation_extension + 1.month)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-04 11:51:22 +01:00
|
|
|
context 'when operation is "follow"' do
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :follow,
|
|
|
|
options.merge(instructeur: create(:instructeur)))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds a follower to the dossier' do
|
|
|
|
expect { subject.perform_now }
|
|
|
|
.to change { dossier_job.reload.follows }
|
|
|
|
.from([])
|
|
|
|
.to(anything)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-13 15:09:24 +01:00
|
|
|
context 'when operation is "unfollow"' do
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :unfollow,
|
|
|
|
options.merge(instructeur: create(:instructeur)))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes a follower to the dossier' do
|
|
|
|
expect { subject.perform_now }
|
2023-01-16 10:16:23 +01:00
|
|
|
.to change { dossier_job.reload.follows.count }
|
|
|
|
.from(1)
|
|
|
|
.to(0)
|
2023-01-13 15:09:24 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-11 11:05:19 +01:00
|
|
|
context 'when operation is "repasser en construction"' do
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :repasser_en_construction,
|
|
|
|
options.merge(instructeur: create(:instructeur)))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'changed the dossier to en construction' do
|
|
|
|
expect { subject.perform_now }
|
|
|
|
.to change { dossier_job.reload.en_construction? }
|
|
|
|
.from(false)
|
|
|
|
.to(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-15 17:35:50 +01:00
|
|
|
context 'when operation is "accepter"' do
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :accepter,
|
2022-12-16 17:38:32 +01:00
|
|
|
options.merge(instructeur: create(:instructeur), motivation: 'motivation'))
|
2022-12-15 17:35:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts the dossier in the batch' do
|
|
|
|
expect { subject.perform_now }
|
|
|
|
.to change { dossier_job.reload.accepte? }
|
|
|
|
.from(false)
|
|
|
|
.to(true)
|
|
|
|
end
|
2022-12-16 17:38:32 +01:00
|
|
|
|
|
|
|
it 'accepts the dossier in the batch with a motivation' do
|
|
|
|
expect { subject.perform_now }
|
|
|
|
.to change { dossier_job.reload.motivation }
|
|
|
|
.from(nil)
|
|
|
|
.to('motivation')
|
|
|
|
end
|
2022-12-22 14:22:09 +01:00
|
|
|
|
|
|
|
context 'when it raises a ActiveRecord::StaleObjectError' do
|
|
|
|
before { allow_any_instance_of(Dossier).to receive(:after_accepter).and_raise(ActiveRecord::StaleObjectError) }
|
|
|
|
|
|
|
|
it 'with invalid dossier (ex: ActiveRecord::StaleObjectError), unlink dossier/batch_operation with update_column' do
|
|
|
|
scope = double
|
|
|
|
expect(scope).to receive(:find).with(dossier_job.id).and_return(dossier_job)
|
|
|
|
expect_any_instance_of(BatchOperation).to receive(:dossiers_safe_scope).and_return(scope)
|
|
|
|
dossier_job.errors.add('KC')
|
|
|
|
|
|
|
|
expect do
|
|
|
|
begin
|
|
|
|
subject.perform_now
|
|
|
|
rescue ActiveRecord::StaleObjectError
|
|
|
|
# noop, juste want to catch existing error but not others
|
|
|
|
end
|
|
|
|
end.to change { dossier_job.reload.batch_operation }.from(batch_operation).to(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not change dossier state' do
|
|
|
|
expect do
|
|
|
|
begin
|
|
|
|
subject.perform_now
|
|
|
|
rescue ActiveRecord::StaleObjectError
|
|
|
|
# noop, juste want to catch existing error but not others
|
|
|
|
end
|
|
|
|
end.not_to change { dossier_job.reload.accepte? }
|
|
|
|
end
|
|
|
|
end
|
2022-12-15 17:35:50 +01:00
|
|
|
end
|
|
|
|
|
2022-12-19 15:03:39 +01:00
|
|
|
context 'when operation is "accepter" with justificatif' do
|
|
|
|
let(:fake_justificatif) { ActiveStorage::Blob.create_and_upload!(io: StringIO.new("ma justification"), filename: 'piece_justificative_0.pdf', content_type: 'application/pdf') }
|
|
|
|
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :accepter,
|
|
|
|
options.merge(instructeur: create(:instructeur), motivation: 'motivation', justificatif_motivation: fake_justificatif.signed_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts the dossier in the batch with a justificatif' do
|
|
|
|
expect { subject.perform_now }
|
|
|
|
.to change { dossier_job.reload.justificatif_motivation.filename }
|
|
|
|
.from(nil)
|
|
|
|
.to(fake_justificatif.filename)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-04 11:40:11 +02:00
|
|
|
context 'when operation is "refuser"' do
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :refuser,
|
|
|
|
options.merge(instructeur: create(:instructeur), motivation: 'motivation'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'refuses the dossier in the batch' do
|
|
|
|
expect { subject.perform_now }
|
|
|
|
.to change { dossier_job.reload.refuse? }
|
|
|
|
.from(false)
|
|
|
|
.to(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'refuses the dossier in the batch with a motivation' do
|
|
|
|
expect { subject.perform_now }
|
|
|
|
.to change { dossier_job.reload.motivation }
|
|
|
|
.from(nil)
|
|
|
|
.to('motivation')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-29 19:40:51 +01:00
|
|
|
context 'when operation is "restaurer"' do
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :restaurer,
|
|
|
|
options.merge(instructeur: create(:instructeur)))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'changed the dossier to en construction' do
|
|
|
|
expect { subject.perform_now }
|
|
|
|
.to change { dossier_job.reload.hidden_by_administration? }
|
|
|
|
.from(true)
|
|
|
|
.to(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-04 11:40:11 +02:00
|
|
|
context 'when operation is "classer_sans_suite"' do
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :classer_sans_suite,
|
|
|
|
options.merge(instructeur: create(:instructeur), motivation: 'motivation'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'closes without continuation the dossier in the batch' do
|
|
|
|
expect { subject.perform_now }
|
|
|
|
.to change { dossier_job.reload.sans_suite? }
|
|
|
|
.from(false)
|
|
|
|
.to(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'closes without continuation the dossier in the batch with a motivation' do
|
|
|
|
expect { subject.perform_now }
|
|
|
|
.to change { dossier_job.reload.motivation }
|
|
|
|
.from(nil)
|
|
|
|
.to('motivation')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-16 10:30:16 +01:00
|
|
|
context 'when operation is "supprimer"' do
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :supprimer,
|
|
|
|
options.merge(instructeur: create(:instructeur)))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'changed the dossier to en construction' do
|
|
|
|
expect { subject.perform_now }
|
|
|
|
.to change { dossier_job.reload.hidden_by_administration? }
|
|
|
|
.from(false)
|
|
|
|
.to(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
context 'when the dossier is out of sync (ie: someone applied a transition somewhere we do not know)' do
|
|
|
|
let(:instructeur) { create(:instructeur) }
|
|
|
|
let(:procedure) { create(:simple_procedure, instructeurs: [instructeur]) }
|
|
|
|
let(:dossier) { create(:dossier, :accepte, :with_individual, archived: true, procedure: procedure) }
|
|
|
|
let(:batch_operation) { create(:batch_operation, operation: :archiver, instructeur: instructeur, dossiers: [dossier]) }
|
2022-11-18 16:59:46 +01:00
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
it 'does run process_one' do
|
|
|
|
allow(batch_operation).to receive(:process_one).and_raise("should have been prevented")
|
|
|
|
subject.perform_now
|
2022-11-18 16:59:46 +01:00
|
|
|
end
|
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
it 'when it fails from dossiers_safe_scope.find' do
|
|
|
|
scope = double
|
|
|
|
expect(scope).to receive(:find).with(dossier_job.id).and_raise(ActiveRecord::RecordNotFound)
|
|
|
|
expect_any_instance_of(BatchOperation).to receive(:dossiers_safe_scope).and_return(scope)
|
2022-11-18 16:59:46 +01:00
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
subject.perform_now
|
|
|
|
|
|
|
|
expect(batch_operation.reload.failed_dossier_ids).to eq([])
|
|
|
|
expect(batch_operation.dossiers).not_to include(dossier_job)
|
2022-11-18 16:59:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|