demarches-normaliennes/spec/models/export_template_spec.rb

346 lines
15 KiB
Ruby
Raw Normal View History

2024-03-02 22:13:09 +01:00
describe ExportTemplate do
let(:groupe_instructeur) { create(:groupe_instructeur, procedure:) }
2024-05-28 10:06:30 +02:00
let(:export_template) { create(:export_template, :with_custom_content, groupe_instructeur:, content:) }
2024-03-20 16:02:34 +01:00
let(:procedure) { create(:procedure_with_dossiers, types_de_champ_public:, for_individual:) }
let(:dossier) { procedure.dossiers.first }
let(:for_individual) { false }
let(:types_de_champ_public) do
[
{ type: :piece_justificative, libelle: "Justificatif de domicile", mandatory: true, stable_id: 3 },
{ type: :titre_identite, libelle: "CNI", mandatory: true, stable_id: 5 }
]
end
2024-03-02 22:13:09 +01:00
let(:content) do
{
"pdf_name" => {
"type" => "doc",
"content" => [
{ "type" => "paragraph", "content" => [{ "text" => "mon_export_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }
]
},
"default_dossier_directory" => {
"type" => "doc",
"content" => [
{ "type" => "paragraph", "content" => [{ "text" => "DOSSIER_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }] }
]
},
"pjs" =>
[
2024-03-19 11:03:29 +01:00
{ path: { "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "type" => "mention", "attrs" => { "id" => "original-filename", "label" => "nom original du fichier" } }, { "text" => " _justif", "type" => "text" }] }] }, stable_id: "3" },
{
path:
{ "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"
},
{
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" }] }] },
stable_id: "10"
}
2024-03-02 22:13:09 +01:00
]
}
end
describe 'new' do
let(:export_template) { build(:export_template, groupe_instructeur: groupe_instructeur) }
2024-03-03 10:19:02 +01:00
it 'set default values' do
export_template.set_default_values
expect(export_template.content).to eq({
"pdf_name" => {
"type" => "doc",
"content" => [
{ "type" => "paragraph", "content" => [{ "text" => "export_", "type" => "text" }, { "type" => "mention", "attrs" => ExportTemplate::DOSSIER_ID_TAG.stringify_keys }] }
]
},
"default_dossier_directory" => {
"type" => "doc",
"content" => [
{ "type" => "paragraph", "content" => [{ "text" => "dossier-", "type" => "text" }, { "type" => "mention", "attrs" => ExportTemplate::DOSSIER_ID_TAG.stringify_keys }] }
]
},
"pjs" =>
[
{
2024-03-20 16:02:34 +01:00
"stable_id" => "3",
2024-03-03 10:19:02 +01:00
"path" => { "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [{ "text" => "justificatif-de-domicile-", "type" => "text" }, { "type" => "mention", "attrs" => ExportTemplate::DOSSIER_ID_TAG.stringify_keys }] }] }
}
]
})
end
2024-03-02 22:13:09 +01:00
end
2024-05-29 11:08:46 +02:00
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
2024-03-02 22:13:09 +01:00
describe '#tiptap_default_dossier_directory' do
it 'returns tiptap_default_dossier_directory from content' do
expect(export_template.tiptap_default_dossier_directory).to eq({
"type" => "doc",
"content" => [
{ "type" => "paragraph", "content" => [{ "text" => "DOSSIER_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " ", "type" => "text" }] }
]
}.to_json)
end
end
describe '#tiptap_pdf_name' do
it 'returns tiptap_pdf_name from content' do
expect(export_template.tiptap_pdf_name).to eq({
"type" => "doc",
"content" => [
{ "type" => "paragraph", "content" => [{ "text" => "mon_export_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }] }
]
}.to_json)
end
end
2024-03-03 10:03:12 +01:00
describe '#content_for_pj' do
let(:type_de_champ_pj) { create(:type_de_champ_piece_justificative, stable_id: 3, libelle: 'Justificatif de domicile', procedure:) }
let(:champ_pj) { create(:champ_piece_justificative, type_de_champ: type_de_champ_pj) }
let(:attachment) { ActiveStorage::Attachment.new(name: 'pj', record: champ_pj, blob: ActiveStorage::Blob.new(filename: "superpj.png")) }
it 'returns tiptap content for pj' do
expect(export_template.content_for_pj(type_de_champ_pj)).to eq({
2024-03-19 11:03:29 +01:00
"type" => "doc",
"content" => [
{ "type" => "paragraph", "content" => [{ "type" => "mention", "attrs" => { "id" => "original-filename", "label" => "nom original du fichier" } }, { "text" => " _justif", "type" => "text" }] }
2024-03-03 10:03:12 +01:00
]
}.to_json)
end
end
2024-03-02 22:13:09 +01:00
describe '#attachment_and_path' do
let(:dossier) { create(:dossier) }
context 'for export pdf' do
let(:attachment) { double("attachment") }
it 'gives absolute filename for export of specific dossier' do
allow(attachment).to receive(:name).and_return('pdf_export_for_instructeur')
expect(export_template.attachment_and_path(dossier, attachment)).to eq([attachment, "DOSSIER_#{dossier.id}/mon_export_#{dossier.id}.pdf"])
end
end
2024-03-03 10:03:12 +01:00
context 'for pj' do
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
let(:champ_pj) { dossier.champs.find(&:piece_justificative?) }
2024-03-03 10:03:12 +01:00
let(:attachment) { ActiveStorage::Attachment.new(name: 'pj', record: champ_pj, blob: ActiveStorage::Blob.new(filename: "superpj.png")) }
it 'returns pj and custom name for pj' do
2024-05-21 17:05:34 +02:00
expect(export_template.attachment_and_path(dossier, attachment, champ: champ_pj)).to eq([attachment, "DOSSIER_#{dossier.id}/superpj_justif-1.png"])
2024-03-03 10:03:12 +01:00
end
end
context 'pj repetable' do
let(:procedure) { create(:procedure, :for_individual, types_de_champ_public:) }
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
2024-03-03 10:03:12 +01:00
let(:draft) { procedure.draft_revision }
let(:types_de_champ_public) do
[
{
type: :repetition,
stable_id: 3333,
mandatory: true, children: [
{ type: :text, libelle: 'sub type de champ' },
{ type: :piece_justificative, stable_id: 10, libelle: 'pj repet' }
]
}
]
2024-03-03 10:03:12 +01:00
end
let(:champ_pj) { dossier.champs.find(&:piece_justificative?) }
2024-03-03 10:03:12 +01:00
let(:attachment) { ActiveStorage::Attachment.new(name: 'pj', record: champ_pj, blob: ActiveStorage::Blob.new(filename: "superpj.png")) }
it 'rename repetable pj' do
2024-05-21 17:05:34 +02:00
expect(export_template.attachment_and_path(dossier, attachment, champ: champ_pj)).to eq([attachment, "DOSSIER_#{dossier.id}/pj_repet_#{dossier.id}-1.png"])
2024-03-03 10:03:12 +01:00
end
end
2024-03-02 22:13:09 +01:00
end
2024-03-03 10:11:26 +01:00
2024-03-20 16:02:34 +01:00
describe '#tiptap_convert' do
it 'convert default dossier directory' do
expect(export_template.tiptap_convert(procedure.dossiers.first, "default_dossier_directory")).to eq "DOSSIER_#{dossier.id}"
end
it 'convert pdf_name' do
expect(export_template.tiptap_convert(procedure.dossiers.first, "pdf_name")).to eq "mon_export_#{dossier.id}"
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
2024-03-20 16:02:34 +01:00
end
2024-03-19 11:03:29 +01:00
describe '#tiptap_convert_pj' do
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative, stable_id: 3, libelle: 'Justificatif de domicile' }]) }
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
let(:champ_pj) { dossier.champs.first }
2024-03-19 11:03:29 +01:00
let(:attachment) { ActiveStorage::Attachment.new(name: 'pj', record: champ_pj, blob: ActiveStorage::Blob.new(filename: "superpj.png")) }
it 'convert pj' do
attachment
expect(export_template.tiptap_convert_pj(dossier, 3, attachment)).to eq "superpj_justif"
2024-03-19 11:03:29 +01:00
end
end
2024-03-03 10:11:26 +01:00
describe '#valid?' do
2024-03-20 16:02:34 +01:00
let(:subject) { build(:export_template, groupe_instructeur:, content:) }
2024-03-03 10:11:26 +01:00
let(:ddd_text) { "DoSSIER" }
let(:mention) { { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } } }
let(:ddd_mention) { mention }
let(:pdf_text) { "export" }
let(:pdf_mention) { mention }
let(:pj_text) { "_pj" }
let(:pj_mention) { mention }
let(:content) do
{
"pdf_name" => {
"type" => "doc",
"content" => [
{ "type" => "paragraph", "content" => [{ "text" => pdf_text, "type" => "text" }, pdf_mention] }
]
},
"default_dossier_directory" => {
"type" => "doc",
"content" => [
{ "type" => "paragraph", "content" => [{ "text" => ddd_text, "type" => "text" }, ddd_mention] }
]
},
"pjs" =>
[
{ path: { "type" => "doc", "content" => [{ "type" => "paragraph", "content" => [pj_mention, { "text" => pj_text, "type" => "text" }] }] }, stable_id: "3" }
]
}
end
context 'with valid default dossier directory' do
it 'has no error for default_dossier_directory' do
expect(subject.valid?).to be_truthy
end
end
context 'with no ddd text' do
let(:ddd_text) { " " }
context 'with mention' do
let(:ddd_mention) { { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } } }
it 'has no error for default_dossier_directory' do
expect(subject.valid?).to be_truthy
end
end
2024-03-20 16:02:34 +01:00
context 'without numéro de dossier' do
2024-03-03 10:11:26 +01:00
let(:ddd_mention) { { "type" => "mention", "attrs" => { "id" => 'dossier_service_name', "label" => "nom du service" } } }
2024-03-20 16:02:34 +01:00
it "add error for tiptap_default_dossier_directory" do
2024-03-03 10:11:26 +01:00
expect(subject.valid?).to be_falsey
2024-03-20 16:02:34 +01:00
expect(subject.errors[:tiptap_default_dossier_directory]).to be_present
expect(subject.errors.full_messages).to include "Le champ « Nom du répertoire » doit contenir le numéro du dossier"
2024-03-03 10:11:26 +01:00
end
end
end
context 'with valid pdf name' do
it 'has no error for pdf name' do
expect(subject.valid?).to be_truthy
2024-03-20 16:02:34 +01:00
expect(subject.errors[:tiptap_pdf_name]).not_to be_present
2024-03-03 10:11:26 +01:00
end
end
context 'with pdf text and without mention' do
let(:pdf_text) { "export" }
let(:pdf_mention) { { "type" => "mention", "attrs" => {} } }
it "add no error" do
expect(subject.valid?).to be_truthy
end
end
context 'with no pdf text' do
let(:pdf_text) { " " }
context 'with mention' do
it 'has no error for default_dossier_directory' do
expect(subject.valid?).to be_truthy
2024-03-20 16:02:34 +01:00
expect(subject.errors[:tiptap_pdf_name]).not_to be_present
2024-03-03 10:11:26 +01:00
end
end
context 'without mention' do
let(:pdf_mention) { { "type" => "mention", "attrs" => {} } }
it "add error for pdf name" do
expect(subject.valid?).to be_falsey
2024-05-21 17:05:34 +02:00
expect(subject.errors.full_messages).to include "Le champ « Nom du dossier au format pdf » doit être rempli"
2024-03-03 10:11:26 +01:00
end
end
end
context 'with no pj text' do
2024-03-20 16:02:34 +01:00
# let!(:type_de_champ_pj) { create(:type_de_champ_piece_justificative, stable_id: 3, libelle: 'Justificatif de domicile', procedure:) }
2024-03-03 10:11:26 +01:00
let(:pj_text) { " " }
context 'with mention' do
it 'has no error for pj' do
expect(subject.valid?).to be_truthy
end
end
context 'without mention' do
let(:pj_mention) { { "type" => "mention", "attrs" => {} } }
it "add error for pj" do
expect(subject.valid?).to be_falsey
2024-03-20 16:02:34 +01:00
expect(subject.errors.full_messages).to include "Le champ « Justificatif de domicile » doit être rempli"
2024-03-03 10:11:26 +01:00
end
end
end
end
2024-03-20 16:02:34 +01:00
2024-05-29 11:08:46 +02:00
context 'for entreprise procedure' do
let(:for_individual) { false }
describe 'specific_tags' do
2024-03-20 16:02:34 +01:00
it do
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"]
end
end
2024-05-29 11:08:46 +02:00
describe 'tags_for_pj' do
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
2024-03-20 16:02:34 +01:00
it do
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"]
end
end
2024-05-29 11:08:46 +02:00
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
2024-03-20 16:02:34 +01:00
end
2024-03-02 22:13:09 +01:00
end