2023-06-20 00:04:55 +02:00
describe Dossier , type : :model do
2015-09-24 11:17:17 +02:00
let ( :user ) { create ( :user ) }
2016-07-19 16:44:26 +02:00
2020-01-28 16:46:05 +01:00
describe 'scopes' do
describe '.default_scope' do
let! ( :dossier ) { create ( :dossier ) }
subject { Dossier . all }
it { is_expected . to match_array ( [ dossier ] ) }
end
describe '.without_followers' do
let! ( :dossier_with_follower ) { create ( :dossier , :followed , :with_entreprise , user : user ) }
let! ( :dossier_without_follower ) { create ( :dossier , :with_entreprise , user : user ) }
it { expect ( Dossier . without_followers . to_a ) . to eq ( [ dossier_without_follower ] ) }
end
2023-04-11 15:50:17 +02:00
2023-04-18 12:03:10 +02:00
describe 'brouillons_recently_updated' do
let! ( :dossier_en_brouillon ) { create ( :dossier ) }
let! ( :dossier_en_brouillon_2 ) { create ( :dossier ) }
2023-04-11 15:50:17 +02:00
2023-04-18 12:03:10 +02:00
it { expect ( Dossier . brouillons_recently_updated ) . to eq ( [ dossier_en_brouillon_2 , dossier_en_brouillon ] ) }
2023-04-11 15:50:17 +02:00
end
2020-01-28 16:46:05 +01:00
end
2020-01-07 16:36:04 +01:00
describe 'validations' do
let ( :procedure ) { create ( :procedure , :for_individual ) }
subject ( :dossier ) { create ( :dossier , procedure : procedure ) }
it { is_expected . to validate_presence_of ( :individual ) }
2023-01-03 14:46:10 +01:00
it { is_expected . to validate_presence_of ( :user ) }
context 'when dossier has deleted_user_email_never_send' do
subject ( :dossier ) { create ( :dossier , procedure : procedure , deleted_user_email_never_send : " seb@totoro.org " ) }
it { is_expected . not_to validate_presence_of ( :user ) }
end
context 'when dossier is prefilled' do
subject ( :dossier ) { create ( :dossier , procedure : procedure , prefilled : true ) }
it { is_expected . not_to validate_presence_of ( :user ) }
end
2020-01-07 16:36:04 +01:00
end
2018-08-30 11:51:35 +02:00
describe 'with_champs' do
2022-05-17 10:39:24 +02:00
let ( :procedure ) { create ( :procedure ) }
let! ( :tdc_1 ) { create ( :type_de_champ , libelle : 'l1' , position : 1 , procedure : procedure ) }
let! ( :tdc_3 ) { create ( :type_de_champ , libelle : 'l3' , position : 3 , procedure : procedure ) }
let! ( :tdc_2 ) { create ( :type_de_champ , libelle : 'l2' , position : 2 , procedure : procedure ) }
2020-08-27 19:56:19 +02:00
let ( :dossier ) { create ( :dossier , procedure : procedure ) }
2018-03-04 14:39:08 +01:00
it do
2022-11-10 22:21:14 +01:00
expect ( Dossier . with_champs . find ( dossier . id ) . champs_public . map ( & :libelle ) ) . to match ( [ 'l1' , 'l2' , 'l3' ] )
2018-03-04 14:39:08 +01:00
end
end
2020-02-26 17:36:24 +01:00
describe 'brouillon_close_to_expiration' do
2020-03-24 12:50:59 +01:00
let ( :procedure ) { create ( :procedure , :published , duree_conservation_dossiers_dans_ds : 6 ) }
2020-02-26 17:36:24 +01:00
let! ( :young_dossier ) { create ( :dossier , :en_construction , procedure : procedure ) }
2021-08-26 10:14:23 +02:00
let! ( :expiring_dossier ) { create ( :dossier , created_at : 175 . days . ago , procedure : procedure ) }
2022-04-08 13:16:08 +02:00
let! ( :expiring_dossier_with_notification ) { create ( :dossier , created_at : 175 . days . ago , brouillon_close_to_expiration_notice_sent_at : Time . zone . now , procedure : procedure ) }
2020-02-26 17:36:24 +01:00
let! ( :just_expired_dossier ) { create ( :dossier , created_at : ( 6 . months + 1 . hour + 10 . seconds ) . ago , procedure : procedure ) }
let! ( :long_expired_dossier ) { create ( :dossier , created_at : 1 . year . ago , procedure : procedure ) }
subject { Dossier . brouillon_close_to_expiration }
it do
is_expected . not_to include ( young_dossier )
is_expected . to include ( expiring_dossier )
is_expected . to include ( just_expired_dossier )
is_expected . to include ( long_expired_dossier )
end
2021-05-04 16:29:29 +02:00
2022-04-08 13:16:08 +02:00
it do
expect ( expiring_dossier . close_to_expiration? ) . to be_truthy
expect ( expiring_dossier_with_notification . close_to_expiration? ) . to be_truthy
end
2021-05-04 16:29:29 +02:00
context 'does not include an expiring dossier that has been postponed' do
before do
2022-04-08 13:16:08 +02:00
expiring_dossier . extend_conservation ( 1 . month )
expiring_dossier_with_notification . extend_conservation ( 1 . month )
2021-05-04 16:29:29 +02:00
expiring_dossier . reload
2022-04-08 13:16:08 +02:00
expiring_dossier_with_notification . reload
2021-05-04 16:29:29 +02:00
end
it { is_expected . not_to include ( expiring_dossier ) }
2022-04-08 13:16:08 +02:00
it do
expect ( expiring_dossier . close_to_expiration? ) . to be_falsey
expect ( expiring_dossier_with_notification . close_to_expiration? ) . to be_falsey
2023-11-10 13:24:37 +01:00
expect ( expiring_dossier . expiration_date ) . to eq ( expiring_dossier . expiration_date_with_extension )
expect ( expiring_dossier_with_notification . expiration_date ) . to eq ( expiring_dossier_with_notification . expiration_date_with_extension )
2022-04-08 13:16:08 +02:00
end
2021-05-04 16:29:29 +02:00
end
2021-11-19 14:28:54 +01:00
context 'when .close_to_expiration' do
subject { Dossier . close_to_expiration }
it do
is_expected . not_to include ( young_dossier )
is_expected . to include ( expiring_dossier )
is_expected . to include ( just_expired_dossier )
is_expected . to include ( long_expired_dossier )
end
end
2020-02-26 17:36:24 +01:00
end
describe 'en_construction_close_to_expiration' do
2020-03-24 12:50:59 +01:00
let ( :procedure ) { create ( :procedure , :published , duree_conservation_dossiers_dans_ds : 6 ) }
2018-08-31 12:56:30 +02:00
let! ( :young_dossier ) { create ( :dossier , procedure : procedure ) }
2021-08-26 10:14:23 +02:00
let! ( :expiring_dossier ) { create ( :dossier , :en_construction , en_construction_at : 175 . days . ago , procedure : procedure ) }
2022-04-08 13:16:08 +02:00
let! ( :expiring_dossier_with_notification ) { create ( :dossier , :en_construction , en_construction_at : 175 . days . ago , en_construction_close_to_expiration_notice_sent_at : Time . zone . now , procedure : procedure ) }
2020-02-26 17:36:24 +01:00
let! ( :just_expired_dossier ) { create ( :dossier , :en_construction , en_construction_at : ( 6 . months + 1 . hour + 10 . seconds ) . ago , procedure : procedure ) }
let! ( :long_expired_dossier ) { create ( :dossier , :en_construction , en_construction_at : 1 . year . ago , procedure : procedure ) }
2018-08-31 12:56:30 +02:00
2020-02-26 17:36:24 +01:00
subject { Dossier . en_construction_close_to_expiration }
2018-08-31 12:56:30 +02:00
2020-02-26 17:36:24 +01:00
it do
is_expected . not_to include ( young_dossier )
is_expected . to include ( expiring_dossier )
is_expected . to include ( just_expired_dossier )
is_expected . to include ( long_expired_dossier )
2018-08-31 12:56:30 +02:00
end
2020-03-20 15:32:36 +01:00
2022-04-08 13:16:08 +02:00
it do
expect ( expiring_dossier . close_to_expiration? ) . to be_truthy
expect ( expiring_dossier_with_notification . close_to_expiration? ) . to be_truthy
end
2020-03-20 15:32:36 +01:00
context 'does not include an expiring dossier that has been postponed' do
before do
2022-04-08 13:16:08 +02:00
expiring_dossier . extend_conservation ( 1 . month )
expiring_dossier_with_notification . extend_conservation ( 1 . month )
2020-03-20 15:32:36 +01:00
expiring_dossier . reload
2022-04-08 13:16:08 +02:00
expiring_dossier_with_notification . reload
2020-03-20 15:32:36 +01:00
end
it { is_expected . not_to include ( expiring_dossier ) }
2022-04-08 13:16:08 +02:00
it do
expect ( expiring_dossier . close_to_expiration? ) . to be_falsey
expect ( expiring_dossier_with_notification . close_to_expiration? ) . to be_falsey
2023-11-10 13:24:37 +01:00
expect ( expiring_dossier . expiration_date ) . to eq ( expiring_dossier . expiration_date_with_extension )
expect ( expiring_dossier_with_notification . expiration_date ) . to eq ( expiring_dossier_with_notification . expiration_date_with_extension )
2022-04-08 13:16:08 +02:00
end
2020-03-20 15:32:36 +01:00
end
2021-11-19 14:28:54 +01:00
context 'when .close_to_expiration' do
subject { Dossier . close_to_expiration }
it do
is_expected . not_to include ( young_dossier )
is_expected . to include ( expiring_dossier )
is_expected . to include ( just_expired_dossier )
is_expected . to include ( long_expired_dossier )
end
end
2021-12-01 17:39:47 +01:00
context 'when .termine_or_en_construction_close_to_expiration' do
subject { Dossier . termine_or_en_construction_close_to_expiration }
it do
is_expected . not_to include ( young_dossier )
is_expected . to include ( expiring_dossier )
is_expected . to include ( just_expired_dossier )
is_expected . to include ( long_expired_dossier )
end
end
2020-02-26 17:36:24 +01:00
end
2018-08-31 12:56:30 +02:00
2021-11-19 14:28:54 +01:00
describe 'termine_close_to_expiration' do
2021-11-30 14:47:19 +01:00
let ( :procedure ) { create ( :procedure , :published , duree_conservation_dossiers_dans_ds : 6 , procedure_expires_when_termine_enabled : true ) }
let! ( :young_dossier ) { create ( :dossier , state : :accepte , procedure : procedure , processed_at : 2 . days . ago ) }
let! ( :expiring_dossier ) { create ( :dossier , state : :accepte , procedure : procedure , processed_at : 175 . days . ago ) }
2022-04-08 13:16:08 +02:00
let! ( :expiring_dossier_with_notification ) { create ( :dossier , state : :accepte , procedure : procedure , processed_at : 175 . days . ago , termine_close_to_expiration_notice_sent_at : Time . zone . now ) }
2021-11-30 14:47:19 +01:00
let! ( :just_expired_dossier ) { create ( :dossier , state : :accepte , procedure : procedure , processed_at : ( 6 . months + 1 . hour + 10 . seconds ) . ago ) }
let! ( :long_expired_dossier ) { create ( :dossier , state : :accepte , procedure : procedure , processed_at : 1 . year . ago ) }
2021-11-19 14:28:54 +01:00
subject { Dossier . termine_close_to_expiration }
it do
is_expected . not_to include ( young_dossier )
is_expected . to include ( expiring_dossier )
is_expected . to include ( just_expired_dossier )
is_expected . to include ( long_expired_dossier )
end
2022-04-08 13:16:08 +02:00
it do
expect ( expiring_dossier . close_to_expiration? ) . to be_truthy
expect ( expiring_dossier_with_notification . close_to_expiration? ) . to be_truthy
end
context 'does not include an expiring dossier that has been postponed' do
before do
expiring_dossier . extend_conservation ( 1 . month )
expiring_dossier_with_notification . extend_conservation ( 1 . month )
expiring_dossier . reload
expiring_dossier_with_notification . reload
end
it { is_expected . not_to include ( expiring_dossier ) }
it do
expect ( expiring_dossier . close_to_expiration? ) . to be_falsey
expect ( expiring_dossier_with_notification . close_to_expiration? ) . to be_falsey
2023-11-10 13:24:37 +01:00
expect ( expiring_dossier . expiration_date ) . to eq ( expiring_dossier . expiration_date_with_extension )
expect ( expiring_dossier_with_notification . expiration_date ) . to eq ( expiring_dossier_with_notification . expiration_date_with_extension )
2022-04-08 13:16:08 +02:00
end
end
2021-11-19 14:28:54 +01:00
context 'when .close_to_expiration' do
subject { Dossier . close_to_expiration }
it do
is_expected . not_to include ( young_dossier )
is_expected . to include ( expiring_dossier )
is_expected . to include ( just_expired_dossier )
is_expected . to include ( long_expired_dossier )
end
end
2021-12-01 17:39:47 +01:00
context 'when .close_to_expiration' do
subject { Dossier . termine_or_en_construction_close_to_expiration }
it do
is_expected . not_to include ( young_dossier )
is_expected . to include ( expiring_dossier )
is_expected . to include ( just_expired_dossier )
is_expected . to include ( long_expired_dossier )
end
end
2018-08-31 12:56:30 +02:00
end
2019-09-23 14:38:12 +02:00
describe 'with_notifications' do
let ( :dossier ) { create ( :dossier ) }
let ( :instructeur ) { create ( :instructeur ) }
before do
create ( :follow , dossier : dossier , instructeur : instructeur , messagerie_seen_at : 2 . hours . ago )
end
2020-09-18 15:40:26 +02:00
subject { instructeur . followed_dossiers . with_notifications }
2019-09-23 14:38:12 +02:00
context ( 'without changes' ) do
it { is_expected . to eq [ ] }
end
context ( 'with changes' ) do
2020-07-30 11:05:39 +02:00
context 'when there is a new commentaire' do
before { dossier . update! ( last_commentaire_updated_at : Time . zone . now ) }
2019-09-23 14:38:12 +02:00
2020-07-30 11:05:39 +02:00
it { is_expected . to match ( [ dossier ] ) }
end
context 'when there is a new avis' do
before { dossier . update! ( last_avis_updated_at : Time . zone . now ) }
it { is_expected . to match ( [ dossier ] ) }
end
context 'when a public champ is updated' do
before { dossier . update! ( last_champ_updated_at : Time . zone . now ) }
it { is_expected . to match ( [ dossier ] ) }
end
context 'when a private champ is updated' do
before { dossier . update! ( last_champ_private_updated_at : Time . zone . now ) }
it { is_expected . to match ( [ dossier ] ) }
end
2019-09-23 14:38:12 +02:00
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
2020-01-07 16:36:04 +01:00
describe '#create' do
let ( :procedure ) { create ( :procedure , :with_type_de_champ , :with_type_de_champ_private ) }
let ( :dossier ) { create ( :dossier , procedure : procedure , user : user ) }
2016-08-08 12:52:30 +02:00
2020-01-07 16:36:04 +01:00
it 'builds public and private champs' do
2022-11-10 22:21:14 +01:00
expect ( dossier . champs_public . count ) . to eq ( 1 )
2020-01-07 16:36:04 +01:00
expect ( dossier . champs_private . count ) . to eq ( 1 )
2015-11-03 15:27:49 +01:00
end
2021-11-19 13:24:39 +01:00
end
describe '#build_default_individual' do
let ( :dossier ) { build ( :dossier , procedure : procedure , user : user ) }
subject do
dossier . individual = nil
dossier . build_default_individual
end
2015-11-03 15:27:49 +01:00
2020-01-07 16:36:04 +01:00
context 'when the dossier belongs to a procedure for individuals' do
2021-11-19 13:24:39 +01:00
let ( :procedure ) { create ( :procedure , for_individual : true ) }
2016-08-30 11:18:43 +02:00
2020-01-07 16:36:04 +01:00
it 'creates a default individual' do
2021-11-19 13:24:39 +01:00
subject
2020-01-07 16:36:04 +01:00
expect ( dossier . individual ) . to be_present
expect ( dossier . individual . nom ) . to be_nil
expect ( dossier . individual . prenom ) . to be_nil
expect ( dossier . individual . gender ) . to be_nil
2016-08-30 11:18:43 +02:00
end
2020-01-07 16:36:04 +01:00
context 'and the user signs-in using France Connect' do
let ( :france_connect_information ) { build ( :france_connect_information ) }
let ( :user ) { build ( :user , france_connect_information : france_connect_information ) }
2016-08-30 11:18:43 +02:00
2020-01-07 16:36:04 +01:00
it 'fills the individual with the informations from France Connect' do
2021-11-19 13:24:39 +01:00
subject
2020-01-07 16:36:04 +01:00
expect ( dossier . individual . nom ) . to eq ( 'DUBOIS' )
expect ( dossier . individual . prenom ) . to eq ( 'Angela Claire Louise' )
expect ( dossier . individual . gender ) . to eq ( Individual :: GENDER_FEMALE )
end
2016-08-30 11:18:43 +02:00
end
end
2016-06-13 12:03:05 +02:00
2020-01-07 16:36:04 +01:00
context 'when the dossier belongs to a procedure for moral personas' do
2021-11-19 13:24:39 +01:00
let ( :procedure ) { create ( :procedure , for_individual : false ) }
2015-11-03 15:27:49 +01:00
2020-01-07 16:36:04 +01:00
it 'doesn’ t create a individual' do
2021-11-19 13:24:39 +01:00
subject
2020-01-07 16:36:04 +01:00
expect ( dossier . individual ) . to be_nil
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 ) }
2019-08-06 11:02:54 +02:00
let ( :instructeur ) { create ( :instructeur ) }
2017-03-02 16:18:14 +01:00
let ( :date1 ) { 1 . day . ago }
let ( :date2 ) { 1 . hour . ago }
let ( :date3 ) { 1 . minute . ago }
2020-07-02 11:02:50 +02:00
let ( :dossier ) do
d = create ( :dossier , :with_entreprise , user : user , procedure : procedure )
Timecop . freeze ( date1 )
d . passer_en_construction!
Timecop . freeze ( date2 )
2021-11-26 12:19:40 +01:00
d . passer_en_instruction! ( instructeur : instructeur )
2020-07-02 11:02:50 +02:00
Timecop . freeze ( date3 )
2021-11-26 12:19:40 +01:00
d . accepter! ( instructeur : instructeur , motivation : " Motivation " )
2020-07-02 11:02:50 +02:00
Timecop . return
d
end
2016-11-14 17:25:17 +01:00
2019-08-06 11:02:54 +02:00
describe " followers_instructeurs " do
let ( :non_following_instructeur ) { create ( :instructeur ) }
subject { dossier . followers_instructeurs }
2018-07-11 16:28:54 +02:00
2019-08-06 11:02:54 +02:00
it { expect ( subject ) . to eq [ instructeur ] }
it { expect ( subject ) . not_to include ( non_following_instructeur ) }
2018-07-11 16:28:54 +02:00
end
2016-11-14 10:41:56 +01:00
end
2018-08-30 11:51:35 +02:00
describe '#champs' do
2022-05-17 10:39:24 +02:00
let ( :procedure ) { create ( :procedure ) }
let! ( :tdc_1 ) { create ( :type_de_champ , libelle : 'l1' , position : 1 , procedure : procedure ) }
let! ( :tdc_3 ) { create ( :type_de_champ , libelle : 'l3' , position : 3 , procedure : procedure ) }
let! ( :tdc_2 ) { create ( :type_de_champ , libelle : 'l2' , position : 2 , procedure : procedure ) }
2020-08-27 19:56:19 +02:00
let ( :dossier ) { create ( :dossier , procedure : procedure ) }
2016-06-21 12:41:28 +02:00
2022-11-10 22:21:14 +01:00
it { expect ( dossier . champs_public . 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
2022-05-17 10:39:24 +02:00
let ( :procedure ) { create ( :procedure ) }
let! ( :tdc_1 ) { create ( :type_de_champ , :private , libelle : 'l1' , position : 1 , procedure : procedure ) }
let! ( :tdc_3 ) { create ( :type_de_champ , :private , libelle : 'l3' , position : 3 , procedure : procedure ) }
let! ( :tdc_2 ) { create ( :type_de_champ , :private , libelle : 'l2' , position : 2 , procedure : procedure ) }
2020-08-27 19:56:19 +02:00
let ( :dossier ) { create ( :dossier , procedure : procedure ) }
2016-08-08 12:52:30 +02:00
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
2021-12-06 15:49:17 +01:00
context 'when the dossier has been submitted' do
let ( :dossier ) { create :dossier , procedure : procedure , state : Dossier . states . fetch ( :en_construction ) , depose_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
2021-12-06 15:49:17 +01:00
context 'when the dossier has not been submitted' 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
2019-08-21 16:16:53 +02:00
let! ( :instructeur ) { create ( :instructeur ) }
let! ( :procedure ) { create ( :procedure , :published , instructeurs : [ instructeur ] ) }
2018-08-28 14:10:55 +02:00
let! ( :dossier ) { create ( :dossier , procedure : procedure , state : Dossier . states . fetch ( :en_construction ) ) }
2021-03-23 12:25:57 +01:00
let! ( :experts_procedure ) { create ( :experts_procedure , expert : expert_1 , procedure : procedure ) }
let! ( :experts_procedure_2 ) { create ( :experts_procedure , expert : expert_2 , procedure : procedure ) }
2021-02-28 22:20:24 +01:00
let! ( :expert_1 ) { create ( :expert ) }
let! ( :expert_2 ) { create ( :expert ) }
2017-09-08 11:52:20 +02:00
2019-08-06 11:02:54 +02:00
context 'when there is a public advice asked from the dossiers instructeur' do
2021-03-23 12:25:57 +01:00
let! ( :avis ) { create ( :avis , dossier : dossier , claimant : instructeur , experts_procedure : experts_procedure , confidentiel : false ) }
2017-09-08 11:52:20 +02:00
2021-02-28 22:20:24 +01:00
it { expect ( dossier . avis_for_instructeur ( instructeur ) ) . to match ( [ avis ] ) }
it { expect ( dossier . avis_for_expert ( expert_1 ) ) . to match ( [ avis ] ) }
it { expect ( dossier . avis_for_expert ( expert_2 ) ) . to match ( [ avis ] ) }
2017-09-08 11:52:20 +02:00
end
2019-08-06 11:02:54 +02:00
context 'when there is a private advice asked from the dossiers instructeur' do
2021-03-23 12:25:57 +01:00
let! ( :avis ) { create ( :avis , dossier : dossier , claimant : instructeur , experts_procedure : experts_procedure , confidentiel : true ) }
2017-09-08 11:52:20 +02:00
2021-02-28 22:20:24 +01:00
it { expect ( dossier . avis_for_instructeur ( instructeur ) ) . to match ( [ avis ] ) }
it { expect ( dossier . avis_for_expert ( expert_1 ) ) . to match ( [ avis ] ) }
it { expect ( dossier . avis_for_expert ( expert_2 ) ) . not_to match ( [ avis ] ) }
2017-09-08 11:52:20 +02:00
end
2021-05-20 10:36:01 +02:00
context 'when there is a public advice asked from one instructeur to an expert' do
let! ( :avis_1 ) { create ( :avis , dossier : dossier , claimant : instructeur , experts_procedure : experts_procedure , confidentiel : false ) }
let! ( :avis_2 ) { create ( :avis , dossier : dossier , claimant : instructeur , experts_procedure : experts_procedure_2 , confidentiel : false ) }
2017-09-08 11:52:20 +02:00
2021-05-20 10:36:01 +02:00
it { expect ( dossier . avis_for_instructeur ( instructeur ) ) . to match ( [ avis_1 , avis_2 ] ) }
it { expect ( dossier . avis_for_expert ( expert_1 ) ) . to match ( [ avis_1 , avis_2 ] ) }
it { expect ( dossier . avis_for_expert ( expert_2 ) ) . to match ( [ avis_1 , avis_2 ] ) }
2017-09-08 11:52:20 +02:00
end
2021-05-20 10:36:01 +02:00
context 'when there is a private advice asked from one instructeur to an expert' do
let! ( :avis_1 ) { create ( :avis , dossier : dossier , claimant : instructeur , experts_procedure : experts_procedure , confidentiel : true ) }
let! ( :avis_2 ) { create ( :avis , dossier : dossier , claimant : instructeur , experts_procedure : experts_procedure_2 , confidentiel : true ) }
2017-09-08 11:52:20 +02:00
2021-05-20 10:36:01 +02:00
it { expect ( dossier . avis_for_instructeur ( instructeur ) ) . to match ( [ avis_1 , avis_2 ] ) }
it { expect ( dossier . avis_for_expert ( expert_1 ) ) . to match ( [ avis_1 ] ) }
it { expect ( dossier . avis_for_expert ( expert_2 ) ) . to match ( [ avis_2 ] ) }
2017-09-08 11:52:20 +02:00
end
context 'when they are a lot of advice' do
2021-03-24 16:43:01 +01:00
let! ( :avis_1 ) { create ( :avis , dossier : dossier , claimant : expert_1 , experts_procedure : experts_procedure_2 , confidentiel : false , created_at : Time . zone . parse ( '10/01/2010' ) ) }
let! ( :avis_2 ) { create ( :avis , dossier : dossier , claimant : expert_1 , experts_procedure : experts_procedure_2 , confidentiel : false , created_at : Time . zone . parse ( '9/01/2010' ) ) }
let! ( :avis_3 ) { create ( :avis , dossier : dossier , claimant : expert_1 , experts_procedure : experts_procedure_2 , confidentiel : false , created_at : Time . zone . parse ( '11/01/2010' ) ) }
2017-09-08 11:52:20 +02:00
2021-02-28 22:20:24 +01:00
it { expect ( dossier . avis_for_instructeur ( instructeur ) ) . to match ( [ avis_2 , avis_1 , avis_3 ] ) }
it { expect ( dossier . avis_for_expert ( expert_1 ) ) . to match ( [ avis_2 , avis_1 , avis_3 ] ) }
2017-09-08 11:52:20 +02:00
end
2021-05-20 16:15:59 +02:00
context 'when they are a advice published on another dossier' do
let! ( :avis ) { create ( :avis , dossier : create ( :dossier , procedure : procedure ) , claimant : instructeur , experts_procedure : experts_procedure , confidentiel : false , created_at : Time . zone . parse ( '9/01/2010' ) ) }
it { expect ( dossier . avis_for_expert ( expert_1 ) ) . to match ( [ ] ) }
end
2017-09-08 11:52:20 +02:00
end
2017-03-01 09:51:55 +01:00
describe '#update_state_dates' do
2021-01-28 13:53:18 +01:00
let ( :dossier ) { create ( :dossier , :brouillon , :with_individual ) }
2018-10-25 15:11:12 +02:00
let ( :beginning_of_day ) { Time . zone . now . beginning_of_day }
2020-07-01 17:31:08 +02:00
let ( :instructeur ) { create ( :instructeur ) }
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
2023-07-18 11:29:44 +02:00
context 'when the procedure.routing_enabled? is false' do
before do
dossier . passer_en_construction!
dossier . reload
end
it { expect ( dossier . state ) . to eq ( Dossier . states . fetch ( :en_construction ) ) }
it { expect ( dossier . en_construction_at ) . to eq ( beginning_of_day ) }
it { expect ( dossier . depose_at ) . to eq ( beginning_of_day ) }
it { expect ( dossier . traitement . state ) . to eq ( Dossier . states . fetch ( :en_construction ) ) }
it { expect ( dossier . traitement . processed_at ) . to eq ( beginning_of_day ) }
it 'should keep first en_construction_at date' do
Timecop . return
dossier . passer_en_instruction! ( instructeur : instructeur )
dossier . repasser_en_construction! ( instructeur : instructeur )
expect ( dossier . traitements . size ) . to eq ( 3 )
expect ( dossier . traitements . first . processed_at ) . to eq ( beginning_of_day )
expect ( dossier . traitement . processed_at . round ) . to eq ( dossier . en_construction_at . round )
expect ( dossier . depose_at ) . to eq ( beginning_of_day )
expect ( dossier . en_construction_at ) . to be > beginning_of_day
end
2017-03-01 09:51:55 +01:00
end
2023-07-18 11:29:44 +02:00
context 'when the procedure.routing_enabled? is true' do
include Logic
let ( :gi_libelle ) { 'Paris' }
let! ( :procedure ) do
create ( :procedure ,
types_de_champ_public : [
{ type : :drop_down_list , libelle : 'Votre ville' , options : [ gi_libelle , 'Lyon' , 'Marseille' ] } ,
{ type : :text , libelle : 'Un champ texte' }
] )
end
let! ( :drop_down_tdc ) { procedure . draft_revision . types_de_champ . first }
let ( :dossier ) { create ( :dossier , :brouillon , user : , procedure : , groupe_instructeur : nil ) }
let ( :gi ) do
create ( :groupe_instructeur ,
routing_rule : ds_eq ( champ_value ( drop_down_tdc . stable_id ) ,
constant ( gi_libelle ) ) )
end
2017-03-01 09:51:55 +01:00
2023-07-18 11:29:44 +02:00
before do
procedure . groupe_instructeurs = [ gi ]
procedure . defaut_groupe_instructeur = gi
procedure . save!
procedure . toggle_routing
dossier . champs . first . value = gi_libelle
dossier . save!
dossier . passer_en_construction!
dossier . reload
end
2017-03-01 11:13:48 +01:00
2023-07-18 11:29:44 +02:00
it 'RoutingEngine.compute' do
expect ( dossier . groupe_instructeur ) . not_to be_nil
end
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
2021-01-28 13:53:18 +01:00
let ( :dossier ) { create ( :dossier , :en_construction , :with_individual ) }
2020-07-01 17:31:08 +02:00
let ( :instructeur ) { create ( :instructeur ) }
2017-03-01 09:51:55 +01:00
before do
2021-11-26 12:19:40 +01:00
dossier . passer_en_instruction! ( instructeur : instructeur )
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 ) }
2021-11-04 19:05:04 +01:00
it { expect ( dossier . traitement . state ) . to eq ( Dossier . states . fetch ( :en_instruction ) ) }
it { expect ( dossier . traitement . processed_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
2022-12-04 12:34:34 +01:00
dossier . repasser_en_construction! ( instructeur : instructeur )
2021-11-26 12:19:40 +01:00
dossier . passer_en_instruction! ( instructeur : instructeur )
2017-03-01 11:13:48 +01:00
2021-11-24 12:27:32 +01:00
expect ( dossier . traitements . size ) . to eq ( 4 )
expect ( dossier . traitements . en_construction . first . processed_at ) . to eq ( dossier . depose_at )
expect ( dossier . traitements . en_instruction . first . processed_at ) . to eq ( beginning_of_day )
2021-11-04 19:05:04 +01:00
expect ( dossier . traitement . processed_at . round ) . to eq ( dossier . en_instruction_at . round )
expect ( dossier . en_instruction_at ) . to be > beginning_of_day
2017-03-01 11:13:48 +01:00
end
2017-03-01 09:51:55 +01:00
end
2020-07-01 17:31:08 +02:00
context 'when dossier is accepte' do
2021-01-28 13:53:18 +01:00
let ( :dossier ) { create ( :dossier , :en_instruction , :with_individual ) }
2020-07-01 17:31:08 +02:00
2017-03-01 09:51:55 +01:00
before do
2021-11-26 12:19:40 +01:00
dossier . accepter! ( instructeur : instructeur )
2017-03-01 09:51:55 +01:00
dossier . reload
end
2020-07-01 17:31:08 +02:00
it { expect ( dossier . state ) . to eq ( Dossier . states . fetch ( :accepte ) ) }
2017-03-01 11:13:48 +01:00
it { expect ( dossier . processed_at ) . to eq ( beginning_of_day ) }
2021-11-04 19:05:04 +01:00
it { expect ( dossier . traitement . state ) . to eq ( Dossier . states . fetch ( :accepte ) ) }
it { expect ( dossier . traitement . processed_at ) . to eq ( beginning_of_day ) }
2017-03-01 09:51:55 +01:00
end
2017-12-04 18:15:40 +01:00
context 'when dossier is refuse' do
2021-01-28 13:53:18 +01:00
let ( :dossier ) { create ( :dossier , :en_instruction , :with_individual ) }
2017-03-01 09:51:55 +01:00
2020-07-01 17:31:08 +02:00
before do
2021-11-26 12:19:40 +01:00
dossier . refuser! ( instructeur : instructeur )
2020-07-01 17:31:08 +02:00
dossier . reload
end
it { expect ( dossier . state ) . to eq ( Dossier . states . fetch ( :refuse ) ) }
it { expect ( dossier . processed_at ) . to eq ( beginning_of_day ) }
2021-11-04 19:05:04 +01:00
it { expect ( dossier . traitement . state ) . to eq ( Dossier . states . fetch ( :refuse ) ) }
it { expect ( dossier . traitement . processed_at ) . to eq ( beginning_of_day ) }
2017-03-01 09:51:55 +01:00
end
2017-12-04 20:23:57 +01:00
context 'when dossier is sans_suite' do
2021-01-28 13:53:18 +01:00
let ( :dossier ) { create ( :dossier , :en_instruction , :with_individual ) }
2017-03-01 09:51:55 +01:00
2020-07-01 17:31:08 +02:00
before do
2021-11-26 12:19:40 +01:00
dossier . classer_sans_suite! ( instructeur : instructeur )
2020-07-01 17:31:08 +02:00
dossier . reload
end
it { expect ( dossier . state ) . to eq ( Dossier . states . fetch ( :sans_suite ) ) }
it { expect ( dossier . processed_at ) . to eq ( beginning_of_day ) }
2021-11-04 19:05:04 +01:00
it { expect ( dossier . traitement . state ) . to eq ( Dossier . states . fetch ( :sans_suite ) ) }
it { expect ( dossier . traitement . processed_at ) . to eq ( beginning_of_day ) }
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
2022-10-10 10:55:31 +02:00
describe '.ordered_for_export' do
2017-03-29 14:09:50 +02:00
let ( :procedure ) { create ( :procedure ) }
2021-12-06 15:49:17 +01:00
let! ( :dossier2 ) { create ( :dossier , :with_entreprise , procedure : procedure , state : Dossier . states . fetch ( :en_construction ) , depose_at : Time . zone . parse ( '03/01/2010' ) ) }
let! ( :dossier3 ) { create ( :dossier , :with_entreprise , procedure : procedure , state : Dossier . states . fetch ( :en_instruction ) , depose_at : Time . zone . parse ( '01/01/2010' ) ) }
let! ( :dossier4 ) { create ( :dossier , :with_entreprise , procedure : procedure , state : Dossier . states . fetch ( :en_instruction ) , archived : true , depose_at : Time . zone . parse ( '02/01/2010' ) ) }
2017-03-29 14:09:50 +02:00
2022-10-10 10:55:31 +02:00
subject { procedure . dossiers . ordered_for_export }
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
2020-02-26 12:30:52 +01:00
describe " # assign_to_groupe_instructeur " do
let ( :procedure ) { create ( :procedure ) }
let ( :new_groupe_instructeur_new_procedure ) { create ( :groupe_instructeur ) }
let ( :new_groupe_instructeur ) { create ( :groupe_instructeur , procedure : procedure ) }
let ( :dossier ) { create ( :dossier , :en_construction , procedure : procedure ) }
it " can change groupe instructeur " do
2023-07-05 17:48:18 +02:00
dossier . assign_to_groupe_instructeur ( new_groupe_instructeur_new_procedure , DossierAssignment . modes . fetch ( :auto ) )
2020-02-26 12:30:52 +01:00
expect ( dossier . groupe_instructeur ) . not_to eq ( new_groupe_instructeur_new_procedure )
end
it " can not change groupe instructeur if new groupe is from another procedure " do
2023-07-05 17:48:18 +02:00
dossier . assign_to_groupe_instructeur ( new_groupe_instructeur , DossierAssignment . modes . fetch ( :auto ) )
2020-02-26 12:30:52 +01:00
expect ( dossier . groupe_instructeur ) . to eq ( new_groupe_instructeur )
end
end
2020-02-25 15:51:30 +01:00
describe " # unfollow_stale_instructeurs " do
2021-01-28 13:53:18 +01:00
let ( :procedure ) { create ( :procedure , :published , :for_individual ) }
2020-02-25 15:51:30 +01:00
let ( :instructeur ) { create ( :instructeur ) }
2020-02-26 12:30:52 +01:00
let ( :new_groupe_instructeur ) { create ( :groupe_instructeur , procedure : procedure ) }
2020-02-25 15:51:30 +01:00
let ( :instructeur2 ) { create ( :instructeur , groupe_instructeurs : [ procedure . defaut_groupe_instructeur , new_groupe_instructeur ] ) }
2021-01-28 13:53:18 +01:00
let ( :dossier ) { create ( :dossier , :en_construction , :with_individual , procedure : procedure ) }
2020-02-26 10:28:37 +01:00
let ( :last_operation ) { DossierOperationLog . last }
2020-02-25 15:51:30 +01:00
2020-02-25 18:16:21 +01:00
before do
allow ( DossierMailer ) . to receive ( :notify_groupe_instructeur_changed ) . and_return ( double ( deliver_later : nil ) )
end
2020-02-25 15:51:30 +01:00
it " unfollows stale instructeurs when groupe instructeur change " do
instructeur . follow ( dossier )
instructeur2 . follow ( dossier )
2023-07-05 17:48:18 +02:00
dossier . reload . assign_to_groupe_instructeur ( new_groupe_instructeur , DossierAssignment . modes . fetch ( :auto ) , procedure . administrateurs . first )
2020-02-25 15:51:30 +01:00
expect ( dossier . reload . followers_instructeurs ) . not_to include ( instructeur )
expect ( dossier . reload . followers_instructeurs ) . to include ( instructeur2 )
2020-02-26 10:28:37 +01:00
2020-02-25 18:16:21 +01:00
expect ( DossierMailer ) . to have_received ( :notify_groupe_instructeur_changed ) . with ( instructeur , dossier )
expect ( DossierMailer ) . not_to have_received ( :notify_groupe_instructeur_changed ) . with ( instructeur2 , dossier )
2020-02-26 10:28:37 +01:00
expect ( last_operation . operation ) . to eq ( " changer_groupe_instructeur " )
expect ( last_operation . dossier ) . to eq ( dossier )
expect ( last_operation . automatic_operation? ) . to be_falsey
2020-02-25 15:51:30 +01:00
end
end
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 ) ) }
2020-07-01 17:31:08 +02:00
let ( :instructeur ) { create ( :instructeur ) }
2017-05-26 20:01:57 +02:00
before do
2021-04-29 19:10:22 +02:00
allow ( NotificationMailer ) . to receive ( :send_en_instruction_notification ) . 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
2021-11-26 12:19:40 +01:00
dossier . passer_en_instruction! ( instructeur : instructeur )
2021-04-29 19:10:22 +02:00
expect ( NotificationMailer ) . to have_received ( :send_en_instruction_notification ) . 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!
2021-04-29 19:10:22 +02:00
expect ( NotificationMailer ) . to_not have_received ( :send_en_instruction_notification )
2017-05-26 20:01:57 +02:00
end
end
2017-06-08 14:04:47 +02:00
2018-04-06 13:09:37 +02:00
describe " # unspecified_attestation_champs " do
2022-08-04 11:39:07 +02:00
let ( :procedure ) { create ( :procedure , attestation_template : attestation_template , types_de_champ_public : types_de_champ , types_de_champ_private : types_de_champ_private ) }
2021-07-21 10:46:44 +02:00
let ( :dossier ) { create ( :dossier , :en_instruction , procedure : procedure ) }
2020-08-27 19:56:19 +02:00
let ( :types_de_champ ) { [ ] }
let ( :types_de_champ_private ) { [ ] }
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 ? " }
2020-07-20 16:30:29 +02:00
let ( :attestation_template ) { build ( :attestation_template , title : title , body : body , activated : activated ) }
2018-04-06 13:09:37 +02:00
context " which is disabled " do
let ( :activated ) { false }
it { is_expected . to eq ( [ ] ) }
end
context " wich is enabled " do
let ( :activated ) { true }
2020-08-27 19:56:19 +02:00
let ( :types_de_champ ) { [ tdc_1 , tdc_2 , tdc_3 , tdc_4 ] }
let ( :types_de_champ_private ) { [ tdc_5 , tdc_6 , tdc_7 , tdc_8 ] }
2022-08-04 11:39:07 +02:00
let ( :tdc_1 ) { { libelle : " specified champ-in-title " } }
let ( :tdc_2 ) { { libelle : " unspecified champ-in-title " } }
let ( :tdc_3 ) { { libelle : " specified champ-in-body " } }
let ( :tdc_4 ) { { libelle : " unspecified champ-in-body " } }
let ( :tdc_5 ) { { libelle : " specified annotation privée-in-title " } }
let ( :tdc_6 ) { { libelle : " unspecified annotation privée-in-title " } }
let ( :tdc_7 ) { { libelle : " specified annotation privée-in-body " } }
let ( :tdc_8 ) { { libelle : " unspecified annotation privée-in-body " } }
2018-04-06 13:09:37 +02:00
before do
2022-11-10 22:21:14 +01:00
( dossier . champs_public + dossier . champs_private )
2019-09-12 11:26:22 +02:00
. filter { | c | c . libelle . match? ( / ^specified / ) }
2018-04-06 13:09:37 +02:00
. 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
2022-11-09 12:09:24 +01:00
let! ( :dossier ) { create ( :dossier , :en_instruction , procedure : procedure ) }
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
2022-11-09 12:09:24 +01:00
let ( :attestation_template ) { build ( :attestation_template , activated : false ) }
2017-06-08 14:04:47 +02:00
it { expect ( dossier . attestation ) . to be_nil }
end
context 'when the procedure attached has an activated attestation' do
2022-11-09 12:09:24 +01:00
let ( :attestation_template ) { build ( :attestation_template , activated : true ) }
2017-06-08 14:04:47 +02:00
it { expect ( dossier . attestation ) . not_to be_nil }
end
end
end
2017-06-27 15:26:40 +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 champ is modified' do
2022-11-10 22:21:14 +01:00
before { dossier . champs_public . first . update_attribute ( 'value' , 'yop' ) }
2017-10-24 18:12:25 +02:00
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
2020-01-07 17:45:39 +01:00
let ( :procedure ) { create ( :procedure ) }
2017-11-17 23:40:51 +01:00
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
2020-01-07 17:45:39 +01:00
let ( :procedure ) { create ( :procedure , :for_individual ) }
2020-01-07 17:47:22 +01:00
let ( :dossier ) { create ( :dossier , :with_individual , procedure : procedure ) }
2017-11-17 23:40:51 +01:00
it { is_expected . to eq ( " #{ dossier . individual . nom } #{ dossier . individual . prenom } " ) }
end
end
2017-10-30 16:54:33 +01:00
2022-03-09 10:29:16 +01:00
describe " # hide_and_keep_track! " do
2020-03-19 11:53:25 +01:00
let ( :dossier ) { create ( :dossier , :en_construction ) }
2022-01-05 10:43:20 +01:00
let ( :user ) { dossier . user }
2019-07-15 16:00:49 +02:00
let ( :last_operation ) { dossier . dossier_operation_logs . last }
2020-03-19 11:53:25 +01:00
let ( :reason ) { :user_request }
2018-07-23 14:44:42 +02:00
before do
allow ( DossierMailer ) . to receive ( :notify_deletion_to_administration ) . and_return ( double ( deliver_later : nil ) )
end
2022-03-09 10:29:16 +01:00
subject! { dossier . hide_and_keep_track! ( user , reason ) }
2018-07-23 14:44:42 +02:00
2020-03-19 11:53:25 +01:00
context 'brouillon' do
let ( :dossier ) { create ( :dossier ) }
2018-07-23 14:44:42 +02:00
2022-03-09 10:29:16 +01:00
it 'hide the dossier' do
expect ( dossier . reload . hidden_by_user_at ) . to be_present
2020-03-19 11:53:25 +01:00
end
2018-07-23 14:44:42 +02:00
2022-03-09 10:29:16 +01:00
it 'does not records operation in the log' do
expect ( dossier . reload . dossier_operation_logs . last ) . to eq ( nil )
2020-03-19 11:53:25 +01:00
end
2019-07-15 16:00:49 +02:00
end
2020-03-19 11:53:25 +01:00
context 'en_construction' do
2022-01-05 10:43:20 +01:00
it 'hide the dossier but does not discard' do
expect ( dossier . hidden_at ) . to be_nil
expect ( dossier . hidden_by_user_at ) . to be_present
2018-07-23 15:06:06 +02:00
end
2020-03-19 11:53:25 +01:00
it 'records the operation in the log' do
expect ( last_operation . operation ) . to eq ( " supprimer " )
expect ( last_operation . automatic_operation? ) . to be_falsey
end
context 'where instructeurs are following the dossier' do
let ( :dossier ) { create ( :dossier , :en_construction , :followed ) }
let! ( :non_following_instructeur ) do
non_following_instructeur = create ( :instructeur )
non_following_instructeur . groupe_instructeurs << dossier . procedure . defaut_groupe_instructeur
non_following_instructeur
end
end
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
context 'with reason: user_removed' do
let ( :reason ) { :user_removed }
2022-01-05 10:43:20 +01:00
it 'hide the dossier' do
expect ( dossier . hidden_by_user_at ) . to be_present
2020-03-19 11:53:25 +01:00
end
2022-03-09 10:29:16 +01:00
it 'write the good reason to hidden_by_reason' do
expect ( dossier . hidden_by_reason ) . to eq ( " user_removed " )
end
2018-08-02 16:36:50 +02:00
end
end
2022-02-08 11:19:50 +01:00
context 'termine' do
let ( :dossier ) { create ( :dossier , state : " accepte " , hidden_by_administration_at : 1 . hour . ago ) }
before { subject }
it 'affect the right deletion reason to the dossier' do
expect ( dossier . hidden_by_reason ) . to eq ( " user_request " )
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 ) }
2020-07-01 17:31:08 +02:00
let ( :instructeur ) { create ( :instructeur ) }
2018-03-01 17:04:05 +01:00
it 'should not call webhook' do
expect {
dossier . accepte!
} . to_not have_enqueued_job ( WebHookJob )
end
2020-09-22 17:03:19 +02:00
it 'should not call webhook with empty value' do
dossier . procedure . update_column ( :web_hook_url , '' )
expect {
dossier . accepte!
} . to_not have_enqueued_job ( WebHookJob )
end
it 'should not call webhook with blank value' do
dossier . procedure . update_column ( :web_hook_url , ' ' )
expect {
dossier . accepte!
} . to_not have_enqueued_job ( WebHookJob )
end
2018-03-01 17:04:05 +01:00
it 'should call webhook' do
dossier . procedure . update_column ( :web_hook_url , '/webhook.json' )
expect {
2020-07-02 11:02:50 +02:00
dossier . update_column ( :search_terms , 'bonjour' )
2018-03-01 17:04:05 +01:00
} . to_not have_enqueued_job ( WebHookJob )
expect {
2020-07-01 17:31:08 +02:00
dossier . passer_en_construction!
2022-06-01 15:03:31 +02:00
} . to have_enqueued_job ( WebHookJob ) . with ( dossier . procedure . id , dossier . id , 'en_construction' , anything )
2018-03-01 17:04:05 +01:00
expect {
2020-07-02 11:02:50 +02:00
dossier . update_column ( :search_terms , 'bonjour2' )
2018-03-01 17:04:05 +01:00
} . to_not have_enqueued_job ( WebHookJob )
expect {
2021-11-26 12:19:40 +01:00
dossier . passer_en_instruction! ( instructeur : instructeur )
2022-06-01 15:03:31 +02:00
} . to have_enqueued_job ( WebHookJob ) . with ( dossier . procedure . id , dossier . id , 'en_instruction' , anything )
2018-03-01 17:04:05 +01:00
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 }
2019-11-14 09:43:45 +01:00
context " procedure is closed " do
before { procedure . close! }
2018-06-25 18:07:16 +02:00
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
2019-06-20 09:58:53 +02:00
describe " # messagerie_available? " do
let ( :procedure ) { create ( :procedure ) }
let ( :dossier ) { create ( :dossier , procedure : procedure ) }
subject { dossier . messagerie_available? }
context " dossier is brouillon " do
before { dossier . state = Dossier . states . fetch ( :brouillon ) }
it { is_expected . to be false }
end
2019-07-11 15:37:04 +02:00
context " dossier is submitted " do
before { dossier . state = Dossier . states . fetch ( :en_instruction ) }
2019-06-20 09:58:53 +02:00
2019-07-11 15:37:04 +02:00
it { is_expected . to be true }
2019-06-20 09:58:53 +02:00
end
2019-07-11 15:37:04 +02:00
context " dossier is archived " do
before { dossier . archived = true }
2019-06-20 09:58:53 +02:00
it { is_expected . to be false }
end
end
2019-01-16 11:00:25 +01:00
describe '#accepter!' do
2021-01-28 13:53:18 +01:00
let ( :dossier ) { create ( :dossier , :en_instruction , :with_individual ) }
2019-05-02 16:22:16 +02:00
let ( :last_operation ) { dossier . dossier_operation_logs . last }
2022-11-18 11:31:23 +01:00
let ( :operation_serialized ) { last_operation . data }
2019-08-06 11:02:54 +02:00
let! ( :instructeur ) { create ( :instructeur ) }
2019-01-16 11:00:25 +01:00
let! ( :now ) { Time . zone . parse ( '01/01/2100' ) }
let ( :attestation ) { Attestation . new }
before do
2021-04-29 19:10:22 +02:00
allow ( NotificationMailer ) . to receive ( :send_accepte_notification ) . and_return ( double ( deliver_later : true ) )
2019-01-16 11:00:25 +01:00
allow ( dossier ) . to receive ( :build_attestation ) . and_return ( attestation )
Timecop . freeze ( now )
2021-11-26 12:19:40 +01:00
dossier . accepter! ( instructeur : instructeur , motivation : 'motivation' )
2019-01-16 11:00:25 +01:00
dossier . reload
end
after { Timecop . return }
2020-07-02 11:02:50 +02:00
it { expect ( dossier . traitements . last . motivation ) . to eq ( 'motivation' ) }
2019-01-16 11:00:25 +01:00
it { expect ( dossier . motivation ) . to eq ( 'motivation' ) }
2020-07-07 10:43:07 +02:00
it { expect ( dossier . traitements . last . instructeur_email ) . to eq ( instructeur . email ) }
2019-07-02 15:38:23 +02:00
it { expect ( dossier . en_instruction_at ) . to eq ( dossier . en_instruction_at ) }
2020-07-02 11:02:50 +02:00
it { expect ( dossier . traitements . last . processed_at ) . to eq ( now ) }
2019-01-16 11:00:25 +01:00
it { expect ( dossier . processed_at ) . to eq ( now ) }
it { expect ( dossier . state ) . to eq ( 'accepte' ) }
2019-05-02 16:22:16 +02:00
it { expect ( last_operation . operation ) . to eq ( 'accepter' ) }
it { expect ( last_operation . automatic_operation? ) . to be_falsey }
it { expect ( operation_serialized [ 'operation' ] ) . to eq ( 'accepter' ) }
it { expect ( operation_serialized [ 'dossier_id' ] ) . to eq ( dossier . id ) }
it { expect ( operation_serialized [ 'executed_at' ] ) . to eq ( last_operation . executed_at . iso8601 ) }
2021-04-29 19:10:22 +02:00
it { expect ( NotificationMailer ) . to have_received ( :send_accepte_notification ) . with ( dossier ) }
2019-01-16 11:00:25 +01:00
it { expect ( dossier . attestation ) . to eq ( attestation ) }
2023-07-07 14:06:10 +02:00
it { expect ( dossier . commentaires . count ) . to eq ( 1 ) }
2019-01-16 11:00:25 +01:00
end
describe '#accepter_automatiquement!' do
2019-05-02 16:22:16 +02:00
let ( :last_operation ) { dossier . dossier_operation_logs . last }
2019-01-16 11:00:25 +01:00
let! ( :now ) { Time . zone . parse ( '01/01/2100' ) }
let ( :attestation ) { Attestation . new }
before do
2021-04-29 19:10:22 +02:00
allow ( NotificationMailer ) . to receive ( :send_accepte_notification ) . and_return ( double ( deliver_later : true ) )
2019-01-16 11:00:25 +01:00
allow ( dossier ) . to receive ( :build_attestation ) . and_return ( attestation )
Timecop . freeze ( now )
2023-06-04 23:50:34 +02:00
end
after { Timecop . return }
subject {
2019-01-16 11:00:25 +01:00
dossier . accepter_automatiquement!
dossier . reload
2023-06-04 23:50:34 +02:00
}
context 'as declarative procedure' do
let ( :dossier ) { create ( :dossier , :en_construction , :with_individual , :with_declarative_accepte ) }
it 'accepts dossier automatiquement' do
expect ( subject . motivation ) . to eq ( nil )
expect ( subject . en_instruction_at ) . to eq ( now )
expect ( subject . processed_at ) . to eq ( now )
expect ( subject . declarative_triggered_at ) . to eq ( now )
expect ( subject . sva_svr_decision_triggered_at ) . to be_nil
expect ( subject ) . to be_accepte
expect ( last_operation . operation ) . to eq ( 'accepter' )
expect ( last_operation . automatic_operation? ) . to be_truthy
expect ( NotificationMailer ) . to have_received ( :send_accepte_notification ) . with ( dossier )
expect ( subject . attestation ) . to eq ( attestation )
end
2019-01-16 11:00:25 +01:00
end
2023-06-04 23:50:34 +02:00
context 'as sva procedure' do
let ( :procedure ) { create ( :procedure , :for_individual , :published , :sva ) }
let ( :dossier ) { create ( :dossier , :en_instruction , :with_individual , procedure : , sva_svr_decision_on : Date . current , en_instruction_at : DateTime . new ( 2021 , 5 , 1 , 12 ) ) }
2019-01-16 11:00:25 +01:00
2023-06-04 23:50:34 +02:00
it 'accepts dossier automatiquement' do
expect ( subject . motivation ) . to eq ( nil )
expect ( subject . en_instruction_at ) . to eq ( DateTime . new ( 2021 , 5 , 1 , 12 ) )
expect ( subject . processed_at ) . to eq ( now )
expect ( subject . declarative_triggered_at ) . to be_nil
expect ( subject . sva_svr_decision_triggered_at ) . to eq ( now )
expect ( subject ) . to be_accepte
expect ( last_operation . operation ) . to eq ( 'accepter' )
expect ( last_operation . automatic_operation? ) . to be_truthy
expect ( NotificationMailer ) . to have_received ( :send_accepte_notification ) . with ( dossier )
expect ( subject . attestation ) . to eq ( attestation )
2023-07-07 14:06:10 +02:00
expect ( dossier . commentaires . count ) . to eq ( 1 )
2023-06-04 23:50:34 +02:00
end
end
2019-01-16 11:00:25 +01:00
end
2023-07-31 13:50:22 +02:00
describe '#refuser_automatiquement' do
context 'as svr procedure' do
let ( :last_operation ) { dossier . dossier_operation_logs . last }
let ( :procedure ) { create ( :procedure , :for_individual , :published , :svr ) }
let ( :dossier ) { create ( :dossier , :en_instruction , :with_individual , procedure : , sva_svr_decision_on : Date . current , en_instruction_at : DateTime . new ( 2021 , 5 , 1 , 12 ) ) }
before {
freeze_time
allow ( NotificationMailer ) . to receive ( :send_refuse_notification ) . and_return ( double ( deliver_later : true ) )
}
subject {
dossier . refuser_automatiquement!
dossier . reload
}
it 'refuses dossier automatiquement' do
expect ( subject . en_instruction_at ) . to eq ( DateTime . new ( 2021 , 5 , 1 , 12 ) )
expect ( subject . processed_at ) . to eq ( Time . current )
expect ( subject . declarative_triggered_at ) . to be_nil
expect ( subject . sva_svr_decision_triggered_at ) . to eq ( Time . current )
expect ( subject . motivation ) . to include ( " dans le délai imparti " )
expect ( subject ) . to be_refuse
expect ( last_operation . operation ) . to eq ( 'refuser' )
expect ( last_operation . automatic_operation? ) . to be_truthy
expect ( NotificationMailer ) . to have_received ( :send_refuse_notification ) . with ( dossier )
expect ( subject . attestation ) . to be_nil
expect ( dossier . commentaires . count ) . to eq ( 1 )
end
context 'for an user having english locale' do
before { dossier . user . update! ( locale : 'en' ) }
it 'translates the motivation' do
expect ( subject . motivation ) . to include ( 'within the time limit' )
end
end
end
end
2019-01-10 17:23:48 +01:00
describe '#passer_en_instruction!' do
2021-11-04 19:05:04 +01:00
let ( :dossier ) { create ( :dossier , :en_construction , en_construction_close_to_expiration_notice_sent_at : Time . zone . now ) }
2019-05-02 16:22:16 +02:00
let ( :last_operation ) { dossier . dossier_operation_logs . last }
2022-11-18 11:31:23 +01:00
let ( :operation_serialized ) { last_operation . data }
2019-08-06 11:02:54 +02:00
let ( :instructeur ) { create ( :instructeur ) }
2019-01-10 17:23:48 +01:00
2023-07-12 12:00:52 +02:00
subject ( :passer_en_instruction ) { dossier . passer_en_instruction! ( instructeur : instructeur ) }
2019-01-10 17:23:48 +01:00
2023-07-12 12:00:52 +02:00
it do
passer_en_instruction
expect ( dossier . state ) . to eq ( 'en_instruction' )
expect ( dossier . followers_instructeurs ) . to include ( instructeur )
expect ( dossier . en_construction_close_to_expiration_notice_sent_at ) . to be_nil
expect ( last_operation . operation ) . to eq ( 'passer_en_instruction' )
expect ( last_operation . automatic_operation? ) . to be_falsey
expect ( operation_serialized [ 'operation' ] ) . to eq ( 'passer_en_instruction' )
expect ( operation_serialized [ 'dossier_id' ] ) . to eq ( dossier . id )
expect ( operation_serialized [ 'executed_at' ] ) . to eq ( last_operation . executed_at . iso8601 )
end
it { expect { passer_en_instruction } . to change { dossier . commentaires . count } . by ( 1 ) }
2023-04-04 10:29:27 +02:00
2023-07-07 14:06:10 +02:00
it 'creates a commentaire in the messagerie with expected wording' do
2023-07-12 12:00:52 +02:00
passer_en_instruction
2023-11-29 12:59:55 +01:00
email_template = dossier . procedure . email_template_for ( dossier . state )
2023-07-12 12:00:52 +02:00
commentaire = dossier . commentaires . last
2023-07-07 14:06:10 +02:00
expect ( commentaire . body ) . to include ( email_template . subject_for_dossier ( dossier ) , email_template . body_for_dossier ( dossier ) )
expect ( commentaire . dossier ) . to eq ( dossier )
end
2019-01-10 17:23:48 +01:00
end
describe '#passer_automatiquement_en_instruction!' do
2019-05-02 16:22:16 +02:00
let ( :last_operation ) { dossier . dossier_operation_logs . last }
2022-11-18 11:31:23 +01:00
let ( :operation_serialized ) { last_operation . data }
2019-08-06 11:02:54 +02:00
let ( :instructeur ) { create ( :instructeur ) }
2019-01-10 17:23:48 +01:00
2023-06-02 17:51:02 +02:00
context " via procedure declarative en instruction " do
let ( :dossier ) { create ( :dossier , :en_construction , :with_declarative_en_instruction , en_construction_close_to_expiration_notice_sent_at : Time . zone . now ) }
2019-01-10 17:23:48 +01:00
2023-06-02 17:51:02 +02:00
subject do
dossier . process_declarative!
dossier . reload
end
it 'passes dossier en instruction' do
expect ( subject . followers_instructeurs ) . not_to include ( instructeur )
expect ( subject . en_construction_close_to_expiration_notice_sent_at ) . to be_nil
expect ( subject . declarative_triggered_at ) . to be_within ( 1 . second ) . of ( Time . current )
expect ( last_operation . operation ) . to eq ( 'passer_en_instruction' )
expect ( last_operation . automatic_operation? ) . to be_truthy
expect ( operation_serialized [ 'operation' ] ) . to eq ( 'passer_en_instruction' )
expect ( operation_serialized [ 'dossier_id' ] ) . to eq ( dossier . id )
expect ( operation_serialized [ 'executed_at' ] ) . to eq ( last_operation . executed_at . iso8601 )
2023-07-07 14:06:10 +02:00
expect ( dossier . commentaires . count ) . to eq ( 1 )
2023-06-02 17:51:02 +02:00
end
end
context " via procedure sva " do
2023-06-20 16:15:18 +02:00
let ( :procedure ) { create ( :procedure , :sva , :published , :for_individual ) }
2023-06-28 12:18:26 +02:00
let ( :dossier ) { create ( :dossier , :en_construction , :with_individual , procedure : , sva_svr_decision_on : 10 . days . from_now ) }
2023-06-02 17:51:02 +02:00
subject do
dossier . process_sva_svr!
dossier . reload
end
it 'passes dossier en instruction' do
2023-06-28 12:18:26 +02:00
expect ( subject . state ) . to eq ( 'en_instruction' )
2023-06-02 17:51:02 +02:00
expect ( subject . followers_instructeurs ) . not_to include ( instructeur )
2023-06-28 12:18:26 +02:00
expect ( subject . sva_svr_decision_on ) . to eq ( 2 . months . from_now . to_date + 1 . day ) # date is updated
2023-06-02 17:51:02 +02:00
expect ( last_operation . operation ) . to eq ( 'passer_en_instruction' )
expect ( last_operation . automatic_operation? ) . to be_truthy
expect ( operation_serialized [ 'operation' ] ) . to eq ( 'passer_en_instruction' )
expect ( operation_serialized [ 'dossier_id' ] ) . to eq ( dossier . id )
expect ( operation_serialized [ 'executed_at' ] ) . to eq ( last_operation . executed_at . iso8601 )
end
2023-06-28 12:18:26 +02:00
context 'when dossier was submitted with sva not yet enabled' do
let ( :dossier ) { create ( :dossier , :en_construction , :with_individual , procedure : , depose_at : 10 . days . ago ) }
it 'leaves dossier en construction' do
expect ( subject . sva_svr_decision_on ) . to be_nil
expect ( subject . state ) . to eq ( 'en_construction' )
end
end
2023-06-02 17:51:02 +02:00
end
2019-01-10 17:23:48 +01:00
end
2019-01-30 16:14:15 +01:00
2023-06-06 14:41:47 +02:00
describe '#can_repasser_en_construction?' do
let ( :dossier ) { create ( :dossier , :en_instruction ) }
it { expect ( dossier . can_repasser_en_construction? ) . to be_truthy }
context 'when procedure is sva' do
let ( :dossier ) { create ( :dossier , :en_instruction , procedure : create ( :procedure , :sva ) ) }
it { expect ( dossier . can_repasser_en_construction? ) . to be_falsey }
end
end
2023-12-07 17:32:39 +01:00
describe '#can_passer_en_instruction?' do
let ( :dossier ) { create ( :dossier , :en_construction ) }
it { expect ( dossier . can_passer_en_instruction? ) . to be_truthy }
context 'when there is a pending correction' do
before { create ( :dossier_correction , dossier : ) }
it { expect ( dossier . can_passer_en_instruction? ) . to be_falsey }
end
context 'when there is a resolved correction' do
before { create ( :dossier_correction , :resolved , dossier : ) }
it { expect ( dossier . can_passer_en_instruction? ) . to be_truthy }
end
end
2022-12-14 18:15:57 +01:00
describe '#can_passer_automatiquement_en_instruction?' do
let ( :dossier ) { create ( :dossier , :en_construction , declarative_triggered_at : declarative_triggered_at ) }
let ( :declarative_triggered_at ) { nil }
it { expect ( dossier . can_passer_automatiquement_en_instruction? ) . to be_falsey }
context 'when dossier is declarative' do
before { dossier . procedure . update ( declarative_with_state : :en_instruction ) }
context 'when dossier never transitioned' do
it { expect ( dossier . can_passer_automatiquement_en_instruction? ) . to be_truthy }
end
context 'when dossier transitioned before' do
let ( :declarative_triggered_at ) { 1 . day . ago }
it { expect ( dossier . can_passer_automatiquement_en_instruction? ) . to be_falsey }
end
end
context 'when procedure has auto archive set' do
before { dossier . procedure . update ( auto_archive_on : 1 . day . ago ) }
it { expect ( dossier . can_passer_automatiquement_en_instruction? ) . to be_truthy }
context 'when auto_archive_on is in the future' do
before { dossier . procedure . update ( auto_archive_on : 1 . day . from_now ) }
it { expect ( dossier . can_passer_automatiquement_en_instruction? ) . to be_falsey }
end
context 'when dossier transitioned before' do
let ( :declarative_triggered_at ) { 1 . day . ago }
it { expect ( dossier . can_passer_automatiquement_en_instruction? ) . to be_truthy }
end
2023-12-12 12:07:12 +01:00
context 'when there are pending correction' do
before { create ( :dossier_correction , dossier : ) }
it " passes en instruction and keep the correction request " do
expect ( dossier . can_passer_automatiquement_en_instruction? ) . to be_truthy
expect ( dossier . pending_correction? ) . to be_truthy
end
end
2022-12-14 18:15:57 +01:00
end
2023-06-02 17:51:02 +02:00
context 'when procedure has sva or svr enabled' do
let ( :procedure ) { create ( :procedure , :published , :sva ) }
let ( :dossier ) { create ( :dossier , :en_construction , procedure : ) }
it { expect ( dossier . can_passer_automatiquement_en_instruction? ) . to be_truthy }
context 'when dossier was already processed by sva' do
let ( :dossier ) { create ( :dossier , :en_construction , procedure : , sva_svr_decision_triggered_at : 1 . hour . ago ) }
it { expect ( dossier . can_passer_automatiquement_en_instruction? ) . to be_falsey }
end
end
2022-12-14 18:15:57 +01:00
end
describe '#can_accepter_automatiquement?' do
2023-06-04 23:50:34 +02:00
let ( :dossier ) { create ( :dossier , state : initial_state , declarative_triggered_at : declarative_triggered_at ) }
let ( :initial_state ) { :en_construction }
2022-12-14 18:15:57 +01:00
let ( :declarative_triggered_at ) { nil }
it { expect ( dossier . can_accepter_automatiquement? ) . to be_falsey }
context 'when dossier is declarative' do
before { dossier . procedure . update ( declarative_with_state : :accepte ) }
context 'when dossier never transitioned' do
it { expect ( dossier . can_accepter_automatiquement? ) . to be_truthy }
end
context 'when dossier transitioned before' do
let ( :declarative_triggered_at ) { 1 . day . ago }
it { expect ( dossier . can_accepter_automatiquement? ) . to be_falsey }
end
end
2023-06-04 23:50:34 +02:00
context 'when procedure is sva/svr' do
let ( :decision ) { :sva }
let ( :initial_state ) { :en_instruction }
before do
dossier . procedure . update! ( sva_svr : SVASVRConfiguration . new ( decision : ) . attributes )
dossier . update! ( sva_svr_decision_on : Date . current )
end
it { expect ( dossier . can_accepter_automatiquement? ) . to be_truthy }
context 'when sva_svr_decision_on is in the future' do
before { dossier . update! ( sva_svr_decision_on : 1 . day . from_now ) }
it { expect ( dossier . can_accepter_automatiquement? ) . to be_falsey }
end
context 'when dossier has pending correction' do
let ( :dossier ) { create ( :dossier , :en_construction ) }
let! ( :dossier_correction ) { create ( :dossier_correction , dossier : ) }
it { expect ( dossier . can_accepter_automatiquement? ) . to be_falsey }
end
context 'when decision is svr' do
let ( :decision ) { :svr }
it { expect ( dossier . can_accepter_automatiquement? ) . to be_falsey }
end
context 'when dossier was already processed by sva' do
before { dossier . update! ( sva_svr_decision_triggered_at : 1 . hour . ago ) }
it { expect ( dossier . can_accepter_automatiquement? ) . to be_falsey }
end
end
2022-12-14 18:15:57 +01:00
end
2023-07-31 13:50:22 +02:00
describe '#can_refuser_automatiquement?' do
let ( :dossier ) { create ( :dossier , state : initial_state ) }
let ( :initial_state ) { :en_instruction }
it { expect ( dossier . can_refuser_automatiquement? ) . to be_falsey }
context 'when procedure is sva/svr' do
let ( :decision ) { :svr }
before do
dossier . procedure . update! ( sva_svr : SVASVRConfiguration . new ( decision : ) . attributes )
dossier . update! ( sva_svr_decision_on : Date . current )
end
it { expect ( dossier . can_refuser_automatiquement? ) . to be_truthy }
context 'when procedure is svr' do
let ( :decision ) { :svr }
before do
dossier . procedure . update! ( sva_svr : SVASVRConfiguration . new ( decision : ) . attributes )
dossier . update! ( sva_svr_decision_on : Date . current )
end
it { expect ( dossier . can_refuser_automatiquement? ) . to be_truthy }
context 'when sva_svr_decision_on is in the future' do
before { dossier . update! ( sva_svr_decision_on : 1 . day . from_now ) }
it { expect ( dossier . can_refuser_automatiquement? ) . to be_falsey }
end
context 'when dossier has pending correction' do
let ( :dossier ) { create ( :dossier , :en_construction ) }
let! ( :dossier_correction ) { create ( :dossier_correction , dossier : ) }
it { expect ( dossier . can_refuser_automatiquement? ) . to be_falsey }
end
context 'when decision is sva' do
let ( :decision ) { :sva }
it { expect ( dossier . can_refuser_automatiquement? ) . to be_falsey }
end
context 'when dossier was already processed by svr' do
before { dossier . update! ( sva_svr_decision_triggered_at : 1 . hour . ago ) }
it { expect ( dossier . can_refuser_automatiquement? ) . to be_falsey }
end
end
end
end
2022-09-21 15:05:11 +02:00
describe " can't transition to terminer when etablissement is in degraded mode " do
let ( :instructeur ) { create ( :instructeur ) }
let ( :motivation ) { 'motivation' }
context " when dossier is en_instruction " do
let ( :dossier_incomplete ) { create ( :dossier , :en_instruction , :with_entreprise , as_degraded_mode : true ) }
let ( :dossier_ok ) { create ( :dossier , :en_instruction , :with_entreprise , as_degraded_mode : false ) }
it " can't accepter " do
expect ( dossier_incomplete . may_accepter? ( instructeur : , motivation : ) ) . to be_falsey
expect ( dossier_ok . accepter ( instructeur : , motivation : ) ) . to be_truthy
end
it " can't refuser " do
expect ( dossier_incomplete . may_refuser? ( instructeur : , motivation : ) ) . to be_falsey
expect ( dossier_ok . may_refuser? ( instructeur : , motivation : ) ) . to be_truthy
end
it " can't classer_sans_suite " do
expect ( dossier_incomplete . may_classer_sans_suite? ( instructeur : , motivation : ) ) . to be_falsey
expect ( dossier_ok . may_classer_sans_suite? ( instructeur : , motivation : ) ) . to be_truthy
end
end
context " when dossier is en_construction " do
2022-12-14 18:15:57 +01:00
let ( :dossier_incomplete ) { create ( :dossier , :en_construction , :with_entreprise , :with_declarative_accepte , as_degraded_mode : true ) }
let ( :dossier_ok ) { create ( :dossier , :en_construction , :with_entreprise , :with_declarative_accepte , as_degraded_mode : false ) }
2022-09-21 15:05:11 +02:00
it " can't accepter_automatiquement " do
expect ( dossier_incomplete . may_accepter_automatiquement? ( instructeur : , motivation : ) ) . to be_falsey
expect ( dossier_ok . accepter_automatiquement ( instructeur : , motivation : ) ) . to be_truthy
end
end
2023-01-18 18:31:37 +01:00
context " when a SIRET champ has etablissement in degraded mode " do
let ( :dossier_incomplete ) { create ( :dossier , :en_instruction ) }
let ( :dossier_ok ) { create ( :dossier , :en_instruction ) }
before do
dossier_incomplete . champs_public << create ( :champ_siret , dossier : dossier_incomplete , etablissement : Etablissement . new ( siret : build ( :etablissement ) . siret ) )
dossier_ok . champs_public << create ( :champ_siret , dossier : dossier_ok )
end
it " can't accepter " do
expect ( dossier_incomplete . may_accepter? ( instructeur : , motivation : ) ) . to be_falsey
expect ( dossier_ok . may_accepter? ( instructeur : , motivation : ) ) . to be_truthy
end
end
2022-09-21 15:05:11 +02:00
end
2022-10-17 10:41:47 +02:00
describe " # check_mandatory_and_visible_champs " do
2022-08-04 14:34:13 +02:00
include Logic
let ( :procedure ) { create ( :procedure , types_de_champ_public : types_de_champ ) }
2019-07-09 15:57:48 +02:00
let ( :dossier ) { create ( :dossier , procedure : procedure ) }
2022-08-04 14:34:13 +02:00
let ( :types_de_champ ) { [ type_de_champ ] }
let ( :type_de_champ ) { { } }
2022-10-17 10:41:47 +02:00
let ( :errors ) { dossier . check_mandatory_and_visible_champs }
2019-01-30 16:14:15 +01:00
it 'no mandatory champs' do
2022-08-04 14:34:13 +02:00
expect ( errors ) . to be_empty
2019-01-30 16:14:15 +01:00
end
context " with mandatory champs " do
2022-08-04 14:34:13 +02:00
let ( :type_de_champ ) { { mandatory : true } }
2022-11-10 22:21:14 +01:00
let ( :champ_with_error ) { dossier . champs_public . first }
2019-01-30 16:14:15 +01:00
before do
champ_with_error . value = nil
champ_with_error . save
end
it 'should have errors' do
expect ( errors ) . not_to be_empty
2023-04-28 15:32:34 +02:00
expect ( errors . first . full_message ) . to eq ( " doit être rempli " )
2019-01-30 16:14:15 +01:00
end
2022-08-04 14:34:13 +02:00
context " conditionaly visible " do
let ( :types_de_champ ) { [ { type : :yes_no , stable_id : 99 } , type_de_champ ] }
let ( :type_de_champ ) { { mandatory : true , condition : ds_eq ( champ_value ( 99 ) , constant ( true ) ) } }
it 'should not have errors' do
expect ( errors ) . to be_empty
end
end
2019-01-30 16:14:15 +01:00
end
2019-03-28 11:28:27 +01:00
context " with mandatory SIRET champ " do
2022-08-04 14:34:13 +02:00
let ( :type_de_champ ) { { type : :siret , mandatory : true } }
2022-11-10 22:21:14 +01:00
let ( :champ_siret ) { dossier . champs_public . first }
2019-03-28 11:28:27 +01:00
before do
2022-08-04 14:34:13 +02:00
champ_siret . value = '44011762001530'
2019-03-28 11:28:27 +01:00
end
it 'should not have errors' do
expect ( errors ) . to be_empty
end
context " and invalid SIRET " do
before do
champ_siret . update ( value : " 1234 " )
dossier . reload
end
it 'should have errors' do
expect ( errors ) . not_to be_empty
2023-04-28 15:32:34 +02:00
expect ( errors . first . full_message ) . to eq ( " doit être rempli " )
2019-03-28 11:28:27 +01:00
end
end
end
2019-01-30 16:14:15 +01:00
context " with champ repetition " do
2022-08-04 14:34:13 +02:00
let ( :type_de_champ ) { { type : :repetition , mandatory : true , children : [ { mandatory : true } ] } }
2022-05-13 17:06:59 +02:00
let ( :revision ) { procedure . active_revision }
let ( :type_de_champ_repetition ) { revision . types_de_champ . first }
2019-01-30 16:14:15 +01:00
context " when no champs " do
2022-11-10 22:21:14 +01:00
let ( :champ_with_error ) { dossier . champs_public . first }
2019-01-30 16:14:15 +01:00
it 'should have errors' do
2022-11-10 22:21:14 +01:00
dossier . champs_public . first . champs . destroy_all
expect ( dossier . champs_public . first . rows ) . to be_empty
2019-01-30 16:14:15 +01:00
expect ( errors ) . not_to be_empty
2023-04-28 15:32:34 +02:00
expect ( errors . first . full_message ) . to eq ( " doit être rempli " )
2019-01-30 16:14:15 +01:00
end
end
context " when mandatory champ inside repetition " do
2022-11-10 22:21:14 +01:00
let ( :champ_with_error ) { dossier . champs_public . first . champs . first }
2019-01-30 16:14:15 +01:00
it 'should have errors' do
2022-11-10 22:21:14 +01:00
expect ( dossier . champs_public . first . rows ) . not_to be_empty
2023-04-28 15:32:34 +02:00
expect ( errors . first . full_message ) . to eq ( " doit être rempli " )
2019-01-30 16:14:15 +01:00
end
2022-08-04 14:34:13 +02:00
context " conditionaly visible " do
2022-11-10 22:21:14 +01:00
let ( :champ_with_error ) { dossier . champs_public . second . champs . first }
2022-08-04 14:34:13 +02:00
let ( :types_de_champ ) { [ { type : :yes_no , stable_id : 99 } , type_de_champ ] }
let ( :type_de_champ ) { { type : :repetition , mandatory : true , children : [ { mandatory : true } ] , condition : ds_eq ( champ_value ( 99 ) , constant ( true ) ) } }
it 'should not have errors' do
2022-11-10 22:21:14 +01:00
expect ( dossier . champs_public . second . rows ) . not_to be_empty
2022-08-04 14:34:13 +02:00
expect ( errors ) . to be_empty
end
it 'should have errors' do
2022-11-10 22:21:14 +01:00
dossier . champs_public . first . update ( value : 'true' )
expect ( dossier . champs_public . second . rows ) . not_to be_empty
2022-08-04 14:34:13 +02:00
expect ( errors ) . not_to be_empty
2023-04-28 15:32:34 +02:00
expect ( errors . first . full_message ) . to eq ( " doit être rempli " )
2022-08-04 14:34:13 +02:00
end
end
2019-01-30 16:14:15 +01:00
end
end
end
2019-02-13 16:13:37 +01:00
2023-09-29 23:09:41 +02:00
describe " # check_expressions_regulieres_champs " do
let ( :procedure ) { create ( :procedure , types_de_champ_public : types_de_champ ) }
let ( :dossier ) { create ( :dossier , procedure : procedure ) }
let ( :types_de_champ ) { [ type_de_champ ] }
2023-10-20 01:22:22 +02:00
let ( :type_de_champ ) { { type : :expression_reguliere , expression_reguliere : , expression_reguliere_exemple_text : , expression_reguliere_error_message : } }
2023-09-29 23:09:41 +02:00
context " with bad example " do
let ( :expression_reguliere_exemple_text ) { " 01234567 " }
let ( :expression_reguliere ) { " [A-Z]+ " }
2023-10-20 01:22:22 +02:00
let ( :expression_reguliere_error_message ) { " Le champ doit être composé de lettres majuscules " }
2023-09-29 23:09:41 +02:00
before do
champ = dossier . champs_public . first
champ . value = expression_reguliere_exemple_text
2023-10-26 16:02:23 +02:00
dossier . save ( context : :champs_public_value )
2023-09-29 23:09:41 +02:00
end
it 'should have errors' do
2023-10-13 14:27:03 +02:00
expect ( dossier . errors ) . not_to be_empty
2023-10-20 01:22:22 +02:00
expect ( dossier . errors . full_messages . join ( ',' ) ) . to include ( dossier . champs_public . first . expression_reguliere_error_message )
2023-09-29 23:09:41 +02:00
end
end
context " with good example " do
let ( :expression_reguliere_exemple_text ) { " AZERTY " }
let ( :expression_reguliere ) { " [A-Z]+ " }
2023-10-20 01:22:22 +02:00
let ( :expression_reguliere_error_message ) { " Le champ doit être composé de lettres majuscules " }
2023-09-29 23:09:41 +02:00
before do
champ = dossier . champs_public . first
champ . value = expression_reguliere_exemple_text
2023-10-13 14:27:03 +02:00
dossier . save
2023-09-29 23:09:41 +02:00
end
it 'should not have errors' do
2023-10-13 14:27:03 +02:00
expect ( dossier . errors ) . to be_empty
2023-09-29 23:09:41 +02:00
end
end
end
2023-03-06 16:23:14 +01:00
describe 'index_for_section_header' do
let ( :procedure ) { create ( :procedure , types_de_champ_public : types_de_champ ) }
let ( :dossier ) { create ( :dossier , procedure : procedure ) }
let ( :types_de_champ ) { [ { type : :repetition , mandatory : true , children : [ { type : :header_section } ] } ] }
it 'index classly' do
repetition = dossier . champs . find ( & :repetition? )
header_in_repetition = repetition . champs . find ( & :header_section? )
expect ( dossier . index_for_section_header ( header_in_repetition ) ) . to eq ( " 1.1 " )
end
end
2019-07-01 17:45:03 +02:00
describe '#repasser_en_instruction!' do
2023-09-04 15:14:50 +02:00
let ( :dossier ) { create ( :dossier , :refuse , :with_attestation , :with_justificatif , archived : true , termine_close_to_expiration_notice_sent_at : Time . zone . now , sva_svr_decision_on : 1 . day . ago ) }
2019-08-06 11:02:54 +02:00
let! ( :instructeur ) { create ( :instructeur ) }
2019-07-01 17:45:03 +02:00
let ( :last_operation ) { dossier . dossier_operation_logs . last }
before do
Timecop . freeze
2023-11-09 10:53:37 +01:00
allow ( NotificationMailer ) . to receive ( :send_repasser_en_instruction_notification ) . and_return ( double ( deliver_later : true ) )
2021-11-26 12:19:40 +01:00
dossier . repasser_en_instruction! ( instructeur : instructeur )
2019-07-01 17:45:03 +02:00
dossier . reload
end
it { expect ( dossier . state ) . to eq ( 'en_instruction' ) }
2020-05-12 19:01:25 +02:00
it { expect ( dossier . archived ) . to be_falsey }
2019-07-01 17:45:03 +02:00
it { expect ( dossier . motivation ) . to be_nil }
2023-07-19 10:57:55 +02:00
it { expect ( dossier . justificatif_motivation . attached? ) . to be_falsey }
2019-07-01 17:45:03 +02:00
it { expect ( dossier . attestation ) . to be_nil }
2023-09-04 15:14:50 +02:00
it { expect ( dossier . sva_svr_decision_on ) . to be_nil }
2021-11-04 19:05:04 +01:00
it { expect ( dossier . termine_close_to_expiration_notice_sent_at ) . to be_nil }
2019-07-01 17:45:03 +02:00
it { expect ( last_operation . operation ) . to eq ( 'repasser_en_instruction' ) }
2022-11-18 11:31:23 +01:00
it { expect ( last_operation . data [ 'author' ] [ 'email' ] ) . to eq ( instructeur . email ) }
2023-11-09 10:53:37 +01:00
it { expect ( NotificationMailer ) . to have_received ( :send_repasser_en_instruction_notification ) . with ( dossier ) }
2019-07-01 17:45:03 +02:00
after { Timecop . return }
end
2019-07-01 15:55:37 +02:00
2020-02-05 22:10:22 +01:00
describe '#notify_draft_not_submitted' do
let! ( :user1 ) { create ( :user ) }
let! ( :user2 ) { create ( :user ) }
2020-04-29 19:18:12 +02:00
let! ( :procedure_near_closing ) { create ( :procedure , :published , auto_archive_on : Time . zone . today + Dossier :: REMAINING_DAYS_BEFORE_CLOSING . days ) }
let! ( :procedure_closed_later ) { create ( :procedure , :published , auto_archive_on : Time . zone . today + Dossier :: REMAINING_DAYS_BEFORE_CLOSING . days + 1 . day ) }
let! ( :procedure_closed_before ) { create ( :procedure , :published , auto_archive_on : Time . zone . today + Dossier :: REMAINING_DAYS_BEFORE_CLOSING . days - 1 . day ) }
2020-02-05 22:10:22 +01:00
# user 1 has three draft dossiers where one is for procedure that closes in two days ==> should trigger one mail
2020-03-25 04:09:14 +01:00
let! ( :draft_near_closing ) { create ( :dossier , user : user1 , procedure : procedure_near_closing ) }
2020-02-05 22:10:22 +01:00
let! ( :draft_before ) { create ( :dossier , user : user1 , procedure : procedure_closed_before ) }
let! ( :draft_later ) { create ( :dossier , user : user1 , procedure : procedure_closed_later ) }
# user 2 submitted a draft and en_construction dossier for the same procedure ==> should not trigger the mail
2020-03-25 04:09:14 +01:00
let! ( :draft_near_closing_2 ) { create ( :dossier , :en_construction , user : user2 , procedure : procedure_near_closing ) }
let! ( :submitted_near_closing_2 ) { create ( :dossier , user : user2 , procedure : procedure_near_closing ) }
2020-02-05 22:10:22 +01:00
before do
2020-03-19 03:46:12 +01:00
allow ( DossierMailer ) . to receive ( :notify_brouillon_not_submitted ) . and_return ( double ( deliver_later : nil ) )
2020-02-05 22:10:22 +01:00
Dossier . notify_draft_not_submitted
end
it 'notifies draft is not submitted' do
2020-03-19 03:46:12 +01:00
expect ( DossierMailer ) . to have_received ( :notify_brouillon_not_submitted ) . once
expect ( DossierMailer ) . to have_received ( :notify_brouillon_not_submitted ) . with ( draft_near_closing )
2020-02-05 22:10:22 +01:00
end
end
2020-01-14 19:00:17 +01:00
describe '#geo_position' do
let ( :lat ) { " 46.538192 " }
let ( :lon ) { " 2.428462 " }
let ( :zoom ) { " 13 " }
let ( :etablissement_geo_adresse_lat ) { " 40.7143528 " }
let ( :etablissement_geo_adresse_lon ) { " -74.0059731 " }
let ( :result ) { { lat : lat , lon : lon , zoom : zoom } }
let ( :dossier ) { create ( :dossier ) }
it 'should geolocate' do
expect ( dossier . geo_position ) . to eq ( result )
end
context 'with etablissement' do
before do
Geocoder :: Lookup :: Test . add_stub (
dossier . etablissement . geo_adresse , [
{
'coordinates' = > [ etablissement_geo_adresse_lat . to_f , etablissement_geo_adresse_lon . to_f ]
}
]
)
end
let ( :dossier ) { create ( :dossier , :with_entreprise ) }
let ( :result ) { { lat : etablissement_geo_adresse_lat , lon : etablissement_geo_adresse_lon , zoom : zoom } }
it 'should geolocate' do
expect ( dossier . geo_position ) . to eq ( result )
end
end
end
2020-03-19 18:33:38 +01:00
2023-01-31 16:39:00 +01:00
describe '#geo_data' do
let ( :dossier ) { create ( :dossier ) }
let ( :type_de_champ_carte ) { create ( :type_de_champ_carte , procedure : dossier . procedure ) }
let ( :geo_area ) { create ( :geo_area ) }
let ( :champ_carte ) { create ( :champ_carte , type_de_champ : type_de_champ_carte , geo_areas : [ geo_area ] ) }
context " without data " do
it { expect ( dossier . geo_data? ) . to be_falsey }
end
context " with geo data in public champ " do
before do
dossier . champs_public << champ_carte
end
it { expect ( dossier . geo_data? ) . to be_truthy }
end
context " with geo data in private champ " do
before do
dossier . champs_private << champ_carte
end
it { expect ( dossier . geo_data? ) . to be_truthy }
end
it " should solve N+1 problem " do
dossier . champs_public << create_list ( :champ_carte , 3 , type_de_champ : type_de_champ_carte , geo_areas : [ create ( :geo_area ) ] )
count = 0
callback = lambda { | * _args | count += 1 }
ActiveSupport :: Notifications . subscribed ( callback , " sql.active_record " ) do
dossier . geo_data?
end
expect ( count ) . to eq ( 1 )
end
end
2020-03-19 18:33:38 +01:00
describe 'dossier_operation_log after dossier deletion' do
let ( :dossier ) { create ( :dossier ) }
let ( :dossier_operation_log ) { create ( :dossier_operation_log , dossier : dossier ) }
it 'should nullify dossier link' do
expect ( dossier_operation_log . dossier ) . to eq ( dossier )
expect ( DossierOperationLog . count ) . to eq ( 1 )
dossier . destroy
expect ( dossier_operation_log . reload . dossier ) . to be_nil
expect ( DossierOperationLog . count ) . to eq ( 1 )
end
end
2020-03-19 13:11:45 +01:00
2022-03-09 10:29:16 +01:00
describe 'brouillon_expired and en_construction_expired' do
2022-01-12 11:31:05 +01:00
let ( :administrateur ) { create ( :administrateur ) }
let ( :user ) { administrateur . user }
let ( :reason ) { DeletedDossier . reasons . fetch ( :user_request ) }
2020-03-26 09:08:52 +01:00
2020-03-19 13:11:45 +01:00
before do
2022-01-12 11:31:05 +01:00
create ( :dossier , user : user )
create ( :dossier , :en_construction , user : user )
2022-03-09 10:29:16 +01:00
create ( :dossier , user : user ) . hide_and_keep_track! ( user , reason )
create ( :dossier , :en_construction , user : user ) . hide_and_keep_track! ( user , reason )
2020-03-19 13:11:45 +01:00
Timecop . travel ( 2 . months . ago ) do
2022-03-09 10:29:16 +01:00
create ( :dossier , user : user ) . hide_and_keep_track! ( user , reason )
create ( :dossier , :en_construction , user : user ) . hide_and_keep_track! ( user , reason )
2020-03-19 13:11:45 +01:00
2022-01-12 11:31:05 +01:00
create ( :dossier , user : user ) . procedure . discard_and_keep_track! ( administrateur )
create ( :dossier , :en_construction , user : user ) . procedure . discard_and_keep_track! ( administrateur )
2020-03-19 13:11:45 +01:00
end
2022-03-09 10:29:16 +01:00
2020-03-19 13:11:45 +01:00
Timecop . travel ( 1 . week . ago ) do
2022-03-09 10:29:16 +01:00
create ( :dossier , user : user ) . hide_and_keep_track! ( user , reason )
create ( :dossier , :en_construction , user : user ) . hide_and_keep_track! ( user , reason )
2020-03-19 13:11:45 +01:00
end
end
2022-03-09 10:29:16 +01:00
it { expect ( Dossier . en_brouillon_expired_to_delete . count ) . to eq ( 2 ) }
it { expect ( Dossier . en_construction_expired_to_delete . count ) . to eq ( 2 ) }
2020-03-19 13:11:45 +01:00
end
2020-03-31 14:51:07 +02:00
describe " discarded procedure dossier should be able to access it's procedure " do
let ( :dossier ) { create ( :dossier ) }
let ( :procedure ) { dossier . reload . procedure }
before { dossier . procedure . discard! }
it { expect ( procedure ) . not_to be_nil }
it { expect ( procedure . discarded? ) . to be_truthy }
end
2020-04-30 15:49:43 +02:00
describe " to_feature_collection " do
2020-08-27 19:56:19 +02:00
let ( :dossier ) { create ( :dossier ) }
let ( :type_de_champ_carte ) { create ( :type_de_champ_carte , procedure : dossier . procedure ) }
2020-04-30 15:49:43 +02:00
let ( :geo_area ) { create ( :geo_area , :selection_utilisateur , :polygon ) }
2020-08-27 19:56:19 +02:00
let ( :champ_carte ) { create ( :champ_carte , type_de_champ : type_de_champ_carte , geo_areas : [ geo_area ] ) }
before do
2022-11-10 22:21:14 +01:00
dossier . champs_public << champ_carte
2020-08-27 19:56:19 +02:00
end
2020-04-30 15:49:43 +02:00
it 'should have all champs carto' do
expect ( dossier . to_feature_collection ) . to eq ( {
type : 'FeatureCollection' ,
id : dossier . id ,
bbox : [ 2 . 428439855575562 , 46 . 538491597754714 , 2 . 42824137210846 , 46 . 53841410755813 ] ,
features : [
{
type : 'Feature' ,
geometry : {
2023-02-15 17:39:19 +01:00
coordinates : [ [ [ 2 . 428439855575562 , 46 . 538476837725796 ] , [ 2 . 4284291267395024 , 46 . 53842148758162 ] , [ 2 . 4282521009445195 , 46 . 53841410755813 ] , [ 2 . 42824137210846 , 46 . 53847314771794 ] , [ 2 . 428284287452698 , 46 . 53847314771794 ] , [ 2 . 428364753723145 , 46 . 538487907747864 ] , [ 2 . 4284291267395024 , 46 . 538491597754714 ] , [ 2 . 428439855575562 , 46 . 538476837725796 ] ] ] ,
type : 'Polygon'
2020-04-30 15:49:43 +02:00
} ,
properties : {
2020-10-01 11:32:56 +02:00
area : 103 . 6 ,
2022-11-16 11:50:19 +01:00
champ_label : champ_carte . libelle ,
2020-08-27 19:56:19 +02:00
champ_id : champ_carte . stable_id ,
2022-11-16 11:50:19 +01:00
champ_private : false ,
2020-04-30 15:49:43 +02:00
dossier_id : dossier . id ,
id : geo_area . id ,
source : 'selection_utilisateur'
}
}
]
} )
end
end
2020-04-29 19:18:12 +02:00
describe " with_notifiable_procedure " do
let ( :test_procedure ) { create ( :procedure ) }
let ( :published_procedure ) { create ( :procedure , :published ) }
let ( :closed_procedure ) { create ( :procedure , :closed ) }
let ( :unpublished_procedure ) { create ( :procedure , :unpublished ) }
let! ( :dossier_on_test_procedure ) { create ( :dossier , procedure : test_procedure ) }
let! ( :dossier_on_published_procedure ) { create ( :dossier , procedure : published_procedure ) }
let! ( :dossier_on_closed_procedure ) { create ( :dossier , procedure : closed_procedure ) }
let! ( :dossier_on_unpublished_procedure ) { create ( :dossier , procedure : unpublished_procedure ) }
let ( :notify_on_closed ) { false }
let ( :dossiers ) { Dossier . with_notifiable_procedure ( notify_on_closed : notify_on_closed ) }
it 'should find dossiers with notifiable procedure' do
expect ( dossiers ) . to match_array ( [ dossier_on_published_procedure , dossier_on_unpublished_procedure ] )
end
context 'when notify on closed is true' do
let ( :notify_on_closed ) { true }
it 'should find dossiers with notifiable procedure' do
expect ( dossiers ) . to match_array ( [ dossier_on_published_procedure , dossier_on_closed_procedure , dossier_on_unpublished_procedure ] )
end
end
end
2020-09-08 15:53:07 +02:00
2023-04-13 13:10:23 +02:00
describe " champs_for_export " do
2022-10-13 18:08:52 +02:00
context 'with a unconditionnal procedure' do
let ( :procedure ) { create ( :procedure , :with_type_de_champ , :with_datetime , :with_yes_no , :with_explication , :with_commune , :with_repetition , zones : [ create ( :zone ) ] ) }
2022-11-16 12:46:33 +01:00
let ( :text_type_de_champ ) { procedure . active_revision . types_de_champ_public . find { | type_de_champ | type_de_champ . type_champ == TypeDeChamp . type_champs . fetch ( :text ) } }
let ( :yes_no_type_de_champ ) { procedure . active_revision . types_de_champ_public . find { | type_de_champ | type_de_champ . type_champ == TypeDeChamp . type_champs . fetch ( :yes_no ) } }
let ( :datetime_type_de_champ ) { procedure . active_revision . types_de_champ_public . find { | type_de_champ | type_de_champ . type_champ == TypeDeChamp . type_champs . fetch ( :datetime ) } }
let ( :explication_type_de_champ ) { procedure . active_revision . types_de_champ_public . find { | type_de_champ | type_de_champ . type_champ == TypeDeChamp . type_champs . fetch ( :explication ) } }
let ( :commune_type_de_champ ) { procedure . active_revision . types_de_champ_public . find { | type_de_champ | type_de_champ . type_champ == TypeDeChamp . type_champs . fetch ( :communes ) } }
let ( :repetition_type_de_champ ) { procedure . active_revision . types_de_champ_public . find { | type_de_champ | type_de_champ . type_champ == TypeDeChamp . type_champs . fetch ( :repetition ) } }
2022-11-10 22:21:14 +01:00
let ( :repetition_champ ) { dossier . champs_public . find ( & :repetition? ) }
let ( :repetition_second_revision_champ ) { dossier_second_revision . champs_public . find ( & :repetition? ) }
2022-10-13 18:08:52 +02:00
let ( :dossier ) { create ( :dossier , procedure : procedure ) }
let ( :dossier_second_revision ) { create ( :dossier , procedure : procedure ) }
2022-11-10 22:21:14 +01:00
let ( :dossier_champs_for_export ) { Dossier . champs_for_export ( dossier . champs_public , procedure . types_de_champ_for_procedure_presentation . not_repetition ) }
let ( :dossier_second_revision_champs_for_export ) { Dossier . champs_for_export ( dossier_second_revision . champs_public , procedure . types_de_champ_for_procedure_presentation . not_repetition ) }
2022-10-13 18:08:52 +02:00
let ( :repetition_second_revision_champs_for_export ) { Dossier . champs_for_export ( repetition_second_revision_champ . champs , procedure . types_de_champ_for_procedure_presentation . repetition ) }
context " when procedure published " do
before do
procedure . publish!
dossier
procedure . draft_revision . remove_type_de_champ ( text_type_de_champ . stable_id )
procedure . draft_revision . add_type_de_champ ( type_champ : TypeDeChamp . type_champs . fetch ( :text ) , libelle : 'New text field' )
procedure . draft_revision . find_and_ensure_exclusive_use ( yes_no_type_de_champ . stable_id ) . update ( libelle : 'Updated yes/no' )
procedure . draft_revision . find_and_ensure_exclusive_use ( commune_type_de_champ . stable_id ) . update ( libelle : 'Commune de naissance' )
procedure . draft_revision . find_and_ensure_exclusive_use ( repetition_type_de_champ . stable_id ) . update ( libelle : 'Repetition' )
procedure . update ( published_revision : procedure . draft_revision , draft_revision : procedure . create_new_revision )
dossier . reload
procedure . reload
end
2020-09-08 15:53:07 +02:00
2022-10-13 18:08:52 +02:00
it " should have champs from all revisions " do
expect ( dossier . types_de_champ . map ( & :libelle ) ) . to eq ( [ text_type_de_champ . libelle , datetime_type_de_champ . libelle , " Yes/no " , explication_type_de_champ . libelle , commune_type_de_champ . libelle , repetition_type_de_champ . libelle ] )
expect ( dossier_second_revision . types_de_champ . map ( & :libelle ) ) . to eq ( [ datetime_type_de_champ . libelle , " Updated yes/no " , explication_type_de_champ . libelle , 'Commune de naissance' , " Repetition " , " New text field " ] )
expect ( dossier_champs_for_export . map { | ( libelle ) | libelle } ) . to eq ( [ datetime_type_de_champ . libelle , text_type_de_champ . libelle , " Updated yes/no " , " Commune de naissance " , " Commune de naissance (Code insee) " , " Commune de naissance (Département) " , " New text field " ] )
expect ( dossier_champs_for_export ) . to eq ( dossier_second_revision_champs_for_export )
expect ( repetition_second_revision_champs_for_export . map { | ( libelle ) | libelle } ) . to eq ( procedure . types_de_champ_for_procedure_presentation . repetition . map ( & :libelle_for_export ) )
expect ( repetition_second_revision_champs_for_export . first . size ) . to eq ( 2 )
end
2021-03-23 09:55:37 +01:00
2022-10-13 18:08:52 +02:00
context 'within a repetition having a type de champs commune (multiple values for export)' do
it 'works' do
proc_test = create ( :procedure )
2022-05-05 17:53:24 +02:00
2022-10-13 18:08:52 +02:00
draft = proc_test . draft_revision
2022-05-18 10:50:33 +02:00
2022-10-13 18:08:52 +02:00
tdc_repetition = draft . add_type_de_champ ( type_champ : :repetition , libelle : " repetition " )
draft . add_type_de_champ ( type_champ : :communes , libelle : " communes " , parent_stable_id : tdc_repetition . stable_id )
dossier_test = create ( :dossier , procedure : proc_test )
repetition = proc_test . types_de_champ_for_procedure_presentation . repetition . first
type_champs = proc_test . types_de_champ_for_procedure_presentation ( repetition ) . to_a
expect ( type_champs . size ) . to eq ( 1 )
2022-11-10 22:21:14 +01:00
expect ( Dossier . champs_for_export ( dossier . champs_public , type_champs ) . size ) . to eq ( 3 )
2022-10-13 18:08:52 +02:00
end
end
end
2022-05-18 10:50:33 +02:00
2022-10-13 18:08:52 +02:00
context " when procedure brouillon " do
let ( :procedure ) { create ( :procedure , :with_type_de_champ , :with_explication ) }
2022-05-18 10:50:33 +02:00
2022-10-13 18:08:52 +02:00
it " should not contain non-exportable types de champ " do
expect ( dossier_champs_for_export . map { | ( libelle ) | libelle } ) . to eq ( [ text_type_de_champ . libelle ] )
2022-05-05 17:53:24 +02:00
end
end
2020-09-08 15:53:07 +02:00
end
2022-10-13 18:08:52 +02:00
context 'with a procedure with a condition' do
include Logic
let ( :types_de_champ ) { [ { type : :yes_no } , { type : :text } ] }
let ( :procedure ) { create ( :procedure , types_de_champ_public : types_de_champ ) }
let ( :dossier ) { create ( :dossier , procedure : ) }
2022-11-16 12:46:33 +01:00
let ( :yes_no_tdc ) { procedure . active_revision . types_de_champ_public . first }
let ( :text_tdc ) { procedure . active_revision . types_de_champ_public . second }
2022-11-10 22:21:14 +01:00
let ( :tdcs ) { dossier . champs_public . map ( & :type_de_champ ) }
2022-10-13 18:08:52 +02:00
2022-11-10 22:21:14 +01:00
subject { Dossier . champs_for_export ( dossier . champs_public , tdcs ) }
2022-10-13 18:08:52 +02:00
before do
text_tdc . update ( condition : ds_eq ( champ_value ( yes_no_tdc . stable_id ) , constant ( true ) ) )
2022-11-10 22:21:14 +01:00
yes_no , text = dossier . champs_public
2022-10-13 18:08:52 +02:00
yes_no . update ( value : yes_no_value )
text . update ( value : 'text' )
end
context 'with a champ visible' do
let ( :yes_no_value ) { 'true' }
it { is_expected . to eq ( [ [ yes_no_tdc . libelle , " Oui " ] , [ text_tdc . libelle , " text " ] ] ) }
end
context 'with a champ invisible' do
let ( :yes_no_value ) { 'false' }
2021-03-23 09:55:37 +01:00
2022-10-13 18:08:52 +02:00
it { is_expected . to eq ( [ [ yes_no_tdc . libelle , " Non " ] , [ text_tdc . libelle , nil ] ] ) }
2021-03-23 09:55:37 +01:00
end
2022-10-18 11:00:44 +02:00
context 'with another revision' do
let ( :tdc_from_another_revision ) { create ( :type_de_champ_communes , libelle : 'commune' , condition : ds_eq ( constant ( true ) , constant ( true ) ) ) }
2022-11-10 22:21:14 +01:00
let ( :tdcs ) { dossier . champs_public . map ( & :type_de_champ ) << tdc_from_another_revision }
2022-10-18 11:00:44 +02:00
let ( :yes_no_value ) { 'true' }
let ( :expected ) do
[
[ yes_no_tdc . libelle , " Oui " ] ,
[ text_tdc . libelle , " text " ] ,
2023-04-04 14:54:29 +02:00
[ " commune " , '' ] ,
[ " commune (Code insee) " , '' ] ,
2022-10-18 11:00:44 +02:00
[ " commune (Département) " , " " ]
]
end
it { is_expected . to eq ( expected ) }
end
2020-09-08 15:53:07 +02:00
end
end
2020-12-08 15:57:48 +01:00
describe " remove_titres_identite! " do
2021-01-28 13:53:18 +01:00
let ( :dossier ) { create ( :dossier , :en_instruction , :followed , :with_individual ) }
2020-12-08 15:57:48 +01:00
let ( :type_de_champ_titre_identite ) { create ( :type_de_champ_titre_identite , procedure : dossier . procedure ) }
let ( :champ_titre_identite ) { create ( :champ_titre_identite , type_de_champ : type_de_champ_titre_identite ) }
let ( :type_de_champ_titre_identite_vide ) { create ( :type_de_champ_titre_identite , procedure : dossier . procedure ) }
let ( :champ_titre_identite_vide ) { create ( :champ_titre_identite , type_de_champ : type_de_champ_titre_identite_vide ) }
before do
champ_titre_identite_vide . piece_justificative_file . purge
2022-11-10 22:21:14 +01:00
dossier . champs_public << champ_titre_identite
dossier . champs_public << champ_titre_identite_vide
2020-12-08 15:57:48 +01:00
end
it " clean up titres identite on accepter " do
expect ( champ_titre_identite . piece_justificative_file . attached? ) . to be_truthy
expect ( champ_titre_identite_vide . piece_justificative_file . attached? ) . to be_falsey
2021-11-26 12:19:40 +01:00
dossier . accepter! ( instructeur : dossier . followers_instructeurs . first , motivation : " yolo! " )
2020-12-08 15:57:48 +01:00
expect ( champ_titre_identite . piece_justificative_file . attached? ) . to be_falsey
end
it " clean up titres identite on refuser " do
expect ( champ_titre_identite . piece_justificative_file . attached? ) . to be_truthy
expect ( champ_titre_identite_vide . piece_justificative_file . attached? ) . to be_falsey
2021-11-26 12:19:40 +01:00
dossier . refuser! ( instructeur : dossier . followers_instructeurs . first , motivation : " yolo! " )
2020-12-08 15:57:48 +01:00
expect ( champ_titre_identite . piece_justificative_file . attached? ) . to be_falsey
end
it " clean up titres identite on classer_sans_suite " do
expect ( champ_titre_identite . piece_justificative_file . attached? ) . to be_truthy
expect ( champ_titre_identite_vide . piece_justificative_file . attached? ) . to be_falsey
2021-11-26 12:19:40 +01:00
dossier . classer_sans_suite! ( instructeur : dossier . followers_instructeurs . first , motivation : " yolo! " )
2020-12-08 15:57:48 +01:00
expect ( champ_titre_identite . piece_justificative_file . attached? ) . to be_falsey
end
context 'en_construction' do
2022-12-14 18:15:57 +01:00
let ( :dossier ) { create ( :dossier , :en_construction , :followed , :with_individual , :with_declarative_accepte ) }
2020-12-08 15:57:48 +01:00
it " clean up titres identite on accepter_automatiquement " do
expect ( champ_titre_identite . piece_justificative_file . attached? ) . to be_truthy
expect ( champ_titre_identite_vide . piece_justificative_file . attached? ) . to be_falsey
2021-03-11 14:42:57 +01:00
dossier . accepter_automatiquement!
2020-12-08 15:57:48 +01:00
expect ( champ_titre_identite . piece_justificative_file . attached? ) . to be_falsey
end
end
end
2021-02-04 19:24:52 +01:00
describe '#log_api_entreprise_job_exception' do
let ( :dossier ) { create ( :dossier ) }
context " add execption to the log " do
before do
dossier . log_api_entreprise_job_exception ( StandardError . new ( 'My special exception!' ) )
end
it { expect ( dossier . api_entreprise_job_exceptions ) . to eq ( [ '#<StandardError: My special exception!>' ] ) }
end
end
2021-04-13 20:24:02 +02:00
2023-04-13 13:10:23 +02:00
describe " # destroy " do
2022-07-06 10:22:14 +02:00
let ( :procedure ) { create ( :procedure , :with_all_champs , :with_all_annotations ) }
2021-10-06 13:16:25 +02:00
let ( :transfer ) { create ( :dossier_transfer ) }
2022-07-06 10:22:14 +02:00
let ( :dossier ) { create ( :dossier , :with_populated_champs , :with_populated_annotations , transfer : transfer , procedure : procedure ) }
2021-10-06 13:16:25 +02:00
2021-04-13 20:24:02 +02:00
before do
2021-10-06 13:16:25 +02:00
create ( :dossier , transfer : transfer )
2021-04-13 20:24:02 +02:00
create ( :attestation , dossier : dossier )
end
2022-04-05 12:13:25 +02:00
it " can destroy dossier " do
2021-04-13 20:24:02 +02:00
expect ( dossier . destroy ) . to be_truthy
2022-07-06 10:22:14 +02:00
expect { dossier . reload } . to raise_error ( ActiveRecord :: RecordNotFound )
end
it " can reset demarche " do
expect { dossier . procedure . reset! } . not_to raise_error
expect { dossier . reload } . to raise_error ( ActiveRecord :: RecordNotFound )
2021-04-13 20:24:02 +02:00
end
2023-06-21 16:42:41 +02:00
it " call logger with context " do
json_message = nil
allow ( Rails . logger ) . to receive ( :info ) { json_message || = _1 }
dossier . destroy
expect ( JSON . parse ( json_message ) ) . to a_hash_including (
{ message : " Dossier destroyed " , dossier_id : dossier . id , procedure_id : procedure . id } . stringify_keys
)
end
2021-04-13 20:24:02 +02:00
end
2021-05-10 11:00:57 +02:00
describe " # spreadsheet_columns " do
let ( :dossier ) { create ( :dossier ) }
2021-06-22 10:17:10 +02:00
it { expect ( dossier . spreadsheet_columns ( types_de_champ : [ ] ) ) . to include ( [ " État du dossier " , " Brouillon " ] ) }
2023-06-22 12:06:38 +02:00
context 'procedure sva' do
let ( :dossier ) { create ( :dossier , :en_instruction , procedure : create ( :procedure , :sva ) ) }
2023-08-31 15:25:29 +02:00
it { expect ( dossier . spreadsheet_columns ( types_de_champ : [ ] ) ) . to include ( [ " Date décision SVA " , :sva_svr_decision_on ] ) }
2023-06-22 12:06:38 +02:00
end
2023-07-31 13:50:22 +02:00
context 'procedure svr' do
let ( :dossier ) { create ( :dossier , :en_instruction , procedure : create ( :procedure , :svr ) ) }
2023-08-31 15:25:29 +02:00
it { expect ( dossier . spreadsheet_columns ( types_de_champ : [ ] ) ) . to include ( [ " Date décision SVR " , :sva_svr_decision_on ] ) }
2023-07-31 13:50:22 +02:00
end
2022-02-11 18:23:46 +01:00
end
2022-04-01 18:20:12 +02:00
describe '#processed_in_month' do
let ( :dossier_accepte_at ) { DateTime . new ( 2022 , 3 , 31 , 12 , 0 ) }
before do
travel_to ( dossier_accepte_at ) do
dossier = create ( :dossier , :accepte )
end
end
context 'given a date' do
let ( :archive_date ) { Date . new ( 2022 , 3 , 1 ) }
it 'includes a dossier processed_at at last day of month' do
expect ( Dossier . processed_in_month ( archive_date ) . count ) . to eq ( 1 )
end
end
context 'given a datetime' do
let ( :archive_date ) { DateTime . new ( 2022 , 3 , 1 , 12 , 0 ) }
it 'includes a dossier processed_at at last day of month' do
expect ( Dossier . processed_in_month ( archive_date ) . count ) . to eq ( 1 )
end
end
end
2022-10-31 17:02:48 +01:00
describe '#processed_by_month' do
let ( :procedure ) { create ( :procedure , :published , groupe_instructeurs : [ groupe_instructeurs ] ) }
let ( :groupe_instructeurs ) { create ( :groupe_instructeur ) }
before do
create_dossier_for_month ( procedure , 2021 , 3 )
create_dossier_for_month ( procedure , 2021 , 3 )
create_archived_dossier_for_month ( procedure , 2021 , 3 )
create_dossier_for_month ( procedure , 2021 , 2 )
end
subject do
Timecop . freeze ( Time . zone . local ( 2021 , 3 , 5 ) ) do
Dossier . processed_by_month ( groupe_instructeurs ) . count
end
end
it 'count dossiers_termines by month' do
expect ( count_for_month ( subject , 3 ) ) . to eq 3
expect ( count_for_month ( subject , 2 ) ) . to eq 1
end
it 'returns descending order by month' do
expect ( subject . keys . first . month ) . to eq 3
expect ( subject . keys . last . month ) . to eq 2
end
end
2022-12-01 11:17:48 +01:00
describe '#find_champs_by_stable_ids' do
2022-11-28 15:42:39 +01:00
let ( :procedure ) { create ( :procedure , :published ) }
let ( :dossier ) { create ( :dossier , :brouillon , procedure : procedure ) }
2022-12-01 11:17:48 +01:00
subject { dossier . find_champs_by_stable_ids ( stable_ids ) }
2022-11-28 15:42:39 +01:00
2022-12-01 11:17:48 +01:00
context 'when stable_ids is empty' do
let ( :stable_ids ) { [ ] }
2022-11-28 15:42:39 +01:00
2022-12-01 11:17:48 +01:00
it { expect ( subject ) . to match ( [ ] ) }
2022-11-28 15:42:39 +01:00
end
2022-12-01 11:17:48 +01:00
context 'when stable_ids contains nil or blank values' do
let ( :stable_ids ) { [ nil , " " ] }
2022-11-28 15:42:39 +01:00
2022-12-01 11:17:48 +01:00
it { expect ( subject ) . to match ( [ ] ) }
2022-11-28 15:42:39 +01:00
end
2022-12-01 11:17:48 +01:00
context 'when stable_ids contains present values' do
context 'when the dossier has no champ with the given stable ids' do
let ( :stable_ids ) { [ 'My Neighbor Totoro' , 'Miyazaki' ] }
2022-11-28 15:42:39 +01:00
2022-12-01 11:17:48 +01:00
it { expect ( subject ) . to match ( [ ] ) }
2022-11-28 15:42:39 +01:00
end
2022-12-01 11:17:48 +01:00
context 'when the dossier has champs with the given stable ids' do
let! ( :type_de_champ_1 ) { create ( :type_de_champ_text , procedure : procedure ) }
let! ( :type_de_champ_2 ) { create ( :type_de_champ_textarea , procedure : procedure ) }
let ( :stable_ids ) { [ type_de_champ_1 . stable_id , type_de_champ_2 . stable_id ] }
2022-11-28 15:42:39 +01:00
2023-12-12 12:07:26 +01:00
it { expect ( subject ) . to match_array ( dossier . champs_public . joins ( :type_de_champ ) . where ( types_de_champ : { stable_id : stable_ids } ) ) }
2022-11-28 15:42:39 +01:00
end
end
end
2022-11-18 15:26:31 +01:00
describe 'BatchOperation' do
subject { build ( :dossier ) }
it { is_expected . to belong_to ( :batch_operation ) . optional }
end
2023-01-03 14:46:10 +01:00
describe '#orphan?' do
subject ( :orphan ) { dossier . orphan? }
context 'when the dossier is prefilled' do
context 'when the dossier has a user' do
let ( :dossier ) { build ( :dossier , :prefilled ) }
it { expect ( orphan ) . to be_falsey }
end
context 'when the dossier does not have a user' do
let ( :dossier ) { build ( :dossier , :prefilled , user : nil ) }
it { expect ( orphan ) . to be_truthy }
end
end
context 'when the dossier is not prefilled' do
context 'when the dossier has a user' do
let ( :dossier ) { build ( :dossier ) }
it { expect ( orphan ) . to be_falsey }
end
context 'when the dossier does not have a user' do
let ( :dossier ) { build ( :dossier , user : nil ) }
it { expect ( orphan ) . to be_falsey }
end
end
end
describe '#owned_by?' do
subject ( :owned_by ) { dossier . owned_by? ( user ) }
context 'when the dossier is orphan' do
let ( :dossier ) { build ( :dossier , user : nil ) }
let ( :user ) { build ( :user ) }
it { expect ( owned_by ) . to be_falsey }
end
context 'when the given user is nil' do
let ( :dossier ) { build ( :dossier ) }
let ( :user ) { nil }
it { expect ( owned_by ) . to be_falsey }
end
context 'when the dossier has a user and it is not the given user' do
let ( :dossier ) { build ( :dossier ) }
let ( :user ) { build ( :user ) }
it { expect ( owned_by ) . to be_falsey }
end
context 'when the dossier has a user and it is the given user' do
let ( :dossier ) { build ( :dossier , user : user ) }
let ( :user ) { build ( :user ) }
it { expect ( owned_by ) . to be_truthy }
end
end
2023-01-06 17:48:00 +01:00
describe 'update procedure dossiers count' do
let ( :dossier ) { create ( :dossier , :brouillon , :with_individual ) }
it 'update procedure dossiers count when passing to construction' do
expect ( dossier . procedure ) . to receive ( :compute_dossiers_count )
dossier . passer_en_construction!
end
end
2023-06-22 14:18:37 +02:00
describe '#sva_svr_decision_in_days' do
let ( :dossier ) { create ( :dossier , :en_instruction , sva_svr_decision_on : 10 . days . from_now ) }
it { expect ( dossier . sva_svr_decision_in_days ) . to eq 10 }
end
2022-10-31 17:02:48 +01:00
private
def count_for_month ( processed_by_month , month )
processed_by_month . find { | date , _count | date . month == month } [ 1 ]
end
def create_dossier_for_month ( procedure , year , month )
Timecop . freeze ( Time . zone . local ( year , month , 5 ) ) do
create ( :dossier , :accepte , :with_attestation , procedure : procedure )
end
end
def create_archived_dossier_for_month ( procedure , year , month )
Timecop . freeze ( Time . zone . local ( year , month , 5 ) ) do
create ( :dossier , :accepte , :archived , :with_attestation , procedure : procedure )
end
end
2015-08-20 17:30:17 +02:00
end