Always create a corresponding User account for Admin accounts

This commit is contained in:
Frederic Merizen 2018-04-11 17:14:47 +02:00
parent f949a6c4a2
commit ee67e7b154
2 changed files with 22 additions and 2 deletions

View file

@ -8,15 +8,21 @@ class Administration < ApplicationRecord
end end
def invite_admin(email) def invite_admin(email)
password = SecureRandom.hex
administrateur = Administrateur.new({ administrateur = Administrateur.new({
email: email, email: email,
active: false active: false,
password: password,
password_confirmation: password
}) })
administrateur.password = administrateur.password_confirmation = SecureRandom.hex
if administrateur.save if administrateur.save
AdministrationMailer.new_admin_email(administrateur, self).deliver_now! AdministrationMailer.new_admin_email(administrateur, self).deliver_now!
administrateur.invite! administrateur.invite!
User.create({
email: email,
password: password
})
end end
administrateur administrateur

View file

@ -13,5 +13,19 @@ describe Administration, type: :model do
} }
it { expect(administration.invite_admin(nil).errors).not_to be_empty } it { expect(administration.invite_admin(nil).errors).not_to be_empty }
it { expect(administration.invite_admin('toto').errors).not_to be_empty } it { expect(administration.invite_admin('toto').errors).not_to be_empty }
it 'creates a corresponding user account for the email' do
subject
user = User.find_by(email: valid_email)
expect(user).to be_present
end
context 'when there already is a user account with the same email' do
before { create(:user, email: valid_email) }
it 'still creates an admin account' do
expect(subject.errors).to be_empty
expect(subject).to be_persisted
end
end
end end
end end