2019-11-05-01 (#4481)

2019-11-05-01
This commit is contained in:
Pierre de La Morinerie 2019-11-05 16:57:23 +01:00 committed by GitHub
commit 3a33b9fd40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 141 additions and 67 deletions

View file

@ -15,7 +15,7 @@ class Admin::InstructeursController < AdminController
procedure_id = params[:procedure_id]
if @instructeur.nil?
invite_instructeur(params[:instructeur][:email])
invite_instructeur(email)
else
assign_instructeur!
end

View file

@ -250,7 +250,7 @@ class ApplicationController < ActionController::Base
payload: {
DS_SIGN_IN_COUNT: current_user&.sign_in_count,
DS_CREATED_AT: current_administrateur&.created_at,
DS_ACTIVE: current_administrateur&.active?,
DS_ACTIVE: current_user&.active?,
DS_ID: current_administrateur&.id,
DS_GESTIONNAIRE_ID: current_instructeur&.id,
DS_ROLES: current_user_roles

View file

@ -4,14 +4,16 @@ class WebhookController < ActionController::Base
def helpscout
email = params[:customer][:email].downcase
user = User.find_by(email: email)
if user.nil?
head :not_found
else
instructeur = user.instructeur
administrateur = user.administrateur
html = []
if user
url = manager_user_url(user)
html << link_to_manager(user, url)
end
html = [link_to_manager(user, url)]
if instructeur
url = manager_instructeur_url(instructeur)
@ -23,9 +25,6 @@ class WebhookController < ActionController::Base
html << link_to_manager(administrateur, url)
end
if html.empty?
head :not_found
else
render json: { html: html.join('<br>') }
end
end

View file

@ -46,7 +46,7 @@ class Administrateur < ApplicationRecord
end
def registration_state
if active?
if user.active?
'Actif'
elsif user.reset_password_period_valid?
'En attente'
@ -56,17 +56,7 @@ class Administrateur < ApplicationRecord
end
def invitation_expired?
!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
!user.active? && !user.reset_password_period_valid?
end
def owns?(procedure)
@ -80,8 +70,4 @@ class Administrateur < ApplicationRecord
def can_be_deleted?
dossiers.state_instruction_commencee.none? && procedures.none?
end
def active?
user.last_sign_in_at.present?
end
end

View file

@ -47,14 +47,13 @@ class User < ApplicationRecord
end
def invite_administrateur!(administration_id)
if administrateur.active?
raise "Impossible d'inviter un utilisateur déjà actif !"
reset_password_token = nil
if !active?
reset_password_token = set_reset_password_token
end
reset_password_token = set_reset_password_token
AdministrationMailer.invite_admin(self, reset_password_token, administration_id).deliver_later
reset_password_token
end
def remind_invitation!
@ -93,6 +92,10 @@ class User < ApplicationRecord
"User:#{id}"
end
def active?
last_sign_in_at.present?
end
private
def link_invites!

View file

@ -29,7 +29,7 @@ class AdministrateurUsageStatisticsService
result = {
ds_sign_in_count: administrateur.user.sign_in_count,
ds_created_at: administrateur.created_at,
ds_active: administrateur.active?,
ds_active: administrateur.user.active?,
ds_id: administrateur.id,
nb_services: nb_services_by_administrateur_id[administrateur.id],
nb_instructeurs: nb_instructeurs_by_administrateur_id[administrateur.id],

View file

@ -9,10 +9,15 @@
%p
Votre compte administrateur a été créé pour l'adresse email #{@admin.email}.
- if @reset_password_token.present?
%p
%b
Pour lactiver, 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
= render partial: "layouts/mailers/bizdev_signature", locals: { author_name: @author_name }

View 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

View file

@ -12,7 +12,7 @@ feature 'As an administrateur', js: true do
end
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)
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(new_admin.reload.active?).to be(true)
expect(new_admin.reload.user.active?).to be(true)
end
end

View file

@ -11,6 +11,10 @@ class AdministrationMailerPreview < ActionMailer::Preview
AdministrationMailer.invite_admin(administrateur, "12345678", 0)
end
def invite_admin_whose_already_has_an_account
AdministrationMailer.invite_admin(administrateur, nil, 0)
end
def refuse_admin
AdministrationMailer.refuse_admin('bad_admin@pipo.com')
end

View file

@ -50,22 +50,4 @@ describe Administrateur, type: :model do
# it { expect(subject).to eq([]) }
# 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

View file

@ -164,4 +164,49 @@ describe User, type: :model do
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