2015-09-23 10:02:01 +02:00
|
|
|
describe User, type: :model do
|
2018-09-19 11:56:05 +02:00
|
|
|
describe '#after_confirmation' do
|
|
|
|
let(:email) { 'mail@beta.gouv.fr' }
|
2019-06-20 00:36:50 +02:00
|
|
|
let!(:invite) { create(:invite, email: email) }
|
2018-09-19 11:56:05 +02:00
|
|
|
let!(:invite2) { create(:invite, email: email) }
|
|
|
|
let(:user) do
|
|
|
|
create(:user,
|
|
|
|
email: email,
|
2019-06-20 00:36:50 +02:00
|
|
|
password: 'démarches-simplifiées-pwd',
|
2018-09-19 11:56:05 +02:00
|
|
|
confirmation_token: '123',
|
|
|
|
confirmed_at: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'when confirming a user, it links the pending invitations to this user' do
|
|
|
|
expect(user.invites.size).to eq(0)
|
|
|
|
user.confirm
|
|
|
|
expect(user.reload.invites.size).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-30 18:26:23 +02:00
|
|
|
describe '#owns?' do
|
|
|
|
let(:owner) { create(:user) }
|
|
|
|
let(:dossier) { create(:dossier, user: owner) }
|
|
|
|
let(:invite_user) { create(:user) }
|
2019-08-06 11:02:54 +02:00
|
|
|
let(:invite_instructeur) { create(:user) }
|
2018-05-30 18:26:23 +02:00
|
|
|
|
|
|
|
subject { user.owns?(dossier) }
|
|
|
|
|
|
|
|
context 'when user is owner' do
|
|
|
|
let(:user) { owner }
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user was invited by user' do
|
|
|
|
before do
|
2018-10-10 09:23:08 +02:00
|
|
|
create(:invite, dossier: dossier, user: invite_user)
|
2018-05-30 18:26:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:user) { invite_user }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is quidam' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-22 17:36:36 +01:00
|
|
|
describe '#invite?' do
|
|
|
|
let(:dossier) { create :dossier }
|
|
|
|
let(:user) { dossier.user }
|
|
|
|
|
|
|
|
subject { user.invite? dossier.id }
|
|
|
|
|
|
|
|
context 'when user is invite at the dossier' do
|
|
|
|
before do
|
|
|
|
create :invite, dossier_id: dossier.id, user: user
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not invite at the dossier' do
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
2016-10-11 10:31:32 +02:00
|
|
|
|
2018-05-30 18:31:02 +02:00
|
|
|
describe '#owns_or_invite?' do
|
|
|
|
let(:owner) { create(:user) }
|
|
|
|
let(:dossier) { create(:dossier, user: owner) }
|
|
|
|
let(:invite_user) { create(:user) }
|
2019-08-06 11:02:54 +02:00
|
|
|
let(:invite_instructeur) { create(:user) }
|
2018-05-30 18:31:02 +02:00
|
|
|
|
|
|
|
subject { user.owns_or_invite?(dossier) }
|
|
|
|
|
|
|
|
context 'when user is owner' do
|
|
|
|
let(:user) { owner }
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user was invited by user' do
|
|
|
|
before do
|
2018-10-10 09:23:08 +02:00
|
|
|
create(:invite, dossier: dossier, user: invite_user)
|
2018-05-30 18:31:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:user) { invite_user }
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is quidam' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
2019-08-16 18:15:05 +02:00
|
|
|
|
|
|
|
describe '.create_or_promote_to_instructeur' do
|
|
|
|
let(:email) { 'inst1@gmail.com' }
|
|
|
|
let(:password) { 'un super password !' }
|
|
|
|
let(:admins) { [] }
|
|
|
|
|
|
|
|
subject { User.create_or_promote_to_instructeur(email, password, administrateurs: admins) }
|
|
|
|
|
|
|
|
context 'without an existing user' do
|
|
|
|
it do
|
|
|
|
user = subject
|
|
|
|
expect(user.valid_password?(password)).to be true
|
|
|
|
expect(user.confirmed_at).to be_present
|
|
|
|
expect(user.instructeur).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an administrateur' do
|
|
|
|
let(:admins) { [create(:administrateur)] }
|
|
|
|
|
|
|
|
it do
|
|
|
|
user = subject
|
|
|
|
expect(user.instructeur.administrateurs).to eq(admins)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an existing user' do
|
|
|
|
before { create(:user, email: email, password: 'démarches-simplifiées-pwd') }
|
|
|
|
|
|
|
|
it 'keeps the previous password' do
|
|
|
|
user = subject
|
|
|
|
expect(user.valid_password?('démarches-simplifiées-pwd')).to be true
|
|
|
|
expect(user.instructeur).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an existing instructeur' do
|
|
|
|
let(:old_admins) { [create(:administrateur)] }
|
|
|
|
let(:admins) { [create(:administrateur)] }
|
2019-10-16 14:15:47 +02:00
|
|
|
let!(:instructeur) { create(:instructeur, email: 'i@mail.com', administrateurs: old_admins) }
|
2019-08-16 18:15:05 +02:00
|
|
|
|
|
|
|
before do
|
|
|
|
User
|
|
|
|
.find_by(email: email)
|
|
|
|
.update!(instructeur: instructeur)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'keeps the existing instructeurs and adds administrateur' do
|
|
|
|
user = subject
|
|
|
|
expect(user.instructeur).to eq(instructeur)
|
2019-11-18 17:26:28 +01:00
|
|
|
expect(user.instructeur.administrateurs).to match_array(old_admins + admins)
|
2019-08-16 18:15:05 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an invalid email' do
|
|
|
|
let(:email) { 'invalid' }
|
|
|
|
|
|
|
|
it 'does not build an instructeur' do
|
|
|
|
user = subject
|
|
|
|
expect(user.valid?).to be false
|
|
|
|
expect(user.instructeur).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-11-05 09:12:14 +01:00
|
|
|
|
|
|
|
describe 'invite_administrateur!' do
|
|
|
|
let(:administration) { create(:administration) }
|
|
|
|
let(:administrateur) { create(:administrateur) }
|
|
|
|
let(:user) { administrateur.user }
|
|
|
|
|
2019-11-05 09:52:25 +01:00
|
|
|
let(:mailer_double) { double('mailer', deliver_later: true) }
|
|
|
|
|
|
|
|
before { allow(AdministrationMailer).to receive(:invite_admin).and_return(mailer_double) }
|
|
|
|
|
2019-11-05 09:12:14 +01:00
|
|
|
subject { user.invite_administrateur!(administration.id) }
|
|
|
|
|
|
|
|
context 'when the user is inactif' do
|
2019-11-05 09:52:25 +01:00
|
|
|
before { subject }
|
2019-11-05 09:12:14 +01:00
|
|
|
|
|
|
|
it { expect(AdministrationMailer).to have_received(:invite_admin).with(user, kind_of(String), administration.id) }
|
|
|
|
end
|
2019-11-05 09:37:53 +01:00
|
|
|
|
|
|
|
context 'when the user is actif' do
|
|
|
|
before do
|
|
|
|
user.update(last_sign_in_at: Time.zone.now)
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(AdministrationMailer).to have_received(:invite_admin).with(user, nil, administration.id) }
|
|
|
|
end
|
2019-11-05 09:12:14 +01:00
|
|
|
end
|
2019-11-05 10:01:07 +01:00
|
|
|
|
|
|
|
describe '#active?' do
|
|
|
|
let!(:user) { create(:user) }
|
|
|
|
|
|
|
|
subject { user.active? }
|
|
|
|
|
|
|
|
context 'when the user has never signed in' do
|
|
|
|
before { user.update(last_sign_in_at: nil) }
|
|
|
|
|
|
|
|
it { is_expected.to be false }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user has already signed in' do
|
|
|
|
before { user.update(last_sign_in_at: Time.zone.now) }
|
|
|
|
|
|
|
|
it { is_expected.to be true }
|
|
|
|
end
|
|
|
|
end
|
2020-01-06 17:33:09 +01:00
|
|
|
|
|
|
|
describe '#can_be_deleted?' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
subject { user.can_be_deleted? }
|
|
|
|
|
|
|
|
context 'when the user has a dossier in instruction' do
|
|
|
|
let!(:dossier) { create(:dossier, :en_instruction, user: user) }
|
|
|
|
|
|
|
|
it { is_expected.to be false }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user has no dossier in instruction' do
|
|
|
|
it { is_expected.to be true }
|
|
|
|
end
|
2020-01-30 10:48:28 +01:00
|
|
|
|
|
|
|
context 'when the user is an administrateur' do
|
|
|
|
it 'cannot be deleted' do
|
|
|
|
administrateur = create(:administrateur)
|
|
|
|
user = administrateur.user
|
|
|
|
|
|
|
|
expect(user.can_be_deleted?).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user is an instructeur' do
|
|
|
|
it 'cannot be deleted' do
|
|
|
|
instructeur = create(:instructeur)
|
|
|
|
user = instructeur.user
|
|
|
|
|
|
|
|
expect(user.can_be_deleted?).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
2020-01-06 17:33:09 +01:00
|
|
|
end
|
2020-01-08 10:50:16 +01:00
|
|
|
|
|
|
|
describe '#delete_and_keep_track_dossiers' do
|
|
|
|
let(:administration) { create(:administration) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2020-01-08 17:05:04 +01:00
|
|
|
context 'with a dossier in instruction' do
|
2020-01-08 10:50:16 +01:00
|
|
|
let!(:dossier_en_instruction) { create(:dossier, :en_instruction, user: user) }
|
2020-01-08 17:00:00 +01:00
|
|
|
it 'raises' do
|
2020-03-25 16:56:54 +01:00
|
|
|
expect { user.delete_and_keep_track_dossiers(administration) }.to raise_error(RuntimeError)
|
2020-01-08 10:50:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-08 17:05:04 +01:00
|
|
|
context 'without a dossier in instruction' do
|
2020-01-08 10:50:16 +01:00
|
|
|
let!(:dossier_en_construction) { create(:dossier, :en_construction, user: user) }
|
|
|
|
let!(:dossier_brouillon) { create(:dossier, user: user) }
|
|
|
|
|
2020-02-05 16:09:03 +01:00
|
|
|
context 'without a discarded dossier' do
|
2020-01-30 10:48:28 +01:00
|
|
|
it "keep track of dossiers and delete user" do
|
|
|
|
user.delete_and_keep_track_dossiers(administration)
|
2020-01-08 10:50:16 +01:00
|
|
|
|
2020-01-30 10:48:28 +01:00
|
|
|
expect(DeletedDossier.find_by(dossier_id: dossier_en_construction)).to be_present
|
2020-03-19 11:53:25 +01:00
|
|
|
expect(DeletedDossier.find_by(dossier_id: dossier_brouillon)).to be_nil
|
2020-01-30 10:48:28 +01:00
|
|
|
expect(User.find_by(id: user.id)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-05 16:09:03 +01:00
|
|
|
context 'with a discarded dossier' do
|
2020-01-30 10:48:28 +01:00
|
|
|
let!(:dossier_cache) do
|
|
|
|
create(:dossier, :en_construction, user: user)
|
|
|
|
end
|
2020-01-30 11:18:01 +01:00
|
|
|
let!(:dossier_from_another_user) do
|
|
|
|
create(:dossier, :en_construction, user: create(:user))
|
|
|
|
end
|
2020-01-30 10:48:28 +01:00
|
|
|
|
|
|
|
it "keep track of dossiers and delete user" do
|
2020-03-25 18:08:32 +01:00
|
|
|
dossier_cache.discard_and_keep_track!(administration, :user_request)
|
2020-01-30 10:48:28 +01:00
|
|
|
user.delete_and_keep_track_dossiers(administration)
|
|
|
|
|
|
|
|
expect(DeletedDossier.find_by(dossier_id: dossier_en_construction)).to be_present
|
2020-03-19 11:53:25 +01:00
|
|
|
expect(DeletedDossier.find_by(dossier_id: dossier_brouillon)).to be_nil
|
2020-01-30 10:48:28 +01:00
|
|
|
expect(User.find_by(id: user.id)).to be_nil
|
|
|
|
end
|
2020-01-30 11:18:01 +01:00
|
|
|
|
|
|
|
it "doesn't destroy dossiers of another user" do
|
2020-03-25 18:08:32 +01:00
|
|
|
dossier_cache.discard_and_keep_track!(administration, :user_request)
|
2020-01-30 11:18:01 +01:00
|
|
|
user.delete_and_keep_track_dossiers(administration)
|
|
|
|
|
2020-02-03 16:14:35 +01:00
|
|
|
expect(Dossier.find_by(id: dossier_from_another_user.id)).to be_present
|
2020-01-30 11:18:01 +01:00
|
|
|
end
|
2020-01-08 10:50:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-09-23 10:02:01 +02:00
|
|
|
end
|