2016-11-14 18:36:09 +01:00
|
|
|
require 'spec_helper'
|
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' }
|
|
|
|
let!(:invite) { create(:invite, email: email) }
|
|
|
|
let!(:invite2) { create(:invite, email: email) }
|
|
|
|
let(:user) do
|
|
|
|
create(:user,
|
|
|
|
email: email,
|
|
|
|
password: 'a good password',
|
|
|
|
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) }
|
|
|
|
let(:invite_gestionnaire) { create(:user) }
|
|
|
|
|
|
|
|
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) }
|
|
|
|
let(:invite_gestionnaire) { create(:user) }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-10-18 15:49:04 +02:00
|
|
|
context 'unified login' do
|
|
|
|
it 'syncs credentials to associated gestionnaire' do
|
|
|
|
user = create(:user)
|
|
|
|
gestionnaire = create(:gestionnaire, email: user.email)
|
2016-10-11 10:31:32 +02:00
|
|
|
|
2018-03-02 16:27:03 +01:00
|
|
|
user.update(email: 'whoami@plop.com', password: 'super secret')
|
2019-07-04 16:49:33 +02:00
|
|
|
user.confirm
|
2016-10-18 15:49:04 +02:00
|
|
|
|
|
|
|
gestionnaire.reload
|
|
|
|
expect(gestionnaire.email).to eq('whoami@plop.com')
|
|
|
|
expect(gestionnaire.valid_password?('super secret')).to be(true)
|
|
|
|
end
|
2016-12-07 17:24:01 +01:00
|
|
|
|
|
|
|
it 'syncs credentials to associated administrateur' do
|
|
|
|
user = create(:user)
|
|
|
|
admin = create(:administrateur, email: user.email)
|
|
|
|
|
2018-03-02 16:27:03 +01:00
|
|
|
user.update(email: 'whoami@plop.com', password: 'super secret')
|
2019-07-04 16:49:33 +02:00
|
|
|
user.confirm
|
2016-12-07 17:24:01 +01:00
|
|
|
|
|
|
|
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
|
2015-09-23 10:02:01 +02:00
|
|
|
end
|