This commit is contained in:
kara Diaby 2021-02-28 22:20:24 +01:00
parent 328c2a8e3c
commit 81f5a5254b
16 changed files with 544 additions and 674 deletions

View file

@ -3,11 +3,11 @@ module DossierLinkHelper
if user.is_a?(Instructeur)
if user.groupe_instructeurs.include?(dossier.groupe_instructeur)
instructeur_dossier_path(dossier.procedure, dossier)
else
avis = dossier.avis.find_by(instructeur: user)
if avis.present?
instructeur_avis_path(avis.procedure, avis)
end
elsif user.is_a?(Expert)
avis = dossier.avis.find_by(expert: user.user)
if avis.present?
expert_avis_path(avis.procedure, avis)
end
elsif user.owns_or_invite?(dossier)
dossier_path(dossier)

View file

@ -0,0 +1,354 @@
describe Experts::AvisController, type: :controller do
context 'with an expert signed in' do
render_views
let(:now) { Time.zone.parse('01/02/2345') }
let(:instructeur) { create(:instructeur) }
let(:claimant) { create(:expert) }
let(:expert) { create(:expert) }
let(:procedure) { create(:procedure, :published, instructeurs: [instructeur]) }
let(:another_procedure) { create(:procedure, :published, instructeurs: [instructeur]) }
let(:dossier) { create(:dossier, :en_construction, procedure: procedure) }
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) }
let!(:avis_without_answer) { Avis.create(dossier: dossier, claimant: claimant, experts_procedure: experts_procedure) }
let!(:avis_with_answer) { Avis.create(dossier: dossier, claimant: claimant, experts_procedure: experts_procedure, answer: 'yop') }
before do
sign_in(expert.user)
end
describe '#index' do
before { get :index }
it { expect(response).to have_http_status(:success) }
it { expect(assigns(:avis_by_procedure).flatten).to include(procedure) }
it { expect(assigns(:avis_by_procedure).flatten).not_to include(another_procedure) }
end
describe '#procedure' do
before { get :procedure, params: { procedure_id: procedure.id } }
it { expect(response).to have_http_status(:success) }
it { expect(assigns(:avis_a_donner)).to match([avis_without_answer]) }
it { expect(assigns(:avis_donnes)).to match([avis_with_answer]) }
it { expect(assigns(:statut)).to eq('a-donner') }
context 'with a statut equal to donnes' do
before { get :procedure, params: { statut: 'donnes', procedure_id: procedure.id } }
it { expect(assigns(:statut)).to eq('donnes') }
end
end
describe '#bilans_bdf' do
before { get :bilans_bdf, params: { id: avis_without_answer.id, procedure_id: procedure.id } }
it { expect(response).to redirect_to(instructeur_avis_path(avis_without_answer)) }
end
describe '#show' do
subject { get :show, params: { id: avis_with_answer.id, procedure_id: procedure.id } }
context 'with a valid avis' do
before { subject }
it { expect(response).to have_http_status(:success) }
it { expect(assigns(:avis)).to eq(avis_with_answer) }
it { expect(assigns(:dossier)).to eq(dossier) }
end
context 'with a revoked avis' do
it "refuse l'accès au dossier" do
avis_with_answer.update!(revoked_at: Time.zone.now)
subject
expect(flash.alert).to eq("Vous n'avez plus accès à ce dossier.")
expect(response).to redirect_to(root_path)
end
end
end
describe '#instruction' do
before { get :instruction, params: { id: avis_without_answer.id, procedure_id: procedure.id } }
it { expect(response).to have_http_status(:success) }
it { expect(assigns(:avis)).to eq(avis_without_answer) }
it { expect(assigns(:dossier)).to eq(dossier) }
end
describe '#messagerie' do
before { get :messagerie, params: { id: avis_without_answer.id, procedure_id: procedure.id } }
it { expect(response).to have_http_status(:success) }
it { expect(assigns(:avis)).to eq(avis_without_answer) }
it { expect(assigns(:dossier)).to eq(dossier) }
end
describe '#update' do
context 'without attachment' do
before do
Timecop.freeze(now)
patch :update, params: { id: avis_without_answer.id, procedure_id: procedure.id, avis: { answer: 'answer' } }
avis_without_answer.reload
end
after { Timecop.return }
it 'should be ok' do
expect(response).to redirect_to(instruction_expert_avis_path(avis_without_answer.procedure, avis_without_answer))
expect(avis_without_answer.answer).to eq('answer')
expect(avis_without_answer.piece_justificative_file).to_not be_attached
expect(dossier.reload.last_avis_updated_at).to eq(now)
expect(flash.notice).to eq('Votre réponse est enregistrée.')
end
end
context 'with attachment' do
include ActiveJob::TestHelper
let(:file) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') }
before do
expect(ClamavService).to receive(:safe_file?).and_return(true)
post :update, params: { id: avis_without_answer.id, procedure_id: procedure.id, avis: { answer: 'answer', piece_justificative_file: file } }
perform_enqueued_jobs
avis_without_answer.reload
end
it 'should be ok' do
expect(response).to redirect_to(instruction_expert_avis_path(avis_without_answer.procedure, avis_without_answer))
expect(avis_without_answer.answer).to eq('answer')
expect(avis_without_answer.piece_justificative_file).to be_attached
expect(flash.notice).to eq('Votre réponse est enregistrée.')
end
end
end
describe '#create_commentaire' do
let(:file) { nil }
let(:scan_result) { true }
let(:now) { Time.zone.parse("14/07/1789") }
subject { post :create_commentaire, params: { id: avis_without_answer.id, procedure_id: procedure.id, commentaire: { body: 'commentaire body', piece_jointe: file } } }
before do
allow(ClamavService).to receive(:safe_file?).and_return(scan_result)
Timecop.freeze(now)
end
after { Timecop.return }
it do
subject
expect(response).to redirect_to(messagerie_expert_avis_path(avis_without_answer.procedure, avis_without_answer))
expect(dossier.commentaires.map(&:body)).to match(['commentaire body'])
expect(dossier.reload.last_commentaire_updated_at).to eq(now)
end
context "with a file" do
let(:file) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') }
it do
subject
expect(Commentaire.last.piece_jointe.filename).to eq("piece_justificative_0.pdf")
end
it { expect { subject }.to change(Commentaire, :count).by(1) }
end
end
describe '#expert_cannot_invite_another_expert' do
let!(:previous_avis) { Avis.create(dossier: dossier, claimant: claimant, experts_procedure: experts_procedure, confidentiel: previous_avis_confidentiel) }
let(:previous_avis_confidentiel) { false }
let(:asked_confidentiel) { false }
let(:intro) { 'introduction' }
let(:emails) { ["toto@totomail.com"] }
let(:invite_linked_dossiers) { nil }
before do
Flipper.enable_actor(:expert_not_allowed_to_invite, procedure)
post :create_avis, params: { id: previous_avis.id, procedure_id: procedure.id, avis: { emails: emails, introduction: intro, confidentiel: asked_confidentiel, invite_linked_dossiers: invite_linked_dossiers, introduction_file: @introduction_file } }
end
context 'when the expert cannot invite another expert' do
let(:asked_confidentiel) { false }
it { expect(flash.alert).to eq("Cette démarche ne vous permet pas de demander un avis externe") }
it { expect(response).to redirect_to(instruction_expert_avis_path(procedure, previous_avis)) }
end
end
describe '#create_avis' do
let!(:previous_avis) { Avis.create(dossier: dossier, claimant: claimant, experts_procedure: experts_procedure, confidentiel: previous_avis_confidentiel) }
let(:emails) { ['a@b.com'] }
let(:intro) { 'introduction' }
let(:created_avis) { Avis.last }
let!(:old_avis_count) { Avis.count }
let(:invite_linked_dossiers) { nil }
before do
Timecop.freeze(now)
@introduction_file = fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf')
post :create_avis, params: { id: previous_avis.id, procedure_id: procedure.id, avis: { emails: emails, introduction: intro, experts_procedure: experts_procedure, confidentiel: asked_confidentiel, invite_linked_dossiers: invite_linked_dossiers, introduction_file: @introduction_file } }
created_avis.reload
end
after { Timecop.return }
context 'when an invalid email' do
let(:previous_avis_confidentiel) { false }
let(:asked_confidentiel) { false }
let(:emails) { ["toto.fr"] }
it { expect(response).to render_template :instruction }
it { expect(flash.alert).to eq(["toto.fr : Email n'est pas valide"]) }
it { expect(Avis.last).to eq(previous_avis) }
it { expect(dossier.last_avis_updated_at).to eq(nil) }
end
context 'ask review with attachment' do
let(:previous_avis_confidentiel) { false }
let(:asked_confidentiel) { false }
let(:emails) { ["toto@totomail.com"] }
it { expect(created_avis.introduction_file).to be_attached }
it { expect(created_avis.introduction_file.filename).to eq("piece_justificative_0.pdf") }
it { expect(created_avis.dossier.reload.last_avis_updated_at).to eq(now) }
it { expect(flash.notice).to eq("Une demande d'avis a été envoyée à toto@totomail.com") }
end
context 'with multiple emails' do
let(:asked_confidentiel) { false }
let(:previous_avis_confidentiel) { false }
let(:emails) { ["toto.fr,titi@titimail.com"] }
it { expect(response).to render_template :instruction }
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) }
end
context 'when the previous avis is public' do
let(:previous_avis_confidentiel) { false }
context 'when the user asked for a public avis' do
let(:asked_confidentiel) { false }
it { expect(created_avis.confidentiel).to be(false) }
it { expect(created_avis.introduction).to eq(intro) }
it { expect(created_avis.dossier).to eq(previous_avis.dossier) }
it { expect(created_avis.claimant).to eq(expert) }
it { expect(response).to redirect_to(instruction_expert_avis_path(previous_avis.procedure, previous_avis)) }
end
context 'when the user asked for a confidentiel avis' do
let(:asked_confidentiel) { true }
it { expect(created_avis.confidentiel).to be(true) }
end
end
context 'when the preivous avis is confidentiel' do
let(:previous_avis_confidentiel) { true }
context 'when the user asked for a public avis' do
let(:asked_confidentiel) { false }
it { expect(created_avis.confidentiel).to be(true) }
end
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 doesnt share linked dossiers' do
let(:invite_linked_dossiers) { false }
it 'sends a single avis for the main dossier, but doesnt give access to the linked dossiers' do
expect(flash.notice).to eq("Une demande d'avis a été envoyée à a@b.com")
expect(Avis.count).to eq(old_avis_count + 1)
expect(created_avis.dossier).to eq(dossier)
end
end
context 'when the expert also shares the linked dossiers' do
context 'and the expert can access the linked dossiers' do
let(:created_avis) { Avis.create(dossier: dossier, claimant: claimant, email: "toto3@gmail.com") }
let(:linked_dossier) { Dossier.find_by(id: dossier.reload.champs.filter(&:dossier_link?).map(&:value).compact) }
let(:linked_avis) { Avis.create(dossier: linked_dossier, claimant: claimant) }
let(:invite_linked_dossiers) { true }
it 'sends one avis for the main dossier' do
expect(flash.notice).to eq("Une demande d'avis a été envoyée à a@b.com")
expect(created_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 cant access the linked dossier' do
it 'sends a single avis for the main dossier, but doesnt give access to the linked dossiers' do
expect(flash.notice).to eq("Une demande d'avis a été envoyée à a@b.com")
expect(Avis.count).to eq(old_avis_count + 1)
expect(created_avis.dossier).to eq(dossier)
end
end
end
end
end
end
context 'without an expert signed in' do
describe '#sign_up' do
let(:invited_email) { 'invited@avis.com' }
let(:claimant) { create(:instructeur) }
let(:expert) { create(:expert) }
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) }
let(:dossier) { create(:dossier) }
let(:procedure) { dossier.procedure }
let!(:avis) { create(:avis, experts_procedure: experts_procedure, claimant: claimant, dossier: dossier) }
let(:invitations_email) { true }
context 'when the expert has already signed up and belongs to the invitation' do
let!(:avis) { create(:avis, dossier: dossier, experts_procedure: experts_procedure, claimant: claimant) }
context 'when the expert is authenticated' do
before do
sign_in(expert.user)
expert.user.update(last_sign_in_at: Time.zone.now)
expert.user.reload
get :sign_up, params: { id: avis.id, procedure_id: procedure.id, email: avis.expert.email }
end
it { is_expected.to redirect_to expert_avis_url(avis.procedure, avis) }
end
context 'when the expert is not authenticated' do
before do
sign_in(expert.user)
expert.user.update(last_sign_in_at: Time.zone.now)
expert.user.reload
sign_out(expert.user)
get :sign_up, params: { id: avis.id, procedure_id: procedure.id, email: avis.expert.email }
end
it { is_expected.to redirect_to new_user_session_url }
end
end
context 'when the expert has already signed up / is authenticated and does not belong to the invitation' do
let(:expert) { create(:expert) }
let!(:avis) { create(:avis, email: invited_email, dossier: dossier, experts_procedure: experts_procedure) }
before do
sign_in(expert.user)
get :sign_up, params: { id: avis.id, procedure_id: procedure.id, email: avis.expert.email }
end
# redirected to dossier but then the instructeur gonna be banished !
it { is_expected.to redirect_to expert_avis_url(avis.procedure, avis) }
end
end
end
end

