2016-11-14 18:36:09 +01:00
|
|
|
require 'spec_helper'
|
2015-09-22 10:15:12 +02:00
|
|
|
|
|
|
|
describe Gestionnaire, type: :model do
|
2016-07-18 18:24:29 +02:00
|
|
|
let(:admin) { create :administrateur }
|
|
|
|
let!(:procedure) { create :procedure, administrateur: admin }
|
|
|
|
let!(:procedure_2) { create :procedure, administrateur: admin }
|
2017-01-05 12:44:15 +01:00
|
|
|
let!(:procedure_3) { create :procedure, administrateur: admin }
|
2016-07-18 18:24:29 +02:00
|
|
|
let(:gestionnaire) { create :gestionnaire, procedure_filter: procedure_filter, administrateurs: [admin] }
|
2017-01-05 12:44:15 +01:00
|
|
|
let(:procedure_filter) { nil }
|
|
|
|
let!(:procedure_assign) { create :assign_to, gestionnaire: gestionnaire, procedure: procedure }
|
2016-07-18 18:24:29 +02:00
|
|
|
|
|
|
|
before do
|
|
|
|
create :assign_to, gestionnaire: gestionnaire, procedure: procedure_2
|
|
|
|
end
|
|
|
|
|
2017-07-17 11:28:14 +02:00
|
|
|
describe 'follow' do
|
|
|
|
let(:dossier) { create :dossier }
|
|
|
|
let(:already_followed_dossier) { create :dossier }
|
|
|
|
|
|
|
|
before { gestionnaire.followed_dossiers << already_followed_dossier }
|
|
|
|
|
|
|
|
context 'when a gestionnaire follow a dossier for the first time' do
|
|
|
|
before { gestionnaire.follow(dossier) }
|
|
|
|
|
|
|
|
it { expect(gestionnaire.follow?(dossier)).to be true }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a gestionnaire follows a dossier already followed' do
|
|
|
|
before { gestionnaire.follow(already_followed_dossier) }
|
|
|
|
|
|
|
|
it { expect(gestionnaire.follow?(already_followed_dossier)).to be true }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-18 18:24:29 +02:00
|
|
|
describe '#follow?' do
|
|
|
|
let!(:dossier) { create :dossier, procedure: procedure }
|
|
|
|
|
2017-07-17 13:13:20 +02:00
|
|
|
subject { gestionnaire.follow?(dossier) }
|
2016-07-18 18:24:29 +02:00
|
|
|
|
|
|
|
context 'when gestionnaire follow a dossier' do
|
|
|
|
before do
|
|
|
|
create :follow, dossier_id: dossier.id, gestionnaire_id: gestionnaire.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when gestionnaire not follow a dossier' do
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-02 14:49:41 +02:00
|
|
|
describe '#build_default_preferences_list_dossier' do
|
|
|
|
subject { gestionnaire.preference_list_dossiers }
|
|
|
|
|
|
|
|
context 'when gestionnaire is created' do
|
|
|
|
it 'build default 5 pref list dossier object' do
|
|
|
|
expect(subject.size).to eq 5
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'build dossier_id column' do
|
|
|
|
expect(subject.first.table).to be_nil
|
|
|
|
expect(subject.first.attr).to eq 'id'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'build dossier state column' do
|
|
|
|
expect(subject[1].table).to be_nil
|
|
|
|
expect(subject[1].attr).to eq 'state'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'build procedure libelle column' do
|
|
|
|
expect(subject[2].table).to eq 'procedure'
|
|
|
|
expect(subject[2].attr).to eq 'libelle'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'build entreprise raison_sociale column' do
|
|
|
|
expect(subject[3].table).to eq 'entreprise'
|
|
|
|
expect(subject[3].attr).to eq 'raison_sociale'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'build entreprise raison_sociale column' do
|
|
|
|
expect(subject.last.table).to eq 'etablissement'
|
|
|
|
expect(subject.last.attr).to eq 'siret'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-10-11 15:37:16 +02:00
|
|
|
|
|
|
|
describe '#build_default_preferences_smart_listing_page' do
|
|
|
|
subject { gestionnaire.preference_smart_listing_page }
|
|
|
|
|
|
|
|
context 'when gestionnaire is created' do
|
|
|
|
it 'build page column' do
|
|
|
|
expect(subject.page).to eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'build liste column' do
|
|
|
|
expect(subject.liste).to eq 'a_traiter'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'build procedure_id column' do
|
|
|
|
expect(subject.procedure).to eq nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'build gestionnaire column' do
|
|
|
|
expect(subject.gestionnaire).to eq gestionnaire
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-10-11 10:31:32 +02:00
|
|
|
|
2016-10-18 15:49:04 +02:00
|
|
|
context 'unified login' do
|
|
|
|
it 'syncs credentials to associated user' do
|
|
|
|
gestionnaire = create(:gestionnaire)
|
|
|
|
user = create(:user, email: gestionnaire.email)
|
2016-10-11 10:31:32 +02:00
|
|
|
|
2016-10-18 15:49:04 +02:00
|
|
|
gestionnaire.update_attributes(email: 'whoami@plop.com', password: 'super secret')
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.email).to eq('whoami@plop.com')
|
|
|
|
expect(user.valid_password?('super secret')).to be(true)
|
|
|
|
end
|
2016-12-07 17:24:01 +01:00
|
|
|
|
|
|
|
it 'syncs credentials to associated administrateur' do
|
|
|
|
gestionnaire = create(:gestionnaire)
|
|
|
|
admin = create(:administrateur, email: gestionnaire.email)
|
|
|
|
|
|
|
|
gestionnaire.update_attributes(email: 'whoami@plop.com', password: 'super secret')
|
|
|
|
|
|
|
|
admin.reload
|
|
|
|
expect(admin.email).to eq('whoami@plop.com')
|
|
|
|
expect(admin.valid_password?('super secret')).to be(true)
|
|
|
|
end
|
2016-10-11 10:31:32 +02:00
|
|
|
end
|
2016-12-26 17:25:59 +01:00
|
|
|
|
2016-12-27 10:57:21 +01:00
|
|
|
describe '#notifications_for' do
|
|
|
|
subject { gestionnaire.notifications_for procedure }
|
2016-12-26 17:25:59 +01:00
|
|
|
|
|
|
|
context 'when gestionnaire follow any dossier' do
|
|
|
|
it { is_expected.to eq 0 }
|
|
|
|
it { expect(gestionnaire.follows.count).to eq 0 }
|
|
|
|
it { expect_any_instance_of(Dossier::ActiveRecord_AssociationRelation).not_to receive(:inject)
|
2017-06-12 14:10:49 +02:00
|
|
|
subject
|
|
|
|
}
|
2016-12-26 17:25:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when gestionnaire follow any dossier into the procedure past in params' do
|
|
|
|
before do
|
|
|
|
create :follow, gestionnaire: gestionnaire, dossier: create(:dossier, procedure: procedure_2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq 0 }
|
|
|
|
it { expect(gestionnaire.follows.count).to eq 1 }
|
|
|
|
it { expect_any_instance_of(Dossier::ActiveRecord_AssociationRelation).not_to receive(:inject)
|
2017-06-12 14:10:49 +02:00
|
|
|
subject
|
|
|
|
}
|
2016-12-26 17:25:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when gestionnaire follow a dossier with a notification into the procedure past in params' do
|
|
|
|
let(:dossier) { create(:dossier, procedure: procedure, state: 'initiated') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create :follow, gestionnaire: gestionnaire, dossier: dossier
|
|
|
|
create :notification, dossier: dossier
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq 1 }
|
|
|
|
it { expect(gestionnaire.follows.count).to eq 1 }
|
|
|
|
it { expect_any_instance_of(Dossier::ActiveRecord_AssociationRelation).to receive(:inject)
|
2017-06-12 14:10:49 +02:00
|
|
|
subject
|
|
|
|
}
|
2016-12-26 17:25:59 +01:00
|
|
|
end
|
|
|
|
end
|
2017-01-05 12:44:15 +01:00
|
|
|
|
|
|
|
describe '#procedure_filter' do
|
|
|
|
subject { gestionnaire.procedure_filter }
|
|
|
|
|
|
|
|
context 'when procedure_filter_id is nil' do
|
|
|
|
it { is_expected.to eq nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when procedure_filter is not nil' do
|
|
|
|
context 'when gestionnaire is assign_to the procedure filter id' do
|
|
|
|
before do
|
|
|
|
gestionnaire.update_column :procedure_filter, procedure.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(AssignTo.where(gestionnaire: gestionnaire, procedure: procedure).count).to eq 1 }
|
|
|
|
it { is_expected.to eq procedure_assign.procedure.id }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when gestionnaire is not any more assign to the procedure filter id' do
|
|
|
|
before do
|
|
|
|
gestionnaire.update_column :procedure_filter, procedure_3.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(AssignTo.where(gestionnaire: gestionnaire, procedure: procedure_3).count).to eq 0 }
|
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
2017-06-29 11:45:30 +02:00
|
|
|
|
|
|
|
context "when procedure is hidden clear procedure_filter" do
|
|
|
|
before do
|
|
|
|
gestionnaire.update_column :procedure_filter, procedure_3.id
|
|
|
|
procedure_3.hide!
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
2017-01-05 12:44:15 +01:00
|
|
|
end
|
2017-04-26 11:28:58 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#dossiers_with_notifications_count' do
|
|
|
|
subject { gestionnaire.dossiers_with_notifications_count }
|
|
|
|
|
|
|
|
context 'when there is no notifications' do
|
|
|
|
it { is_expected.to eq(0) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is one notification for one dossier' do
|
|
|
|
let(:notification){ create(:notification, already_read: false) }
|
|
|
|
let!(:follow){ create(:follow, dossier: notification.dossier, gestionnaire: gestionnaire) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(1) }
|
|
|
|
end
|
|
|
|
|
2017-05-10 18:10:43 +02:00
|
|
|
context 'when there is one notification read' do
|
|
|
|
let(:notification){ create(:notification, already_read: true) }
|
|
|
|
let!(:follow){ create(:follow, dossier: notification.dossier, gestionnaire: gestionnaire) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(0) }
|
|
|
|
end
|
|
|
|
|
2017-04-26 11:28:58 +02:00
|
|
|
context 'when there are many notifications for one dossier' do
|
|
|
|
let(:notification){ create(:notification, already_read: false) }
|
|
|
|
let(:notification2){ create(:notification, already_read: false, dossier: notification.dossier) }
|
|
|
|
let!(:follow){ create(:follow, dossier: notification.dossier, gestionnaire: gestionnaire) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are many notifications for many dossiers' do
|
|
|
|
let(:notification){ create(:notification, already_read: false) }
|
|
|
|
let(:notification2){ create(:notification, already_read: false) }
|
|
|
|
let!(:follow){ create(:follow, dossier: notification.dossier, gestionnaire: gestionnaire) }
|
|
|
|
let!(:follow2){ create(:follow, dossier: notification2.dossier, gestionnaire: gestionnaire) }
|
2017-01-05 12:44:15 +01:00
|
|
|
|
2017-04-26 11:28:58 +02:00
|
|
|
it { is_expected.to eq(2) }
|
|
|
|
end
|
2017-01-05 12:44:15 +01:00
|
|
|
end
|
2017-05-11 10:15:18 +02:00
|
|
|
|
2017-05-12 11:35:01 +02:00
|
|
|
describe '#dossiers_with_notifications_count_for_procedure' do
|
|
|
|
subject { gestionnaire.dossiers_with_notifications_count_for_procedure(procedure) }
|
2017-05-11 10:15:18 +02:00
|
|
|
|
|
|
|
context 'without notifications' do
|
|
|
|
it { is_expected.to eq(0) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a followed dossier' do
|
|
|
|
let!(:dossier){create(:dossier, procedure: procedure, state: 'received')}
|
|
|
|
let!(:follow){ create(:follow, dossier: dossier, gestionnaire: gestionnaire) }
|
|
|
|
|
|
|
|
context 'with 1 notification' do
|
|
|
|
let!(:notification){ create(:notification, already_read: false, dossier: dossier) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with 1 read notification' do
|
|
|
|
let!(:notification){ create(:notification, already_read: true, dossier: dossier) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(0) }
|
|
|
|
end
|
|
|
|
|
2017-05-12 12:12:57 +02:00
|
|
|
context 'with 2 notifications' do
|
2017-05-11 10:15:18 +02:00
|
|
|
let!(:notification){ create(:notification, already_read: false, dossier: dossier) }
|
|
|
|
let!(:notification2){ create(:notification, already_read: false, dossier: dossier) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(1) }
|
|
|
|
end
|
2017-05-12 12:12:57 +02:00
|
|
|
|
|
|
|
context 'with another dossier' do
|
|
|
|
let!(:dossier2){create(:dossier, procedure: procedure, state: 'received')}
|
|
|
|
let!(:follow2){ create(:follow, dossier: dossier2, gestionnaire: gestionnaire) }
|
|
|
|
|
|
|
|
context 'and some notifications' do
|
|
|
|
let!(:notification){ create(:notification, already_read: false, dossier: dossier) }
|
|
|
|
let!(:notification2){ create(:notification, already_read: false, dossier: dossier) }
|
|
|
|
let!(:notification3){ create(:notification, already_read: false, dossier: dossier) }
|
|
|
|
|
|
|
|
let!(:notification4){ create(:notification, already_read: false, dossier: dossier2) }
|
|
|
|
let!(:notification5){ create(:notification, already_read: false, dossier: dossier2) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(2) }
|
|
|
|
end
|
|
|
|
end
|
2017-05-11 10:15:18 +02:00
|
|
|
end
|
|
|
|
end
|
2017-05-12 16:47:18 +02:00
|
|
|
|
|
|
|
describe 'last_week_overview' do
|
|
|
|
let!(:gestionnaire2) { create(:gestionnaire) }
|
|
|
|
subject { gestionnaire2.last_week_overview }
|
|
|
|
let(:friday) { DateTime.new(2017, 5, 12) }
|
|
|
|
let(:monday) { DateTime.now.beginning_of_week }
|
|
|
|
|
|
|
|
before :each do
|
|
|
|
Timecop.freeze(friday)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no procedure published was active last week' do
|
2017-07-11 14:21:10 +02:00
|
|
|
let!(:procedure) { create(:procedure, gestionnaires: [gestionnaire2], libelle: 'procedure', published_at: Time.now) }
|
2017-05-12 16:47:18 +02:00
|
|
|
context 'when the gestionnaire has no notifications' do
|
|
|
|
it { is_expected.to eq(nil) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a procedure published was active' do
|
2017-07-11 14:21:10 +02:00
|
|
|
let!(:procedure) { create(:procedure, gestionnaires: [gestionnaire2], libelle: 'procedure', published_at: Time.now) }
|
2017-05-12 16:47:18 +02:00
|
|
|
let(:procedure_overview) { double('procedure_overview', 'had_some_activities?'.to_sym => true) }
|
|
|
|
|
|
|
|
before :each do
|
|
|
|
expect_any_instance_of(Procedure).to receive(:procedure_overview).and_return(procedure_overview)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(gestionnaire.last_week_overview[:procedure_overviews]).to match([procedure_overview]) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a procedure not published was active with no notifications' do
|
2017-07-11 14:21:10 +02:00
|
|
|
let!(:procedure) { create(:procedure, gestionnaires: [gestionnaire2], libelle: 'procedure', published_at: nil) }
|
2017-05-12 16:47:18 +02:00
|
|
|
let(:procedure_overview) { double('procedure_overview', 'had_some_activities?'.to_sym => true) }
|
|
|
|
|
|
|
|
before :each do
|
|
|
|
allow_any_instance_of(Procedure).to receive(:procedure_overview).and_return(procedure_overview)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(nil) }
|
|
|
|
end
|
|
|
|
end
|
2017-05-16 15:20:38 +02:00
|
|
|
|
|
|
|
describe '.can_view_dossier?' do
|
|
|
|
subject{ gestionnaire.can_view_dossier?(dossier.id) }
|
|
|
|
|
|
|
|
context 'when gestionnaire is assigned on dossier' do
|
|
|
|
let!(:dossier){ create(:dossier, procedure: procedure, state: 'received') }
|
|
|
|
|
|
|
|
it { expect(subject).to be true }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when gestionnaire is invited on dossier' do
|
|
|
|
let(:dossier){ create(:dossier) }
|
|
|
|
let!(:avis){ create(:avis, dossier: dossier, gestionnaire: gestionnaire) }
|
|
|
|
|
|
|
|
it { expect(subject).to be true }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when gestionnaire is neither assigned nor invited on dossier' do
|
|
|
|
let(:dossier){ create(:dossier) }
|
|
|
|
|
|
|
|
it { expect(subject).to be false }
|
|
|
|
end
|
|
|
|
end
|
2015-09-22 10:15:12 +02:00
|
|
|
end
|