Always create a corresponding User account for Admin accounts
This commit is contained in:
parent
f949a6c4a2
commit
ee67e7b154
2 changed files with 22 additions and 2 deletions
|
@ -8,15 +8,21 @@ class Administration < ApplicationRecord
|
|||
end
|
||||
|
||||
def invite_admin(email)
|
||||
password = SecureRandom.hex
|
||||
administrateur = Administrateur.new({
|
||||
email: email,
|
||||
active: false
|
||||
active: false,
|
||||
password: password,
|
||||
password_confirmation: password
|
||||
})
|
||||
administrateur.password = administrateur.password_confirmation = SecureRandom.hex
|
||||
|
||||
if administrateur.save
|
||||
AdministrationMailer.new_admin_email(administrateur, self).deliver_now!
|
||||
administrateur.invite!
|
||||
User.create({
|
||||
email: email,
|
||||
password: password
|
||||
})
|
||||
end
|
||||
|
||||
administrateur
|
||||
|
|
|
@ -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('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
|
||||
|
|
Loading…
Reference in a new issue