View file

@ -3,318 +3,28 @@ describe Instructeurs::AvisController, type: :controller do
render_views
let(:now) { Time.zone.parse('01/02/2345') }
let(:expert) { create(:expert) }
let(:claimant) { create(:instructeur) }
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) }
let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, :published, instructeurs: [claimant]) }
let(:another_procedure) { create(:procedure, :published, instructeurs: [claimant]) }
let(:procedure) { create(:procedure, :published, instructeurs: [instructeur]) }
let(:dossier) { create(:dossier, :en_construction, procedure: procedure) }
let!(:avis_without_answer) { Avis.create(dossier: dossier, claimant: claimant, instructeur: instructeur) }
let!(:avis_with_answer) { Avis.create(dossier: dossier, claimant: claimant, instructeur: instructeur, answer: 'yop') }
let!(:avis) { Avis.create(dossier: dossier, claimant: instructeur, experts_procedure: experts_procedure) }
let!(:avis_without_answer) { Avis.create(dossier: dossier, claimant: claimant, experts_procedure: experts_procedure) }
before { sign_in(instructeur.user) }
describe '#index' do
before { get :index }
it { expect(response).to have_http_status(:success) }
it { expect(assigns(:avis_by_procedure).flatten).to include(procedure) }
it { expect(assigns(:avis_by_procedure).flatten).not_to include(another_procedure) }
end
describe '#procedure' do
before { get :procedure, params: { procedure_id: procedure.id } }
it { expect(response).to have_http_status(:success) }
it { expect(assigns(:avis_a_donner)).to match([avis_without_answer]) }
it { expect(assigns(:avis_donnes)).to match([avis_with_answer]) }
it { expect(assigns(:statut)).to eq('a-donner') }
context 'with a statut equal to donnes' do
before { get :procedure, params: { statut: 'donnes', procedure_id: procedure.id } }
it { expect(assigns(:statut)).to eq('donnes') }
end
end
describe '#show' do
subject { get :show, params: { id: avis_with_answer.id, procedure_id: procedure.id } }
context 'with a valid avis' do
before { subject }
it { expect(response).to have_http_status(:success) }
it { expect(assigns(:avis)).to eq(avis_with_answer) }
it { expect(assigns(:dossier)).to eq(dossier) }
end
context 'with a revoked avis' do
it "refuse l'accès au dossier" do
avis_with_answer.update!(revoked_at: Time.zone.now)
subject
expect(flash.alert).to eq("Vous n'avez plus accès à ce dossier.")
expect(response).to redirect_to(root_path)
end
end
end
describe '#instruction' do
before { get :instruction, params: { id: avis_without_answer.id, procedure_id: procedure.id } }
it { expect(response).to have_http_status(:success) }
it { expect(assigns(:avis)).to eq(avis_without_answer) }
it { expect(assigns(:dossier)).to eq(dossier) }
end
describe '#messagerie' do
before { get :messagerie, params: { id: avis_without_answer.id, procedure_id: procedure.id } }
it { expect(response).to have_http_status(:success) }
it { expect(assigns(:avis)).to eq(avis_without_answer) }
it { expect(assigns(:dossier)).to eq(dossier) }
end
describe '#bilans_bdf' do
before { get :bilans_bdf, params: { id: avis_without_answer.id, procedure_id: procedure.id } }
it { expect(response).to redirect_to(instructeur_avis_path(avis_without_answer)) }
end
describe '#update' do
context 'without attachment' do
before do
Timecop.freeze(now)
patch :update, params: { id: avis_without_answer.id, procedure_id: procedure.id, avis: { answer: 'answer' } }
avis_without_answer.reload
end
after { Timecop.return }
it 'should be ok' do
expect(response).to redirect_to(instruction_instructeur_avis_path(avis_without_answer.procedure, avis_without_answer))
expect(avis_without_answer.answer).to eq('answer')
expect(avis_without_answer.piece_justificative_file).to_not be_attached
expect(dossier.reload.last_avis_updated_at).to eq(now)
expect(flash.notice).to eq('Votre réponse est enregistrée.')
end
end
context 'with attachment' do
let(:file) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') }
before do
post :update, params: { id: avis_without_answer.id, procedure_id: procedure.id, avis: { answer: 'answer', piece_justificative_file: file } }
avis_without_answer.reload
end
it 'should be ok' do
expect(response).to redirect_to(instruction_instructeur_avis_path(avis_without_answer.procedure, avis_without_answer))
expect(avis_without_answer.answer).to eq('answer')
expect(avis_without_answer.piece_justificative_file).to be_attached
expect(flash.notice).to eq('Votre réponse est enregistrée.')
end
end
end
describe '#create_commentaire' do
let(:file) { nil }
let(:scan_result) { true }
let(:now) { Time.zone.parse("14/07/1789") }
subject { post :create_commentaire, params: { id: avis_without_answer.id, procedure_id: procedure.id, commentaire: { body: 'commentaire body', piece_jointe: file } } }
before do
Timecop.freeze(now)
end
after { Timecop.return }
it do
subject
expect(response).to redirect_to(messagerie_instructeur_avis_path(avis_without_answer.procedure, avis_without_answer))
expect(dossier.commentaires.map(&:body)).to match(['commentaire body'])
expect(dossier.reload.last_commentaire_updated_at).to eq(now)
end
context "with a file" do
let(:file) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') }
it do
subject
expect(Commentaire.last.piece_jointe.filename).to eq("piece_justificative_0.pdf")
end
it { expect { subject }.to change(Commentaire, :count).by(1) }
end
end
describe '#expert_cannot_invite_another_expert' do
let!(:previous_avis) { Avis.create(dossier: dossier, claimant: claimant, instructeur: instructeur, confidentiel: previous_avis_confidentiel) }
let(:previous_avis_confidentiel) { false }
let(:asked_confidentiel) { false }
let(:intro) { 'introduction' }
let(:emails) { ["toto@totomail.com"] }
let(:invite_linked_dossiers) { nil }
before do
Flipper.enable_actor(:expert_not_allowed_to_invite, procedure)
post :create_avis, params: { id: previous_avis.id, procedure_id: procedure.id, avis: { emails: emails, introduction: intro, confidentiel: asked_confidentiel, invite_linked_dossiers: invite_linked_dossiers, introduction_file: @introduction_file } }
end
context 'when the expert cannot invite another expert' do
let(:asked_confidentiel) { false }
it { expect(flash.alert).to eq("Cette démarche ne vous permet pas de demander un avis externe") }
it { expect(response).to redirect_to(instruction_instructeur_avis_path(procedure, previous_avis)) }
end
end
describe '#create_avis' do
let!(:previous_avis) { Avis.create(dossier: dossier, claimant: claimant, instructeur: instructeur, confidentiel: previous_avis_confidentiel) }
let(:emails) { ['a@b.com'] }
let(:intro) { 'introduction' }
let(:created_avis) { Avis.last }
let!(:old_avis_count) { Avis.count }
let(:invite_linked_dossiers) { nil }
before do
Timecop.freeze(now)
@introduction_file = fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf')
post :create_avis, params: { id: previous_avis.id, procedure_id: procedure.id, avis: { emails: emails, introduction: intro, confidentiel: asked_confidentiel, invite_linked_dossiers: invite_linked_dossiers, introduction_file: @introduction_file } }
created_avis.reload
end
after { Timecop.return }
context 'when an invalid email' do
let(:previous_avis_confidentiel) { false }
let(:asked_confidentiel) { false }
let(:emails) { ["toto.fr"] }
it { expect(response).to render_template :instruction }
it { expect(flash.alert).to eq(["toto.fr : Email n'est pas valide"]) }
it { expect(Avis.last).to eq(previous_avis) }
it { expect(dossier.last_avis_updated_at).to eq(nil) }
end
context 'ask review with attachment' do
let(:previous_avis_confidentiel) { false }
let(:asked_confidentiel) { false }
let(:emails) { ["toto@totomail.com"] }
it { expect(created_avis.introduction_file).to be_attached }
it { expect(created_avis.introduction_file.filename).to eq("piece_justificative_0.pdf") }
it { expect(created_avis.dossier.reload.last_avis_updated_at).to eq(now) }
it { expect(flash.notice).to eq("Une demande d'avis a été envoyée à toto@totomail.com") }
end
context 'with multiple emails' do
let(:asked_confidentiel) { false }
let(:previous_avis_confidentiel) { false }
let(:emails) { ["toto.fr,titi@titimail.com"] }
it { expect(response).to render_template :instruction }
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(created_avis.email).to eq("titi@titimail.com") }
end
context 'when the previous avis is public' do
let(:previous_avis_confidentiel) { false }
context 'when the user asked for a public avis' do
let(:asked_confidentiel) { false }
it { expect(created_avis.confidentiel).to be(false) }
it { expect(created_avis.email).to eq(emails.last) }
it { expect(created_avis.introduction).to eq(intro) }
it { expect(created_avis.dossier).to eq(previous_avis.dossier) }
it { expect(created_avis.claimant).to eq(instructeur) }
it { expect(response).to redirect_to(instruction_instructeur_avis_path(previous_avis.procedure, previous_avis)) }
end
context 'when the user asked for a confidentiel avis' do
let(:asked_confidentiel) { true }
it { expect(created_avis.confidentiel).to be(true) }
end
end
context 'when the preivous avis is confidentiel' do
let(:previous_avis_confidentiel) { true }
context 'when the user asked for a public avis' do
let(:asked_confidentiel) { false }
it { expect(created_avis.confidentiel).to be(true) }
end
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 doesnt share linked dossiers' do
let(:invite_linked_dossiers) { false }
it 'sends a single avis for the main dossier, but doesnt give access to the linked dossiers' do
expect(flash.notice).to eq("Une demande d'avis a été envoyée à a@b.com")
expect(Avis.count).to eq(old_avis_count + 1)
expect(created_avis.email).to eq("a@b.com")
expect(created_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(:created_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 à a@b.com")
expect(created_avis.email).to eq("a@b.com")
expect(created_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 cant access the linked dossier' do
it 'sends a single avis for the main dossier, but doesnt give access to the linked dossiers' do
expect(flash.notice).to eq("Une demande d'avis a été envoyée à a@b.com")
expect(Avis.count).to eq(old_avis_count + 1)
expect(created_avis.email).to eq("a@b.com")
expect(created_avis.dossier).to eq(dossier)
end
end
end
end
end
describe "#revoker" do
let(:avis) { create(:avis, claimant: instructeur) }
let(:procedure) { avis.procedure }
before do
patch :revoquer, params: { procedure_id: procedure.id, id: avis.id }
end
it "revoke the dossier" do
patch :revoquer, params: { procedure_id: procedure.id, id: avis.id }
expect(flash.notice).to eq("#{avis.email} ne peut plus donner son avis sur ce dossier.")
expect(flash.notice).to eq("#{avis.expert.email} ne peut plus donner son avis sur ce dossier.")
end
end
describe 'revive' do
let(:avis) { create(:avis, claimant: instructeur, email: 'expert@gouv.fr') }
let(:procedure) { avis.procedure }
before do
allow(AvisMailer).to receive(:avis_invitation).and_return(double(deliver_later: nil))
end
@ -322,141 +32,7 @@ describe Instructeurs::AvisController, type: :controller do
it 'sends a reminder to the expert' do
get :revive, params: { procedure_id: procedure.id, id: avis.id }
expect(AvisMailer).to have_received(:avis_invitation).once.with(avis)
expect(flash.notice).to eq("Un mail de relance a été envoyé à #{avis.email}")
end
end
end
context 'without a instructeur signed in' do
describe '#sign_up' do
let(:invited_email) { 'invited@avis.com' }
let(:dossier) { create(:dossier) }
let(:procedure) { dossier.procedure }
let!(:avis) { create(:avis, email: invited_email, dossier: dossier) }
let(:invitations_email) { true }
context 'when the new instructeur has never signed up' do
before do
expect(Avis).to receive(:avis_exists_and_email_belongs_to_avis?)
.with(avis.id.to_s, invited_email)
.and_return(invitations_email)
get :sign_up, params: { id: avis.id, procedure_id: procedure.id, email: invited_email }
end
context 'when the email belongs to the invitation' do
it { expect(subject.status).to eq(200) }
it { expect(assigns(:email)).to eq(invited_email) }
it { expect(assigns(:dossier)).to eq(dossier) }
end
context 'when the email does not belong to the invitation' do
let(:invitations_email) { false }
it { is_expected.to redirect_to root_path }
end
end
context 'when the instructeur has already signed up and belongs to the invitation' do
let(:instructeur) { create(:instructeur, email: invited_email) }
let!(:avis) { create(:avis, dossier: dossier, instructeur: instructeur) }
context 'when the instructeur is authenticated' do
before do
sign_in(instructeur.user)
get :sign_up, params: { id: avis.id, procedure_id: procedure.id, email: invited_email }
end
it { is_expected.to redirect_to instructeur_avis_url(avis.procedure, avis) }
end
context 'when the instructeur is not authenticated' do
before do
get :sign_up, params: { id: avis.id, procedure_id: procedure.id, email: invited_email }
end
it { is_expected.to redirect_to new_user_session_url }
end
end
context 'when the instructeur has already signed up / is authenticated and does not belong to the invitation' do
let(:instructeur) { create(:instructeur, email: 'other@gmail.com') }
let!(:avis) { create(:avis, email: invited_email, dossier: dossier) }
before do
sign_in(instructeur.user)
get :sign_up, params: { id: avis.id, procedure_id: procedure.id, email: invited_email }
end
# redirected to dossier but then the instructeur gonna be banished !
it { is_expected.to redirect_to instructeur_avis_url(avis.procedure, avis) }
end
end
describe '#create_instructeur' do
let(:existing_user_mail) { 'dummy@example.org' }
let!(:existing_user) { create(:user, email: existing_user_mail) }
let(:invited_email) { 'invited@avis.com' }
let(:dossier) { create(:dossier) }
let(:procedure) { dossier.procedure }
let!(:avis) { create(:avis, email: invited_email, dossier: dossier) }
let(:avis_id) { avis.id }
let(:password) { 'my-s3cure-p4ssword' }
let(:created_instructeur) { Instructeur.by_email(invited_email) }
let(:invitations_email) { true }
before do
allow(Avis).to receive(:link_avis_to_instructeur)
expect(Avis).to receive(:avis_exists_and_email_belongs_to_avis?)
.with(avis_id.to_s, invited_email)
.and_return(invitations_email)
post :create_instructeur, params: {
id: avis_id,
procedure_id: procedure.id,
email: invited_email,
user: {
password: password
}
}
end
context 'when the email does not belong to the invitation' do
let(:invitations_email) { false }
it { is_expected.to redirect_to root_path }
end
context 'when the email belongs to the invitation' do
context 'when the instructeur creation succeeds' do
it { expect(created_instructeur).to be_present }
it { expect(created_instructeur.user.valid_password?(password)).to be true }
it { expect(Avis).to have_received(:link_avis_to_instructeur) }
it { expect(subject.current_instructeur).to eq(created_instructeur) }
it { is_expected.to redirect_to instructeur_all_avis_path }
it 'creates a corresponding user account for the email' do
user = User.find_by(email: invited_email)
expect(user).to be_present
end
context 'when there already is a user account with the same email' do
let(:existing_user_mail) { invited_email }
it 'still creates a instructeur account' do
expect(created_instructeur).to be_present
end
end
end
context 'when the instructeur creation fails' do
let(:password) { '' }
it { expect(created_instructeur).to be_nil }
it { is_expected.to redirect_to sign_up_instructeur_avis_path(procedure.id, avis_id, invited_email) }
it { expect(flash.alert).to eq(['Le mot de passe doit être rempli']) }
end
expect(flash.notice).to eq("Un mail de relance a été envoyé à #{avis.expert.email}")
end
end
end

View file

@ -434,6 +434,8 @@ describe Instructeurs::DossiersController, type: :controller do
end
describe "#create_avis" do
let(:expert) { create(:expert) }
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: dossier.procedure) }
let(:invite_linked_dossiers) { false }
let(:saved_avis) { dossier.avis.first }
let!(:old_avis_count) { Avis.count }
@ -442,7 +444,7 @@ describe Instructeurs::DossiersController, type: :controller do
post :create_avis, params: {
procedure_id: procedure.id,
dossier_id: dossier.id,
avis: { emails: emails, introduction: 'intro', confidentiel: true, invite_linked_dossiers: invite_linked_dossiers }
avis: { emails: emails, introduction: 'intro', confidentiel: true, invite_linked_dossiers: invite_linked_dossiers, claimant: instructeur, experts_procedure: experts_procedure }
}
end
@ -466,7 +468,7 @@ describe Instructeurs::DossiersController, type: :controller do
subject
end
it { expect(saved_avis.email).to eq('email@a.com') }
it { expect(saved_avis.expert.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) }
@ -493,7 +495,7 @@ describe Instructeurs::DossiersController, type: :controller do
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") }
it { expect(saved_avis.expert.email).to eq("titi@titimail.com") }
end
context 'with linked dossiers' do
@ -507,7 +509,7 @@ describe Instructeurs::DossiersController, type: :controller do
it 'sends a single avis for the main dossier, but doesnt 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.expert.email).to eq("email@a.com")
expect(saved_avis.dossier).to eq(dossier)
end
end
@ -526,7 +528,7 @@ describe Instructeurs::DossiersController, type: :controller do
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.expert.email).to eq("email@a.com")
expect(saved_avis.dossier).to eq(dossier)
end
@ -540,7 +542,7 @@ describe Instructeurs::DossiersController, type: :controller do
it 'sends a single avis for the main dossier, but doesnt 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.expert.email).to eq("email@a.com")
expect(saved_avis.dossier).to eq(dossier)
end
end
@ -552,7 +554,9 @@ describe Instructeurs::DossiersController, type: :controller do
describe "#show" do
context "when the dossier is exported as PDF" do
let(:instructeur) { create(:instructeur) }
let(:expert) { create(:expert) }
let(:procedure) { create(:procedure, :published, instructeurs: instructeurs) }
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) }
let(:dossier) do
create(:dossier,
:accepte,
@ -562,7 +566,7 @@ describe Instructeurs::DossiersController, type: :controller do
:with_entreprise,
:with_commentaires, procedure: procedure)
end
let(:avis) { create(:avis, dossier: dossier, instructeur: instructeur) }
let!(:avis) { create(:avis, dossier: dossier, claimant: instructeur, experts_procedure: experts_procedure) }
subject do
avis

