From 52d5a52ff9f03de9e19f8308f3b10baa5da91c83 Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Mon, 7 Sep 2020 14:55:15 +0200 Subject: [PATCH] test fonctionnel notifications avis --- .../instructeurs/dossiers_controller_spec.rb | 145 ++++++++++-------- 1 file changed, 82 insertions(+), 63 deletions(-) diff --git a/spec/controllers/instructeurs/dossiers_controller_spec.rb b/spec/controllers/instructeurs/dossiers_controller_spec.rb index ee8b88899..a967a0d80 100644 --- a/spec/controllers/instructeurs/dossiers_controller_spec.rb +++ b/spec/controllers/instructeurs/dossiers_controller_spec.rb @@ -445,79 +445,64 @@ describe Instructeurs::DossiersController, type: :controller do } end - before do - subject - end - let(:emails) { ['email@a.com'] } - it { expect(saved_avis.email).to eq('email@a.com') } - it { expect(saved_avis.introduction).to eq('intro') } - it { expect(saved_avis.confidentiel).to eq(true) } - it { expect(saved_avis.dossier).to eq(dossier) } - it { expect(saved_avis.claimant).to eq(instructeur) } - it { expect(response).to redirect_to(avis_instructeur_dossier_path(dossier.procedure, dossier)) } + context "notifications updates" do + context 'when an instructeur follows the dossier' do + let(:follower) { create(:instructeur) } + before { follower.follow(dossier) } - context "with an invalid email" do - let(:emails) { ['emaila.com'] } - - it { expect(response).to render_template :avis } - it { expect(flash.alert).to eq(["emaila.com : Email n'est pas valide"]) } - it { expect { subject }.not_to change(Avis, :count) } - it { expect(dossier.last_avis_updated_at).to eq(nil) } - end - - context 'with multiple emails' do - let(:emails) { ["toto.fr,titi@titimail.com"] } - - it { expect(response).to render_template :avis } - it { expect(flash.alert).to eq(["toto.fr : Email n'est pas valide"]) } - it { expect(flash.notice).to eq("Une demande d'avis a été envoyée à titi@titimail.com") } - it { expect(Avis.count).to eq(old_avis_count + 1) } - it { expect(saved_avis.email).to eq("titi@titimail.com") } - end - - context 'with linked dossiers' do - let(:asked_confidentiel) { false } - let(:previous_avis_confidentiel) { false } - let(:dossier) { create(:dossier, :en_construction, :with_dossier_link, procedure: procedure) } - - context 'when the expert doesn’t share linked dossiers' do - let(:invite_linked_dossiers) { false } - - it 'sends a single avis for the main dossier, but doesn’t give access to the linked dossiers' do - expect(flash.notice).to eq("Une demande d'avis a été envoyée à email@a.com") - expect(Avis.count).to eq(old_avis_count + 1) - expect(saved_avis.email).to eq("email@a.com") - expect(saved_avis.dossier).to eq(dossier) + it 'the follower has a notification' do + expect(follower.followed_dossiers.with_notifications(follower)).to eq([]) + subject + expect(follower.followed_dossiers.with_notifications(follower)).to eq([dossier.reload]) end end + end - context 'when the expert also shares the linked dossiers' do - let(:invite_linked_dossiers) { true } + context 'email sending' do + before do + subject + end - context 'and the expert can access the linked dossiers' do - let(:saved_avis) { Avis.last(2).first } - let(:linked_avis) { Avis.last } - let(:linked_dossier) { Dossier.find_by(id: dossier.reload.champs.filter(&:dossier_link?).map(&:value).compact) } - let(:invite_linked_dossiers) do - instructeur.assign_to_procedure(linked_dossier.procedure) - true - end + it { expect(saved_avis.email).to eq('email@a.com') } + it { expect(saved_avis.introduction).to eq('intro') } + it { expect(saved_avis.confidentiel).to eq(true) } + it { expect(saved_avis.dossier).to eq(dossier) } + it { expect(saved_avis.claimant).to eq(instructeur) } + it { expect(response).to redirect_to(avis_instructeur_dossier_path(dossier.procedure, dossier)) } - it 'sends one avis for the main dossier' do - expect(flash.notice).to eq("Une demande d'avis a été envoyée à email@a.com") - expect(saved_avis.email).to eq("email@a.com") - expect(saved_avis.dossier).to eq(dossier) - end + context "with an invalid email" do + let(:emails) { ['emaila.com'] } - it 'sends another avis for the linked dossiers' do - expect(Avis.count).to eq(old_avis_count + 2) - expect(linked_avis.dossier).to eq(linked_dossier) - end - end + before { subject } + + it { expect(response).to render_template :avis } + it { expect(flash.alert).to eq(["emaila.com : Email n'est pas valide"]) } + it { expect { subject }.not_to change(Avis, :count) } + it { expect(dossier.last_avis_updated_at).to eq(nil) } + end + + context 'with multiple emails' do + let(:emails) { ["toto.fr,titi@titimail.com"] } + + before { subject } + + it { expect(response).to render_template :avis } + it { expect(flash.alert).to eq(["toto.fr : Email n'est pas valide"]) } + it { expect(flash.notice).to eq("Une demande d'avis a été envoyée à titi@titimail.com") } + it { expect(Avis.count).to eq(old_avis_count + 1) } + it { expect(saved_avis.email).to eq("titi@titimail.com") } + end + + context 'with linked dossiers' do + let(:asked_confidentiel) { false } + let(:previous_avis_confidentiel) { false } + let(:dossier) { create(:dossier, :en_construction, :with_dossier_link, procedure: procedure) } + before { subject } + context 'when the expert doesn’t share linked dossiers' do + let(:invite_linked_dossiers) { false } - context 'but the expert can’t access the linked dossier' do it 'sends a single avis for the main dossier, but doesn’t give access to the linked dossiers' do expect(flash.notice).to eq("Une demande d'avis a été envoyée à email@a.com") expect(Avis.count).to eq(old_avis_count + 1) @@ -525,6 +510,40 @@ describe Instructeurs::DossiersController, type: :controller do expect(saved_avis.dossier).to eq(dossier) end end + + context 'when the expert also shares the linked dossiers' do + let(:invite_linked_dossiers) { true } + + context 'and the expert can access the linked dossiers' do + let(:saved_avis) { Avis.last(2).first } + let(:linked_avis) { Avis.last } + let(:linked_dossier) { Dossier.find_by(id: dossier.reload.champs.filter(&:dossier_link?).map(&:value).compact) } + let(:invite_linked_dossiers) do + instructeur.assign_to_procedure(linked_dossier.procedure) + true + end + + it 'sends one avis for the main dossier' do + expect(flash.notice).to eq("Une demande d'avis a été envoyée à email@a.com") + expect(saved_avis.email).to eq("email@a.com") + expect(saved_avis.dossier).to eq(dossier) + end + + it 'sends another avis for the linked dossiers' do + expect(Avis.count).to eq(old_avis_count + 2) + expect(linked_avis.dossier).to eq(linked_dossier) + end + end + + context 'but the expert can’t access the linked dossier' do + it 'sends a single avis for the main dossier, but doesn’t give access to the linked dossiers' do + expect(flash.notice).to eq("Une demande d'avis a été envoyée à email@a.com") + expect(Avis.count).to eq(old_avis_count + 1) + expect(saved_avis.email).to eq("email@a.com") + expect(saved_avis.dossier).to eq(dossier) + end + end + end end end end