Add invitation related methods to administrateur model

This commit is contained in:
Paul Chavard 2018-01-11 14:17:50 +01:00
parent 5a984a3637
commit 0cf40f4f18
4 changed files with 98 additions and 0 deletions

View file

@ -9,6 +9,16 @@ class Administrateur < ActiveRecord::Base
include CredentialsSyncableConcern
scope :inactive, -> { where(active: false) }
def self.find_inactive_by_token(reset_password_token)
self.inactive.with_reset_password_token(reset_password_token)
end
def self.find_inactive_by_id(id)
self.inactive.find(id)
end
def ensure_api_token
if api_token.nil?
self.api_token = generate_api_token
@ -19,6 +29,46 @@ class Administrateur < ActiveRecord::Base
update_attributes(api_token: generate_api_token)
end
def registration_state
if active?
'Actif'
elsif reset_password_period_valid?
'En attente'
else
'Expiré'
end
end
def invite!
if active?
raise "Impossible d'inviter un utilisateur déjà actif !"
end
reset_password_token = set_reset_password_token
AdministrationMailer.invite_admin(self, reset_password_token).deliver_now!
reset_password_token
end
def invitation_expired?
!active && !reset_password_period_valid?
end
def self.reset_password(reset_password_token, password)
administrateur = self.reset_password_by_token({
password: password,
password_confirmation: password,
reset_password_token: reset_password_token
})
if administrateur && administrateur.errors.empty?
administrateur.update_column(:active, true)
end
administrateur
end
private
def generate_api_token

View file

@ -6,4 +6,19 @@ class Administration < ActiveRecord::Base
def self.from_omniauth(params)
find_by(email: params["info"]["email"])
end
def invite_admin(email)
administrateur = Administrateur.new({
email: email,
active: false
})
administrateur.password = administrateur.password_confirmation = SecureRandom.hex
if administrateur.save
AdministrationMailer.new_admin_email(administrateur, self).deliver_now!
administrateur.invite!
end
administrateur
end
end

View file

@ -50,4 +50,20 @@ describe Administrateur, type: :model do
expect(gestionnaire.valid_password?('super secret')).to be(true)
end
end
describe '#find_inactive_by_token' do
let(:administrateur) { create(:administration).invite_admin('paul@tps.fr') }
let(:reset_password_token) { administrateur.invite! }
it { expect(Administrateur.find_inactive_by_token(reset_password_token)).not_to be_nil }
end
describe '#reset_password' do
let(:administrateur) { create(:administration).invite_admin('paul@tps.fr') }
let(:reset_password_token) { administrateur.invite! }
it { expect(Administrateur.reset_password(reset_password_token, '12345678').errors).to be_empty }
it { expect(Administrateur.reset_password('123', '12345678').errors).not_to be_empty }
it { expect(Administrateur.reset_password(reset_password_token, '').errors).not_to be_empty }
end
end

View file

@ -0,0 +1,17 @@
require 'spec_helper'
describe Administration, type: :model do
describe '#invite_admin' do
let(:administration) { create :administration }
let(:valid_email) { 'paul@tps.fr' }
subject { administration.invite_admin(valid_email) }
it {
expect(subject.errors).to be_empty
expect(subject).to be_persisted
expect(administration.invite_admin(valid_email).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 }
end
end