Devise: move after_sign_up to after_confirmation

This commit is contained in:
simon lehericey 2018-05-22 17:11:48 +02:00 committed by Paul Chavard
parent 5bd589344e
commit 6a69d958da
5 changed files with 74 additions and 32 deletions

View file

@ -0,0 +1,38 @@
# frozen_string_literal: true
class Users::ConfirmationsController < Devise::ConfirmationsController
# GET /resource/confirmation/new
# def new
# super
# end
# POST /resource/confirmation
# def create
# super
# end
# GET /resource/confirmation?confirmation_token=abcdef
# def show
# super
# end
protected
# The path used after resending confirmation instructions.
# def after_resending_confirmation_instructions_path_for(resource_name)
# super(resource_name)
# end
# The path used after confirmation.
def after_confirmation_path_for(resource_name, resource)
check_invite!(resource)
super(resource_name, resource)
end
private
def check_invite!(user)
Invite.where(email: user.email).update_all user_id: user.id
end
end

View file

@ -4,12 +4,9 @@ class Users::RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
def after_sign_up_path_for(resource_or_scope)
WelcomeMailer.welcome_email(resource_or_scope).deliver_now!
check_invite! resource_or_scope
super
end
# def after_sign_up_path_for(resource_or_scope)
# super
# end
# GET /resource/sign_up
# def new
@ -66,10 +63,4 @@ class Users::RegistrationsController < Devise::RegistrationsController
# def after_inactive_sign_up_path_for(resource)
# super(resource)
# end
private
def check_invite!(user)
Invite.where(email: user.email).update_all user_id: user.id
end
end

View file

@ -43,7 +43,8 @@ Rails.application.routes.draw do
devise_for :users, controllers: {
sessions: 'users/sessions',
registrations: 'users/registrations',
passwords: 'users/passwords'
passwords: 'users/passwords',
confirmations: 'users/confirmations'
}
devise_scope :user do

View file

@ -0,0 +1,23 @@
describe Users::ConfirmationsController, type: :controller do
let(:email) { 'mail@beta.gouv.fr' }
let(:user) do
create(:user,
email: email,
password: 'a good password',
confirmation_token: '123',
confirmed_at: nil)
end
before { @request.env["devise.mapping"] = Devise.mappings[:user] }
describe '#check_invite!' do
let!(:invite) { create(:invite, email: email) }
let!(:invite2) { create(:invite, email: email) }
before { get :show, params: { confirmation_token: user.confirmation_token } }
it 'the new user is connect at his two invite' do
expect(User.last.invites.size).to eq(2)
end
end
end

View file

@ -11,35 +11,24 @@ describe Users::RegistrationsController, type: :controller do
end
describe '#create' do
subject { post :create, params: { user: user } }
subject do
post :create, params: { user: user }
end
context 'when user is correct' do
it 'sends welcome email' do
expect(WelcomeMailer).to receive(:welcome_email).and_return(WelcomeMailer)
expect(WelcomeMailer).to receive(:deliver_now!)
it 'sends confirmation instruction' do
expect(DeviseUserMailer).to receive(:confirmation_instructions).and_return(DeviseUserMailer)
expect(DeviseUserMailer).to receive(:deliver)
subject
end
describe '#check_invite!' do
let!(:invite) { create :invite, email: email }
let!(:invite2) { create :invite, email: email }
before do
subject
end
it 'the new user is connect at his two invite' do
expect(User.last.invites.size).to eq 2
end
end
end
context 'when user is not correct' do
let(:user) { { email: '', password: password } }
it 'not sends welcome email' do
expect(WelcomeMailer).not_to receive(:welcome_email)
it 'not sends confirmation instruction' do
expect(DeviseUserMailer).not_to receive(:confirmation_instructions)
subject
end