Merge pull request #10454 from demarches-simplifiees/9827-add-specs

corrige quelques bugs de la feature export template (renommage dans le zip)
This commit is contained in:
krichtof 2024-06-04 13:18:12 +00:00 committed by GitHub
commit 45814fdbfb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 282 additions and 69 deletions

View file

@ -15,12 +15,12 @@
= link_to download_export_path(export_format: format), role: 'menuitem', data: { turbo_method: :post, turbo: true } do = link_to download_export_path(export_format: format), role: 'menuitem', data: { turbo_method: :post, turbo: true } do
= t(".everything_#{format}_html") = t(".everything_#{format}_html")
- if export_templates.present? - if @procedure.feature_enabled?(:export_template)
- export_templates.each do |export_template| - if export_templates.present?
- export_templates.each do |export_template|
- menu.with_item do
= link_to download_export_path(export_template_id: export_template.id), role: 'menuitem', data: { turbo_method: :post, turbo: true } do
= "Exporter à partir du modèle #{export_template.name}"
- menu.with_item do - menu.with_item do
= link_to download_export_path(export_template_id: export_template.id), role: 'menuitem', data: { turbo_method: :post, turbo: true } do = link_to new_instructeur_export_template_path(procedure_id: params[:procedure_id]), role: 'menuitem' do
= "Exporter à partir du modèle #{export_template.name}" Ajouter un modèle d'export
- if feature_enabled?(:export_template)
- menu.with_item do
= link_to new_instructeur_export_template_path(procedure_id: params[:procedure_id]), role: 'menuitem' do
Ajouter un modèle d'export

View file

@ -11,9 +11,10 @@ class Dossiers::ExportLinkComponent < ApplicationComponent
@export_url = export_url @export_url = export_url
end end
def download_export_path(export_format:, statut:, no_progress_notification: nil) def download_export_path(export_format:, statut:, export_template_id: nil, no_progress_notification: nil)
@export_url.call(@procedure, @export_url.call(@procedure,
export_format: export_format, export_format: export_format,
export_template_id:,
statut: statut, statut: statut,
no_progress_notification: no_progress_notification) no_progress_notification: no_progress_notification)
end end

View file

@ -7,6 +7,9 @@
= export_title(export) = export_title(export)
%span.fr-text-mention--grey.fr-mb-1w %span.fr-text-mention--grey.fr-mb-1w
= time_info(export) = time_info(export)
- if export.export_template
%span.fr-tag.fr-tag--sm.fr-ml-1w
= export.export_template.name
.fr-ml-auto .fr-ml-auto
= badge(export) = badge(export)
@ -14,4 +17,4 @@
= export_button(export) = export_button(export)
- if export.failed? - if export.failed?
= button_to refresh_button_options(export)[:title], download_export_path(export_format: export.format, statut: export.statut), refresh_button_options(export) = button_to refresh_button_options(export)[:title], download_export_path(export_template_id: export.export_template&.id, export_format: export.format, statut: export.statut), refresh_button_options(export)

View file

@ -88,10 +88,6 @@ class Champ < ApplicationRecord
parent_id.present? parent_id.present?
end end
def stable_id_with_row
[row_id, stable_id].compact
end
# used for the `required` html attribute # used for the `required` html attribute
# check visibility to avoid hidden required input # check visibility to avoid hidden required input
# which prevent the form from being sent. # which prevent the form from being sent.

View file

@ -307,7 +307,8 @@ module TagsSubstitutionConcern
def format_date(date) def format_date(date)
if date.present? if date.present?
date.strftime('%d/%m/%Y') format = defined?(self.class::FORMAT_DATE) ? self.class::FORMAT_DATE : '%d/%m/%Y'
date.strftime(format)
else else
'' ''
end end

View file

