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