From 6a69d958dafa510b0f313e37eaa2cd12c7fd16ef Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 22 May 2018 17:11:48 +0200 Subject: [PATCH] Devise: move after_sign_up to after_confirmation --- .../users/confirmations_controller.rb | 38 +++++++++++++++++++ .../users/registrations_controller.rb | 15 ++------ config/routes.rb | 3 +- .../users/confirmations_controller_spec.rb | 23 +++++++++++ .../users/registrations_controller_spec.rb | 27 ++++--------- 5 files changed, 74 insertions(+), 32 deletions(-) create mode 100644 app/controllers/users/confirmations_controller.rb create mode 100644 spec/controllers/users/confirmations_controller_spec.rb diff --git a/app/controllers/users/confirmations_controller.rb b/app/controllers/users/confirmations_controller.rb new file mode 100644 index 000000000..1992ab9c1 --- /dev/null +++ b/app/controllers/users/confirmations_controller.rb @@ -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 diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 526c97c4d..f9abe0b35 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 2c1f00ca1..f4a174bb8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/controllers/users/confirmations_controller_spec.rb b/spec/controllers/users/confirmations_controller_spec.rb new file mode 100644 index 000000000..6da736c12 --- /dev/null +++ b/spec/controllers/users/confirmations_controller_spec.rb @@ -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 diff --git a/spec/controllers/users/registrations_controller_spec.rb b/spec/controllers/users/registrations_controller_spec.rb index e48d50126..14f391b35 100644 --- a/spec/controllers/users/registrations_controller_spec.rb +++ b/spec/controllers/users/registrations_controller_spec.rb @@ -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