@ -7,6 +7,7 @@ class ExportTemplate < ApplicationRecord
validates_with ExportTemplateValidator validates_with ExportTemplateValidator
DOSSIER_STATE = Dossier.states.fetch(:en_construction) DOSSIER_STATE = Dossier.states.fetch(:en_construction)
FORMAT_DATE = "%Y-%m-%d"
def set_default_values def set_default_values
content["default_dossier_directory"] = tiptap_json("dossier-") content["default_dossier_directory"] = tiptap_json("dossier-")
@ -48,7 +49,7 @@ class ExportTemplate < ApplicationRecord
def attachment_and_path(dossier, attachment, index: 0, row_index: nil, champ: nil) def attachment_and_path(dossier, attachment, index: 0, row_index: nil, champ: nil)
[ [
attachment, attachment,
path(dossier, attachment, index, row_index, champ) path(dossier, attachment, index:, row_index:, champ:)
] ]
end end
@ -116,7 +117,7 @@ class ExportTemplate < ApplicationRecord
"#{render_attributes_for(content["pdf_name"], dossier)}.pdf" "#{render_attributes_for(content["pdf_name"], dossier)}.pdf"
end end
def path(dossier, attachment, index, row_index, champ) def path(dossier, attachment, index: 0, row_index: nil, champ: nil)
if attachment.name == 'pdf_export_for_instructeur' if attachment.name == 'pdf_export_for_instructeur'
return export_path(dossier) return export_path(dossier)
end end
@ -128,6 +129,8 @@ class ExportTemplate < ApplicationRecord
'messagerie' 'messagerie'
when 'Avis' when 'Avis'
'avis' 'avis'
when 'Attestation', 'Etablissement'
'pieces_justificatives'
else else
# for attachment # for attachment
return attachment_path(dossier, attachment, index, row_index, champ) return attachment_path(dossier, attachment, index, row_index, champ)

View file

@ -169,7 +169,12 @@ class PiecesJustificativesService
.filter { |a| safe_attachment(a) } .filter { |a| safe_attachment(a) }
.map do |a| .map do |a|
dossier_id = commentaire_id_dossier_id[a.record_id] dossier_id = commentaire_id_dossier_id[a.record_id]
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) if @export_template
dossier = dossiers.find { _1.id == dossier_id }
@export_template.attachment_and_path(dossier, a)
else
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
end
end end
end end
@ -190,7 +195,12 @@ class PiecesJustificativesService
.where(record_type: "Etablissement", record_id: etablissement_id_dossier_id.keys) .where(record_type: "Etablissement", record_id: etablissement_id_dossier_id.keys)
.map do |a| .map do |a|
dossier_id = etablissement_id_dossier_id[a.record_id] dossier_id = etablissement_id_dossier_id[a.record_id]
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) if @export_template
dossier = dossiers.find { _1.id == dossier_id }
@export_template.attachment_and_path(dossier, a)
else
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
end
end end
end end
@ -201,7 +211,12 @@ class PiecesJustificativesService
.filter { |a| safe_attachment(a) } .filter { |a| safe_attachment(a) }
.map do |a| .map do |a|
dossier_id = a.record_id dossier_id = a.record_id
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) if @export_template
dossier = dossiers.find { _1.id == dossier_id }
@export_template.attachment_and_path(dossier, a)
else
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
end
end end
end end
@ -217,7 +232,12 @@ class PiecesJustificativesService
.where(record_type: "Attestation", record_id: attestation_id_dossier_id.keys) .where(record_type: "Attestation", record_id: attestation_id_dossier_id.keys)
.map do |a| .map do |a|
dossier_id = attestation_id_dossier_id[a.record_id] dossier_id = attestation_id_dossier_id[a.record_id]
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) if @export_template
dossier = dossiers.find { _1.id == dossier_id }
@export_template.attachment_and_path(dossier, a)
else
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
end
end end
end end
@ -241,7 +261,12 @@ class PiecesJustificativesService
.filter { |a| safe_attachment(a) } .filter { |a| safe_attachment(a) }
.map do |a| .map do |a|
dossier_id = avis_ids_dossier_id[a.record_id] dossier_id = avis_ids_dossier_id[a.record_id]
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a) if @export_template
dossier = dossiers.find { _1.id == dossier_id }
@export_template.attachment_and_path(dossier, a)
else
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
end
end end
end end

