Merge pull request #1662 from betagouv/frederic/fix_1563/remove_attestation_pj

#1563 Remove attestation mail attachment
This commit is contained in:
Frederic Merizen 2018-05-09 11:04:22 +02:00 committed by GitHub
commit ccb6eefc55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 9 additions and 97 deletions

View file

@ -106,18 +106,9 @@ module NewGestionnaire
dossier.save dossier.save
# needed to force Carrierwave to provide dossier.attestation.pdf.read
# when the Flipflop.remote_storage? is true, otherwise pdf.read is a closed stream.
dossier.reload
attestation_pdf = nil
if check_attestation_emailable
attestation_pdf = dossier.attestation.pdf.read
end
flash.notice = notice flash.notice = notice
NotificationMailer.send_notification(dossier, template, attestation_pdf).deliver_now! NotificationMailer.send_notification(dossier, template).deliver_now!
redirect_to gestionnaire_dossier_path(procedure, dossier) redirect_to gestionnaire_dossier_path(procedure, dossier)
end end
@ -204,16 +195,6 @@ module NewGestionnaire
]) ])
end end
def check_attestation_emailable
if dossier&.attestation&.emailable? == false
human_size = number_to_human_size(dossier.attestation.pdf.size)
msg = "the attestation of the dossier #{dossier.id} cannot be mailed because it is too heavy: #{human_size}"
capture_message(msg, level: 'error')
end
dossier&.attestation&.emailable?
end
def mark_demande_as_read def mark_demande_as_read
current_gestionnaire.mark_tab_as_seen(dossier, :demande) current_gestionnaire.mark_tab_as_seen(dossier, :demande)
end end

View file

@ -8,16 +8,12 @@ class NotificationMailer < ApplicationMailer
send_notification(dossier, dossier.procedure.received_mail_template) send_notification(dossier, dossier.procedure.received_mail_template)
end end
def send_notification(dossier, mail_template, attestation = nil) def send_notification(dossier, mail_template)
vars_mailer(dossier) vars_mailer(dossier)
@subject = mail_template.subject_for_dossier dossier @subject = mail_template.subject_for_dossier dossier
@body = mail_template.body_for_dossier dossier @body = mail_template.body_for_dossier dossier
if attestation.present?
attachments['attestation.pdf'] = attestation
end
mail(subject: @subject) { |format| format.html { @body } } mail(subject: @subject) { |format| format.html { @body } }
end end

View file

@ -2,10 +2,4 @@ class Attestation < ApplicationRecord
belongs_to :dossier belongs_to :dossier
mount_uploader :pdf, AttestationUploader mount_uploader :pdf, AttestationUploader
MAX_SIZE_EMAILABLE = 2.megabytes
def emailable?
pdf.size <= MAX_SIZE_EMAILABLE
end
end end

View file