View file

@ -40,47 +40,6 @@ describe Instructeurs::ProceduresController, type: :controller do
end
end
describe "before_action: redirect_to_avis_if_needed" do
it "is present" do
before_actions = Instructeurs::ProceduresController
._process_action_callbacks
.filter { |process_action_callbacks| process_action_callbacks.kind == :before }
.map(&:filter)
expect(before_actions).to include(:redirect_to_avis_if_needed)
end
end
describe "redirect_to_avis_if_needed" do
let(:instructeur) { create(:instructeur) }
before do
expect(@controller).to receive(:current_instructeur).at_least(:once).and_return(instructeur)
allow(@controller).to receive(:redirect_to)
end
context "when a instructeur has some procedures" do
let!(:some_procedure) { create(:procedure, instructeurs: [instructeur]) }
before { @controller.send(:redirect_to_avis_if_needed) }
it "does not redirects nor flash" do
expect(@controller).not_to have_received(:redirect_to)
end
end
context "when a instructeur has no procedure and some avis" do
before do
Avis.create!(dossier: create(:dossier), claimant: create(:instructeur), instructeur: instructeur)
@controller.send(:redirect_to_avis_if_needed)
end
it "redirects avis" do
expect(@controller).to have_received(:redirect_to).with(instructeur_all_avis_path)
end
end
end
describe "#index" do
let(:instructeur) { create(:instructeur) }
subject { get :index }

