2015-08-10 11:05:06 +02:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-09-23 19:20:03 +02:00
|
|
|
describe Users::DescriptionController, type: :controller do
|
2015-09-24 11:17:17 +02:00
|
|
|
let(:user) { create(:user) }
|
2016-02-04 14:08:35 +01:00
|
|
|
|
|
|
|
let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, cerfa_flag: true) }
|
2016-03-15 17:17:56 +01:00
|
|
|
let(:dossier) { create(:dossier, procedure: procedure, user: user) }
|
2016-02-04 14:08:35 +01:00
|
|
|
|
2015-08-20 16:25:54 +02:00
|
|
|
let(:dossier_id) { dossier.id }
|
2016-02-04 14:08:35 +01:00
|
|
|
let(:bad_dossier_id) { Dossier.count + 10000 }
|
2015-08-10 11:05:06 +02:00
|
|
|
|
2015-10-09 14:43:19 +02:00
|
|
|
before do
|
|
|
|
sign_in dossier.user
|
|
|
|
end
|
|
|
|
|
2015-08-20 16:52:36 +02:00
|
|
|
describe 'GET #show' do
|
2015-10-09 14:43:19 +02:00
|
|
|
context 'user is not connected' do
|
|
|
|
before do
|
|
|
|
sign_out dossier.user
|
|
|
|
end
|
|
|
|
|
2015-11-02 16:36:52 +01:00
|
|
|
it 'redirects to users/sign_in' do
|
2015-10-09 14:43:19 +02:00
|
|
|
get :show, dossier_id: dossier_id
|
|
|
|
expect(response).to redirect_to('/users/sign_in')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-20 16:52:36 +02:00
|
|
|
it 'returns http success' do
|
2015-08-18 10:43:36 +02:00
|
|
|
get :show, dossier_id: dossier_id
|
2015-08-10 11:05:06 +02:00
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirection vers start si mauvais dossier ID' do
|
2015-08-18 10:43:36 +02:00
|
|
|
get :show, dossier_id: bad_dossier_id
|
2015-10-09 17:33:33 +02:00
|
|
|
expect(response).to redirect_to(root_path)
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
2015-10-09 17:33:33 +02:00
|
|
|
|
|
|
|
it_behaves_like "not owner of dossier", :show
|
2016-01-25 15:54:21 +01:00
|
|
|
|
|
|
|
describe 'before_action authorized_routes?' do
|
|
|
|
context 'when dossier does not have a valid state' do
|
|
|
|
before do
|
|
|
|
dossier.state = 'validated'
|
|
|
|
dossier.save
|
|
|
|
|
|
|
|
get :show, dossier_id: dossier.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to root_path }
|
|
|
|
end
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2015-08-20 16:52:36 +02:00
|
|
|
describe 'POST #create' do
|
|
|
|
let(:timestamp) { Time.now }
|
|
|
|
let(:nom_projet) { 'Projet de test' }
|
|
|
|
let(:description) { 'Description de test Coucou, je suis un saut à la ligne Je suis un double saut la ligne.' }
|
2015-08-11 15:39:16 +02:00
|
|
|
|
2015-09-21 17:59:03 +02:00
|
|
|
let(:name_piece_justificative) { 'dossierPDF.pdf' }
|
|
|
|
let(:name_piece_justificative_0) { 'piece_justificative_0.pdf' }
|
|
|
|
let(:name_piece_justificative_1) { 'piece_justificative_1.pdf' }
|
2015-08-11 15:39:16 +02:00
|
|
|
|
2015-09-21 17:59:03 +02:00
|
|
|
let(:cerfa_pdf) { Rack::Test::UploadedFile.new("./spec/support/files/#{name_piece_justificative}", 'application/pdf') }
|
|
|
|
let(:piece_justificative_0) { Rack::Test::UploadedFile.new("./spec/support/files/#{name_piece_justificative_0}", 'application/pdf') }
|
|
|
|
let(:piece_justificative_1) { Rack::Test::UploadedFile.new("./spec/support/files/#{name_piece_justificative_1}", 'application/pdf') }
|
2015-08-11 15:39:16 +02:00
|
|
|
|
2015-08-10 11:05:06 +02:00
|
|
|
context 'Tous les attributs sont bons' do
|
2015-08-20 16:52:36 +02:00
|
|
|
# TODO separer en deux tests : check donnees et check redirect
|
2015-09-28 10:32:41 +02:00
|
|
|
describe 'Premier enregistrement des données' do
|
|
|
|
before do
|
|
|
|
dossier.draft!
|
2015-11-02 18:56:41 +01:00
|
|
|
post :create, dossier_id: dossier_id, nom_projet: nom_projet, description: description
|
2015-09-28 10:32:41 +02:00
|
|
|
dossier.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirection vers la page recapitulative" do
|
|
|
|
expect(response).to redirect_to("/users/dossiers/#{dossier_id}/recapitulatif")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'etat du dossier est soumis' do
|
2015-11-02 15:31:15 +01:00
|
|
|
expect(dossier.state).to eq('initiated')
|
2015-09-28 10:32:41 +02:00
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2015-08-20 16:52:36 +02:00
|
|
|
# TODO changer les valeurs des champs et check in bdd
|
2015-09-28 10:32:41 +02:00
|
|
|
context 'En train de manipuler un dossier non brouillon' do
|
2015-08-10 11:05:06 +02:00
|
|
|
before do
|
2015-11-02 15:31:15 +01:00
|
|
|
dossier.initiated!
|
2015-11-02 18:56:41 +01:00
|
|
|
post :create, dossier_id: dossier_id, nom_projet: nom_projet, description: description
|
2015-09-28 10:32:41 +02:00
|
|
|
dossier.reload
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'Redirection vers la page récapitulatif' do
|
2015-09-23 19:20:03 +02:00
|
|
|
expect(response).to redirect_to("/users/dossiers/#{dossier_id}/recapitulatif")
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
2015-09-28 10:32:41 +02:00
|
|
|
|
|
|
|
it 'etat du dossier n\'est pas soumis' do
|
|
|
|
expect(dossier.state).not_to eq('draft')
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'Attribut(s) manquant(s)' do
|
2015-08-25 10:19:39 +02:00
|
|
|
subject {
|
|
|
|
post :create,
|
2016-03-15 17:17:56 +01:00
|
|
|
dossier_id: dossier_id,
|
|
|
|
nom_projet: nom_projet,
|
|
|
|
description: description
|
2015-08-25 10:19:39 +02:00
|
|
|
}
|
|
|
|
before { subject }
|
|
|
|
|
|
|
|
context 'nom_projet empty' do
|
|
|
|
let(:nom_projet) { '' }
|
|
|
|
it { is_expected.to render_template(:show) }
|
|
|
|
it { expect(flash[:alert]).to be_present }
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2015-08-25 10:19:39 +02:00
|
|
|
context 'description empty' do
|
|
|
|
let(:description) { '' }
|
|
|
|
it { is_expected.to render_template(:show) }
|
|
|
|
it { expect(flash[:alert]).to be_present }
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
2016-02-02 18:37:38 +01:00
|
|
|
end
|
2015-08-11 15:39:16 +02:00
|
|
|
|
2016-02-02 18:37:38 +01:00
|
|
|
context 'Quand la procédure accepte les CERFA' do
|
|
|
|
context 'Sauvegarde du CERFA PDF' do
|
|
|
|
before do
|
|
|
|
post :create, dossier_id: dossier_id,
|
2016-03-15 17:17:56 +01:00
|
|
|
nom_projet: nom_projet,
|
|
|
|
description: description,
|
|
|
|
cerfa_pdf: cerfa_pdf
|
2016-02-02 18:37:38 +01:00
|
|
|
dossier.reload
|
|
|
|
end
|
2015-08-11 15:39:16 +02:00
|
|
|
|
2016-03-16 15:34:35 +01:00
|
|
|
context 'when a CERFA PDF is send' do
|
|
|
|
subject { dossier.cerfa.first }
|
|
|
|
|
2016-02-02 18:37:38 +01:00
|
|
|
it 'content' do
|
|
|
|
expect(subject['content']).to eq(name_piece_justificative)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'dossier_id' do
|
|
|
|
expect(subject.dossier_id).to eq(dossier_id)
|
|
|
|
end
|
2016-03-17 17:33:38 +01:00
|
|
|
|
|
|
|
it { expect(subject.user).to eq user }
|
2015-08-11 15:39:16 +02:00
|
|
|
end
|
|
|
|
|
2016-03-16 15:34:35 +01:00
|
|
|
context 'les anciens CERFA PDF ne sont pas écrasées' do
|
|
|
|
let(:cerfas) { Cerfa.where(dossier_id: dossier_id) }
|
2016-02-04 14:08:35 +01:00
|
|
|
|
|
|
|
before do
|
2016-02-02 18:37:38 +01:00
|
|
|
post :create, dossier_id: dossier_id, nom_projet: nom_projet, description: description, cerfa_pdf: cerfa_pdf
|
2016-02-04 14:08:35 +01:00
|
|
|
end
|
|
|
|
|
2016-03-16 15:34:35 +01:00
|
|
|
it "il y a deux CERFA PDF pour ce dossier" do
|
|
|
|
expect(cerfas.size).to eq 2
|
2016-02-02 18:37:38 +01:00
|
|
|
end
|
2015-08-11 15:39:16 +02:00
|
|
|
end
|
|
|
|
end
|
2016-02-02 18:37:38 +01:00
|
|
|
end
|
2015-08-11 15:39:16 +02:00
|
|
|
|
2016-02-02 18:37:38 +01:00
|
|
|
context 'Quand la procédure n\'accepte pas les CERFA' do
|
|
|
|
context 'Sauvegarde du CERFA PDF' do
|
|
|
|
let!(:procedure) { create(:procedure) }
|
2016-03-15 17:17:56 +01:00
|
|
|
before do
|
2016-02-02 18:37:38 +01:00
|
|
|
post :create, dossier_id: dossier_id,
|
2016-03-15 17:17:56 +01:00
|
|
|
nom_projet: nom_projet,
|
|
|
|
description: description,
|
|
|
|
cerfa_pdf: cerfa_pdf
|
2016-02-02 18:37:38 +01:00
|
|
|
dossier.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'un CERFA PDF est envoyé' do
|
|
|
|
it { expect(dossier.cerfa_available?).to be_falsey }
|
|
|
|
end
|
2015-08-11 15:39:16 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-15 17:17:56 +01:00
|
|
|
describe 'Sauvegarde des champs' do
|
2015-11-03 16:16:39 +01:00
|
|
|
let(:champs_dossier) { dossier.champs }
|
|
|
|
let(:dossier_champs_first) { 'test value' }
|
2016-02-04 14:08:35 +01:00
|
|
|
|
2015-11-03 16:16:39 +01:00
|
|
|
before do
|
|
|
|
post :create, {dossier_id: dossier_id,
|
|
|
|
nom_projet: nom_projet,
|
|
|
|
description: description,
|
|
|
|
champs: {
|
|
|
|
"'#{dossier.champs.first.id}'" => dossier_champs_first
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dossier.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(dossier.champs.first.value).to eq(dossier_champs_first) }
|
2016-03-15 17:17:56 +01:00
|
|
|
it { expect(response).to redirect_to users_dossier_recapitulatif_path }
|
2015-11-03 16:16:39 +01:00
|
|
|
|
|
|
|
context 'when champs value is empty' do
|
2016-03-15 17:17:56 +01:00
|
|
|
let(:dossier_champs_first) { '' }
|
2015-11-03 16:16:39 +01:00
|
|
|
|
|
|
|
it { expect(dossier.champs.first.value).to eq(dossier_champs_first) }
|
2016-03-15 17:17:56 +01:00
|
|
|
it { expect(response).to redirect_to users_dossier_recapitulatif_path }
|
|
|
|
|
|
|
|
context 'when champs is mandatory' do
|
|
|
|
let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ_mandatory, cerfa_flag: true) }
|
|
|
|
|
|
|
|
it { expect(response).not_to redirect_to users_dossier_recapitulatif_path }
|
|
|
|
it { expect(flash[:alert]).to be_present }
|
|
|
|
end
|
2015-11-03 16:16:39 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-21 17:59:03 +02:00
|
|
|
context 'Sauvegarde des pièces justificatives' do
|
2016-03-15 17:17:56 +01:00
|
|
|
let(:all_pj_type) { dossier.procedure.type_de_piece_justificative_ids }
|
2015-08-11 15:39:16 +02:00
|
|
|
before do
|
2015-09-21 17:59:03 +02:00
|
|
|
post :create, {dossier_id: dossier_id,
|
2016-03-15 17:17:56 +01:00
|
|
|
nom_projet: nom_projet,
|
|
|
|
description: description,
|
|
|
|
'piece_justificative_'+all_pj_type[0].to_s => piece_justificative_0,
|
|
|
|
'piece_justificative_'+all_pj_type[1].to_s => piece_justificative_1}
|
2015-08-20 15:15:20 +02:00
|
|
|
dossier.reload
|
2015-08-11 15:39:16 +02:00
|
|
|
end
|
|
|
|
|
2015-09-21 17:59:03 +02:00
|
|
|
context 'for piece 0' do
|
2016-03-17 14:50:10 +01:00
|
|
|
subject { dossier.retrieve_last_piece_justificative_by_type all_pj_type[0].to_s }
|
2015-08-20 15:15:20 +02:00
|
|
|
it { expect(subject.content).not_to be_nil }
|
2016-03-17 17:33:38 +01:00
|
|
|
it { expect(subject.user).to eq user }
|
2015-08-20 15:15:20 +02:00
|
|
|
end
|
2015-09-21 17:59:03 +02:00
|
|
|
context 'for piece 1' do
|
2016-03-17 14:50:10 +01:00
|
|
|
subject { dossier.retrieve_last_piece_justificative_by_type all_pj_type[1].to_s }
|
2015-08-20 15:15:20 +02:00
|
|
|
it { expect(subject.content).not_to be_nil }
|
2016-03-17 17:33:38 +01:00
|
|
|
it { expect(subject.user).to eq user }
|
2015-08-11 15:39:16 +02:00
|
|
|
end
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
end
|