2017-06-08 14:16:48 +02:00
|
|
|
describe AttestationTemplate, type: :model do
|
2019-08-27 17:42:52 +02:00
|
|
|
# describe 'validate' do
|
|
|
|
# let(:logo_size) { AttestationTemplate::FILE_MAX_SIZE_IN_MB.megabyte }
|
|
|
|
# let(:signature_size) { AttestationTemplate::FILE_MAX_SIZE_IN_MB.megabyte }
|
|
|
|
# let(:fake_logo) { double(AttestationTemplateLogoUploader, file: double(size: logo_size)) }
|
|
|
|
# let(:fake_signature) { double(AttestationTemplateSignatureUploader, file: double(size: signature_size)) }
|
|
|
|
# let(:attestation_template) { AttestationTemplate.new }
|
|
|
|
|
|
|
|
# before do
|
|
|
|
# allow(attestation_template).to receive(:logo).and_return(fake_logo)
|
|
|
|
# allow(attestation_template).to receive(:signature).and_return(fake_signature)
|
|
|
|
# attestation_template.validate
|
|
|
|
# end
|
|
|
|
|
|
|
|
# subject { attestation_template.errors.details }
|
|
|
|
|
|
|
|
# context 'when no files are present' do
|
|
|
|
# let(:fake_logo) { nil }
|
|
|
|
# let(:fake_signature) { nil }
|
|
|
|
|
|
|
|
# it { is_expected.to match({}) }
|
|
|
|
# end
|
|
|
|
|
|
|
|
# context 'when the logo and the signature have the right size' do
|
|
|
|
# it { is_expected.to match({}) }
|
|
|
|
# end
|
|
|
|
|
|
|
|
# context 'when the logo and the signature are too heavy' do
|
|
|
|
# let(:logo_size) { AttestationTemplate::FILE_MAX_SIZE_IN_MB.megabyte + 1 }
|
|
|
|
# let(:signature_size) { AttestationTemplate::FILE_MAX_SIZE_IN_MB.megabyte + 1 }
|
|
|
|
|
|
|
|
# it do
|
|
|
|
# expected = {
|
|
|
|
# signature: [{ error: ' : vous ne pouvez pas charger une image de plus de 0,5 Mo' }],
|
|
|
|
# logo: [{ error: ' : vous ne pouvez pas charger une image de plus de 0,5 Mo' }]
|
|
|
|
# }
|
|
|
|
|
|
|
|
# is_expected.to match(expected)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
2017-06-08 14:16:48 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
it { is_expected.to have_attributes(attributes) }
|
|
|
|
it { is_expected.to have_attributes(id: nil) }
|
2019-08-28 13:11:58 +02:00
|
|
|
it { expect(subject.logo.attached?).to be_falsey }
|
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
|
|
|
for_individual: for_individual,
|
|
|
|
attestation_template: attestation_template)
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
let(:for_individual) { false }
|
|
|
|
let(:individual) { nil }
|
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) { [] }
|
2018-04-23 11:59:41 +02:00
|
|
|
let!(:dossier) { create(:dossier, procedure: procedure, individual: individual, etablissement: etablissement) }
|
2017-06-08 14:04:47 +02:00
|
|
|
let(:template_title) { 'title' }
|
|
|
|
let(:template_body) { 'body' }
|
|
|
|
let(:attestation_template) do
|
2022-02-11 08:45:16 +01:00
|
|
|
build(:attestation_template,
|
2017-06-08 14:04:47 +02:00
|
|
|
title: template_title,
|
|
|
|
body: template_body,
|
|
|
|
logo: @logo,
|
|
|
|
signature: @signature)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2018-10-25 15:11:12 +02:00
|
|
|
Timecop.freeze(Time.zone.now)
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2017-11-29 16:07:39 +01:00
|
|
|
Timecop.return
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:view_args) do
|
|
|
|
arguments = nil
|
|
|
|
|
2020-08-04 16:53:46 +02:00
|
|
|
allow(ApplicationController).to receive(:render).and_wrap_original do |m, *args|
|
|
|
|
arguments = args.first[:assigns]
|
|
|
|
m.call(*args)
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
attestation_template.attestation_for(dossier)
|
|
|
|
|
|
|
|
arguments
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:attestation) { attestation_template.attestation_for(dossier) }
|
|
|
|
|
|
|
|
context 'when the procedure has a type de champ named libelleA et libelleB' do
|
|
|
|
let(:types_de_champ) do
|
2018-01-15 14:42:48 +01:00
|
|
|
[
|
2022-08-04 11:39:07 +02:00
|
|
|
{ libelle: 'libelleA' },
|
|
|
|
{ libelle: 'libelleB' }
|
2018-01-15 14:42:48 +01:00
|
|
|
]
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'and the are used in the template title and body' do
|
|
|
|
let(:template_title) { 'title --libelleA--' }
|
|
|
|
let(:template_body) { 'body --libelleB--' }
|
|
|
|
|
|
|
|
context 'and their value in the dossier are not nil' do
|
2017-12-15 11:21:54 +01:00
|
|
|
before do
|
2022-11-10 22:21:14 +01:00
|
|
|
dossier.champs_public
|
2020-09-24 15:09:41 +02:00
|
|
|
.find { |champ| champ.libelle == 'libelleA' }
|
2018-03-02 16:27:03 +01:00
|
|
|
.update(value: 'libelle1')
|
2017-06-08 14:04:47 +02:00
|
|
|
|
2022-11-10 22:21:14 +01:00
|
|
|
dossier.champs_public
|
2020-09-24 15:09:41 +02:00
|
|
|
.find { |champ| champ.libelle == 'libelleB' }
|
2018-03-02 16:27:03 +01:00
|
|
|
.update(value: 'libelle2')
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
|
2020-08-04 16:53:46 +02:00
|
|
|
it 'passes the correct parameters to the view' do
|
2019-08-28 14:27:41 +02:00
|
|
|
expect(view_args[:attestation][:title]).to eq('title libelle1')
|
|
|
|
expect(view_args[:attestation][:body]).to eq('body libelle2')
|
2020-08-04 16:53:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'generates an attestation' do
|
2019-08-28 14:27:41 +02:00
|
|
|
expect(attestation.title).to eq('title libelle1')
|
2020-08-04 16:53:46 +02:00
|
|
|
expect(attestation.pdf).to be_attached
|
2019-08-28 14:27:41 +02:00
|
|
|
end
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
end
|