From 0954367bf42fc09cb40a2bf1b6b5a3776c3d7439 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Sat, 15 Apr 2017 14:48:05 +0530 Subject: [PATCH] FIX: send activation email when accepting invite if password is set --- app/controllers/invites_controller.rb | 20 +++++++++++++------- app/models/invite_redeemer.rb | 8 -------- app/models/user.rb | 2 +- spec/controllers/invites_controller_spec.rb | 9 ++++++++- spec/models/invite_redeemer_spec.rb | 1 - 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb index e4990e0af16..176b067c543 100644 --- a/app/controllers/invites_controller.rb +++ b/app/controllers/invites_controller.rb @@ -36,9 +36,7 @@ class InvitesController < ApplicationController user = invite.redeem(username: params[:username], password: params[:password]) if user.present? log_on_user(user) - - # Send a welcome message if required - user.enqueue_welcome_message('welcome_invite') if user.send_welcome_message + post_process_invite(user) end topic = user.present? ? invite.topics.first : nil @@ -128,10 +126,7 @@ class InvitesController < ApplicationController user = Invite.redeem_from_token(params[:token], params[:email], params[:username], params[:name], params[:topic].to_i) if user.present? log_on_user(user) - - # Send a welcome message if required - user.enqueue_welcome_message('welcome_invite') if user.send_welcome_message - + post_process_invite(user) topic = invite.topics.first if topic.present? redirect_to path("#{topic.relative_url}") @@ -223,4 +218,15 @@ class InvitesController < ApplicationController false end end + + private + + def post_process_invite(user) + user.enqueue_welcome_message('welcome_invite') if user.send_welcome_message + if user.has_password? + email_token = user.email_tokens.create(email: user.email) + Jobs.enqueue(:critical_user_email, type: :signup, user_id: user.id, email_token: email_token.token) + end + end + end diff --git a/app/models/invite_redeemer.rb b/app/models/invite_redeemer.rb index 6f1406b06aa..d12f0c57bb6 100644 --- a/app/models/invite_redeemer.rb +++ b/app/models/invite_redeemer.rb @@ -62,7 +62,6 @@ InviteRedeemer = Struct.new(:invite, :username, :name, :password) do send_welcome_message notify_invitee send_password_instructions - enqueue_activation_mail delete_duplicate_invites end @@ -127,13 +126,6 @@ InviteRedeemer = Struct.new(:invite, :username, :name, :password) do end end - def enqueue_activation_mail - if invited_user.has_password? - email_token = invited_user.email_tokens.create(email: invited_user.email) - Jobs.enqueue(:critical_user_email, type: :signup, user_id: invited_user.id, email_token: email_token.token) - end - end - def notify_invitee if inviter = invite.invited_by inviter.notifications.create(notification_type: Notification.types[:invitee_accepted], diff --git a/app/models/user.rb b/app/models/user.rb index d47f6749cad..f3d7e325c6b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -797,7 +797,7 @@ class User < ActiveRecord::Base end def find_email - last_sent_email_address || email + last_sent_email_address.present? && EmailValidator.email_regex =~ last_sent_email_address ? last_sent_email_address : email end def tl3_requirements diff --git a/spec/controllers/invites_controller_spec.rb b/spec/controllers/invites_controller_spec.rb index 146a007753f..741d9a35ffd 100644 --- a/spec/controllers/invites_controller_spec.rb +++ b/spec/controllers/invites_controller_spec.rb @@ -223,10 +223,11 @@ describe InvitesController do end end - context 'welcome message' do + context 'welcome message and activation email' do before do Invite.any_instance.stubs(:redeem).returns(user) Jobs.expects(:enqueue).with(:invite_email, has_key(:invite_id)) + user.password_hash = nil end it 'sends a welcome message if set' do @@ -239,6 +240,12 @@ describe InvitesController do user.expects(:enqueue_welcome_message).with('welcome_invite').never xhr :put, :perform_accept_invitation, id: invite.invite_key, format: :json end + + it 'sends an activation email if password is set' do + user.password_hash = 'qaw3ni3h2wyr63lakw7pea1nrtr44pls' + Jobs.expects(:enqueue).with(:critical_user_email, has_entries(type: :signup, user_id: user.id)) + xhr :put, :perform_accept_invitation, id: invite.invite_key, format: :json + end end end end diff --git a/spec/models/invite_redeemer_spec.rb b/spec/models/invite_redeemer_spec.rb index 39d718508ec..c8e3be646a2 100644 --- a/spec/models/invite_redeemer_spec.rb +++ b/spec/models/invite_redeemer_spec.rb @@ -89,7 +89,6 @@ describe InviteRedeemer do it "can set password" do inviter = invite.invited_by - Jobs.expects(:enqueue).with(:critical_user_email, has_entries(type: :signup)) user = InviteRedeemer.new(invite, username, name, password).redeem expect(user).to have_password expect(user.confirm_password?(password)).to eq(true)