commit
3a33b9fd40
12 changed files with 141 additions and 67 deletions
|
@ -15,7 +15,7 @@ class Admin::InstructeursController < AdminController
|
||||||
procedure_id = params[:procedure_id]
|
procedure_id = params[:procedure_id]
|
||||||
|
|
||||||
if @instructeur.nil?
|
if @instructeur.nil?
|
||||||
invite_instructeur(params[:instructeur][:email])
|
invite_instructeur(email)
|
||||||
else
|
else
|
||||||
assign_instructeur!
|
assign_instructeur!
|
||||||
end
|
end
|
||||||
|
|
|
@ -250,7 +250,7 @@ class ApplicationController < ActionController::Base
|
||||||
payload: {
|
payload: {
|
||||||
DS_SIGN_IN_COUNT: current_user&.sign_in_count,
|
DS_SIGN_IN_COUNT: current_user&.sign_in_count,
|
||||||
DS_CREATED_AT: current_administrateur&.created_at,
|
DS_CREATED_AT: current_administrateur&.created_at,
|
||||||
DS_ACTIVE: current_administrateur&.active?,
|
DS_ACTIVE: current_user&.active?,
|
||||||
DS_ID: current_administrateur&.id,
|
DS_ID: current_administrateur&.id,
|
||||||
DS_GESTIONNAIRE_ID: current_instructeur&.id,
|
DS_GESTIONNAIRE_ID: current_instructeur&.id,
|
||||||
DS_ROLES: current_user_roles
|
DS_ROLES: current_user_roles
|
||||||
|
|
|
@ -4,28 +4,27 @@ class WebhookController < ActionController::Base
|
||||||
def helpscout
|
def helpscout
|
||||||
email = params[:customer][:email].downcase
|
email = params[:customer][:email].downcase
|
||||||
user = User.find_by(email: email)
|
user = User.find_by(email: email)
|
||||||
instructeur = user.instructeur
|
|
||||||
administrateur = user.administrateur
|
|
||||||
html = []
|
|
||||||
|
|
||||||
if user
|
if user.nil?
|
||||||
url = manager_user_url(user)
|
|
||||||
html << link_to_manager(user, url)
|
|
||||||
end
|
|
||||||
|
|
||||||
if instructeur
|
|
||||||
url = manager_instructeur_url(instructeur)
|
|
||||||
html << link_to_manager(instructeur, url)
|
|
||||||
end
|
|
||||||
|
|
||||||
if administrateur
|
|
||||||
url = manager_administrateur_url(administrateur)
|
|
||||||
html << link_to_manager(administrateur, url)
|
|
||||||
end
|
|
||||||
|
|
||||||
if html.empty?
|
|
||||||
head :not_found
|
head :not_found
|
||||||
|
|
||||||
else
|
else
|
||||||
|
instructeur = user.instructeur
|
||||||
|
administrateur = user.administrateur
|
||||||
|
|
||||||
|
url = manager_user_url(user)
|
||||||
|
html = [link_to_manager(user, url)]
|
||||||
|
|
||||||
|
if instructeur
|
||||||
|
url = manager_instructeur_url(instructeur)
|
||||||
|
html << link_to_manager(instructeur, url)
|
||||||
|
end
|
||||||
|
|
||||||
|
if administrateur
|
||||||
|
url = manager_administrateur_url(administrateur)
|
||||||
|
html << link_to_manager(administrateur, url)
|
||||||
|
end
|
||||||
|
|
||||||
render json: { html: html.join('<br>') }
|
render json: { html: html.join('<br>') }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -46,7 +46,7 @@ class Administrateur < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def registration_state
|
def registration_state
|
||||||
if active?
|
if user.active?
|
||||||
'Actif'
|
'Actif'
|
||||||
elsif user.reset_password_period_valid?
|
elsif user.reset_password_period_valid?
|
||||||
'En attente'
|
'En attente'
|
||||||
|
@ -56,17 +56,7 @@ class Administrateur < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def invitation_expired?
|
def invitation_expired?
|
||||||
!active? && !user.reset_password_period_valid?
|
!user.active? && !user.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
|
|
||||||
})
|
|
||||||
|
|
||||||
administrateur
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def owns?(procedure)
|
def owns?(procedure)
|
||||||
|
@ -80,8 +70,4 @@ class Administrateur < ApplicationRecord
|
||||||
def can_be_deleted?
|
def can_be_deleted?
|
||||||
dossiers.state_instruction_commencee.none? && procedures.none?
|
dossiers.state_instruction_commencee.none? && procedures.none?
|
||||||
end
|
end
|
||||||
|
|
||||||
def active?
|
|
||||||
user.last_sign_in_at.present?
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -47,14 +47,13 @@ class User < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def invite_administrateur!(administration_id)
|
def invite_administrateur!(administration_id)
|
||||||
if administrateur.active?
|
reset_password_token = nil
|
||||||
raise "Impossible d'inviter un utilisateur déjà actif !"
|
|
||||||
|
if !active?
|
||||||
|
reset_password_token = set_reset_password_token
|
||||||
end
|
end
|
||||||
|
|
||||||
reset_password_token = set_reset_password_token
|
|
||||||
AdministrationMailer.invite_admin(self, reset_password_token, administration_id).deliver_later
|
AdministrationMailer.invite_admin(self, reset_password_token, administration_id).deliver_later
|
||||||
|
|
||||||
reset_password_token
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def remind_invitation!
|
def remind_invitation!
|
||||||
|
@ -93,6 +92,10 @@ class User < ApplicationRecord
|
||||||
"User:#{id}"
|
"User:#{id}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def active?
|
||||||
|
last_sign_in_at.present?
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def link_invites!
|
def link_invites!
|
||||||
|
|
|
@ -29,7 +29,7 @@ class AdministrateurUsageStatisticsService
|
||||||
result = {
|
result = {
|
||||||
ds_sign_in_count: administrateur.user.sign_in_count,
|
ds_sign_in_count: administrateur.user.sign_in_count,
|
||||||
ds_created_at: administrateur.created_at,
|
ds_created_at: administrateur.created_at,
|
||||||
ds_active: administrateur.active?,
|
ds_active: administrateur.user.active?,
|
||||||
ds_id: administrateur.id,
|
ds_id: administrateur.id,
|
||||||
nb_services: nb_services_by_administrateur_id[administrateur.id],
|
nb_services: nb_services_by_administrateur_id[administrateur.id],
|
||||||
nb_instructeurs: nb_instructeurs_by_administrateur_id[administrateur.id],
|
nb_instructeurs: nb_instructeurs_by_administrateur_id[administrateur.id],
|
||||||
|
|
|
@ -9,10 +9,15 @@
|
||||||
%p
|
%p
|
||||||
Votre compte administrateur a été créé pour l'adresse email #{@admin.email}.
|
Votre compte administrateur a été créé pour l'adresse email #{@admin.email}.
|
||||||
|
|
||||||
%p
|
- if @reset_password_token.present?
|
||||||
%b
|
%p
|
||||||
Pour l’activer, cliquez sur le lien suivant :
|
%b
|
||||||
= link_to(admin_activate_url(token: @reset_password_token), admin_activate_url(token: @reset_password_token))
|
Pour l’activer, cliquez sur le lien suivant :
|
||||||
|
= link_to(admin_activate_url(token: @reset_password_token), admin_activate_url(token: @reset_password_token))
|
||||||
|
- else
|
||||||
|
%p
|
||||||
|
Pour vous connecter, cliquez sur le lien suivant :
|
||||||
|
= link_to(new_user_session_url, new_user_session_url)
|
||||||
|
|
||||||
%p
|
%p
|
||||||
= render partial: "layouts/mailers/bizdev_signature", locals: { author_name: @author_name }
|
= render partial: "layouts/mailers/bizdev_signature", locals: { author_name: @author_name }
|
||||||
|
|
50
spec/controllers/webhook_controller_spec.rb
Normal file
50
spec/controllers/webhook_controller_spec.rb
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe WebhookController, type: :controller do
|
||||||
|
describe '#helpscout' do
|
||||||
|
before { allow(controller).to receive(:verify_signature!).and_return(true) }
|
||||||
|
|
||||||
|
subject(:response) { get :helpscout, params: { customer: { email: customer_email } } }
|
||||||
|
|
||||||
|
let(:payload) { JSON.parse(subject.body) }
|
||||||
|
|
||||||
|
context 'when there is no matching user' do
|
||||||
|
let(:customer_email) { 'not-a-user@exemple.fr' }
|
||||||
|
|
||||||
|
it 'returns an empty response' do
|
||||||
|
expect(subject.status).to eq(404)
|
||||||
|
expect(subject.body).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when there is a matching user' do
|
||||||
|
let(:user) { create(:user) }
|
||||||
|
let(:customer_email) { user.email }
|
||||||
|
|
||||||
|
it 'returns a 200 response' do
|
||||||
|
expect(subject.status).to eq(200)
|
||||||
|
expect(subject.body).to be_present
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns a link to the User profile in the Manager' do
|
||||||
|
expect(payload).to have_key('html')
|
||||||
|
expect(payload['html']).to have_selector("a[href='#{manager_user_url(user)}']")
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when there are an associated Instructeur and Administrateur' do
|
||||||
|
let!(:instructeur) { create(:instructeur, user: user) }
|
||||||
|
let!(:admin) { create(:administrateur, user: user) }
|
||||||
|
|
||||||
|
it 'returns a link to the Instructeur profile in the Manager' do
|
||||||
|
expect(payload).to have_key('html')
|
||||||
|
expect(payload['html']).to have_selector("a[href='#{manager_instructeur_url(instructeur)}']")
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns a link to the Administrateur profile in the Manager' do
|
||||||
|
expect(payload).to have_key('html')
|
||||||
|
expect(payload['html']).to have_selector("a[href='#{manager_administrateur_url(admin)}']")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -12,7 +12,7 @@ feature 'As an administrateur', js: true do
|
||||||
end
|
end
|
||||||
|
|
||||||
scenario 'I can register' do
|
scenario 'I can register' do
|
||||||
expect(new_admin.reload.active?).to be(false)
|
expect(new_admin.reload.user.active?).to be(false)
|
||||||
|
|
||||||
confirmation_email = open_email(admin_email)
|
confirmation_email = open_email(admin_email)
|
||||||
token_params = confirmation_email.body.match(/token=[^"]+/)
|
token_params = confirmation_email.body.match(/token=[^"]+/)
|
||||||
|
@ -24,6 +24,6 @@ feature 'As an administrateur', js: true do
|
||||||
|
|
||||||
expect(page).to have_content 'Mot de passe enregistré'
|
expect(page).to have_content 'Mot de passe enregistré'
|
||||||
|
|
||||||
expect(new_admin.reload.active?).to be(true)
|
expect(new_admin.reload.user.active?).to be(true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,6 +11,10 @@ class AdministrationMailerPreview < ActionMailer::Preview
|
||||||
AdministrationMailer.invite_admin(administrateur, "12345678", 0)
|
AdministrationMailer.invite_admin(administrateur, "12345678", 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def invite_admin_whose_already_has_an_account
|
||||||
|
AdministrationMailer.invite_admin(administrateur, nil, 0)
|
||||||
|
end
|
||||||
|
|
||||||
def refuse_admin
|
def refuse_admin
|
||||||
AdministrationMailer.refuse_admin('bad_admin@pipo.com')
|
AdministrationMailer.refuse_admin('bad_admin@pipo.com')
|
||||||
end
|
end
|
||||||
|
|
|
@ -50,22 +50,4 @@ describe Administrateur, type: :model do
|
||||||
# it { expect(subject).to eq([]) }
|
# it { expect(subject).to eq([]) }
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
|
||||||
describe '#active?' do
|
|
||||||
let!(:administrateur) { create(:administrateur) }
|
|
||||||
|
|
||||||
subject { administrateur.active? }
|
|
||||||
|
|
||||||
context 'when the user has never signed in' do
|
|
||||||
before { administrateur.user.update(last_sign_in_at: nil) }
|
|
||||||
|
|
||||||
it { is_expected.to be false }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when the user has already signed in' do
|
|
||||||
before { administrateur.user.update(last_sign_in_at: Time.zone.now) }
|
|
||||||
|
|
||||||
it { is_expected.to be true }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -164,4 +164,49 @@ describe User, type: :model do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'invite_administrateur!' do
|
||||||
|
let(:administration) { create(:administration) }
|
||||||
|
let(:administrateur) { create(:administrateur) }
|
||||||
|
let(:user) { administrateur.user }
|
||||||
|
|
||||||
|
let(:mailer_double) { double('mailer', deliver_later: true) }
|
||||||
|
|
||||||
|
before { allow(AdministrationMailer).to receive(:invite_admin).and_return(mailer_double) }
|
||||||
|
|
||||||
|
subject { user.invite_administrateur!(administration.id) }
|
||||||
|
|
||||||
|
context 'when the user is inactif' do
|
||||||
|
before { subject }
|
||||||
|
|
||||||
|
it { expect(AdministrationMailer).to have_received(:invite_admin).with(user, kind_of(String), administration.id) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the user is actif' do
|
||||||
|
before do
|
||||||
|
user.update(last_sign_in_at: Time.zone.now)
|
||||||
|
subject
|
||||||
|
end
|
||||||
|
|
||||||
|
it { expect(AdministrationMailer).to have_received(:invite_admin).with(user, nil, administration.id) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#active?' do
|
||||||
|
let!(:user) { create(:user) }
|
||||||
|
|
||||||
|
subject { user.active? }
|
||||||
|
|
||||||
|
context 'when the user has never signed in' do
|
||||||
|
before { user.update(last_sign_in_at: nil) }
|
||||||
|
|
||||||
|
it { is_expected.to be false }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the user has already signed in' do
|
||||||
|
before { user.update(last_sign_in_at: Time.zone.now) }
|
||||||
|
|
||||||
|
it { is_expected.to be true }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue