Merge pull request #1782 from betagouv/frederic/fix_1563/warn_on_mail_attestation_discrepancy

#1563 – warn on mail attestation discrepancy
This commit is contained in:
Frederic Merizen 2018-04-06 10:23:55 +02:00 committed by GitHub
commit fd194bd667
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 169 additions and 0 deletions

View file

@ -265,6 +265,18 @@ class Procedure < ApplicationRecord
update_attribute('whitelisted_at', DateTime.now)
end
def closed_mail_template_attestation_inconsistency_state
# As an optimization, dont check the predefined templates (they are presumed correct)
if closed_mail.present?
tag_present = closed_mail.body.include?("--lien attestation--")
if attestation_template&.activated? && !tag_present
:missing_tag
elsif !attestation_template&.activated? && tag_present
:extraneous_tag
end
end
end
private
def field_hash(label, table, column)

View file

@ -0,0 +1,22 @@
- case @procedure.closed_mail_template_attestation_inconsistency_state
- when :missing_tag
.center
.alert{ class: flash_class('alert') }
Cette procédure comporte une attestation, mais laccusé dacceptation ne la mentionne pas :
- if !@procedure.locked?
= link_to('désactivez lattestation', edit_admin_procedure_attestation_template_path(@procedure))
ou
= link_to('ajoutez la balise', edit_admin_procedure_mail_template_path(@procedure, Mails::ClosedMail::SLUG))
%code{ style: "white-space: pre-wrap;" }
\--lien attestation--
à laccusé dacceptation
- when :extraneous_tag
.center
.alert{ class: flash_class('alert') }
Cette procédure ne comporte pas dattestation, mais laccusé dacceptation en mentionne une :
= link_to('activez lattestation', edit_admin_procedure_attestation_template_path(@procedure))
ou
= link_to('enlevez la balise', edit_admin_procedure_mail_template_path(@procedure, Mails::ClosedMail::SLUG))
%code{ style: "white-space: pre-wrap;" }
\--lien attestation--
de laccusé dacceptation

View file

@ -1,3 +1,4 @@
= render partial: 'admin/closed_mail_template_attestation_inconsistency_alert'
#attestation-template-edit.row.white-back
= form_for @attestation_template, url: admin_procedure_attestation_template_path do |f|
.row

View file

@ -1,3 +1,5 @@
- if params[:id] == 'closed_mail'
= render partial: 'admin/closed_mail_template_attestation_inconsistency_alert'
.white-back
%h3
= @mail_template.class.const_get(:DISPLAYED_NAME)

View file

@ -1,3 +1,4 @@
= render partial: 'admin/closed_mail_template_attestation_inconsistency_alert'
#custom-mails
.wrapper
%h1 E-mails personnalisables

View file

@ -1,3 +1,4 @@
= render partial: 'admin/closed_mail_template_attestation_inconsistency_alert'
.row.white-back
#procedure_show
- if @facade.procedure.brouillon?

View file

@ -67,6 +67,90 @@ describe Procedure do
end
end
describe '#closed_mail_template_attestation_inconsistency_state' do
let(:procedure_without_attestation) { create(:procedure, closed_mail: closed_mail) }
let(:procedure_with_active_attestation) do
procedure = create(:procedure, closed_mail: closed_mail)
create(:attestation_template, procedure: procedure, activated: true)
procedure
end
let(:procedure_with_inactive_attestation) do
procedure = create(:procedure, closed_mail: closed_mail)
create(:attestation_template, procedure: procedure, activated: false)
procedure
end
subject { procedure.closed_mail_template_attestation_inconsistency_state }
context 'with a custom mail template' do
context 'that contains a lien attestation tag' do
let(:closed_mail) { create(:closed_mail, body: '--lien attestation--') }
context 'when the procedure doesnt have an attestation' do
let(:procedure) { procedure_without_attestation }
it { is_expected.to eq(:extraneous_tag) }
end
context 'when the procedure has an active attestation' do
let(:procedure) { procedure_with_active_attestation }
it { is_expected.to be(nil) }
end
context 'when the procedure has an inactive attestation' do
let(:procedure) { procedure_with_inactive_attestation }
it { is_expected.to eq(:extraneous_tag) }
end
end
context 'that doesnt contain a lien attestation tag' do
let(:closed_mail) { create(:closed_mail) }
context 'when the procedure doesnt have an attestation' do
let(:procedure) { procedure_without_attestation }
it { is_expected.to be(nil) }
end
context 'when the procedure has an active attestation' do
let(:procedure) { procedure_with_active_attestation }
it { is_expected.to eq(:missing_tag) }
end
context 'when the procedure has an inactive attestation' do
let(:procedure) { procedure_with_inactive_attestation }
it { is_expected.to be(nil) }
end
end
end
context 'with the default mail template' do
let(:closed_mail) { nil }
context 'when the procedure doesnt have an attestation' do
let(:procedure) { procedure_without_attestation }
it { is_expected.to be(nil) }
end
context 'when the procedure has an active attestation' do
let(:procedure) { procedure_with_active_attestation }
it { is_expected.to be(nil) }
end
context 'when the procedure has an inactive attestation' do
let(:procedure) { procedure_with_inactive_attestation }
it { is_expected.to be(nil) }
end
end
end
describe 'validation' do
context 'libelle' do
it { is_expected.not_to allow_value(nil).for(:libelle) }

View file

@ -0,0 +1,46 @@
require 'spec_helper'
describe 'admin/_closed_mail_template_attestation_inconsistency_alert.html.haml', type: :view do
let(:procedure) { create(:procedure, closed_mail: closed_mail, published_at: published_at) }
let(:published_at) { nil }
def alert
assign(:procedure, procedure)
render
rendered
end
context 'when there is no inconsistency' do
let(:closed_mail) { nil }
it { expect(alert).to be_empty }
end
context 'when there is no active attestation but the closed mail template mentions one' do
let(:closed_mail) { create(:closed_mail, body: '--lien attestation--') }
it { expect(alert).to include("Cette procédure ne comporte pas dattestation, mais laccusé dacceptation en mentionne une") }
it { expect(alert).to include(edit_admin_procedure_attestation_template_path(procedure)) }
it { expect(alert).to include(edit_admin_procedure_mail_template_path(procedure, Mails::ClosedMail::SLUG)) }
end
context 'when there is an active attestation but the closed mail template does not mention it' do
let(:closed_mail) { create(:closed_mail) }
let!(:attestation_template) { create(:attestation_template, procedure: procedure, activated: true) }
it { expect(alert).to include("Cette procédure comporte une attestation, mais laccusé dacceptation ne la mentionne pas") }
it { expect(alert).to include(edit_admin_procedure_mail_template_path(procedure, Mails::ClosedMail::SLUG)) }
context 'when the procedure has been published, the attestation cannot be deactivated' do
let(:published_at) { Time.now }
it { expect(procedure.locked?).to be_truthy }
it { expect(alert).not_to include(edit_admin_procedure_attestation_template_path(procedure)) }
end
context 'when the procedure is still a draft' do
it { expect(procedure.locked?).to be_falsey }
it { expect(alert).to include(edit_admin_procedure_attestation_template_path(procedure)) }
end
end
end