FIX: send activation email when accepting invite if password is set

This commit is contained in:
Arpit Jalan 2017-04-15 14:48:05 +05:30
parent c155b05595
commit 0954367bf4
5 changed files with 22 additions and 18 deletions

View File

@ -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

View File

@ -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],

View File

@ -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

View File

@ -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

View File

@ -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)