From 6daa31d410632ac85ac308dad5a76f5268f175c9 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 23 Jul 2018 12:44:42 +0000 Subject: [PATCH 1/2] spec: add tests for Dossier#delete_and_keep_track --- spec/models/dossier_spec.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 829009f61..b6106cca8 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -806,6 +806,34 @@ describe Dossier do end end + describe "#delete_and_keep_track" do + let(:dossier) { create(:dossier) } + let(:deleted_dossier) { DeletedDossier.find_by!(dossier_id: dossier.id) } + + before do + allow(DossierMailer).to receive(:notify_deletion_to_user).and_return(double(deliver_later: nil)) + allow(DossierMailer).to receive(:notify_deletion_to_administration).and_return(double(deliver_later: nil)) + end + + subject! { dossier.delete_and_keep_track } + + it 'hides the dossier' do + expect(dossier.hidden_at).to be_present + end + + it 'creates a DeletedDossier record' do + expect(deleted_dossier.dossier_id).to eq dossier.id + expect(deleted_dossier.procedure).to eq dossier.procedure + expect(deleted_dossier.state).to eq dossier.state + expect(deleted_dossier.deleted_at).to be_present + end + + it 'sends notification emails' do + expect(DossierMailer).to have_received(:notify_deletion_to_user).with(deleted_dossier, dossier.user.email) + expect(DossierMailer).to have_received(:notify_deletion_to_administration).with(deleted_dossier, dossier.procedure.administrateur.email) + end + end + describe 'webhook' do let(:dossier) { create(:dossier) } From 13182ad291f0796e0d5bf47f50ae177067e845cd Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 23 Jul 2018 13:06:06 +0000 Subject: [PATCH 2/2] dossier: send deletion email also to the following gestionnaires --- app/models/dossier.rb | 6 +++++- spec/models/dossier_spec.rb | 24 ++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 1cf543a63..f91a0e245 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -276,8 +276,12 @@ class Dossier < ApplicationRecord now = Time.now deleted_dossier = DeletedDossier.create!(dossier_id: id, procedure: procedure, state: state, deleted_at: now) update(hidden_at: now) + + administration_emails = followers_gestionnaires.present? ? followers_gestionnaires.pluck(:email) : [procedure.administrateur.email] + administration_emails.each do |email| + DossierMailer.notify_deletion_to_administration(deleted_dossier, email).deliver_later + end DossierMailer.notify_deletion_to_user(deleted_dossier, user.email).deliver_later - DossierMailer.notify_deletion_to_administration(deleted_dossier, procedure.administrateur.email).deliver_later end private diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index b6106cca8..dc7bc7999 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -828,9 +828,29 @@ describe Dossier do expect(deleted_dossier.deleted_at).to be_present end - it 'sends notification emails' do + it 'notifies the user' do expect(DossierMailer).to have_received(:notify_deletion_to_user).with(deleted_dossier, dossier.user.email) - expect(DossierMailer).to have_received(:notify_deletion_to_administration).with(deleted_dossier, dossier.procedure.administrateur.email) + end + + context 'where gestionnaires are following the dossier' do + let(:dossier) { create(:dossier, :followed) } + let!(:non_following_gestionnaire) do + non_following_gestionnaire = create(:gestionnaire) + non_following_gestionnaire.procedures << dossier.procedure + non_following_gestionnaire + end + + it 'notifies the following gestionnaires' do + expect(DossierMailer).to have_received(:notify_deletion_to_administration).once + expect(DossierMailer).to have_received(:notify_deletion_to_administration).with(deleted_dossier, dossier.followers_gestionnaires.first.email) + end + end + + context 'when there are no following gestionnaires' do + it 'notifies the procedure administrateur' do + expect(DossierMailer).to have_received(:notify_deletion_to_administration).once + expect(DossierMailer).to have_received(:notify_deletion_to_administration).with(deleted_dossier, dossier.procedure.administrateur.email) + end end end