2024-04-29 00:17:15 +02:00
# frozen_string_literal: true
2015-11-03 15:27:49 +01:00
describe Champ do
2021-03-04 12:50:22 +01:00
include ActiveJob :: TestHelper
2024-07-01 15:31:32 +02:00
describe 'mandatory_blank?' do
let ( :type_de_champ ) { build ( :type_de_champ , mandatory : mandatory ) }
2024-10-21 12:24:12 +02:00
let ( :champ ) { Champs :: TextChamp . new ( value : value ) }
2024-07-01 15:31:32 +02:00
let ( :value ) { '' }
let ( :mandatory ) { true }
2015-11-03 10:48:40 +01:00
2024-09-20 14:54:26 +02:00
context 'with champ' do
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( type_de_champ ) }
2024-08-22 17:29:49 +02:00
2024-09-20 14:54:26 +02:00
context 'when mandatory and blank' do
it { expect ( champ . mandatory_blank? ) . to be ( true ) }
end
2024-08-22 17:29:49 +02:00
2024-09-20 14:54:26 +02:00
context 'when carte mandatory and blank' do
let ( :type_de_champ ) { build ( :type_de_champ_carte , mandatory : mandatory ) }
let ( :champ ) { Champs :: CarteChamp . new ( value : value ) }
let ( :value ) { nil }
it { expect ( champ . mandatory_blank? ) . to be ( true ) }
end
context 'when multiple_drop_down_list mandatory and blank' do
let ( :type_de_champ ) { build ( :type_de_champ_multiple_drop_down_list , mandatory : mandatory ) }
let ( :champ ) { Champs :: MultipleDropDownListChamp . new ( value : value ) }
let ( :value ) { '[]' }
it { expect ( champ . mandatory_blank? ) . to be ( true ) }
end
context 'when repetition blank' do
2024-11-04 15:59:58 +01:00
let ( :procedure ) { create ( :procedure , types_de_champ_public : [ { type : :repetition , mandatory : false , children : [ { type : :text } ] } ] ) }
let ( :dossier ) { create ( :dossier , procedure : ) }
let ( :champ ) { dossier . project_champs_public . find ( & :repetition? ) }
2024-09-20 14:54:26 +02:00
it { expect ( champ . blank? ) . to be ( true ) }
end
2024-08-22 17:29:49 +02:00
2024-09-20 14:54:26 +02:00
context 'when not blank' do
let ( :value ) { 'yop' }
it { expect ( champ . mandatory_blank? ) . to be ( false ) }
end
context 'when not mandatory' do
let ( :mandatory ) { false }
it { expect ( champ . mandatory_blank? ) . to be ( false ) }
end
2024-07-01 15:31:32 +02:00
2024-09-20 14:54:26 +02:00
context 'when not mandatory or blank' do
let ( :value ) { 'u' }
let ( :mandatory ) { false }
it { expect ( champ . mandatory_blank? ) . to be ( false ) }
end
2024-07-01 15:31:32 +02:00
end
context 'when repetition not blank' do
let ( :procedure ) { create ( :procedure , types_de_champ_public : [ { type : :repetition , children : [ { type : :text } ] } ] ) }
let ( :dossier ) { create ( :dossier , :with_populated_champs , procedure : ) }
let ( :champ ) { dossier . champs . find ( & :repetition? ) }
it { expect ( champ . blank? ) . to be ( false ) }
end
end
2017-07-27 20:54:02 +02:00
2020-04-22 11:46:42 +02:00
describe " associations " do
it { is_expected . to belong_to ( :dossier ) }
end
2022-08-29 10:20:05 +02:00
describe " normalization " do
it " should remove null bytes before save " do
2024-07-01 15:31:32 +02:00
champ = Champ . new ( value : " foo \ u0000bar " )
champ . normalize
2022-08-29 10:20:05 +02:00
expect ( champ . value ) . to eq " foobar "
end
end
2024-09-27 15:44:57 +02:00
describe 'public and private' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champ . new }
2018-02-14 18:13:23 +01:00
let ( :dossier ) { create ( :dossier ) }
it 'partition public and private' do
2024-09-27 15:37:11 +02:00
expect ( dossier . project_champs_public . count ) . to eq ( 1 )
expect ( dossier . project_champs_private . count ) . to eq ( 1 )
2018-02-14 18:13:23 +01:00
end
2024-09-27 15:44:57 +02:00
it { expect ( champ . public? ) . to be_truthy }
it { expect ( champ . private? ) . to be_falsey }
2020-09-04 14:16:07 +02:00
context 'when a procedure has 2 revisions' do
2024-09-27 15:44:57 +02:00
it { expect ( dossier . procedure . revisions . count ) . to eq ( 2 ) }
it 'does not duplicate public champs' do
2024-09-27 15:37:11 +02:00
expect ( dossier . project_champs_public . count ) . to eq ( 1 )
2020-09-04 14:16:07 +02:00
end
2024-09-27 15:44:57 +02:00
it 'does not duplicate private champs' do
2024-09-27 15:37:11 +02:00
expect ( dossier . project_champs_private . count ) . to eq ( 1 )
2020-09-04 14:16:07 +02:00
end
end
end
2022-03-10 19:22:07 +01:00
describe '#sections' do
let ( :procedure ) do
2023-02-02 09:59:03 +01:00
create ( :procedure , types_de_champ_public : [ { } , { type : :header_section } , { type : :repetition , mandatory : true , children : [ { type : :header_section } ] } ] , types_de_champ_private : [ { } , { type : :header_section } ] )
2022-03-10 19:22:07 +01:00
end
2020-02-13 11:07:10 +01:00
let ( :dossier ) { create ( :dossier , procedure : procedure ) }
2024-09-27 15:37:11 +02:00
let ( :public_champ ) { dossier . project_champs_public . first }
let ( :private_champ ) { dossier . project_champs_private . first }
let ( :champ_in_repetition ) { dossier . project_champs_public . find ( & :repetition? ) . champs . first }
2022-03-10 19:22:07 +01:00
let ( :standalone_champ ) { build ( :champ , type_de_champ : build ( :type_de_champ ) , dossier : build ( :dossier ) ) }
2024-09-27 15:37:11 +02:00
let ( :public_sections ) { dossier . project_champs_public . filter ( & :header_section? ) }
let ( :private_sections ) { dossier . project_champs_private . filter ( & :header_section? ) }
2024-09-20 14:54:26 +02:00
let ( :sections_in_repetition ) { dossier . champs . filter ( & :child? ) . filter ( & :header_section? ) }
2022-03-10 19:22:07 +01:00
it 'returns the sibling sections of a champ' do
expect ( public_sections ) . not_to be_empty
expect ( private_sections ) . not_to be_empty
expect ( sections_in_repetition ) . not_to be_empty
2020-02-13 11:07:10 +01:00
end
end
2018-01-19 17:42:55 +01:00
describe '#format_datetime' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: DatetimeChamp . new ( value : value ) }
before { champ . run_callbacks ( :validation ) }
2018-01-19 17:42:55 +01:00
context 'when the value is sent by a modern browser' do
let ( :value ) { '2017-12-31 10:23' }
2017-07-27 20:54:02 +02:00
2023-07-06 02:53:57 +02:00
it { expect ( champ . value ) . to eq ( Time . zone . parse ( " 2017-12-31T10:23:00 " ) . iso8601 ) }
2017-07-27 20:54:02 +02:00
end
2018-01-19 17:42:55 +01:00
context 'when the value is sent by a old browser' do
let ( :value ) { '31/12/2018 09:26' }
2017-07-27 20:54:02 +02:00
2023-07-06 02:53:57 +02:00
it { expect ( champ . value ) . to eq ( Time . zone . parse ( " 2018-12-31T09:26:00 " ) . iso8601 ) }
2017-07-27 20:54:02 +02:00
end
end
describe '#multiple_select_to_string' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: MultipleDropDownListChamp . new ( value : value ) }
# before { champ.save! }
2017-07-27 20:54:02 +02:00
# when using the old form, and the ChampsService Class
# TODO: to remove
context 'when the value is already deserialized' do
2023-03-29 23:31:13 +02:00
let ( :value ) { '["val1","val2"]' }
2017-07-27 20:54:02 +02:00
it { expect ( champ . value ) . to eq ( value ) }
context 'when the value is nil' do
let ( :value ) { nil }
it { expect ( champ . value ) . to eq ( value ) }
end
end
# for explanation for the "" entry, see
# https://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select
# GOTCHA
context 'when the value is not already deserialized' do
context 'when a choice is selected' do
2023-01-24 15:10:25 +01:00
let ( :value ) { '["", "val1", "val2"]' }
2017-07-27 20:54:02 +02:00
2023-03-29 23:31:13 +02:00
it { expect ( champ . value ) . to eq ( '["val1","val2"]' ) }
2017-07-27 20:54:02 +02:00
end
context 'when all choices are removed' do
let ( :value ) { '[""]' }
it { expect ( champ . value ) . to eq ( nil ) }
end
end
end
2017-10-26 16:54:10 +02:00
2023-04-13 13:10:23 +02:00
describe 'for_export' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: TextChamp . new ( value : , dossier : build ( :dossier ) ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_text ) ) }
2017-10-26 16:54:10 +02:00
2024-10-21 11:51:34 +02:00
let ( :value_for_export ) { champ . type_de_champ . champ_value_for_export ( champ ) }
2017-10-26 16:54:10 +02:00
context 'when type_de_champ is text' do
let ( :value ) { '123' }
2024-10-21 11:51:34 +02:00
it { expect ( value_for_export ) . to eq ( '123' ) }
2017-10-26 16:54:10 +02:00
end
context 'when type_de_champ is textarea' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: TextareaChamp . new ( value : , dossier : build ( :dossier ) ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_textarea ) ) }
2017-10-26 16:54:10 +02:00
let ( :value ) { '<b>gras<b>' }
2024-10-21 11:51:34 +02:00
it { expect ( value_for_export ) . to eq ( 'gras' ) }
2017-10-26 16:54:10 +02:00
end
2017-10-26 17:45:35 +02:00
context 'when type_de_champ is yes_no' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: YesNoChamp . new ( value : value ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_yes_no ) ) }
2017-10-26 17:45:35 +02:00
context 'if yes' do
2017-11-21 10:28:21 +01:00
let ( :value ) { 'true' }
2017-10-26 17:45:35 +02:00
2024-10-21 11:51:34 +02:00
it { expect ( value_for_export ) . to eq ( 'Oui' ) }
2017-10-26 17:45:35 +02:00
end
context 'if no' do
2017-11-21 10:28:21 +01:00
let ( :value ) { 'false' }
2017-10-26 17:45:35 +02:00
2024-10-21 11:51:34 +02:00
it { expect ( value_for_export ) . to eq ( 'Non' ) }
2017-10-26 17:45:35 +02:00
end
2017-11-21 10:28:21 +01:00
context 'if nil' do
let ( :value ) { nil }
2024-10-22 18:15:28 +02:00
it { expect ( value_for_export ) . to eq ( '' ) }
2017-11-21 10:28:21 +01:00
end
2017-10-26 17:45:35 +02:00
end
2017-10-27 10:01:53 +02:00
2024-04-23 10:14:19 +02:00
context 'when type_de_champ is multiple_drop_down_list' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: MultipleDropDownListChamp . new ( value : , dossier : build ( :dossier ) ) }
2024-11-06 22:30:31 +01:00
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_multiple_drop_down_list , drop_down_options : [ " Crétinier " , " Mousserie " ] ) ) }
2024-07-01 15:31:32 +02:00
2024-04-23 10:14:19 +02:00
let ( :value ) { '["Crétinier", "Mousserie"]' }
2018-07-25 19:34:06 +02:00
2024-10-21 11:51:34 +02:00
it { expect ( value_for_export ) . to eq ( 'Crétinier, Mousserie' ) }
2024-04-23 10:14:19 +02:00
end
2024-05-13 15:31:29 +02:00
context 'when type_de_champ and champ.type mismatch' do
2024-07-01 15:31:32 +02:00
let ( :value ) { :noop }
let ( :champ_yes_no ) { Champs :: YesNoChamp . new ( value : 'true' ) }
let ( :champ_text ) { Champs :: TextChamp . new ( value : 'hello' ) }
2024-10-21 11:51:34 +02:00
let ( :type_de_champ_yes_no ) { build ( :type_de_champ_yes_no ) }
let ( :type_de_champ_text ) { build ( :type_de_champ_text ) }
2024-09-06 16:57:39 +02:00
before do
2024-10-21 11:51:34 +02:00
allow ( champ_yes_no ) . to receive ( :type_de_champ ) . and_return ( type_de_champ_yes_no )
allow ( champ_text ) . to receive ( :type_de_champ ) . and_return ( type_de_champ_text )
2024-09-06 16:57:39 +02:00
end
2024-10-21 11:51:34 +02:00
it { expect ( type_de_champ_text . champ_value_for_export ( champ_yes_no ) ) . to eq ( nil ) }
2024-10-22 18:15:28 +02:00
it { expect ( type_de_champ_yes_no . champ_value_for_export ( champ_text ) ) . to eq ( '' ) }
2024-05-13 15:31:29 +02:00
end
2024-04-23 10:14:19 +02:00
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
describe '#search_terms' do
subject { champ . search_terms }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for adresse champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: AddressChamp . new ( value : ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " 10 rue du Pinson qui Piaille " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ value ] ) }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for checkbox champ' do
let ( :libelle ) { champ . libelle }
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: CheckboxChamp . new ( value : ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_checkbox ) ) }
2024-04-23 10:14:19 +02:00
context 'when the box is checked' do
let ( :value ) { 'true' }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ libelle ] ) }
2018-07-25 19:34:06 +02:00
end
2024-04-23 10:14:19 +02:00
context 'when the box is unchecked' do
let ( :value ) { 'false' }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to be_nil }
2018-07-25 19:34:06 +02:00
end
2024-04-23 10:14:19 +02:00
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for civilite champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: CiviliteChamp . new ( value : ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_civilite ) ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " M. " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ value ] ) }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for date champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: DateChamp . new ( value : ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " 2018-07-30 " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to be_nil }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for date time champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: DatetimeChamp . new ( value : ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " 2018-04-29 09:00 " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to be_nil }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for département champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: DepartementChamp . new ( value : ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_departements ) ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " 69 " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ '69 – Rhône' ] ) }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for dossier link champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: DossierLinkChamp . new ( value : ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_dossier_link ) ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " 9103132886 " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ value ] ) }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for drop down list champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: DropDownListChamp . new ( value : ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_drop_down_list ) ) }
2024-11-06 22:30:31 +01:00
let ( :value ) { " val1 " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ value ] ) }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for email champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: EmailChamp . new ( value : ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_email ) ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " machin@example.com " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ value ] ) }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for explication champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: ExplicationChamp . new }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to be_nil }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for header section champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: HeaderSectionChamp . new }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to be_nil }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for linked drop down list champ' do
2024-11-06 22:30:31 +01:00
let ( :champ ) { Champs :: LinkedDropDownListChamp . new ( value : '["hello","world"]' ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_linked_drop_down_list , drop_down_options : [ '--hello--' , 'world' ] ) ) }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ " hello " , " world " ] ) }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for multiple drop down list champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: MultipleDropDownListChamp . new ( value : ) }
2024-11-06 22:30:31 +01:00
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_multiple_drop_down_list , drop_down_options : [ 'goodbye' , 'cruel' , 'world' ] ) ) }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'when there are multiple values selected' do
let ( :value ) { JSON . generate ( [ 'goodbye' , 'cruel' , 'world' ] ) }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ " goodbye " , " cruel " , " world " ] ) }
2018-07-25 19:34:06 +02:00
end
2024-04-23 10:14:19 +02:00
context 'when there is no value selected' do
let ( :value ) { nil }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ ] ) }
2018-07-25 19:34:06 +02:00
end
2024-04-23 10:14:19 +02:00
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for number champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: NumberChamp . new ( value : ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_number ) ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " 1234 " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ value ] ) }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for pays champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: PaysChamp . new ( value : ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_pays ) ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " FR " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ 'France' ] ) }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for phone champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: PhoneChamp . new ( value : ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_phone ) ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " 06 06 06 06 06 " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ value ] ) }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for pièce justificative champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: PieceJustificativeChamp . new ( value : ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { nil }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to be_nil }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for region champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: RegionChamp . new ( value : ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_regions ) ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " 11 " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ 'Île-de-France' ] ) }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for siret champ' do
context 'when there is an etablissement' do
let ( :etablissement ) do
build (
:etablissement ,
siret : " 35130347400024 " ,
siege_social : true ,
naf : " 9004Z " ,
libelle_naf : " Gestion de salles de spectacles " ,
adresse : " MAISON JEUNES CULTURE FABRIQUE \r \n 98 RUE DE PARIS \r \n 59200 TOURCOING \r \n FRANCE \r \n " ,
numero_voie : " 98 " ,
type_voie : " RUE " ,
nom_voie : " DE PARIS " ,
code_postal : " 59200 " ,
localite : " TOURCOING " ,
code_insee_localite : " 59599 " ,
entreprise_siren : " 351303474 " ,
entreprise_numero_tva_intracommunautaire : " FR02351303474 " ,
entreprise_forme_juridique : " Association déclarée " ,
entreprise_forme_juridique_code : " 9220 " ,
entreprise_nom_commercial : " " ,
entreprise_raison_sociale : " MAISON DES JEUNES ET DE LA CULTURE DE LA FABRIQUE " ,
entreprise_siret_siege_social : " 35130347400024 " ,
entreprise_nom : 'Martin' ,
entreprise_prenom : 'Guillaume' ,
entreprise_code_effectif_entreprise : " 12 " ,
entreprise_date_creation : " 1989-07-09 " ,
association_rna : " W595004053 " ,
association_titre : " MAISON DES JEUNES ET DE LA CULTURE DE LA FABRIQUE " ,
association_objet : " Création, gestion et animation de la Maison des Jeunes et de la Culture de la Fabrique, qui constitue un élément essentiel de la vie sociale et culturelle d'un territoire de vie : pays, agglomération, ville, communauté de communes, village, quartier ... " ,
association_date_creation : " 1962-05-23 " ,
association_date_declaration : " 2016-12-02 " ,
association_date_publication : " 1962-05-31 "
)
2018-07-25 19:34:06 +02:00
end
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: SiretChamp . new ( value : etablissement . siret , etablissement : ) }
2024-04-23 10:14:19 +02:00
2024-09-13 17:35:27 +02:00
it { is_expected . to eq ( [ etablissement . entreprise_siren , etablissement . entreprise_numero_tva_intracommunautaire , etablissement . entreprise_forme_juridique , etablissement . entreprise_forme_juridique_code , etablissement . entreprise_nom_commercial , etablissement . entreprise_raison_sociale , etablissement . entreprise_siret_siege_social , etablissement . entreprise_nom , etablissement . entreprise_prenom , etablissement . association_rna , etablissement . association_titre , etablissement . association_objet , etablissement . siret , etablissement . enseigne , etablissement . naf , etablissement . libelle_naf , etablissement . adresse , etablissement . code_postal , etablissement . localite , etablissement . code_insee_localite , nil ] ) }
2018-07-25 19:34:06 +02:00
end
2024-04-23 10:14:19 +02:00
context 'when there is no etablissement' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: SiretChamp . new ( value : , etablissement : nil ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " 35130347400024 " }
2018-07-25 19:34:06 +02:00
2018-12-24 17:29:46 +01:00
it { is_expected . to eq ( [ value ] ) }
2018-07-25 19:34:06 +02:00
end
2024-04-23 10:14:19 +02:00
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for text champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: TextChamp . new ( value : ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_text ) ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " Blah " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ value ] ) }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for text area champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: TextareaChamp . new ( value : ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_textarea ) ) }
2024-04-23 10:14:19 +02:00
let ( :value ) { " Bla \n Blah de bla. " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ value ] ) }
end
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'for yes/no champ' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: YesNoChamp . new ( value : ) }
before { allow ( champ ) . to receive ( :type_de_champ ) . and_return ( build ( :type_de_champ_yes_no ) ) }
2024-04-23 10:14:19 +02:00
let ( :libelle ) { champ . libelle }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
context 'when the box is checked' do
let ( :value ) { " true " }
2018-07-25 19:34:06 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to eq ( [ libelle ] ) }
2018-07-25 19:34:06 +02:00
end
2024-04-23 10:14:19 +02:00
context 'when the box is unchecked' do
let ( :value ) { " false " }
2017-10-27 10:01:53 +02:00
2024-04-23 10:14:19 +02:00
it { is_expected . to be_nil }
end
2017-10-27 10:01:53 +02:00
end
2017-10-26 16:54:10 +02:00
end
2018-05-11 14:59:20 +02:00
2021-03-04 12:50:22 +01:00
describe '#enqueue_virus_scan' do
2018-05-11 14:59:20 +02:00
context 'when type_champ is type_de_champ_piece_justificative' do
2024-07-01 15:31:32 +02:00
let ( :procedure ) { create ( :procedure , types_de_champ_public : [ { type : :piece_justificative } ] ) }
let ( :dossier ) { create ( :dossier , procedure : ) }
let ( :champ ) { dossier . champs . first }
2018-05-11 14:59:20 +02:00
context 'and there is a blob' do
2019-05-02 11:37:35 +02:00
before do
2021-03-04 12:50:22 +01:00
allow ( ClamavService ) . to receive ( :safe_file? ) . and_return ( true )
end
subject do
2019-05-02 11:37:35 +02:00
champ . piece_justificative_file . attach ( io : StringIO . new ( " toto " ) , filename : " toto.txt " , content_type : " text/plain " )
2020-07-20 17:18:44 +02:00
champ . save!
2021-03-04 12:50:22 +01:00
champ
end
it 'marks the file as pending virus scan' do
2022-10-25 14:14:24 +02:00
expect ( subject . piece_justificative_file . first . virus_scanner . started? ) . to be_truthy
2021-03-04 12:50:22 +01:00
end
it 'marks the file as safe once the scan completes' do
2021-03-11 14:42:57 +01:00
subject
perform_enqueued_jobs
2022-10-25 14:14:24 +02:00
expect ( champ . reload . piece_justificative_file . first . virus_scanner . safe? ) . to be_truthy
2019-05-02 11:37:35 +02:00
end
2021-03-04 12:50:22 +01:00
end
end
end
describe '#enqueue_watermark_job' do
context 'when type_champ is type_de_champ_titre_identite' do
2024-07-01 15:31:32 +02:00
let ( :procedure ) { create ( :procedure , types_de_champ_public : [ { type : :titre_identite } ] ) }
let ( :dossier ) { create ( :dossier , procedure : ) }
let ( :champ ) { dossier . champs . first }
2021-03-04 12:50:22 +01:00
before do
allow ( ClamavService ) . to receive ( :safe_file? ) . and_return ( true )
end
subject do
champ . piece_justificative_file . attach ( fixture_file_upload ( 'spec/fixtures/files/logo_test_procedure.png' , 'image/png' ) )
champ . save!
champ
end
2021-03-11 14:42:57 +01:00
it 'marks the file as needing watermarking' do
2022-10-25 14:14:24 +02:00
expect ( subject . piece_justificative_file . first . watermark_pending? ) . to be_truthy
2021-03-04 12:50:22 +01:00
end
2018-05-11 14:59:20 +02:00
2021-03-04 12:50:22 +01:00
it 'watermarks the file' do
2021-03-11 14:42:57 +01:00
subject
perform_enqueued_jobs
2022-10-25 14:14:24 +02:00
expect ( champ . reload . piece_justificative_file . first . watermark_pending? ) . to be_falsy
expect ( champ . reload . piece_justificative_file . first . blob . watermark_done? ) . to be_truthy
2018-05-11 14:59:20 +02:00
end
end
end
2018-12-18 11:17:52 +01:00
2021-02-04 19:24:52 +01:00
describe '#log_fetch_external_data_exception' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: SiretChamp . new }
2021-02-04 19:24:52 +01:00
context " add execption to the log " do
2024-07-01 15:31:32 +02:00
it do
expect ( champ ) . to receive ( :update_column ) . with ( :fetch_external_data_exceptions , [ 'PAN' ] )
champ . log_fetch_external_data_exception ( double ( inspect : 'PAN' ) )
2021-02-04 19:24:52 +01:00
end
end
end
2021-02-09 12:35:23 +01:00
describe " fetch_external_data " do
2024-07-01 15:31:32 +02:00
let ( :procedure ) { create ( :procedure , types_de_champ_public : [ { type : :rnf } ] ) }
let ( :dossier ) { create ( :dossier , procedure : ) }
let ( :champ ) { dossier . champs . first . tap { _1 . update_column ( :data , 'some data' ) } }
2021-02-09 12:35:23 +01:00
context " cleanup_if_empty " do
it " remove data if external_id changes " do
expect ( champ . data ) . to_not be_nil
champ . update ( external_id : 'external_id' )
expect ( champ . data ) . to be_nil
end
end
context " fetch_external_data_later " do
2024-08-19 17:07:43 +02:00
let ( :data ) { { address : { city : " some external data " } } . with_indifferent_access }
2021-02-09 12:35:23 +01:00
it " fill data from external source " do
2024-02-20 17:49:07 +01:00
expect_any_instance_of ( Champs :: RNFChamp ) . to receive ( :fetch_external_data ) { data }
2021-02-09 12:35:23 +01:00
perform_enqueued_jobs do
champ . update ( external_id : 'external_id' )
end
expect ( champ . reload . data ) . to eq data
end
end
2024-02-20 17:49:07 +01:00
end
2022-05-04 15:57:25 +02:00
2024-02-20 17:49:07 +01:00
describe " # input_name " do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: TextChamp . new }
2024-04-22 14:22:52 +02:00
it { expect ( champ . input_name ) . to eq " dossier[champs_public_attributes][ #{ champ . public_id } ] " }
2022-05-04 15:57:25 +02:00
2024-02-20 17:49:07 +01:00
context " when private " do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: TextChamp . new ( private : true ) }
2024-04-22 14:22:52 +02:00
it { expect ( champ . input_name ) . to eq " dossier[champs_private_attributes][ #{ champ . public_id } ] " }
2024-02-20 17:49:07 +01:00
end
2021-02-09 12:35:23 +01:00
end
2023-02-28 13:55:52 +01:00
2024-03-21 12:01:55 +01:00
describe 'dom_id' do
2024-07-01 15:31:32 +02:00
let ( :champ ) { Champs :: TextChamp . new ( row_id : '1234' ) }
before do
allow ( champ ) . to receive ( :type_de_champ ) . and_return ( create ( :type_de_champ_text ) )
end
2024-03-21 12:01:55 +01:00
2024-07-01 15:31:32 +02:00
it do
expect ( champ . public_id ) . to eq ( " #{ champ . stable_id } - #{ champ . row_id } " )
expect ( ActionView :: RecordIdentifier . dom_id ( champ ) ) . to eq ( " champ_ #{ champ . public_id } " )
expect ( ActionView :: RecordIdentifier . dom_id ( champ . type_de_champ ) ) . to eq ( " type_de_champ_ #{ champ . type_de_champ . id } " )
expect ( ActionView :: RecordIdentifier . dom_class ( champ ) ) . to eq ( " champ " )
end
2024-03-21 12:01:55 +01:00
end
2024-05-21 09:50:27 +02:00
describe 'clone' do
2024-07-01 15:31:32 +02:00
let ( :procedure ) { create ( :procedure , types_de_champ_private : , types_de_champ_public : ) }
let ( :types_de_champ_private ) { [ ] }
let ( :types_de_champ_public ) { [ ] }
let ( :champ ) { dossier . champs . first }
2024-05-21 09:50:27 +02:00
subject { champ . clone ( fork ) }
context 'when champ public' do
2024-07-01 15:31:32 +02:00
let ( :types_de_champ_public ) { [ { type : :piece_justificative } ] }
let ( :dossier ) { create ( :dossier , :with_populated_champs , procedure : ) }
2024-05-21 09:50:27 +02:00
context 'when fork' do
let ( :fork ) { true }
it do
expect ( subject . piece_justificative_file ) . to be_attached
end
end
context 'when not fork' do
let ( :fork ) { false }
it do
expect ( subject . piece_justificative_file ) . to be_attached
end
end
end
context 'champ private' do
2024-07-01 15:31:32 +02:00
let ( :dossier ) { create ( :dossier , :with_populated_annotations , procedure : ) }
let ( :types_de_champ_private ) { [ { type : :piece_justificative } ] }
2024-05-21 09:50:27 +02:00
context 'when fork' do
let ( :fork ) { true }
it do
expect ( subject . piece_justificative_file ) . to be_attached
end
end
context 'when not fork' do
let ( :fork ) { false }
it do
expect ( subject . piece_justificative_file ) . not_to be_attached
end
end
end
end
2017-04-04 15:27:04 +02:00
end