demarches-normaliennes/spec/models/dossier_spec.rb

567 lines
19 KiB
Ruby
Raw Normal View History

require 'spec_helper'
describe Dossier do
let(:user) { create(:user) }
2015-08-17 11:13:21 +02:00
describe 'database columns' do
it { is_expected.to have_db_column(:autorisation_donnees) }
it { is_expected.to have_db_column(:nom_projet) }
2015-09-22 11:04:04 +02:00
it { is_expected.to have_db_column(:created_at) }
it { is_expected.to have_db_column(:updated_at) }
it { is_expected.to have_db_column(:state) }
it { is_expected.to have_db_column(:procedure_id) }
it { is_expected.to have_db_column(:user_id) }
2015-08-17 11:13:21 +02:00
end
describe 'associations' do
it { is_expected.to belong_to(:procedure) }
it { is_expected.to have_many(:pieces_justificatives) }
2015-11-03 10:48:40 +01:00
it { is_expected.to have_many(:champs) }
2015-08-17 11:13:21 +02:00
it { is_expected.to have_many(:commentaires) }
it { is_expected.to have_many(:quartier_prioritaires) }
2016-01-18 12:03:18 +01:00
it { is_expected.to have_many(:cadastres) }
it { is_expected.to have_many(:cerfa) }
2015-08-17 11:13:21 +02:00
it { is_expected.to have_one(:etablissement) }
it { is_expected.to have_one(:entreprise) }
2015-09-23 12:16:21 +02:00
it { is_expected.to belong_to(:user) }
it { is_expected.to have_many(:invites) }
2015-08-17 11:13:21 +02:00
end
2015-08-20 17:30:17 +02:00
describe 'delegation' do
2015-08-20 14:24:26 +02:00
it { is_expected.to delegate_method(:siren).to(:entreprise) }
it { is_expected.to delegate_method(:siret).to(:etablissement) }
it { is_expected.to delegate_method(:types_de_piece_justificative).to(:procedure) }
2015-11-05 11:21:44 +01:00
it { is_expected.to delegate_method(:types_de_champ).to(:procedure) }
2015-08-20 14:24:26 +02:00
end
2015-08-21 11:37:13 +02:00
describe 'validation' do
2015-08-25 10:19:39 +02:00
context 'nom_projet' do
it { is_expected.to allow_value(nil).for(:nom_projet) }
it { is_expected.not_to allow_value('').for(:nom_projet) }
it { is_expected.to allow_value('mon super projet').for(:nom_projet) }
end
2015-08-21 11:37:13 +02:00
end
2015-08-21 11:37:13 +02:00
describe 'methods' do
2016-02-04 14:08:35 +01:00
let(:dossier) { create(:dossier, :with_entreprise, user: user) }
2015-08-21 11:37:13 +02:00
let(:entreprise) { dossier.entreprise }
let(:etablissement) { dossier.etablissement }
2015-08-18 14:22:16 +02:00
2015-08-21 11:37:13 +02:00
subject { dossier }
2015-08-18 14:22:16 +02:00
describe '#types_de_piece_justificative' do
subject { dossier.types_de_piece_justificative }
2015-08-21 11:37:13 +02:00
it 'returns list of required piece justificative' do
expect(subject.size).to eq(2)
expect(subject).to include(TypeDePieceJustificative.find(TypeDePieceJustificative.first.id))
2015-08-21 11:37:13 +02:00
end
2015-08-18 14:22:16 +02:00
end
2015-08-21 11:37:13 +02:00
describe 'creation' do
2016-02-02 18:37:38 +01:00
describe 'Procedure accepts cerfa upload' do
let(:procedure) { create(:procedure, cerfa_flag: true) }
let(:dossier) { create(:dossier, :with_entreprise, procedure: procedure, user: user) }
it 'create default cerfa' do
expect { subject.to change(Cerfa.count).by(1) }
expect { subject.cerfa_available.to be_truthy }
end
it 'link cerfa to dossier' do
expect { subject.cerfa.to eq(Cerfa.last) }
end
2015-08-21 11:37:13 +02:00
end
2015-08-20 15:15:20 +02:00
2016-02-02 18:37:38 +01:00
describe 'Procedure does not accept cerfa upload' do
let(:procedure) { create(:procedure, cerfa_flag: false) }
2016-02-04 14:08:35 +01:00
let(:dossier) { create(:dossier, :with_entreprise, user: user) }
2016-02-02 18:37:38 +01:00
it 'default cerfa is not created' do
expect { subject.to change(Cerfa.count).by(0) }
expect { subject.cerfa.to eq(nil) }
expect { subject.cerfa_available.to be_falsey }
end
2015-08-21 11:37:13 +02:00
end
2015-08-20 15:15:20 +02:00
end
describe '#retrieve_last_piece_justificative_by_type', vcr: { cassette_name: 'models_dossier_retrieve_last_piece_justificative_by_type' } do
2016-03-17 14:50:10 +01:00
let(:types_de_pj_dossier) { dossier.procedure.types_de_piece_justificative }
subject { dossier.retrieve_last_piece_justificative_by_type types_de_pj_dossier.first }
before do
2016-03-17 14:50:10 +01:00
create :piece_justificative, :rib, dossier: dossier, type_de_piece_justificative: types_de_pj_dossier.first
end
2015-08-21 11:37:13 +02:00
it 'returns piece justificative with given type' do
2016-03-17 14:50:10 +01:00
expect(subject.type).to eq(types_de_pj_dossier.first.id)
end
end
describe '#build_default_champs' do
context 'when dossier is linked to a procedure' do
2016-02-04 14:08:35 +01:00
let(:dossier) { create(:dossier, user: user) }
it 'build all champs needed' do
expect(dossier.champs.count).to eq(1)
end
end
end
describe '#save' do
2016-02-02 18:37:38 +01:00
subject { build(:dossier, procedure: procedure, user: user) }
let!(:procedure) { create(:procedure) }
2016-06-13 12:03:05 +02:00
context 'when is linked to a procedure' do
it 'creates default champs' do
expect(subject).to receive(:build_default_champs)
2016-02-02 18:37:38 +01:00
subject.save
end
end
context 'when is not linked to a procedure' do
2016-06-13 12:03:05 +02:00
subject { create(:dossier, procedure: nil, user: user) }
2015-11-04 17:27:01 +01:00
it 'does not create default champs' do
expect(subject).not_to receive(:build_default_champs)
2016-06-13 12:03:05 +02:00
subject.update_attributes(nom_projet: 'plop')
end
end
end
2015-09-24 10:27:14 +02:00
describe '#next_step' do
2016-01-18 17:18:04 +01:00
let(:dossier) { create(:dossier) }
2015-09-24 10:27:14 +02:00
let(:role) { 'user' }
let(:action) { 'initiate' }
2015-09-24 10:27:14 +02:00
subject { dossier.next_step! role, action }
context 'when action is not valid' do
let(:action) { 'test' }
2015-11-16 15:58:35 +01:00
it { expect { subject }.to raise_error('action is not valid') }
2015-09-24 10:27:14 +02:00
end
context 'when role is not valid' do
let(:role) { 'test' }
2015-11-16 15:58:35 +01:00
it { expect { subject }.to raise_error('role is not valid') }
2015-09-24 10:27:14 +02:00
end
context 'when dossier is at state draft' do
before do
dossier.draft!
end
context 'when user is connected' do
let(:role) { 'user' }
context 'when he updates dossier informations' do
2015-11-16 15:58:35 +01:00
let(:action) { 'update' }
2015-09-24 10:27:14 +02:00
it { is_expected.to eq('draft') }
end
context 'when he posts a comment' do
2015-11-16 15:58:35 +01:00
let(:action) { 'comment' }
2015-09-24 10:27:14 +02:00
it { is_expected.to eq('draft') }
end
context 'when he initiate a dossier' do
let(:action) { 'initiate' }
2015-09-24 10:27:14 +02:00
it { is_expected.to eq('initiated') }
2015-09-24 10:27:14 +02:00
end
end
end
context 'when dossier is at state initiated' do
2015-09-24 10:27:14 +02:00
before do
dossier.initiated!
2015-09-24 10:27:14 +02:00
end
context 'when user is connect' do
let(:role) { 'user' }
context 'when is update dossier informations' do
let(:action) { 'update' }
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('initiated') }
2015-09-24 10:27:14 +02:00
end
context 'when is post a comment' do
let(:action) { 'comment' }
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('initiated') }
2015-09-24 10:27:14 +02:00
end
end
context 'when gestionnaire is connect' do
let(:role) { 'gestionnaire' }
context 'when is post a comment' do
let(:action) { 'comment' }
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('replied') }
2015-09-24 10:27:14 +02:00
end
context 'when is validated the dossier' do
let(:action) { 'valid' }
2015-09-24 10:27:14 +02:00
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('validated') }
2015-09-24 10:27:14 +02:00
end
end
end
context 'when dossier is at state replied' do
2015-09-24 10:27:14 +02:00
before do
dossier.replied!
2015-09-24 10:27:14 +02:00
end
context 'when user is connect' do
let(:role) { 'user' }
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('updated') }
end
context 'when is updated dossier informations' do
let(:action) { 'update' }
it {
is_expected.to eq('updated')
}
end
end
context 'when gestionnaire is connect' do
let(:role) { 'gestionnaire' }
context 'when is post a comment' do
let(:action) { 'comment' }
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('replied') }
2015-09-24 10:27:14 +02:00
end
context 'when is validated the dossier' do
let(:action) { 'valid' }
2015-09-24 10:27:14 +02:00
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('validated') }
2015-09-24 10:27:14 +02:00
end
end
end
context 'when dossier is at state updated' do
before do
dossier.updated!
end
context 'when user is connect' do
let(:role) { 'user' }
context 'when is post a comment' do
let(:action) { 'comment' }
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('updated') }
2015-09-24 10:27:14 +02:00
end
context 'when is updated dossier informations' do
let(:action) { 'update' }
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('updated') }
2015-09-24 10:27:14 +02:00
end
end
context 'when gestionnaire is connect' do
let(:role) { 'gestionnaire' }
context 'when is post a comment' do
let(:action) { 'comment' }
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('replied') }
2015-09-24 10:27:14 +02:00
end
context 'when is validated the dossier' do
let(:action) { 'valid' }
2015-09-24 10:27:14 +02:00
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('validated') }
2015-09-24 10:27:14 +02:00
end
end
end
context 'when dossier is at state validated' do
2015-09-24 10:27:14 +02:00
before do
dossier.validated!
2015-09-24 10:27:14 +02:00
end
context 'when user is connect' do
let(:role) { 'user' }
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('validated') }
2015-09-24 10:27:14 +02:00
end
context 'when is submitted the dossier' do
let(:action) { 'submit' }
2015-09-24 10:27:14 +02:00
it { is_expected.to eq('submitted') }
2015-09-24 10:27:14 +02:00
end
end
context 'when gestionnaire is connect' do
let(:role) { 'gestionnaire' }
context 'when is post a comment' do
let(:action) { 'comment' }
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('validated') }
2015-09-24 10:27:14 +02:00
end
end
end
context 'when dossier is at state submitted' do
2015-09-24 10:27:14 +02:00
before do
dossier.submitted!
2015-09-24 10:27:14 +02:00
end
context 'when user is connect' do
let(:role) { 'user' }
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('submitted') }
2015-09-24 10:27:14 +02:00
end
end
context 'when gestionnaire is connect' do
let(:role) { 'gestionnaire' }
context 'when is post a comment' do
let(:action) { 'comment' }
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('submitted') }
2015-09-24 10:27:14 +02:00
end
context 'when is closed the dossier' do
let(:action) { 'close' }
2015-09-24 10:27:14 +02:00
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('closed') }
2015-09-24 10:27:14 +02:00
end
end
end
context 'when dossier is at state closed' do
2015-09-24 10:27:14 +02:00
before do
dossier.closed!
2015-09-24 10:27:14 +02:00
end
context 'when user is connect' do
let(:role) { 'user' }
context 'when is post a comment' do
let(:action) { 'comment' }
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('closed') }
2015-09-24 10:27:14 +02:00
end
end
context 'when gestionnaire is connect' do
let(:role) { 'gestionnaire' }
context 'when is post a comment' do
let(:action) { 'comment' }
2015-11-16 15:58:35 +01:00
it { is_expected.to eq('closed') }
2015-09-24 10:27:14 +02:00
end
end
end
end
describe 'gestionnaire backoffice methods' do
let(:admin) { create(:administrateur) }
let(:admin_2) { create(:administrateur) }
let(:gestionnaire) { create(:gestionnaire, administrateurs: [admin]) }
let(:procedure_admin) { create(:procedure, administrateur: admin) }
let(:procedure_admin_2) { create(:procedure, administrateur: admin_2) }
before do
create :assign_to, gestionnaire: gestionnaire, procedure: procedure_admin
end
2016-01-18 17:18:04 +01:00
let!(:dossier1) { create(:dossier, procedure: procedure_admin, state: 'draft') }
let!(:dossier2) { create(:dossier, procedure: procedure_admin, state: 'initiated') } #a_traiter
let!(:dossier3) { create(:dossier, procedure: procedure_admin, state: 'initiated') } #a_traiter
let!(:dossier4) { create(:dossier, procedure: procedure_admin, state: 'replied') } #en_attente
let!(:dossier5) { create(:dossier, procedure: procedure_admin, state: 'updated') } #a_traiter
let!(:dossier6) { create(:dossier, procedure: procedure_admin_2, state: 'validated') } #en_attente
let!(:dossier7) { create(:dossier, procedure: procedure_admin_2, state: 'submitted') } #a_traiter
let!(:dossier8) { create(:dossier, procedure: procedure_admin_2, state: 'closed') } #termine
let!(:dossier9) { create(:dossier, procedure: procedure_admin, state: 'closed') } #termine
let!(:dossier10) { create(:dossier, procedure: procedure_admin, state: 'initiated', archived: true) } #a_traiter #archived
let!(:dossier11) { create(:dossier, procedure: procedure_admin, state: 'replied', archived: true) } #en_attente #archived
let!(:dossier12) { create(:dossier, procedure: procedure_admin, state: 'closed', archived: true) } #termine #archived
2015-11-30 14:48:37 +01:00
describe '#waiting_for_gestionnaire' do
subject { gestionnaire.dossiers.waiting_for_gestionnaire }
it { expect(subject.size).to eq(3) }
end
2015-11-30 14:48:37 +01:00
describe '#waiting_for_user' do
subject { gestionnaire.dossiers.waiting_for_user }
it { expect(subject.size).to eq(1) }
end
describe '#termine' do
2015-11-30 14:48:37 +01:00
subject { gestionnaire.dossiers.termine }
it { expect(subject.size).to eq(1) }
end
end
2015-11-16 15:58:35 +01:00
describe '.search' do
subject { liste_dossiers }
2015-11-16 15:58:35 +01:00
let(:liste_dossiers) { described_class.search(gestionnaire_1, terms)[0] }
let(:dossier) { described_class.search(gestionnaire_1, terms)[1] }
let(:administrateur_1) { create(:administrateur) }
let(:administrateur_2) { create(:administrateur) }
let(:gestionnaire_1) { create(:gestionnaire, administrateurs: [administrateur_1]) }
let(:gestionnaire_2) { create(:gestionnaire, administrateurs: [administrateur_2]) }
before do
create :assign_to, gestionnaire: gestionnaire_1, procedure: procedure_1
create :assign_to, gestionnaire: gestionnaire_2, procedure: procedure_2
end
let(:procedure_1) { create(:procedure, administrateur: administrateur_1) }
let(:procedure_2) { create(:procedure, administrateur: administrateur_2) }
let!(:dossier_0) { create(:dossier, nom_projet: 'je suis un brouillon', state: 'draft', procedure: procedure_1, user: create(:user, email: 'brouillon@clap.fr')) }
let!(:dossier_1) { create(:dossier, nom_projet: 'Projet de test', state: 'initiated', procedure: procedure_1, user: create(:user, email: 'contact@test.com')) }
let!(:dossier_2) { create(:dossier, nom_projet: 'Lili et Marcel', state: 'initiated', procedure: procedure_1, user: create(:user, email: 'plop@gmail.com')) }
let!(:dossier_3) { create(:dossier, nom_projet: 'Construction projet marcel', state: 'initiated', procedure: procedure_2, user: create(:user, email: 'peace@clap.fr')) }
2015-11-27 15:04:24 +01:00
let!(:dossier_archived) { create(:dossier, nom_projet: 'je suis un Marcel archivé', state: 'initiated', procedure: procedure_1, archived: true, user: create(:user, email: 'brouillonArchived@clap.fr')) }
2015-11-16 15:58:35 +01:00
let!(:etablissement_1) { create(:etablissement, entreprise: create(:entreprise, raison_sociale: 'OCTO Academy', dossier: dossier_1), dossier: dossier_1, siret: '41636169600051') }
let!(:etablissement_2) { create(:etablissement, entreprise: create(:entreprise, raison_sociale: 'Plop octo', dossier: dossier_2), dossier: dossier_2, siret: '41816602300012') }
let!(:etablissement_3) { create(:etablissement, entreprise: create(:entreprise, raison_sociale: 'OCTO Technology', dossier: dossier_3), dossier: dossier_3, siret: '41816609600051') }
describe 'search is empty' do
let(:terms) { '' }
it { expect(subject.size).to eq(0) }
end
describe 'search draft file' do
let(:terms) { 'brouillon' }
it { expect(subject.size).to eq(0) }
2015-11-16 15:58:35 +01:00
end
describe 'search on file title' do
let(:terms) { 'Marcel' }
it { expect(subject.size).to eq(1) }
2015-11-16 15:58:35 +01:00
end
describe 'search on contact email' do
let(:terms) { 'clap' }
it { expect(subject.size).to eq(0) }
2015-11-16 15:58:35 +01:00
end
describe 'search on ID dossier' do
let(:terms) { "#{dossier_2.id}" }
it { expect(dossier.id).to eq(dossier_2.id) }
2015-11-16 15:58:35 +01:00
end
describe 'search on SIRET' do
context 'when is part of SIRET' do
let(:terms) { '4181' }
2015-11-16 15:58:35 +01:00
it { expect(subject.size).to eq(1) }
end
2015-11-16 15:58:35 +01:00
context 'when is a complet SIRET' do
let(:terms) { '41816602300012' }
it { expect(subject.size).to eq(1) }
end
2015-11-16 15:58:35 +01:00
end
describe 'search on raison social' do
let(:terms) { 'OCTO' }
it { expect(subject.size).to eq(2) }
2015-11-16 15:58:35 +01:00
end
describe 'search on multiple fields' do
let(:terms) { 'octo test' }
2015-11-16 15:58:35 +01:00
it { expect(subject.size).to eq(1) }
end
end
end
2016-02-02 18:37:38 +01:00
describe '#cerfa_available?' do
let(:procedure) { create(:procedure, cerfa_flag: cerfa_flag) }
let(:dossier) { create(:dossier, procedure: procedure)}
2016-02-02 18:37:38 +01:00
context 'Procedure accepts CERFA' do
let(:cerfa_flag) { true }
context 'when cerfa is not uploaded' do
it { expect(dossier.cerfa_available?).to be_falsey }
end
context 'when cerfa is uploaded' do
let(:dossier) { create :dossier, :with_cerfa_upload, procedure: procedure }
it { expect(dossier.cerfa_available?).to be_truthy }
2016-02-02 18:37:38 +01:00
end
end
context 'Procedure does not accept CERFA' do
let(:cerfa_flag) { false }
it { expect(dossier.cerfa_available?).to be_falsey }
end
end
2016-02-19 16:59:18 +01:00
describe '#as_csv?' do
let(:procedure) { create(:procedure) }
let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure) }
subject { dossier.as_csv }
it { expect(subject[:nom_projet]).to eq("Demande de subvention dans le cadre d'accompagnement d'enfant à l'étranger") }
it { expect(subject[:archived]).to be_falsey }
it { expect(subject['etablissement.siret']).to eq('44011762001530') }
it { expect(subject['etablissement.siege_social']).to be_truthy }
it { expect(subject['etablissement.naf']).to eq('4950Z') }
it { expect(subject['etablissement.libelle_naf']).to eq('Transports par conduites') }
it { expect(subject['etablissement.adresse']).to eq("GRTGAZ IMMEUBLE BORA 6 RUE RAOUL NORDLING 92270 BOIS COLOMBES") }
2016-02-19 16:59:18 +01:00
it { expect(subject['etablissement.numero_voie']).to eq('6') }
it { expect(subject['etablissement.type_voie']).to eq('RUE') }
it { expect(subject['etablissement.nom_voie']).to eq('RAOUL NORDLING') }
it { expect(subject['etablissement.complement_adresse']).to eq('IMMEUBLE BORA') }
it { expect(subject['etablissement.code_postal']).to eq('92270') }
it { expect(subject['etablissement.localite']).to eq('BOIS COLOMBES') }
it { expect(subject['etablissement.code_insee_localite']).to eq('92009') }
it { expect(subject['entreprise.siren']).to eq('440117620') }
it { expect(subject['entreprise.capital_social']).to eq(537100000) }
it { expect(subject['entreprise.numero_tva_intracommunautaire']).to eq('FR27440117620') }
it { expect(subject['entreprise.forme_juridique']).to eq("SA à conseil d'administration (s.a.i.)") }
it { expect(subject['entreprise.forme_juridique_code']).to eq('5599') }
it { expect(subject['entreprise.nom_commercial']).to eq('GRTGAZ') }
it { expect(subject['entreprise.raison_sociale']).to eq('GRTGAZ') }
it { expect(subject['entreprise.siret_siege_social']).to eq('44011762001530') }
it { expect(subject['entreprise.code_effectif_entreprise']).to eq('51') }
it { expect(subject['entreprise.date_creation']).to eq('Thu, 28 Jan 2016 10:16:29 UTC +00:0') }
it { expect(subject['entreprise.nom']).to be_nil }
it { expect(subject['entreprise.prenom']).to be_nil }
end
2015-08-20 17:30:17 +02:00
end