2017-09-06 11:21:29 +02:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe NewGestionnaire::AvisController, type: :controller do
|
2017-12-22 17:11:56 +01:00
|
|
|
context 'with a gestionnaire signed in' do
|
|
|
|
render_views
|
2017-09-06 11:21:29 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
let(:claimant) { create(:gestionnaire) }
|
|
|
|
let(:gestionnaire) { create(:gestionnaire) }
|
|
|
|
let(:procedure) { create(:procedure, :published, gestionnaires: [gestionnaire]) }
|
|
|
|
let(:dossier) { create(:dossier, :en_construction, procedure: procedure) }
|
|
|
|
let!(:avis_without_answer) { Avis.create(dossier: dossier, claimant: claimant, gestionnaire: gestionnaire) }
|
|
|
|
let!(:avis_with_answer) { Avis.create(dossier: dossier, claimant: claimant, gestionnaire: gestionnaire, answer: 'yop') }
|
2017-09-06 11:21:29 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
before { sign_in(gestionnaire) }
|
2017-09-06 11:21:29 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
describe '#index' do
|
|
|
|
before { get :index }
|
2017-09-06 11:21:29 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
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') }
|
2017-09-06 11:21:29 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
context 'with a statut equal to donnes' do
|
2018-01-23 18:16:44 +01:00
|
|
|
before { get :index, params: { statut: 'donnes' } }
|
2017-09-06 11:21:29 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
it { expect(assigns(:statut)).to eq('donnes') }
|
|
|
|
end
|
2017-09-06 11:21:29 +02:00
|
|
|
end
|
2017-09-06 11:26:32 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
describe '#show' do
|
2018-01-23 18:16:44 +01:00
|
|
|
before { get :show, params: { id: avis_without_answer.id } }
|
2017-09-06 16:14:03 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
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
|
2017-09-06 17:06:03 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
describe '#instruction' do
|
2018-01-23 18:16:44 +01:00
|
|
|
before { get :instruction, params: { id: avis_without_answer.id } }
|
2017-09-06 17:06:03 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
it { expect(response).to have_http_status(:success) }
|
|
|
|
it { expect(assigns(:avis)).to eq(avis_without_answer) }
|
|
|
|
it { expect(assigns(:dossier)).to eq(dossier) }
|
2017-09-06 16:14:03 +02:00
|
|
|
end
|
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
describe '#messagerie' do
|
2018-01-23 18:16:44 +01:00
|
|
|
before { get :messagerie, params: { id: avis_without_answer.id } }
|
2017-09-06 17:06:03 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
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
|
2017-11-07 17:53:11 +01:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
describe '#update' do
|
|
|
|
before do
|
2018-01-23 18:16:44 +01:00
|
|
|
patch :update, params: { id: avis_without_answer.id, avis: { answer: 'answer' } }
|
2017-12-22 17:11:56 +01:00
|
|
|
avis_without_answer.reload
|
|
|
|
end
|
2017-11-07 17:53:11 +01:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
it { expect(response).to redirect_to(instruction_avis_path(avis_without_answer)) }
|
|
|
|
it { expect(avis_without_answer.answer).to eq('answer') }
|
|
|
|
it { expect(flash.notice).to eq('Votre réponse est enregistrée.') }
|
2017-11-07 17:53:11 +01:00
|
|
|
end
|
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
describe '#create_commentaire' do
|
|
|
|
let(:file) { nil }
|
|
|
|
let(:scan_result) { true }
|
2017-11-07 17:53:11 +01:00
|
|
|
|
2018-01-23 18:16:44 +01:00
|
|
|
subject { post :create_commentaire, params: { id: avis_without_answer.id, commentaire: { body: 'commentaire body', file: file } } }
|
2017-09-06 17:06:03 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
before do
|
|
|
|
allow(ClamavService).to receive(:safe_file?).and_return(scan_result)
|
|
|
|
end
|
2017-11-07 17:53:11 +01:00
|
|
|
|
|
|
|
it do
|
|
|
|
subject
|
2017-12-22 17:11:56 +01:00
|
|
|
|
|
|
|
expect(response).to redirect_to(messagerie_avis_path(avis_without_answer))
|
|
|
|
expect(dossier.commentaires.map(&:body)).to match(['commentaire body'])
|
2017-11-07 17:53:11 +01:00
|
|
|
end
|
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
context "with a file" do
|
|
|
|
let(:file) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') }
|
|
|
|
|
|
|
|
it do
|
|
|
|
subject
|
|
|
|
expect(Commentaire.last.file.path).to include("piece_justificative_0.pdf")
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect { subject }.to change(Commentaire, :count).by(1) }
|
2017-11-07 17:53:11 +01:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
context "and a virus" do
|
|
|
|
let(:scan_result) { false }
|
2017-11-07 17:53:11 +01:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
it { expect { subject }.not_to change(Commentaire, :count) }
|
|
|
|
end
|
2017-11-07 17:53:11 +01:00
|
|
|
end
|
|
|
|
end
|
2017-09-20 10:52:48 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
describe '#create_avis' do
|
|
|
|
let!(:previous_avis) { Avis.create(dossier: dossier, claimant: claimant, gestionnaire: gestionnaire, confidentiel: previous_avis_confidentiel) }
|
|
|
|
let(:email) { 'a@b.com' }
|
|
|
|
let(:intro) { 'introduction' }
|
|
|
|
let(:created_avis) { Avis.last }
|
2017-09-20 10:52:48 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
before do
|
2018-01-23 18:16:44 +01:00
|
|
|
post :create_avis, params: { id: previous_avis.id, avis: { email: email, introduction: intro, confidentiel: asked_confidentiel } }
|
2017-12-22 17:11:56 +01:00
|
|
|
end
|
2017-09-20 10:52:48 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
context 'when the previous avis is public' do
|
|
|
|
let(:previous_avis_confidentiel) { false }
|
2017-09-20 10:52:48 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
context 'when the user asked for a public avis' do
|
|
|
|
let(:asked_confidentiel) { false }
|
2017-09-20 10:52:48 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
it { expect(created_avis.confidentiel).to be(false) }
|
|
|
|
it { expect(created_avis.email).to eq(email) }
|
|
|
|
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(gestionnaire) }
|
|
|
|
it { expect(response).to redirect_to(instruction_avis_path(previous_avis)) }
|
|
|
|
end
|
2017-09-20 10:52:48 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
context 'when the user asked for a confidentiel avis' do
|
|
|
|
let(:asked_confidentiel) { true }
|
2017-09-20 10:52:48 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
it { expect(created_avis.confidentiel).to be(true) }
|
|
|
|
end
|
2017-09-20 10:52:48 +02:00
|
|
|
end
|
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
context 'when the preivous avis is confidentiel' do
|
|
|
|
let(:previous_avis_confidentiel) { true }
|
2017-09-20 10:52:48 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
context 'when the user asked for a public avis' do
|
|
|
|
let(:asked_confidentiel) { false }
|
2017-09-20 10:52:48 +02:00
|
|
|
|
2017-12-22 17:11:56 +01:00
|
|
|
it { expect(created_avis.confidentiel).to be(true) }
|
|
|
|
end
|
2017-09-20 10:52:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-22 17:34:58 +01:00
|
|
|
|
|
|
|
context 'without a gestionnaire signed in' do
|
|
|
|
describe '.sign_up' do
|
|
|
|
let(:invited_email) { 'invited@avis.com' }
|
|
|
|
let(:dossier) { create(:dossier) }
|
|
|
|
let!(:avis) { create(:avis, email: invited_email, dossier: dossier) }
|
|
|
|
let(:invitations_email) { true }
|
|
|
|
|
|
|
|
context 'when the new gestionnaire 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, 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 gestionnaire has already signed up and belongs to the invitation' do
|
|
|
|
let(:gestionnaire) { create(:gestionnaire, email: invited_email) }
|
|
|
|
let!(:avis) { create(:avis, dossier: dossier, gestionnaire: gestionnaire) }
|
|
|
|
|
|
|
|
context 'when the gestionnaire is authenticated' do
|
|
|
|
before do
|
|
|
|
sign_in gestionnaire
|
|
|
|
get :sign_up, params: { id: avis.id, email: invited_email }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to avis_url(avis) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the gestionnaire is not authenticated' do
|
|
|
|
before do
|
|
|
|
get :sign_up, params: { id: avis.id, email: invited_email }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to new_gestionnaire_session_url }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the gestionnaire has already signed up / is authenticated and does not belong to the invitation' do
|
|
|
|
let(:gestionnaire) { create(:gestionnaire, email: 'other@gmail.com') }
|
|
|
|
let!(:avis) { create(:avis, email: invited_email, dossier: dossier) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in gestionnaire
|
|
|
|
get :sign_up, params: { id: avis.id, email: invited_email }
|
|
|
|
end
|
|
|
|
|
|
|
|
# redirected to dossier but then the gestionnaire gonna be banished !
|
|
|
|
it { is_expected.to redirect_to avis_url(avis) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.create_gestionnaire' do
|
|
|
|
let(:invited_email) { 'invited@avis.com' }
|
|
|
|
let(:dossier) { create(:dossier) }
|
|
|
|
let!(:avis) { create(:avis, email: invited_email, dossier: dossier) }
|
|
|
|
let(:avis_id) { avis.id }
|
|
|
|
let(:password) { '12345678' }
|
|
|
|
let(:created_gestionnaire) { Gestionnaire.find_by(email: invited_email) }
|
|
|
|
let(:invitations_email) { true }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Avis).to receive(:link_avis_to_gestionnaire)
|
|
|
|
expect(Avis).to receive(:avis_exists_and_email_belongs_to_avis?)
|
|
|
|
.with(avis_id.to_s, invited_email)
|
|
|
|
.and_return(invitations_email)
|
|
|
|
|
2018-01-15 15:34:26 +01:00
|
|
|
post :create_gestionnaire, params: {
|
|
|
|
id: avis_id,
|
|
|
|
email: invited_email,
|
|
|
|
gestionnaire: {
|
|
|
|
password: password
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 17:34:58 +01:00
|
|
|
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 gestionnaire creation succeeds' do
|
|
|
|
it { expect(created_gestionnaire).to be_present }
|
|
|
|
it { expect(created_gestionnaire.valid_password?(password)).to be true }
|
|
|
|
|
|
|
|
it { expect(Avis).to have_received(:link_avis_to_gestionnaire) }
|
|
|
|
|
|
|
|
it { expect(subject.current_gestionnaire).to eq(created_gestionnaire) }
|
|
|
|
it { is_expected.to redirect_to avis_index_path }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the gestionnaire creation fails' do
|
|
|
|
let(:password) { '' }
|
|
|
|
|
|
|
|
it { expect(created_gestionnaire).to be_nil }
|
|
|
|
it { is_expected.to redirect_to sign_up_avis_path(avis_id, invited_email) }
|
|
|
|
it { expect(flash.alert).to eq(['Password : Le mot de passe est vide']) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-06 11:21:29 +02:00
|
|
|
end
|