View file

@ -23,7 +23,7 @@
- else - else
= t('.no_export_html', expiration_time: Export::MAX_DUREE_CONSERVATION_EXPORT.in_hours.to_i ) = t('.no_export_html', expiration_time: Export::MAX_DUREE_CONSERVATION_EXPORT.in_hours.to_i )
- if feature_enabled?(:export_template) - if @procedure.feature_enabled?(:export_template)
%h2.fr-mb-1w.fr-mt-8w %h2.fr-mb-1w.fr-mt-8w
Liste des modèles d'export Liste des modèles d'export
%p.fr-hint-text %p.fr-hint-text

View file

@ -21,26 +21,45 @@ describe Instructeurs::ExportTemplatesController, type: :controller do
{ "type" => "paragraph", "content" => [{ "text" => "DOSSIER_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }] } { "type" => "paragraph", "content" => [{ "text" => "DOSSIER_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }] }
] ]
}.to_json, }.to_json,
"pjs" => tiptap_pj_3: {
[ "type" => "doc",
{ path: { "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " _justif", "type" => "text" }] }] }, stable_id: "3" }, "content" => [{ "type" => "paragraph", "content" => [{ "type" => "text", "text" => "avis-commission-" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }]
{ }.to_json,
path: tiptap_pj_5: {
{ "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "text" => "cni_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }] }] },
stable_id: "5" "type" => "doc",
}, "content" => [{ "type" => "paragraph", "content" => [{ "type" => "text", "text" => "avis-commission-" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }]
{ }.to_json,
path: { "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "text" => "pj_repet_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }] }] }, tiptap_pj_10: {
stable_id: "10"
} "type" => "doc",
] "content" => [{ "type" => "paragraph", "content" => [{ "type" => "text", "text" => "avis-commission-" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }]
}.to_json
} }
end end
let(:instructeur) { create(:instructeur) } let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, instructeurs: [instructeur]) } let(:procedure) do
create(
:procedure, instructeurs: [instructeur],
types_de_champ_public: [
{ type: :piece_justificative, libelle: "pj1", stable_id: 3 },
{ type: :piece_justificative, libelle: "pj2", stable_id: 5 },
{ type: :piece_justificative, libelle: "pj3", stable_id: 10 }
]
)
end
let(:groupe_instructeur) { procedure.defaut_groupe_instructeur } let(:groupe_instructeur) { procedure.defaut_groupe_instructeur }
describe '#new' do
let(:subject) { get :new, params: { procedure_id: procedure.id } }
it do
subject
expect(assigns(:export_template)).to be_present
end
end
describe '#create' do describe '#create' do
let(:subject) { post :create, params: { procedure_id: procedure.id, export_template: export_template_params } } let(:subject) { post :create, params: { procedure_id: procedure.id, export_template: export_template_params } }
@ -130,4 +149,19 @@ describe Instructeurs::ExportTemplatesController, type: :controller do
end end
end end
end end
describe '#preview' do
render_views
let(:export_template) { create(:export_template, groupe_instructeur:) }
let(:subject) { get :preview, params: { procedure_id: procedure.id, id: export_template.id, export_template: export_template_params }, format: :turbo_stream }
it '' do
dossier = create(:dossier, procedure: procedure, for_procedure_preview: true)
subject
expect(response.body).to include "DOSSIER_#{dossier.id}"
expect(response.body).to include "mon_export_#{dossier.id}.pdf"
end
end
end end

View file

