2015-08-13 15:55:19 +02:00
require 'spec_helper'
describe Dossier do
2018-05-25 18:05:28 +02:00
include ActiveJob :: TestHelper
2015-09-24 11:17:17 +02:00
let ( :user ) { create ( :user ) }
2016-07-19 16:44:26 +02:00
2017-10-10 18:35:00 +02:00
describe " without_followers scope " do
let! ( :dossier ) { create ( :dossier , :followed , :with_entreprise , user : user ) }
let! ( :dossier2 ) { create ( :dossier , :with_entreprise , user : user ) }
it { expect ( Dossier . without_followers . to_a ) . to eq ( [ dossier2 ] ) }
end
2018-08-30 11:51:35 +02:00
describe 'with_champs' do
2018-03-04 14:39:08 +01:00
let ( :procedure ) { create ( :procedure ) }
let ( :dossier ) { Dossier . create ( user : create ( :user ) , procedure : procedure ) }
before do
create ( :type_de_champ , libelle : 'l1' , order_place : 1 , procedure : procedure )
create ( :type_de_champ , libelle : 'l3' , order_place : 3 , procedure : procedure )
create ( :type_de_champ , libelle : 'l2' , order_place : 2 , procedure : procedure )
end
it do
2018-10-01 13:55:12 +02:00
expect ( Dossier . with_champs . find ( dossier . id ) . champs . map ( & :libelle ) ) . to match ( [ 'l1' , 'l2' , 'l3' ] )
2018-03-04 14:39:08 +01:00
end
end
2018-08-31 12:56:30 +02:00
describe 'nearing_end_of_retention' do
let ( :procedure ) { create ( :procedure , duree_conservation_dossiers_dans_ds : 6 ) }
let! ( :young_dossier ) { create ( :dossier , procedure : procedure ) }
let! ( :expiring_dossier ) { create ( :dossier , :en_instruction , en_instruction_at : 170 . days . ago , procedure : procedure ) }
let! ( :just_expired_dossier ) { create ( :dossier , :en_instruction , en_instruction_at : ( 6 . months + 1 . second ) . ago , procedure : procedure ) }
let! ( :long_expired_dossier ) { create ( :dossier , :en_instruction , en_instruction_at : 1 . year . ago , procedure : procedure ) }
context 'with default delay to end of retention' do
subject { Dossier . nearing_end_of_retention }
it { is_expected . not_to include ( young_dossier ) }
it { is_expected . to include ( expiring_dossier ) }
it { is_expected . to include ( just_expired_dossier ) }
it { is_expected . to include ( long_expired_dossier ) }
end
context 'with custom delay to end of retention' do
subject { Dossier . nearing_end_of_retention ( '0' ) }
it { is_expected . not_to include ( young_dossier ) }
it { is_expected . not_to include ( expiring_dossier ) }
it { is_expected . to include ( just_expired_dossier ) }
it { is_expected . to include ( long_expired_dossier ) }
end
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 ( :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
2018-07-25 19:34:06 +02:00
describe '#update_search_terms' do
let ( :etablissement ) { build ( :etablissement , entreprise_nom : 'Dupont' , entreprise_prenom : 'Thomas' , association_rna : '12345' , association_titre : 'asso de test' , association_objet : 'tests unitaires' ) }
let ( :procedure ) { create ( :procedure , :with_type_de_champ , :with_type_de_champ_private ) }
let ( :dossier ) { create ( :dossier , etablissement : etablissement , user : user , procedure : procedure ) }
let ( :france_connect_information ) { build ( :france_connect_information , given_name : 'Chris' , family_name : 'Harrisson' ) }
let ( :user ) { build ( :user , france_connect_information : france_connect_information ) }
2018-08-22 17:48:25 +02:00
let ( :champ_public ) { dossier . champs . first }
let ( :champ_private ) { dossier . champs_private . first }
2018-07-25 19:34:06 +02:00
before do
2018-08-22 17:48:25 +02:00
champ_public . update_attribute ( :value , " champ public " )
champ_private . update_attribute ( :value , " champ privé " )
2018-07-25 19:34:06 +02:00
dossier . update_search_terms
end
2018-10-01 12:59:34 +02:00
it { expect ( dossier . search_terms ) . to eq ( " #{ user . email } champ public #{ etablissement . entreprise_siren } #{ etablissement . entreprise_numero_tva_intracommunautaire } #{ etablissement . entreprise_forme_juridique } #{ etablissement . entreprise_forme_juridique_code } #{ etablissement . entreprise_nom_commercial } #{ etablissement . entreprise_raison_sociale } #{ etablissement . entreprise_siret_siege_social } #{ etablissement . entreprise_nom } #{ etablissement . entreprise_prenom } #{ etablissement . association_rna } #{ etablissement . association_titre } #{ etablissement . association_objet } #{ etablissement . siret } #{ etablissement . naf } #{ etablissement . libelle_naf } #{ etablissement . adresse } #{ etablissement . code_postal } #{ etablissement . localite } #{ etablissement . code_insee_localite } " ) }
2018-07-25 19:34:06 +02:00
it { expect ( dossier . private_search_terms ) . to eq ( 'champ privé' ) }
2018-08-22 17:48:25 +02:00
context 'with an update' do
before do
dossier . update (
champs_attributes : [ { id : champ_public . id , value : 'nouvelle valeur publique' } ] ,
champs_private_attributes : [ { id : champ_private . id , value : 'nouvelle valeur privee' } ]
)
end
it { expect ( dossier . search_terms ) . to include ( 'nouvelle valeur publique' ) }
it { expect ( dossier . private_search_terms ) . to include ( 'nouvelle valeur privee' ) }
end
2018-07-25 19:34:06 +02:00
end
2015-09-21 17:59:03 +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
2015-09-21 17:59:03 +02:00
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-18 15:15:34 +02:00
2018-01-16 13:34:24 +01:00
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 }
2015-08-18 15:15:34 +02:00
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
2015-08-18 15:15:34 +02:00
end
2015-08-21 11:37:13 +02:00
2015-09-21 17:59:03 +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 )
2015-08-18 15:15:34 +02:00
end
end
2015-08-24 15:23:07 +02:00
2015-11-03 15:27:49 +01:00
describe '#build_default_champs' do
2016-08-08 12:52:30 +02:00
context 'when dossier is linked to a procedure with type_de_champ_public and private' do
2016-02-04 14:08:35 +01:00
let ( :dossier ) { create ( :dossier , user : user ) }
2015-11-03 15:27:49 +01:00
it 'build all champs needed' do
expect ( dossier . champs . count ) . to eq ( 1 )
end
2016-08-08 12:52:30 +02:00
it 'build all champs_private needed' do
expect ( dossier . champs_private . count ) . to eq ( 1 )
end
2015-11-03 15:27:49 +01:00
end
end
2016-08-30 11:18:43 +02:00
describe '#build_default_individual' do
context 'when dossier is linked to a procedure with for_individual attr false' do
let ( :dossier ) { create ( :dossier , user : user ) }
it 'have no object created' do
expect ( dossier . individual ) . to be_nil
end
end
context 'when dossier is linked to a procedure with for_individual attr true' do
let ( :dossier ) { create ( :dossier , user : user , procedure : ( create :procedure , for_individual : true ) ) }
it 'have no object created' do
expect ( dossier . individual ) . not_to be_nil
end
end
end
2015-08-24 15:23:07 +02:00
describe '#save' do
2016-02-02 18:37:38 +01:00
subject { build ( :dossier , procedure : procedure , user : user ) }
2015-11-03 15:27:49 +01:00
let! ( :procedure ) { create ( :procedure ) }
2016-06-13 12:03:05 +02:00
2015-09-21 17:59:03 +02:00
context 'when is linked to a procedure' do
2015-11-03 15:27:49 +01:00
it 'creates default champs' do
expect ( subject ) . to receive ( :build_default_champs )
2016-02-02 18:37:38 +01:00
subject . save
2015-08-24 15:23:07 +02:00
end
end
2015-09-21 17:59:03 +02:00
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-03 15:27:49 +01:00
2015-11-04 17:27:01 +01:00
it 'does not create default champs' do
2015-11-03 15:27:49 +01:00
expect ( subject ) . not_to receive ( :build_default_champs )
2018-08-28 14:10:55 +02:00
subject . update ( state : Dossier . states . fetch ( :en_construction ) )
2015-11-03 15:27:49 +01:00
end
2015-08-24 15:23:07 +02:00
end
end
2015-08-18 15:15:34 +02:00
end
2016-02-02 18:37:38 +01:00
2017-01-26 17:54:04 +01:00
context 'when dossier is followed' do
2017-06-27 10:50:29 +02:00
let ( :procedure ) { create ( :procedure , :with_type_de_champ , :with_type_de_champ_private ) }
2017-01-26 17:54:04 +01:00
let ( :gestionnaire ) { create ( :gestionnaire ) }
2017-03-02 16:18:14 +01:00
let ( :date1 ) { 1 . day . ago }
let ( :date2 ) { 1 . hour . ago }
let ( :date3 ) { 1 . minute . ago }
2017-12-14 15:53:02 +01:00
let ( :dossier ) { create ( :dossier , :with_entreprise , user : user , procedure : procedure , en_construction_at : date1 , en_instruction_at : date2 , processed_at : date3 , motivation : " Motivation " ) }
2017-07-18 15:03:08 +02:00
let! ( :follow ) { create ( :follow , gestionnaire : gestionnaire , dossier : dossier ) }
2016-11-14 17:25:17 +01:00
2018-07-11 16:28:54 +02:00
describe " followers_gestionnaires " do
let ( :non_following_gestionnaire ) { create ( :gestionnaire ) }
subject { dossier . followers_gestionnaires }
it { expect ( subject ) . to eq [ gestionnaire ] }
it { expect ( subject ) . not_to include ( non_following_gestionnaire ) }
end
2016-11-14 10:41:56 +01:00
end
2016-06-20 13:57:57 +02:00
describe '#reset!' do
let! ( :dossier ) { create :dossier , :with_entreprise , autorisation_donnees : true }
let! ( :exercice ) { create :exercice , etablissement : dossier . etablissement }
subject { dossier . reset! }
it { expect ( dossier . etablissement ) . not_to be_nil }
it { expect ( dossier . etablissement . exercices ) . not_to be_empty }
it { expect ( dossier . etablissement . exercices . size ) . to eq 1 }
it { expect ( dossier . autorisation_donnees ) . to be_truthy }
2016-07-19 16:44:26 +02:00
it { expect { subject } . to change ( Exercice , :count ) . by ( - 1 ) }
it { expect { subject } . to change ( Etablissement , :count ) . by ( - 1 ) }
2016-06-20 13:57:57 +02:00
context 'when method reset! is call' do
before do
subject
dossier . reload
end
it { expect ( dossier . etablissement ) . to be_nil }
it { expect ( dossier . autorisation_donnees ) . to be_falsey }
end
end
2016-06-21 12:41:28 +02:00
2018-08-30 11:51:35 +02:00
describe '#champs' do
2017-08-02 19:15:53 +02:00
let ( :procedure ) { create ( :procedure ) }
let ( :dossier ) { Dossier . create ( user : create ( :user ) , procedure : procedure ) }
2016-06-21 12:41:28 +02:00
before do
2018-02-13 17:00:19 +01:00
create ( :type_de_champ , libelle : 'l1' , order_place : 1 , procedure : procedure )
create ( :type_de_champ , libelle : 'l3' , order_place : 3 , procedure : procedure )
create ( :type_de_champ , libelle : 'l2' , order_place : 2 , procedure : procedure )
2016-06-21 12:41:28 +02:00
end
2018-10-01 13:55:12 +02:00
it { expect ( dossier . champs . pluck ( :libelle ) ) . to match ( [ 'l1' , 'l2' , 'l3' ] ) }
2016-06-21 12:41:28 +02:00
end
2016-07-19 16:44:26 +02:00
2018-08-30 11:51:35 +02:00
describe '#champs_private' do
2017-07-27 20:54:02 +02:00
let ( :procedure ) { create :procedure }
let ( :dossier ) { Dossier . create ( user : create ( :user ) , procedure : procedure ) }
2016-08-08 12:52:30 +02:00
before do
2018-02-13 17:00:19 +01:00
create :type_de_champ , :private , libelle : 'l1' , order_place : 1 , procedure : procedure
create :type_de_champ , :private , libelle : 'l3' , order_place : 3 , procedure : procedure
create :type_de_champ , :private , libelle : 'l2' , order_place : 2 , procedure : procedure
2016-08-08 12:52:30 +02:00
end
2018-10-01 13:55:12 +02:00
it { expect ( dossier . champs_private . pluck ( :libelle ) ) . to match ( [ 'l1' , 'l2' , 'l3' ] ) }
2016-08-08 12:52:30 +02:00
end
2017-04-18 17:31:01 +02:00
describe " # text_summary " do
2019-02-05 11:29:49 +01:00
let ( :service ) { create ( :service , nom : 'nom du service' ) }
let ( :procedure ) { create ( :procedure , libelle : " Démarche " , organisation : " Organisme " , service : service ) }
2017-04-18 17:31:01 +02:00
2017-12-14 15:51:45 +01:00
context 'when the dossier has been en_construction' do
2018-08-28 14:10:55 +02:00
let ( :dossier ) { create :dossier , procedure : procedure , state : Dossier . states . fetch ( :en_construction ) , en_construction_at : " 31/12/2010 " . to_date }
2017-04-18 17:31:01 +02:00
subject { dossier . text_summary }
2019-02-05 11:29:49 +01:00
it { is_expected . to eq ( " Dossier déposé le 31/12/2010 sur la démarche Démarche gérée par l'organisme nom du service " ) }
2017-04-18 17:31:01 +02:00
end
2017-12-14 15:51:45 +01:00
context 'when the dossier has not been en_construction' do
2018-08-28 14:10:55 +02:00
let ( :dossier ) { create :dossier , procedure : procedure , state : Dossier . states . fetch ( :brouillon ) }
2017-04-18 17:31:01 +02:00
subject { dossier . text_summary }
2019-02-05 11:29:49 +01:00
it { is_expected . to eq ( " Dossier en brouillon répondant à la démarche Démarche gérée par l'organisme nom du service " ) }
2017-04-18 17:31:01 +02:00
end
end
2017-09-08 11:52:20 +02:00
describe '#avis_for' do
let! ( :procedure ) { create ( :procedure , :published ) }
2018-08-28 14:10:55 +02:00
let! ( :dossier ) { create ( :dossier , procedure : procedure , state : Dossier . states . fetch ( :en_construction ) ) }
2017-09-08 11:52:20 +02:00
let! ( :gestionnaire ) { create ( :gestionnaire , procedures : [ procedure ] ) }
let! ( :expert_1 ) { create ( :gestionnaire ) }
let! ( :expert_2 ) { create ( :gestionnaire ) }
context 'when there is a public advice asked from the dossiers gestionnaire' do
let! ( :avis ) { Avis . create ( dossier : dossier , claimant : gestionnaire , gestionnaire : expert_1 , confidentiel : false ) }
it { expect ( dossier . avis_for ( gestionnaire ) ) . to match ( [ avis ] ) }
it { expect ( dossier . avis_for ( expert_1 ) ) . to match ( [ avis ] ) }
it { expect ( dossier . avis_for ( expert_2 ) ) . to match ( [ avis ] ) }
end
context 'when there is a private advice asked from the dossiers gestionnaire' do
let! ( :avis ) { Avis . create ( dossier : dossier , claimant : gestionnaire , gestionnaire : expert_1 , confidentiel : true ) }
it { expect ( dossier . avis_for ( gestionnaire ) ) . to match ( [ avis ] ) }
it { expect ( dossier . avis_for ( expert_1 ) ) . to match ( [ avis ] ) }
it { expect ( dossier . avis_for ( expert_2 ) ) . to match ( [ ] ) }
end
context 'when there is a public advice asked from one expert to another' do
let! ( :avis ) { Avis . create ( dossier : dossier , claimant : expert_1 , gestionnaire : expert_2 , confidentiel : false ) }
it { expect ( dossier . avis_for ( gestionnaire ) ) . to match ( [ avis ] ) }
it { expect ( dossier . avis_for ( expert_1 ) ) . to match ( [ avis ] ) }
it { expect ( dossier . avis_for ( 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 , gestionnaire : expert_2 , confidentiel : true ) }
it { expect ( dossier . avis_for ( gestionnaire ) ) . to match ( [ avis ] ) }
it { expect ( dossier . avis_for ( expert_1 ) ) . to match ( [ avis ] ) }
it { expect ( dossier . avis_for ( expert_2 ) ) . to match ( [ avis ] ) }
end
context 'when they are a lot of advice' do
2018-10-25 15:17:33 +02:00
let! ( :avis_1 ) { Avis . create ( dossier : dossier , claimant : expert_1 , gestionnaire : expert_2 , confidentiel : false , created_at : Time . zone . parse ( '10/01/2010' ) ) }
let! ( :avis_2 ) { Avis . create ( dossier : dossier , claimant : expert_1 , gestionnaire : expert_2 , confidentiel : false , created_at : Time . zone . parse ( '9/01/2010' ) ) }
let! ( :avis_3 ) { Avis . create ( dossier : dossier , claimant : expert_1 , gestionnaire : expert_2 , confidentiel : false , created_at : Time . zone . parse ( '11/01/2010' ) ) }
2017-09-08 11:52:20 +02:00
it { expect ( dossier . avis_for ( gestionnaire ) ) . to match ( [ avis_2 , avis_1 , avis_3 ] ) }
it { expect ( dossier . avis_for ( expert_1 ) ) . to match ( [ avis_2 , avis_1 , avis_3 ] ) }
end
end
2017-03-01 09:51:55 +01:00
describe '#update_state_dates' do
2018-08-28 14:10:55 +02:00
let ( :state ) { Dossier . states . fetch ( :brouillon ) }
2017-03-01 09:51:55 +01:00
let ( :dossier ) { create ( :dossier , state : state ) }
2018-10-25 15:11:12 +02:00
let ( :beginning_of_day ) { Time . zone . now . beginning_of_day }
2017-03-01 09:51:55 +01:00
2018-08-14 11:38:37 +02:00
before { Timecop . freeze ( beginning_of_day ) }
after { Timecop . return }
2017-03-01 09:51:55 +01:00
2017-12-14 15:51:45 +01:00
context 'when dossier is en_construction' do
2017-03-01 09:51:55 +01:00
before do
2017-12-14 15:51:45 +01:00
dossier . en_construction!
2017-03-01 09:51:55 +01:00
dossier . reload
end
2018-08-28 14:10:55 +02:00
it { expect ( dossier . state ) . to eq ( Dossier . states . fetch ( :en_construction ) ) }
2017-12-14 15:51:45 +01:00
it { expect ( dossier . en_construction_at ) . to eq ( beginning_of_day ) }
2017-03-01 09:51:55 +01:00
2017-12-14 15:51:45 +01:00
it 'should keep first en_construction_at date' do
2017-03-01 11:13:48 +01:00
Timecop . return
2017-12-14 15:53:02 +01:00
dossier . en_instruction!
2017-12-14 15:51:45 +01:00
dossier . en_construction!
2017-03-01 11:13:48 +01:00
2017-12-14 15:51:45 +01:00
expect ( dossier . en_construction_at ) . to eq ( beginning_of_day )
2017-03-01 11:13:48 +01:00
end
2017-03-01 09:51:55 +01:00
end
2017-12-14 15:53:02 +01:00
context 'when dossier is en_instruction' do
2018-08-28 14:10:55 +02:00
let ( :state ) { Dossier . states . fetch ( :en_construction ) }
2017-03-01 09:51:55 +01:00
before do
2017-12-14 15:53:02 +01:00
dossier . en_instruction!
2017-03-01 09:51:55 +01:00
dossier . reload
end
2018-08-28 14:10:55 +02:00
it { expect ( dossier . state ) . to eq ( Dossier . states . fetch ( :en_instruction ) ) }
2017-12-14 15:53:02 +01:00
it { expect ( dossier . en_instruction_at ) . to eq ( beginning_of_day ) }
2017-03-01 11:13:48 +01:00
2017-12-14 15:53:02 +01:00
it 'should keep first en_instruction_at date if dossier is set to en_construction again' do
2017-03-01 11:13:48 +01:00
Timecop . return
2017-12-14 15:51:45 +01:00
dossier . en_construction!
2017-12-14 15:53:02 +01:00
dossier . en_instruction!
2017-03-01 11:13:48 +01:00
2017-12-14 15:53:02 +01:00
expect ( dossier . en_instruction_at ) . to eq ( beginning_of_day )
2017-03-01 11:13:48 +01:00
end
2017-03-01 09:51:55 +01:00
end
shared_examples 'dossier is processed' do | new_state |
before do
dossier . update ( state : new_state )
dossier . reload
end
it { expect ( dossier . state ) . to eq ( new_state ) }
2017-03-01 11:13:48 +01:00
it { expect ( dossier . processed_at ) . to eq ( beginning_of_day ) }
2017-03-01 09:51:55 +01:00
end
2017-12-04 18:00:12 +01:00
context 'when dossier is accepte' do
2018-08-28 14:10:55 +02:00
let ( :state ) { Dossier . states . fetch ( :en_instruction ) }
2017-03-01 09:51:55 +01:00
2018-08-28 14:10:55 +02:00
it_behaves_like 'dossier is processed' , Dossier . states . fetch ( :accepte )
2017-03-01 09:51:55 +01:00
end
2017-12-04 18:15:40 +01:00
context 'when dossier is refuse' do
2018-08-28 14:10:55 +02:00
let ( :state ) { Dossier . states . fetch ( :en_instruction ) }
2017-03-01 09:51:55 +01:00
2018-08-28 14:10:55 +02:00
it_behaves_like 'dossier is processed' , Dossier . states . fetch ( :refuse )
2017-03-01 09:51:55 +01:00
end
2017-12-04 20:23:57 +01:00
context 'when dossier is sans_suite' do
2018-08-28 14:10:55 +02:00
let ( :state ) { Dossier . states . fetch ( :en_instruction ) }
2017-03-01 09:51:55 +01:00
2018-08-28 14:10:55 +02:00
it_behaves_like 'dossier is processed' , Dossier . states . fetch ( :sans_suite )
2017-03-01 09:51:55 +01:00
end
2017-01-26 17:54:04 +01:00
end
2017-03-29 14:09:50 +02:00
2017-12-01 12:24:41 +01:00
describe '.downloadable_sorted' do
2017-03-29 14:09:50 +02:00
let ( :procedure ) { create ( :procedure ) }
2018-08-28 14:10:55 +02:00
let! ( :dossier ) { create ( :dossier , :with_entreprise , procedure : procedure , state : Dossier . states . fetch ( :brouillon ) ) }
2018-10-25 15:17:33 +02:00
let! ( :dossier2 ) { create ( :dossier , :with_entreprise , procedure : procedure , state : Dossier . states . fetch ( :en_construction ) , en_construction_at : Time . zone . parse ( '03/01/2010' ) ) }
let! ( :dossier3 ) { create ( :dossier , :with_entreprise , procedure : procedure , state : Dossier . states . fetch ( :en_instruction ) , en_construction_at : Time . zone . parse ( '01/01/2010' ) ) }
let! ( :dossier4 ) { create ( :dossier , :with_entreprise , procedure : procedure , state : Dossier . states . fetch ( :en_instruction ) , archived : true , en_construction_at : Time . zone . parse ( '02/01/2010' ) ) }
2017-03-29 14:09:50 +02:00
2017-12-01 12:24:41 +01:00
subject { procedure . dossiers . downloadable_sorted }
2017-03-29 14:09:50 +02:00
2018-01-15 21:54:40 +01:00
it { is_expected . to match ( [ dossier3 , dossier4 , dossier2 ] ) }
2017-03-29 14:09:50 +02:00
end
2017-05-26 20:01:57 +02:00
2017-10-13 18:35:12 +02:00
describe " # send_dossier_received " do
2017-05-26 20:01:57 +02:00
let ( :procedure ) { create ( :procedure ) }
2018-08-28 14:10:55 +02:00
let ( :dossier ) { create ( :dossier , procedure : procedure , state : Dossier . states . fetch ( :en_construction ) ) }
2017-05-26 20:01:57 +02:00
before do
2017-10-13 18:35:12 +02:00
allow ( NotificationMailer ) . to receive ( :send_dossier_received ) . and_return ( double ( deliver_later : nil ) )
2017-05-26 20:01:57 +02:00
end
2017-12-14 15:53:02 +01:00
it " sends an email when the dossier becomes en_instruction " do
dossier . en_instruction!
2018-05-30 23:55:34 +02:00
expect ( NotificationMailer ) . to have_received ( :send_dossier_received ) . with ( dossier )
2017-05-26 20:01:57 +02:00
end
2017-12-04 18:00:12 +01:00
it " does not an email when the dossier becomes accepte " do
dossier . accepte!
2017-10-13 18:35:12 +02:00
expect ( NotificationMailer ) . to_not have_received ( :send_dossier_received )
2017-05-26 20:01:57 +02:00
end
end
2017-06-08 14:04:47 +02:00
2017-10-11 15:36:40 +02:00
describe " # send_draft_notification_email " do
2018-08-08 11:56:58 +02:00
include Rails . application . routes . url_helpers
2017-10-11 15:36:40 +02:00
let ( :procedure ) { create ( :procedure ) }
let ( :user ) { create ( :user ) }
it " send an email when the dossier is created for the very first time " do
2018-08-08 11:56:58 +02:00
dossier = nil
2018-05-25 18:05:28 +02:00
ActiveJob :: Base . queue_adapter = :test
expect do
perform_enqueued_jobs do
2018-08-28 14:10:55 +02:00
dossier = Dossier . create ( procedure : procedure , state : Dossier . states . fetch ( :brouillon ) , user : user )
2018-05-25 18:05:28 +02:00
end
end . to change ( ActionMailer :: Base . deliveries , :size ) . from ( 0 ) . to ( 1 )
2017-10-11 15:36:40 +02:00
mail = ActionMailer :: Base . deliveries . last
2018-05-31 15:50:13 +02:00
expect ( mail . subject ) . to eq ( " Retrouvez votre brouillon pour la démarche \" #{ procedure . libelle } \" " )
2018-08-08 11:56:58 +02:00
expect ( mail . html_part . body ) . to include ( dossier_url ( dossier ) )
2017-10-11 15:36:40 +02:00
end
2017-12-04 16:17:15 +01:00
it " does not send an email when the dossier is created with a non brouillon state " do
2018-08-28 14:10:55 +02:00
expect { Dossier . create ( procedure : procedure , state : Dossier . states . fetch ( :en_construction ) , user : user ) } . not_to change ( ActionMailer :: Base . deliveries , :size )
expect { Dossier . create ( procedure : procedure , state : Dossier . states . fetch ( :en_instruction ) , user : user ) } . not_to change ( ActionMailer :: Base . deliveries , :size )
expect { Dossier . create ( procedure : procedure , state : Dossier . states . fetch ( :accepte ) , user : user ) } . not_to change ( ActionMailer :: Base . deliveries , :size )
expect { Dossier . create ( procedure : procedure , state : Dossier . states . fetch ( :refuse ) , user : user ) } . not_to change ( ActionMailer :: Base . deliveries , :size )
expect { Dossier . create ( procedure : procedure , state : Dossier . states . fetch ( :sans_suite ) , user : user ) } . not_to change ( ActionMailer :: Base . deliveries , :size )
2017-10-11 15:36:40 +02:00
end
end
2018-04-06 13:09:37 +02:00
describe " # unspecified_attestation_champs " do
let ( :procedure ) { create ( :procedure , attestation_template : attestation_template ) }
2018-08-28 14:10:55 +02:00
let ( :dossier ) { create ( :dossier , procedure : procedure , state : Dossier . states . fetch ( :en_instruction ) ) }
2018-04-06 13:09:37 +02:00
subject { dossier . unspecified_attestation_champs . map ( & :libelle ) }
context " without attestation template " do
let ( :attestation_template ) { nil }
it { is_expected . to eq ( [ ] ) }
end
context " with attestation template " do
# Test all combinations:
# - with tag specified and unspecified
# - with tag in body and tag in title
# - with tag correponsing to a champ and an annotation privée
# - with a dash in the champ libelle / tag
2018-04-06 19:22:42 +02:00
let ( :title ) { " voici --specified champ-in-title-- un --unspecified champ-in-title-- beau --specified annotation privée-in-title-- titre --unspecified annotation privée-in-title-- non --numéro du dossier-- " }
2018-04-06 13:09:37 +02:00
let ( :body ) { " voici --specified champ-in-body-- un --unspecified champ-in-body-- beau --specified annotation privée-in-body-- body --unspecified annotation privée-in-body-- non ? " }
let ( :attestation_template ) { create ( :attestation_template , title : title , body : body , activated : activated ) }
context " which is disabled " do
let ( :activated ) { false }
it { is_expected . to eq ( [ ] ) }
end
context " wich is enabled " do
let ( :activated ) { true }
let! ( :tdc_1 ) { create ( :type_de_champ , libelle : " specified champ-in-title " , procedure : procedure ) }
let! ( :tdc_2 ) { create ( :type_de_champ , libelle : " unspecified champ-in-title " , procedure : procedure ) }
let! ( :tdc_3 ) { create ( :type_de_champ , libelle : " specified champ-in-body " , procedure : procedure ) }
let! ( :tdc_4 ) { create ( :type_de_champ , libelle : " unspecified champ-in-body " , procedure : procedure ) }
let! ( :tdc_5 ) { create ( :type_de_champ , private : true , libelle : " specified annotation privée-in-title " , procedure : procedure ) }
let! ( :tdc_6 ) { create ( :type_de_champ , private : true , libelle : " unspecified annotation privée-in-title " , procedure : procedure ) }
let! ( :tdc_7 ) { create ( :type_de_champ , private : true , libelle : " specified annotation privée-in-body " , procedure : procedure ) }
let! ( :tdc_8 ) { create ( :type_de_champ , private : true , libelle : " unspecified annotation privée-in-body " , procedure : procedure ) }
before do
( dossier . champs + dossier . champs_private )
. select { | c | c . libelle . match? ( / ^specified / ) }
. each { | c | c . update_attribute ( :value , " specified " ) }
end
it do
is_expected . to eq ( [
" unspecified champ-in-title " ,
" unspecified annotation privée-in-title " ,
" unspecified champ-in-body " ,
" unspecified annotation privée-in-body "
] )
end
end
end
end
2018-03-20 16:00:30 +01:00
describe '#build_attestation' do
2017-06-08 14:04:47 +02:00
let ( :attestation_template ) { nil }
let ( :procedure ) { create ( :procedure , attestation_template : attestation_template ) }
before :each do
2017-12-05 17:43:32 +01:00
dossier . attestation = dossier . build_attestation
2017-06-08 14:04:47 +02:00
dossier . reload
end
2017-12-14 15:53:02 +01:00
context 'when the dossier is in en_instruction state ' do
2018-08-28 14:10:55 +02:00
let! ( :dossier ) { create ( :dossier , procedure : procedure , state : Dossier . states . fetch ( :en_instruction ) ) }
2017-06-08 14:04:47 +02:00
context 'when the procedure has no attestation' do
it { expect ( dossier . attestation ) . to be_nil }
end
context 'when the procedure has an unactivated attestation' do
let ( :attestation_template ) { AttestationTemplate . new ( activated : false ) }
it { expect ( dossier . attestation ) . to be_nil }
end
context 'when the procedure attached has an activated attestation' do
let ( :attestation_template ) { AttestationTemplate . new ( activated : true ) }
it { expect ( dossier . attestation ) . not_to be_nil }
end
end
end
2017-06-27 15:26:40 +02:00
describe " .default_scope " do
let! ( :dossier ) { create ( :dossier , hidden_at : hidden_at ) }
context " when dossier is not hidden " do
let ( :hidden_at ) { nil }
it { expect ( Dossier . count ) . to eq ( 1 ) }
it { expect ( Dossier . all ) . to include ( dossier ) }
end
context " when dossier is hidden " do
let ( :hidden_at ) { 1 . day . ago }
it { expect ( Dossier . count ) . to eq ( 0 ) }
end
end
2017-10-02 17:03:38 +02:00
2017-10-26 16:53:40 +02:00
describe 'updated_at' do
2017-10-24 18:12:25 +02:00
let! ( :dossier ) { create ( :dossier ) }
2018-10-25 15:17:33 +02:00
let ( :modif_date ) { Time . zone . parse ( '01/01/2100' ) }
2017-10-24 18:12:25 +02:00
before { Timecop . freeze ( modif_date ) }
2017-11-29 16:07:39 +01:00
after { Timecop . return }
2017-10-24 18:12:25 +02:00
subject do
dossier . reload
dossier . updated_at
end
it { is_expected . not_to eq ( modif_date ) }
context 'when a piece justificative is modified' do
before { dossier . pieces_justificatives << create ( :piece_justificative , :contrat ) }
it { is_expected . to eq ( modif_date ) }
end
context 'when a champ is modified' do
before { dossier . champs . first . update_attribute ( 'value' , 'yop' ) }
it { is_expected . to eq ( modif_date ) }
end
context 'when a commentaire is modified' do
before { dossier . commentaires << create ( :commentaire ) }
it { is_expected . to eq ( modif_date ) }
end
context 'when an avis is modified' do
before { dossier . avis << create ( :avis ) }
it { is_expected . to eq ( modif_date ) }
end
end
2017-11-17 23:40:51 +01:00
describe '#owner_name' do
let! ( :procedure ) { create ( :procedure ) }
subject { dossier . owner_name }
context 'when there is no entreprise or individual' do
2018-04-23 11:57:38 +02:00
let ( :dossier ) { create ( :dossier , individual : nil , procedure : procedure ) }
2017-11-17 23:40:51 +01:00
it { is_expected . to be_nil }
end
context 'when there is entreprise' do
let ( :dossier ) { create ( :dossier , :with_entreprise , procedure : procedure ) }
2018-04-23 11:57:38 +02:00
it { is_expected . to eq ( dossier . etablissement . entreprise_raison_sociale ) }
2017-11-17 23:40:51 +01:00
end
context 'when there is an individual' do
let ( :dossier ) { create ( :dossier , :for_individual , procedure : procedure ) }
it { is_expected . to eq ( " #{ dossier . individual . nom } #{ dossier . individual . prenom } " ) }
end
end
2017-10-30 16:54:33 +01:00
2018-07-23 14:44:42 +02:00
describe " # delete_and_keep_track " do
let ( :dossier ) { create ( :dossier ) }
let ( :deleted_dossier ) { DeletedDossier . find_by! ( dossier_id : dossier . id ) }
before do
allow ( DossierMailer ) . to receive ( :notify_deletion_to_user ) . and_return ( double ( deliver_later : nil ) )
allow ( DossierMailer ) . to receive ( :notify_deletion_to_administration ) . and_return ( double ( deliver_later : nil ) )
end
subject! { dossier . delete_and_keep_track }
it 'hides the dossier' do
expect ( dossier . hidden_at ) . to be_present
end
it 'creates a DeletedDossier record' do
expect ( deleted_dossier . dossier_id ) . to eq dossier . id
expect ( deleted_dossier . procedure ) . to eq dossier . procedure
expect ( deleted_dossier . state ) . to eq dossier . state
expect ( deleted_dossier . deleted_at ) . to be_present
end
2018-07-23 15:06:06 +02:00
it 'notifies the user' do
2018-07-23 14:44:42 +02:00
expect ( DossierMailer ) . to have_received ( :notify_deletion_to_user ) . with ( deleted_dossier , dossier . user . email )
2018-07-23 15:06:06 +02:00
end
context 'where gestionnaires are following the dossier' do
2018-08-02 16:36:50 +02:00
let ( :dossier ) { create ( :dossier , :en_construction , :followed ) }
2018-07-23 15:06:06 +02:00
let! ( :non_following_gestionnaire ) do
non_following_gestionnaire = create ( :gestionnaire )
non_following_gestionnaire . procedures << dossier . procedure
non_following_gestionnaire
end
it 'notifies the following gestionnaires' do
expect ( DossierMailer ) . to have_received ( :notify_deletion_to_administration ) . once
expect ( DossierMailer ) . to have_received ( :notify_deletion_to_administration ) . with ( deleted_dossier , dossier . followers_gestionnaires . first . email )
end
end
context 'when there are no following gestionnaires' do
2018-08-02 16:36:50 +02:00
let ( :dossier ) { create ( :dossier , :en_construction ) }
2018-07-23 15:06:06 +02:00
it 'notifies the procedure administrateur' do
expect ( DossierMailer ) . to have_received ( :notify_deletion_to_administration ) . once
expect ( DossierMailer ) . to have_received ( :notify_deletion_to_administration ) . with ( deleted_dossier , dossier . procedure . administrateur . email )
end
2018-07-23 14:44:42 +02:00
end
2018-08-02 16:36:50 +02:00
context 'when dossier is brouillon' do
let ( :dossier ) { create ( :dossier ) }
it 'do not notifies the procedure administrateur' do
expect ( DossierMailer ) . not_to have_received ( :notify_deletion_to_administration )
end
end
2018-07-23 14:44:42 +02:00
end
2018-03-01 17:04:05 +01:00
describe 'webhook' do
let ( :dossier ) { create ( :dossier ) }
it 'should not call webhook' do
expect {
dossier . accepte!
} . to_not have_enqueued_job ( WebHookJob )
end
it 'should call webhook' do
dossier . procedure . update_column ( :web_hook_url , '/webhook.json' )
expect {
dossier . update_column ( :motivation , 'bonjour' )
} . to_not have_enqueued_job ( WebHookJob )
expect {
dossier . en_construction!
} . to have_enqueued_job ( WebHookJob )
expect {
dossier . update_column ( :motivation , 'bonjour2' )
} . to_not have_enqueued_job ( WebHookJob )
expect {
dossier . en_instruction!
} . to have_enqueued_job ( WebHookJob )
end
end
2018-06-25 18:07:16 +02:00
describe " # can_transition_to_en_construction? " do
let ( :procedure ) { create ( :procedure , :published ) }
let ( :dossier ) { create ( :dossier , state : state , procedure : procedure ) }
subject { dossier . can_transition_to_en_construction? }
context " dossier state is brouillon " do
2018-08-28 14:10:55 +02:00
let ( :state ) { Dossier . states . fetch ( :brouillon ) }
2018-06-25 18:07:16 +02:00
it { is_expected . to be true }
context " procedure is archived " do
before { procedure . archive }
it { is_expected . to be false }
end
end
context " dossier state is en_construction " do
2018-08-28 14:10:55 +02:00
let ( :state ) { Dossier . states . fetch ( :en_construction ) }
2018-06-25 18:07:16 +02:00
it { is_expected . to be false }
end
context " dossier state is en_instruction " do
2018-08-28 14:10:55 +02:00
let ( :state ) { Dossier . states . fetch ( :en_instruction ) }
2018-06-25 18:07:16 +02:00
it { is_expected . to be false }
end
context " dossier state is en_instruction " do
2018-08-28 14:10:55 +02:00
let ( :state ) { Dossier . states . fetch ( :accepte ) }
2018-06-25 18:07:16 +02:00
it { is_expected . to be false }
end
context " dossier state is en_instruction " do
2018-08-28 14:10:55 +02:00
let ( :state ) { Dossier . states . fetch ( :refuse ) }
2018-06-25 18:07:16 +02:00
it { is_expected . to be false }
end
context " dossier state is en_instruction " do
2018-08-28 14:10:55 +02:00
let ( :state ) { Dossier . states . fetch ( :sans_suite ) }
2018-06-25 18:07:16 +02:00
it { is_expected . to be false }
end
end
2018-08-31 15:44:07 +02:00
context " retention date " do
let ( :procedure ) { create ( :procedure , duree_conservation_dossiers_dans_ds : 6 ) }
let ( :uninstructed_dossier ) { create ( :dossier , :en_construction , procedure : procedure ) }
2018-10-25 15:07:15 +02:00
let ( :young_dossier ) { create ( :dossier , :en_instruction , en_instruction_at : Time . zone . now , procedure : procedure ) }
2018-08-31 15:44:07 +02:00
let ( :just_expired_dossier ) { create ( :dossier , :en_instruction , en_instruction_at : 6 . months . ago , procedure : procedure ) }
let ( :long_expired_dossier ) { create ( :dossier , :en_instruction , en_instruction_at : 1 . year . ago , procedure : procedure ) }
2018-10-25 15:17:33 +02:00
let ( :modif_date ) { Time . zone . parse ( '01/01/2100' ) }
2018-08-31 15:44:07 +02:00
before { Timecop . freeze ( modif_date ) }
after { Timecop . return }
describe " # retention_end_date " do
it { expect ( uninstructed_dossier . retention_end_date ) . to be_nil }
it { expect ( young_dossier . retention_end_date ) . to eq ( 6 . months . from_now ) }
2018-10-25 15:07:15 +02:00
it { expect ( just_expired_dossier . retention_end_date ) . to eq ( Time . zone . now ) }
2018-08-31 15:44:07 +02:00
it { expect ( long_expired_dossier . retention_end_date ) . to eq ( 6 . months . ago ) }
end
describe " # retention_expired? " do
it { expect ( uninstructed_dossier ) . not_to be_retention_expired }
it { expect ( young_dossier ) . not_to be_retention_expired }
it { expect ( just_expired_dossier ) . to be_retention_expired }
it { expect ( long_expired_dossier ) . to be_retention_expired }
end
end
2019-01-10 17:23:48 +01:00
2019-01-16 11:00:25 +01:00
describe '#accepter!' do
let ( :dossier ) { create ( :dossier ) }
let! ( :gestionnaire ) { create ( :gestionnaire ) }
let! ( :now ) { Time . zone . parse ( '01/01/2100' ) }
let ( :attestation ) { Attestation . new }
before do
allow ( NotificationMailer ) . to receive ( :send_closed_notification ) . and_return ( double ( deliver_later : true ) )
allow ( dossier ) . to receive ( :build_attestation ) . and_return ( attestation )
Timecop . freeze ( now )
dossier . accepter! ( gestionnaire , 'motivation' )
dossier . reload
end
after { Timecop . return }
it { expect ( dossier . motivation ) . to eq ( 'motivation' ) }
it { expect ( dossier . en_instruction_at ) . to eq ( now ) }
it { expect ( dossier . processed_at ) . to eq ( now ) }
it { expect ( dossier . state ) . to eq ( 'accepte' ) }
it { expect ( dossier . dossier_operation_logs . pluck ( :gestionnaire_id , :operation , :automatic_operation ) ) . to match ( [ [ gestionnaire . id , 'accepter' , false ] ] ) }
it { expect ( NotificationMailer ) . to have_received ( :send_closed_notification ) . with ( dossier ) }
it { expect ( dossier . attestation ) . to eq ( attestation ) }
end
describe '#accepter_automatiquement!' do
let ( :dossier ) { create ( :dossier ) }
let! ( :now ) { Time . zone . parse ( '01/01/2100' ) }
let ( :attestation ) { Attestation . new }
before do
allow ( NotificationMailer ) . to receive ( :send_closed_notification ) . and_return ( double ( deliver_later : true ) )
allow ( dossier ) . to receive ( :build_attestation ) . and_return ( attestation )
Timecop . freeze ( now )
dossier . accepter_automatiquement!
dossier . reload
end
after { Timecop . return }
it { expect ( dossier . motivation ) . to eq ( nil ) }
it { expect ( dossier . en_instruction_at ) . to eq ( now ) }
it { expect ( dossier . processed_at ) . to eq ( now ) }
it { expect ( dossier . state ) . to eq ( 'accepte' ) }
it { expect ( dossier . dossier_operation_logs . pluck ( :gestionnaire_id , :operation , :automatic_operation ) ) . to match ( [ [ nil , 'accepter' , true ] ] ) }
it { expect ( NotificationMailer ) . to have_received ( :send_closed_notification ) . with ( dossier ) }
it { expect ( dossier . attestation ) . to eq ( attestation ) }
end
2019-01-10 17:23:48 +01:00
describe '#passer_en_instruction!' do
let ( :dossier ) { create ( :dossier ) }
let ( :gestionnaire ) { create ( :gestionnaire ) }
before { dossier . passer_en_instruction! ( gestionnaire ) }
it { expect ( dossier . state ) . to eq ( 'en_instruction' ) }
it { expect ( dossier . followers_gestionnaires ) . to include ( gestionnaire ) }
it { expect ( dossier . dossier_operation_logs . pluck ( :gestionnaire_id , :operation ) ) . to match ( [ [ gestionnaire . id , 'passer_en_instruction' ] ] ) }
end
describe '#passer_automatiquement_en_instruction!' do
let ( :dossier ) { create ( :dossier ) }
let ( :gestionnaire ) { create ( :gestionnaire ) }
before { dossier . passer_automatiquement_en_instruction! }
it { expect ( dossier . followers_gestionnaires ) . not_to include ( gestionnaire ) }
it { expect ( dossier . dossier_operation_logs . pluck ( :gestionnaire_id , :operation , :automatic_operation ) ) . to match ( [ [ nil , 'passer_en_instruction' , true ] ] ) }
end
2019-01-30 16:14:15 +01:00
describe " # check_mandatory_champs " do
let ( :procedure ) { create ( :procedure , :with_type_de_champ ) }
let ( :dossier ) { create ( :dossier , :with_all_champs , procedure : procedure ) }
it 'no mandatory champs' do
expect ( dossier . check_mandatory_champs ) . to be_empty
end
context " with mandatory champs " do
let ( :procedure ) { create ( :procedure , :with_type_de_champ_mandatory ) }
let ( :champ_with_error ) { dossier . champs . first }
before do
champ_with_error . value = nil
champ_with_error . save
end
it 'should have errors' do
errors = dossier . check_mandatory_champs
expect ( errors ) . not_to be_empty
expect ( errors . first ) . to eq ( " Le champ #{ champ_with_error . libelle } doit être rempli. " )
end
end
context " with champ repetition " do
let ( :procedure ) { create ( :procedure ) }
let ( :type_de_champ_repetition ) { create ( :type_de_champ_repetition , mandatory : true ) }
before do
procedure . types_de_champ << type_de_champ_repetition
type_de_champ_repetition . types_de_champ << create ( :type_de_champ_text , mandatory : true )
end
context " when no champs " do
let ( :champ_with_error ) { dossier . champs . first }
it 'should have errors' do
errors = dossier . check_mandatory_champs
expect ( errors ) . not_to be_empty
expect ( errors . first ) . to eq ( " Le champ #{ champ_with_error . libelle } doit être rempli. " )
end
end
context " when mandatory champ inside repetition " do
let ( :champ_with_error ) { dossier . champs . first . champs . first }
before do
dossier . champs . first . add_row
end
it 'should have errors' do
errors = dossier . check_mandatory_champs
expect ( errors ) . not_to be_empty
expect ( errors . first ) . to eq ( " Le champ #{ champ_with_error . libelle } doit être rempli. " )
end
end
end
end
2015-08-20 17:30:17 +02:00
end