diff --git a/app/models/email_token.rb b/app/models/email_token.rb index ce8c6b0241c..a7eb036f907 100644 --- a/app/models/email_token.rb +++ b/app/models/email_token.rb @@ -60,7 +60,7 @@ class EmailToken < ActiveRecord::Base end end # redeem invite, if available - Invite.redeem_from_email(user.email) + return User.find_by(email: Email.downcase(user.email)) if Invite.redeem_from_email(user.email).present? user rescue ActiveRecord::RecordInvalid # If the user's email is already taken, just return nil (failure) diff --git a/app/services/user_activator.rb b/app/services/user_activator.rb index d96c8ca981c..63defef4dc4 100644 --- a/app/services/user_activator.rb +++ b/app/services/user_activator.rb @@ -23,7 +23,8 @@ class UserActivator end def factory - if SiteSetting.must_approve_users? + invite = Invite.find_by(email: Email.downcase(@user.email)) + if SiteSetting.must_approve_users? && !(invite.present? && !invite.expired? && !invite.destroyed? && invite.link_valid?) ApprovalActivator elsif !user.active? EmailActivator diff --git a/spec/models/email_token_spec.rb b/spec/models/email_token_spec.rb index 994a020bdd4..b1651ea59ac 100644 --- a/spec/models/email_token_spec.rb +++ b/spec/models/email_token_spec.rb @@ -130,7 +130,40 @@ describe EmailToken do expect(EmailToken.confirm(email_token.token)).to be_blank end end + + context 'confirms the token and redeems invite' do + before do + SiteSetting.must_approve_users = true + end + + let(:invite) { Fabricate(:invite, email: 'test@example.com', user_id: nil) } + let(:invited_user) { Fabricate(:user, active: false, email: invite.email) } + let(:user_email_token) { invited_user.email_tokens.first } + let!(:confirmed_invited_user) { EmailToken.confirm(user_email_token.token) } + + it "returns the correct user" do + expect(confirmed_invited_user).to eq invited_user + end + + it 'marks the user as active' do + confirmed_invited_user.reload + expect(confirmed_invited_user).to be_active + end + + it 'marks the token as confirmed' do + user_email_token.reload + expect(user_email_token).to be_confirmed + end + + it 'redeems invite' do + invite.reload + expect(invite).to be_redeemed + end + + it 'marks the user as approved' do + expect(confirmed_invited_user).to be_approved + end + end end end -