View file

@ -1,6 +1,9 @@
describe InvitesController, type: :controller do
let(:dossier) { create(:dossier, :en_construction) }
let(:email) { 'plop@octo.com' }
let(:expert) { create(:expert) }
let(:procedure) { create(:procedure) }
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) }
describe '#POST create' do
let(:invite) { Invite.last }
@ -23,7 +26,7 @@ describe InvitesController, type: :controller do
end
context 'when instructeur is invited for avis on dossier' do
before { Avis.create(instructeur: signed_in_profile.instructeur, claimant: create(:instructeur), dossier: dossier) }
before { Avis.create(experts_procedure: experts_procedure, claimant: create(:instructeur), dossier: dossier) }
it_behaves_like "he can not create invitation"
end

View file

@ -0,0 +1,77 @@
feature 'Inviting an expert:' do
include ActiveJob::TestHelper
include ActionView::Helpers
context 'as an invited Expert' do
let(:expert) { create(:expert) }
let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, :published, instructeurs: [instructeur]) }
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) }
let(:dossier) { create(:dossier, :en_construction, :with_dossier_link, procedure: procedure) }
let(:avis) { create(:avis, dossier: dossier, claimant: instructeur, experts_procedure: experts_procedure, confidentiel: true) }
context 'when I dont already have an account' do
scenario 'I can sign up' do
visit sign_up_expert_avis_path(avis.dossier.procedure, avis, avis.expert.email)
expect(page).to have_field('Email', with: avis.expert.email, disabled: true)
fill_in 'Mot de passe', with: 'This is a very complicated password !'
click_on 'Créer un compte'
expect(page).to have_current_path(expert_all_avis_path)
expect(page).to have_text('1 avis à donner')
end
end
context 'when I already have an existing account' do
before do
avis.expert.user.update!(last_sign_in_at: Time.zone.now)
avis.expert.user.reload
end
scenario 'I can sign in' do
visit sign_up_expert_avis_path(avis.dossier.procedure, avis, avis.expert.email)
expect(page).to have_current_path(new_user_session_path)
login_as avis.expert.user, scope: :user
sign_in_with(avis.expert.email, 'This is a very complicated password !')
click_on 'Passer en expert'
expect(page).to have_current_path(expert_all_avis_path)
expect(page).to have_text('1 avis à donner')
end
end
scenario 'I can give an answer' do
avis # create avis
login_as expert.user, scope: :user
visit expert_all_avis_path
expect(page).to have_text('1 avis à donner')
expect(page).to have_text('0 avis donnés')
click_on '1 avis à donner'
click_on avis.dossier.user.email
within('.tabs') { click_on 'Avis' }
expect(page).to have_text("Demandeur : #{avis.claimant.email}")
expect(page).to have_text('Cet avis est confidentiel')
fill_in 'avis_answer', with: 'Ma réponse dexpert : cest un oui.'
find('.attachment input[name="avis[piece_justificative_file]"]').attach_file(Rails.root + 'spec/fixtures/files/RIB.pdf')
click_on 'Envoyer votre avis'
expect(page).to have_content('Votre réponse est enregistrée')
expect(page).to have_content('Ma réponse dexpert : cest un oui.')
expect(page).to have_content('RIB.pdf')
within('.breadcrumbs') { click_on 'Avis' }
expect(page).to have_text('0 avis à donner')
expect(page).to have_text('1 avis donné')
end
# TODO
# scenario 'I can read other experts advices' do
# end
# scenario 'I can invite other experts' do
# end
end
end

