Avis: add create_gestionnaire logic
This commit is contained in:
parent
aaf155df72
commit
842999d229
3 changed files with 68 additions and 2 deletions
|
@ -1,7 +1,7 @@
|
|||
class Backoffice::AvisController < ApplicationController
|
||||
|
||||
before_action :authenticate_gestionnaire!, except: [:sign_up]
|
||||
before_action :check_avis_exists_and_email_belongs_to_avis, only: [:sign_up]
|
||||
before_action :authenticate_gestionnaire!, except: [:sign_up, :create_gestionnaire]
|
||||
before_action :check_avis_exists_and_email_belongs_to_avis, only: [:sign_up, :create_gestionnaire]
|
||||
|
||||
def create
|
||||
avis = Avis.new(create_params)
|
||||
|
@ -33,6 +33,23 @@ class Backoffice::AvisController < ApplicationController
|
|||
render layout: 'new_application'
|
||||
end
|
||||
|
||||
def create_gestionnaire
|
||||
email = params[:email]
|
||||
password = params['gestionnaire']['password']
|
||||
|
||||
gestionnaire = Gestionnaire.new(email: email, password: password)
|
||||
|
||||
if gestionnaire.save
|
||||
sign_in(gestionnaire, scope: :gestionnaire)
|
||||
Avis.link_avis_to_gestionnaire(gestionnaire)
|
||||
avis = Avis.find(params[:id])
|
||||
redirect_to url_for(backoffice_dossier_path(avis.dossier_id))
|
||||
else
|
||||
flash[:alert] = gestionnaire.errors.full_messages.join('<br>')
|
||||
redirect_to url_for(avis_sign_up_path(params[:id], email))
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def dossier
|
||||
|
|
|
@ -31,6 +31,7 @@ Rails.application.routes.draw do
|
|||
end
|
||||
|
||||
get 'avis/:id/sign_up/email/:email' => 'backoffice/avis#sign_up', constraints: { email: /.*/ }, as: 'avis_sign_up'
|
||||
post 'avis/:id/sign_up/email/:email' => 'backoffice/avis#create_gestionnaire', constraints: { email: /.*/ }
|
||||
|
||||
devise_scope :administrateur do
|
||||
get '/administrateurs/sign_in/demo' => 'administrateurs/sessions#demo'
|
||||
|
|
|
@ -92,4 +92,52 @@ describe Backoffice::AvisController, type: :controller do
|
|||
end
|
||||
end
|
||||
|
||||
describe '.create_gestionnaire' do
|
||||
let(:invited_email) { 'invited@avis.com' }
|
||||
let(:dossier) { create(:dossier) }
|
||||
let!(:avis) { Avis.create(email: invited_email, dossier: dossier) }
|
||||
let(:avis_id) { avis.id }
|
||||
let(:password) { '12345678' }
|
||||
let(:created_gestionnaire) { Gestionnaire.find_by(email: invited_email) }
|
||||
let(:invitations_email) { true }
|
||||
|
||||
before do
|
||||
allow(Avis).to receive(:link_avis_to_gestionnaire)
|
||||
expect(Avis).to receive(:avis_exists_and_email_belongs_to_avis?)
|
||||
.with(avis_id.to_s, invited_email)
|
||||
.and_return(invitations_email)
|
||||
|
||||
post :create_gestionnaire, params: { id: avis_id,
|
||||
email: invited_email,
|
||||
gestionnaire: {
|
||||
password: password
|
||||
} }
|
||||
end
|
||||
|
||||
context 'when the email does not belong to the invitation' do
|
||||
let(:invitations_email) { false }
|
||||
|
||||
it { is_expected.to redirect_to root_path }
|
||||
end
|
||||
|
||||
context 'when the email belongs to the invitation' do
|
||||
context 'when the gestionnaire creation succeeds' do
|
||||
it { expect(created_gestionnaire).to be_present }
|
||||
it { expect(created_gestionnaire.valid_password?(password)).to be true }
|
||||
|
||||
it { expect(Avis).to have_received(:link_avis_to_gestionnaire) }
|
||||
|
||||
it { expect(subject.current_gestionnaire).to eq(created_gestionnaire) }
|
||||
it { is_expected.to redirect_to backoffice_dossier_path(dossier) }
|
||||
end
|
||||
|
||||
context 'when the gestionnaire creation fails' do
|
||||
let(:password) { '' }
|
||||
|
||||
it { expect(created_gestionnaire).to be_nil }
|
||||
it { is_expected.to redirect_to avis_sign_up_path(avis_id, invited_email) }
|
||||
it { expect(flash.alert).to eq('Password : Le mot de passe est vide') }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue