Merge pull request #17351 from overleaf/jel-async-getUserForPasswordResetToken

[web] Promisify getUserForPasswordResetToken

GitOrigin-RevId: 4b0363b390af155f1bae4332fba7cf10c130e1c6
This commit is contained in:
Jessica Lawshe 2024-03-11 07:28:28 -05:00 committed by Copybot
parent 7a9c2fd644
commit 814ee0ac62
4 changed files with 65 additions and 71 deletions

View file

@ -165,8 +165,9 @@ module.exports = {
if (req.query.passwordResetToken != null) { if (req.query.passwordResetToken != null) {
return PasswordResetHandler.getUserForPasswordResetToken( return PasswordResetHandler.getUserForPasswordResetToken(
req.query.passwordResetToken, req.query.passwordResetToken,
(err, user, remainingUses) => { (err, result) => {
if (err || !user || remainingUses <= 0) { const { user, remainingPeeks } = result || {}
if (err || !user || remainingPeeks <= 0) {
return res.redirect('/user/password/reset?error=token_expired') return res.redirect('/user/password/reset?error=token_expired')
} }
req.session.resetToken = req.query.passwordResetToken req.session.resetToken = req.query.passwordResetToken

View file

@ -46,50 +46,50 @@ function expirePasswordResetToken(token, callback) {
}) })
} }
function getUserForPasswordResetToken(token, callback) { async function getUserForPasswordResetToken(token) {
OneTimeTokenHandler.peekValueFromToken('password', token, (err, result) => { let result
const { data, remainingPeeks } = result || {} try {
if (err != null) { result = await OneTimeTokenHandler.promises.peekValueFromToken(
'password',
token
)
} catch (err) {
if (err.name === 'NotFoundError') { if (err.name === 'NotFoundError') {
return callback(null, null) return
} else { } else {
return callback(err) throw err
} }
} }
const { data, remainingPeeks } = result || {}
if (data == null || data.email == null) { if (data == null || data.email == null) {
return callback(null, null, remainingPeeks) return { user: null, remainingPeeks }
} }
UserGetter.getUserByMainEmail(
data.email, const user = await UserGetter.promises.getUserByMainEmail(data.email, {
{ _id: 1, 'overleaf.id': 1, email: 1 }, _id: 1,
(err, user) => { 'overleaf.id': 1,
if (err != null) { email: 1,
callback(err) })
} else if (user == null) { if (user == null) {
callback(null, null, 0) return { user: null, remainingPeeks: 0 }
} else if ( } else if (data.user_id != null && data.user_id === user._id.toString()) {
data.user_id != null && return { user, remainingPeeks }
data.user_id === user._id.toString()
) {
callback(null, user, remainingPeeks)
} else if ( } else if (
data.v1_user_id != null && data.v1_user_id != null &&
user.overleaf != null && user.overleaf != null &&
data.v1_user_id === user.overleaf.id data.v1_user_id === user.overleaf.id
) { ) {
callback(null, user, remainingPeeks) return { user, remainingPeeks }
} else { } else {
callback(null, null, 0) return { user: null, remainingPeeks: 0 }
} }
}
)
})
} }
async function setNewUserPassword(token, password, auditLog) { async function setNewUserPassword(token, password, auditLog) {
const user = await PasswordResetHandler.promises.getUserForPasswordResetToken( const result =
token await PasswordResetHandler.promises.getUserForPasswordResetToken(token)
) const { user } = result || {}
if (!user) { if (!user) {
return { return {
@ -98,7 +98,6 @@ async function setNewUserPassword(token, password, auditLog) {
userId: null, userId: null,
} }
} }
await UserAuditLogHandler.promises.addEntry( await UserAuditLogHandler.promises.addEntry(
user._id, user._id,
'reset-password', 'reset-password',
@ -122,16 +121,14 @@ const PasswordResetHandler = {
setNewUserPassword: callbackify(setNewUserPassword), setNewUserPassword: callbackify(setNewUserPassword),
getUserForPasswordResetToken, getUserForPasswordResetToken: callbackify(getUserForPasswordResetToken),
expirePasswordResetToken, expirePasswordResetToken,
} }
PasswordResetHandler.promises = { PasswordResetHandler.promises = {
generateAndEmailResetToken, generateAndEmailResetToken,
getUserForPasswordResetToken: promisify( getUserForPasswordResetToken,
PasswordResetHandler.getUserForPasswordResetToken
),
expirePasswordResetToken: promisify( expirePasswordResetToken: promisify(
PasswordResetHandler.expirePasswordResetToken PasswordResetHandler.expirePasswordResetToken
), ),

View file

@ -43,7 +43,7 @@ describe('PasswordResetController', function () {
getUserForPasswordResetToken: sinon getUserForPasswordResetToken: sinon
.stub() .stub()
.withArgs(this.token) .withArgs(this.token)
.yields(null, { _id: this.user_id }, 1), .yields(null, { user: { _id: this.user_id }, remainingPeeks: 1 }),
} }
this.UserSessionsManager = { this.UserSessionsManager = {
promises: { promises: {
@ -377,7 +377,7 @@ describe('PasswordResetController', function () {
this.PasswordResetHandler.getUserForPasswordResetToken = sinon this.PasswordResetHandler.getUserForPasswordResetToken = sinon
.stub() .stub()
.withArgs(this.token) .withArgs(this.token)
.yields(null, { _id: this.user_id }, 0) .yields(null, { user: { _id: this.user_id }, remainingPeeks: 0 })
}) })
it('should redirect to the reset request page with an error message', function (done) { it('should redirect to the reset request page with an error message', function (done) {

View file

@ -13,6 +13,7 @@ describe('PasswordResetHandler', function () {
this.OneTimeTokenHandler = { this.OneTimeTokenHandler = {
promises: { promises: {
getNewToken: sinon.stub(), getNewToken: sinon.stub(),
peekValueFromToken: sinon.stub(),
}, },
peekValueFromToken: sinon.stub(), peekValueFromToken: sinon.stub(),
expireToken: sinon.stub(), expireToken: sinon.stub(),
@ -22,6 +23,7 @@ describe('PasswordResetHandler', function () {
getUser: sinon.stub(), getUser: sinon.stub(),
promises: { promises: {
getUserByAnyEmail: sinon.stub(), getUserByAnyEmail: sinon.stub(),
getUserByMainEmail: sinon.stub(),
}, },
} }
this.EmailHandler = { promises: { sendEmail: sinon.stub() } } this.EmailHandler = { promises: { sendEmail: sinon.stub() } }
@ -195,7 +197,7 @@ describe('PasswordResetHandler', function () {
}) })
describe('when no data is found', function () { describe('when no data is found', function () {
beforeEach(function () { beforeEach(function () {
this.OneTimeTokenHandler.peekValueFromToken.yields(null, null) this.OneTimeTokenHandler.promises.peekValueFromToken.resolves(null)
}) })
it('should return found == false and reset == false', function () { it('should return found == false and reset == false', function () {
@ -217,9 +219,7 @@ describe('PasswordResetHandler', function () {
describe('when the token has a user_id and email', function () { describe('when the token has a user_id and email', function () {
beforeEach(function () { beforeEach(function () {
this.OneTimeTokenHandler.peekValueFromToken this.OneTimeTokenHandler.promises.peekValueFromToken.resolves({
.withArgs('password', this.token)
.yields(null, {
data: { data: {
user_id: this.user._id, user_id: this.user._id,
email: this.email, email: this.email,
@ -285,7 +285,7 @@ describe('PasswordResetHandler', function () {
describe('when the email and user match', function () { describe('when the email and user match', function () {
describe('success', function () { describe('success', function () {
beforeEach(function () { beforeEach(function () {
this.UserGetter.getUserByMainEmail.yields(null, this.user) this.UserGetter.promises.getUserByMainEmail.resolves(this.user)
this.OneTimeTokenHandler.expireToken = sinon this.OneTimeTokenHandler.expireToken = sinon
.stub() .stub()
.callsArgWith(2, null) .callsArgWith(2, null)
@ -370,7 +370,7 @@ describe('PasswordResetHandler', function () {
describe('via setUserPassword', function () { describe('via setUserPassword', function () {
beforeEach(function () { beforeEach(function () {
this.PasswordResetHandler.promises.getUserForPasswordResetToken = this.PasswordResetHandler.promises.getUserForPasswordResetToken =
sinon.stub().withArgs(this.token).resolves(this.user) sinon.stub().withArgs(this.token).resolves({ user: this.user })
this.AuthenticationManager.promises.setUserPassword this.AuthenticationManager.promises.setUserPassword
.withArgs(this.user, this.password) .withArgs(this.user, this.password)
.rejects() .rejects()
@ -394,7 +394,7 @@ describe('PasswordResetHandler', function () {
describe('via UserAuditLogHandler', function () { describe('via UserAuditLogHandler', function () {
beforeEach(function () { beforeEach(function () {
this.PasswordResetHandler.promises.getUserForPasswordResetToken = this.PasswordResetHandler.promises.getUserForPasswordResetToken =
sinon.stub().withArgs(this.token).resolves(this.user) sinon.stub().withArgs(this.token).resolves({ user: this.user })
this.UserAuditLogHandler.promises.addEntry.rejects( this.UserAuditLogHandler.promises.addEntry.rejects(
new Error('oops') new Error('oops')
) )
@ -423,9 +423,7 @@ describe('PasswordResetHandler', function () {
describe('when the token has a v1_user_id and email', function () { describe('when the token has a v1_user_id and email', function () {
beforeEach(function () { beforeEach(function () {
this.user.overleaf = { id: 184 } this.user.overleaf = { id: 184 }
this.OneTimeTokenHandler.peekValueFromToken this.OneTimeTokenHandler.promises.peekValueFromToken.resolves({
.withArgs('password', this.token)
.yields(null, {
data: { data: {
v1_user_id: this.user.overleaf.id, v1_user_id: this.user.overleaf.id,
email: this.email, email: this.email,
@ -493,9 +491,7 @@ describe('PasswordResetHandler', function () {
describe('when the email and user match', function () { describe('when the email and user match', function () {
beforeEach(function () { beforeEach(function () {
this.UserGetter.getUserByMainEmail this.UserGetter.promises.getUserByMainEmail.resolves(this.user)
.withArgs(this.email)
.yields(null, this.user)
}) })
it('should return reset == true and the user id', function (done) { it('should return reset == true and the user id', function (done) {