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 }
|
|
|
|
let(:gestionnaire) { create :gestionnaire, procedure_filter: procedure_filter, administrateurs: [admin] }
|
|
|
|
let(:procedure_filter) { [] }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create :assign_to, gestionnaire: gestionnaire, procedure: procedure
|
|
|
|
create :assign_to, gestionnaire: gestionnaire, procedure: procedure_2
|
|
|
|
end
|
|
|
|
|
2015-09-22 10:15:12 +02:00
|
|
|
describe 'database column' do
|
|
|
|
it { is_expected.to have_db_column(:email) }
|
|
|
|
it { is_expected.to have_db_column(:encrypted_password) }
|
|
|
|
it { is_expected.to have_db_column(:reset_password_token) }
|
|
|
|
it { is_expected.to have_db_column(:reset_password_sent_at) }
|
|
|
|
it { is_expected.to have_db_column(:remember_created_at) }
|
|
|
|
it { is_expected.to have_db_column(:sign_in_count) }
|
|
|
|
it { is_expected.to have_db_column(:current_sign_in_at) }
|
|
|
|
it { is_expected.to have_db_column(:last_sign_in_at) }
|
|
|
|
it { is_expected.to have_db_column(:current_sign_in_ip) }
|
|
|
|
it { is_expected.to have_db_column(:last_sign_in_ip) }
|
|
|
|
it { is_expected.to have_db_column(:created_at) }
|
|
|
|
it { is_expected.to have_db_column(:updated_at) }
|
|
|
|
end
|
2015-11-10 10:23:15 +01:00
|
|
|
|
|
|
|
describe 'association' do
|
2016-10-11 15:37:16 +02:00
|
|
|
it { is_expected.to have_one(:preference_smart_listing_page) }
|
2016-05-20 15:39:17 +02:00
|
|
|
it { is_expected.to have_and_belong_to_many(:administrateurs) }
|
2015-11-16 18:08:19 +01:00
|
|
|
it { is_expected.to have_many(:procedures) }
|
|
|
|
it { is_expected.to have_many(:dossiers) }
|
2016-07-18 18:24:29 +02:00
|
|
|
it { is_expected.to have_many(:follows) }
|
2016-08-01 18:10:32 +02:00
|
|
|
it { is_expected.to have_many(:preference_list_dossiers) }
|
2015-11-10 10:23:15 +01:00
|
|
|
end
|
2016-03-14 16:49:12 +01:00
|
|
|
|
2016-07-18 18:24:29 +02:00
|
|
|
describe '#toggle_follow_dossier' do
|
|
|
|
let!(:dossier) { create :dossier, procedure: procedure }
|
|
|
|
|
|
|
|
subject { gestionnaire.toggle_follow_dossier dossier_id }
|
|
|
|
|
|
|
|
context 'when dossier id not valid' do
|
|
|
|
let(:dossier_id) { 0 }
|
|
|
|
|
|
|
|
it { expect(subject).to eq nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when dossier id is valid' do
|
|
|
|
let(:dossier_id) { dossier.id }
|
|
|
|
|
|
|
|
context 'when dossier is not follow by gestionnaire' do
|
|
|
|
it 'value change in database' do
|
|
|
|
expect { subject }.to change(Follow, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject).to be_an_instance_of Follow }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when dossier is follow by gestionnaire' do
|
|
|
|
before do
|
|
|
|
create :follow, dossier_id: dossier.id, gestionnaire_id: gestionnaire.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'value change in database' do
|
|
|
|
expect { subject }.to change(Follow, :count).by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject).to eq 1 }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when dossier instance is past' do
|
|
|
|
let(:dossier_id) { dossier }
|
|
|
|
|
|
|
|
context 'when dossier is not follow by gestionnaire' do
|
|
|
|
it 'value change in database' do
|
|
|
|
expect { subject }.to change(Follow, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject).to be_an_instance_of Follow }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when dossier is follow by gestionnaire' do
|
|
|
|
before do
|
|
|
|
create :follow, dossier_id: dossier.id, gestionnaire_id: gestionnaire.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'value change in database' do
|
|
|
|
expect { subject }.to change(Follow, :count).by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(subject).to eq 1 }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#follow?' do
|
|
|
|
let!(:dossier) { create :dossier, procedure: procedure }
|
|
|
|
|
|
|
|
subject { gestionnaire.follow? dossier.id }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
describe '#dossiers_follow' do
|
2016-12-01 18:07:51 +01:00
|
|
|
let!(:dossier) { create :dossier, procedure: procedure, state: :initiated }
|
2016-07-18 18:24:29 +02:00
|
|
|
|
|
|
|
before do
|
|
|
|
create :follow, dossier_id: dossier.id, gestionnaire_id: gestionnaire.id
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { gestionnaire.dossiers_follow }
|
|
|
|
|
|
|
|
it { expect(Follow.all.size).to eq 1 }
|
|
|
|
it { expect(subject.first).to eq dossier }
|
|
|
|
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)
|
|
|
|
subject }
|
|
|
|
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)
|
|
|
|
subject }
|
|
|
|
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)
|
|
|
|
subject }
|
|
|
|
end
|
|
|
|
end
|
2015-09-22 10:15:12 +02:00
|
|
|
end
|