diff --git a/app/controllers/admin/instructeurs_controller.rb b/app/controllers/admin/instructeurs_controller.rb index fd35b15b5..5a179eac5 100644 --- a/app/controllers/admin/instructeurs_controller.rb +++ b/app/controllers/admin/instructeurs_controller.rb @@ -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 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 03c8bcd57..1ee6f1411 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/webhook_controller.rb b/app/controllers/webhook_controller.rb index 970b07f27..5382d065f 100644 --- a/app/controllers/webhook_controller.rb +++ b/app/controllers/webhook_controller.rb @@ -4,28 +4,27 @@ class WebhookController < ActionController::Base def helpscout email = params[:customer][:email].downcase user = User.find_by(email: email) - instructeur = user.instructeur - administrateur = user.administrateur - html = [] - if user - 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? + if user.nil? head :not_found + 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('
') } end end diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index a874fbe27..d097ebd74 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index a94d9212d..41a4984b3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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! diff --git a/app/services/administrateur_usage_statistics_service.rb b/app/services/administrateur_usage_statistics_service.rb index b9b38f868..5c85f2a96 100644 --- a/app/services/administrateur_usage_statistics_service.rb +++ b/app/services/administrateur_usage_statistics_service.rb @@ -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], diff --git a/app/views/administration_mailer/invite_admin.html.haml b/app/views/administration_mailer/invite_admin.html.haml index f646d2946..b048e8b30 100644 --- a/app/views/administration_mailer/invite_admin.html.haml +++ b/app/views/administration_mailer/invite_admin.html.haml @@ -9,10 +9,15 @@ %p Votre compte administrateur a été créé pour l'adresse email #{@admin.email}. -%p - %b - Pour l’activer, cliquez sur le lien suivant : - = link_to(admin_activate_url(token: @reset_password_token), admin_activate_url(token: @reset_password_token)) +- if @reset_password_token.present? + %p + %b + 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 = render partial: "layouts/mailers/bizdev_signature", locals: { author_name: @author_name } diff --git a/spec/controllers/webhook_controller_spec.rb b/spec/controllers/webhook_controller_spec.rb new file mode 100644 index 000000000..81e07e8c6 --- /dev/null +++ b/spec/controllers/webhook_controller_spec.rb @@ -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 diff --git a/spec/features/admin/admin_creation_spec.rb b/spec/features/admin/admin_creation_spec.rb index 166983b64..de719edc9 100644 --- a/spec/features/admin/admin_creation_spec.rb +++ b/spec/features/admin/admin_creation_spec.rb @@ -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 diff --git a/spec/mailers/previews/administration_mailer_preview.rb b/spec/mailers/previews/administration_mailer_preview.rb index e0fd0b0df..7527f9632 100644 --- a/spec/mailers/previews/administration_mailer_preview.rb +++ b/spec/mailers/previews/administration_mailer_preview.rb @@ -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 diff --git a/spec/models/administrateur_spec.rb b/spec/models/administrateur_spec.rb index e06c53630..4b9fef1f0 100644 --- a/spec/models/administrateur_spec.rb +++ b/spec/models/administrateur_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 430e0b1d4..a24949fbc 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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