From 6a4a9c8e0321d4a4c3eeca14541285110a59430c Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Thu, 22 Sep 2022 16:26:14 +0200 Subject: [PATCH] fix(cron/procedure-declarative): don't auto accept a dossier still in degraded mode --- app/models/procedure.rb | 4 +- .../cron/declarative_procedures_job_spec.rb | 48 ++++++++++++++----- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 9c0a1aa65..89bd58036 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -640,7 +640,9 @@ class Procedure < ApplicationRecord dossiers .state_en_construction .where(declarative_triggered_at: nil) - .find_each(&:accepter_automatiquement!) + .find_each do |dossier| + dossier.accepter_automatiquement! if dossier.may_accepter_automatiquement? + end end end diff --git a/spec/jobs/cron/declarative_procedures_job_spec.rb b/spec/jobs/cron/declarative_procedures_job_spec.rb index 3263914d9..c1bc64f30 100644 --- a/spec/jobs/cron/declarative_procedures_job_spec.rb +++ b/spec/jobs/cron/declarative_procedures_job_spec.rb @@ -12,20 +12,23 @@ RSpec.describe Cron::DeclarativeProceduresJob, type: :job do let(:dossier_repasse_en_construction) { create(:dossier, :en_construction, :with_individual, procedure: procedure) } before do - Timecop.freeze(date) - dossier_repasse_en_construction.touch(:declarative_triggered_at) - dossiers = [ - nouveau_dossier1, - nouveau_dossier2, - dossier_recu, - dossier_brouillon, - dossier_repasse_en_construction - ] + Timecop.freeze(date) + dossier_repasse_en_construction&.touch(:declarative_triggered_at) + end - Cron::DeclarativeProceduresJob.new.perform + subject(:perform_job) do + dossiers = [ + nouveau_dossier1, + nouveau_dossier2, + dossier_recu, + dossier_brouillon, + dossier_repasse_en_construction + ].compact - dossiers.each(&:reload) - end + Cron::DeclarativeProceduresJob.new.perform + + dossiers.each(&:reload) + end after { Timecop.return } @@ -35,6 +38,7 @@ RSpec.describe Cron::DeclarativeProceduresJob, type: :job do let(:last_operation) { nouveau_dossier1.dossier_operation_logs.last } it { + perform_job expect(nouveau_dossier1.en_instruction?).to be_truthy expect(nouveau_dossier1.en_instruction_at).to eq(date) expect(last_operation.operation).to eq('passer_en_instruction') @@ -58,6 +62,7 @@ RSpec.describe Cron::DeclarativeProceduresJob, type: :job do let(:last_operation) { nouveau_dossier1.dossier_operation_logs.last } it { + perform_job expect(nouveau_dossier1.accepte?).to be true expect(nouveau_dossier1.en_instruction_at).to eq(date) expect(nouveau_dossier1.processed_at).to eq(date) @@ -78,6 +83,25 @@ RSpec.describe Cron::DeclarativeProceduresJob, type: :job do expect(dossier_brouillon.en_instruction_at).to eq(nil) expect(dossier_brouillon.processed_at).to eq(nil) } + + context "having etablissement in degraded_mode" do + let(:procedure) { create(:procedure, :published, :with_instructeur, for_individual: false, declarative_with_state: state) } + let(:nouveau_dossier1) { create(:dossier, :en_construction, :with_entreprise, :with_attestation, procedure: procedure, as_degraded_mode: false) } + let(:nouveau_dossier2) { create(:dossier, :en_construction, :with_entreprise, :with_attestation, procedure: procedure, as_degraded_mode: true) } + let(:dossier_recu) { nil } + let(:dossier_repasse_en_construction) { nil } + + before do + expect(nouveau_dossier2).to_not receive(:accepter_automatiquement) + expect(Sentry).to_not receive(:capture_exception) + end + + it { + perform_job + expect(nouveau_dossier1).to be_accepte + expect(nouveau_dossier2).to be_en_construction + } + end end end end