From 03665e8466bba6a2370089a943cd70ad278827c3 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Fri, 26 Jun 2015 01:21:26 +0530 Subject: [PATCH] FIX: mark user as approved if an invite is already present --- app/models/email_token.rb | 2 +- app/services/user_activator.rb | 3 ++- spec/models/email_token_spec.rb | 35 ++++++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/app/models/email_token.rb b/app/models/email_token.rb index ce8c6b024..a7eb036f9 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 d96c8ca98..63defef4d 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 994a020bd..b1651ea59 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 -