2020-12-16 17:27:57 +01:00
|
|
|
describe PiecesJustificativesService do
|
2022-04-07 10:39:54 +02:00
|
|
|
describe '.liste_documents' do
|
|
|
|
let(:for_expert) { false }
|
2021-05-04 10:31:13 +02:00
|
|
|
|
2022-04-07 10:39:54 +02:00
|
|
|
subject do
|
|
|
|
PiecesJustificativesService
|
|
|
|
.liste_documents(Dossier.where(id: dossier.id), for_expert)
|
|
|
|
.map(&:first)
|
|
|
|
end
|
2020-12-16 17:27:57 +01:00
|
|
|
|
2022-04-07 10:39:54 +02:00
|
|
|
context 'with a pj champ' do
|
|
|
|
let(:procedure) { create(:procedure, :with_piece_justificative) }
|
2022-04-07 12:02:20 +02:00
|
|
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
|
|
|
let(:witness) { create(:dossier, procedure: procedure) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2022-04-07 12:02:20 +02:00
|
|
|
let(:pj_champ) { -> (d) { d.champs.find { |c| c.type == 'Champs::PieceJustificativeChamp' } } }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2022-04-07 12:02:20 +02:00
|
|
|
before do
|
|
|
|
attach_file_to_champ(pj_champ.call(dossier))
|
|
|
|
attach_file_to_champ(pj_champ.call(witness))
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject).to match_array([pj_champ.call(dossier).piece_justificative_file.attachment]) }
|
2022-04-07 10:39:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a private pj champ' do
|
|
|
|
let(:procedure) { create(:procedure) }
|
2022-04-07 12:02:20 +02:00
|
|
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
|
|
|
let(:witness) { create(:dossier, procedure: procedure) }
|
|
|
|
|
2022-04-07 10:39:54 +02:00
|
|
|
let!(:private_pj) { create(:type_de_champ_piece_justificative, procedure: procedure, private: true) }
|
2022-04-07 12:02:20 +02:00
|
|
|
let(:private_pj_champ) { -> (d) { d.champs_private.find { |c| c.type == 'Champs::PieceJustificativeChamp' } } }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2022-04-07 12:02:20 +02:00
|
|
|
before do
|
|
|
|
attach_file_to_champ(private_pj_champ.call(dossier))
|
|
|
|
attach_file_to_champ(private_pj_champ.call(witness))
|
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2022-04-07 12:02:20 +02:00
|
|
|
it { expect(subject).to match_array([private_pj_champ.call(dossier).piece_justificative_file.attachment]) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
|
|
|
context 'for expert' do
|
|
|
|
let(:for_expert) { true }
|
|
|
|
|
|
|
|
it { expect(subject).to be_empty }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a identite champ pj' do
|
|
|
|
let(:procedure) { create(:procedure, :with_titre_identite) }
|
2022-04-07 12:02:20 +02:00
|
|
|
let(:dossier) { create(:dossier, procedure: procedure) }
|
|
|
|
let(:witness) { create(:dossier, procedure: procedure) }
|
|
|
|
|
2022-04-07 10:39:54 +02:00
|
|
|
let(:champ_identite) { dossier.champs.find { |c| c.type == 'Champs::TitreIdentiteChamp' } }
|
|
|
|
|
|
|
|
before { attach_file_to_champ(champ_identite) }
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a pj on an commentaire' do
|
2022-04-07 12:02:20 +02:00
|
|
|
let(:dossier) { create(:dossier) }
|
|
|
|
let(:witness) { create(:dossier) }
|
|
|
|
|
2022-04-07 10:39:54 +02:00
|
|
|
let!(:commentaire) { create(:commentaire, :with_file, dossier: dossier) }
|
2022-04-07 12:02:20 +02:00
|
|
|
let!(:witness_commentaire) { create(:commentaire, :with_file, dossier: witness) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
|
|
|
it { expect(subject).to match_array(dossier.commentaires.first.piece_jointe.attachment) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a motivation' do
|
|
|
|
let(:dossier) { create(:dossier, :with_justificatif) }
|
2022-04-07 12:02:20 +02:00
|
|
|
let!(:witness) { create(:dossier, :with_justificatif) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
|
|
|
it { expect(subject).to match_array(dossier.justificatif_motivation.attachment) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an attestation' do
|
|
|
|
let(:dossier) { create(:dossier, :with_attestation) }
|
2022-04-07 12:02:20 +02:00
|
|
|
let!(:witness) { create(:dossier, :with_attestation) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
|
|
|
it { expect(subject).to match_array(dossier.attestation.pdf.attachment) }
|
|
|
|
end
|
|
|
|
|
|
|
|
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 12:02:20 +02: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-07 10:39:54 +02:00
|
|
|
before do
|
|
|
|
attach_file(attestation_sociale)
|
|
|
|
attach_file(attestation_fiscale)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject).to match_array([attestation_sociale.attachment, attestation_fiscale.attachment]) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a bill' do
|
2022-04-07 12:02:20 +02:00
|
|
|
let(:dossier) { create(:dossier) }
|
|
|
|
let(:witness) { create(:dossier) }
|
|
|
|
|
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 }
|
|
|
|
|
|
|
|
it "returns serialized bill and signature" do
|
|
|
|
expect(subject).to match_array([dossier_bs.serialized.attachment, dossier_bs.signature.attachment])
|
|
|
|
end
|
2021-05-04 10:31:13 +02:00
|
|
|
|
2022-04-07 10:39:54 +02:00
|
|
|
context 'for expert' do
|
|
|
|
let(:for_expert) { true }
|
|
|
|
|
|
|
|
it { expect(subject).to be_empty }
|
|
|
|
end
|
2021-05-04 10:31:13 +02:00
|
|
|
end
|
|
|
|
|
2022-04-07 10:39:54 +02:00
|
|
|
context 'with a dol' do
|
2022-04-07 12:02:20 +02:00
|
|
|
let(:dossier) { create(:dossier) }
|
|
|
|
let(:witness) { create(:dossier) }
|
|
|
|
|
2022-04-07 10:39:54 +02:00
|
|
|
let(:dol) { create(:dossier_operation_log, dossier: dossier) }
|
2022-04-07 12:02:20 +02:00
|
|
|
let(:witness_dol) { create(:dossier_operation_log, dossier: witness) }
|
2022-04-07 10:39:54 +02:00
|
|
|
|
2022-04-07 12:02:20 +02:00
|
|
|
before do
|
|
|
|
attach_file(dol.serialized)
|
|
|
|
attach_file(witness_dol.serialized)
|
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
|
|
|
|
it { expect(subject).to match_array(dol.serialized.attachment) }
|
|
|
|
|
|
|
|
context 'for expert' do
|
|
|
|
let(:for_expert) { true }
|
|
|
|
|
|
|
|
it { expect(subject).to be_empty }
|
|
|
|
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
|
|
|
|
|
|
|
describe '.generate_dossier_export' do
|
2022-04-07 10:39:54 +02:00
|
|
|
let(:dossier) { create(:dossier) }
|
|
|
|
|
2022-04-07 15:23:18 +02:00
|
|
|
subject { PiecesJustificativesService.generate_dossier_export(Dossier.where(id: dossier.id)) }
|
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
|
|
|
|
end
|
2022-04-07 10:39:54 +02:00
|
|
|
|
|
|
|
def attach_file_to_champ(champ)
|
|
|
|
attach_file(champ.piece_justificative_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
def attach_file(attachable)
|
|
|
|
attachable
|
|
|
|
.attach(io: StringIO.new("toto"), filename: "toto.png", content_type: "image/png")
|
|
|
|
end
|
2020-12-16 17:27:57 +01:00
|
|
|
end
|