Merge pull request #889 from sgmap/fix-mail-template-object-modification

[Fix #839] Fix mail template modification by side effect
This commit is contained in:
gregoirenovel 2017-10-23 14:47:10 +02:00 committed by GitHub
commit 18ca9dfc5f
2 changed files with 46 additions and 7 deletions

View file

@ -36,7 +36,7 @@ module MailTemplateConcern
def replace_tags(string, dossier) def replace_tags(string, dossier)
TAGS.inject(string) do |acc, tag| TAGS.inject(string) do |acc, tag|
acc.gsub!("--#{tag[:name]}--", replace_tag(tag, dossier)) || acc acc.gsub("--#{tag[:name]}--", replace_tag(tag, dossier)) || acc
end end
end end

View file

@ -1,17 +1,56 @@
require 'spec_helper' require 'spec_helper'
describe MailTemplateConcern do describe MailTemplateConcern do
describe '.replace_tags' do let(:dossier) { create :dossier }
let(:dossier) { create :dossier } let(:dossier2) { create :dossier }
let(:initiated_mail) { Mails::InitiatedMail.default } let(:initiated_mail) { Mails::InitiatedMail.default }
it 'works' do shared_examples "can replace tokens in template" do
initiated_mail.object = '[TPS] --numero_dossier-- --libelle_procedure-- --lien_dossier--' describe 'with no token to replace' do
let(:template) { '[TPS] rien à remplacer' }
it do
is_expected.to eq("[TPS] rien à remplacer")
end
end
describe 'with one token to replace' do
let(:template) { '[TPS] Dossier : --numero_dossier--' }
it do
is_expected.to eq("[TPS] Dossier : #{dossier.id}")
end
end
describe 'with multiples tokens to replace' do
let(:template) { '[TPS] --numero_dossier-- --libelle_procedure-- --lien_dossier--' }
it do
expected = expected =
"[TPS] #{dossier.id} #{dossier.procedure.libelle} " + "[TPS] #{dossier.id} #{dossier.procedure.libelle} " +
"<a target=\"_blank\" href=\"http://localhost:3000/users/dossiers/#{dossier.id}/recapitulatif\">http://localhost:3000/users/dossiers/#{dossier.id}/recapitulatif</a>" "<a target=\"_blank\" href=\"http://localhost:3000/users/dossiers/#{dossier.id}/recapitulatif\">http://localhost:3000/users/dossiers/#{dossier.id}/recapitulatif</a>"
expect(initiated_mail.object_for_dossier(dossier)).to eq(expected) is_expected.to eq(expected)
end
end
end
describe '.object_for_dossier' do
before { initiated_mail.object = template }
subject { initiated_mail.object_for_dossier(dossier) }
it_behaves_like "can replace tokens in template"
end
describe '.body_for_dossier' do
before { initiated_mail.body = template }
subject { initiated_mail.body_for_dossier(dossier) }
it_behaves_like "can replace tokens in template"
end
describe '.replace_tags' do
it "avoids side effects" do
subject = "n --numero_dossier--"
expect(initiated_mail.replace_tags(subject, dossier)).to eq("n #{dossier.id}")
expect(initiated_mail.replace_tags(subject, dossier2)).to eq("n #{dossier2.id}")
end end
end end
end end