2017-06-08 14:16:48 +02:00
|
|
|
describe AttestationTemplate, type: :model do
|
2017-06-27 13:07:06 +02:00
|
|
|
describe 'validates footer length' do
|
2020-03-11 16:57:29 +01:00
|
|
|
let(:attestation_template) { build(:attestation_template, footer: footer) }
|
2017-06-27 13:07:06 +02:00
|
|
|
|
|
|
|
subject do
|
|
|
|
attestation_template.validate
|
|
|
|
attestation_template.errors.details
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the footer is too long' do
|
|
|
|
let(:footer) { 'a' * 191 }
|
|
|
|
|
|
|
|
it { is_expected.to match({ footer: [{ error: :too_long, count: 190 }] }) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-08 14:16:48 +02:00
|
|
|
describe 'dup' do
|
2020-03-11 16:57:29 +01:00
|
|
|
let(:attestation_template) { create(:attestation_template, attributes) }
|
2017-06-08 14:16:48 +02:00
|
|
|
subject { attestation_template.dup }
|
|
|
|
|
|
|
|
context 'with an attestation without images' do
|
2020-03-11 16:57:29 +01:00
|
|
|
let(:attributes) { attributes_for(:attestation_template) }
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2024-05-28 16:22:47 +02:00
|
|
|
it "works" do
|
|
|
|
is_expected.to have_attributes(attributes)
|
|
|
|
is_expected.to have_attributes(id: nil)
|
|
|
|
expect(subject.logo.attached?).to be_falsey
|
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an attestation with images' do
|
2020-03-11 16:57:29 +01:00
|
|
|
let(:attestation_template) { create(:attestation_template, :with_files) }
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2020-03-11 16:57:29 +01:00
|
|
|
it do
|
2022-11-10 13:01:15 +01:00
|
|
|
expect(subject.logo.attachment).not_to eq(attestation_template.logo.attachment)
|
|
|
|
expect(subject.logo.blob).to eq(attestation_template.logo.blob)
|
2020-03-11 16:57:29 +01:00
|
|
|
expect(subject.logo.attached?).to be_truthy
|
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2020-03-11 16:57:29 +01:00
|
|
|
it do
|
2022-11-10 13:01:15 +01:00
|
|
|
expect(subject.signature.attachment).not_to eq(attestation_template.signature.attachment)
|
|
|
|
expect(subject.signature.blob).to eq(attestation_template.signature.blob)
|
2020-03-11 16:57:29 +01:00
|
|
|
expect(subject.signature.attached?).to be_truthy
|
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
end
|
|
|
|
end
|
2017-06-08 14:04:47 +02:00
|
|
|
|
2020-02-24 11:17:52 +01:00
|
|
|
describe 'invalidate attestation if images attachments are not valid' do
|
2020-03-11 16:57:29 +01:00
|
|
|
subject { build(:attestation_template, :with_gif_files) }
|
2020-02-24 11:17:52 +01:00
|
|
|
|
|
|
|
context 'with an attestation which has gif files' do
|
|
|
|
it { is_expected.not_to be_valid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-08 14:04:47 +02:00
|
|
|
describe 'attestation_for' do
|
|
|
|
let(:procedure) do
|
|
|
|
create(:procedure,
|
2022-08-04 11:39:07 +02:00
|
|
|
types_de_champ_public: types_de_champ,
|
2017-06-08 14:04:47 +02:00
|
|
|
types_de_champ_private: types_de_champ_private,
|
2022-02-11 08:45:16 +01:00
|
|
|
attestation_template: attestation_template)
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
2018-04-23 11:59:41 +02:00
|
|
|
let(:etablissement) { create(:etablissement) }
|
2017-06-08 14:04:47 +02:00
|
|
|
let(:types_de_champ) { [] }
|
|
|
|
let(:types_de_champ_private) { [] }
|
2024-05-29 23:00:34 +02:00
|
|
|
let(:dossier) { create(:dossier, :accepte, procedure:) }
|
|
|
|
|
|
|
|
let(:types_de_champ) do
|
|
|
|
[
|
|
|
|
{ libelle: 'libelleA' },
|
|
|
|
{ libelle: 'libelleB' }
|
|
|
|
]
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2024-05-29 23:00:34 +02:00
|
|
|
dossier.champs_public
|
|
|
|
.find { |champ| champ.libelle == 'libelleA' }
|
|
|
|
.update(value: 'libelle1')
|
2017-06-08 14:04:47 +02:00
|
|
|
|
2024-05-29 23:00:34 +02:00
|
|
|
dossier.champs_public
|
|
|
|
.find { |champ| champ.libelle == 'libelleB' }
|
|
|
|
.update(value: 'libelle2')
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
|
2024-05-29 23:00:34 +02:00
|
|
|
let(:attestation) { attestation_template.attestation_for(dossier) }
|
2017-06-08 14:04:47 +02:00
|
|
|
|
2024-05-29 23:00:34 +02:00
|
|
|
context 'attestation v1' do
|
|
|
|
let(:template_title) { 'title --libelleA--' }
|
|
|
|
let(:template_body) { 'body --libelleB--' }
|
|
|
|
let(:attestation_template) do
|
|
|
|
build(:attestation_template,
|
|
|
|
title: template_title,
|
|
|
|
body: template_body)
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
|
2024-05-29 23:00:34 +02:00
|
|
|
let(:view_args) do
|
|
|
|
arguments = nil
|
|
|
|
|
|
|
|
allow(ApplicationController).to receive(:render).and_wrap_original do |m, *args|
|
|
|
|
arguments = args.first[:assigns]
|
|
|
|
m.call(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
attestation_template.attestation_for(dossier)
|
2017-06-08 14:04:47 +02:00
|
|
|
|
2024-05-29 23:00:34 +02:00
|
|
|
arguments
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes the correct parameters and generates an attestation' do
|
|
|
|
expect(view_args[:attestation][:title]).to eq('title libelle1')
|
|
|
|
expect(view_args[:attestation][:body]).to eq('body libelle2')
|
|
|
|
expect(attestation.title).to eq('title libelle1')
|
|
|
|
expect(attestation.pdf).to be_attached
|
|
|
|
end
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
|
2024-05-29 23:00:34 +02:00
|
|
|
context 'attestation v2' do
|
|
|
|
let(:attestation_template) do
|
|
|
|
build(:attestation_template, :v2, :with_files, label_logo: "Ministère des specs")
|
|
|
|
end
|
2017-06-08 14:04:47 +02:00
|
|
|
|
2024-05-29 23:00:34 +02:00
|
|
|
before do
|
|
|
|
stub_request(:post, WEASYPRINT_URL)
|
|
|
|
.with(body: {
|
|
|
|
html: /Ministère des specs.+Mon titre pour #{procedure.libelle}.+Dossier: n° #{dossier.id}/m,
|
|
|
|
upstream_context: { procedure_id: procedure.id, dossier_id: dossier.id }
|
|
|
|
})
|
|
|
|
.to_return(body: 'PDF_DATA')
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
|
2024-05-29 23:00:34 +02:00
|
|
|
it 'generates an attestation' do
|
|
|
|
expect(attestation.pdf).to be_attached
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-09-19 15:41:51 +02:00
|
|
|
|
|
|
|
describe '#render_attributes_for' do
|
|
|
|
context 'signature' do
|
|
|
|
let(:dossier) { create(:dossier, procedure: attestation.procedure, groupe_instructeur: groupe_instructeur) }
|
|
|
|
|
|
|
|
subject { attestation.render_attributes_for(dossier: dossier)[:signature] }
|
|
|
|
|
|
|
|
context 'procedure with signature' do
|
|
|
|
let(:attestation) { create(:attestation_template, signature: Rack::Test::UploadedFile.new('spec/fixtures/files/logo_test_procedure.png', 'image/png')) }
|
|
|
|
|
|
|
|
context "groupe instructeur without signature" do
|
|
|
|
let(:groupe_instructeur) { create(:groupe_instructeur, signature: nil) }
|
|
|
|
|
|
|
|
it { expect(subject.blob.filename).to eq("logo_test_procedure.png") }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'groupe instructeur with signature' do
|
|
|
|
let(:groupe_instructeur) { create(:groupe_instructeur, signature: Rack::Test::UploadedFile.new('spec/fixtures/files/black.png', 'image/png')) }
|
|
|
|
|
|
|
|
it { expect(subject.blob.filename).to eq("black.png") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'procedure without signature' do
|
|
|
|
let(:attestation) { create(:attestation_template, signature: nil) }
|
|
|
|
|
|
|
|
context "groupe instructeur without signature" do
|
|
|
|
let(:groupe_instructeur) { create(:groupe_instructeur, signature: nil) }
|
|
|
|
|
|
|
|
it { expect(subject.attached?).to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'groupe instructeur with signature' do
|
|
|
|
let(:groupe_instructeur) { create(:groupe_instructeur, signature: Rack::Test::UploadedFile.new('spec/fixtures/files/black.png', 'image/png')) }
|
|
|
|
|
|
|
|
it { expect(subject.blob.filename).to eq("black.png") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-01-18 12:38:46 +01:00
|
|
|
|
|
|
|
context 'body v2' do
|
|
|
|
let(:attestation) { create(:attestation_template, :v2) }
|
|
|
|
let(:dossier) { create(:dossier, procedure: attestation.procedure, individual: build(:individual, nom: 'Doe', prenom: 'John')) }
|
|
|
|
|
|
|
|
it do
|
|
|
|
body = attestation.render_attributes_for(dossier: dossier)[:body]
|
|
|
|
expect(body).to include("Mon titre pour #{dossier.procedure.libelle}")
|
|
|
|
expect(body).to include("Doe John")
|
|
|
|
end
|
|
|
|
end
|
2023-09-19 15:41:51 +02:00
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
end
|