2024-04-29 00:17:15 +02:00
# frozen_string_literal: true
2015-09-21 17:59:03 +02:00
describe Procedure do
2017-05-29 14:44:38 +02:00
describe 'mail templates' do
2017-05-27 01:08:01 +02:00
subject { create ( :procedure ) }
2024-06-25 17:17:51 +02:00
it " returns expected classes " do
expect ( subject . passer_en_construction_email_template ) . to be_a ( Mails :: InitiatedMail )
expect ( subject . passer_en_instruction_email_template ) . to be_a ( Mails :: ReceivedMail )
expect ( subject . accepter_email_template ) . to be_a ( Mails :: ClosedMail )
expect ( subject . refuser_email_template ) . to be_a ( Mails :: RefusedMail )
expect ( subject . classer_sans_suite_email_template ) . to be_a ( Mails :: WithoutContinuationMail )
expect ( subject . repasser_en_instruction_email_template ) . to be_a ( Mails :: ReInstructedMail )
end
2017-03-06 15:00:05 +01:00
end
2023-01-06 17:47:28 +01:00
describe 'compute_dossiers_count' do
let ( :procedure ) { create ( :procedure_with_dossiers , dossiers_count : 2 , dossiers_count_computed_at : Time . zone . now - Procedure :: DOSSIERS_COUNT_EXPIRING ) }
it 'caches estimated_dossiers_count' do
procedure . dossiers . each ( & :passer_en_construction! )
expect { procedure . compute_dossiers_count } . to change ( procedure , :estimated_dossiers_count ) . from ( nil ) . to ( 2 )
expect { create ( :dossier , procedure : procedure ) . passer_en_construction! } . not_to change ( procedure , :estimated_dossiers_count )
Timecop . freeze ( Time . zone . now + Procedure :: DOSSIERS_COUNT_EXPIRING )
expect { procedure . compute_dossiers_count } . to change ( procedure , :estimated_dossiers_count ) . from ( 2 ) . to ( 3 )
Timecop . return
end
end
2017-03-04 09:56:09 +01:00
describe 'initiated_mail' do
2017-12-22 21:37:08 +01:00
let ( :procedure ) { create ( :procedure ) }
subject { procedure }
2017-03-04 09:56:09 +01:00
context 'when initiated_mail is not customize' do
2023-11-29 12:59:55 +01:00
it { expect ( subject . passer_en_construction_email_template . body ) . to eq ( Mails :: InitiatedMail . default_for_procedure ( procedure ) . body ) }
2017-03-04 09:56:09 +01:00
end
context 'when initiated_mail is customize' do
before :each do
2017-03-06 22:37:37 +01:00
subject . initiated_mail = Mails :: InitiatedMail . new ( body : 'sisi' )
2017-03-04 09:56:09 +01:00
subject . save
subject . reload
end
2023-11-29 12:59:55 +01:00
it { expect ( subject . passer_en_construction_email_template . body ) . to eq ( 'sisi' ) }
2017-03-04 09:56:09 +01:00
end
context 'when initiated_mail is customize ... again' do
before :each do
2017-03-06 22:37:37 +01:00
subject . initiated_mail = Mails :: InitiatedMail . new ( body : 'toto' )
2017-03-04 09:56:09 +01:00
subject . save
subject . reload
end
2023-11-29 12:59:55 +01:00
it { expect ( subject . passer_en_construction_email_template . body ) . to eq ( 'toto' ) }
2017-03-04 09:56:09 +01:00
2017-03-06 22:37:37 +01:00
it { expect ( Mails :: InitiatedMail . count ) . to eq ( 1 ) }
2017-03-04 09:56:09 +01:00
end
end
2018-03-16 19:39:48 +01:00
describe 'closed mail template body' do
2022-02-11 08:45:16 +01:00
let ( :procedure ) { create ( :procedure , attestation_template : attestation_template ) }
let ( :attestation_template ) { nil }
2018-03-16 19:39:48 +01:00
2023-11-29 12:59:55 +01:00
subject { procedure . accepter_email_template . rich_body . body . to_html }
2018-03-16 19:39:48 +01:00
context 'for procedures without an attestation' do
it { is_expected . not_to include ( 'lien attestation' ) }
end
context 'for procedures with an attestation' do
2022-02-11 08:45:16 +01:00
let ( :attestation_template ) { build ( :attestation_template , activated : activated ) }
2018-03-16 19:39:48 +01:00
context 'when the attestation is inactive' do
let ( :activated ) { false }
it { is_expected . not_to include ( 'lien attestation' ) }
end
context 'when the attestation is inactive' do
let ( :activated ) { true }
it { is_expected . to include ( 'lien attestation' ) }
end
end
end
2018-04-03 11:33:11 +02:00
describe '#closed_mail_template_attestation_inconsistency_state' do
2022-02-02 16:10:29 +01:00
let ( :procedure_without_attestation ) { create ( :procedure , closed_mail : closed_mail , attestation_template : nil ) }
2018-04-03 11:33:11 +02:00
let ( :procedure_with_active_attestation ) do
2022-02-02 16:10:29 +01:00
create ( :procedure , closed_mail : closed_mail , attestation_template : build ( :attestation_template , activated : true ) )
2018-04-03 11:33:11 +02:00
end
let ( :procedure_with_inactive_attestation ) do
2022-02-02 16:10:29 +01:00
create ( :procedure , closed_mail : closed_mail , attestation_template : build ( :attestation_template , activated : false ) )
2018-04-03 11:33:11 +02:00
end
subject { procedure . closed_mail_template_attestation_inconsistency_state }
context 'with a custom mail template' do
context 'that contains a lien attestation tag' do
2020-07-20 17:25:17 +02:00
let ( :closed_mail ) { build ( :closed_mail , body : '--lien attestation--' ) }
2018-04-03 11:33:11 +02:00
context 'when the procedure doesn’ t have an attestation' do
let ( :procedure ) { procedure_without_attestation }
it { is_expected . to eq ( :extraneous_tag ) }
end
context 'when the procedure has an active attestation' do
let ( :procedure ) { procedure_with_active_attestation }
it { is_expected . to be ( nil ) }
end
context 'when the procedure has an inactive attestation' do
let ( :procedure ) { procedure_with_inactive_attestation }
it { is_expected . to eq ( :extraneous_tag ) }
end
end
context 'that doesn’ t contain a lien attestation tag' do
2020-07-20 17:25:17 +02:00
let ( :closed_mail ) { build ( :closed_mail ) }
2018-04-03 11:33:11 +02:00
context 'when the procedure doesn’ t have an attestation' do
let ( :procedure ) { procedure_without_attestation }
it { is_expected . to be ( nil ) }
end
context 'when the procedure has an active attestation' do
let ( :procedure ) { procedure_with_active_attestation }
it { is_expected . to eq ( :missing_tag ) }
end
context 'when the procedure has an inactive attestation' do
let ( :procedure ) { procedure_with_inactive_attestation }
it { is_expected . to be ( nil ) }
end
end
end
context 'with the default mail template' do
let ( :closed_mail ) { nil }
context 'when the procedure doesn’ t have an attestation' do
let ( :procedure ) { procedure_without_attestation }
it { is_expected . to be ( nil ) }
end
context 'when the procedure has an active attestation' do
let ( :procedure ) { procedure_with_active_attestation }
it { is_expected . to be ( nil ) }
end
context 'when the procedure has an inactive attestation' do
let ( :procedure ) { procedure_with_inactive_attestation }
it { is_expected . to be ( nil ) }
end
end
end
2020-01-28 16:41:15 +01:00
describe 'scopes' do
let! ( :procedure ) { create ( :procedure ) }
2020-02-05 16:09:03 +01:00
let! ( :discarded_procedure ) { create ( :procedure , :discarded ) }
2020-01-28 16:41:15 +01:00
describe 'default_scope' do
subject { Procedure . all }
it { is_expected . to match_array ( [ procedure ] ) }
end
end
2015-09-22 11:21:52 +02:00
describe 'validation' do
context 'libelle' do
it { is_expected . not_to allow_value ( nil ) . for ( :libelle ) }
it { is_expected . not_to allow_value ( '' ) . for ( :libelle ) }
it { is_expected . to allow_value ( 'Demande de subvention' ) . for ( :libelle ) }
end
2024-02-15 10:41:58 +01:00
context 'closing procedure' do
context 'without replacing procedure in DS' do
let ( :procedure ) { create ( :procedure ) }
context 'valid' do
before do
procedure . update! ( closing_details : " Bonjour, \n La démarche est désormais hébergée sur une autre plateforme \n Cordialement " , closing_reason : Procedure . closing_reasons . fetch ( :other ) )
end
it { expect ( procedure ) . to be_valid }
end
end
end
2015-09-22 11:21:52 +02:00
context 'description' do
it { is_expected . not_to allow_value ( nil ) . for ( :description ) }
it { is_expected . not_to allow_value ( '' ) . for ( :description ) }
it { is_expected . to allow_value ( 'Description Demande de subvention' ) . for ( :description ) }
end
2017-11-23 11:37:41 +01:00
context 'organisation' do
it { is_expected . to allow_value ( 'URRSAF' ) . for ( :organisation ) }
end
2018-05-31 10:59:38 +02:00
2019-04-29 18:27:44 +02:00
context 'administrateurs' do
it { is_expected . not_to allow_value ( [ ] ) . for ( :administrateurs ) }
end
2018-05-31 10:59:38 +02:00
context 'juridique' do
2022-11-22 15:17:37 +01:00
it { is_expected . not_to allow_value ( nil ) . on ( :publication ) . for ( :cadre_juridique ) }
it { is_expected . to allow_value ( 'text' ) . on ( :publication ) . for ( :cadre_juridique ) }
2018-05-31 10:59:38 +02:00
context 'with deliberation' do
2024-06-10 09:57:34 +02:00
let ( :procedure ) { build ( :procedure , cadre_juridique : nil , revisions : [ build ( :procedure_revision ) ] ) }
2018-05-31 10:59:38 +02:00
2022-11-22 15:17:37 +01:00
it { expect ( procedure . valid? ( :publication ) ) . to eq ( false ) }
2018-05-31 10:59:38 +02:00
context 'when the deliberation is uploaded ' do
before do
2020-06-30 17:53:54 +02:00
procedure . deliberation = fixture_file_upload ( 'spec/fixtures/files/file.pdf' , 'application/pdf' )
2018-05-31 10:59:38 +02:00
end
2022-11-22 15:17:37 +01:00
it { expect ( procedure . valid? ( :publication ) ) . to eq ( true ) }
2018-05-31 10:59:38 +02:00
end
2020-03-16 17:55:16 +01:00
context 'when the deliberation is uploaded with an unauthorized format' do
before do
2020-06-30 17:53:54 +02:00
procedure . deliberation = fixture_file_upload ( 'spec/fixtures/files/french-flag.gif' , 'image/gif' )
2020-03-16 17:55:16 +01:00
end
2022-11-22 15:17:37 +01:00
it { expect ( procedure . valid? ( :publication ) ) . to eq ( false ) }
2020-03-16 17:55:16 +01:00
end
2018-05-31 10:59:38 +02:00
end
2020-07-08 17:00:21 +02:00
2021-07-16 17:03:58 +02:00
context 'when juridique_required is false' do
let ( :procedure ) { build ( :procedure , juridique_required : false , cadre_juridique : nil ) }
2022-11-22 15:17:37 +01:00
it { expect ( procedure . valid? ( :publication ) ) . to eq ( true ) }
2020-07-08 17:00:21 +02:00
end
2018-05-31 10:59:38 +02:00
end
2018-06-01 10:51:04 +02:00
2021-07-16 17:03:58 +02:00
context 'api_entreprise_token' do
let ( :valid_token ) { " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c " }
let ( :invalid_token ) { 'plouf' }
it { is_expected . to allow_value ( valid_token ) . for ( :api_entreprise_token ) }
it { is_expected . not_to allow_value ( invalid_token ) . for ( :api_entreprise_token ) }
end
2018-06-01 10:51:04 +02:00
2021-07-16 17:03:58 +02:00
context 'api_particulier_token' do
let ( :valid_token ) { " 3841b13fa8032ed3c31d160d3437a76a " }
let ( :invalid_token ) { 'jet0n 1nvalide' }
it { is_expected . to allow_value ( valid_token ) . for ( :api_particulier_token ) }
it { is_expected . not_to allow_value ( invalid_token ) . for ( :api_particulier_token ) }
2018-06-01 10:51:04 +02:00
end
2018-05-24 23:29:33 +02:00
2019-07-17 15:12:07 +02:00
context 'monavis' do
context 'nil is allowed' do
2019-07-17 15:34:10 +02:00
it { is_expected . to allow_value ( nil ) . for ( :monavis_embed ) }
it { is_expected . to allow_value ( '' ) . for ( :monavis_embed ) }
2019-07-17 15:12:07 +02:00
end
context 'random string is not allowed' do
2019-07-17 15:34:10 +02:00
let ( :procedure ) { build ( :procedure , monavis_embed : " plop " ) }
2019-07-17 15:12:07 +02:00
it { expect ( procedure . valid? ) . to eq ( false ) }
end
context 'random html is not allowed' do
2019-07-17 15:34:10 +02:00
let ( :procedure ) { build ( :procedure , monavis_embed : '<img src="http://some.analytics/hello.gif">' ) }
2019-07-17 15:12:07 +02:00
it { expect ( procedure . valid? ) . to eq ( false ) }
end
context 'Monavis embed code with white button is allowed' do
monavis_blanc = <<-MSG
2019-07-17 15:34:10 +02:00
< a href = " https://monavis.numerique.gouv.fr/Demarches/123?&view-mode=formulaire-avis&nd_mode=en-ligne-enti%C3%A8rement&nd_source=button&key=cd4a872d475e4045666057f " >
< img src = " https://monavis.numerique.gouv.fr/monavis-static/bouton-blanc.png " alt = " Je donne mon avis " title = " Je donne mon avis sur cette démarche " / >
< / a>
MSG
let ( :procedure ) { build ( :procedure , monavis_embed : monavis_blanc ) }
2019-07-17 15:12:07 +02:00
it { expect ( procedure . valid? ) . to eq ( true ) }
end
context 'Monavis embed code with blue button is allowed' do
2019-07-17 15:34:10 +02:00
monavis_bleu = <<-MSG
< a href = " https://monavis.numerique.gouv.fr/Demarches/123?&view-mode=formulaire-avis&nd_mode=en-ligne-enti%C3%A8rement&nd_source=button&key=cd4a872d475e4045666057f " >
< img src = " https://monavis.numerique.gouv.fr/monavis-static/bouton-bleu.png " alt = " Je donne mon avis " title = " Je donne mon avis sur cette démarche " / >
< / a>
MSG
let ( :procedure ) { build ( :procedure , monavis_embed : monavis_bleu ) }
2019-07-17 15:12:07 +02:00
it { expect ( procedure . valid? ) . to eq ( true ) }
end
2021-12-13 14:49:40 +01:00
context 'Monavis embed code with voxusages is allowed' do
monavis_issue_phillipe = <<-MSG
< a href = " https://voxusagers.numerique.gouv.fr/Demarches/3193?&view-mode=formulaire-avis&nd_mode=en-ligne-enti%C3%A8rement&nd_source=button&key=58e099a09c02abe629c14905ed2b055d " >
< img src = " https://monavis.numerique.gouv.fr/monavis-static/bouton-bleu.png " alt = " Je donne mon avis " title = " Je donne mon avis sur cette démarche " / >
< / a>
MSG
let ( :procedure ) { build ( :procedure , monavis_embed : monavis_issue_phillipe ) }
it { expect ( procedure . valid? ) . to eq ( true ) }
end
2022-01-24 15:56:29 +01:00
context 'Monavis embed code without title allowed' do
monavis_issue_bouchra = <<-MSG
< a href = " https://voxusagers.numerique.gouv.fr/Demarches/3193?&view-mode=formulaire-avis&nd_mode=en-ligne-enti%C3%A8rement&nd_source=button&key=58e099a09c02abe629c14905ed2b055d " >
< img src = " https://voxusagers.numerique.gouv.fr/static/bouton-bleu.svg " alt = " Je donne mon avis " / >
< / a>
MSG
let ( :procedure ) { build ( :procedure , monavis_embed : monavis_issue_bouchra ) }
it { expect ( procedure . valid? ) . to eq ( true ) }
end
2024-02-20 10:30:35 +01:00
context 'Monavis embed code with jedonnemonavis' do
monavis_jedonnemonavis = <<-MSG
< a href = " https://jedonnemonavis.numerique.gouv.fr/Demarches/3839?&view-mode=formulaire-avis&nd_source=button&key=6af80846f64fb213abcabaeea7a3ea8c " >
< img src = " https://jedonnemonavis.numerique.gouv.fr/static/bouton-bleu.svg " alt = " Je donne mon avis " / >
< / a>
MSG
let ( :procedure ) { build ( :procedure , monavis_embed : monavis_jedonnemonavis ) }
it { expect ( procedure . valid? ) . to eq ( true ) }
end
context 'Monavis embed code with kthxbye' do
monavis_jedonnemonavis = <<-MSG
< a href = " https://kthxbye.fr/?key=6af80846f64fb213abcabaeea7a3ea8c " >
< img src = " https://kthxbye.fr/static/bouton-bleu.svg " alt = " Je donne mon avis " / >
< / a>
MSG
let ( :procedure ) { build ( :procedure , monavis_embed : monavis_jedonnemonavis ) }
it { expect ( procedure . valid? ) . to eq ( false ) }
end
2019-07-17 15:12:07 +02:00
end
2022-07-08 17:45:14 +02:00
describe 'duree de conservation dans ds' do
let ( :field_name ) { :duree_conservation_dossiers_dans_ds }
2022-10-05 17:21:51 +02:00
context 'by default is caped to 12' do
2022-10-06 15:04:59 +02:00
subject { create ( :procedure , duree_conservation_dossiers_dans_ds : 12 , max_duree_conservation_dossiers_dans_ds : 12 ) }
2018-05-24 23:29:33 +02:00
it { is_expected . not_to allow_value ( nil ) . for ( field_name ) }
it { is_expected . not_to allow_value ( '' ) . for ( field_name ) }
it { is_expected . not_to allow_value ( 'trois' ) . for ( field_name ) }
it { is_expected . to allow_value ( 3 ) . for ( field_name ) }
2022-10-05 17:21:51 +02:00
it { is_expected . to validate_numericality_of ( field_name ) . is_less_than_or_equal_to ( 12 ) }
2018-05-24 23:29:33 +02:00
end
2022-10-05 17:21:51 +02:00
context 'can be over riden' do
subject { create ( :procedure , duree_conservation_dossiers_dans_ds : 60 , max_duree_conservation_dossiers_dans_ds : 60 ) }
2022-07-08 17:45:14 +02:00
it { is_expected . not_to allow_value ( nil ) . for ( field_name ) }
it { is_expected . not_to allow_value ( '' ) . for ( field_name ) }
it { is_expected . not_to allow_value ( 'trois' ) . for ( field_name ) }
2022-10-05 17:21:51 +02:00
it { is_expected . to allow_value ( 3 ) . for ( field_name ) }
it { is_expected . to allow_value ( 60 ) . for ( field_name ) }
it { is_expected . to validate_numericality_of ( field_name ) . is_less_than_or_equal_to ( 60 ) }
2022-07-08 17:45:14 +02:00
end
2018-05-24 23:29:33 +02:00
end
2021-11-23 08:12:40 +01:00
2022-01-26 18:06:39 +01:00
describe 'draft_types_de_champ validations' do
2024-06-10 09:35:29 +02:00
let ( :procedure ) { create ( :procedure , types_de_champ_public : , types_de_champ_private : ) }
2021-11-30 12:26:19 +01:00
2021-11-23 08:12:40 +01:00
context 'on a draft procedure' do
2024-06-10 09:35:29 +02:00
let ( :types_de_champ_private ) { [ ] }
let ( :types_de_champ_public ) { [ { type : :repetition , libelle : 'Enfants' , children : [ ] } ] }
2022-01-26 18:06:39 +01:00
it 'doesn’ t validate the types de champs' do
2021-11-23 08:12:40 +01:00
procedure . validate
2022-11-16 12:46:33 +01:00
expect ( procedure . errors [ :draft_types_de_champ_public ] ) . not_to be_present
2021-11-23 08:12:40 +01:00
end
end
2022-11-02 10:03:10 +01:00
context 'when validating for publication' do
2024-06-10 09:35:29 +02:00
let ( :types_de_champ_public ) do
[
{ type : :repetition , libelle : 'Enfants' , children : [ ] } ,
{ type : :drop_down_list , libelle : 'Civilité' , options : [ ] }
]
end
let ( :types_de_champ_private ) { [ ] }
2024-06-05 18:00:19 +02:00
let ( :invalid_repetition_error_message ) { " doit comporter au moins un champ répétable " }
let ( :invalid_drop_down_error_message ) { " doit comporter au moins un choix sélectionnable " }
2024-06-10 09:35:29 +02:00
2021-11-23 08:12:40 +01:00
it 'validates that no repetition type de champ is empty' do
2022-11-02 10:03:10 +01:00
procedure . validate ( :publication )
2024-06-05 18:00:19 +02:00
expect ( procedure . errors . messages_for ( :draft_types_de_champ_public ) ) . to include ( invalid_repetition_error_message )
2021-11-23 08:12:40 +01:00
2022-05-18 18:04:28 +02:00
new_draft = procedure . draft_revision
2024-06-10 09:35:29 +02:00
repetition = procedure . draft_revision . types_de_champ_public . find ( & :repetition? )
2022-05-18 18:04:28 +02:00
parent_coordinate = new_draft . revision_types_de_champ . find_by ( type_de_champ : repetition )
new_draft . revision_types_de_champ . create ( type_de_champ : create ( :type_de_champ ) , position : 0 , parent : parent_coordinate )
2021-11-23 08:12:40 +01:00
2022-11-02 10:03:10 +01:00
procedure . validate ( :publication )
2024-06-05 18:00:19 +02:00
expect ( procedure . errors . messages_for ( :draft_types_de_champ_public ) ) . not_to include ( invalid_repetition_error_message )
2021-11-23 08:12:40 +01:00
end
2021-11-30 12:26:19 +01:00
it 'validates that no drop-down type de champ is empty' do
2024-10-07 14:35:23 +02:00
drop_down = procedure . draft_revision . types_de_champ_public . find ( & :drop_down_list? )
drop_down . update! ( drop_down_options : [ ] )
procedure . reload . validate ( :publication )
2024-06-05 18:00:19 +02:00
expect ( procedure . errors . messages_for ( :draft_types_de_champ_public ) ) . to include ( invalid_drop_down_error_message )
2021-11-30 12:26:19 +01:00
2024-09-20 10:56:12 +02:00
drop_down . update! ( drop_down_options : [ " --title-- " , " some value " ] )
2022-11-02 10:03:10 +01:00
procedure . reload . validate ( :publication )
2024-06-05 18:00:19 +02:00
expect ( procedure . errors . messages_for ( :draft_types_de_champ_public ) ) . not_to include ( invalid_drop_down_error_message )
2021-11-30 12:26:19 +01:00
end
2021-11-23 08:12:40 +01:00
end
2022-01-26 17:52:54 +01:00
context 'when the champ is private' do
2024-06-10 09:35:29 +02:00
let ( :types_de_champ_private ) do
[
{ type : :repetition , libelle : 'Enfants' , children : [ ] } ,
{ type : :drop_down_list , libelle : 'Civilité' , options : [ ] }
]
2022-05-18 18:04:28 +02:00
end
2024-06-10 09:35:29 +02:00
let ( :types_de_champ_public ) { [ ] }
2022-01-26 17:52:54 +01:00
2024-06-05 18:00:19 +02:00
let ( :invalid_repetition_error_message ) { " doit comporter au moins un champ répétable " }
let ( :invalid_drop_down_error_message ) { " doit comporter au moins un choix sélectionnable " }
2022-01-26 17:52:54 +01:00
it 'validates that no repetition type de champ is empty' do
procedure . validate ( :publication )
2024-06-05 18:00:19 +02:00
expect ( procedure . errors . messages_for ( :draft_types_de_champ_private ) ) . to include ( invalid_repetition_error_message )
2024-10-07 14:35:23 +02:00
2024-06-05 18:00:19 +02:00
repetition = procedure . draft_revision . types_de_champ_private . find ( & :repetition? )
expect ( procedure . errors . to_enum . to_a . map { _1 . options [ :type_de_champ ] } ) . to include ( repetition )
2022-01-26 17:52:54 +01:00
end
it 'validates that no drop-down type de champ is empty' do
2024-06-05 18:00:19 +02:00
drop_down = procedure . draft_revision . types_de_champ_private . find ( & :drop_down_list? )
2024-10-07 14:35:23 +02:00
drop_down . update! ( drop_down_options : [ ] )
procedure . reload . validate ( :publication )
expect ( procedure . errors . messages_for ( :draft_types_de_champ_private ) ) . to include ( invalid_drop_down_error_message )
2024-06-05 18:00:19 +02:00
expect ( procedure . errors . to_enum . to_a . map { _1 . options [ :type_de_champ ] } ) . to include ( drop_down )
2022-01-26 17:52:54 +01:00
end
end
2024-06-10 09:35:29 +02:00
2024-06-10 09:57:34 +02:00
context 'when condition on champ private use public champ' do
include Logic
let ( :types_de_champ_public ) { [ { type : :decimal_number , stable_id : 1 } ] }
let ( :types_de_champ_private ) { [ { type : :text , condition : ds_eq ( champ_value ( 1 ) , constant ( 2 ) ) , stable_id : 2 } ] }
it 'validate without context' do
procedure . validate
expect ( procedure . errors . full_messages_for ( :draft_types_de_champ_private ) ) . to be_empty
end
2024-06-11 11:40:15 +02:00
it 'validate allows condition' do
procedure . validate ( :types_de_champ_private_editor )
expect ( procedure . errors . full_messages_for ( :draft_types_de_champ_private ) ) . to be_empty
end
end
context 'when condition on champ private use public champ having a position higher than the champ private' do
include Logic
let ( :types_de_champ_public ) do
[
{ type : :decimal_number , stable_id : 1 } ,
{ type : :decimal_number , stable_id : 2 }
]
end
let ( :types_de_champ_private ) do
[
{ type : :text , condition : ds_eq ( champ_value ( 2 ) , constant ( 2 ) ) , stable_id : 3 }
]
end
it 'validate without context' do
procedure . validate
expect ( procedure . errors . full_messages_for ( :draft_types_de_champ_private ) ) . to be_empty
end
2024-06-10 09:57:34 +02:00
it 'validate allows condition' do
procedure . validate ( :types_de_champ_private_editor )
expect ( procedure . errors . full_messages_for ( :draft_types_de_champ_private ) ) . to be_empty
2024-06-10 09:35:29 +02:00
end
end
2024-06-10 09:57:34 +02:00
context 'when condition on champ public use private champ' do
2024-06-10 09:35:29 +02:00
include Logic
2024-06-10 09:57:34 +02:00
let ( :types_de_champ_public ) { [ { type : :text , libelle : 'condition' , condition : ds_eq ( champ_value ( 1 ) , constant ( 2 ) ) , stable_id : 2 } ] }
let ( :types_de_champ_private ) { [ { type : :decimal_number , stable_id : 1 } ] }
2024-06-05 18:00:19 +02:00
let ( :error_on_condition ) { " Le champ a une logique conditionnelle invalide " }
2024-06-10 09:35:29 +02:00
it 'validate without context' do
2024-06-10 09:57:34 +02:00
procedure . validate
expect ( procedure . errors . full_messages_for ( :draft_types_de_champ_public ) ) . to be_empty
2024-06-10 09:35:29 +02:00
end
2024-06-10 09:57:34 +02:00
it 'validate prevent condition' do
procedure . validate ( :types_de_champ_public_editor )
expect ( procedure . errors . full_messages_for ( :draft_types_de_champ_public ) ) . to include ( error_on_condition )
2024-06-10 09:35:29 +02:00
end
2022-01-26 17:52:54 +01:00
end
2021-11-23 08:12:40 +01:00
end
2023-06-01 14:47:06 +02:00
context 'with auto archive' do
let ( :procedure ) { create ( :procedure , auto_archive_on : 1 . day . from_now ) }
it { expect ( procedure ) . to be_valid }
context 'when auto_archive_on is in the past' do
it 'validates only when attribute is changed' do
procedure . auto_archive_on = 1 . day . ago
expect ( procedure ) . not_to be_valid
procedure . save! ( validate : false )
expect ( procedure ) . to be_valid
end
end
end
2023-06-02 18:34:46 +02:00
context 'with sva svr' do
before {
procedure . sva_svr [ " decision " ] = " svr "
}
context 'when procedure is published with sva' do
let ( :procedure ) { create ( :procedure , :published , :sva ) }
it 'prevents changes to sva_svr' do
expect ( procedure ) . not_to be_valid
expect ( procedure . errors [ :sva_svr ] . join ) . to include ( 'ne peut plus être modifiée' )
end
end
context 'when procedure is published without sva' do
let ( :procedure ) { create ( :procedure , :published ) }
it 'allow activation' do
expect ( procedure ) . to be_valid
end
it 'allow activation from disabled value' do
procedure . sva_svr [ " decision " ] = " disabled "
procedure . save!
procedure . sva_svr [ " decision " ] = " svr "
expect ( procedure ) . to be_valid
end
end
context 'brouillon procedure' do
let ( :procedure ) { create ( :procedure , :sva ) }
it " can update sva config " do
expect ( procedure ) . to be_valid
end
end
2023-06-26 17:34:54 +02:00
context " with declarative " do
let ( :procedure ) { create ( :procedure , declarative_with_state : " accepte " ) }
it 'is not valid' do
expect ( procedure ) . not_to be_valid
expect ( procedure . errors [ :sva_svr ] . join ) . to include ( 'incompatible avec une démarche déclarative' )
end
end
2023-06-02 18:34:46 +02:00
end
2015-09-22 11:21:52 +02:00
end
2015-11-17 15:30:03 +01:00
2022-06-22 15:32:35 +02:00
describe 'opendata' do
let ( :procedure ) { create ( :procedure ) }
it 'is true by default' do
expect ( procedure . opendata ) . to be_truthy
end
end
2023-02-28 16:25:53 +01:00
describe 'publiques' do
let ( :draft_procedure ) { create ( :procedure_with_dossiers , :draft , estimated_dossiers_count : 4 , lien_site_web : 'https://monministere.gouv.fr/cparici' ) }
let ( :published_procedure ) { create ( :procedure_with_dossiers , :published , estimated_dossiers_count : 4 , lien_site_web : 'https://monministere.gouv.fr/cparici' ) }
let ( :published_procedure_no_opendata ) { create ( :procedure_with_dossiers , :published , estimated_dossiers_count : 4 , opendata : false ) }
let ( :published_procedure_with_3_dossiers ) { create ( :procedure_with_dossiers , :published , estimated_dossiers_count : 3 ) }
let ( :published_procedure_with_mail ) { create ( :procedure_with_dossiers , :published , estimated_dossiers_count : 4 , lien_site_web : 'par mail' ) }
let ( :published_procedure_with_intra ) { create ( :procedure_with_dossiers , :published , estimated_dossiers_count : 4 , lien_site_web : 'https://intra.service-etat.gouv.fr' ) }
it 'returns published procedure, with opendata flag, with accepted lien_site_web' do
expect ( Procedure . publiques ) . not_to include ( published_procedure_no_opendata )
end
it " returns only published or closed procedures " do
expect ( Procedure . publiques ) . not_to include ( draft_procedure )
end
it " returns only procedures with opendata flag " do
expect ( Procedure . publiques ) . not_to include ( published_procedure_with_mail )
end
it " returns only procedures without mail in lien_site_web " do
expect ( Procedure . publiques ) . not_to include ( published_procedure_with_mail )
end
it " returns only procedures without intra in lien_site_web " do
expect ( Procedure . publiques ) . not_to include ( published_procedure_with_intra )
end
it " does not return procedures with less than 4 dossiers " do
expect ( Procedure . publiques ) . not_to include ( published_procedure_with_3_dossiers )
end
end
2016-06-09 17:49:38 +02:00
describe 'active' do
2018-05-16 17:21:12 +02:00
let ( :procedure ) { create ( :procedure ) }
2016-06-09 17:49:38 +02:00
subject { Procedure . active ( procedure . id ) }
2015-12-21 14:40:28 +01:00
2019-11-14 09:43:45 +01:00
context 'when procedure is in draft status and not closed' do
2016-06-09 17:49:38 +02:00
it { expect { subject } . to raise_error ( ActiveRecord :: RecordNotFound ) }
end
2019-11-14 09:43:45 +01:00
context 'when procedure is published and not closed' do
2018-05-16 17:21:12 +02:00
let ( :procedure ) { create ( :procedure , :published ) }
2015-12-21 14:40:28 +01:00
it { is_expected . to be_truthy }
end
2016-06-09 17:49:38 +02:00
2019-11-14 09:43:45 +01:00
context 'when procedure is published and closed' do
let ( :procedure ) { create ( :procedure , :closed ) }
2016-06-09 17:49:38 +02:00
it { expect { subject } . to raise_error ( ActiveRecord :: RecordNotFound ) }
end
2015-12-21 14:40:28 +01:00
end
2016-06-15 11:34:05 +02:00
2020-05-05 15:26:08 +02:00
describe 'api_entreprise_token_expired?' do
2020-07-08 17:00:21 +02:00
let ( :token ) { " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c " }
2020-05-05 15:26:08 +02:00
let ( :procedure ) { create ( :procedure , api_entreprise_token : token ) }
let ( :payload ) {
[
{ " exp " = > expiration_time }
]
}
let ( :subject ) { procedure . api_entreprise_token_expired? }
before do
allow ( JWT ) . to receive ( :decode ) . with ( token , nil , false ) . and_return ( payload )
end
context " with token expired " do
2023-04-19 11:05:30 +02:00
let ( :expiration_time ) { ( 1 . day . ago ) . to_i }
2020-05-05 15:26:08 +02:00
it { is_expected . to be_truthy }
end
context " with token not expired " do
2023-04-19 11:05:30 +02:00
let ( :expiration_time ) { ( 1 . day . from_now ) . to_i }
2020-05-05 15:26:08 +02:00
it { is_expected . to be_falsey }
end
end
2016-06-15 11:34:05 +02:00
describe 'clone' do
2020-08-27 19:56:19 +02:00
let ( :service ) { create ( :service ) }
2022-02-11 08:45:16 +01:00
let ( :procedure ) do
create ( :procedure ,
received_mail : received_mail ,
service : service ,
2022-05-27 18:51:06 +02:00
opendata : opendata ,
2022-08-17 09:07:12 +02:00
duree_conservation_etendue_par_ds : true ,
duree_conservation_dossiers_dans_ds : Procedure :: OLD_MAX_DUREE_CONSERVATION ,
2022-10-06 15:04:59 +02:00
max_duree_conservation_dossiers_dans_ds : Procedure :: OLD_MAX_DUREE_CONSERVATION ,
2022-02-11 08:45:16 +01:00
attestation_template : build ( :attestation_template , logo : logo , signature : signature ) ,
2022-08-04 11:39:07 +02:00
types_de_champ_public : [ { } , { } , { type : :drop_down_list } , { type : :piece_justificative } , { type : :repetition , children : [ { } ] } ] ,
types_de_champ_private : [ { } , { } , { type : :drop_down_list } , { type : :repetition , children : [ { } ] } ] ,
2022-02-11 08:45:16 +01:00
api_particulier_token : '123456789012345' ,
2023-03-07 18:16:28 +01:00
api_particulier_scopes : [ 'cnaf_famille' ] ,
2024-05-07 10:16:44 +02:00
estimated_dossiers_count : 4 ,
template : true )
2022-02-11 08:45:16 +01:00
end
2022-11-16 12:46:33 +01:00
let ( :type_de_champ_repetition ) { procedure . draft_revision . types_de_champ_public . last }
let ( :type_de_champ_private_repetition ) { procedure . draft_revision . types_de_champ_private . last }
2020-07-20 17:25:17 +02:00
let ( :received_mail ) { build ( :received_mail ) }
2018-04-12 18:35:13 +02:00
let ( :from_library ) { false }
2022-05-27 18:51:06 +02:00
let ( :opendata ) { true }
2019-02-26 16:57:04 +01:00
let ( :administrateur ) { procedure . administrateurs . first }
2022-02-11 08:45:16 +01:00
let ( :logo ) { Rack :: Test :: UploadedFile . new ( 'spec/fixtures/files/white.png' , 'image/png' ) }
let ( :signature ) { Rack :: Test :: UploadedFile . new ( 'spec/fixtures/files/black.png' , 'image/png' ) }
2016-09-01 10:43:49 +02:00
2023-08-29 14:41:28 +02:00
let ( :groupe_instructeur_1 ) { create ( :groupe_instructeur , procedure : procedure , label : " groupe_1 " , contact_information : create ( :contact_information ) ) }
2020-08-27 19:56:19 +02:00
let ( :instructeur_1 ) { create ( :instructeur ) }
let ( :instructeur_2 ) { create ( :instructeur ) }
2020-03-11 19:49:04 +01:00
let! ( :assign_to_1 ) { create ( :assign_to , procedure : procedure , groupe_instructeur : groupe_instructeur_1 , instructeur : instructeur_1 ) }
let! ( :assign_to_2 ) { create ( :assign_to , procedure : procedure , groupe_instructeur : groupe_instructeur_1 , instructeur : instructeur_2 ) }
2023-04-26 10:47:51 +02:00
subject do
2018-12-19 15:35:07 +01:00
@procedure = procedure . clone ( administrateur , from_library )
2018-01-08 15:42:38 +01:00
@procedure . save
2023-04-26 10:47:51 +02:00
@procedure
2017-06-08 14:16:48 +02:00
end
2018-04-24 15:23:07 +02:00
it { expect ( subject . parent_procedure ) . to eq ( procedure ) }
2020-03-11 19:49:04 +01:00
2024-05-07 10:16:44 +02:00
it 'the cloned procedure should not be a template anymore' do
expect ( subject . template ) . to be_falsey
end
2020-03-11 19:49:04 +01:00
describe " should keep groupe instructeurs " do
it " should clone groupe instructeurs " do
expect ( subject . groupe_instructeurs . size ) . to eq ( 2 )
expect ( subject . groupe_instructeurs . size ) . to eq ( procedure . groupe_instructeurs . size )
expect ( subject . groupe_instructeurs . where ( label : " groupe_1 " ) . first ) . not_to be nil
2023-04-21 11:19:09 +02:00
expect ( subject . defaut_groupe_instructeur_id ) . to eq ( subject . groupe_instructeurs . find_by ( label : 'défaut' ) . id )
2020-03-11 19:49:04 +01:00
end
it " should clone instructeurs in the groupe " do
expect ( subject . groupe_instructeurs . where ( label : " groupe_1 " ) . first . instructeurs . map ( & :email ) ) . to eq ( procedure . groupe_instructeurs . where ( label : " groupe_1 " ) . first . instructeurs . map ( & :email ) )
end
2023-05-05 10:19:57 +02:00
it 'should clone with success a second group instructeur closed' do
procedure . groupe_instructeurs . last . update ( closed : true )
expect { subject } . not_to raise_error
end
2023-08-29 14:41:28 +02:00
it 'should clone groupe instructeur services' do
expect ( procedure . groupe_instructeurs . last . contact_information ) . not_to eq nil
expect ( subject . groupe_instructeurs . last . contact_information ) . not_to eq nil
end
2020-03-11 19:49:04 +01:00
end
2018-04-24 15:23:07 +02:00
2022-08-17 09:07:12 +02:00
it 'should reset duree_conservation_etendue_par_ds' do
expect ( subject . duree_conservation_etendue_par_ds ) . to eq ( false )
2023-11-07 10:45:40 +01:00
expect ( subject . duree_conservation_dossiers_dans_ds ) . to eq ( Expired :: DEFAULT_DOSSIER_RENTENTION_IN_MONTH )
2022-08-17 09:07:12 +02:00
end
2016-06-15 11:34:05 +02:00
it 'should duplicate specific objects with different id' do
expect ( subject . id ) . not_to eq ( procedure . id )
2022-11-16 12:46:33 +01:00
expect ( subject . draft_revision . types_de_champ_public . size ) . to eq ( procedure . draft_revision . types_de_champ_public . size )
expect ( subject . draft_revision . types_de_champ_private . size ) . to eq ( procedure . draft_revision . types_de_champ_private . size )
2016-09-01 10:43:49 +02:00
2022-11-16 12:46:33 +01:00
procedure . draft_revision . types_de_champ_public . zip ( subject . draft_revision . types_de_champ_public ) . each do | ptc , stc |
2022-05-25 09:51:40 +02:00
expect ( stc ) . to have_same_attributes_as ( ptc )
2020-07-28 16:39:32 +02:00
expect ( stc . revision ) . to eq ( subject . draft_revision )
2016-06-15 11:34:05 +02:00
end
2022-05-31 13:28:56 +02:00
public_repetition = type_de_champ_repetition
2022-11-16 12:46:33 +01:00
cloned_public_repetition = subject . draft_revision . types_de_champ_public . repetition . first
2022-05-31 13:28:56 +02:00
procedure . draft_revision . children_of ( public_repetition ) . zip ( subject . draft_revision . children_of ( cloned_public_repetition ) ) . each do | ptc , stc |
2022-05-25 09:51:40 +02:00
expect ( stc ) . to have_same_attributes_as ( ptc )
2020-12-18 18:06:59 +01:00
expect ( stc . revision ) . to eq ( subject . draft_revision )
end
2022-11-16 12:46:33 +01:00
procedure . draft_revision . types_de_champ_private . zip ( subject . draft_revision . types_de_champ_private ) . each do | ptc , stc |
2022-05-25 09:51:40 +02:00
expect ( stc ) . to have_same_attributes_as ( ptc )
2020-07-28 16:39:32 +02:00
expect ( stc . revision ) . to eq ( subject . draft_revision )
2016-09-09 17:39:56 +02:00
end
2022-05-31 13:28:56 +02:00
private_repetition = type_de_champ_private_repetition
2022-11-16 12:46:33 +01:00
cloned_private_repetition = subject . draft_revision . types_de_champ_private . repetition . first
2022-05-31 13:28:56 +02:00
procedure . draft_revision . children_of ( private_repetition ) . zip ( subject . draft_revision . children_of ( cloned_private_repetition ) ) . each do | ptc , stc |
2022-05-25 09:51:40 +02:00
expect ( stc ) . to have_same_attributes_as ( ptc )
2020-12-18 18:06:59 +01:00
expect ( stc . revision ) . to eq ( subject . draft_revision )
end
2017-06-08 14:16:48 +02:00
expect ( subject . attestation_template . title ) . to eq ( procedure . attestation_template . title )
2018-04-12 18:35:13 +02:00
expect ( subject . cloned_from_library ) . to be ( false )
2018-04-24 15:23:07 +02:00
cloned_procedure = subject
cloned_procedure . parent_procedure_id = nil
2023-03-07 18:16:28 +01:00
expect ( cloned_procedure ) . to have_same_attributes_as ( procedure , except : [
" path " , " draft_revision_id " , " service_id " , 'estimated_dossiers_count' ,
2023-04-21 11:19:09 +02:00
" duree_conservation_etendue_par_ds " , " duree_conservation_dossiers_dans_ds " , 'max_duree_conservation_dossiers_dans_ds' ,
2024-05-07 10:16:44 +02:00
" defaut_groupe_instructeur_id " , " template "
2023-03-07 18:16:28 +01:00
] )
2018-04-12 18:35:13 +02:00
end
2022-05-27 18:51:06 +02:00
context 'which is opendata' do
let ( :opendata ) { false }
it 'should keep opendata for same admin' do
expect ( subject . opendata ) . to be_falsy
end
end
2019-01-10 13:34:47 +01:00
context 'when the procedure is cloned from the library' do
2018-04-12 18:35:13 +02:00
let ( :from_library ) { true }
2019-01-29 11:49:28 +01:00
it 'should set cloned_from_library to true' do
expect ( subject . cloned_from_library ) . to be ( true )
end
2018-12-19 15:35:07 +01:00
it 'should set service_id to nil' do
expect ( subject . service ) . to eq ( nil )
end
2019-01-10 13:43:22 +01:00
it 'should discard old pj information' do
2022-11-16 12:46:33 +01:00
subject . draft_revision . types_de_champ_public . each do | stc |
2019-01-10 13:43:22 +01:00
expect ( stc . old_pj ) . to be_nil
end
end
2019-01-29 11:49:28 +01:00
it 'should have one administrateur' do
expect ( subject . administrateurs ) . to eq ( [ administrateur ] )
end
2019-08-27 15:05:56 +02:00
it 'should set ask_birthday to false' do
expect ( subject . ask_birthday? ) . to eq ( false )
end
end
context 'when the procedure is cloned from the library' do
let ( :procedure ) { create ( :procedure , received_mail : received_mail , service : service , ask_birthday : true ) }
it 'should set ask_birthday to false' do
expect ( subject . ask_birthday? ) . to eq ( false )
end
2017-03-07 18:19:48 +01:00
end
2022-08-17 08:47:19 +02:00
it 'should skips service_id' do
expect ( subject . service ) . to eq ( nil )
2018-04-23 16:29:32 +02:00
end
2018-12-19 15:35:07 +01:00
context 'when the procedure is cloned to another administrateur' do
let ( :administrateur ) { create ( :administrateur ) }
2022-05-27 18:51:06 +02:00
let ( :opendata ) { false }
2018-12-19 15:35:07 +01:00
2023-04-26 10:52:31 +02:00
context 'and the procedure does not have a groupe with the defaut label' do
before do
procedure . defaut_groupe_instructeur . update! ( label : 'another label' )
end
it " affects the first groupe as the defaut groupe " do
expect ( subject . defaut_groupe_instructeur ) . to eq ( subject . groupe_instructeurs . first )
end
end
2022-08-17 08:47:19 +02:00
it 'should not clone service' do
expect ( subject . service ) . to eq ( nil )
2018-12-19 15:35:07 +01:00
end
2019-01-10 13:43:22 +01:00
2023-08-29 14:41:28 +02:00
context 'with groupe instructeur services' do
it 'should not clone groupe instructeur services' do
expect ( procedure . groupe_instructeurs . last . contact_information ) . not_to eq nil
expect ( subject . groupe_instructeurs . last . contact_information ) . to eq nil
end
end
2019-01-10 13:43:22 +01:00
it 'should discard old pj information' do
2022-11-16 12:46:33 +01:00
subject . draft_revision . types_de_champ_public . each do | stc |
2019-01-10 13:43:22 +01:00
expect ( stc . old_pj ) . to be_nil
end
end
2019-01-29 11:49:28 +01:00
2020-04-29 16:55:52 +02:00
it 'should discard specific api_entreprise_token' do
expect ( subject . read_attribute ( :api_entreprise_token ) ) . to be_nil
end
2022-05-27 18:51:06 +02:00
it 'should reset opendata to true' do
expect ( subject . opendata ) . to be_truthy
end
2019-01-29 11:49:28 +01:00
it 'should have one administrateur' do
expect ( subject . administrateurs ) . to eq ( [ administrateur ] )
end
2020-04-06 17:42:09 +02:00
it " should discard the existing groupe instructeurs " do
expect ( subject . groupe_instructeurs . size ) . not_to eq ( procedure . groupe_instructeurs . size )
expect ( subject . groupe_instructeurs . where ( label : " groupe_1 " ) . first ) . to be nil
end
2021-09-08 16:29:54 +02:00
it " should discard api_particulier_scopes and token " do
expect ( subject . encrypted_api_particulier_token ) . to be_nil
expect ( subject . api_particulier_scopes ) . to be_empty
end
2023-09-21 15:24:17 +02:00
it 'should not route the procedure' do
expect ( subject . routing_enabled ) . to eq ( false )
end
2020-04-06 17:42:09 +02:00
it 'should have a default groupe instructeur' do
expect ( subject . groupe_instructeurs . size ) . to eq ( 1 )
2021-03-03 11:33:10 +01:00
expect ( subject . groupe_instructeurs . first . label ) . to eq ( GroupeInstructeur :: DEFAUT_LABEL )
2020-04-06 17:42:09 +02:00
expect ( subject . groupe_instructeurs . first . instructeurs . size ) . to eq ( 0 )
end
2018-12-19 15:35:07 +01:00
end
2017-03-07 18:19:48 +01:00
it 'should duplicate existing mail_templates' do
expect ( subject . received_mail . attributes . except ( " id " , " procedure_id " , " created_at " , " updated_at " ) ) . to eq procedure . received_mail . attributes . except ( " id " , " procedure_id " , " created_at " , " updated_at " )
expect ( subject . received_mail . id ) . not_to eq procedure . received_mail . id
expect ( subject . received_mail . id ) . not_to be nil
expect ( subject . received_mail . procedure_id ) . not_to eq procedure . received_mail . procedure_id
expect ( subject . received_mail . procedure_id ) . not_to be nil
end
it 'should not duplicate default mail_template' do
2023-11-29 12:59:55 +01:00
expect ( subject . passer_en_construction_email_template . attributes ) . to eq Mails :: InitiatedMail . default_for_procedure ( subject ) . attributes
2016-06-15 11:34:05 +02:00
end
it 'should not duplicate specific related objects' do
expect ( subject . dossiers ) . to eq ( [ ] )
end
2023-03-07 18:16:28 +01:00
it " should reset estimated_dossiers_count " do
expect ( subject . estimated_dossiers_count ) . to eq ( 0 )
end
2018-12-06 13:51:41 +01:00
describe 'should not duplicate lien_notice' do
let ( :procedure ) { create ( :procedure , lien_notice : " http://toto.com " ) }
it { expect ( subject . lien_notice ) . to be_nil }
end
2016-06-15 11:34:05 +02:00
describe 'procedure status is reset' do
2022-04-21 17:39:03 +02:00
let ( :procedure ) { create ( :procedure , :closed , received_mail : received_mail , service : service , auto_archive_on : 3 . weeks . from_now ) }
2018-05-28 14:58:40 +02:00
2019-11-14 09:43:45 +01:00
it 'Not published nor closed' do
expect ( subject . closed_at ) . to be_nil
2017-07-11 14:21:10 +02:00
expect ( subject . published_at ) . to be_nil
2019-12-04 15:45:06 +01:00
expect ( subject . unpublished_at ) . to be_nil
2022-04-21 17:39:03 +02:00
expect ( subject . auto_archive_on ) . to be_nil
2018-05-28 14:58:40 +02:00
expect ( subject . aasm_state ) . to eq " brouillon "
2019-03-06 14:42:27 +01:00
expect ( subject . path ) . not_to be_nil
2016-06-15 11:34:05 +02:00
end
end
2018-11-27 14:52:20 +01:00
it 'should keep types_de_champ ids stable' do
2022-11-16 12:46:33 +01:00
expect ( subject . draft_revision . types_de_champ_public . first . id ) . not_to eq ( procedure . draft_revision . types_de_champ_public . first . id )
expect ( subject . draft_revision . types_de_champ_public . first . stable_id ) . to eq ( procedure . draft_revision . types_de_champ_public . first . id )
2018-11-27 14:52:20 +01:00
end
2019-03-26 11:00:24 +01:00
it 'should duplicate piece_justificative_template on a type_de_champ' do
2022-11-16 12:46:33 +01:00
expect ( subject . draft_revision . types_de_champ_public . where ( type_champ : " piece_justificative " ) . first . piece_justificative_template . attached? ) . to be_truthy
2019-03-26 11:00:24 +01:00
end
2019-03-28 17:17:29 +01:00
context 'with a notice attached' do
let ( :procedure ) { create ( :procedure , :with_notice , received_mail : received_mail , service : service ) }
it 'should duplicate notice' do
2022-11-10 13:01:15 +01:00
expect ( subject . notice . attached? ) . to be_truthy
expect ( subject . notice . attachment ) . not_to eq ( procedure . notice . attachment )
expect ( subject . notice . attachment . blob ) . to eq ( procedure . notice . attachment . blob )
subject . notice . attach ( logo )
subject . reload
procedure . reload
expect ( subject . notice . attached? ) . to be_truthy
expect ( subject . notice . attachment . blob ) . not_to eq ( procedure . notice . attachment . blob )
subject . notice . purge
subject . reload
procedure . reload
expect ( subject . notice . attached? ) . to be_falsey
expect ( procedure . notice . attached? ) . to be_truthy
2019-03-28 17:17:29 +01:00
end
end
context 'with a deliberation attached' do
let ( :procedure ) { create ( :procedure , :with_deliberation , received_mail : received_mail , service : service ) }
it 'should duplicate deliberation' do
expect ( subject . deliberation . attached? ) . to be true
end
end
2020-02-25 13:47:44 +01:00
context 'with canonical procedure' do
let ( :canonical_procedure ) { create ( :procedure ) }
let ( :procedure ) { create ( :procedure , canonical_procedure : canonical_procedure , received_mail : received_mail , service : service ) }
it 'do not clone canonical procedure' do
expect ( subject . canonical_procedure ) . to be_nil
end
end
2023-11-07 14:53:32 +01:00
describe 'feature flag' do
context 'with a feature flag enabled' do
before do
2024-03-02 16:38:00 +01:00
Flipper . enable ( :dossier_pdf_vide , procedure )
2023-11-07 14:53:32 +01:00
end
it 'should enable feature' do
2024-03-02 16:38:00 +01:00
expect ( subject . feature_enabled? ( :dossier_pdf_vide ) ) . to be true
expect ( Flipper . feature ( :dossier_pdf_vide ) . enabled_gate_names ) . to include ( :actor )
end
end
context 'with feature flag is fully enabled' do
before do
Flipper . enable ( :dossier_pdf_vide )
end
it 'should not clone feature for actor' do
expect ( subject . feature_enabled? ( :dossier_pdf_vide ) ) . to be true
expect ( Flipper . feature ( :dossier_pdf_vide ) . enabled_gate_names ) . not_to include ( :actor )
2023-11-07 14:53:32 +01:00
end
end
context 'with a feature flag disabled' do
before do
2024-03-02 16:38:00 +01:00
Flipper . disable ( :dossier_pdf_vide , procedure )
2023-11-07 14:53:32 +01:00
end
it 'should not enable feature' do
2024-03-02 16:38:00 +01:00
expect ( subject . feature_enabled? ( :dossier_pdf_vide ) ) . to be false
2023-11-07 14:53:32 +01:00
end
end
end
2016-06-15 11:34:05 +02:00
end
2016-06-24 16:41:44 +02:00
2017-06-27 13:48:27 +02:00
describe '#publish!' do
2022-09-27 19:22:42 +02:00
let ( :procedure ) { create ( :procedure , path : 'example-path' , zones : [ create ( :zone ) ] ) }
2018-10-25 15:11:12 +02:00
let ( :now ) { Time . zone . now . beginning_of_minute }
2017-06-27 13:48:27 +02:00
2020-01-07 17:44:45 +01:00
context 'when publishing a new procedure' do
2019-12-04 15:45:06 +01:00
before do
2020-01-07 17:44:45 +01:00
Timecop . freeze ( now ) do
procedure . publish!
end
2019-12-04 15:45:06 +01:00
end
2020-01-07 17:44:45 +01:00
it 'no reference to the canonical procedure on the published procedure' do
2019-12-04 16:58:36 +01:00
expect ( procedure . canonical_procedure ) . to be_nil
2020-01-07 17:44:45 +01:00
end
it 'changes the procedure state to published' do
2019-12-04 15:45:06 +01:00
expect ( procedure . closed_at ) . to be_nil
expect ( procedure . published_at ) . to eq ( now )
expect ( Procedure . find_by ( path : " example-path " ) ) . to eq ( procedure )
expect ( Procedure . find_by ( path : " example-path " ) . administrateurs ) . to eq ( procedure . administrateurs )
end
2020-07-08 19:11:03 +02:00
it 'creates a new draft revision' do
expect ( procedure . published_revision ) . not_to be_nil
expect ( procedure . draft_revision ) . not_to be_nil
expect ( procedure . revisions . count ) . to eq ( 2 )
expect ( procedure . revisions ) . to eq ( [ procedure . published_revision , procedure . draft_revision ] )
end
2019-12-04 15:45:06 +01:00
end
2019-12-04 16:58:36 +01:00
2020-01-07 17:44:45 +01:00
context 'when publishing over a previous canonical procedure' do
2019-12-04 16:58:36 +01:00
let ( :canonical_procedure ) { create ( :procedure , :published ) }
before do
2020-01-07 17:44:45 +01:00
Timecop . freeze ( now ) do
procedure . publish! ( canonical_procedure )
end
2019-12-04 16:58:36 +01:00
end
2020-01-07 17:44:45 +01:00
it 'references the canonical procedure on the published procedure' do
2019-12-04 16:58:36 +01:00
expect ( procedure . canonical_procedure ) . to eq ( canonical_procedure )
2020-01-07 17:44:45 +01:00
end
it 'changes the procedure state to published' do
2019-12-04 16:58:36 +01:00
expect ( procedure . closed_at ) . to be_nil
expect ( procedure . published_at ) . to eq ( now )
end
end
2019-12-04 15:45:06 +01:00
end
describe " # publish_or_reopen! " do
2019-12-04 16:58:36 +01:00
let ( :canonical_procedure ) { create ( :procedure , :published ) }
let ( :administrateur ) { canonical_procedure . administrateurs . first }
2019-12-04 15:45:06 +01:00
2022-09-27 19:22:42 +02:00
let ( :procedure ) { create ( :procedure , administrateurs : [ administrateur ] , zones : [ create ( :zone ) ] ) }
2019-12-04 15:45:06 +01:00
let ( :now ) { Time . zone . now . beginning_of_minute }
2020-01-07 17:44:45 +01:00
context 'when publishing over a previous canonical procedure' do
2019-12-04 16:58:36 +01:00
before do
procedure . path = canonical_procedure . path
2020-01-07 17:44:45 +01:00
Timecop . freeze ( now ) do
procedure . publish_or_reopen! ( administrateur )
end
2021-03-09 11:21:30 +01:00
procedure . reload
2019-12-04 16:58:36 +01:00
canonical_procedure . reload
end
2020-01-07 17:44:45 +01:00
it 'references the canonical procedure on the published procedure' do
2019-12-04 16:58:36 +01:00
expect ( procedure . canonical_procedure ) . to eq ( canonical_procedure )
2020-01-07 17:44:45 +01:00
end
it 'changes the procedure state to published' do
2019-12-04 16:58:36 +01:00
expect ( procedure . closed_at ) . to be_nil
expect ( procedure . published_at ) . to eq ( now )
2020-01-07 17:44:45 +01:00
end
it 'unpublishes the canonical procedure' do
2019-12-04 16:58:36 +01:00
expect ( canonical_procedure . unpublished_at ) . to eq ( now )
end
2020-07-08 19:11:03 +02:00
it 'creates a new draft revision' do
expect ( procedure . published_revision ) . not_to be_nil
expect ( procedure . draft_revision ) . not_to be_nil
expect ( procedure . revisions . count ) . to eq ( 2 )
expect ( procedure . revisions ) . to eq ( [ procedure . published_revision , procedure . draft_revision ] )
2021-05-25 15:12:30 +02:00
expect ( procedure . published_revision . published_at ) . to eq ( now )
2020-07-08 19:11:03 +02:00
end
2019-12-04 16:58:36 +01:00
end
2020-01-07 17:44:45 +01:00
context 'when publishing over a previous procedure with canonical procedure' do
2019-12-04 16:58:36 +01:00
let ( :canonical_procedure ) { create ( :procedure , :closed ) }
let ( :parent_procedure ) { create ( :procedure , :published , administrateurs : [ administrateur ] ) }
2019-12-04 15:45:06 +01:00
before do
2019-12-04 16:58:36 +01:00
parent_procedure . update! ( path : canonical_procedure . path , canonical_procedure : canonical_procedure )
procedure . path = canonical_procedure . path
2020-01-07 17:44:45 +01:00
Timecop . freeze ( now ) do
procedure . publish_or_reopen! ( administrateur )
end
2019-12-04 16:58:36 +01:00
parent_procedure . reload
2019-12-04 15:45:06 +01:00
end
2020-01-07 17:44:45 +01:00
it 'references the canonical procedure on the published procedure' do
expect ( procedure . canonical_procedure ) . to eq ( canonical_procedure )
end
it 'changes the procedure state to published' do
2019-12-04 16:58:36 +01:00
expect ( procedure . canonical_procedure ) . to eq ( canonical_procedure )
2019-12-04 15:45:06 +01:00
expect ( procedure . closed_at ) . to be_nil
expect ( procedure . published_at ) . to eq ( now )
2021-05-25 15:12:30 +02:00
expect ( procedure . published_revision . published_at ) . to eq ( now )
2020-01-07 17:44:45 +01:00
end
it 'unpublishes parent procedure' do
2019-12-04 16:58:36 +01:00
expect ( parent_procedure . unpublished_at ) . to eq ( now )
2019-12-04 15:45:06 +01:00
end
end
2021-05-25 15:12:30 +02:00
context 'when republishing a previously closed procedure' do
let ( :procedure ) { create ( :procedure , :published , administrateurs : [ administrateur ] ) }
before do
procedure . close!
Timecop . freeze ( now ) do
procedure . publish_or_reopen! ( administrateur )
end
end
it 'changes the procedure state to published' do
expect ( procedure . closed_at ) . to be_nil
expect ( procedure . published_at ) . to eq ( now )
expect ( procedure . published_revision . published_at ) . not_to eq ( now )
end
2021-06-01 10:49:06 +02:00
it " doesn't create a new revision " do
2021-05-25 15:12:30 +02:00
expect ( procedure . published_revision ) . not_to be_nil
expect ( procedure . draft_revision ) . not_to be_nil
expect ( procedure . revisions . count ) . to eq ( 2 )
expect ( procedure . revisions ) . to eq ( [ procedure . published_revision , procedure . draft_revision ] )
end
end
2019-12-04 15:45:06 +01:00
end
2021-11-23 13:27:28 +01:00
describe " # publish_revision! " do
let ( :procedure ) { create ( :procedure , :published ) }
let ( :tdc_attributes ) { { type_champ : :number , libelle : 'libelle 1' } }
let ( :publication_date ) { Time . zone . local ( 2021 , 1 , 1 , 12 , 00 , 00 ) }
before do
procedure . draft_revision . add_type_de_champ ( tdc_attributes )
end
subject do
Timecop . freeze ( publication_date ) do
procedure . publish_revision!
end
end
it 'publishes the new revision' do
subject
expect ( procedure . published_revision ) . to be_present
expect ( procedure . published_revision . published_at ) . to eq ( publication_date )
2022-04-28 14:25:49 +02:00
expect ( procedure . published_revision . types_de_champ_public . first . libelle ) . to eq ( 'libelle 1' )
2021-11-23 13:27:28 +01:00
end
it 'creates a new draft revision' do
expect { subject } . to change ( ProcedureRevision , :count ) . by ( 1 )
expect ( procedure . draft_revision ) . to be_present
2022-04-28 14:25:49 +02:00
expect ( procedure . draft_revision . revision_types_de_champ_public ) . to be_present
expect ( procedure . draft_revision . types_de_champ_public ) . to be_present
expect ( procedure . draft_revision . types_de_champ_public . first . libelle ) . to eq ( 'libelle 1' )
2021-11-23 13:27:28 +01:00
end
context 'when the procedure has dossiers' do
let ( :dossier_draft ) { create ( :dossier , :brouillon , procedure : procedure ) }
let ( :dossier_submitted ) { create ( :dossier , :en_construction , procedure : procedure ) }
2022-02-11 18:23:46 +01:00
let ( :dossier_termine ) { create ( :dossier , :accepte , procedure : procedure ) }
2021-11-23 13:27:28 +01:00
2022-02-11 18:23:46 +01:00
before { [ dossier_draft , dossier_submitted , dossier_termine ] }
2021-11-23 13:27:28 +01:00
it 'enqueues rebase jobs for draft dossiers' do
subject
expect ( DossierRebaseJob ) . to have_been_enqueued . with ( dossier_draft )
2022-02-11 18:23:46 +01:00
expect ( DossierRebaseJob ) . to have_been_enqueued . with ( dossier_submitted )
expect ( DossierRebaseJob ) . not_to have_been_enqueued . with ( dossier_termine )
2021-11-23 13:27:28 +01:00
end
end
end
2022-07-06 12:37:08 +02:00
describe " # reset_draft_revision! " do
let ( :procedure ) { create ( :procedure ) }
let ( :tdc_attributes ) { { type_champ : :number , libelle : 'libelle 1' } }
let ( :publication_date ) { Time . zone . local ( 2021 , 1 , 1 , 12 , 00 , 00 ) }
context " brouillon procedure " do
it " should not reset draft revision " do
procedure . draft_revision . add_type_de_champ ( tdc_attributes )
previous_draft_revision = procedure . draft_revision
procedure . reset_draft_revision!
expect ( procedure . draft_revision ) . to eq ( previous_draft_revision )
end
end
context " published procedure " do
2022-10-07 14:13:42 +02:00
let ( :procedure ) do
create (
:procedure ,
:published ,
2022-11-09 12:09:24 +01:00
attestation_template : build ( :attestation_template ) ,
2022-10-07 14:13:42 +02:00
dossier_submitted_message : create ( :dossier_submitted_message ) ,
types_de_champ_public : [ { type : :text , libelle : 'published tdc' } ]
)
end
2022-07-06 12:37:08 +02:00
it " should reset draft revision " do
procedure . draft_revision . add_type_de_champ ( tdc_attributes )
previous_draft_revision = procedure . draft_revision
2022-11-09 12:09:24 +01:00
previous_attestation_template = procedure . attestation_template
2022-07-06 15:55:09 +02:00
previous_dossier_submitted_message = previous_draft_revision . dossier_submitted_message
2022-07-06 12:37:08 +02:00
expect ( procedure . draft_changed? ) . to be_truthy
procedure . reset_draft_revision!
expect ( procedure . draft_changed? ) . to be_falsey
expect ( procedure . draft_revision ) . not_to eq ( previous_draft_revision )
expect { previous_draft_revision . reload } . to raise_error ( ActiveRecord :: RecordNotFound )
2022-11-09 12:09:24 +01:00
expect ( procedure . attestation_template ) . to eq ( previous_attestation_template )
2022-07-06 15:55:09 +02:00
expect ( procedure . draft_revision . dossier_submitted_message ) . to eq ( previous_dossier_submitted_message )
2022-07-06 12:37:08 +02:00
end
2022-10-07 14:13:42 +02:00
it " should erase orphan tdc " do
published_tdc = procedure . published_revision . types_de_champ . first
draft_tdc = procedure . draft_revision . add_type_de_champ ( tdc_attributes )
procedure . reset_draft_revision!
2023-07-06 10:56:01 +02:00
expect { published_tdc . reload } . not_to raise_error
2022-10-07 14:13:42 +02:00
expect { draft_tdc . reload } . to raise_error ( ActiveRecord :: RecordNotFound )
end
2022-07-06 12:37:08 +02:00
end
end
2019-12-04 15:45:06 +01:00
describe " # unpublish! " do
let ( :procedure ) { create ( :procedure , :published ) }
let ( :now ) { Time . zone . now . beginning_of_minute }
2017-06-27 13:48:27 +02:00
before do
2020-01-07 17:44:45 +01:00
Timecop . freeze ( now ) do
procedure . unpublish!
end
2017-06-27 13:48:27 +02:00
end
2019-12-04 15:45:06 +01:00
it {
expect ( procedure . closed_at ) . to eq ( nil )
expect ( procedure . published_at ) . not_to be_nil
expect ( procedure . unpublished_at ) . to eq ( now )
}
2020-07-08 19:11:03 +02:00
it 'sets published revision' do
expect ( procedure . published_revision ) . not_to be_nil
expect ( procedure . draft_revision ) . not_to be_nil
expect ( procedure . revisions . count ) . to eq ( 2 )
2020-07-22 11:56:19 +02:00
expect ( procedure . revisions ) . to eq ( [ procedure . published_revision , procedure . draft_revision ] )
2020-07-08 19:11:03 +02:00
end
2017-06-27 13:48:27 +02:00
end
2017-07-13 15:21:52 +02:00
describe " # brouillon? " do
2019-12-04 15:45:06 +01:00
let ( :procedure_brouillon ) { build ( :procedure ) }
let ( :procedure_publiee ) { build ( :procedure , :published ) }
let ( :procedure_close ) { build ( :procedure , :closed ) }
let ( :procedure_depubliee ) { build ( :procedure , :unpublished ) }
2017-07-13 15:21:52 +02:00
it { expect ( procedure_brouillon . brouillon? ) . to be_truthy }
it { expect ( procedure_publiee . brouillon? ) . to be_falsey }
2019-11-14 09:43:45 +01:00
it { expect ( procedure_close . brouillon? ) . to be_falsey }
2019-12-04 15:45:06 +01:00
it { expect ( procedure_depubliee . brouillon? ) . to be_falsey }
2017-07-13 15:21:52 +02:00
end
2017-07-13 15:14:28 +02:00
describe " # publiee? " do
2019-12-04 15:45:06 +01:00
let ( :procedure_brouillon ) { build ( :procedure ) }
let ( :procedure_publiee ) { build ( :procedure , :published ) }
let ( :procedure_close ) { build ( :procedure , :closed ) }
let ( :procedure_depubliee ) { build ( :procedure , :unpublished ) }
2017-07-13 15:14:28 +02:00
it { expect ( procedure_brouillon . publiee? ) . to be_falsey }
it { expect ( procedure_publiee . publiee? ) . to be_truthy }
2019-11-14 09:43:45 +01:00
it { expect ( procedure_close . publiee? ) . to be_falsey }
2019-12-04 15:45:06 +01:00
it { expect ( procedure_depubliee . publiee? ) . to be_falsey }
2017-07-13 15:14:28 +02:00
end
2019-11-14 09:43:45 +01:00
describe " # close? " do
2019-12-04 15:45:06 +01:00
let ( :procedure_brouillon ) { build ( :procedure ) }
let ( :procedure_publiee ) { build ( :procedure , :published ) }
let ( :procedure_close ) { build ( :procedure , :closed ) }
let ( :procedure_depubliee ) { build ( :procedure , :unpublished ) }
2017-07-13 15:09:25 +02:00
2019-11-14 09:43:45 +01:00
it { expect ( procedure_brouillon . close? ) . to be_falsey }
it { expect ( procedure_publiee . close? ) . to be_falsey }
it { expect ( procedure_close . close? ) . to be_truthy }
2019-12-04 15:45:06 +01:00
it { expect ( procedure_depubliee . close? ) . to be_falsey }
end
describe " # depubliee? " do
let ( :procedure_brouillon ) { build ( :procedure ) }
let ( :procedure_publiee ) { build ( :procedure , :published ) }
let ( :procedure_close ) { build ( :procedure , :closed ) }
let ( :procedure_depubliee ) { build ( :procedure , :unpublished ) }
it { expect ( procedure_brouillon . depubliee? ) . to be_falsey }
it { expect ( procedure_publiee . depubliee? ) . to be_falsey }
it { expect ( procedure_close . depubliee? ) . to be_falsey }
it { expect ( procedure_depubliee . depubliee? ) . to be_truthy }
2017-07-13 15:20:24 +02:00
end
2019-12-04 15:45:06 +01:00
describe " # locked? " do
let ( :procedure_brouillon ) { build ( :procedure ) }
let ( :procedure_publiee ) { build ( :procedure , :published ) }
let ( :procedure_close ) { build ( :procedure , :closed ) }
let ( :procedure_depubliee ) { build ( :procedure , :unpublished ) }
2017-07-13 15:20:24 +02:00
2019-12-04 15:45:06 +01:00
it { expect ( procedure_brouillon . locked? ) . to be_falsey }
it { expect ( procedure_publiee . locked? ) . to be_truthy }
it { expect ( procedure_close . locked? ) . to be_truthy }
it { expect ( procedure_depubliee . locked? ) . to be_truthy }
2017-07-13 15:09:25 +02:00
end
2019-12-04 15:45:06 +01:00
describe 'close' do
2016-06-24 16:41:44 +02:00
let ( :procedure ) { create ( :procedure , :published ) }
2018-10-25 15:11:12 +02:00
let ( :now ) { Time . zone . now . beginning_of_minute }
2016-06-24 16:41:44 +02:00
before do
2020-01-07 17:44:45 +01:00
Timecop . freeze ( now ) do
procedure . close!
end
2016-06-24 16:41:44 +02:00
procedure . reload
end
2019-11-14 09:43:45 +01:00
it { expect ( procedure . close? ) . to be_truthy }
it { expect ( procedure . closed_at ) . to eq ( now ) }
2020-07-08 19:11:03 +02:00
it 'sets published revision' do
expect ( procedure . published_revision ) . not_to be_nil
expect ( procedure . draft_revision ) . not_to be_nil
expect ( procedure . revisions . count ) . to eq ( 2 )
2020-07-22 11:56:19 +02:00
expect ( procedure . revisions ) . to eq ( [ procedure . published_revision , procedure . draft_revision ] )
2020-07-08 19:11:03 +02:00
end
2016-06-24 16:41:44 +02:00
end
2016-07-22 15:06:30 +02:00
2019-07-05 14:38:20 +02:00
describe 'path_customized?' do
let ( :procedure ) { create :procedure }
subject { procedure . path_customized? }
context 'when the path is still the default' do
it { is_expected . to be_falsey }
end
context 'when the path has been changed' do
before { procedure . path = 'custom_path' }
it { is_expected . to be_truthy }
end
end
2016-07-22 15:06:30 +02:00
describe 'total_dossier' do
let ( :procedure ) { create :procedure }
before do
2018-08-28 14:10:55 +02:00
create :dossier , procedure : procedure , state : Dossier . states . fetch ( :en_construction )
create :dossier , procedure : procedure , state : Dossier . states . fetch ( :brouillon )
create :dossier , procedure : procedure , state : Dossier . states . fetch ( :en_construction )
2016-07-22 15:06:30 +02:00
end
subject { procedure . total_dossier }
it { is_expected . to eq 2 }
end
2017-04-05 11:34:16 +02:00
2019-07-05 14:38:20 +02:00
describe 'suggested_path' do
2022-09-27 19:22:42 +02:00
let ( :procedure ) { create ( :procedure , aasm_state : :publiee , libelle : 'Inscription au Collège' , zones : [ create ( :zone ) ] ) }
2017-04-14 11:34:53 +02:00
2019-07-05 14:38:20 +02:00
subject { procedure . suggested_path ( procedure . administrateurs . first ) }
context 'when the path has been customized' do
before { procedure . path = 'custom_path' }
it { is_expected . to eq 'custom_path' }
end
2017-04-14 11:34:53 +02:00
2019-07-05 14:38:20 +02:00
context 'when the suggestion does not conflict' do
it { is_expected . to eq 'inscription-au-college' }
end
context 'when the suggestion conflicts with one procedure' do
before do
2022-09-27 19:22:42 +02:00
create ( :procedure , aasm_state : :publiee , path : 'inscription-au-college' , zones : [ create ( :zone ) ] )
2019-07-05 14:38:20 +02:00
end
it { is_expected . to eq 'inscription-au-college-2' }
end
context 'when the suggestion conflicts with several procedures' do
before do
2022-09-27 19:22:42 +02:00
create ( :procedure , aasm_state : :publiee , path : 'inscription-au-college' , zones : [ create ( :zone ) ] )
create ( :procedure , aasm_state : :publiee , path : 'inscription-au-college-2' , zones : [ create ( :zone ) ] )
2019-07-05 14:38:20 +02:00
end
it { is_expected . to eq 'inscription-au-college-3' }
end
context 'when the suggestion conflicts with another procedure of the same admin' do
before do
2022-09-27 19:22:42 +02:00
create ( :procedure , aasm_state : :publiee , path : 'inscription-au-college' , administrateurs : procedure . administrateurs , zones : [ create ( :zone ) ] )
2019-07-05 14:38:20 +02:00
end
2024-03-14 17:17:47 +01:00
it { is_expected . to eq 'inscription-au-college-2' }
2019-07-05 14:38:20 +02:00
end
2017-04-14 11:34:53 +02:00
end
2017-06-27 14:22:43 +02:00
describe " .default_scope " do
let! ( :procedure ) { create ( :procedure , hidden_at : hidden_at ) }
context " when hidden_at is nil " do
let ( :hidden_at ) { nil }
it { expect ( Procedure . count ) . to eq ( 1 ) }
it { expect ( Procedure . all ) . to include ( procedure ) }
end
context " when hidden_at is not nil " do
let ( :hidden_at ) { 2 . days . ago }
it { expect ( Procedure . count ) . to eq ( 0 ) }
it { expect { Procedure . find ( procedure . id ) } . to raise_error ( ActiveRecord :: RecordNotFound ) }
end
end
2017-06-27 15:26:40 +02:00
2020-03-26 09:08:52 +01:00
describe " # discard_and_keep_track! " do
2020-11-05 15:09:11 +01:00
let ( :super_admin ) { create ( :super_admin ) }
2017-06-27 15:26:40 +02:00
let ( :procedure ) { create ( :procedure ) }
let! ( :dossier ) { create ( :dossier , procedure : procedure ) }
let! ( :dossier2 ) { create ( :dossier , procedure : procedure ) }
2019-08-06 11:02:54 +02:00
let ( :instructeur ) { create ( :instructeur ) }
2017-06-27 15:26:40 +02:00
it { expect ( Dossier . count ) . to eq ( 2 ) }
it { expect ( Dossier . all ) . to include ( dossier , dossier2 ) }
2020-03-26 09:08:52 +01:00
context " when discarding procedure " do
2017-06-27 15:26:40 +02:00
before do
2019-08-06 11:02:54 +02:00
instructeur . followed_dossiers << dossier
2020-11-05 15:09:11 +01:00
procedure . discard_and_keep_track! ( super_admin )
2019-08-06 11:02:54 +02:00
instructeur . reload
2017-06-27 15:26:40 +02:00
end
it { expect ( procedure . dossiers . count ) . to eq ( 0 ) }
it { expect ( Dossier . count ) . to eq ( 0 ) }
2019-08-06 11:02:54 +02:00
it { expect ( instructeur . followed_dossiers ) . not_to include ( dossier ) }
2017-06-27 15:26:40 +02:00
end
end
2017-10-02 17:03:38 +02:00
2017-09-27 15:16:07 +02:00
describe " .default_sort " do
2018-09-20 17:02:28 +02:00
it { expect ( Procedure . default_sort ) . to eq ( { " table " = > " self " , " column " = > " id " , " order " = > " desc " } ) }
2017-09-27 15:16:07 +02:00
end
2018-03-16 12:00:01 +01:00
2018-07-03 20:18:49 +02:00
describe " # organisation_name " do
subject { procedure . organisation_name }
context 'when the procedure has a service (and no organization)' do
let ( :procedure ) { create ( :procedure , :with_service , organisation : nil ) }
it { is_expected . to eq procedure . service . nom }
end
context 'when the procedure has an organization (and no service)' do
let ( :procedure ) { create ( :procedure , organisation : 'DDT des Vosges' , service : nil ) }
it { is_expected . to eq procedure . organisation }
end
end
2018-06-01 11:06:12 +02:00
describe '#juridique_required' do
it 'automatically jumps to true once cadre_juridique or deliberation have been set' do
p = create (
:procedure ,
juridique_required : false ,
2018-10-01 14:08:12 +02:00
cadre_juridique : nil
2018-06-01 11:06:12 +02:00
)
expect ( p . juridique_required ) . to be_falsey
p . update ( cadre_juridique : 'cadre' )
expect ( p . juridique_required ) . to be_truthy
p . update ( cadre_juridique : nil )
expect ( p . juridique_required ) . to be_truthy
p . update_columns ( cadre_juridique : nil , juridique_required : false )
p . reload
expect ( p . juridique_required ) . to be_falsey
2020-06-30 17:53:54 +02:00
@deliberation = fixture_file_upload ( 'spec/fixtures/files/file.pdf' , 'application/pdf' )
2020-03-16 17:55:16 +01:00
p . update ( deliberation : @deliberation )
p . reload
2018-06-01 11:06:12 +02:00
expect ( p . juridique_required ) . to be_truthy
end
end
2018-09-27 14:24:42 +02:00
2019-08-27 15:28:02 +02:00
describe '.ensure_a_groupe_instructeur_exists' do
2023-04-07 14:30:24 +02:00
let ( :procedure ) { create ( :procedure , groupe_instructeurs : [ ] ) }
2019-08-27 15:28:02 +02:00
2023-04-07 14:30:24 +02:00
it do
expect ( procedure . groupe_instructeurs . count ) . to eq ( 1 )
expect ( procedure . groupe_instructeurs . first . label ) . to eq ( GroupeInstructeur :: DEFAUT_LABEL )
expect ( procedure . defaut_groupe_instructeur_id ) . not_to be_nil
end
2019-08-27 15:28:02 +02:00
end
2019-08-21 13:53:53 +02:00
describe '.missing_instructeurs?' do
let! ( :procedure ) { create ( :procedure ) }
subject { procedure . missing_instructeurs? }
it { is_expected . to be true }
context 'when an instructeur is assign to this procedure' do
let! ( :instructeur ) { create ( :instructeur ) }
before { instructeur . assign_to_procedure ( procedure ) }
it { is_expected . to be false }
end
end
2021-04-13 20:24:12 +02:00
2022-09-27 19:46:50 +02:00
describe '.missing_zones?' do
before do
2022-12-20 09:22:28 +01:00
Rails . application . config . ds_zonage_enabled = true
2022-09-27 19:46:50 +02:00
end
let ( :procedure ) { create ( :procedure , zones : [ ] ) }
subject { procedure . missing_zones? }
it { is_expected . to be true }
context 'when a procedure has zones' do
let ( :zone ) { create ( :zone ) }
before { procedure . zones << zone }
it { is_expected . to be false }
end
end
describe '.missing_steps' do
before do
Flipper . enable :zonage
end
subject { procedure . missing_steps . include? ( step ) }
context 'without zone' do
let ( :procedure ) { create ( :procedure , zones : [ ] ) }
let ( :step ) { :zones }
it { is_expected . to be_truthy }
end
context 'with zone' do
let ( :procedure ) { create ( :procedure , zones : [ create ( :zone ) ] ) }
let ( :step ) { :zones }
it { is_expected . to be_falsey }
end
context 'without service' do
let ( :procedure ) { create ( :procedure , service : nil ) }
let ( :step ) { :service }
it { is_expected . to be_truthy }
end
context 'with service' do
let ( :procedure ) { create ( :procedure ) }
let ( :step ) { :service }
it { is_expected . to be_truthy }
end
end
2021-04-13 20:24:12 +02:00
describe " # destroy " do
2021-12-09 12:20:22 +01:00
let ( :procedure ) { create ( :procedure , :closed , :with_type_de_champ , :with_bulk_message ) }
2021-05-27 12:17:10 +02:00
before do
procedure . discard!
end
2021-04-13 20:24:12 +02:00
it " can destroy procedure " do
2021-05-27 12:17:10 +02:00
expect ( procedure . revisions . count ) . to eq ( 2 )
2021-04-13 20:24:12 +02:00
expect ( procedure . destroy ) . to be_truthy
end
end
2021-06-02 11:10:44 +02:00
describe '#average_dossier_weight' do
2024-07-01 15:31:32 +02:00
let ( :procedure ) { create ( :procedure , :published , types_de_champ_public : [ { type : :piece_justificative } ] ) }
2021-06-02 11:10:44 +02:00
before do
2024-07-01 15:31:32 +02:00
create ( :dossier , :accepte , :with_populated_champs , procedure : )
create ( :dossier , :accepte , :with_populated_champs , procedure : )
create ( :dossier , :accepte , :with_populated_champs , procedure : )
ActiveStorage :: Blob . first . update! ( byte_size : 4 )
ActiveStorage :: Blob . second . update! ( byte_size : 5 )
ActiveStorage :: Blob . third . update! ( byte_size : 6 )
2021-06-02 11:10:44 +02:00
end
it 'estimates average dossier weight' do
2021-07-06 15:00:33 +02:00
expect ( procedure . reload . average_dossier_weight ) . to eq ( 5 + Procedure :: MIN_WEIGHT )
2021-06-02 11:10:44 +02:00
end
2021-05-11 13:01:42 +02:00
end
2021-06-02 11:10:44 +02:00
2022-04-25 17:38:30 +02:00
describe 'lien_dpo' do
it { expect ( build ( :procedure ) . valid? ) . to be ( true ) }
it { expect ( build ( :procedure , lien_dpo : 'dpo@ministere.amere' ) . valid? ) . to be ( true ) }
it { expect ( build ( :procedure , lien_dpo : 'https://legal.fr/contact_dpo' ) . valid? ) . to be ( true ) }
it { expect ( build ( :procedure , lien_dpo : 'askjdlad l akdj asd ' ) . valid? ) . to be ( false ) }
end
2022-07-23 12:29:55 +02:00
describe 'factory' do
let ( :types_de_champ ) { [ { type : :yes_no } , { type : :integer_number } ] }
context 'create' do
let ( :types_de_champ ) { [ { type : :yes_no } , { type : :repetition , children : [ { type : :integer_number } ] } ] }
let ( :procedure ) { create ( :procedure , types_de_champ_public : types_de_champ ) }
context 'with brouillon procedure' do
it do
expect ( procedure . draft_revision . types_de_champ_public . count ) . to eq ( 2 )
expect ( procedure . draft_revision . types_de_champ . count ) . to eq ( 3 )
end
end
context 'with published procedure' do
let ( :procedure ) { create ( :procedure , :published , types_de_champ_public : types_de_champ ) }
it do
expect ( procedure . draft_revision . types_de_champ_public . count ) . to eq ( 2 )
expect ( procedure . draft_revision . types_de_champ . count ) . to eq ( 3 )
expect ( procedure . published_revision . types_de_champ_public . count ) . to eq ( 2 )
expect ( procedure . published_revision . types_de_champ . count ) . to eq ( 3 )
end
end
end
context 'with bouillon procedure' do
let ( :procedure ) { build ( :procedure , types_de_champ_public : types_de_champ , types_de_champ_private : types_de_champ ) }
it do
expect ( procedure . revisions . size ) . to eq ( 1 )
expect ( procedure . draft_revision . types_de_champ . size ) . to eq ( 4 )
expect ( procedure . draft_revision . types_de_champ_public . size ) . to eq ( 2 )
expect ( procedure . published_revision ) . to be_nil
end
end
context 'with published procedure' do
let ( :procedure ) { build ( :procedure , :published , types_de_champ_public : types_de_champ , types_de_champ_private : types_de_champ ) }
it do
expect ( procedure . revisions . size ) . to eq ( 2 )
expect ( procedure . draft_revision . types_de_champ . size ) . to eq ( 4 )
expect ( procedure . draft_revision . types_de_champ_public . size ) . to eq ( 2 )
expect ( procedure . published_revision . types_de_champ . size ) . to eq ( 4 )
expect ( procedure . published_revision . types_de_champ_public . size ) . to eq ( 2 )
end
end
context 'repetition' do
let ( :types_de_champ ) do
[
{ type : :yes_no } ,
{
type : :repetition ,
children : [
{ libelle : 'Nom' , mandatory : true } ,
{ libelle : 'Prénom' , mandatory : true } ,
2024-06-25 17:17:51 +02:00
{ libelle : 'Age' , type : :integer_number , mandatory : false }
2022-07-23 12:29:55 +02:00
]
}
]
end
let ( :revision ) { procedure . draft_revision }
let ( :repetition ) { revision . revision_types_de_champ_public . last }
context 'with bouillon procedure' do
let ( :procedure ) { build ( :procedure , types_de_champ_public : types_de_champ ) }
it do
expect ( revision . types_de_champ . size ) . to eq ( 5 )
expect ( revision . types_de_champ_public . size ) . to eq ( 2 )
expect ( revision . types_de_champ_public . map ( & :type_champ ) ) . to eq ( [ 'yes_no' , 'repetition' ] )
expect ( repetition . revision_types_de_champ . size ) . to eq ( 3 )
expect ( repetition . revision_types_de_champ . map ( & :type_champ ) ) . to eq ( [ 'text' , 'text' , 'integer_number' ] )
expect ( repetition . revision_types_de_champ . map ( & :mandatory? ) ) . to eq ( [ true , true , false ] )
end
end
context 'with published procedure' do
let ( :procedure ) { build ( :procedure , :published , types_de_champ_public : types_de_champ ) }
context 'draft revision' do
it do
expect ( revision . types_de_champ . size ) . to eq ( 5 )
expect ( revision . types_de_champ_public . size ) . to eq ( 2 )
expect ( revision . types_de_champ_public . map ( & :type_champ ) ) . to eq ( [ 'yes_no' , 'repetition' ] )
expect ( repetition . revision_types_de_champ . size ) . to eq ( 3 )
expect ( repetition . revision_types_de_champ . map ( & :type_champ ) ) . to eq ( [ 'text' , 'text' , 'integer_number' ] )
expect ( repetition . revision_types_de_champ . map ( & :mandatory? ) ) . to eq ( [ true , true , false ] )
end
end
context 'published revision' do
let ( :revision ) { procedure . published_revision }
it do
expect ( revision . types_de_champ . size ) . to eq ( 5 )
expect ( revision . types_de_champ_public . size ) . to eq ( 2 )
expect ( revision . types_de_champ_public . map ( & :type_champ ) ) . to eq ( [ 'yes_no' , 'repetition' ] )
expect ( repetition . revision_types_de_champ . size ) . to eq ( 3 )
expect ( repetition . revision_types_de_champ . map ( & :type_champ ) ) . to eq ( [ 'text' , 'text' , 'integer_number' ] )
expect ( repetition . revision_types_de_champ . map ( & :mandatory? ) ) . to eq ( [ true , true , false ] )
end
end
end
end
end
2023-07-06 10:27:29 +02:00
describe 'lien_notice' do
let ( :procedure ) { build ( :procedure , lien_notice : ) }
context 'when empty' do
let ( :lien_notice ) { '' }
it { expect ( procedure . valid? ) . to be_truthy }
end
context 'when valid link' do
let ( :lien_notice ) { 'https://www.demarches-simplifiees.fr' }
it { expect ( procedure . valid? ) . to be_truthy }
end
context 'when valid link with accents' do
let ( :lien_notice ) { 'https://www.démarches-simplifiées.fr' }
it { expect ( procedure . valid? ) . to be_truthy }
end
context 'when not a valid link' do
let ( :lien_notice ) { 'www.démarches-simplifiées.fr' }
it { expect ( procedure . valid? ) . to be_falsey }
end
2023-07-18 15:16:31 +02:00
context 'when an email' do
let ( :lien_notice ) { 'test@demarches-simplifiees.fr' }
it { expect ( procedure . valid? ) . to be_falsey }
end
2023-07-06 10:27:29 +02:00
end
describe 'lien_dpo' do
let ( :procedure ) { build ( :procedure , lien_dpo : ) }
context 'when empty' do
let ( :lien_dpo ) { '' }
it { expect ( procedure . valid? ) . to be_truthy }
end
context 'when valid link' do
let ( :lien_dpo ) { 'https://www.demarches-simplifiees.fr' }
it { expect ( procedure . valid? ) . to be_truthy }
end
context 'when valid link with accents' do
let ( :lien_dpo ) { 'https://www.démarches-simplifiées.fr' }
it { expect ( procedure . valid? ) . to be_truthy }
end
context 'when valid email' do
let ( :lien_dpo ) { 'test@demarches-simplifiees.fr' }
it { expect ( procedure . valid? ) . to be_truthy }
end
2023-07-18 15:16:31 +02:00
context 'when valid email with accents' do
let ( :lien_dpo ) { 'test@démarches-simplifiées.fr' }
it { expect ( procedure . valid? ) . to be_truthy }
end
2023-07-06 10:27:29 +02:00
context 'when not a valid link' do
let ( :lien_dpo ) { 'www.démarches-simplifiées.fr' }
it { expect ( procedure . valid? ) . to be_falsey }
end
end
2023-11-09 16:41:32 +01:00
describe 'extend_conservation_for_dossiers' do
let ( :duree_conservation_dossiers_dans_ds ) { 2 }
let ( :procedure ) { create ( :procedure , duree_conservation_dossiers_dans_ds : ) }
let ( :expiring_dossier_brouillon ) { create ( :dossier , :brouillon , procedure : procedure , brouillon_close_to_expiration_notice_sent_at : duree_conservation_dossiers_dans_ds . months . ago ) }
let ( :expiring_dossier_en_construction ) { create ( :dossier , :en_construction , procedure : procedure , en_construction_close_to_expiration_notice_sent_at : duree_conservation_dossiers_dans_ds . months . ago ) }
let ( :expiring_dossier_en_termine ) { create ( :dossier , :accepte , procedure : procedure , termine_close_to_expiration_notice_sent_at : duree_conservation_dossiers_dans_ds . months . ago ) }
let ( :not_expiring_dossie ) { create ( :dossier , :accepte , procedure : procedure , created_at : duree_conservation_dossiers_dans_ds . months . ago ) }
before do
procedure
expiring_dossier_brouillon
expiring_dossier_en_construction
expiring_dossier_en_termine
not_expiring_dossie
end
context 'when duree_conservation_dossiers_dans_ds does not changes' do
it 'does not enqueues any job' do
expect ( ResetExpiringDossiersJob ) . not_to receive ( :perform_later )
procedure . update! ( libelle : 'does not change duree_conservation_dossiers_dans_ds' )
end
end
context 'when duree_conservation_dossiers_dans_ds decreases' do
it 'calls extend_conservation_for_dossiers' do
expect ( ResetExpiringDossiersJob ) . not_to receive ( :perform_later )
procedure . update ( duree_conservation_dossiers_dans_ds : duree_conservation_dossiers_dans_ds - 1 )
end
end
context 'when duree_conservation_dossiers_dans_ds increases' do
it 'calls extend_conservation_for_dossiers' do
2023-11-28 16:59:41 +01:00
expect ( ResetExpiringDossiersJob ) . to receive ( :perform_later )
2023-11-09 16:41:32 +01:00
procedure . update ( duree_conservation_dossiers_dans_ds : duree_conservation_dossiers_dans_ds + 1 )
end
end
end
2024-02-19 16:08:47 +01:00
describe " # attestation_template " do
let ( :procedure ) { create ( :procedure ) }
2024-05-29 11:31:49 +02:00
subject { procedure . reload }
2024-02-19 16:08:47 +01:00
2024-05-29 11:31:49 +02:00
context " when there is a v2 draft and a v1 " do
2024-02-19 16:08:47 +01:00
before do
create ( :attestation_template , procedure : procedure )
2024-05-29 11:31:49 +02:00
create ( :attestation_template , :v2 , :draft , procedure : procedure )
2024-02-19 16:08:47 +01:00
end
2024-05-29 11:31:49 +02:00
it { expect ( subject . attestation_template . version ) . to eq ( 1 ) }
2024-02-19 16:08:47 +01:00
end
2024-05-29 11:31:49 +02:00
context " when there is only a v1 " do
2024-02-19 16:08:47 +01:00
before do
2024-05-29 11:31:49 +02:00
create ( :attestation_template , procedure : procedure )
2024-02-19 16:08:47 +01:00
end
2024-05-29 11:31:49 +02:00
it { expect ( subject . attestation_template . version ) . to eq ( 1 ) }
2024-02-19 16:08:47 +01:00
end
context " when there is only a v2 " do
before do
create ( :attestation_template , :v2 , procedure : procedure )
end
2024-05-29 11:31:49 +02:00
it { expect ( subject . attestation_template . version ) . to eq ( 2 ) }
end
context " when there is a v2 draft " do
before do
create ( :attestation_template , :v2 , :draft , procedure : procedure )
end
it { expect ( subject . attestation_template ) . to be_nil }
context " and a published " do
before do
create ( :attestation_template , :v2 , :published , procedure : procedure )
end
it { expect ( subject . attestation_template ) . to be_published }
end
2024-02-19 16:08:47 +01:00
end
end
2024-06-05 13:52:18 +02:00
describe " # parsed_latest_zone_labels " do
let! ( :draft_procedure ) { create ( :procedure ) }
let! ( :published_procedure ) { create ( :procedure_with_dossiers , :published , dossiers_count : 2 ) }
let! ( :closed_procedure ) { create ( :procedure , :closed ) }
let! ( :procedure_detail_draft ) { ProcedureDetail . new ( id : draft_procedure . id , latest_zone_labels : '{ "zone1", "zone2" }' ) }
let! ( :procedure_detail_published ) { ProcedureDetail . new ( id : published_procedure . id , latest_zone_labels : '{ "zone3", "zone4" }' ) }
let! ( :procedure_detail_closed ) { ProcedureDetail . new ( id : closed_procedure . id , latest_zone_labels : '{ "zone5", "zone6" }' ) }
context 'with parsed latest zone labels' do
it 'parses the latest zone labels correctly' do
expect ( procedure_detail_draft . parsed_latest_zone_labels ) . to eq ( [ " zone1 " , " zone2 " ] )
expect ( procedure_detail_published . parsed_latest_zone_labels ) . to eq ( [ " zone3 " , " zone4 " ] )
expect ( procedure_detail_closed . parsed_latest_zone_labels ) . to eq ( [ " zone5 " , " zone6 " ] )
end
it 'returns an empty array for invalid JSON' do
procedure_detail_draft . latest_zone_labels = '{ invalid json }'
expect ( procedure_detail_draft . parsed_latest_zone_labels ) . to eq ( [ ] )
end
it 'returns an empty array when latest_zone_labels is nil' do
procedure_detail_draft . latest_zone_labels = nil
expect ( procedure_detail_draft . parsed_latest_zone_labels ) . to eq ( [ ] )
end
it 'returns an empty array when latest_zone_labels is empty' do
procedure_detail_draft . latest_zone_labels = ''
expect ( procedure_detail_draft . parsed_latest_zone_labels ) . to eq ( [ ] )
end
end
end
2021-05-11 13:01:42 +02:00
private
2021-06-02 11:10:44 +02:00
2021-05-11 13:01:42 +02:00
def create_dossier_with_pj_of_size ( size , procedure )
dossier = create ( :dossier , :accepte , procedure : procedure )
create ( :champ_piece_justificative , size : size , dossier : dossier )
end
2015-09-21 17:59:03 +02:00
end