@ -11,24 +11,78 @@ FactoryBot.define do
{ "type" => "paragraph", "content" => [{ "text" => "export_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_id", "label" => "id dossier" } }, { "text" => " .pdf", "type" => "text" }] } { "type" => "paragraph", "content" => [{ "text" => "export_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_id", "label" => "id dossier" } }, { "text" => " .pdf", "type" => "text" }] }
] ]
}, },
"default_dossier_directory" => "default_dossier_directory" => {
{ "type" => "doc",
"type" => "doc", "content" =>
"content" => [
[ {
{ "type" => "paragraph",
"type" => "paragraph", "content" =>
"content" => [
[ { "text" => "dossier_", "type" => "text" },
{ "text" => "dossier_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } },
{ "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }
{ "text" => " ", "type" => "text" } ]
] }
]
} }
]
}
} }
} }
kind { "zip" } kind { "zip" }
to_create do |export_template, _context|
export_template.set_default_values
export_template.save
end
trait :with_custom_content do
to_create do |export_template, context|
export_template.set_default_values
export_template.content = context.content
export_template.save
end
end
trait :with_custom_ddd_prefix do
transient do
ddd_prefix { 'dossier_' }
end
to_create do |export_template, context|
export_template.set_default_values
export_template.content["default_dossier_directory"]["content"] = [
{
"type" => "paragraph",
"content" =>
[
{ "text" => context.ddd_prefix, "type" => "text" },
{ "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } },
{ "text" => " ", "type" => "text" }
]
}
]
export_template.save
end
end
trait :with_date_depot_for_export_pdf do
to_create do |export_template, _|
export_template.set_default_values
export_template.content["pdf_name"]["content"] = [
{
"type" => "paragraph",
"content" =>
[
{ "text" => "export_", "type" => "text" },
{ "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } },
{ "text" => "-", "type" => "text" },
{ "type" => "mention", "attrs" => { "id" => "dossier_depose_at", "label" => "date de dépôt" } },
{ "text" => " ", "type" => "text" }
]
}
]
export_template.save
end
end
end end
end end

View file

@ -1,6 +1,6 @@
describe ExportTemplate do describe ExportTemplate do
let(:groupe_instructeur) { create(:groupe_instructeur, procedure:) } let(:groupe_instructeur) { create(:groupe_instructeur, procedure:) }
let(:export_template) { create(:export_template, groupe_instructeur:, content:) } let(:export_template) { create(:export_template, :with_custom_content, groupe_instructeur:, content:) }
let(:procedure) { create(:procedure_with_dossiers, types_de_champ_public:, for_individual:) } let(:procedure) { create(:procedure_with_dossiers, types_de_champ_public:, for_individual:) }
let(:dossier) { procedure.dossiers.first } let(:dossier) { procedure.dossiers.first }
let(:for_individual) { false } let(:for_individual) { false }
@ -69,6 +69,22 @@ describe ExportTemplate do
end end
end end
describe '#assign_pj_names' do
let(:pj_params) do
{
"tiptap_pj_1" => {
"type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "type" => "text", "text" => "avis-commission-" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }]
}.to_json
}
end
it 'values content from pj params' do
export_template.assign_pj_names(pj_params)
expect(export_template.content["pjs"]).to eq [
{ :path => { "content" => [{ "content" => [{ "text" => "avis-commission-", "type" => "text" }, { "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" }, "type" => "mention" }], "type" => "paragraph" }], "type" => "doc" }, :stable_id => "1" }
]
end
end
describe '#tiptap_default_dossier_directory' do describe '#tiptap_default_dossier_directory' do
it 'returns tiptap_default_dossier_directory from content' do it 'returns tiptap_default_dossier_directory from content' do
expect(export_template.tiptap_default_dossier_directory).to eq({ expect(export_template.tiptap_default_dossier_directory).to eq({
@ -174,6 +190,14 @@ describe ExportTemplate do
it 'convert pdf_name' do it 'convert pdf_name' do
expect(export_template.tiptap_convert(procedure.dossiers.first, "pdf_name")).to eq "mon_export_#{dossier.id}" expect(export_template.tiptap_convert(procedure.dossiers.first, "pdf_name")).to eq "mon_export_#{dossier.id}"
end end
context 'for date' do
let(:export_template) { create(:export_template, :with_date_depot_for_export_pdf, groupe_instructeur:) }
let(:dossier) { create(:dossier, :en_construction, procedure:, depose_at: Date.parse("2024/03/30")) }
it 'convert date with dash' do
expect(export_template.tiptap_convert(dossier, "pdf_name")).to eq "export_#{dossier.id}-2024-03-30"
end
end
end end
describe '#tiptap_convert_pj' do describe '#tiptap_convert_pj' do
@ -297,21 +321,37 @@ describe ExportTemplate do
end end
end end
describe 'specific_tags' do context 'for entreprise procedure' do
context 'for entreprise procedure' do let(:for_individual) { false }
let(:for_individual) { false } describe 'specific_tags' do
it do it do
tags = export_template.specific_tags tags = export_template.specific_tags
expect(tags.map { _1[:id] }).to eq ["entreprise_siren", "entreprise_numero_tva_intracommunautaire", "entreprise_siret_siege_social", "entreprise_raison_sociale", "entreprise_adresse", "dossier_depose_at", "dossier_procedure_libelle", "dossier_service_name", "dossier_number", "dossier_groupe_instructeur"] expect(tags.map { _1[:id] }).to eq ["entreprise_siren", "entreprise_numero_tva_intracommunautaire", "entreprise_siret_siege_social", "entreprise_raison_sociale", "entreprise_adresse", "dossier_depose_at", "dossier_procedure_libelle", "dossier_service_name", "dossier_number", "dossier_groupe_instructeur"]
end end
end end
context 'for individual procedure' do describe 'tags_for_pj' do
let(:for_individual) { true } it do
tags = export_template.tags_for_pj
expect(tags.map { _1[:id] }).to eq ["entreprise_siren", "entreprise_numero_tva_intracommunautaire", "entreprise_siret_siege_social", "entreprise_raison_sociale", "entreprise_adresse", "dossier_depose_at", "dossier_procedure_libelle", "dossier_service_name", "dossier_number", "dossier_groupe_instructeur", "original-filename"]
end
end
end
context 'for individual procedure' do
let(:for_individual) { true }
describe 'specific_tags' do
it do it do
tags = export_template.specific_tags tags = export_template.specific_tags
expect(tags.map { _1[:id] }).to eq ["individual_gender", "individual_last_name", "individual_first_name", "dossier_depose_at", "dossier_procedure_libelle", "dossier_service_name", "dossier_number", "dossier_groupe_instructeur"] expect(tags.map { _1[:id] }).to eq ["individual_gender", "individual_last_name", "individual_first_name", "dossier_depose_at", "dossier_procedure_libelle", "dossier_service_name", "dossier_number", "dossier_groupe_instructeur"]
end end
end end
describe 'tags_for_pj' do
it do
tags = export_template.tags_for_pj
expect(tags.map { _1[:id] }).to eq ["individual_gender", "individual_last_name", "individual_first_name", "dossier_depose_at", "dossier_procedure_libelle", "dossier_service_name", "dossier_number", "dossier_groupe_instructeur", "original-filename"]
end
end
end end
end end

View file

@ -110,7 +110,7 @@ describe PiecesJustificativesService do
it { expect(subject).to match_array(pj_champ.call(dossier).piece_justificative_file.attachments) } it { expect(subject).to match_array(pj_champ.call(dossier).piece_justificative_file.attachments) }
context 'with export_template' do context 'with export_template' do
let(:export_template) { create(:export_template, groupe_instructeur: procedure.defaut_groupe_instructeur).tap(&:set_default_values) } let(:export_template) { create(:export_template, groupe_instructeur: procedure.defaut_groupe_instructeur) }
it { expect(subject).to match_array(pj_champ.call(dossier).piece_justificative_file.attachments) } it { expect(subject).to match_array(pj_champ.call(dossier).piece_justificative_file.attachments) }
end end
end end
@ -164,6 +164,13 @@ describe PiecesJustificativesService do
end end
it { expect(subject).to match_array(dossier.commentaires.first.piece_jointe.attachments) } it { expect(subject).to match_array(dossier.commentaires.first.piece_jointe.attachments) }
context 'with export_template' do
let(:export_template) { create(:export_template, :with_custom_ddd_prefix, ddd_prefix: "DOSSIER-", groupe_instructeur: procedure.defaut_groupe_instructeur) }
it 'uses specific name for dossier directory' do
expect(PiecesJustificativesService.new(user_profile:, export_template:).liste_documents(dossiers).map(&:second)[0].starts_with?("DOSSIER-#{dossier.id}/messagerie")).to be true
end
end
end end
context 'with a pj not safe on a commentaire' do context 'with a pj not safe on a commentaire' do
@ -180,6 +187,13 @@ describe PiecesJustificativesService do
let!(:witness) { create(:dossier, :with_justificatif) } let!(:witness) { create(:dossier, :with_justificatif) }
it { expect(subject).to match_array(dossier.justificatif_motivation.attachment) } it { expect(subject).to match_array(dossier.justificatif_motivation.attachment) }
context 'with export_template' do
let(:export_template) { create(:export_template, :with_custom_ddd_prefix, ddd_prefix: "DOSSIER-", groupe_instructeur: procedure.defaut_groupe_instructeur) }
it 'uses specific name for dossier directory' do
expect(PiecesJustificativesService.new(user_profile:, export_template:).liste_documents(dossiers).map(&:second)[0].starts_with?("DOSSIER-#{dossier.id}/dossier")).to be true
end
end
end end
context 'with a motivation not safe' do context 'with a motivation not safe' do
@ -195,6 +209,16 @@ describe PiecesJustificativesService do
let!(:witness) { create(:dossier, :with_attestation) } let!(:witness) { create(:dossier, :with_attestation) }
it { expect(subject).to match_array(dossier.attestation.pdf.attachment) } it { expect(subject).to match_array(dossier.attestation.pdf.attachment) }
it 'uses default name for dossier directory' do
expect(PiecesJustificativesService.new(user_profile:, export_template: nil).liste_documents(dossiers).map(&:second)[0].starts_with?("dossier-#{dossier.id}/pieces_justificatives")).to be true
end
context 'with export_template' do
let(:export_template) { create(:export_template, :with_custom_ddd_prefix, ddd_prefix: "DOSSIER-", groupe_instructeur: procedure.defaut_groupe_instructeur) }
it 'uses specific name for dossier directory' do
expect(PiecesJustificativesService.new(user_profile:, export_template:).liste_documents(dossiers).map(&:second)[0].starts_with?("DOSSIER-#{dossier.id}/pieces_justificatives")).to be true
end
end
end end
context 'with an etablissement' do context 'with an etablissement' do
@ -212,6 +236,17 @@ describe PiecesJustificativesService do
end end
it { expect(subject).to match_array([attestation_sociale.attachment, attestation_fiscale.attachment]) } it { expect(subject).to match_array([attestation_sociale.attachment, attestation_fiscale.attachment]) }
it 'uses default name for dossier directory' do
expect(PiecesJustificativesService.new(user_profile:, export_template: nil).liste_documents(dossiers).map(&:second)[0].starts_with?("dossier-#{dossier.id}/pieces_justificatives")).to be true
end
context 'with export_template' do
let(:export_template) { create(:export_template, :with_custom_ddd_prefix, ddd_prefix: "DOSSIER-", groupe_instructeur: procedure.defaut_groupe_instructeur) }
it 'uses specific name for dossier directory' do
expect(PiecesJustificativesService.new(user_profile:, export_template:).liste_documents(dossiers).map(&:second)[0].starts_with?("DOSSIER-#{dossier.id}/pieces_justificatives")).to be true
end
end
end end
end end
@ -323,14 +358,14 @@ describe PiecesJustificativesService do
context 'given an administrateur' do context 'given an administrateur' do
let(:user_profile) { build(:administrateur) } let(:user_profile) { build(:administrateur) }
it "doesn't return confidentiel avis.piece_justificative_file" do it "return confidentiel avis.piece_justificative_file" do
expect(subject.size).to eq(2) expect(subject.size).to eq(2)
end end
end end
context 'given an instructeur' do context 'given an instructeur' do
let(:user_profile) { create(:instructeur) } let(:user_profile) { create(:instructeur) }
it "doesn't return confidentiel avis.piece_justificative_file" do it "return confidentiel avis.piece_justificative_file" do
expect(subject.size).to eq(2) expect(subject.size).to eq(2)
end end
end end
@ -346,7 +381,7 @@ describe PiecesJustificativesService do
let(:experts_procedure) { create(:experts_procedure, expert: user_profile, procedure:) } let(:experts_procedure) { create(:experts_procedure, expert: user_profile, procedure:) }
let(:avis) { create(:avis, experts_procedure:, dossier: dossier, confidentiel: true) } let(:avis) { create(:avis, experts_procedure:, dossier: dossier, confidentiel: true) }
let(:user_profile) { create(:expert) } let(:user_profile) { create(:expert) }
it "doesn't return confidentiel avis.piece_justificative_file" do it "return confidentiel avis.piece_justificative_file" do
expect(subject.size).to eq(2) expect(subject.size).to eq(2)
end end
end end
@ -370,21 +405,28 @@ describe PiecesJustificativesService do
context 'given an administrateur' do context 'given an administrateur' do
let(:user_profile) { build(:administrateur) } let(:user_profile) { build(:administrateur) }
it "doesn't return confidentiel avis.piece_justificative_file" do it "return confidentiel avis.piece_justificative_file" do
expect(subject.size).to eq(2) expect(subject.size).to eq(2)
end end
end end
context 'given an instructeur' do context 'given an instructeur' do
let(:user_profile) { create(:instructeur) } let(:user_profile) { create(:instructeur) }
it "doesn't return confidentiel avis.piece_justificative_file" do it "return confidentiel avis.piece_justificative_file" do
expect(subject.size).to eq(2) expect(subject.size).to eq(2)
end end
context 'with export_template' do
let(:export_template) { create(:export_template, :with_custom_ddd_prefix, ddd_prefix: "DOSSIER-", groupe_instructeur: procedure.defaut_groupe_instructeur) }
it 'uses specific name for dossier directory' do
expect(PiecesJustificativesService.new(user_profile:, export_template:).liste_documents(dossiers).map(&:second)[0].starts_with?("DOSSIER-#{dossier.id}/avis")).to be true
end
end
end end
context 'given an expert' do context 'given an expert' do
let(:user_profile) { create(:expert) } let(:user_profile) { create(:expert) }
it "doesn't return confidentiel avis.piece_justificative_file" do it "return confidentiel avis.piece_justificative_file" do
expect(subject.size).to eq(2) expect(subject.size).to eq(2)
end end
end end
@ -397,7 +439,8 @@ describe PiecesJustificativesService do
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :repetition, children: [{ type: :piece_justificative }] }]) } let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :repetition, children: [{ type: :piece_justificative }] }]) }
let(:dossier) { create(:dossier, :with_populated_champs, procedure: procedure) } let(:dossier) { create(:dossier, :with_populated_champs, procedure: procedure) }
let(:dossiers) { Dossier.where(id: dossier.id) } let(:dossiers) { Dossier.where(id: dossier.id) }
subject { PiecesJustificativesService.new(user_profile:, export_template: nil).generate_dossiers_export(dossiers) } let(:export_template) { nil }
subject { PiecesJustificativesService.new(user_profile:, export_template:).generate_dossiers_export(dossiers) }
it "doesn't update dossier" do it "doesn't update dossier" do
expect { subject }.not_to change { dossier.updated_at } expect { subject }.not_to change { dossier.updated_at }
@ -409,11 +452,24 @@ describe PiecesJustificativesService do
let!(:not_confidentiel_avis) { create(:avis, :not_confidentiel, dossier: dossier) } let!(:not_confidentiel_avis) { create(:avis, :not_confidentiel, dossier: dossier) }
let!(:expert_avis) { create(:avis, :confidentiel, dossier: dossier, expert: user_profile) } let!(:expert_avis) { create(:avis, :confidentiel, dossier: dossier, expert: user_profile) }
subject { PiecesJustificativesService.new(user_profile:, export_template: nil).generate_dossiers_export(dossiers) } subject { PiecesJustificativesService.new(user_profile:, export_template:).generate_dossiers_export(dossiers) }
it "includes avis not confidentiel as well as expert's avis" do it "includes avis not confidentiel as well as expert's avis" do
expect_any_instance_of(Dossier).to receive(:avis_for_expert).with(user_profile).and_return([]) expect_any_instance_of(Dossier).to receive(:avis_for_expert).with(user_profile).and_return([])
subject subject
end end
it 'gives default name to export pdf file' do
expect(subject.first.second.starts_with?("dossier-#{dossier.id}/export-#{dossier.id}")).to eq true
end
end
context 'with export template' do
let(:export_template) { create(:export_template, :with_custom_ddd_prefix, ddd_prefix: "DOSSIER-", groupe_instructeur: procedure.defaut_groupe_instructeur) }
subject { PiecesJustificativesService.new(user_profile:, export_template:).generate_dossiers_export(dossiers) }
it 'gives custom name to export pdf file' do
expect(subject.first.second).to eq "DOSSIER-#{dossier.id}/export_#{dossier.id}.pdf"
end
end end
end end

