demarches-normaliennes/spec/factories/procedure.rb

345 lines
10 KiB
Ruby
Raw Normal View History

2018-01-23 17:15:42 +01:00
FactoryBot.define do
sequence(:published_path) { |n| "fake_path#{n}" }
factory :procedure do
sequence(:libelle) { |n| "Procedure #{n}" }
2018-09-04 16:46:48 +02:00
description { "Demande de subvention à l'intention des associations" }
organisation { "Orga DINUM" }
direction { "direction DINUM" }
2018-09-04 16:46:48 +02:00
cadre_juridique { "un cadre juridique important" }
published_at { nil }
duree_conservation_dossiers_dans_ds { 3 }
ask_birthday { false }
lien_site_web { "https://mon-site.gouv" }
path { SecureRandom.uuid }
2021-12-01 21:05:15 +01:00
association :zone
groupe_instructeurs { [association(:groupe_instructeur, :default, procedure: instance, strategy: :build)] }
administrateurs { administrateur.present? ? [administrateur] : [association(:administrateur)] }
transient do
administrateur {}
instructeurs { [] }
2020-08-27 19:55:37 +02:00
types_de_champ { [] }
types_de_champ_private { [] }
updated_at { nil }
attestation_template { nil }
dossier_submitted_message { nil }
end
after(:build) do |procedure, evaluator|
initial_revision = build(:procedure_revision, procedure: procedure, attestation_template: evaluator.attestation_template, dossier_submitted_message: evaluator.dossier_submitted_message)
add_types_de_champs(evaluator.types_de_champ, to: initial_revision, scope: :public)
add_types_de_champs(evaluator.types_de_champ_private, to: initial_revision, scope: :private)
2020-08-27 19:55:37 +02:00
if procedure.brouillon?
procedure.draft_revision = initial_revision
else
procedure.published_revision = initial_revision
procedure.published_revision.published_at = Time.zone.now
procedure.draft_revision = build(:procedure_revision, from_original: initial_revision)
2020-08-27 19:55:37 +02:00
end
end
after(:create) do |procedure, evaluator|
evaluator.instructeurs.each { |i| i.assign_to_procedure(procedure) }
2020-08-27 19:55:37 +02:00
if evaluator.updated_at
procedure.update_column(:updated_at, evaluator.updated_at)
end
procedure.reload
end
factory :procedure_with_dossiers do
transient do
2018-09-04 16:46:48 +02:00
dossiers_count { 1 }
end
after(:create) do |procedure, evaluator|
user = create(:user)
evaluator.dossiers_count.times do
2020-08-27 19:55:37 +02:00
create(:dossier, procedure: procedure, user: user)
end
end
end
factory :simple_procedure do
published
for_individual { true }
after(:build) do |procedure, _evaluator|
2020-08-27 19:55:37 +02:00
build(:type_de_champ, libelle: 'Texte obligatoire', mandatory: true, procedure: procedure)
end
end
trait :with_bulk_message do
groupe_instructeurs { [association(:groupe_instructeur, :default, :with_bulk_message, procedure: instance, strategy: :build)] }
end
2019-03-27 12:32:38 +01:00
trait :with_logo do
logo { Rack::Test::UploadedFile.new('spec/fixtures/files/logo_test_procedure.png', 'image/png') }
end
2019-03-27 12:32:38 +01:00
trait :with_path do
2018-10-25 17:40:23 +02:00
path { generate(:published_path) }
end
trait :with_service do
service { association :service, administrateur: administrateurs.first }
end
trait :with_instructeur do
after(:create) do |procedure, _evaluator|
procedure.defaut_groupe_instructeur.instructeurs << build(:instructeur)
end
end
trait :routee do
after(:create) do |procedure, _evaluator|
procedure.groupe_instructeurs.create(label: 'deuxième groupe')
end
end
trait :for_individual do
for_individual { true }
end
trait :with_auto_archive do
auto_archive_on { Time.zone.today + 20 }
end
2015-11-05 11:21:44 +01:00
trait :with_type_de_champ do
transient do
2018-09-04 16:46:48 +02:00
types_de_champ_count { 1 }
end
after(:build) do |procedure, evaluator|
2020-08-27 19:55:37 +02:00
evaluator.types_de_champ_count.times do |position|
build(:type_de_champ, procedure: procedure, position: position)
end
end
end
trait :with_type_de_champ_private do
transient do
2018-09-04 16:46:48 +02:00
types_de_champ_private_count { 1 }
end
after(:build) do |procedure, evaluator|
2020-08-27 19:55:37 +02:00
evaluator.types_de_champ_private_count.times do |position|
build(:type_de_champ, :private, procedure: procedure, position: position)
end
end
end
trait :with_type_de_champ_mandatory do
after(:build) do |procedure, _evaluator|
2020-08-27 19:55:37 +02:00
build(:type_de_champ, mandatory: true, procedure: procedure)
end
end
trait :with_datetime do
after(:build) do |procedure, _evaluator|
2020-08-27 19:55:37 +02:00
build(:type_de_champ_datetime, mandatory: true, procedure: procedure)
end
end
trait :with_dossier_link do
after(:build) do |procedure, _evaluator|
2020-08-27 19:55:37 +02:00
build(:type_de_champ_dossier_link, procedure: procedure)
end
end
trait :with_yes_no do
after(:build) do |procedure, _evaluator|
2020-08-27 19:55:37 +02:00
build(:type_de_champ_yes_no, procedure: procedure)
end
end
2021-08-18 14:14:16 +02:00
trait :with_commune do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_communes, procedure: procedure)
end
end
trait :with_piece_justificative do
after(:build) do |procedure, _evaluator|
2020-08-27 19:55:37 +02:00
build(:type_de_champ_piece_justificative, procedure: procedure)
end
end
trait :with_titre_identite do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_titre_identite, procedure: procedure)
end
end
trait :with_repetition do
after(:build) do |procedure, _evaluator|
2020-08-27 19:55:37 +02:00
build(:type_de_champ_repetition, :with_types_de_champ, procedure: procedure)
end
end
trait :with_number do
after(:build) do |procedure, _evaluator|
2020-08-27 19:55:37 +02:00
build(:type_de_champ_number, procedure: procedure)
end
end
2020-09-08 15:31:06 +02:00
trait :with_phone do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_phone, procedure: procedure)
end
end
2021-05-19 15:59:18 +02:00
trait :with_address do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_address, procedure: procedure)
end
end
2021-09-28 15:11:15 +02:00
trait :with_cnaf do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_cnaf, procedure: procedure)
end
end
2021-11-24 17:30:35 +01:00
trait :with_dgfip do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_dgfip, procedure: procedure)
end
end
trait :with_pole_emploi do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_pole_emploi, procedure: procedure)
end
end
2021-12-15 15:25:02 +01:00
trait :with_mesri do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_mesri, procedure: procedure)
end
end
trait :with_explication do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_explication, procedure: procedure)
end
end
trait :published do
aasm_state { :publiee }
path { generate(:published_path) }
published_at { Time.zone.now }
unpublished_at { nil }
closed_at { nil }
end
2017-07-04 13:52:08 +02:00
trait :closed do
published
aasm_state { :close }
published_at { Time.zone.now - 1.second }
closed_at { Time.zone.now }
end
2019-12-04 15:45:06 +01:00
trait :unpublished do
published
aasm_state { :depubliee }
published_at { Time.zone.now - 1.second }
unpublished_at { Time.zone.now }
2019-12-04 15:45:06 +01:00
end
2020-02-05 16:09:03 +01:00
trait :discarded do
2020-03-26 09:08:52 +01:00
hidden_at { Time.zone.now }
2017-07-04 13:52:08 +02:00
end
2018-02-21 18:32:07 +01:00
2018-05-30 14:21:17 +02:00
trait :whitelisted do
after(:build) do |procedure, _evaluator|
2018-10-25 15:07:15 +02:00
procedure.update(whitelisted_at: Time.zone.now)
2018-05-30 14:21:17 +02:00
end
end
2018-04-26 14:36:27 +02:00
trait :with_notice do
after(:create) do |procedure, _evaluator|
procedure.notice.attach(
io: StringIO.new('Hello World'),
filename: 'hello.txt',
# we don't want to run virus scanner on this file
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
2018-05-31 11:00:22 +02:00
)
end
end
trait :with_deliberation do
after(:create) do |procedure, _evaluator|
procedure.deliberation.attach(
io: StringIO.new('Hello World'),
filename: 'hello.txt',
# we don't want to run virus scanner on this file
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
2018-04-26 14:36:27 +02:00
)
end
end
2018-02-21 18:32:07 +01:00
trait :with_all_champs_mandatory do
after(:build) do |procedure, _evaluator|
2020-08-27 19:55:37 +02:00
TypeDeChamp.type_champs.map.with_index do |(libelle, type_champ), index|
if libelle == 'drop_down_list'
libelle = 'simple_drop_down_list'
end
2020-08-27 19:55:37 +02:00
build(:"type_de_champ_#{type_champ}", procedure: procedure, mandatory: true, libelle: libelle, position: index)
end
2020-08-27 19:55:37 +02:00
build(:type_de_champ_drop_down_list, :long, procedure: procedure, mandatory: true, libelle: 'simple_choice_drop_down_list_long', position: TypeDeChamp.type_champs.size)
build(:type_de_champ_multiple_drop_down_list, :long, procedure: procedure, mandatory: true, libelle: 'multiple_choice_drop_down_list_long', position: TypeDeChamp.type_champs.size + 1)
end
end
trait :with_all_champs do
after(:build) do |procedure, _evaluator|
2020-08-27 19:55:37 +02:00
TypeDeChamp.type_champs.map.with_index do |(libelle, type_champ), index|
if libelle == 'drop_down_list'
libelle = 'simple_drop_down_list'
end
2020-08-27 19:55:37 +02:00
build(:"type_de_champ_#{type_champ}", procedure: procedure, libelle: libelle, position: index)
end
end
end
trait :with_all_annotations do
after(:build) do |procedure, _evaluator|
2020-08-27 19:55:37 +02:00
TypeDeChamp.type_champs.map.with_index do |(libelle, type_champ), index|
if libelle == 'drop_down_list'
libelle = 'simple_drop_down_list'
end
2020-08-27 19:55:37 +02:00
build(:"type_de_champ_#{type_champ}", procedure: procedure, private: true, libelle: libelle, position: index)
end
2018-02-21 18:32:07 +01:00
end
end
trait :with_dossier_submitted_message do
after(:build) do |procedure, _evaluator|
build(:dossier_submitted_message, revisions: [procedure.active_revision])
end
end
end
end
def add_types_de_champs(types_de_champ, to: nil, scope: :public)
revision = to
association_name = scope == :private ? :revision_types_de_champ_private : :revision_types_de_champ
types_de_champ.each do |type_de_champ|
type_de_champ.revision = revision
type_de_champ.private = (scope == :private)
type_de_champ.revision.public_send(association_name) << build(:procedure_revision_type_de_champ,
revision: revision,
position: type_de_champ.order_place,
type_de_champ: type_de_champ)
end
end