View file

@ -3,7 +3,7 @@ feature 'Inviting an expert:' do
include ActionView::Helpers
let(:instructeur) { create(:instructeur, password: 'my-s3cure-p4ssword') }
let(:expert) { create(:instructeur, password: expert_password) }
let(:expert) { create(:expert, password: expert_password) }
let(:expert_password) { 'mot de passe dexpert' }
let(:procedure) { create(:procedure, :published, instructeurs: [instructeur]) }
let(:dossier) { create(:dossier, :en_construction, :with_dossier_link, procedure: procedure) }
@ -20,7 +20,7 @@ feature 'Inviting an expert:' do
click_on 'Avis externes'
expect(page).to have_current_path(avis_instructeur_dossier_path(procedure, dossier))
fill_in 'avis_emails', with: 'expert1@exemple.fr, expert2@exemple.fr'
fill_in 'avis_emails', with: 'expert1@expert.com, expert2@expert.com'
fill_in 'avis_introduction', with: 'Bonjour, merci de me donner votre avis sur ce dossier.'
check 'avis_invite_linked_dossiers'
page.select 'confidentiel', from: 'avis_confidentiel'
@ -31,23 +31,24 @@ feature 'Inviting an expert:' do
expect(page).to have_content('Une demande d\'avis a été envoyée')
expect(page).to have_content('Avis des invités')
within('.list-avis') do
expect(page).to have_content('expert1@exemple.fr')
expect(page).to have_content('expert2@exemple.fr')
expect(page).to have_content('expert1@expert.com')
expect(page).to have_content('expert2@expert.com')
expect(page).to have_content('Bonjour, merci de me donner votre avis sur ce dossier.')
end
expect(Avis.count).to eq(4)
expect(emails_sent_to('expert1@exemple.fr').size).to eq(1)
expect(emails_sent_to('expert2@exemple.fr').size).to eq(1)
expect(emails_sent_to('expert1@expert.com').size).to eq(1)
expect(emails_sent_to('expert2@expert.com').size).to eq(1)
invitation_email = open_email('expert2@exemple.fr')
avis = Avis.find_by(email: 'expert2@exemple.fr', dossier: dossier)
sign_up_link = sign_up_instructeur_avis_path(avis.dossier.procedure, avis, avis.email)
invitation_email = open_email('expert1@expert.com')
avis = Avis.find_by(expert: expert.id)
sign_up_link = sign_up_expert_avis_path(avis.dossier.procedure, avis, avis.expert.email)
expect(invitation_email.body).to include(sign_up_link)
end
context 'when experts submitted their answer' do
let!(:answered_avis) { create(:avis, :with_answer, dossier: dossier, claimant: instructeur, email: expert.email) }
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) }
let!(:answered_avis) { create(:avis, :with_answer, dossier: dossier, claimant: instructeur, experts_procedure: experts_procedure) }
scenario 'I can read the expert answer' do
login_as instructeur.user, scope: :user
@ -55,80 +56,11 @@ feature 'Inviting an expert:' do
click_on 'Avis externes'
expect(page).to have_content(expert.email)
expect(page).to have_content(answered_avis.expert.email)
answered_avis.answer.split("\n").each do |answer_line|
expect(page).to have_content(answer_line)
end
end
end
end
context 'as an invited Expert' do
let(:avis_email) { expert.email }
let(:avis) { create(:avis, dossier: dossier, claimant: instructeur, email: avis_email, confidentiel: true) }
context 'when I dont already have an account' do
let(:avis_email) { 'not-signed-up-expert@exemple.fr' }
scenario 'I can sign up' do
visit sign_up_instructeur_avis_path(avis.dossier.procedure, avis, avis_email)
expect(page).to have_field('Email', with: avis_email, disabled: true)
fill_in 'Mot de passe', with: 'This is a very complicated password !'
click_on 'Créer un compte'
expect(page).to have_current_path(instructeur_all_avis_path)
expect(page).to have_text('1 avis à donner')
end
end
context 'when I already have an existing account' do
let(:avis_email) { expert.email }
scenario 'I can sign in' do
visit sign_up_instructeur_avis_path(avis.dossier.procedure, avis, avis_email)
expect(page).to have_current_path(new_user_session_path)
sign_in_with(expert.email, expert_password)
expect(page).to have_current_path(instructeur_all_avis_path)
expect(page).to have_text('1 avis à donner')
end
end
scenario 'I can give an answer' do
avis # create avis
login_as expert.user, scope: :user
visit instructeur_all_avis_path
expect(page).to have_text('1 avis à donner')
expect(page).to have_text('0 avis donnés')
click_on '1 avis à donner'
click_on avis.dossier.user.email
within('.tabs') { click_on 'Avis' }
expect(page).to have_text("Demandeur : #{instructeur.email}")
expect(page).to have_text('Cet avis est confidentiel')
fill_in 'avis_answer', with: 'Ma réponse dexpert : cest un oui.'
find('.attachment input[name="avis[piece_justificative_file]"]').attach_file(Rails.root + 'spec/fixtures/files/RIB.pdf')
click_on 'Envoyer votre avis'
expect(page).to have_content('Votre réponse est enregistrée')
expect(page).to have_content('Ma réponse dexpert : cest un oui.')
expect(page).to have_content('RIB.pdf')
within('.new-header') { click_on 'Avis' }
expect(page).to have_text('0 avis à donner')
expect(page).to have_text('1 avis donné')
end
# TODO
# scenario 'I can read other experts advices' do
# end
# scenario 'I can invite other experts' do
# end
end
end

