force password reset on admin promotion or creation

This commit is contained in:
clemkeirua 2020-09-17 15:35:40 +02:00
parent 6f5547cafa
commit de3118bc7b
4 changed files with 21 additions and 13 deletions

View file

@ -90,13 +90,7 @@ class User < ApplicationRecord
end
def invite_administrateur!(administration_id)
reset_password_token = nil
if !active?
reset_password_token = set_reset_password_token
end
AdministrationMailer.invite_admin(self, reset_password_token, administration_id).deliver_later
AdministrationMailer.invite_admin(self, set_reset_password_token, administration_id).deliver_later
end
def remind_invitation!

View file

@ -9,7 +9,7 @@
%p
Votre compte administrateur a été créé pour l'adresse email #{@admin.email}.
- if @reset_password_token.present?
- if !@admin.active?
%p
%b
Pour lactiver, cliquez sur le lien suivant :
@ -17,7 +17,7 @@
- else
%p
Pour vous connecter, cliquez sur le lien suivant :
= link_to(new_user_session_url, new_user_session_url)
= link_to edit_user_password_url(@admin, reset_password_token: @reset_password_token), edit_user_password_url(@admin, reset_password_token: @reset_password_token)
%p
= render partial: "layouts/mailers/bizdev_signature", locals: { author_name: @author_name }

View file

@ -1,12 +1,24 @@
RSpec.describe AdministrationMailer, type: :mailer do
describe '#invite_admin' do
let(:admin) { create(:administrateur) }
let(:token) { "Toc toc toc" }
let(:admin_user) { create(:user, last_sign_in_at: last_sign_in_at) }
let(:token) { "some_token" }
let(:administration_id) { BizDev::PIPEDRIVE_ID }
let(:last_sign_in_at) { nil }
subject { described_class.invite_admin(admin, token, administration_id) }
subject { described_class.invite_admin(admin_user, token, administration_id) }
it { expect(subject.subject).not_to be_empty }
describe "when the user has not been activated" do
it { expect(subject.body).to include(admin_activate_path(token: token)) }
it { expect(subject.body).not_to include(edit_user_password_url(admin_user, reset_password_token: token)) }
end
describe "when the user is already active" do
let(:last_sign_in_at) { Time.zone.now }
it { expect(subject.body).not_to include(admin_activate_path(token: token)) }
it { expect(subject.body).to include(edit_user_password_url(admin_user, reset_password_token: token)) }
end
end
describe '#refuse_admin' do

View file

@ -186,7 +186,9 @@ describe User, type: :model do
subject
end
it { expect(AdministrationMailer).to have_received(:invite_admin).with(user, nil, administration.id) }
it 'receives an invitation to update its password' do
expect(AdministrationMailer).to have_received(:invite_admin).with(user, kind_of(String), administration.id)
end
end
end