2016-11-14 18:36:09 +01:00
|
|
|
require 'spec_helper'
|
2015-09-22 15:00:59 +02:00
|
|
|
|
|
|
|
describe Backoffice::DossiersController, type: :controller do
|
2016-07-19 16:10:50 +02:00
|
|
|
before do
|
2016-09-01 10:27:22 +02:00
|
|
|
@request.env['HTTP_REFERER'] = TPS::Application::URL
|
2016-07-19 16:10:50 +02:00
|
|
|
end
|
2017-07-18 15:26:33 +02:00
|
|
|
let(:procedure) { create :procedure, :published }
|
|
|
|
let(:procedure2) { create :procedure, :published }
|
2016-07-19 16:10:50 +02:00
|
|
|
|
2017-12-14 15:51:45 +01:00
|
|
|
let(:dossier) { create(:dossier, :with_entreprise, procedure: procedure, state: :en_construction) }
|
|
|
|
let(:dossier2) { create(:dossier, :with_entreprise, procedure: procedure2, state: :en_construction) }
|
2016-07-18 18:24:29 +02:00
|
|
|
let(:dossier_archived) { create(:dossier, :with_entreprise, archived: true) }
|
2015-11-27 15:09:16 +01:00
|
|
|
|
2015-09-22 15:00:59 +02:00
|
|
|
let(:dossier_id) { dossier.id }
|
2017-02-21 10:50:23 +01:00
|
|
|
let(:dossier2_id) { dossier2.id }
|
2015-09-22 15:00:59 +02:00
|
|
|
let(:bad_dossier_id) { Dossier.count + 10 }
|
2017-02-20 18:07:33 +01:00
|
|
|
|
2016-05-20 15:39:17 +02:00
|
|
|
let(:gestionnaire) { create(:gestionnaire, administrateurs: [create(:administrateur)]) }
|
2017-02-20 18:07:33 +01:00
|
|
|
let!(:gestionnaire2) { create(:gestionnaire, administrateurs: [create(:administrateur)]) }
|
2015-09-22 15:00:59 +02:00
|
|
|
|
2016-09-09 15:55:03 +02:00
|
|
|
before do
|
|
|
|
create :assign_to, procedure: procedure, gestionnaire: gestionnaire
|
2017-02-20 18:07:33 +01:00
|
|
|
create :assign_to, procedure: procedure2, gestionnaire: gestionnaire2
|
|
|
|
|
|
|
|
procedure.dossiers << dossier
|
|
|
|
procedure2.dossiers << dossier2
|
2016-09-09 15:55:03 +02:00
|
|
|
end
|
|
|
|
|
2016-12-14 18:41:33 +01:00
|
|
|
describe 'GET #index' do
|
|
|
|
subject { get :index }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in gestionnaire
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when gestionnaire is assign to a procedure' do
|
|
|
|
it { is_expected.to redirect_to backoffice_dossiers_procedure_path(id: procedure.id) }
|
2017-01-05 12:44:15 +01:00
|
|
|
|
|
|
|
context 'when gestionnaire is assign to many proceudure' do
|
|
|
|
before do
|
2017-07-18 15:26:33 +02:00
|
|
|
create :assign_to, procedure: create(:procedure, :published), gestionnaire: gestionnaire
|
|
|
|
create :assign_to, procedure: create(:procedure, :published), gestionnaire: gestionnaire
|
2017-01-05 12:44:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(gestionnaire.procedures.count).to eq 3 }
|
|
|
|
|
|
|
|
context 'when gestionnaire procedure_filter is nil' do
|
|
|
|
it { expect(gestionnaire.procedure_filter).to be_nil }
|
|
|
|
it { is_expected.to redirect_to backoffice_dossiers_procedure_path(id: gestionnaire.procedures.order('libelle ASC').first.id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when gestionnaire procedure_filter is not nil' do
|
|
|
|
let(:procedure_filter_id) { gestionnaire.procedures.order('libelle ASC').last.id }
|
|
|
|
|
|
|
|
before do
|
|
|
|
gestionnaire.update_column :procedure_filter, procedure_filter_id
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when gestionnaire is assign_to the procedure filter id' do
|
|
|
|
it { is_expected.to redirect_to backoffice_dossiers_procedure_path(id: procedure_filter_id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when gestionnaire not any more assign_to the procedure filter id' do
|
|
|
|
before do
|
|
|
|
AssignTo.where(procedure: procedure_filter_id, gestionnaire: gestionnaire).delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(gestionnaire.procedure_filter).to be_nil }
|
|
|
|
it { expect(AssignTo.where(procedure: procedure_filter_id, gestionnaire: gestionnaire).count).to eq 0 }
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to backoffice_dossiers_procedure_path(id: gestionnaire.procedures.order('libelle ASC').first.id) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-14 18:41:33 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when gestionnaire is not assign to a procedure' do
|
|
|
|
before do
|
|
|
|
AssignTo.where(procedure: procedure, gestionnaire: gestionnaire).delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to root_path }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-22 15:00:59 +02:00
|
|
|
describe 'GET #show' do
|
2016-12-27 11:23:19 +01:00
|
|
|
subject { get :show, params: {id: dossier_id} }
|
|
|
|
|
2015-11-02 16:36:52 +01:00
|
|
|
context 'gestionnaire is connected' do
|
2015-09-22 15:00:59 +02:00
|
|
|
before do
|
|
|
|
sign_in gestionnaire
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns http success' do
|
2016-12-27 11:23:19 +01:00
|
|
|
expect(subject).to have_http_status(200)
|
2015-09-22 15:00:59 +02:00
|
|
|
end
|
|
|
|
|
2016-12-27 11:23:19 +01:00
|
|
|
describe 'all notifications unread are changed' do
|
|
|
|
it do
|
2017-05-24 17:51:43 +02:00
|
|
|
expect(Notification).to receive(:where).with(dossier_id: dossier_id.to_s).and_return(Notification::ActiveRecord_Relation)
|
2016-12-27 11:23:19 +01:00
|
|
|
expect(Notification::ActiveRecord_Relation).to receive(:update_all).with(already_read: true).and_return(true)
|
|
|
|
|
|
|
|
subject
|
2015-12-02 18:17:47 +01:00
|
|
|
end
|
2016-12-27 11:23:19 +01:00
|
|
|
end
|
|
|
|
|
2015-12-02 18:17:47 +01:00
|
|
|
context 'when dossier id does not exist' do
|
2016-12-27 11:23:19 +01:00
|
|
|
let(:dossier_id) { bad_dossier_id }
|
|
|
|
|
|
|
|
it { expect(subject).to redirect_to('/backoffice') }
|
2015-09-22 15:00:59 +02:00
|
|
|
end
|
2017-05-24 18:45:31 +02:00
|
|
|
|
|
|
|
describe 'he can invite somebody for avis' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
it { expect(subject.body).to include("Invitez une personne externe à consulter le dossier et à vous donner un avis sur celui ci.") }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and is invited on a dossier' do
|
|
|
|
let(:dossier_invited){ create(:dossier, procedure: create(:procedure)) }
|
|
|
|
let!(:avis){ create(:avis, dossier: dossier_invited, gestionnaire: gestionnaire) }
|
|
|
|
|
|
|
|
subject { get :show, params: { id: dossier_invited.id } }
|
|
|
|
|
|
|
|
render_views
|
|
|
|
|
|
|
|
it { expect(subject.status).to eq(200) }
|
|
|
|
it { expect(subject.body).to include("Votre avis est sollicité sur le dossier") }
|
|
|
|
it { expect(subject.body).to_not include("Invitez une personne externe à consulter le dossier et à vous donner un avis sur celui ci.") }
|
2017-05-24 17:55:00 +02:00
|
|
|
|
|
|
|
describe 'the notifications are not marked as read' do
|
|
|
|
it do
|
|
|
|
expect(Notification).not_to receive(:where)
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
2017-05-24 18:45:31 +02:00
|
|
|
end
|
2015-09-22 15:00:59 +02:00
|
|
|
end
|
|
|
|
|
2015-12-02 18:17:47 +01:00
|
|
|
context 'gestionnaire does not connected but dossier id is correct' do
|
2017-01-03 11:38:39 +01:00
|
|
|
it { is_expected.to redirect_to('/users/sign_in') }
|
2015-09-22 15:00:59 +02:00
|
|
|
end
|
|
|
|
end
|
2015-09-24 16:51:14 +02:00
|
|
|
|
2015-11-13 15:23:21 +01:00
|
|
|
describe 'GET #a_traiter' do
|
|
|
|
context 'when gestionnaire is connected' do
|
|
|
|
before do
|
|
|
|
sign_in gestionnaire
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns http success' do
|
2016-11-15 04:50:18 +01:00
|
|
|
get :index, params: {liste: :a_traiter}
|
2016-12-01 18:05:49 +01:00
|
|
|
expect(response).to have_http_status(302)
|
2015-11-13 15:23:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #termine' do
|
|
|
|
context 'when gestionnaire is connected' do
|
|
|
|
before do
|
|
|
|
sign_in gestionnaire
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns http success' do
|
2016-11-15 04:50:18 +01:00
|
|
|
get :index, params: {liste: :termine}
|
2016-12-01 18:05:49 +01:00
|
|
|
expect(response).to have_http_status(302)
|
2015-11-13 15:23:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-07 16:01:32 +02:00
|
|
|
describe 'GET #list_fake' do
|
|
|
|
context 'when gestionnaire is connected' do
|
|
|
|
before do
|
|
|
|
sign_in gestionnaire
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns http success' do
|
2016-11-15 04:50:18 +01:00
|
|
|
get :index, params: {liste: :list_fake}
|
2016-12-01 18:05:49 +01:00
|
|
|
expect(response).to redirect_to(backoffice_dossiers_procedure_path(id: gestionnaire.procedures.first.id))
|
2016-10-07 16:01:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 17:13:31 +02:00
|
|
|
describe 'POST #search' do
|
|
|
|
describe 'by id' do
|
|
|
|
context 'when I am logged as a gestionnaire' do
|
|
|
|
before do
|
|
|
|
sign_in gestionnaire
|
|
|
|
end
|
2017-02-20 18:07:33 +01:00
|
|
|
|
2017-04-12 17:13:31 +02:00
|
|
|
context 'when I own the dossier' do
|
|
|
|
before :each do
|
|
|
|
post :search, params: { q: dossier_id }
|
|
|
|
end
|
2015-11-17 18:21:03 +01:00
|
|
|
|
2017-04-12 17:13:31 +02:00
|
|
|
it 'returns http success' do
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
2017-02-20 18:07:33 +01:00
|
|
|
|
2017-04-12 17:13:31 +02:00
|
|
|
it 'returns the expected dossier' do
|
|
|
|
expect(assigns(:dossiers).count).to eq(1)
|
|
|
|
expect(assigns(:dossiers).first.id).to eq(dossier_id)
|
2017-02-20 18:07:33 +01:00
|
|
|
end
|
2017-04-12 17:13:31 +02:00
|
|
|
end
|
2017-02-20 18:07:33 +01:00
|
|
|
|
2017-04-12 17:13:31 +02:00
|
|
|
context 'when I do not own the dossier' do
|
|
|
|
before :each do
|
|
|
|
post :search, params: { q: dossier2_id }
|
|
|
|
end
|
2017-02-20 18:07:33 +01:00
|
|
|
|
2017-04-12 17:13:31 +02:00
|
|
|
it 'returns http success' do
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
2017-02-21 16:47:59 +01:00
|
|
|
|
2017-09-06 14:36:09 +02:00
|
|
|
it 'does not return the dossier' do
|
|
|
|
expect(assigns(:dossiers).pluck(:id)).not_to include(dossier2_id)
|
2017-02-20 18:07:33 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-11-17 18:21:03 +01:00
|
|
|
end
|
|
|
|
|
2016-08-25 15:21:25 +02:00
|
|
|
describe 'POST #receive' do
|
2015-11-02 16:36:52 +01:00
|
|
|
before do
|
2017-12-14 15:51:45 +01:00
|
|
|
dossier.en_construction!
|
2015-11-02 16:36:52 +01:00
|
|
|
sign_in gestionnaire
|
2017-07-17 14:34:41 +02:00
|
|
|
post :receive, params: { dossier_id: dossier_id }
|
|
|
|
dossier.reload
|
2015-11-02 16:36:52 +01:00
|
|
|
end
|
2015-10-09 12:05:07 +02:00
|
|
|
|
2017-12-14 15:53:02 +01:00
|
|
|
it { expect(dossier.state).to eq('en_instruction') }
|
2017-07-17 14:34:41 +02:00
|
|
|
it { is_expected.to redirect_to backoffice_dossier_path(dossier) }
|
|
|
|
it { expect(gestionnaire.follow?(dossier)).to be true }
|
2016-08-25 15:21:25 +02:00
|
|
|
end
|
|
|
|
|
2017-06-01 21:23:47 +02:00
|
|
|
describe 'POST #process_dossier' do
|
|
|
|
context "with refuse" do
|
2017-06-07 11:42:14 +02:00
|
|
|
before do
|
2017-12-14 15:53:02 +01:00
|
|
|
dossier.en_instruction!
|
2017-06-07 11:42:14 +02:00
|
|
|
sign_in gestionnaire
|
2017-06-01 21:23:47 +02:00
|
|
|
end
|
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
subject { post :process_dossier, params: { process_action: "refuse", dossier_id: dossier_id} }
|
2017-06-01 21:23:47 +02:00
|
|
|
|
2017-12-04 18:15:40 +01:00
|
|
|
it 'change state to refuse' do
|
2017-06-07 11:42:14 +02:00
|
|
|
subject
|
|
|
|
|
|
|
|
dossier.reload
|
2017-12-04 18:15:40 +01:00
|
|
|
expect(dossier.state).to eq('refuse')
|
2017-06-01 21:23:47 +02:00
|
|
|
end
|
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
it 'Notification email is sent' do
|
|
|
|
expect(NotificationMailer).to receive(:send_notification)
|
2017-06-09 22:29:48 +02:00
|
|
|
.with(dossier, kind_of(Mails::RefusedMail), nil).and_return(NotificationMailer)
|
2017-06-07 11:42:14 +02:00
|
|
|
expect(NotificationMailer).to receive(:deliver_now!)
|
2017-06-01 21:23:47 +02:00
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
subject
|
2017-06-01 21:23:47 +02:00
|
|
|
end
|
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
it { is_expected.to redirect_to backoffice_dossier_path(id: dossier.id) }
|
2016-08-25 18:02:56 +02:00
|
|
|
end
|
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
context "with without_continuation" do
|
|
|
|
before do
|
2017-12-14 15:53:02 +01:00
|
|
|
dossier.en_instruction!
|
2017-06-07 11:42:14 +02:00
|
|
|
sign_in gestionnaire
|
|
|
|
end
|
2016-08-25 18:02:56 +02:00
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
subject { post :process_dossier, params: { process_action: "without_continuation", dossier_id: dossier_id} }
|
2016-08-25 18:02:56 +02:00
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
it 'change state to without_continuation' do
|
|
|
|
subject
|
2016-12-19 17:11:26 +01:00
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
dossier.reload
|
|
|
|
expect(dossier.state).to eq('without_continuation')
|
|
|
|
end
|
2016-08-25 18:02:56 +02:00
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
it 'Notification email is sent' do
|
|
|
|
expect(NotificationMailer).to receive(:send_notification)
|
2017-06-09 22:29:48 +02:00
|
|
|
.with(dossier, kind_of(Mails::WithoutContinuationMail), nil).and_return(NotificationMailer)
|
2017-06-07 11:42:14 +02:00
|
|
|
expect(NotificationMailer).to receive(:deliver_now!)
|
2016-08-25 18:02:56 +02:00
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
subject
|
|
|
|
end
|
2016-08-25 18:02:56 +02:00
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
it { is_expected.to redirect_to backoffice_dossier_path(id: dossier.id) }
|
2016-08-25 18:02:56 +02:00
|
|
|
end
|
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
context "with close" do
|
2017-06-09 22:29:48 +02:00
|
|
|
let(:expected_attestation) { nil }
|
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
before do
|
2017-12-14 15:53:02 +01:00
|
|
|
dossier.en_instruction!
|
2017-06-07 11:42:14 +02:00
|
|
|
sign_in gestionnaire
|
2017-06-09 22:29:48 +02:00
|
|
|
|
|
|
|
expect(NotificationMailer).to receive(:send_notification)
|
|
|
|
.with(dossier, kind_of(Mails::ClosedMail), expected_attestation)
|
|
|
|
.and_return(NotificationMailer)
|
|
|
|
|
|
|
|
expect(NotificationMailer).to receive(:deliver_now!)
|
2017-06-07 11:42:14 +02:00
|
|
|
end
|
2016-12-19 17:11:26 +01:00
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
subject { post :process_dossier, params: { process_action: "close", dossier_id: dossier_id} }
|
2016-08-25 18:02:56 +02:00
|
|
|
|
2017-12-04 18:00:12 +01:00
|
|
|
it 'change state to accepte' do
|
2017-06-07 11:42:14 +02:00
|
|
|
subject
|
2016-08-25 15:21:25 +02:00
|
|
|
|
2017-06-07 11:42:14 +02:00
|
|
|
dossier.reload
|
2017-12-04 18:00:12 +01:00
|
|
|
expect(dossier.state).to eq('accepte')
|
2017-06-07 11:42:14 +02:00
|
|
|
end
|
2015-10-09 12:05:07 +02:00
|
|
|
|
2017-06-09 22:29:48 +02:00
|
|
|
context 'when the dossier does not have any attestation' do
|
|
|
|
it 'Notification email is sent' do
|
|
|
|
subject
|
|
|
|
end
|
2017-06-07 11:42:14 +02:00
|
|
|
end
|
2016-08-25 18:02:56 +02:00
|
|
|
|
2017-06-09 22:29:48 +02:00
|
|
|
context 'when the dossier has an attestation' do
|
|
|
|
before do
|
2017-06-16 14:10:08 +02:00
|
|
|
attestation = Attestation.new
|
|
|
|
allow(attestation).to receive(:pdf).and_return(double(read: 'pdf', size: 2.megabytes))
|
|
|
|
allow(attestation).to receive(:emailable?).and_return(emailable)
|
|
|
|
|
2017-06-19 13:47:00 +02:00
|
|
|
expect_any_instance_of(Dossier).to receive(:reload)
|
2017-06-16 14:10:08 +02:00
|
|
|
allow_any_instance_of(Dossier).to receive(:build_attestation).and_return(attestation)
|
2017-06-09 22:29:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'emailable' do
|
|
|
|
let(:emailable) { true }
|
|
|
|
let(:expected_attestation) { 'pdf' }
|
|
|
|
|
|
|
|
it 'Notification email is sent with the attestation' do
|
|
|
|
subject
|
2017-08-21 16:34:52 +02:00
|
|
|
|
|
|
|
is_expected.to redirect_to backoffice_dossier_path(id: dossier.id)
|
2017-06-09 22:29:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the dossier has an attestation not emailable' do
|
|
|
|
let(:emailable) { false }
|
|
|
|
let(:expected_attestation) { nil }
|
|
|
|
|
|
|
|
it 'Notification email is sent without the attestation' do
|
|
|
|
expect(controller).to receive(:capture_message)
|
|
|
|
|
|
|
|
subject
|
2017-08-21 16:34:52 +02:00
|
|
|
|
|
|
|
is_expected.to redirect_to backoffice_dossier_path(id: dossier.id)
|
2017-06-09 22:29:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-25 18:02:56 +02:00
|
|
|
end
|
2015-10-09 12:05:07 +02:00
|
|
|
end
|
2016-07-18 18:24:29 +02:00
|
|
|
|
2017-02-28 15:14:27 +01:00
|
|
|
describe 'POST #reopen' do
|
|
|
|
before do
|
2017-12-14 15:53:02 +01:00
|
|
|
dossier.en_instruction!
|
2017-02-28 15:14:27 +01:00
|
|
|
sign_in gestionnaire
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { post :reopen, params: {dossier_id: dossier_id} }
|
|
|
|
|
2017-12-14 15:51:45 +01:00
|
|
|
it 'change state to en_construction' do
|
2017-02-28 15:14:27 +01:00
|
|
|
subject
|
|
|
|
|
|
|
|
dossier.reload
|
2017-12-14 15:51:45 +01:00
|
|
|
expect(dossier.state).to eq('en_construction')
|
2017-02-28 15:14:27 +01:00
|
|
|
end
|
|
|
|
|
2017-08-02 16:20:01 +02:00
|
|
|
it { is_expected.to redirect_to backoffice_dossier_path(id: dossier_id) }
|
2017-02-28 15:14:27 +01:00
|
|
|
end
|
2017-02-08 17:35:28 +01:00
|
|
|
|
|
|
|
describe 'POST #archive' do
|
|
|
|
before do
|
|
|
|
dossier.update(archived: false)
|
|
|
|
sign_in gestionnaire
|
|
|
|
end
|
|
|
|
|
2017-02-28 14:33:05 +01:00
|
|
|
subject { post :archive, params: {id: dossier_id} }
|
2017-02-08 17:35:28 +01:00
|
|
|
|
|
|
|
it 'change state to archived' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
dossier.reload
|
|
|
|
expect(dossier.archived).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to backoffice_dossiers_path }
|
|
|
|
end
|
2015-09-22 15:00:59 +02:00
|
|
|
end
|