[Fix #196] Attestation: join the attestation to the closed mail

Add a upper limit to the attachment size as it could be a problem with Mailjet and receiver
(https://www.mailjet.com/support/what-is-the-size-limit-for-attachments-files-sent-via-mailjet,289.htm)

If the attestation cannot be sent, it is logged in sentry
This commit is contained in:
Simon Lehericey 2017-06-09 22:29:48 +02:00
parent b664709c3d
commit e60ce35ae8
7 changed files with 114 additions and 18 deletions

View file

@ -106,6 +106,8 @@ gem 'select2-rails'
gem 'prawn', '~> 2.0.1' gem 'prawn', '~> 2.0.1'
gem 'prawn_rails', '~> 0.0.11' gem 'prawn_rails', '~> 0.0.11'
gem 'sentry-raven'
group :test do group :test do
gem 'capybara' gem 'capybara'
gem 'launchy' gem 'launchy'
@ -148,8 +150,3 @@ group :development, :test do
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'dotenv-rails' gem 'dotenv-rails'
end end
group :production, :staging do
gem 'sentry-raven'
end

View file

@ -1,4 +1,6 @@
class Backoffice::DossiersController < Backoffice::DossiersListController class Backoffice::DossiersController < Backoffice::DossiersListController
include ActionView::Helpers::NumberHelper
respond_to :html, :xlsx, :ods, :csv respond_to :html, :xlsx, :ods, :csv
prepend_before_action :store_current_location, only: :show prepend_before_action :store_current_location, only: :show
@ -112,6 +114,8 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
dossier = @facade.dossier dossier = @facade.dossier
attestation_pdf = nil
case params[:process_action] case params[:process_action]
when "refuse" when "refuse"
next_step = "refuse" next_step = "refuse"
@ -125,12 +129,15 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
next_step = "close" next_step = "close"
notice = "Dossier traité avec succès." notice = "Dossier traité avec succès."
template = dossier.procedure.closed_mail_template template = dossier.procedure.closed_mail_template
if check_attestation_emailable(dossier)
attestation_pdf = dossier.attestation.pdf.read
end
end end
dossier.next_step! 'gestionnaire', next_step, motivation dossier.next_step! 'gestionnaire', next_step, motivation
flash.notice = notice flash.notice = notice
NotificationMailer.send_notification(dossier, template).deliver_now! NotificationMailer.send_notification(dossier, template, attestation_pdf).deliver_now!
redirect_to backoffice_dossier_path(id: dossier.id) redirect_to backoffice_dossier_path(id: dossier.id)
end end
@ -185,6 +192,16 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
private private
def check_attestation_emailable(dossier)
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 store_current_location def store_current_location
if !gestionnaire_signed_in? if !gestionnaire_signed_in?
store_location_for(:gestionnaire, request.url) store_location_for(:gestionnaire, request.url)

View file

@ -3,12 +3,16 @@ class NotificationMailer < ApplicationMailer
after_action :create_commentaire_for_notification, only: :send_notification after_action :create_commentaire_for_notification, only: :send_notification
def send_notification(dossier, mail_template) def send_notification(dossier, mail_template, attestation = nil)
vars_mailer(dossier) vars_mailer(dossier)
@object = mail_template.object_for_dossier dossier @object = mail_template.object_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: @object) { |format| format.html { @body } } mail(subject: @object) { |format| format.html { @body } }
end end

View file

@ -2,4 +2,10 @@ 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

@ -268,7 +268,7 @@ describe Backoffice::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)).and_return(NotificationMailer) .with(dossier, kind_of(Mails::RefusedMail), nil).and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_now!) expect(NotificationMailer).to receive(:deliver_now!)
subject subject
@ -294,7 +294,7 @@ describe Backoffice::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)).and_return(NotificationMailer) .with(dossier, kind_of(Mails::WithoutContinuationMail), nil).and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_now!) expect(NotificationMailer).to receive(:deliver_now!)
subject subject
@ -304,9 +304,17 @@ describe Backoffice::DossiersController, type: :controller do
end end
context "with close" do context "with close" do
let(:expected_attestation) { nil }
before do before do
dossier.received! dossier.received!
sign_in gestionnaire sign_in gestionnaire
expect(NotificationMailer).to receive(:send_notification)
.with(dossier, kind_of(Mails::ClosedMail), expected_attestation)
.and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_now!)
end end
subject { post :process_dossier, params: { process_action: "close", dossier_id: dossier_id} } subject { post :process_dossier, params: { process_action: "close", dossier_id: dossier_id} }
@ -318,17 +326,44 @@ describe Backoffice::DossiersController, type: :controller do
expect(dossier.state).to eq('closed') expect(dossier.state).to eq('closed')
end end
context 'when the dossier does not have any attestation' do
it 'Notification email is sent' do it 'Notification email is sent' do
expect(NotificationMailer).to receive(:send_notification) subject
.with(dossier, kind_of(Mails::ClosedMail)).and_return(NotificationMailer) end
expect(NotificationMailer).to receive(:deliver_now!) end
context 'when the dossier has an attestation' do
let(:emailable) { false }
before do
fake_attestation = double(pdf: double(read: 'pdf', size: 2.megabytes), emailable?: emailable)
allow_any_instance_of(Dossier).to receive(:attestation).and_return(fake_attestation)
end
context 'emailable' do
let(:emailable) { true }
let(:expected_attestation) { 'pdf' }
it 'Notification email is sent with the attestation' do
subject
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 subject
end end
end
it { is_expected.to redirect_to backoffice_dossier_path(id: dossier.id) } it { is_expected.to redirect_to backoffice_dossier_path(id: dossier.id) }
end end
end end
end
describe 'PUT #toggle_follow' do describe 'PUT #toggle_follow' do
before do before do

View file

@ -5,11 +5,25 @@ RSpec.describe NotificationMailer, type: :mailer do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:dossier) { create(:dossier, user: user) } let(:dossier) { create(:dossier, user: user) }
let(:email) { instance_double('email', object_for_dossier: 'object', body_for_dossier: 'body') } let(:email) { instance_double('email', object_for_dossier: 'object', body_for_dossier: 'body') }
let (:notifications_count_before) { Notification.count } let(:attestation) { nil }
subject { described_class.send_notification(dossier, email) } let(:notifications_count_before) { Notification.count }
subject { described_class.send_notification(dossier, email, attestation) }
it { expect(subject.subject).to eq(email.object_for_dossier) } it { expect(subject.subject).to eq(email.object_for_dossier) }
it { expect(subject.body).to eq(email.body_for_dossier) } it { expect(subject.body).to eq(email.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 "creates a commentaire, which is not notified" do it "creates a commentaire, which is not notified" do
described_class.send_notification(dossier, email).deliver_now described_class.send_notification(dossier, email).deliver_now

View file

@ -0,0 +1,23 @@
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