View file

@ -27,9 +27,10 @@ describe DossierLinkHelper do
context "when access as expert" do
let(:dossier) { create(:dossier) }
let(:instructeur) { create(:instructeur) }
let!(:avis) { create(:avis, dossier: dossier, instructeur: instructeur) }
it { expect(helper.dossier_linked_path(instructeur, dossier)).to eq(instructeur_avis_path(avis.dossier.procedure, avis)) }
let(:expert) { create(:expert) }
let!(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: dossier.procedure) }
let!(:avis) { create(:avis, dossier: dossier, claimant: instructeur, experts_procedure: experts_procedure) }
it { expect(helper.dossier_linked_path(expert, dossier)).to eq(expert_avis_path(avis.dossier.procedure, avis)) }
end
context "when access as user" do

View file

@ -1,6 +1,10 @@
RSpec.describe AvisMailer, type: :mailer do
describe '.avis_invitation' do
let(:avis) { create(:avis) }
let(:claimant) { create(:instructeur) }
let(:expert) { create(:expert) }
let(:dossier) { create(:dossier) }
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: dossier.procedure) }
let(:avis) { Avis.create(dossier: dossier, claimant: claimant, experts_procedure: experts_procedure, introduction: 'intro') }
subject { described_class.avis_invitation(avis) }
@ -10,12 +14,7 @@ RSpec.describe AvisMailer, type: :mailer do
it { expect(subject.body).to include(instructeur_avis_url(avis.dossier.procedure.id, avis)) }
context 'when the recipient is not already registered' do
before do
avis.email = 'instructeur@email.com'
avis.instructeur = nil
end
it { expect(subject.body).to include(sign_up_instructeur_avis_url(avis.dossier.procedure.id, avis.id, avis.email)) }
it { expect(subject.body).to include(sign_up_expert_avis_url(avis.dossier.procedure.id, avis.id, avis.expert.email)) }
end
end
end

View file

