4cb747fdb6
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
134 lines
3.8 KiB
Ruby
134 lines
3.8 KiB
Ruby
RSpec.describe Avis, type: :model do
|
|
let(:claimant) { create(:instructeur) }
|
|
|
|
describe '#email_to_display' do
|
|
let(:invited_email) { 'invited@avis.com' }
|
|
let!(:avis) do
|
|
avis = create(:avis, email: invited_email, dossier: create(:dossier))
|
|
avis.instructeur = nil
|
|
avis
|
|
end
|
|
|
|
subject { avis.email_to_display }
|
|
|
|
context 'when instructeur is not known' do
|
|
it { is_expected.to eq(invited_email) }
|
|
end
|
|
|
|
context 'when instructeur is known' do
|
|
let!(:avis) { create(:avis, email: nil, instructeur: create(:instructeur), dossier: create(:dossier)) }
|
|
|
|
it { is_expected.to eq(avis.instructeur.email) }
|
|
end
|
|
end
|
|
|
|
describe '.by_latest' do
|
|
context 'with 3 avis' do
|
|
let!(:avis) { create(:avis) }
|
|
let!(:avis2) { create(:avis, updated_at: 4.hours.ago) }
|
|
let!(:avis3) { create(:avis, updated_at: 3.hours.ago) }
|
|
|
|
subject { Avis.by_latest }
|
|
|
|
it { expect(subject).to eq([avis, avis3, avis2]) }
|
|
end
|
|
end
|
|
|
|
describe ".link_avis_to_instructeur" do
|
|
let(:instructeur) { create(:instructeur) }
|
|
|
|
subject { Avis.link_avis_to_instructeur(instructeur) }
|
|
|
|
context 'when there are 2 avis linked by email to a instructeur' do
|
|
let!(:avis) { create(:avis, email: instructeur.email, instructeur: nil) }
|
|
let!(:avis2) { create(:avis, email: instructeur.email, instructeur: nil) }
|
|
|
|
before do
|
|
subject
|
|
avis.reload
|
|
avis2.reload
|
|
end
|
|
|
|
it { expect(avis.email).to be_nil }
|
|
it { expect(avis.instructeur).to eq(instructeur) }
|
|
it { expect(avis2.email).to be_nil }
|
|
it { expect(avis2.instructeur).to eq(instructeur) }
|
|
end
|
|
end
|
|
|
|
describe '.avis_exists_and_email_belongs_to_avis?' do
|
|
let(:dossier) { create(:dossier) }
|
|
let(:invited_email) { 'invited@avis.com' }
|
|
let!(:avis) { create(:avis, email: invited_email, dossier: dossier) }
|
|
|
|
subject { Avis.avis_exists_and_email_belongs_to_avis?(avis_id, email) }
|
|
|
|
context 'when the avis is unknown' do
|
|
let(:avis_id) { 666 }
|
|
let(:email) { 'unknown@mystery.com' }
|
|
|
|
it { is_expected.to be false }
|
|
end
|
|
|
|
context 'when the avis is known' do
|
|
let(:avis_id) { avis.id }
|
|
|
|
context 'when the email belongs to the invitation' do
|
|
let(:email) { invited_email }
|
|
it { is_expected.to be true }
|
|
end
|
|
|
|
context 'when the email is unknown' do
|
|
let(:email) { 'unknown@mystery.com' }
|
|
it { is_expected.to be false }
|
|
end
|
|
end
|
|
end
|
|
|
|
describe '#try_to_assign_instructeur' do
|
|
let!(:instructeur) { create(:instructeur) }
|
|
let(:avis) { Avis.create(claimant: claimant, email: email, dossier: create(:dossier)) }
|
|
|
|
context 'when the email belongs to a instructeur' do
|
|
let(:email) { instructeur.email }
|
|
|
|
it { expect(avis.instructeur).to eq(instructeur) }
|
|
it { expect(avis.email).to be_nil }
|
|
end
|
|
|
|
context 'when the email does not belongs to a instructeur' do
|
|
let(:email) { 'unknown@email' }
|
|
|
|
it { expect(avis.instructeur).to be_nil }
|
|
it { expect(avis.email).to eq(email) }
|
|
end
|
|
end
|
|
|
|
describe "email sanitization" do
|
|
subject { Avis.create(claimant: claimant, email: email, dossier: create(:dossier), instructeur: create(:instructeur)) }
|
|
|
|
context "when there is no email" do
|
|
let(:email) { nil }
|
|
|
|
it { expect(subject.email).to be_nil }
|
|
end
|
|
|
|
context "when the email is in lowercase" do
|
|
let(:email) { "toto@tps.fr" }
|
|
|
|
it { expect(subject.email).to eq("toto@tps.fr") }
|
|
end
|
|
|
|
context "when the email is not in lowercase" do
|
|
let(:email) { "TOTO@tps.fr" }
|
|
|
|
it { expect(subject.email).to eq("toto@tps.fr") }
|
|
end
|
|
|
|
context "when the email has some spaces before and after" do
|
|
let(:email) { " toto@tps.fr " }
|
|
|
|
it { expect(subject.email).to eq("toto@tps.fr") }
|
|
end
|
|
end
|
|
end
|