2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe EmailToken do
|
2014-12-31 22:55:03 +08:00
|
|
|
it { is_expected.to validate_presence_of :user_id }
|
|
|
|
it { is_expected.to validate_presence_of :email }
|
|
|
|
it { is_expected.to belong_to :user }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
context '#create' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user, active: false) }
|
2013-02-06 03:16:51 +08:00
|
|
|
let!(:original_token) { user.email_tokens.first }
|
2021-11-25 15:34:39 +08:00
|
|
|
let!(:email_token) { Fabricate(:email_token, user: user, email: 'bubblegum@adventuretime.ooo') }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it 'should create the email token' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(email_token).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-07-14 22:16:24 +08:00
|
|
|
it 'should downcase the email' do
|
2021-11-25 15:34:39 +08:00
|
|
|
token = Fabricate(:email_token, user: user, email: "UpperCaseSoWoW@GMail.com")
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(token.email).to eq "uppercasesowow@gmail.com"
|
2014-07-14 22:16:24 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'is valid' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(email_token).to be_valid
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a token' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(email_token.token).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not confirmed' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(email_token).to_not be_confirmed
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not expired' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(email_token).to_not be_expired
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks the older token as expired' do
|
|
|
|
original_token.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(original_token).to be_expired
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '#confirm' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user, active: false) }
|
2021-11-25 15:34:39 +08:00
|
|
|
let!(:email_token) { Fabricate(:email_token, user: user) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it 'returns nil with a nil token' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(EmailToken.confirm(nil)).to be_blank
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
it 'returns nil with an invalid token' do
|
|
|
|
expect(EmailToken.confirm("random token")).to be_blank
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil when a token is expired' do
|
|
|
|
email_token.update_column(:expired, true)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(EmailToken.confirm(email_token.token)).to be_blank
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it 'returns nil when a token is older than a specific time' do
|
2014-07-02 07:08:25 +08:00
|
|
|
SiteSetting.email_token_valid_hours = 10
|
|
|
|
email_token.update_column(:created_at, 11.hours.ago)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(EmailToken.confirm(email_token.token)).to be_blank
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
context 'taken email address' do
|
|
|
|
before do
|
|
|
|
@other_user = Fabricate(:coding_horror)
|
|
|
|
email_token.update_attribute :email, @other_user.email
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil when the email has been taken since the token has been generated' do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(EmailToken.confirm(email_token.token)).to be_blank
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'welcome message' do
|
|
|
|
it 'sends a welcome message when the user is activated' do
|
|
|
|
user = EmailToken.confirm(email_token.token)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(user.send_welcome_message).to eq true
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'success' do
|
|
|
|
let!(:confirmed_user) { EmailToken.confirm(email_token.token) }
|
|
|
|
|
|
|
|
it "returns the correct user" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(confirmed_user).to eq user
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks the user as active' do
|
|
|
|
confirmed_user.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(confirmed_user).to be_active
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks the token as confirmed' do
|
|
|
|
email_token.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(email_token).to be_confirmed
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2016-12-19 14:15:20 +08:00
|
|
|
it "will not confirm again" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(EmailToken.confirm(email_token.token)).to be_blank
|
2014-03-05 03:03:04 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2015-06-26 03:51:26 +08:00
|
|
|
|
|
|
|
context 'confirms the token and redeems invite' do
|
|
|
|
before do
|
|
|
|
SiteSetting.must_approve_users = true
|
2019-04-04 00:04:05 +08:00
|
|
|
Jobs.run_immediately!
|
2015-06-26 03:51:26 +08:00
|
|
|
end
|
|
|
|
|
2020-06-09 23:19:32 +08:00
|
|
|
fab!(:invite) { Fabricate(:invite, email: 'test@example.com') }
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:invited_user) { Fabricate(:user, active: false, email: invite.email) }
|
2021-11-25 15:34:39 +08:00
|
|
|
let!(:user_email_token) { Fabricate(:email_token, user: invited_user) }
|
2015-06-26 03:51:26 +08:00
|
|
|
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
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|