@ -3,22 +3,16 @@ RSpec.describe Avis, type: :model do
describe '#email_to_display' do
let(:invited_email) { 'invited@avis.com' }
let!(:avis) do
avis = create(:avis, email: invited_email, dossier: create(:dossier))
avis.instructeur = nil
avis
end
let(:expert) { create(:expert) }
let(:procedure) { create(:procedure) }
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) }
subject { avis.email_to_display }
context 'when instructeur is not known' do
it { is_expected.to eq(invited_email) }
end
context 'when expert is known' do
let!(:avis) { create(:avis, claimant: claimant, dossier: create(:dossier), experts_procedure: experts_procedure) }
context 'when instructeur is known' do
let!(:avis) { create(:avis, email: nil, instructeur: create(:instructeur), dossier: create(:dossier)) }
it { is_expected.to eq(avis.instructeur.email) }
it { is_expected.to eq(avis.expert.email) }
end
end
@ -34,28 +28,6 @@ RSpec.describe Avis, type: :model do
end
end
describe ".link_avis_to_instructeur" do
let(:instructeur) { create(:instructeur) }
subject { Avis.link_avis_to_instructeur(instructeur) }
context 'when there are 2 avis linked by email to a instructeur' do
let!(:avis) { create(:avis, email: instructeur.email, instructeur: nil) }
let!(:avis2) { create(:avis, email: instructeur.email, instructeur: nil) }
before do
subject
avis.reload
avis2.reload
end
it { expect(avis.email).to be_nil }
it { expect(avis.instructeur).to eq(instructeur) }
it { expect(avis2.email).to be_nil }
it { expect(avis2.instructeur).to eq(instructeur) }
end
end
describe "an avis is linked to an expert_procedure" do
let(:procedure) { create(:procedure) }
let(:expert) { create(:expert) }
@ -102,31 +74,14 @@ RSpec.describe Avis, type: :model do
end
end
describe '#try_to_assign_instructeur' do
let!(:instructeur) { create(:instructeur) }
let(:avis) { create(:avis, claimant: claimant, email: email, dossier: create(:dossier)) }
context 'when the email belongs to a instructeur' do
let(:email) { instructeur.email }
it { expect(avis.instructeur).to eq(instructeur) }
it { expect(avis.email).to be_nil }
end
context 'when the email does not belongs to a instructeur' do
let(:email) { 'unknown@email' }
it { expect(avis.instructeur).to be_nil }
it { expect(avis.email).to eq(email) }
end
end
describe "email sanitization" do
subject { Avis.create(claimant: claimant, email: email, dossier: create(:dossier), instructeur: create(:instructeur)) }
let(:expert) { create(:expert) }
let(:procedure) { create(:procedure) }
let!(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) }
subject { Avis.create(claimant: claimant, email: email, experts_procedure: experts_procedure, dossier: create(:dossier)) }
context "when there is no email" do
let(:email) { nil }
it { expect(subject.email).to be_nil }
end
@ -191,12 +146,13 @@ RSpec.describe Avis, type: :model do
let(:procedure) { create(:procedure, :published, instructeurs: instructeurs) }
let(:dossier) { create(:dossier, :en_instruction, procedure: procedure) }
let(:claimant_expert) { create(:instructeur) }
let(:expert) { create(:instructeur) }
let(:another_expert) { create(:instructeur) }
let(:expert) { create(:expert) }
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) }
let(:another_expert) { create(:expert) }
context "when avis claimed by an expert" do
let(:avis) { create(:avis, dossier: dossier, claimant: claimant_expert, instructeur: expert) }
let(:another_avis) { create(:avis, dossier: dossier, claimant: instructeur, instructeur: another_expert) }
let(:avis) { create(:avis, dossier: dossier, claimant: claimant_expert, experts_procedure: experts_procedure) }
let(:another_avis) { create(:avis, dossier: dossier, claimant: instructeur, experts_procedure: experts_procedure) }
it "is revokable by this expert or any instructeurs of the dossier" do
expect(avis.revokable_by?(claimant_expert)).to be_truthy
expect(avis.revokable_by?(another_expert)).to be_falsy
@ -205,8 +161,13 @@ RSpec.describe Avis, type: :model do
end
context "when avis claimed by an instructeur" do
let(:avis) { create(:avis, dossier: dossier, claimant: instructeur, instructeur: expert) }
let(:another_avis) { create(:avis, dossier: dossier, claimant: expert, instructeur: another_expert) }
let(:expert) { create(:expert) }
let(:expert_2) { create(:expert) }
let!(:procedure) { create(:procedure, :published, instructeurs: instructeurs) }
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) }
let(:experts_procedure_2) { ExpertsProcedure.create(expert: expert_2, procedure: procedure) }
let(:avis) { create(:avis, dossier: dossier, claimant: instructeur, experts_procedure: experts_procedure) }
let(:another_avis) { create(:avis, dossier: dossier, claimant: expert, experts_procedure: experts_procedure_2) }
let(:another_instructeur) { create(:instructeur) }
let(:instructeurs) { [instructeur, another_instructeur] }

View file

