Merge branch 'dev'

This commit is contained in:
Frederic Merizen 2018-05-09 11:17:48 +02:00
commit 2646a12434
14 changed files with 43 additions and 117 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

@ -2,8 +2,8 @@ class Administrateurs::ActivateBeforeExpirationJob < ApplicationJob
queue_as :cron queue_as :cron
def perform(*args) def perform(*args)
Administrateur.inactive.where(created_at: 2.days.ago.all_day).each do |a| Administrateur.inactive.where(created_at: 3.days.ago.all_day).each do |a|
AdministrateurMailer.activate_before_expiration(a).deliver_later a.remind_invitation!
end end
end end
end end

View file

@ -24,8 +24,6 @@ class FindDubiousProceduresJob < ApplicationJob
.group_by(&:procedure_id) .group_by(&:procedure_id)
.map { |_procedure_id, tdcs| [tdcs[0].procedure, tdcs] } .map { |_procedure_id, tdcs| [tdcs[0].procedure, tdcs] }
if dubious_procedures_and_tdcs.present?
AdministrationMailer.dubious_procedures(dubious_procedures_and_tdcs).deliver_now AdministrationMailer.dubious_procedures(dubious_procedures_and_tdcs).deliver_now
end end
end
end end

View file

@ -1,9 +1,10 @@
class AdministrateurMailer < ApplicationMailer class AdministrateurMailer < ApplicationMailer
layout 'mailers/layout' layout 'mailers/layout'
def activate_before_expiration(administrateur) def activate_before_expiration(administrateur, reset_password_token)
@administrateur = administrateur @administrateur = administrateur
@expiration_date = administrateur.reset_password_sent_at + Devise.reset_password_within @reset_password_token = reset_password_token
@expiration_date = @administrateur.reset_password_sent_at + Devise.reset_password_within
mail(to: administrateur.email, mail(to: administrateur.email,
subject: "demarches-simplifiees.fr - N'oubliez pas d'activer votre compte administrateur", subject: "demarches-simplifiees.fr - N'oubliez pas d'activer votre compte administrateur",
reply_to: "contact@demarches-simplifiees.fr") reply_to: "contact@demarches-simplifiees.fr")

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

@ -55,6 +55,16 @@ class Administrateur < ApplicationRecord
reset_password_token reset_password_token
end end
def remind_invitation!
if active?
raise "Impossible d'envoyer un rappel d'invitation à un utilisateur déjà actif !"
end
reset_password_token = set_reset_password_token
AdministrateurMailer.activate_before_expiration(self, reset_password_token).deliver_later
end
def invitation_expired? def invitation_expired?
!active && !reset_password_period_valid? !active && !reset_password_period_valid?
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

@ -8,7 +8,7 @@ Votre compte a été créé mais reste inactif, il arrivera à expiration le #{@
%br %br
%br %br
Afin dactiver votre compte, veuillez cliquer sur le lien ci-dessous : Afin dactiver votre compte, veuillez cliquer sur le lien ci-dessous :
= link_to(admin_activate_url(token: @administrateur.reset_password_token), admin_activate_url(token: @administrateur.reset_password_token)) = link_to(admin_activate_url(token: @reset_password_token), admin_activate_url(token: @reset_password_token))
%br %br
%br %br
Nous restons à votre disposition si vous avez besoin daccompagnement. Nous restons à votre disposition si vous avez besoin daccompagnement.

View file

@ -1,8 +1,11 @@
- content_for(:title, 'Liste de procédures douteuses') - content_for(:title, 'Liste de procédures douteuses')
%ul - if @procedures_and_type_de_champs.any?
%ul
- @procedures_and_type_de_champs.each do |procedure, type_de_champs| - @procedures_and_type_de_champs.each do |procedure, type_de_champs|
%li %li
= link_to "Nº #{procedure.id},", manager_procedure_url(procedure) = link_to "Nº #{procedure.id},", manager_procedure_url(procedure)
 #{procedure.libelle} :  #{procedure.libelle} :
%b= type_de_champs.map(&:libelle).join(', ') %b= type_de_champs.map(&:libelle).join(', ')
- else
Il n'y a aucune procédure douteuse aujourd'hui

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,37 +218,17 @@ 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 }
let(:expected_attestation) { 'pdf' }
it 'Notification email is sent with the attestation' do
subject subject
is_expected.to redirect_to redirect_to gestionnaire_dossier_path(procedure, dossier) is_expected.to redirect_to redirect_to gestionnaire_dossier_path(procedure, dossier)
end end
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
context 'when the attestation template uses the motivation field' do context 'when the attestation template uses the motivation field' do
let(:emailable) { false } let(:emailable) { false }
let(:template) { create(:attestation_template) } let(:template) { create(:attestation_template) }

View file

@ -32,13 +32,13 @@ RSpec.describe Administrateurs::ActivateBeforeExpirationJob, type: :job do
it { expect(AdministrateurMailer).not_to have_received(:activate_before_expiration) } it { expect(AdministrateurMailer).not_to have_received(:activate_before_expiration) }
end end
context "created 2 days ago" do context "created 3 days ago" do
before do before do
administrateur.update_columns(created_at: DateTime.new(2018, 03, 18, 20, 00)) administrateur.update_columns(created_at: DateTime.new(2018, 03, 17, 20, 00))
subject subject
end end
it { expect(AdministrateurMailer).to have_received(:activate_before_expiration).with(administrateur) } it { expect(AdministrateurMailer).to have_received(:activate_before_expiration).with(administrateur, kind_of(String)) }
end end
end end

View file

@ -30,25 +30,27 @@ RSpec.describe FindDubiousProceduresJob, type: :job do
expect(receive_procedure).to eq(procedure) expect(receive_procedure).to eq(procedure)
expect(receive_forbidden_tdcs).to match(forbidden_tdcs) expect(receive_forbidden_tdcs).to match(forbidden_tdcs)
expect(AdministrationMailer).to have_received(:dubious_procedures).with(@dubious_procedures_args)
end end
context 'and a whitelisted procedure' do context 'and a whitelisted procedure' do
let(:procedure) { create(:procedure, whitelisted_at: DateTime.now) } let(:procedure) { create(:procedure, whitelisted_at: DateTime.now) }
it { expect(AdministrationMailer).not_to have_received(:dubious_procedures) } it { expect(AdministrationMailer).to have_received(:dubious_procedures).with([]) }
end end
context 'and a archived procedure' do context 'and a archived procedure' do
let(:procedure) { create(:procedure, archived_at: DateTime.now) } let(:procedure) { create(:procedure, archived_at: DateTime.now) }
it { expect(AdministrationMailer).not_to have_received(:dubious_procedures) } it { expect(AdministrationMailer).to have_received(:dubious_procedures).with([]) }
end end
end end
context 'with no suspicious champs' do context 'with no suspicious champs' do
let(:tdcs) { [allowed_tdc] } let(:tdcs) { [allowed_tdc] }
it { expect(AdministrationMailer).not_to receive(:dubious_procedures) } it { expect(AdministrationMailer).to have_received(:dubious_procedures).with([]) }
end 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