2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-12-16 17:27:57 +01:00
|
|
|
describe PiecesJustificativesService do
|
2024-04-30 19:03:27 +02:00
|
|
|
describe 'pjs_for_champs' do
|
2024-06-25 17:17:51 +02:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative, mandatory: false }, { type: :repetition, mandatory: false, children: [{ type: :piece_justificative, mandatory: false }] }]) }
|
2024-04-30 19:03:27 +02:00
|
|
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
|
|
|
let(:dossiers) { Dossier.where(id: dossier.id) }
|
|
|
|
let(:witness) { create(:dossier, procedure: procedure) }
|
|
|
|
let(:export_template) { double('ExportTemplate') }
|
|
|
|
let(:pj_service) { PiecesJustificativesService.new(user_profile:, export_template:) }
|
|
|
|
let(:user_profile) { build(:administrateur) }
|
|
|
|
|
|
|
|
def pj_champ(d) = d.champs_public.find_by(type: 'Champs::PieceJustificativeChamp')
|
|
|
|
def repetition(d) = d.champs.find_by(type: "Champs::RepetitionChamp")
|
|
|
|
def attachments(champ) = champ.piece_justificative_file.attachments
|
|
|
|
|
|
|
|
before { attach_file_to_champ(pj_champ(witness)) }
|
|
|
|
|
|
|
|
subject { pj_service.send(:pjs_for_champs, dossiers) }
|
|
|
|
|
|
|
|
context 'without any attachment' do
|
|
|
|
it { expect(subject).to be_empty }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single attachment' do
|
|
|
|
let(:champ) { pj_champ(dossier) }
|
|
|
|
before { attach_file_to_champ(champ) }
|
|
|
|
|
|
|
|
it do
|
2024-07-23 17:08:39 +02:00
|
|
|
expect(export_template).to receive(:attachment_path)
|
2024-04-30 19:03:27 +02:00
|
|
|
.with(dossier, attachments(pj_champ(dossier)).first, index: 0, row_index: nil, champ:)
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple attachments' do
|
|
|
|
let(:champ) { pj_champ(dossier) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
attach_file_to_champ(champ)
|
|
|
|
attach_file_to_champ(champ)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
2024-07-23 17:08:39 +02:00
|
|
|
expect(export_template).to receive(:attachment_path)
|
2024-04-30 19:03:27 +02:00
|
|
|
.with(dossier, attachments(pj_champ(dossier)).first, index: 0, row_index: nil, champ:)
|
|
|
|
|
2024-07-23 17:08:39 +02:00
|
|
|
expect(export_template).to receive(:attachment_path)
|
2024-04-30 19:03:27 +02:00
|
|
|
.with(dossier, attachments(pj_champ(dossier)).second, index: 1, row_index: nil, champ:)
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a repetition' do
|
|
|
|
let(:first_champ) { repetition(dossier).champs.first }
|
|
|
|
let(:second_champ) { repetition(dossier).champs.second }
|
|
|
|
|
|
|
|
before do
|
2024-08-27 10:29:10 +02:00
|
|
|
repetition(dossier).add_row(updated_by: 'test')
|
2024-04-30 19:03:27 +02:00
|
|
|
attach_file_to_champ(first_champ)
|
|
|
|
attach_file_to_champ(first_champ)
|
|
|
|
|
2024-08-27 10:29:10 +02:00
|
|
|
repetition(dossier).add_row(updated_by: 'test')
|
2024-04-30 19:03:27 +02:00
|
|
|
attach_file_to_champ(second_champ)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
first_child_attachments = attachments(repetition(dossier).champs.first)
|
|
|
|
second_child_attachments = attachments(repetition(dossier).champs.second)
|
|
|
|
|
2024-07-23 17:08:39 +02:00
|
|
|
expect(export_template).to receive(:attachment_path)
|
2024-04-30 19:03:27 +02:00
|
|
|
.with(dossier, first_child_attachments.first, index: 0, row_index: 0, champ: first_champ)
|
|
|
|
|
2024-07-23 17:08:39 +02:00
|
|
|
expect(export_template).to receive(:attachment_path)
|
2024-04-30 19:03:27 +02:00
|
|
|
.with(dossier, first_child_attachments.second, index: 1, row_index: 0, champ: first_champ)
|
|
|
|
|
2024-07-23 17:08:39 +02:00
|
|
|
expect(export_template).to receive(:attachment_path)
|
2024-04-30 19:03:27 +02:00
|
|
|
.with(dossier, second_child_attachments.first, index: 0, row_index: 1, champ: second_champ)
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
callback = lambda { |*_args| count += 1 }
|
|
|
|
ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2024-05-01 21:23:41 +02:00
|
|
|
expect(count).to eq(10)
|
2024-04-30 19:03:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-07 10:39:54 +02:00
|
|
|
describe '.liste_documents' do
|
2024-02-07 17:33:34 +01:00
|
|
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
|
|
|
let(:dossiers) { Dossier.where(id: dossier.id) }
|
2024-07-23 17:08:39 +02:00
|
|
|
let(:default_export_template) { build(:export_template, groupe_instructeur: procedure.defaut_groupe_instructeur) }
|
2024-03-08 17:14:21 +01:00
|
|
|
let(:export_template) { nil }
|
2022-04-07 10:39:54 +02:00
|
|
|
subject do
|
2024-03-08 17:14:21 +01:00
|
|
|
PiecesJustificativesService.new(user_profile:, export_template:).liste_documents(dossiers).map(&:first)
|
2022-04-07 10:39:54 +02:00
|
|
|
end
|
2020-12-16 17:27:57 +01:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'no acl' do
|
|
|
|
let(:user_profile) { build(:administrateur) }
|
2024-01-23 09:52:20 +01:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative }]) }
|
2022-04-07 12:02:20 +02:00
|
|
|
let(:witness) { create(:dossier, procedure: procedure) }
|
2024-07-23 17:08:39 +02:00
|
|
|
def pj_champ(d) = d.champs_public.find { |c| c.type == 'Champs::PieceJustificativeChamp' }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2022-10-25 14:14:24 +02:00
|
|
|
context 'with a single attachment' do
|
2024-02-07 17:33:34 +01:00
|
|
|
before do
|
2024-07-23 17:08:39 +02:00
|
|
|
attach_file_to_champ(pj_champ(dossier))
|
|
|
|
attach_file_to_champ(pj_champ(witness))
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
|
|
|
|
2024-07-23 17:08:39 +02:00
|
|
|
it { expect(subject).to match_array(pj_champ(dossier).piece_justificative_file.attachments) }
|
2024-03-08 17:14:21 +01:00
|
|
|
|
|
|
|
context 'with export_template' do
|
2024-07-23 17:08:39 +02:00
|
|
|
let(:export_template) { build(:export_template, :enabled_pjs, groupe_instructeur: procedure.defaut_groupe_instructeur) }
|
|
|
|
|
|
|
|
it { expect(subject).to match_array(pj_champ(dossier).piece_justificative_file.attachments) }
|
2024-03-08 17:14:21 +01:00
|
|
|
end
|
2022-10-25 14:14:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a multiple attachments' do
|
|
|
|
before do
|
2024-07-23 17:08:39 +02:00
|
|
|
attach_file_to_champ(pj_champ(dossier))
|
|
|
|
attach_file_to_champ(pj_champ(witness))
|
|
|
|
attach_file_to_champ(pj_champ(dossier))
|
2022-10-25 14:14:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject.count).to eq(2) }
|
2024-07-23 17:08:39 +02:00
|
|
|
it { expect(subject).to match_array(pj_champ(dossier).piece_justificative_file.attachments) }
|
2022-10-25 14:14:24 +02:00
|
|
|
end
|
2022-04-08 14:45:51 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'with a pj not safe on a champ' do
|
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative }]) }
|
|
|
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
2022-04-07 12:02:20 +02:00
|
|
|
|
2024-07-23 17:08:39 +02:00
|
|
|
before { attach_file_to_champ(pj_champ(dossier), false) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
it { expect(subject).to be_empty }
|
2022-04-07 12:02:20 +02:00
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'with a identite champ pj' do
|
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :titre_identite }]) }
|
|
|
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
let(:champ_identite) { dossier.champs_public.find { |c| c.type == 'Champs::TitreIdentiteChamp' } }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
before { attach_file_to_champ(champ_identite) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
it "doesn't return sensitive documents like titre_identite" do
|
|
|
|
expect(champ_identite.piece_justificative_file).to be_attached
|
|
|
|
expect(subject).to be_empty
|
|
|
|
end
|
2024-07-23 17:08:39 +02:00
|
|
|
|
|
|
|
context 'with export_template' do
|
|
|
|
let(:export_template) { build(:export_template, :enabled_pjs, groupe_instructeur: procedure.defaut_groupe_instructeur) }
|
|
|
|
|
|
|
|
it { expect(subject).to be_empty }
|
|
|
|
end
|
2023-10-03 17:07:25 +02:00
|
|
|
end
|
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'with a pj on an commentaire' do
|
|
|
|
let(:dossier) { create(:dossier) }
|
|
|
|
let(:witness) { create(:dossier) }
|
2023-10-03 17:07:25 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
let!(:commentaire) { create(:commentaire, dossier: dossier) }
|
|
|
|
let!(:witness_commentaire) { create(:commentaire, dossier: witness) }
|
2023-10-03 17:07:25 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
before do
|
|
|
|
attach_file(commentaire.piece_jointe)
|
|
|
|
attach_file(witness_commentaire.piece_jointe)
|
|
|
|
end
|
2023-10-03 17:07:25 +02:00
|
|
|
|
2024-02-21 03:01:24 +01:00
|
|
|
it { expect(subject).to match_array(dossier.commentaires.first.piece_jointe.attachments) }
|
2024-05-28 11:06:31 +02:00
|
|
|
|
|
|
|
context 'with export_template' do
|
2024-07-23 17:08:39 +02:00
|
|
|
let(:export_template) { default_export_template }
|
|
|
|
|
|
|
|
it { expect(subject).to be_empty }
|
2024-05-28 11:06:31 +02:00
|
|
|
end
|
2023-10-03 17:07:25 +02:00
|
|
|
end
|
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'with a pj not safe on a commentaire' do
|
|
|
|
let(:dossier) { create(:dossier) }
|
|
|
|
let!(:commentaire) { create(:commentaire, dossier: dossier) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-07-23 17:08:39 +02:00
|
|
|
before { attach_file(commentaire.piece_jointe, false) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
it { expect(subject).to be_empty }
|
2022-04-07 10:39:54 +02:00
|
|
|
end
|
2022-04-07 12:02:20 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'with a motivation' do
|
|
|
|
let(:dossier) { create(:dossier, :with_justificatif) }
|
|
|
|
let!(:witness) { create(:dossier, :with_justificatif) }
|
2022-04-08 14:45:51 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
it { expect(subject).to match_array(dossier.justificatif_motivation.attachment) }
|
2024-05-28 11:28:13 +02:00
|
|
|
|
|
|
|
context 'with export_template' do
|
2024-07-23 17:08:39 +02:00
|
|
|
let(:export_template) { default_export_template }
|
|
|
|
|
|
|
|
it { expect(subject).to be_empty }
|
2024-05-28 11:28:13 +02:00
|
|
|
end
|
2022-04-08 14:45:51 +02:00
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'with a motivation not safe' do
|
|
|
|
let(:dossier) { create(:dossier) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-07-23 17:08:39 +02:00
|
|
|
before { attach_file(dossier.justificatif_motivation, false) }
|
2022-04-08 14:45:51 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
it { expect(subject).to be_empty }
|
|
|
|
end
|
2022-04-08 14:45:51 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'with an attestation' do
|
|
|
|
let(:dossier) { create(:dossier, :with_attestation) }
|
|
|
|
let!(:witness) { create(:dossier, :with_attestation) }
|
2022-04-08 14:45:51 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
it { expect(subject).to match_array(dossier.attestation.pdf.attachment) }
|
2024-05-28 11:35:49 +02:00
|
|
|
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
|
2024-07-23 17:08:39 +02:00
|
|
|
let(:export_template) { default_export_template }
|
|
|
|
|
|
|
|
it { expect(subject).to be_empty }
|
2024-05-28 11:35:49 +02:00
|
|
|
end
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'with an etablissement' do
|
|
|
|
let(:dossier) { create(:dossier, :with_entreprise) }
|
|
|
|
let(:attestation_sociale) { dossier.etablissement.entreprise_attestation_sociale }
|
|
|
|
let(:attestation_fiscale) { dossier.etablissement.entreprise_attestation_fiscale }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
let!(:witness) { create(:dossier, :with_entreprise) }
|
|
|
|
let!(:witness_attestation_sociale) { witness.etablissement.entreprise_attestation_sociale }
|
|
|
|
let!(:witness_attestation_fiscale) { witness.etablissement.entreprise_attestation_fiscale }
|
2022-04-08 14:45:51 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
before do
|
|
|
|
attach_file(attestation_sociale)
|
|
|
|
attach_file(attestation_fiscale)
|
|
|
|
end
|
2022-04-08 14:45:51 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
it { expect(subject).to match_array([attestation_sociale.attachment, attestation_fiscale.attachment]) }
|
2024-05-28 11:40:25 +02:00
|
|
|
|
|
|
|
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
|
2024-07-23 17:08:39 +02:00
|
|
|
let(:export_template) { default_export_template }
|
|
|
|
|
|
|
|
it { expect(subject).to be_empty }
|
2024-05-28 11:40:25 +02:00
|
|
|
end
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
2022-04-08 14:45:51 +02:00
|
|
|
end
|
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'acl on private pj champ' do
|
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative }]) }
|
|
|
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
|
|
|
let(:witness) { create(:dossier, procedure: procedure) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
let!(:private_pj) { create(:type_de_champ_piece_justificative, procedure: procedure, private: true) }
|
2024-07-23 17:08:39 +02:00
|
|
|
def private_pj_champ(d) = d.champs_private.find { |c| c.type == 'Champs::PieceJustificativeChamp' }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
before do
|
2024-07-23 17:08:39 +02:00
|
|
|
attach_file_to_champ(private_pj_champ(dossier))
|
|
|
|
attach_file_to_champ(private_pj_champ(witness))
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'given an administrateur' do
|
|
|
|
let(:user_profile) { build(:administrateur) }
|
2024-07-23 17:08:39 +02:00
|
|
|
it { expect(subject).to match_array(private_pj_champ(dossier).piece_justificative_file.attachments) }
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
2022-04-07 12:02:20 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'given an instructeur' do
|
|
|
|
let(:user_profile) { create(:instructeur) }
|
2024-07-23 17:08:39 +02:00
|
|
|
it { expect(subject).to match_array(private_pj_champ(dossier).piece_justificative_file.attachments) }
|
2022-04-07 10:39:54 +02:00
|
|
|
end
|
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'given an expert' do
|
|
|
|
let(:user_profile) { create(:expert) }
|
2024-07-23 17:08:39 +02:00
|
|
|
it { expect(subject).not_to match_array(private_pj_champ(dossier).piece_justificative_file.attachments) }
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
end
|
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'acl on bill' do
|
2022-04-07 12:02:20 +02:00
|
|
|
let(:dossier) { create(:dossier) }
|
|
|
|
let(:witness) { create(:dossier) }
|
2024-07-23 17:08:39 +02:00
|
|
|
let(:default_export_template) { build(:export_template, groupe_instructeur: dossier.procedure.defaut_groupe_instructeur) }
|
2022-04-07 12:02:20 +02:00
|
|
|
|
2022-04-07 10:39:54 +02:00
|
|
|
let(:bill_signature) do
|
|
|
|
bs = build(:bill_signature, :with_serialized, :with_signature)
|
|
|
|
bs.save(validate: false)
|
|
|
|
bs
|
|
|
|
end
|
|
|
|
|
2022-04-07 12:02:20 +02:00
|
|
|
let(:witness_bill_signature) do
|
|
|
|
bs = build(:bill_signature, :with_serialized, :with_signature)
|
|
|
|
bs.save(validate: false)
|
|
|
|
bs
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:dossier_operation_log, dossier: dossier, bill_signature: bill_signature)
|
|
|
|
create(:dossier_operation_log, dossier: witness, bill_signature: witness_bill_signature)
|
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
|
|
|
|
let(:dossier_bs) { dossier.dossier_operation_logs.first.bill_signature }
|
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'given an Administrateur, includes bills' do
|
|
|
|
let(:user_profile) { build(:administrateur) }
|
|
|
|
|
|
|
|
it "returns serialized bill and signature" do
|
|
|
|
expect(subject).to match_array([dossier_bs.serialized.attachment, dossier_bs.signature.attachment])
|
|
|
|
end
|
|
|
|
|
2024-07-23 17:09:28 +02:00
|
|
|
context 'with export_template' do
|
|
|
|
let(:export_template) { default_export_template }
|
|
|
|
|
|
|
|
it { expect(subject).to be_empty }
|
|
|
|
end
|
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'with a dol' do
|
|
|
|
let(:dol) { create(:dossier_operation_log, dossier: dossier) }
|
|
|
|
let(:witness_dol) { create(:dossier_operation_log, dossier: witness) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
attach_file(dol.serialized)
|
|
|
|
attach_file(witness_dol.serialized)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject).to include(dol.serialized.attachment) }
|
2024-07-23 17:09:28 +02:00
|
|
|
|
|
|
|
context 'with export_template' do
|
|
|
|
let(:export_template) { default_export_template }
|
|
|
|
|
|
|
|
it { expect(subject).to be_empty }
|
|
|
|
end
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
end
|
2021-05-04 10:31:13 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'given an expert, does not includes bills' do
|
|
|
|
let(:user_profile) { create(:expert) }
|
|
|
|
|
|
|
|
it { expect(subject).to be_empty }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'given an instructeur, does not includes bills' do
|
|
|
|
let(:user_profile) { create(:instructeur) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
|
|
|
it { expect(subject).to be_empty }
|
|
|
|
end
|
2021-05-04 10:31:13 +02:00
|
|
|
end
|
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'acl on with_avis_piece_justificative' do
|
|
|
|
let(:user_profile) { create(:expert) }
|
|
|
|
let(:procedure) { create(:procedure) }
|
|
|
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
2022-04-07 12:02:20 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'with avis.piece_justificative being confidentiel' do
|
|
|
|
let(:procedure) { create(:procedure) }
|
|
|
|
let(:avis) { create(:avis, dossier: dossier, confidentiel: true) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
before do
|
|
|
|
to_be_attached = {
|
|
|
|
io: StringIO.new("toto"),
|
|
|
|
filename: "toto.png",
|
|
|
|
content_type: "image/png",
|
|
|
|
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
|
|
}
|
|
|
|
|
|
|
|
avis.piece_justificative_file.attach(to_be_attached)
|
2024-02-19 16:21:26 +01:00
|
|
|
avis.introduction_file.attach(avis.piece_justificative_file.blob)
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'given an administrateur' do
|
|
|
|
let(:user_profile) { build(:administrateur) }
|
2024-05-27 15:14:34 +02:00
|
|
|
it "return confidentiel avis.piece_justificative_file" do
|
2024-02-19 16:21:26 +01:00
|
|
|
expect(subject.size).to eq(2)
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
2024-07-23 17:08:39 +02:00
|
|
|
|
|
|
|
context 'with export_template' do
|
|
|
|
let(:export_template) { default_export_template }
|
|
|
|
|
|
|
|
it { expect(subject).to be_empty }
|
|
|
|
end
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'given an instructeur' do
|
|
|
|
let(:user_profile) { create(:instructeur) }
|
2024-05-27 15:14:34 +02:00
|
|
|
it "return confidentiel avis.piece_justificative_file" do
|
2024-02-19 16:21:26 +01:00
|
|
|
expect(subject.size).to eq(2)
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'given an expert' do
|
|
|
|
let(:user_profile) { create(:expert) }
|
|
|
|
it "doesn't return confidentiel avis.piece_justificative_file" do
|
2024-02-19 16:22:08 +01:00
|
|
|
expect(subject.size).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the expert has given the avis' do
|
|
|
|
let(:experts_procedure) { create(:experts_procedure, expert: user_profile, procedure:) }
|
|
|
|
let(:avis) { create(:avis, experts_procedure:, dossier: dossier, confidentiel: true) }
|
|
|
|
let(:user_profile) { create(:expert) }
|
2024-05-27 15:14:34 +02:00
|
|
|
it "return confidentiel avis.piece_justificative_file" do
|
2024-02-19 16:22:08 +01:00
|
|
|
expect(subject.size).to eq(2)
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
|
|
|
end
|
2022-04-07 12:02:20 +02:00
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'with avis.piece_justificative being public' do
|
|
|
|
let(:procedure) { create(:procedure) }
|
|
|
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
2024-02-19 16:21:26 +01:00
|
|
|
let(:avis) { create(:avis, dossier: dossier, confidentiel: false) }
|
2024-02-07 17:33:34 +01:00
|
|
|
before do
|
|
|
|
to_be_attached = {
|
|
|
|
io: StringIO.new("toto"),
|
|
|
|
filename: "toto.png",
|
|
|
|
content_type: "image/png",
|
|
|
|
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
|
|
}
|
|
|
|
|
|
|
|
avis.piece_justificative_file.attach(to_be_attached)
|
2024-02-19 16:21:26 +01:00
|
|
|
avis.introduction_file.attach(avis.piece_justificative_file.blob)
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'given an administrateur' do
|
|
|
|
let(:user_profile) { build(:administrateur) }
|
2024-05-27 15:14:34 +02:00
|
|
|
it "return confidentiel avis.piece_justificative_file" do
|
2024-02-19 16:21:26 +01:00
|
|
|
expect(subject.size).to eq(2)
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
context 'given an instructeur' do
|
|
|
|
let(:user_profile) { create(:instructeur) }
|
2024-05-27 15:14:34 +02:00
|
|
|
it "return confidentiel avis.piece_justificative_file" do
|
2024-02-19 16:21:26 +01:00
|
|
|
expect(subject.size).to eq(2)
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'given an expert' do
|
|
|
|
let(:user_profile) { create(:expert) }
|
2024-05-27 15:14:34 +02:00
|
|
|
it "return confidentiel avis.piece_justificative_file" do
|
2024-02-19 16:21:26 +01:00
|
|
|
expect(subject.size).to eq(2)
|
2024-02-07 17:33:34 +01:00
|
|
|
end
|
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
end
|
2021-05-04 10:31:13 +02:00
|
|
|
end
|
2020-12-16 17:27:57 +01:00
|
|
|
end
|
2021-06-18 09:06:05 +02:00
|
|
|
|
2024-02-07 17:33:34 +01:00
|
|
|
describe '.generate_dossiers_export' do
|
|
|
|
let(:user_profile) { build(:administrateur) }
|
2022-08-04 11:39:07 +02:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :repetition, children: [{ type: :piece_justificative }] }]) }
|
2022-07-21 10:01:10 +02:00
|
|
|
let(:dossier) { create(:dossier, :with_populated_champs, procedure: procedure) }
|
2024-02-07 17:33:34 +01:00
|
|
|
let(:dossiers) { Dossier.where(id: dossier.id) }
|
2024-05-28 18:24:45 +02:00
|
|
|
let(:export_template) { nil }
|
|
|
|
subject { PiecesJustificativesService.new(user_profile:, export_template:).generate_dossiers_export(dossiers) }
|
2021-06-18 09:06:05 +02:00
|
|
|
|
|
|
|
it "doesn't update dossier" do
|
2022-04-07 10:39:54 +02:00
|
|
|
expect { subject }.not_to change { dossier.updated_at }
|
2021-06-18 09:06:05 +02:00
|
|
|
end
|
2023-03-28 16:51:35 +02:00
|
|
|
|
|
|
|
context 'when given an expert' do
|
2024-02-07 17:33:34 +01:00
|
|
|
let!(:user_profile) { create(:expert) }
|
2023-03-28 16:51:35 +02:00
|
|
|
let!(:confidentiel_avis) { create(:avis, :confidentiel, dossier: dossier) }
|
|
|
|
let!(:not_confidentiel_avis) { create(:avis, :not_confidentiel, dossier: dossier) }
|
2024-02-07 17:33:34 +01:00
|
|
|
let!(:expert_avis) { create(:avis, :confidentiel, dossier: dossier, expert: user_profile) }
|
2023-03-28 16:51:35 +02:00
|
|
|
|
2024-05-28 18:24:45 +02:00
|
|
|
subject { PiecesJustificativesService.new(user_profile:, export_template:).generate_dossiers_export(dossiers) }
|
2023-03-28 16:51:35 +02:00
|
|
|
it "includes avis not confidentiel as well as expert's avis" do
|
2024-02-07 17:33:34 +01:00
|
|
|
expect_any_instance_of(Dossier).to receive(:avis_for_expert).with(user_profile).and_return([])
|
2023-03-28 16:51:35 +02:00
|
|
|
subject
|
|
|
|
end
|
2024-05-28 18:24:45 +02:00
|
|
|
|
|
|
|
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
|
2024-07-23 17:08:39 +02:00
|
|
|
let(:groupe_instructeur) { procedure.defaut_groupe_instructeur }
|
|
|
|
let(:export_template) { create(:export_template, groupe_instructeur:, dossier_folder: ExportItem.default(prefix: 'DOSSIER')) }
|
2024-05-28 18:24:45 +02:00
|
|
|
subject { PiecesJustificativesService.new(user_profile:, export_template:).generate_dossiers_export(dossiers) }
|
|
|
|
|
|
|
|
it 'gives custom name to export pdf file' do
|
2024-07-23 17:08:39 +02:00
|
|
|
expect(subject.first.second).to eq "DOSSIER-#{dossier.id}/export-#{dossier.id}.pdf"
|
2024-05-28 18:24:45 +02:00
|
|
|
end
|
2023-03-28 16:51:35 +02:00
|
|
|
end
|
2021-06-18 09:06:05 +02:00
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2024-05-23 09:59:09 +02:00
|
|
|
describe '#compute_champ_id_row_index' do
|
|
|
|
let(:user_profile) { build(:administrateur) }
|
|
|
|
let(:types_de_champ_public) do
|
|
|
|
[
|
2024-06-25 17:17:51 +02:00
|
|
|
{ type: :repetition, mandatory: false, children: [{ type: :piece_justificative }] },
|
|
|
|
{ type: :repetition, mandatory: false, children: [{ type: :piece_justificative }, { type: :piece_justificative }] }
|
2024-05-23 09:59:09 +02:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public:) }
|
|
|
|
let(:dossier_1) { create(:dossier, procedure:) }
|
|
|
|
let(:champs) { dossier_1.champs }
|
|
|
|
|
|
|
|
def pj_champ(d) = d.champs_public.find_by(type: 'Champs::PieceJustificativeChamp')
|
|
|
|
def repetition(d, index:) = d.champs_public.filter(&:repetition?)[index]
|
|
|
|
|
|
|
|
subject { PiecesJustificativesService.new(user_profile:, export_template: nil).send(:compute_champ_id_row_index, champs) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
pj_champ(dossier_1)
|
|
|
|
|
|
|
|
# repet_0 (stable_id: r0)
|
|
|
|
# # row_0
|
|
|
|
# # # pj_champ_0 (stable_id: 0)
|
|
|
|
# # row_1
|
|
|
|
# # # pj_champ_1 (stable_id: 0)
|
|
|
|
# repet_1 (stable_id: r1)
|
|
|
|
# # row_0
|
|
|
|
# # # pj_champ_2 (stable_id: 1)
|
|
|
|
# # # pj_champ_3 (stable_id: 2)
|
|
|
|
# # row_1
|
|
|
|
# # # pj_champ_4 (stable_id: 1)
|
|
|
|
# # # pj_champ_5 (stable_id: 2)
|
|
|
|
|
|
|
|
repet_0 = repetition(dossier_1, index: 0)
|
|
|
|
repet_1 = repetition(dossier_1, index: 1)
|
|
|
|
|
2024-08-27 10:29:10 +02:00
|
|
|
repet_0.add_row(updated_by: 'test')
|
|
|
|
repet_0.add_row(updated_by: 'test')
|
2024-05-23 09:59:09 +02:00
|
|
|
|
2024-08-27 10:29:10 +02:00
|
|
|
repet_1.add_row(updated_by: 'test')
|
|
|
|
repet_1.add_row(updated_by: 'test')
|
2024-05-23 09:59:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
champs = dossier_1.champs_public
|
|
|
|
repet_0 = champs[0]
|
|
|
|
pj_0 = repet_0.rows.first.first
|
|
|
|
pj_1 = repet_0.rows.second.first
|
|
|
|
|
|
|
|
repet_1 = champs[1]
|
|
|
|
pj_2 = repet_1.rows.first.first
|
|
|
|
pj_3 = repet_1.rows.first.second
|
|
|
|
|
|
|
|
pj_4 = repet_1.rows.second.first
|
|
|
|
pj_5 = repet_1.rows.second.second
|
|
|
|
|
|
|
|
is_expected.to eq({ pj_0.id => 0, pj_1.id => 1, pj_2.id => 0, pj_3.id => 0, pj_4.id => 1, pj_5.id => 1 })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-08 14:45:51 +02:00
|
|
|
def attach_file_to_champ(champ, safe = true)
|
|
|
|
attach_file(champ.piece_justificative_file, safe)
|
2022-04-07 10:39:54 +02:00
|
|
|
end
|
|
|
|
|
2022-04-08 14:45:51 +02:00
|
|
|
def attach_file(attachable, safe = true)
|
|
|
|
to_be_attached = {
|
|
|
|
io: StringIO.new("toto"),
|
|
|
|
filename: "toto.png", content_type: "image/png"
|
|
|
|
}
|
|
|
|
|
|
|
|
if safe
|
|
|
|
to_be_attached[:metadata] = { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
|
|
end
|
|
|
|
|
|
|
|
attachable.attach(to_be_attached)
|
2022-04-07 10:39:54 +02:00
|
|
|
end
|
2020-12-16 17:27:57 +01:00
|
|
|
end
|