@ -307,49 +307,50 @@ describe Dossier do
let!(:instructeur) { create(:instructeur) }
let!(:procedure) { create(:procedure, :published, instructeurs: [instructeur]) }
let!(:dossier) { create(:dossier, procedure: procedure, state: Dossier.states.fetch(:en_construction)) }
let!(:expert_1) { create(:instructeur) }
let!(:expert_2) { create(:instructeur) }
let!(:experts_procedure) { ExpertsProcedure.create(expert: expert_1, procedure: procedure) }
let!(:experts_procedure_2) { ExpertsProcedure.create(expert: expert_2, procedure: procedure) }
let!(:expert_1) { create(:expert) }
let!(:expert_2) { create(:expert) }
context 'when there is a public advice asked from the dossiers instructeur' do
let!(:avis) { Avis.create(dossier: dossier, claimant: instructeur, instructeur: expert_1, confidentiel: false) }
let!(:avis) { Avis.create(dossier: dossier, claimant: instructeur, experts_procedure: experts_procedure, confidentiel: false) }
it { expect(dossier.avis_for(instructeur)).to match([avis]) }
it { expect(dossier.avis_for(expert_1)).to match([avis]) }
it { expect(dossier.avis_for(expert_2)).to match([avis]) }
it { expect(dossier.avis_for_instructeur(instructeur)).to match([avis]) }
it { expect(dossier.avis_for_expert(expert_1)).to match([avis]) }
it { expect(dossier.avis_for_expert(expert_2)).to match([avis]) }
end
context 'when there is a private advice asked from the dossiers instructeur' do
let!(:avis) { Avis.create(dossier: dossier, claimant: instructeur, instructeur: expert_1, confidentiel: true) }
let!(:avis) { Avis.create(dossier: dossier, claimant: instructeur, experts_procedure: experts_procedure, confidentiel: true) }
it { expect(dossier.avis_for(instructeur)).to match([avis]) }
it { expect(dossier.avis_for(expert_1)).to match([avis]) }
it { expect(dossier.avis_for(expert_2)).to match([]) }
it { expect(dossier.avis_for_instructeur(instructeur)).to match([avis]) }
it { expect(dossier.avis_for_expert(expert_1)).to match([avis]) }
it { expect(dossier.avis_for_expert(expert_2)).not_to match([avis]) }
end
context 'when there is a public advice asked from one expert to another' do
let!(:avis) { Avis.create(dossier: dossier, claimant: expert_1, instructeur: expert_2, confidentiel: false) }
let!(:avis) { Avis.create(dossier: dossier, claimant: instructeur, experts_procedure: experts_procedure_2, confidentiel: false) }
it { expect(dossier.avis_for(instructeur)).to match([avis]) }
it { expect(dossier.avis_for(expert_1)).to match([avis]) }
it { expect(dossier.avis_for(expert_2)).to match([avis]) }
it { expect(dossier.avis_for_instructeur(instructeur)).to match([avis]) }
it { expect(dossier.avis_for_expert(expert_1)).to match([avis]) }
it { expect(dossier.avis_for_expert(expert_2)).to match([avis]) }
end
context 'when there is a private advice asked from one expert to another' do
let!(:avis) { Avis.create(dossier: dossier, claimant: expert_1, instructeur: expert_2, confidentiel: true) }
let!(:avis) { Avis.create(dossier: dossier, claimant: instructeur, experts_procedure: experts_procedure_2, confidentiel: true) }
it { expect(dossier.avis_for(instructeur)).to match([avis]) }
it { expect(dossier.avis_for(expert_1)).to match([avis]) }
it { expect(dossier.avis_for(expert_2)).to match([avis]) }
it { expect(dossier.avis_for_instructeur(instructeur)).to match([avis]) }
it { expect(dossier.avis_for_expert(expert_1)).not_to match([avis]) }
it { expect(dossier.avis_for_expert(expert_2)).to match([avis]) }
end
context 'when they are a lot of advice' do
let!(:avis_1) { Avis.create(dossier: dossier, claimant: expert_1, instructeur: expert_2, confidentiel: false, created_at: Time.zone.parse('10/01/2010')) }
let!(:avis_2) { Avis.create(dossier: dossier, claimant: expert_1, instructeur: expert_2, confidentiel: false, created_at: Time.zone.parse('9/01/2010')) }
let!(:avis_3) { Avis.create(dossier: dossier, claimant: expert_1, instructeur: expert_2, confidentiel: false, created_at: Time.zone.parse('11/01/2010')) }
let!(:avis_1) { Avis.create(dossier: dossier, claimant: expert_1, experts_procedure: experts_procedure_2, confidentiel: false, created_at: Time.zone.parse('10/01/2010'), tmp_expert_migrated: true) }
let!(:avis_2) { Avis.create(dossier: dossier, claimant: expert_1, experts_procedure: experts_procedure_2, confidentiel: false, created_at: Time.zone.parse('9/01/2010'), tmp_expert_migrated: true) }
let!(:avis_3) { Avis.create(dossier: dossier, claimant: expert_1, experts_procedure: experts_procedure_2, confidentiel: false, created_at: Time.zone.parse('11/01/2010'), tmp_expert_migrated: true) }
it { expect(dossier.avis_for(instructeur)).to match([avis_2, avis_1, avis_3]) }
it { expect(dossier.avis_for(expert_1)).to match([avis_2, avis_1, avis_3]) }
it { expect(dossier.avis_for_instructeur(instructeur)).to match([avis_2, avis_1, avis_3]) }
it { expect(dossier.avis_for_expert(expert_1)).to match([avis_2, avis_1, avis_3]) }
end
end

View file

@ -1,6 +1,7 @@
RSpec.describe ExpertsProcedure, type: :model do
describe '#invited_expert_emails' do
let!(:procedure) { create(:procedure, :published) }
let(:claimant) { create(:instructeur) }
let(:expert) { create(:expert) }
let(:expert2) { create(:expert) }
let(:expert3) { create(:expert) }
@ -13,7 +14,7 @@ RSpec.describe ExpertsProcedure, type: :model do
let!(:dossier) { create(:dossier, procedure: procedure) }
context 'when a procedure has one avis and known instructeur' do
let!(:avis) { create(:avis, dossier: dossier, instructeur: create(:instructeur, email: expert.email), experts_procedure: experts_procedure) }
let!(:avis) { create(:avis, dossier: dossier, claimant: claimant, experts_procedure: experts_procedure) }
it { is_expected.to eq([experts_procedure]) }
it { expect(procedure.experts.count).to eq(1) }

View file

@ -1,12 +1,15 @@
describe 'instructeurs/avis/instruction.html.haml', type: :view do
let(:expert) { create(:instructeur) }
let(:avis) { create(:avis, confidentiel: confidentiel, email: expert.email) }
describe 'experts/avis/instruction.html.haml', type: :view do
let(:expert) { create(:expert) }
let(:claimant) { create(:instructeur) }
let(:procedure) { create(:procedure) }
let!(:avis) { create(:avis, confidentiel: confidentiel, claimant: claimant, experts_procedure: experts_procedure) }
let!(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) }
before do
assign(:avis, avis)
assign(:new_avis, Avis.new)
assign(:dossier, avis.dossier)
allow(view).to receive(:current_instructeur).and_return(avis.instructeur)
allow(view).to receive(:current_expert).and_return(avis.expert)
end
subject { render }

View file

@ -4,7 +4,10 @@ describe 'instructeurs/shared/avis/_list.html.haml', type: :view do
subject { render 'instructeurs/shared/avis/list.html.haml', avis: avis, avis_seen_at: seen_at, current_instructeur: instructeur }
let(:instructeur) { create(:instructeur) }
let(:avis) { [create(:avis, claimant: instructeur)] }
let(:expert) { create(:expert) }
let!(:dossier) { create(:dossier) }
let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: dossier.procedure) }
let(:avis) { [create(:avis, claimant: instructeur, experts_procedure: experts_procedure)] }
let(:seen_at) { avis.first.created_at + 1.hour }
it { is_expected.to have_text(avis.first.introduction) }
@ -17,7 +20,7 @@ describe 'instructeurs/shared/avis/_list.html.haml', type: :view do
end
context 'with an answer' do
let(:avis) { [create(:avis, :with_answer, claimant: instructeur)] }
let(:avis) { [create(:avis, :with_answer, claimant: instructeur, experts_procedure: experts_procedure)] }
it 'renders the answer formatted with newlines' do
expect(subject).to include(simple_format(avis.first.answer))

View file

@ -12,7 +12,6 @@ describe 'shared/dossiers/champs.html.haml', type: :view do
context "there are some champs" do
let(:dossier) { create(:dossier) }
let(:avis) { create :avis, dossier: dossier, instructeur: instructeur }
let(:champ1) { create(:champ_checkbox, dossier: dossier, value: "on") }
let(:champ2) { create(:champ_header_section, dossier: dossier, value: "Section") }
let(:champ3) { create(:champ_explication, dossier: dossier, value: "mazette") }
@ -20,8 +19,6 @@ describe 'shared/dossiers/champs.html.haml', type: :view do
let(:champ5) { create(:champ_textarea, dossier: dossier, value: "Some long text in a textarea.") }
let(:champs) { [champ1, champ2, champ3, champ4, champ5] }
before { dossier.avis << avis }
it "renders titles and values of champs" do
expect(subject).to include(champ1.libelle)
expect(subject).to include(champ1.value)
@ -29,7 +26,6 @@ describe 'shared/dossiers/champs.html.haml', type: :view do
expect(subject).to have_css(".header-section")
expect(subject).to include(champ2.libelle)
expect(subject).to have_link("Dossier nº #{dossier.id}")
expect(subject).to include(dossier.text_summary)
expect(subject).to include(champ5.libelle)