2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-17 08:33:51 +08:00
|
|
|
require 'rotp'
|
2016-03-08 02:45:33 +08:00
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe UsersEmailController do
|
2019-11-20 15:31:25 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2021-11-25 15:34:39 +08:00
|
|
|
let!(:email_token) { Fabricate(:email_token, user: user) }
|
2019-11-20 15:31:25 +08:00
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
|
|
|
|
|
|
|
describe "#confirm-new-email" do
|
2020-10-07 11:02:24 +08:00
|
|
|
it 'does not redirect to login for signed out accounts, this route works fine as anon user' do
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-new-email/invalidtoken"
|
2019-11-20 15:31:25 +08:00
|
|
|
|
2020-10-07 11:02:24 +08:00
|
|
|
expect(response.status).to eq(200)
|
2019-11-20 15:31:25 +08:00
|
|
|
end
|
|
|
|
|
2021-01-20 08:52:25 +08:00
|
|
|
it 'does not redirect to login for signed out accounts on login_required sites, this route works fine as anon user' do
|
|
|
|
SiteSetting.login_required = true
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-new-email/invalidtoken"
|
2021-01-20 08:52:25 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
2016-03-08 03:40:11 +08:00
|
|
|
it 'errors out for invalid tokens' do
|
2019-11-20 15:31:25 +08:00
|
|
|
sign_in(user)
|
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-new-email/invalidtoken"
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-10-21 00:04:56 +08:00
|
|
|
expect(response.body).to include(I18n.t('change_email.already_done'))
|
2016-03-08 03:40:11 +08:00
|
|
|
end
|
|
|
|
|
2020-10-07 11:02:24 +08:00
|
|
|
it 'does not change email if accounts mismatch for a signed in user' do
|
2020-02-20 07:52:21 +08:00
|
|
|
updater = EmailUpdater.new(guardian: user.guardian, user: user)
|
2021-11-25 15:34:39 +08:00
|
|
|
updater.change_to('bubblegum@adventuretime.ooo')
|
2016-03-08 03:40:11 +08:00
|
|
|
|
2019-11-20 15:31:25 +08:00
|
|
|
old_email = user.email
|
2016-03-08 03:40:11 +08:00
|
|
|
|
2019-11-20 15:31:25 +08:00
|
|
|
sign_in(moderator)
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
put "/u/confirm-new-email", params: { token: "#{email_token.token}" }
|
|
|
|
expect(user.reload.email).to eq(old_email)
|
2016-03-08 03:40:11 +08:00
|
|
|
end
|
|
|
|
|
2019-11-20 15:31:25 +08:00
|
|
|
context "with a valid user" do
|
2020-02-20 07:52:21 +08:00
|
|
|
let(:updater) { EmailUpdater.new(guardian: user.guardian, user: user) }
|
2016-03-08 03:40:11 +08:00
|
|
|
|
|
|
|
before do
|
2019-11-20 15:31:25 +08:00
|
|
|
sign_in(user)
|
2021-11-25 15:34:39 +08:00
|
|
|
updater.change_to('bubblegum@adventuretime.ooo')
|
2016-03-08 03:40:11 +08:00
|
|
|
end
|
|
|
|
|
2020-06-18 02:41:29 +08:00
|
|
|
it 'includes security_key_allowed_credential_ids in a hidden field' do
|
|
|
|
key1 = Fabricate(:user_security_key_with_random_credential, user: user)
|
|
|
|
key2 = Fabricate(:user_security_key_with_random_credential, user: user)
|
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-new-email/#{updater.change_req.new_email_token.token}"
|
2020-06-18 02:41:29 +08:00
|
|
|
|
|
|
|
doc = Nokogiri::HTML5(response.body)
|
|
|
|
credential_ids = doc.css("#security-key-allowed-credential-ids").first["value"].split(",")
|
|
|
|
expect(credential_ids).to contain_exactly(key1.credential_id, key2.credential_id)
|
|
|
|
end
|
|
|
|
|
2016-03-08 03:40:11 +08:00
|
|
|
it 'confirms with a correct token' do
|
2017-02-20 17:37:01 +08:00
|
|
|
user.user_stat.update_columns(bounce_score: 42, reset_bounce_score_after: 1.week.from_now)
|
|
|
|
|
2020-07-16 20:51:30 +08:00
|
|
|
put "/u/confirm-new-email", params: {
|
2021-11-25 15:34:39 +08:00
|
|
|
token: "#{updater.change_req.new_email_token.token}"
|
2020-07-16 20:51:30 +08:00
|
|
|
}
|
2017-06-01 16:19:42 +08:00
|
|
|
|
2019-11-20 15:31:25 +08:00
|
|
|
expect(response.status).to eq(302)
|
|
|
|
expect(response.redirect_url).to include("done")
|
2017-02-20 17:37:01 +08:00
|
|
|
user.reload
|
|
|
|
expect(user.user_stat.bounce_score).to eq(0)
|
|
|
|
expect(user.user_stat.reset_bounce_score_after).to eq(nil)
|
2021-11-25 15:34:39 +08:00
|
|
|
expect(user.email).to eq('bubblegum@adventuretime.ooo')
|
2018-05-09 03:23:45 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context 'when second factor is required' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:second_factor) { Fabricate(:user_second_factor_totp, user: user) }
|
2019-11-15 07:19:23 +08:00
|
|
|
fab!(:backup_code) { Fabricate(:user_second_factor_backup, user: user) }
|
2017-12-22 09:18:12 +08:00
|
|
|
|
|
|
|
it 'requires a second factor token' do
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-new-email/#{updater.change_req.new_email_token.token}"
|
2018-02-20 14:44:51 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2021-11-25 15:34:39 +08:00
|
|
|
expect(response.body).to include(I18n.t("login.second_factor_title"))
|
|
|
|
expect(response.body).not_to include(I18n.t("login.invalid_second_factor_code"))
|
2017-12-22 09:18:12 +08:00
|
|
|
end
|
|
|
|
|
2019-11-15 07:19:23 +08:00
|
|
|
it 'requires a backup token' do
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-new-email/#{updater.change_req.new_email_token.token}?show_backup=true"
|
2019-11-15 07:19:23 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2021-11-25 15:34:39 +08:00
|
|
|
expect(response.body).to include(I18n.t("login.second_factor_backup_title"))
|
2019-11-15 07:19:23 +08:00
|
|
|
end
|
|
|
|
|
2017-12-22 09:18:12 +08:00
|
|
|
it 'adds an error on a second factor attempt' do
|
2019-11-20 15:31:25 +08:00
|
|
|
put "/u/confirm-new-email", params: {
|
2021-11-25 15:34:39 +08:00
|
|
|
token: updater.change_req.new_email_token.token,
|
2018-06-28 16:12:32 +08:00
|
|
|
second_factor_token: "000000",
|
|
|
|
second_factor_method: UserSecondFactor.methods[:totp]
|
2018-02-20 14:44:51 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 15:31:25 +08:00
|
|
|
expect(response.status).to eq(302)
|
|
|
|
expect(flash[:invalid_second_factor]).to eq(true)
|
2017-12-22 09:18:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'confirms with a correct second token' do
|
2019-11-20 15:31:25 +08:00
|
|
|
put "/u/confirm-new-email", params: {
|
2018-06-28 16:12:32 +08:00
|
|
|
second_factor_token: ROTP::TOTP.new(second_factor.data).now,
|
2019-11-20 15:31:25 +08:00
|
|
|
second_factor_method: UserSecondFactor.methods[:totp],
|
2021-11-25 15:34:39 +08:00
|
|
|
token: updater.change_req.new_email_token.token
|
2018-02-20 14:44:51 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 15:31:25 +08:00
|
|
|
expect(response.status).to eq(302)
|
2021-11-25 15:34:39 +08:00
|
|
|
expect(user.reload.email).to eq('bubblegum@adventuretime.ooo')
|
2017-12-22 09:18:12 +08:00
|
|
|
end
|
2021-02-04 07:03:30 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with rate limiting" do
|
2021-02-04 07:03:30 +08:00
|
|
|
before { RateLimiter.clear_all!; RateLimiter.enable }
|
|
|
|
|
|
|
|
it "rate limits by IP" do
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
6.times do
|
|
|
|
put "/u/confirm-new-email", params: {
|
|
|
|
token: "blah",
|
|
|
|
second_factor_token: "000000",
|
|
|
|
second_factor_method: UserSecondFactor.methods[:totp]
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
end
|
|
|
|
|
|
|
|
put "/u/confirm-new-email", params: {
|
|
|
|
token: "blah",
|
|
|
|
second_factor_token: "000000",
|
|
|
|
second_factor_method: UserSecondFactor.methods[:totp]
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(429)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "rate limits by username" do
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
6.times do |x|
|
|
|
|
user.email_change_requests.last.update(change_state: EmailChangeRequest.states[:complete])
|
|
|
|
put "/u/confirm-new-email", params: {
|
2021-11-25 15:34:39 +08:00
|
|
|
token: updater.change_req.new_email_token.token,
|
2021-02-04 07:03:30 +08:00
|
|
|
second_factor_token: "000000",
|
|
|
|
second_factor_method: UserSecondFactor.methods[:totp]
|
|
|
|
}, env: { "REMOTE_ADDR": "1.2.3.#{x}" }
|
|
|
|
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
end
|
|
|
|
|
|
|
|
user.email_change_requests.last.update(change_state: EmailChangeRequest.states[:authorizing_new])
|
|
|
|
put "/u/confirm-new-email", params: {
|
2021-11-25 15:34:39 +08:00
|
|
|
token: updater.change_req.new_email_token.token,
|
2021-02-04 07:03:30 +08:00
|
|
|
second_factor_token: "000000",
|
|
|
|
second_factor_method: UserSecondFactor.methods[:totp]
|
|
|
|
}, env: { "REMOTE_ADDR": "1.2.3.4" }
|
|
|
|
|
|
|
|
expect(response.status).to eq(429)
|
|
|
|
end
|
|
|
|
end
|
2017-12-22 09:18:12 +08:00
|
|
|
end
|
2020-01-15 18:27:12 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when security key is required" do
|
2020-01-15 18:27:12 +08:00
|
|
|
fab!(:user_security_key) do
|
|
|
|
Fabricate(
|
|
|
|
:user_security_key,
|
|
|
|
user: user,
|
|
|
|
credential_id: valid_security_key_data[:credential_id],
|
|
|
|
public_key: valid_security_key_data[:public_key]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
simulate_localhost_webauthn_challenge
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'requires a security key' do
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-new-email/#{updater.change_req.new_email_token.token}"
|
2020-01-15 18:27:12 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2021-11-25 15:34:39 +08:00
|
|
|
expect(response.body).to include(I18n.t("login.security_key_authenticate"))
|
|
|
|
expect(response.body).to include(I18n.t("login.security_key_description"))
|
2020-01-15 18:27:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "if the user has a TOTP enabled and wants to use that instead" do
|
|
|
|
before do
|
|
|
|
Fabricate(:user_second_factor_totp, user: user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows entering the totp code instead' do
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-new-email/#{updater.change_req.new_email_token.token}?show_totp=true"
|
2020-01-15 18:27:12 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2021-11-25 15:34:39 +08:00
|
|
|
expect(response.body).to include(I18n.t("login.second_factor_title"))
|
|
|
|
expect(response.body).not_to include(I18n.t("login.security_key_authenticate"))
|
2020-01-15 18:27:12 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an error on a security key attempt' do
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-new-email/#{updater.change_req.new_email_token.token}"
|
2020-01-15 18:27:12 +08:00
|
|
|
put "/u/confirm-new-email", params: {
|
2021-11-25 15:34:39 +08:00
|
|
|
token: updater.change_req.new_email_token.token,
|
2020-01-15 18:27:12 +08:00
|
|
|
second_factor_token: "{}",
|
|
|
|
second_factor_method: UserSecondFactor.methods[:security_key]
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
expect(flash[:invalid_second_factor]).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'confirms with a correct security key token' do
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-new-email/#{updater.change_req.new_email_token.token}"
|
2020-01-15 18:27:12 +08:00
|
|
|
put "/u/confirm-new-email", params: {
|
2021-11-25 15:34:39 +08:00
|
|
|
token: updater.change_req.new_email_token.token,
|
2020-01-15 18:27:12 +08:00
|
|
|
second_factor_token: valid_security_key_auth_post_data.to_json,
|
2021-11-25 15:34:39 +08:00
|
|
|
second_factor_method: UserSecondFactor.methods[:security_key]
|
2020-01-15 18:27:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(302)
|
2021-11-25 15:34:39 +08:00
|
|
|
expect(user.reload.email).to eq('bubblegum@adventuretime.ooo')
|
2020-01-15 18:27:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "if the security key data JSON is garbled" do
|
|
|
|
it "raises an invalid parameters error" do
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-new-email/#{updater.change_req.new_email_token.token}"
|
2020-01-15 18:27:12 +08:00
|
|
|
put "/u/confirm-new-email", params: {
|
2021-11-25 15:34:39 +08:00
|
|
|
token: updater.change_req.new_email_token.token,
|
2020-01-15 18:27:12 +08:00
|
|
|
second_factor_token: "{someweird: 8notjson}",
|
2021-11-25 15:34:39 +08:00
|
|
|
second_factor_method: UserSecondFactor.methods[:security_key]
|
2020-01-15 18:27:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-03-08 03:40:11 +08:00
|
|
|
end
|
2023-01-05 06:08:55 +08:00
|
|
|
|
|
|
|
it "destroys email tokens associated with the old email after the new email is confirmed" do
|
|
|
|
SiteSetting.enable_secondary_emails = true
|
|
|
|
|
|
|
|
email_token = user.email_tokens.create!(email: user.email, scope: EmailToken.scopes[:password_reset])
|
|
|
|
|
|
|
|
updater = EmailUpdater.new(guardian: user.guardian, user: user)
|
|
|
|
updater.change_to('bubblegum@adventuretime.ooo')
|
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
put "/u/confirm-new-email", params: {
|
|
|
|
token: "#{updater.change_req.new_email_token.token}"
|
|
|
|
}
|
|
|
|
|
|
|
|
new_password = SecureRandom.hex
|
|
|
|
put "/u/password-reset/#{email_token.token}.json", params: {
|
|
|
|
password: new_password
|
|
|
|
}
|
|
|
|
expect(response.parsed_body["success"]).to eq(false)
|
|
|
|
expect(response.parsed_body["message"]).to eq(I18n.t("password_reset.no_token", base_url: Discourse.base_url))
|
|
|
|
expect(user.reload.confirm_password?(new_password)).to eq(false)
|
|
|
|
end
|
2016-03-08 03:40:11 +08:00
|
|
|
end
|
|
|
|
|
2019-11-20 15:31:25 +08:00
|
|
|
describe '#confirm-old-email' do
|
|
|
|
it 'redirects to login for signed out accounts' do
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-old-email/invalidtoken"
|
2019-11-20 15:31:25 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
expect(response.redirect_url).to eq("http://test.localhost/login")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'errors out for invalid tokens' do
|
|
|
|
sign_in(user)
|
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-old-email/invalidtoken"
|
2019-11-20 15:31:25 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.body).to include(I18n.t('change_email.already_done'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'bans change when accounts do not match' do
|
|
|
|
sign_in(user)
|
2020-02-20 07:52:21 +08:00
|
|
|
updater = EmailUpdater.new(guardian: moderator.guardian, user: moderator)
|
2021-11-25 15:34:39 +08:00
|
|
|
email_change_request = updater.change_to('bubblegum@adventuretime.ooo')
|
2019-11-20 15:31:25 +08:00
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-old-email/#{email_change_request.old_email_token.token}"
|
2019-11-20 15:31:25 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(body).to include("alert-error")
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context 'with valid old token' do
|
2019-11-20 15:31:25 +08:00
|
|
|
it 'confirms with a correct token' do
|
|
|
|
sign_in(moderator)
|
2020-02-20 07:52:21 +08:00
|
|
|
updater = EmailUpdater.new(guardian: moderator.guardian, user: moderator)
|
2021-11-25 15:34:39 +08:00
|
|
|
email_change_request = updater.change_to('bubblegum@adventuretime.ooo')
|
2019-11-20 15:31:25 +08:00
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
get "/u/confirm-old-email/#{email_change_request.old_email_token.token}"
|
2019-11-20 15:31:25 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
body = CGI.unescapeHTML(response.body)
|
2021-11-25 15:34:39 +08:00
|
|
|
expect(body).to include(I18n.t('change_email.authorizing_old.title'))
|
|
|
|
expect(body).to include(I18n.t('change_email.authorizing_old.description'))
|
2019-11-20 15:31:25 +08:00
|
|
|
|
|
|
|
put "/u/confirm-old-email", params: {
|
2021-11-25 15:34:39 +08:00
|
|
|
token: email_change_request.old_email_token.token
|
2019-11-20 15:31:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
expect(response.redirect_url).to include("done=true")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
describe '#create' do
|
|
|
|
it 'has an email token' do
|
|
|
|
sign_in(user)
|
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
expect { post "/u/#{user.username}/preferences/email.json", params: { email: 'bubblegum@adventuretime.ooo' } }
|
2020-06-11 00:11:49 +08:00
|
|
|
.to change(EmailChangeRequest, :count)
|
|
|
|
|
|
|
|
emailChangeRequest = EmailChangeRequest.last
|
|
|
|
expect(emailChangeRequest.old_email).to eq(nil)
|
2021-11-25 15:34:39 +08:00
|
|
|
expect(emailChangeRequest.new_email).to eq('bubblegum@adventuretime.ooo')
|
2020-06-11 00:11:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-20 14:44:51 +08:00
|
|
|
describe '#update' do
|
2016-03-08 02:45:33 +08:00
|
|
|
it "requires you to be logged in" do
|
2021-11-25 15:34:39 +08:00
|
|
|
put "/u/#{user.username}/preferences/email.json", params: { email: 'bubblegum@adventuretime.ooo' }
|
2018-01-12 11:15:10 +08:00
|
|
|
expect(response.status).to eq(403)
|
2016-03-08 02:45:33 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when logged in' do
|
2017-08-31 12:06:56 +08:00
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2016-03-08 02:45:33 +08:00
|
|
|
|
|
|
|
it 'raises an error without an email parameter' do
|
2018-01-12 11:15:10 +08:00
|
|
|
put "/u/#{user.username}/preferences/email.json"
|
|
|
|
expect(response.status).to eq(400)
|
2016-03-08 02:45:33 +08:00
|
|
|
end
|
|
|
|
|
2018-08-21 09:54:34 +08:00
|
|
|
it 'raises an error without an invalid email' do
|
|
|
|
put "/u/#{user.username}/preferences/email.json", params: { email: "sam@not-email.com'" }
|
|
|
|
expect(response.status).to eq(422)
|
2020-06-03 10:13:25 +08:00
|
|
|
expect(response.body).to include("Email is invalid")
|
2018-08-21 09:54:34 +08:00
|
|
|
end
|
|
|
|
|
2016-03-08 02:45:33 +08:00
|
|
|
it "raises an error if you can't edit the user's email" do
|
2021-11-25 15:34:39 +08:00
|
|
|
SiteSetting.email_editable = false
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
put "/u/#{user.username}/preferences/email.json", params: { email: 'bubblegum@adventuretime.ooo' }
|
2016-03-08 02:45:33 +08:00
|
|
|
expect(response).to be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the new email address is taken' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:other_user) { Fabricate(:coding_horror) }
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context 'when hide_email_address_taken is disabled' do
|
2017-10-04 23:41:08 +08:00
|
|
|
before do
|
|
|
|
SiteSetting.hide_email_address_taken = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error' do
|
2021-11-25 15:34:39 +08:00
|
|
|
put "/u/#{user.username}/preferences/email.json", params: { email: other_user.email }
|
2018-06-05 15:29:17 +08:00
|
|
|
expect(response).to_not be_successful
|
2017-10-04 23:41:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error if there is whitespace too' do
|
2021-11-25 15:34:39 +08:00
|
|
|
put "/u/#{user.username}/preferences/email.json", params: { email: "#{other_user.email} " }
|
2018-06-05 15:29:17 +08:00
|
|
|
expect(response).to_not be_successful
|
2017-10-04 23:41:08 +08:00
|
|
|
end
|
2016-03-08 02:45:33 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context 'when hide_email_address_taken is enabled' do
|
2017-10-04 23:41:08 +08:00
|
|
|
before do
|
|
|
|
SiteSetting.hide_email_address_taken = true
|
|
|
|
end
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2017-10-04 23:41:08 +08:00
|
|
|
it 'responds with success' do
|
2021-11-25 15:34:39 +08:00
|
|
|
put "/u/#{user.username}/preferences/email.json", params: { email: other_user.email }
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-10-04 23:41:08 +08:00
|
|
|
end
|
2016-03-08 02:45:33 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when new email is different case of existing email' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:other_user) { Fabricate(:user, email: 'case.insensitive@gmail.com') }
|
2016-03-08 02:45:33 +08:00
|
|
|
|
|
|
|
it 'raises an error' do
|
2021-11-25 15:34:39 +08:00
|
|
|
put "/u/#{user.username}/preferences/email.json", params: { email: other_user.email.upcase }
|
2018-06-05 15:29:17 +08:00
|
|
|
expect(response).to_not be_successful
|
2016-03-08 02:45:33 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
it 'raises an error when new email domain is present in blocked_email_domains site setting' do
|
|
|
|
SiteSetting.blocked_email_domains = "mailinator.com"
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
put "/u/#{user.username}/preferences/email.json", params: { email: "not_good@mailinator.com" }
|
2018-06-05 15:29:17 +08:00
|
|
|
expect(response).to_not be_successful
|
2016-03-08 02:45:33 +08:00
|
|
|
end
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
it 'raises an error when new email domain is not present in allowed_email_domains site setting' do
|
|
|
|
SiteSetting.allowed_email_domains = "discourse.org"
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
put "/u/#{user.username}/preferences/email.json", params: { email: 'bubblegum@adventuretime.ooo' }
|
2018-06-05 15:29:17 +08:00
|
|
|
expect(response).to_not be_successful
|
2016-03-08 02:45:33 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context 'with success' do
|
2016-03-08 02:45:33 +08:00
|
|
|
it 'has an email token' do
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
2021-11-25 15:34:39 +08:00
|
|
|
put "/u/#{user.username}/preferences/email.json", params: { email: 'bubblegum@adventuretime.ooo' }
|
2017-08-31 12:06:56 +08:00
|
|
|
end.to change(EmailChangeRequest, :count)
|
2016-03-08 02:45:33 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|