2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
RSpec.describe PrefillChamps do
|
|
|
|
describe "#to_a", vcr: { cassette_name: 'api_geo_all' } do
|
2022-12-21 19:20:47 +01:00
|
|
|
let(:procedure) { create(:procedure, :published, types_de_champ_public:, types_de_champ_private:) }
|
2024-12-09 15:15:04 +01:00
|
|
|
let(:dossier) { create(:dossier, :brouillon, procedure:) }
|
|
|
|
let(:linked_dossier) { create(:dossier, :en_construction, procedure:) }
|
2022-12-21 19:20:47 +01:00
|
|
|
let(:types_de_champ_public) { [] }
|
|
|
|
let(:types_de_champ_private) { [] }
|
2022-11-28 15:42:58 +01:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
subject(:prefill_champs_array) { described_class.new(dossier, params).to_a }
|
2022-11-28 15:42:58 +01:00
|
|
|
|
|
|
|
context "when the stable ids match the TypeDeChamp of the corresponding procedure" do
|
2022-12-21 19:20:47 +01:00
|
|
|
let(:types_de_champ_public) { [{ type: :text }, { type: :textarea }] }
|
|
|
|
let(:type_de_champ_1) { procedure.published_revision.types_de_champ_public.first }
|
2022-11-28 15:42:58 +01:00
|
|
|
let(:value_1) { "any value" }
|
2022-12-01 11:17:48 +01:00
|
|
|
let(:champ_id_1) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
|
2022-11-28 15:42:58 +01:00
|
|
|
|
2022-12-21 19:20:47 +01:00
|
|
|
let(:type_de_champ_2) { procedure.published_revision.types_de_champ_public.second }
|
2022-11-28 15:42:58 +01:00
|
|
|
let(:value_2) { "another value" }
|
2022-12-01 11:17:48 +01:00
|
|
|
let(:champ_id_2) { find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).id }
|
2022-11-28 15:42:58 +01:00
|
|
|
|
|
|
|
let(:params) {
|
|
|
|
{
|
2023-02-22 19:32:25 +01:00
|
|
|
"champ_#{type_de_champ_1.to_typed_id_for_query}" => value_1,
|
|
|
|
"champ_#{type_de_champ_2.to_typed_id_for_query}" => value_2
|
2022-11-28 15:42:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it "builds an array of hash(id, value) matching all the given params" do
|
2023-09-07 10:03:17 +02:00
|
|
|
expect(prefill_champs_array).to match_array([
|
2022-11-28 15:42:58 +01:00
|
|
|
{ id: champ_id_1, value: value_1 },
|
|
|
|
{ id: champ_id_2, value: value_2 }
|
|
|
|
])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-05 10:09:44 +01:00
|
|
|
context "when the typed id is not prefixed by 'champ_'" do
|
2022-12-21 19:20:47 +01:00
|
|
|
let(:type_de_champ) { procedure.published_revision.types_de_champ_public.first }
|
|
|
|
let(:types_de_champ_public) { [{ type: :text }] }
|
2022-12-05 10:09:44 +01:00
|
|
|
|
2023-02-22 19:32:25 +01:00
|
|
|
let(:params) { { type_de_champ.to_typed_id_for_query => "value" } }
|
2022-12-05 10:09:44 +01:00
|
|
|
|
|
|
|
it "filters out the champ" do
|
2023-09-07 10:03:17 +02:00
|
|
|
expect(prefill_champs_array).to match([])
|
2022-12-05 10:09:44 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-28 15:42:58 +01:00
|
|
|
context "when the typed id is unknown" do
|
2022-12-05 10:09:44 +01:00
|
|
|
let(:params) { { "champ_jane_doe" => "value" } }
|
2022-11-28 15:42:58 +01:00
|
|
|
|
|
|
|
it "filters out the unknown params" do
|
2023-09-07 10:03:17 +02:00
|
|
|
expect(prefill_champs_array).to match([])
|
2022-11-28 15:42:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no Champ that matches the TypeDeChamp with the given stable id' do
|
|
|
|
let!(:type_de_champ) { create(:type_de_champ_text) } # goes to another procedure
|
|
|
|
|
2023-02-22 19:32:25 +01:00
|
|
|
let(:params) { { "champ_#{type_de_champ.to_typed_id_for_query}" => "value" } }
|
2022-11-28 15:42:58 +01:00
|
|
|
|
|
|
|
it "filters out the param" do
|
2023-09-07 10:03:17 +02:00
|
|
|
expect(prefill_champs_array).to match([])
|
2022-11-28 15:42:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-21 19:20:47 +01:00
|
|
|
shared_examples "a champ public value that is authorized" do |type_de_champ_type, value|
|
|
|
|
context "when the type de champ is authorized (#{type_de_champ_type})" do
|
|
|
|
let(:types_de_champ_public) { [{ type: type_de_champ_type }] }
|
|
|
|
let(:type_de_champ) { procedure.published_revision.types_de_champ_public.first }
|
2023-02-12 11:55:28 +01:00
|
|
|
let(:champ) { find_champ_by_stable_id(dossier, type_de_champ.stable_id) }
|
2024-12-09 15:15:04 +01:00
|
|
|
let(:champ_value) { value == 'linked_dossier_id' ? linked_dossier.id : value }
|
2022-11-28 15:42:58 +01:00
|
|
|
|
2024-12-09 15:15:04 +01:00
|
|
|
let(:params) { { "champ_#{type_de_champ.to_typed_id_for_query}" => champ_value } }
|
2022-11-28 15:42:58 +01:00
|
|
|
|
2023-02-15 17:39:19 +01:00
|
|
|
it "builds an array of hash matching the given params" do
|
2024-12-09 15:15:04 +01:00
|
|
|
expect(prefill_champs_array).to match([{ id: champ.id }.merge(attributes(champ, champ_value))])
|
2022-11-28 15:42:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-21 19:20:47 +01:00
|
|
|
shared_examples "a champ private value that is authorized" do |type_de_champ_type, value|
|
|
|
|
context "when the type de champ is authorized (#{type_de_champ_type})" do
|
|
|
|
let(:types_de_champ_private) { [{ type: type_de_champ_type }] }
|
|
|
|
let(:type_de_champ) { procedure.published_revision.types_de_champ_private.first }
|
2023-02-12 11:55:28 +01:00
|
|
|
let(:champ) { find_champ_by_stable_id(dossier, type_de_champ.stable_id) }
|
2024-12-09 15:15:04 +01:00
|
|
|
let(:champ_value) { value == 'linked_dossier_id' ? linked_dossier.id : value }
|
2022-12-21 19:20:47 +01:00
|
|
|
|
2024-12-09 15:15:04 +01:00
|
|
|
let(:params) { { "champ_#{type_de_champ.to_typed_id_for_query}" => champ_value } }
|
2022-12-21 19:20:47 +01:00
|
|
|
|
2023-02-15 17:39:19 +01:00
|
|
|
it "builds an array of hash matching the given params" do
|
2024-12-09 15:15:04 +01:00
|
|
|
expect(prefill_champs_array).to match([{ id: champ.id }.merge(attributes(champ, champ_value))])
|
2022-12-21 19:20:47 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "a champ public value that is unauthorized" do |type_de_champ_type, value|
|
|
|
|
let(:types_de_champ_public) { [{ type: type_de_champ_type }] }
|
|
|
|
let(:type_de_champ) { procedure.published_revision.types_de_champ_public.first }
|
2022-11-28 15:42:58 +01:00
|
|
|
|
2023-02-22 19:32:25 +01:00
|
|
|
let(:params) { { "champ_#{type_de_champ.to_typed_id_for_query}" => value } }
|
2022-11-28 15:42:58 +01:00
|
|
|
|
2022-12-21 19:20:47 +01:00
|
|
|
context "when the type de champ is unauthorized (#{type_de_champ_type})" do
|
2022-11-28 15:42:58 +01:00
|
|
|
it "filters out the param" do
|
2023-09-07 10:03:17 +02:00
|
|
|
expect(prefill_champs_array).to match([])
|
2022-11-28 15:42:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-21 19:20:47 +01:00
|
|
|
it_behaves_like "a champ public value that is authorized", :text, "value"
|
|
|
|
it_behaves_like "a champ public value that is authorized", :textarea, "value"
|
|
|
|
it_behaves_like "a champ public value that is authorized", :decimal_number, "3.14"
|
|
|
|
it_behaves_like "a champ public value that is authorized", :integer_number, "42"
|
|
|
|
it_behaves_like "a champ public value that is authorized", :email, "value"
|
|
|
|
it_behaves_like "a champ public value that is authorized", :phone, "value"
|
|
|
|
it_behaves_like "a champ public value that is authorized", :iban, "value"
|
|
|
|
it_behaves_like "a champ public value that is authorized", :civilite, "M."
|
2023-01-18 12:52:38 +01:00
|
|
|
it_behaves_like "a champ public value that is authorized", :pays, "FR"
|
2023-01-23 14:28:39 +01:00
|
|
|
it_behaves_like "a champ public value that is authorized", :regions, "03"
|
2023-01-12 17:42:02 +01:00
|
|
|
it_behaves_like "a champ public value that is authorized", :date, "2022-12-22"
|
|
|
|
it_behaves_like "a champ public value that is authorized", :datetime, "2022-12-22T10:30"
|
2023-01-06 14:01:59 +01:00
|
|
|
it_behaves_like "a champ public value that is authorized", :yes_no, "true"
|
|
|
|
it_behaves_like "a champ public value that is authorized", :yes_no, "false"
|
|
|
|
it_behaves_like "a champ public value that is authorized", :checkbox, "true"
|
|
|
|
it_behaves_like "a champ public value that is authorized", :checkbox, "false"
|
2023-01-18 09:47:22 +01:00
|
|
|
it_behaves_like "a champ public value that is authorized", :drop_down_list, "value"
|
2023-01-20 10:38:42 +01:00
|
|
|
it_behaves_like "a champ public value that is authorized", :departements, "03"
|
2023-03-21 14:47:14 +01:00
|
|
|
it_behaves_like "a champ public value that is authorized", :communes, ['01540', '01457']
|
2023-02-28 08:13:15 +01:00
|
|
|
it_behaves_like "a champ public value that is authorized", :address, "20 avenue de Ségur 75007 Paris"
|
2023-01-23 14:28:39 +01:00
|
|
|
it_behaves_like "a champ public value that is authorized", :multiple_drop_down_list, ["val1", "val2"]
|
2024-12-09 15:15:04 +01:00
|
|
|
it_behaves_like "a champ public value that is authorized", :dossier_link, 'linked_dossier_id'
|
2023-02-15 17:39:19 +01:00
|
|
|
it_behaves_like "a champ public value that is authorized", :epci, ['01', '200042935']
|
2023-01-16 15:30:15 +01:00
|
|
|
it_behaves_like "a champ public value that is authorized", :siret, "13002526500013"
|
2022-12-21 19:20:47 +01:00
|
|
|
|
2023-02-08 17:38:51 +01:00
|
|
|
context "when the public type de champ is authorized (repetition)" do
|
|
|
|
let(:types_de_champ_public) { [{ type: :repetition, children: [{ type: :text }] }] }
|
|
|
|
let(:type_de_champ) { procedure.published_revision.types_de_champ_public.first }
|
|
|
|
let(:type_de_champ_child) { procedure.published_revision.children_of(type_de_champ).first }
|
|
|
|
let(:type_de_champ_child_value) { "value" }
|
|
|
|
let(:type_de_champ_child_value2) { "value2" }
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:child_champs) { dossier.champs.where(stable_id: type_de_champ_child.stable_id) }
|
2023-02-08 17:38:51 +01:00
|
|
|
|
2023-02-22 19:40:52 +01:00
|
|
|
let(:params) { { "champ_#{type_de_champ.to_typed_id_for_query}" => [{ "champ_#{type_de_champ_child.to_typed_id_for_query}" => type_de_champ_child_value }, { "champ_#{type_de_champ_child.to_typed_id_for_query}" => type_de_champ_child_value2 }] } }
|
2023-02-08 17:38:51 +01:00
|
|
|
|
|
|
|
it "builds an array of hash(id, value) matching the given params" do
|
2024-07-01 15:31:32 +02:00
|
|
|
expect(prefill_champs_array).to match([{ id: child_champs.first.id, value: type_de_champ_child_value }, { id: child_champs.second.id, value: type_de_champ_child_value2 }])
|
2023-02-08 17:38:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-21 19:20:47 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :text, "value"
|
|
|
|
it_behaves_like "a champ private value that is authorized", :textarea, "value"
|
|
|
|
it_behaves_like "a champ private value that is authorized", :decimal_number, "3.14"
|
|
|
|
it_behaves_like "a champ private value that is authorized", :integer_number, "42"
|
|
|
|
it_behaves_like "a champ private value that is authorized", :email, "value"
|
|
|
|
it_behaves_like "a champ private value that is authorized", :phone, "value"
|
|
|
|
it_behaves_like "a champ private value that is authorized", :iban, "value"
|
|
|
|
it_behaves_like "a champ private value that is authorized", :civilite, "M."
|
2023-01-18 12:52:38 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :pays, "FR"
|
2023-01-23 14:28:39 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :regions, "93"
|
2023-01-12 17:42:02 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :date, "2022-12-22"
|
|
|
|
it_behaves_like "a champ private value that is authorized", :datetime, "2022-12-22T10:30"
|
2023-01-06 14:01:59 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :yes_no, "true"
|
|
|
|
it_behaves_like "a champ private value that is authorized", :yes_no, "false"
|
|
|
|
it_behaves_like "a champ private value that is authorized", :checkbox, "true"
|
|
|
|
it_behaves_like "a champ private value that is authorized", :checkbox, "false"
|
2023-01-18 09:47:22 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :drop_down_list, "value"
|
2023-01-24 12:25:17 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :regions, "93"
|
2023-01-16 15:30:15 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :siret, "13002526500013"
|
2023-02-15 17:39:19 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :departements, "03"
|
2023-03-21 14:47:14 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :communes, ['01540', '01457']
|
2023-02-28 08:13:15 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :address, "20 avenue de Ségur 75007 Paris"
|
2023-01-23 14:28:39 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :multiple_drop_down_list, ["val1", "val2"]
|
2024-12-09 15:15:04 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :dossier_link, 'linked_dossier_id'
|
2023-02-15 17:39:19 +01:00
|
|
|
it_behaves_like "a champ private value that is authorized", :epci, ['01', '200042935']
|
2023-02-08 17:38:51 +01:00
|
|
|
|
|
|
|
context "when the private type de champ is authorized (repetition)" do
|
|
|
|
let(:types_de_champ_private) { [{ type: :repetition, children: [{ type: :text }] }] }
|
|
|
|
let(:type_de_champ) { procedure.published_revision.types_de_champ_private.first }
|
|
|
|
let(:type_de_champ_child) { procedure.published_revision.children_of(type_de_champ).first }
|
|
|
|
let(:type_de_champ_child_value) { "value" }
|
|
|
|
let(:type_de_champ_child_value2) { "value2" }
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:child_champs) { dossier.champs.where(stable_id: type_de_champ_child.stable_id) }
|
2023-02-08 17:38:51 +01:00
|
|
|
|
2023-02-22 19:40:52 +01:00
|
|
|
let(:params) { { "champ_#{type_de_champ.to_typed_id_for_query}" => [{ "champ_#{type_de_champ_child.to_typed_id_for_query}" => type_de_champ_child_value }, { "champ_#{type_de_champ_child.to_typed_id_for_query}" => type_de_champ_child_value2 }] } }
|
2023-02-08 17:38:51 +01:00
|
|
|
|
|
|
|
it "builds an array of hash(id, value) matching the given params" do
|
2024-07-01 15:31:32 +02:00
|
|
|
expect(prefill_champs_array).to match([{ id: child_champs.first.id, value: type_de_champ_child_value }, { id: child_champs.second.id, value: type_de_champ_child_value2 }])
|
2023-02-08 17:38:51 +01:00
|
|
|
end
|
|
|
|
end
|
2022-12-21 19:20:47 +01:00
|
|
|
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :decimal_number, "non decimal string"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :integer_number, "non integer string"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :number, "value"
|
2023-02-22 15:56:31 +01:00
|
|
|
it_behaves_like "a champ public value that is unauthorized", :dossier_link, "value"
|
2022-12-21 19:20:47 +01:00
|
|
|
it_behaves_like "a champ public value that is unauthorized", :titre_identite, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :civilite, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :date, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :datetime, "value"
|
2023-01-12 17:42:02 +01:00
|
|
|
it_behaves_like "a champ public value that is unauthorized", :datetime, "12-22-2022T10:30"
|
2022-12-21 19:20:47 +01:00
|
|
|
it_behaves_like "a champ public value that is unauthorized", :linked_drop_down_list, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :header_section, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :explication, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :piece_justificative, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :cnaf, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :dgfip, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :pole_emploi, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :mesri, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :carte, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :pays, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :regions, "value"
|
|
|
|
it_behaves_like "a champ public value that is unauthorized", :departements, "value"
|
2023-02-23 10:23:47 +01:00
|
|
|
it_behaves_like "a champ public value that is unauthorized", :communes, "value"
|
2023-01-24 15:10:25 +01:00
|
|
|
it_behaves_like "a champ public value that is unauthorized", :multiple_drop_down_list, ["value"]
|
2023-02-08 17:38:51 +01:00
|
|
|
|
|
|
|
context "when the public type de champ is unauthorized because of wrong value format (repetition)" do
|
|
|
|
let(:types_de_champ_public) { [{ type: :repetition, children: [{ type: :text }] }] }
|
|
|
|
let(:type_de_champ) { procedure.published_revision.types_de_champ_public.first }
|
|
|
|
let(:type_de_champ_child) { procedure.published_revision.children_of(type_de_champ).first }
|
|
|
|
|
2023-02-22 19:32:25 +01:00
|
|
|
let(:params) { { "champ_#{type_de_champ.to_typed_id_for_query}" => "value" } }
|
2023-02-08 17:38:51 +01:00
|
|
|
|
|
|
|
it "builds an array of hash(id, value) matching the given params" do
|
2023-09-07 10:03:17 +02:00
|
|
|
expect(prefill_champs_array).to match([])
|
2023-02-08 17:38:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-16 16:58:55 +01:00
|
|
|
context "when the public type de champ is unauthorized because of wrong value typed_id (repetition)" do
|
2023-02-08 17:38:51 +01:00
|
|
|
let(:types_de_champ_public) { [{ type: :repetition, children: [{ type: :text }] }] }
|
|
|
|
let(:type_de_champ) { procedure.published_revision.types_de_champ_public.first }
|
|
|
|
let(:type_de_champ_child) { procedure.published_revision.children_of(type_de_champ).first }
|
|
|
|
|
2023-02-22 19:32:25 +01:00
|
|
|
let(:params) { { "champ_#{type_de_champ.to_typed_id_for_query}" => ["{\"wrong\":\"value\"}", "{\"wrong\":\"value2\"}"] } }
|
2023-02-08 17:38:51 +01:00
|
|
|
|
|
|
|
it "builds an array of hash(id, value) matching the given params" do
|
2023-09-07 10:03:17 +02:00
|
|
|
expect(prefill_champs_array).to match([])
|
2023-02-08 17:38:51 +01:00
|
|
|
end
|
|
|
|
end
|
2022-11-28 15:42:58 +01:00
|
|
|
end
|
2022-12-01 11:17:48 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_champ_by_stable_id(dossier, stable_id)
|
2024-07-01 15:31:32 +02:00
|
|
|
dossier.champs.find_by(stable_id:)
|
2022-12-01 11:17:48 +01:00
|
|
|
end
|
2023-02-12 11:55:28 +01:00
|
|
|
|
|
|
|
def attributes(champ, value)
|
|
|
|
TypesDeChamp::PrefillTypeDeChamp
|
2023-02-21 17:32:24 +01:00
|
|
|
.build(champ.type_de_champ, procedure.active_revision)
|
2023-02-12 11:55:28 +01:00
|
|
|
.to_assignable_attributes(champ, value)
|
|
|
|
end
|
2022-11-28 15:42:58 +01:00
|
|
|
end
|