View file

@ -529,7 +529,7 @@ describe ProcedureExportService do
context 'with export_template' do context 'with export_template' do
let!(:dossier) { create(:dossier, :accepte, :with_populated_champs, :with_individual, procedure: procedure) } let!(:dossier) { create(:dossier, :accepte, :with_populated_champs, :with_individual, procedure: procedure) }
let(:dossier_exports) { PiecesJustificativesService.new(user_profile: instructeur, export_template:).generate_dossiers_export(Dossier.where(id: dossier)) } let(:dossier_exports) { PiecesJustificativesService.new(user_profile: instructeur, export_template:).generate_dossiers_export(Dossier.where(id: dossier)) }
let(:export_template) { create(:export_template, groupe_instructeur: procedure.defaut_groupe_instructeur).tap(&:set_default_values) } let(:export_template) { create(:export_template, groupe_instructeur: procedure.defaut_groupe_instructeur) }
before do before do
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io") allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io")
end end

View file

@ -2,7 +2,7 @@ describe ProcedureExportService do
let(:instructeur) { create(:instructeur) } let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative, libelle: 'pj' }, { type: :repetition, children: [{ type: :piece_justificative, libelle: 'repet_pj' }] }]) } let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative, libelle: 'pj' }, { type: :repetition, children: [{ type: :piece_justificative, libelle: 'repet_pj' }] }]) }
let(:dossiers) { create_list(:dossier, 10, procedure: procedure) } let(:dossiers) { create_list(:dossier, 10, procedure: procedure) }
let(:export_template) { create(:export_template, groupe_instructeur: procedure.defaut_groupe_instructeur).tap(&:set_default_values) } let(:export_template) { create(:export_template, groupe_instructeur: procedure.defaut_groupe_instructeur) }
let(:service) { ProcedureExportService.new(procedure, procedure.dossiers, instructeur, export_template) } let(:service) { ProcedureExportService.new(procedure, procedure.dossiers, instructeur, export_template) }
def pj_champ(d) = d.champs_public.find_by(type: 'Champs::PieceJustificativeChamp') def pj_champ(d) = d.champs_public.find_by(type: 'Champs::PieceJustificativeChamp')