From c3610fc96e1110de0554bb7e8f4d0dc220f5b91a Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Wed, 20 Jun 2018 13:30:32 +0200 Subject: [PATCH] [fix #2067] Resend confirmation mail if the user is not confirmed --- .../users/registrations_controller.rb | 6 +++- .../users/registrations_controller_spec.rb | 28 +++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 95be35e5b..6bbcbdf4a 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -17,7 +17,11 @@ class Users::RegistrationsController < Devise::RegistrationsController def create user = User.find_by(email: params[:user][:email]) if user.present? - UserMailer.new_account_warning(user).deliver_later + if user.confirmed? + UserMailer.new_account_warning(user).deliver_later + else + user.resend_confirmation_instructions + end flash.notice = t('devise.registrations.signed_up_but_unconfirmed') redirect_to root_path else diff --git a/spec/controllers/users/registrations_controller_spec.rb b/spec/controllers/users/registrations_controller_spec.rb index baebdd389..1c9a5793b 100644 --- a/spec/controllers/users/registrations_controller_spec.rb +++ b/spec/controllers/users/registrations_controller_spec.rb @@ -34,16 +34,34 @@ describe Users::RegistrationsController, type: :controller do end context 'when the user already exists' do - let!(:existing_user) { create(:user, email: email, password: password) } + let!(:existing_user) { create(:user, email: email, password: password, confirmed_at: confirmed_at) } before do allow(UserMailer).to receive(:new_account_warning).and_return(double(deliver_later: 'deliver')) - subject end - it { expect(response).to redirect_to(root_path) } - it { expect(flash.notice).to eq(I18n.t('devise.registrations.signed_up_but_unconfirmed')) } - it { expect(UserMailer).to have_received(:new_account_warning) } + context 'and the user is confirmed' do + let(:confirmed_at) { DateTime.now } + + before { subject } + + it { expect(response).to redirect_to(root_path) } + it { expect(flash.notice).to eq(I18n.t('devise.registrations.signed_up_but_unconfirmed')) } + it { expect(UserMailer).to have_received(:new_account_warning) } + end + + context 'and the user is not confirmed' do + let(:confirmed_at) { nil } + + before do + expect_any_instance_of(User).to receive(:resend_confirmation_instructions) + subject + end + + it { expect(response).to redirect_to(root_path) } + it { expect(flash.notice).to eq(I18n.t('devise.registrations.signed_up_but_unconfirmed')) } + it { expect(UserMailer).not_to have_received(:new_account_warning) } + end end end end