2017-04-25 17:02:54 +02:00
|
|
|
RSpec.describe Avis, type: :model do
|
2019-08-06 11:02:54 +02:00
|
|
|
let(:claimant) { create(:instructeur) }
|
2017-05-23 13:28:17 +02:00
|
|
|
|
2018-03-20 16:00:30 +01:00
|
|
|
describe '#email_to_display' do
|
2017-04-25 17:02:54 +02:00
|
|
|
let(:invited_email) { 'invited@avis.com' }
|
2017-05-23 13:28:17 +02:00
|
|
|
let!(:avis) do
|
|
|
|
avis = create(:avis, email: invited_email, dossier: create(:dossier))
|
2019-08-06 11:02:54 +02:00
|
|
|
avis.instructeur = nil
|
2017-05-23 13:28:17 +02:00
|
|
|
avis
|
|
|
|
end
|
2017-04-25 17:02:54 +02:00
|
|
|
|
|
|
|
subject { avis.email_to_display }
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
context 'when instructeur is not known' do
|
2018-12-24 17:28:20 +01:00
|
|
|
it { is_expected.to eq(invited_email) }
|
2017-04-25 17:02:54 +02:00
|
|
|
end
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
context 'when instructeur is known' do
|
|
|
|
let!(:avis) { create(:avis, email: nil, instructeur: create(:instructeur), dossier: create(:dossier)) }
|
2017-04-25 17:02:54 +02:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
it { is_expected.to eq(avis.instructeur.email) }
|
2017-04-25 17:02:54 +02:00
|
|
|
end
|
|
|
|
end
|
2017-05-02 13:54:57 +02:00
|
|
|
|
|
|
|
describe '.by_latest' do
|
|
|
|
context 'with 3 avis' do
|
2018-12-24 17:28:20 +01:00
|
|
|
let!(:avis) { create(:avis) }
|
|
|
|
let!(:avis2) { create(:avis, updated_at: 4.hours.ago) }
|
|
|
|
let!(:avis3) { create(:avis, updated_at: 3.hours.ago) }
|
2017-05-02 13:54:57 +02:00
|
|
|
|
|
|
|
subject { Avis.by_latest }
|
|
|
|
|
2018-01-15 21:54:40 +01:00
|
|
|
it { expect(subject).to eq([avis, avis3, avis2]) }
|
2017-05-02 13:54:57 +02:00
|
|
|
end
|
|
|
|
end
|
2017-05-02 16:13:09 +02:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
describe ".link_avis_to_instructeur" do
|
|
|
|
let(:instructeur) { create(:instructeur) }
|
2017-05-02 16:13:09 +02:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
subject { Avis.link_avis_to_instructeur(instructeur) }
|
2017-05-02 16:13:09 +02:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
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) }
|
2017-05-02 16:13:09 +02:00
|
|
|
|
|
|
|
before do
|
|
|
|
subject
|
|
|
|
avis.reload
|
|
|
|
avis2.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(avis.email).to be_nil }
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect(avis.instructeur).to eq(instructeur) }
|
2017-05-02 16:13:09 +02:00
|
|
|
it { expect(avis2.email).to be_nil }
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect(avis2.instructeur).to eq(instructeur) }
|
2017-05-02 16:13:09 +02:00
|
|
|
end
|
|
|
|
end
|
2017-05-12 10:22:18 +02:00
|
|
|
|
2018-03-20 16:00:30 +01:00
|
|
|
describe '.avis_exists_and_email_belongs_to_avis?' do
|
2017-05-12 10:22:18 +02:00
|
|
|
let(:dossier) { create(:dossier) }
|
|
|
|
let(:invited_email) { 'invited@avis.com' }
|
2017-05-23 13:28:17 +02:00
|
|
|
let!(:avis) { create(:avis, email: invited_email, dossier: dossier) }
|
2017-05-12 10:22:18 +02:00
|
|
|
|
|
|
|
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
|
2017-07-20 11:54:41 +02:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
describe '#try_to_assign_instructeur' do
|
|
|
|
let!(:instructeur) { create(:instructeur) }
|
2020-07-20 16:32:22 +02:00
|
|
|
let(:avis) { create(:avis, claimant: claimant, email: email, dossier: create(:dossier)) }
|
2017-07-20 16:30:11 +02:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
context 'when the email belongs to a instructeur' do
|
|
|
|
let(:email) { instructeur.email }
|
2017-07-20 16:30:11 +02:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect(avis.instructeur).to eq(instructeur) }
|
2017-07-20 16:30:11 +02:00
|
|
|
it { expect(avis.email).to be_nil }
|
|
|
|
end
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
context 'when the email does not belongs to a instructeur' do
|
2017-07-20 16:30:11 +02:00
|
|
|
let(:email) { 'unknown@email' }
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect(avis.instructeur).to be_nil }
|
2017-07-20 16:30:11 +02:00
|
|
|
it { expect(avis.email).to eq(email) }
|
|
|
|
end
|
|
|
|
end
|
2017-09-12 17:02:45 +02:00
|
|
|
|
2018-02-28 15:46:27 +01:00
|
|
|
describe "email sanitization" do
|
2019-08-06 11:02:54 +02:00
|
|
|
subject { Avis.create(claimant: claimant, email: email, dossier: create(:dossier), instructeur: create(:instructeur)) }
|
2017-09-12 17:02:45 +02:00
|
|
|
|
|
|
|
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
|
2017-10-27 16:27:55 +02:00
|
|
|
|
|
|
|
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
|
2017-09-12 17:02:45 +02:00
|
|
|
end
|
2020-07-16 11:14:37 +02:00
|
|
|
|
2020-07-16 20:42:50 +02:00
|
|
|
describe ".revoke_by!" do
|
|
|
|
let(:claimant) { create(:instructeur) }
|
|
|
|
|
2020-07-16 11:14:37 +02:00
|
|
|
context "when no answer" do
|
2020-07-16 20:42:50 +02:00
|
|
|
let(:avis) { create(:avis, claimant: claimant) }
|
2020-07-16 11:14:37 +02:00
|
|
|
|
|
|
|
it "supprime l'avis" do
|
2020-07-16 20:42:50 +02:00
|
|
|
avis.revoke_by!(claimant)
|
2020-07-16 11:14:37 +02:00
|
|
|
expect(avis).to be_destroyed
|
|
|
|
expect(Avis.count).to eq 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with answer" do
|
2020-07-16 20:42:50 +02:00
|
|
|
let(:avis) { create(:avis, :with_answer, claimant: claimant) }
|
2020-07-16 11:14:37 +02:00
|
|
|
|
|
|
|
it "revoque l'avis" do
|
2020-07-16 20:42:50 +02:00
|
|
|
avis.revoke_by!(claimant)
|
2020-07-16 11:14:37 +02:00
|
|
|
expect(avis).not_to be_destroyed
|
|
|
|
expect(avis).to be_revoked
|
|
|
|
end
|
|
|
|
end
|
2020-07-16 20:42:50 +02:00
|
|
|
|
|
|
|
context "by an instructeur who can't revoke" do
|
|
|
|
let(:avis) { create(:avis, :with_answer, claimant: claimant) }
|
|
|
|
let(:expert) { create(:instructeur) }
|
|
|
|
|
|
|
|
it "doesn't revoke avis and returns false" do
|
|
|
|
result = avis.revoke_by!(expert)
|
|
|
|
expect(result).to be_falsey
|
|
|
|
expect(avis).not_to be_destroyed
|
|
|
|
expect(avis).not_to be_revoked
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "revokable_by?" do
|
|
|
|
let(:instructeur) { create(:instructeur) }
|
|
|
|
let(:instructeurs) { [instructeur] }
|
|
|
|
let(:procedure) { create(:procedure, :published, instructeurs: instructeurs) }
|
|
|
|
let(:dossier) { create(:dossier, :en_instruction, procedure: procedure) }
|
|
|
|
let(:claimant_expert) { create(:instructeur) }
|
|
|
|
let(:expert) { create(:instructeur) }
|
|
|
|
let(:another_expert) { create(:instructeur) }
|
|
|
|
|
|
|
|
context "when avis claimed by an expert" do
|
|
|
|
let(:avis) { create(:avis, dossier: dossier, claimant: claimant_expert, instructeur: expert) }
|
|
|
|
let(:another_avis) { create(:avis, dossier: dossier, claimant: instructeur, instructeur: another_expert) }
|
|
|
|
it "is revokable by this expert or any instructeurs of the dossier" do
|
|
|
|
expect(avis.revokable_by?(claimant_expert)).to be_truthy
|
|
|
|
expect(avis.revokable_by?(another_expert)).to be_falsy
|
|
|
|
expect(avis.revokable_by?(instructeur)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when avis claimed by an instructeur" do
|
|
|
|
let(:avis) { create(:avis, dossier: dossier, claimant: instructeur, instructeur: expert) }
|
|
|
|
let(:another_avis) { create(:avis, dossier: dossier, claimant: expert, instructeur: another_expert) }
|
|
|
|
let(:another_instructeur) { create(:instructeur) }
|
|
|
|
let(:instructeurs) { [instructeur, another_instructeur] }
|
|
|
|
|
|
|
|
it "is revokable by any instructeur of the dossier, not by an expert" do
|
|
|
|
expect(avis.revokable_by?(instructeur)).to be_truthy
|
|
|
|
expect(avis.revokable_by?(another_expert)).to be_falsy
|
|
|
|
expect(avis.revokable_by?(another_instructeur)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2020-07-16 11:14:37 +02:00
|
|
|
end
|
2020-12-17 14:28:21 +01:00
|
|
|
|
|
|
|
describe '#invited_expert_emails' do
|
|
|
|
let!(:procedure) { create(:procedure, :published) }
|
|
|
|
|
|
|
|
subject { Avis.invited_expert_emails(procedure) }
|
|
|
|
|
|
|
|
context 'when there is one dossier' do
|
|
|
|
let!(:dossier) { create(:dossier, procedure: procedure) }
|
|
|
|
|
|
|
|
context 'when a procedure has one avis and unknown instructeur' do
|
|
|
|
let!(:avis) { create(:avis, dossier: dossier, email: 'expert@expert.com') }
|
|
|
|
|
|
|
|
it { is_expected.to eq(['expert@expert.com']) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a procedure has one avis and known instructeur' do
|
|
|
|
let!(:avis) { create(:avis, dossier: dossier, instructeur: create(:instructeur, email: 'expert@expert.com')) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(['expert@expert.com']) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a dossier has 2 avis from the same expert' do
|
|
|
|
let!(:avis) { create(:avis, dossier: dossier, email: 'expert@expert.com') }
|
|
|
|
let!(:avis2) { create(:avis, dossier: dossier, email: 'expert@expert.com') }
|
|
|
|
|
|
|
|
it { is_expected.to eq(['expert@expert.com']) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are two dossiers' do
|
|
|
|
let!(:dossier) { create(:dossier, procedure: procedure) }
|
|
|
|
let!(:dossier2) { create(:dossier, procedure: procedure) }
|
|
|
|
|
|
|
|
context 'and each one has an avis from 3 different experts' do
|
|
|
|
let!(:avis) { create(:avis, dossier: dossier, instructeur: create(:instructeur, email: '2_expert@expert.com')) }
|
|
|
|
let!(:unaffected_avis) { create(:avis, dossier: dossier2, email: '3_expert@expert.com') }
|
|
|
|
let!(:unaffected_avis2) { create(:avis, dossier: dossier2, email: '1_expert@expert.com') }
|
|
|
|
|
|
|
|
it { is_expected.to eq(['1_expert@expert.com', '2_expert@expert.com', '3_expert@expert.com']) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-25 17:02:54 +02:00
|
|
|
end
|