demarches-normaliennes/spec/models/concern/mail_template_concern_spec.rb
Pierre de La Morinerie 4cb747fdb6 specs: always require rails_helper
Test helpers are separated between two files: spec_helper and
rails_helper. This separation is meant to allow tests that do not
require Rails (like testing standalone libs) to boot faster.

The spec_helper file is always loaded, through `--require spec_helper`
in the `.rspec` config file. When needed, the rails_helper file is
expected to be required manually.

This is fine, but:
- Many test files have a redundant `require 'spec_helper'` line;
- Many test files should require `rails_helper`, but don't.

Not requiring `rails_helper` will cause the Rails-concerned section of
the test environment not to be configured–which may cause subtle bugs
(like the test database not being properly initialized).

Moreover, Spring loads all the Rails files on preloading anyway. So the
gains from using only `spec_helper` are thin.

To streamline this process, this commit:
- Configures `.rspec` to require `rails_helper` by default;
- Remove all manual requires to spec_helper or rails_helper.

Reference: https://stackoverflow.com/questions/24145329/how-is-spec-rails-helper-rb-different-from-spec-spec-helper-rb-do-i-need-it
2020-03-31 12:48:32 +02:00

157 lines
5.8 KiB
Ruby

describe MailTemplateConcern do
let(:procedure) { create(:procedure) }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:dossier2) { create(:dossier, procedure: procedure) }
let(:initiated_mail) { create(:initiated_mail, procedure: procedure) }
let(:justificatif) { Rack::Test::UploadedFile.new("./spec/fixtures/files/piece_justificative_0.pdf", 'application/pdf') }
shared_examples "can replace tokens in template" do
describe 'with no token to replace' do
let(:template) { '[demarches-simplifiees.fr] rien à remplacer' }
it do
is_expected.to eq("[demarches-simplifiees.fr] rien à remplacer")
end
end
describe 'with one token to replace' do
let(:template) { '[demarches-simplifiees.fr] Dossier : --numéro du dossier--' }
it do
is_expected.to eq("[demarches-simplifiees.fr] Dossier : #{dossier.id}")
end
end
describe 'with multiples tokens to replace' do
let(:template) { '[demarches-simplifiees.fr] --numéro du dossier-- --libellé démarche-- --lien dossier--' }
it do
expected =
"[demarches-simplifiees.fr] #{dossier.id} #{dossier.procedure.libelle} " +
"<a target=\"_blank\" rel=\"noopener\" href=\"http://localhost:3000/dossiers/#{dossier.id}\">http://localhost:3000/dossiers/#{dossier.id}</a>"
is_expected.to eq(expected)
end
end
end
describe '#subject_for_dossier' do
before { initiated_mail.subject = template }
subject { initiated_mail.subject_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 'tags' do
describe 'in initiated mail' do
it "does not treat date de passage en instruction as a tag" do
expect(initiated_mail.tags).not_to include(include({ libelle: 'date de passage en instruction' }))
end
end
describe 'in received mail' do
let(:received_mail) { create(:received_mail, procedure: procedure) }
it "treats date de passage en instruction as a tag" do
expect(received_mail.tags).to include(include({ libelle: 'date de passage en instruction' }))
end
end
describe '--lien attestation--' do
let(:attestation_template) { AttestationTemplate.new(activated: true) }
let(:procedure) { create(:procedure, attestation_template: attestation_template) }
subject { mail.body_for_dossier(dossier) }
before do
dossier.attestation = dossier.build_attestation
dossier.reload
mail.body = "--lien attestation--"
end
describe "in closed mail without justificatif" do
let(:mail) { create(:closed_mail, procedure: procedure) }
it { is_expected.to eq("<a target=\"_blank\" rel=\"noopener\" href=\"http://localhost:3000/dossiers/#{dossier.id}/attestation\">http://localhost:3000/dossiers/#{dossier.id}/attestation</a>") }
it { is_expected.to_not include("Télécharger le justificatif") }
end
describe "in closed mail with justificatif" do
before do
dossier.justificatif_motivation.attach(justificatif)
end
let(:mail) { create(:closed_mail, procedure: procedure) }
it { expect(dossier.justificatif_motivation).to be_attached }
it { is_expected.to include("<a target=\"_blank\" rel=\"noopener\" href=\"http://localhost:3000/dossiers/#{dossier.id}/attestation\">http://localhost:3000/dossiers/#{dossier.id}/attestation</a>") }
it { is_expected.to_not include("Télécharger le justificatif") }
end
describe "in refuse mail" do
let(:mail) { create(:refused_mail, procedure: procedure) }
it { is_expected.to eq("--lien attestation--") }
end
describe "in without continuation mail" do
let(:mail) { create(:without_continuation_mail, procedure: procedure) }
it { is_expected.to eq("--lien attestation--") }
end
end
shared_examples 'inserting the --lien document justificatif-- tag' do
let(:procedure) { create(:procedure) }
subject { mail.body_for_dossier(dossier) }
before do
mail.body = "--lien document justificatif--"
end
describe 'without justificatif' do
it { is_expected.to include("[l'instructeur n'a pas joint de document supplémentaire]") }
end
describe 'with justificatif' do
before do
dossier.justificatif_motivation.attach(justificatif)
end
it { expect(dossier.justificatif_motivation).to be_attached }
it { is_expected.to include("Télécharger le document justificatif") }
end
end
context 'in closed mail' do
let(:mail) { create(:closed_mail, procedure: procedure) }
it_behaves_like 'inserting the --lien document justificatif-- tag'
end
context 'in refused mail' do
let(:mail) { create(:refused_mail, procedure: procedure) }
it_behaves_like 'inserting the --lien document justificatif-- tag'
end
context 'in without continuation mail' do
let(:mail) { create(:without_continuation_mail, procedure: procedure) }
it_behaves_like 'inserting the --lien document justificatif-- tag'
end
end
describe '#replace_tags' do
before { initiated_mail.body = "n --numéro du dossier--" }
it "avoids side effects" do
expect(initiated_mail.body_for_dossier(dossier)).to eq("n #{dossier.id}")
expect(initiated_mail.body_for_dossier(dossier2)).to eq("n #{dossier2.id}")
end
end
describe '#update_rich_body' do
before { initiated_mail.update(body: "Voici le corps du mail") }
it { expect(initiated_mail.rich_body.to_plain_text).to eq(initiated_mail.body) }
end
end