Fix findOneAnd* usages

findOneAnd* now returns either a document or null rather than a result
set (unless the result set is requested explicitly). See
https://www.mongodb.com/blog/post/behavioral-changes-find-one-family-apis-node-js-driver-6-0-0.

GitOrigin-RevId: 93dc64cbcc663217f914cf9e9821e2b9642154db
This commit is contained in:
andrew rumble 2024-08-16 10:29:14 +01:00 committed by Copybot
parent 5b1c0c5008
commit 5f699ac5ef
2 changed files with 6 additions and 11 deletions

View file

@ -7,7 +7,7 @@ const { callbackify } = require('util')
const ONE_HOUR_IN_S = 60 * 60 const ONE_HOUR_IN_S = 60 * 60
async function peekValueFromToken(use, token) { async function peekValueFromToken(use, token) {
const result = await db.tokens.findOneAndUpdate( const tokenDoc = await db.tokens.findOneAndUpdate(
{ {
use, use,
token, token,
@ -22,8 +22,6 @@ async function peekValueFromToken(use, token) {
returnDocument: 'after', returnDocument: 'after',
} }
) )
const tokenDoc = result.value
if (!tokenDoc) { if (!tokenDoc) {
throw new Errors.NotFoundError('no token found') throw new Errors.NotFoundError('no token found')
} }
@ -82,11 +80,10 @@ const OneTimeTokenHandler = {
usedAt: now, usedAt: now,
}, },
}, },
function (error, result) { function (error, token) {
if (error) { if (error) {
return callback(error) return callback(error)
} }
const token = result.value
if (!token) { if (!token) {
return callback(new Errors.NotFoundError('no token found')) return callback(new Errors.NotFoundError('no token found'))
} }

View file

@ -97,7 +97,7 @@ describe('OneTimeTokenHandler', function () {
beforeEach(async function () { beforeEach(async function () {
this.db.tokens.findOneAndUpdate = sinon this.db.tokens.findOneAndUpdate = sinon
.stub() .stub()
.resolves({ value: { data, peekCount: 1 } }) .resolves({ data, peekCount: 1 })
result = await this.OneTimeTokenHandler.promises.peekValueFromToken( result = await this.OneTimeTokenHandler.promises.peekValueFromToken(
'password', 'password',
'mock-token' 'mock-token'
@ -128,7 +128,7 @@ describe('OneTimeTokenHandler', function () {
describe('when a valid token is not found', function () { describe('when a valid token is not found', function () {
beforeEach(function () { beforeEach(function () {
this.db.tokens.findOneAndUpdate = sinon.stub().resolves({ value: null }) this.db.tokens.findOneAndUpdate = sinon.stub().resolves(null)
}) })
it('should return a NotFoundError', async function () { it('should return a NotFoundError', async function () {
@ -175,7 +175,7 @@ describe('OneTimeTokenHandler', function () {
beforeEach(function () { beforeEach(function () {
this.db.tokens.findOneAndUpdate = sinon this.db.tokens.findOneAndUpdate = sinon
.stub() .stub()
.yields(null, { value: { data: 'mock-data' } }) .yields(null, { data: 'mock-data' })
this.OneTimeTokenHandler.getValueFromTokenAndExpire( this.OneTimeTokenHandler.getValueFromTokenAndExpire(
'password', 'password',
'mock-token', 'mock-token',
@ -207,9 +207,7 @@ describe('OneTimeTokenHandler', function () {
describe('when a valid token is not found', function () { describe('when a valid token is not found', function () {
beforeEach(function () { beforeEach(function () {
this.db.tokens.findOneAndUpdate = sinon this.db.tokens.findOneAndUpdate = sinon.stub().yields(null, null)
.stub()
.yields(null, { value: null })
this.OneTimeTokenHandler.getValueFromTokenAndExpire( this.OneTimeTokenHandler.getValueFromTokenAndExpire(
'password', 'password',
'mock-token', 'mock-token',