@ -152,7 +152,7 @@ describe NewGestionnaire::DossiersController, type: :controller do
it 'Notification email is sent' do it 'Notification email is sent' do
expect(NotificationMailer).to receive(:send_notification) expect(NotificationMailer).to receive(:send_notification)
.with(dossier, kind_of(Mails::RefusedMail), nil).and_return(NotificationMailer) .with(dossier, kind_of(Mails::RefusedMail)).and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_now!) expect(NotificationMailer).to receive(:deliver_now!)
subject subject
@ -178,7 +178,7 @@ describe NewGestionnaire::DossiersController, type: :controller do
it 'Notification email is sent' do it 'Notification email is sent' do
expect(NotificationMailer).to receive(:send_notification) expect(NotificationMailer).to receive(:send_notification)
.with(dossier, kind_of(Mails::WithoutContinuationMail), nil).and_return(NotificationMailer) .with(dossier, kind_of(Mails::WithoutContinuationMail)).and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_now!) expect(NotificationMailer).to receive(:deliver_now!)
subject subject
@ -188,14 +188,12 @@ describe NewGestionnaire::DossiersController, type: :controller do
end end
context "with accepter" do context "with accepter" do
let(:expected_attestation) { nil }
before do before do
dossier.en_instruction! dossier.en_instruction!
sign_in gestionnaire sign_in gestionnaire
expect(NotificationMailer).to receive(:send_notification) expect(NotificationMailer).to receive(:send_notification)
.with(dossier, kind_of(Mails::ClosedMail), expected_attestation) .with(dossier, kind_of(Mails::ClosedMail))
.and_return(NotificationMailer) .and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_now!) expect(NotificationMailer).to receive(:deliver_now!)
@ -220,34 +218,14 @@ describe NewGestionnaire::DossiersController, type: :controller do
before do before do
attestation = Attestation.new attestation = Attestation.new
allow(attestation).to receive(:pdf).and_return(double(read: 'pdf', size: 2.megabytes)) allow(attestation).to receive(:pdf).and_return(double(read: 'pdf', size: 2.megabytes))
allow(attestation).to receive(:emailable?).and_return(emailable)
expect_any_instance_of(Dossier).to receive(:reload)
allow_any_instance_of(Dossier).to receive(:build_attestation).and_return(attestation) allow_any_instance_of(Dossier).to receive(:build_attestation).and_return(attestation)
end end
context 'emailable' do it 'The gestionnaire is sent back to the dossier page' do
let(:emailable) { true } subject
let(:expected_attestation) { 'pdf' }
it 'Notification email is sent with the attestation' do is_expected.to redirect_to redirect_to gestionnaire_dossier_path(procedure, dossier)
subject
is_expected.to redirect_to redirect_to gestionnaire_dossier_path(procedure, dossier)
end
end
context 'when the dossier has an attestation not emailable' do
let(:emailable) { false }
let(:expected_attestation) { nil }
it 'Notification email is sent without the attestation' do
expect(controller).to receive(:capture_message)
subject
is_expected.to redirect_to redirect_to gestionnaire_dossier_path(procedure, dossier)
end
end end
end end

View file

@ -17,25 +17,11 @@ RSpec.describe NotificationMailer, type: :mailer do
describe '.send_notification' do describe '.send_notification' do
let(:email_template) { instance_double('email_template', subject_for_dossier: 'subject', body_for_dossier: 'body') } let(:email_template) { instance_double('email_template', subject_for_dossier: 'subject', body_for_dossier: 'body') }
let(:attestation) { nil }
subject { described_class.send_notification(dossier, email_template, attestation) } subject { described_class.send_notification(dossier, email_template) }
it { expect(subject.subject).to eq(email_template.subject_for_dossier) } it { expect(subject.subject).to eq(email_template.subject_for_dossier) }
it { expect(subject.body).to eq(email_template.body_for_dossier) } it { expect(subject.body).to eq(email_template.body_for_dossier) }
it { expect(subject.attachments['attestation.pdf']).to eq(nil) }
context 'when an attestation is provided' do
let(:attestation) { 'attestation' }
it do
expect(subject.attachments['attestation.pdf'].content_type)
.to eq('application/pdf; filename=attestation.pdf')
expect(subject.attachments['attestation.pdf'].body).to eq('attestation')
end
end
it_behaves_like "create a commentaire not notified" it_behaves_like "create a commentaire not notified"
end end

View file

@ -1,23 +0,0 @@
RSpec.describe Attestation, type: :model do
describe 'emailable' do
let(:attestation) do
attestation = Attestation.new
expect(attestation).to receive(:pdf).and_return(double(size: size))
attestation
end
subject { attestation.emailable? }
context 'when the pdf size is acceptable' do
let(:size) { Attestation::MAX_SIZE_EMAILABLE }
it { is_expected.to be true }
end
context 'when the pdf size is unacceptable' do
let(:size) { Attestation::MAX_SIZE_EMAILABLE + 1 }
it { is_expected.to be false }
end
end
end