Merge pull request #16186 from overleaf/mj-mongo-object-id

[web] Use constructor for ObjectId

GitOrigin-RevId: 9eb8b377ea599605b72af237d1ab12f4d8287162
This commit is contained in:
Mathias Jakobsen
2023-12-19 09:04:02 +00:00
committed by Copybot
parent 0ac514f81b
commit c371732e6e
100 changed files with 423 additions and 368 deletions
@@ -1,17 +1,22 @@
const { expect } = require('chai')
const sinon = require('sinon')
const SandboxedModule = require('sandboxed-module')
const { ObjectId } = require('mongodb')
const MODULE_PATH =
'../../../../app/src/Features/Project/FolderStructureBuilder'
const MOCK_OBJECT_ID = 'MOCK_OBJECT_ID'
const MOCK_OBJECT_ID = new ObjectId('657306930a1cf28031c358da')
describe('FolderStructureBuilder', function () {
beforeEach(function () {
this.ObjectId = sinon.stub().returns(MOCK_OBJECT_ID)
this.FolderStructureBuilder = SandboxedModule.require(MODULE_PATH, {
requires: {
mongodb: { ObjectId: this.ObjectId },
mongodb: {
ObjectId: class {
constructor() {
return MOCK_OBJECT_ID
}
},
},
},
})
})
@@ -25,9 +25,9 @@ const modulePath = Path.join(
describe('ProjectCollabratecDetailsHandler', function () {
beforeEach(function () {
this.projectId = ObjectId('5bea8747c7bba6012fcaceb3')
this.userId = ObjectId('5be316a9c7f6aa03802ea8fb')
this.userId2 = ObjectId('5c1794b3f0e89b1d1c577eca')
this.projectId = new ObjectId('5bea8747c7bba6012fcaceb3')
this.userId = new ObjectId('5be316a9c7f6aa03802ea8fb')
this.userId2 = new ObjectId('5c1794b3f0e89b1d1c577eca')
this.ProjectModel = {}
this.ProjectCollabratecDetailsHandler = SandboxedModule.require(
modulePath,
@@ -132,10 +132,10 @@ describe('ProjectCollabratecDetailsHandler', function () {
it('should call find with project and user id', function () {
return expect(this.ProjectModel.findOne).to.have.been.calledWithMatch(
{
_id: ObjectId(this.projectId),
_id: new ObjectId(this.projectId),
collabratecUsers: {
$elemMatch: {
user_id: ObjectId(this.userId),
user_id: new ObjectId(this.userId),
},
},
}
@@ -11,10 +11,10 @@ const MODULE_PATH = path.join(
describe('ProjectController', function () {
beforeEach(function () {
this.project_id = ObjectId('abcdefabcdefabcdefabcdef')
this.project_id = new ObjectId('abcdefabcdefabcdefabcdef')
this.user = {
_id: ObjectId('123456123456123456123456'),
_id: new ObjectId('123456123456123456123456'),
email: 'test@overleaf.com',
first_name: 'bjkdsjfk',
features: {},
@@ -590,7 +590,7 @@ describe('ProjectController', function () {
this.res.render = (pageName, opts) => {
expect(this.UserModel.updateOne).to.have.been.calledOnce
expect(this.UserModel.updateOne.args[0][0]).to.deep.equal({
_id: ObjectId(this.user._id),
_id: new ObjectId(this.user._id),
})
expect(this.UserModel.updateOne.args[0][1].$set.lastActive).to.exist
done()
@@ -567,7 +567,7 @@ describe('ProjectDeleter', function () {
describe('archiveProject', function () {
beforeEach(function () {
const archived = [ObjectId(this.user._id)]
const archived = [new ObjectId(this.user._id)]
this.ProjectHelper.calculateArchivedArray.returns(archived)
this.ProjectMock.expects('findOne')
@@ -580,7 +580,7 @@ describe('ProjectDeleter', function () {
{ _id: this.project._id },
{
$set: { archived },
$pull: { trashed: ObjectId(this.user._id) },
$pull: { trashed: new ObjectId(this.user._id) },
}
)
.resolves()
@@ -609,7 +609,7 @@ describe('ProjectDeleter', function () {
describe('unarchiveProject', function () {
beforeEach(function () {
const archived = [ObjectId(this.user._id)]
const archived = [new ObjectId(this.user._id)]
this.ProjectHelper.calculateArchivedArray.returns(archived)
this.ProjectMock.expects('findOne')
@@ -645,7 +645,7 @@ describe('ProjectDeleter', function () {
describe('trashProject', function () {
beforeEach(function () {
const archived = [ObjectId(this.user._id)]
const archived = [new ObjectId(this.user._id)]
this.ProjectHelper.calculateArchivedArray.returns(archived)
this.ProjectMock.expects('findOne')
@@ -657,7 +657,7 @@ describe('ProjectDeleter', function () {
.withArgs(
{ _id: this.project._id },
{
$addToSet: { trashed: ObjectId(this.user._id) },
$addToSet: { trashed: new ObjectId(this.user._id) },
$set: { archived },
}
)
@@ -695,7 +695,7 @@ describe('ProjectDeleter', function () {
this.ProjectMock.expects('updateOne')
.withArgs(
{ _id: this.project._id },
{ $pull: { trashed: ObjectId(this.user._id) } }
{ $pull: { trashed: new ObjectId(this.user._id) } }
)
.resolves()
})
@@ -731,8 +731,8 @@ describe('ProjectDeleter', function () {
describe('undeleteProject', function () {
beforeEach(function () {
this.unknownProjectId = ObjectId()
this.purgedProjectId = ObjectId()
this.unknownProjectId = new ObjectId()
this.purgedProjectId = new ObjectId()
this.deletedProject = {
_id: 'deleted',
@@ -10,16 +10,16 @@ const MODULE_PATH = '../../../../app/src/Features/Project/ProjectDetailsHandler'
describe('ProjectDetailsHandler', function () {
beforeEach(function () {
this.user = {
_id: ObjectId(),
_id: new ObjectId(),
email: 'user@example.com',
features: 'mock-features',
}
this.collaborator = {
_id: ObjectId(),
_id: new ObjectId(),
email: 'collaborator@example.com',
}
this.project = {
_id: ObjectId(),
_id: new ObjectId(),
name: 'project',
description: 'this is a great project',
something: 'should not exist',
@@ -13,7 +13,7 @@ const MODULE_PATH =
describe('ProjectEntityMongoUpdateHandler', function () {
beforeEach(function () {
this.doc = {
_id: ObjectId(),
_id: new ObjectId(),
name: 'test-doc.txt',
lines: ['hello', 'world'],
rev: 1234,
@@ -23,7 +23,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
fileSystem: '/test-doc.txt',
}
this.file = {
_id: ObjectId(),
_id: new ObjectId(),
name: 'something.jpg',
linkedFileData: { provider: 'url' },
hash: 'some-hash',
@@ -32,18 +32,18 @@ describe('ProjectEntityMongoUpdateHandler', function () {
fileSystem: '/something.png',
mongo: 'rootFolder.0.fileRefs.0',
}
this.subfolder = { _id: ObjectId(), name: 'test-subfolder' }
this.subfolder = { _id: new ObjectId(), name: 'test-subfolder' }
this.subfolderPath = {
fileSystem: '/test-folder/test-subfolder',
mongo: 'rootFolder.0.folders.0.folders.0',
}
this.notSubfolder = { _id: ObjectId(), name: 'test-folder-2' }
this.notSubfolder = { _id: new ObjectId(), name: 'test-folder-2' }
this.notSubfolderPath = {
fileSystem: '/test-folder-2/test-subfolder',
mongo: 'rootFolder.0.folders.0.folders.0',
}
this.folder = {
_id: ObjectId(),
_id: new ObjectId(),
name: 'test-folder',
folders: [this.subfolder],
}
@@ -52,7 +52,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
mongo: 'rootFolder.0.folders.0',
}
this.rootFolder = {
_id: ObjectId(),
_id: new ObjectId(),
folders: [this.folder],
docs: [this.doc],
fileRefs: [this.file],
@@ -62,7 +62,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
mongo: 'rootFolder.0',
}
this.project = {
_id: ObjectId(),
_id: new ObjectId(),
name: 'project name',
rootFolder: [this.rootFolder],
}
@@ -221,7 +221,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
describe('addDoc', function () {
beforeEach(async function () {
const doc = { _id: ObjectId(), name: 'other.txt' }
const doc = { _id: new ObjectId(), name: 'other.txt' }
this.ProjectMock.expects('findOneAndUpdate')
.withArgs(
{
@@ -261,7 +261,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
describe('addFile', function () {
beforeEach(function () {
this.newFile = { _id: ObjectId(), name: 'picture.jpg' }
this.newFile = { _id: new ObjectId(), name: 'picture.jpg' }
this.ProjectMock.expects('findOneAndUpdate')
.withArgs(
{
@@ -329,7 +329,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
beforeEach(async function () {
const folderName = 'New folder'
this.FolderModel.withArgs({ name: folderName }).returns({
_id: ObjectId(),
_id: new ObjectId(),
name: folderName,
})
this.ProjectMock.expects('findOneAndUpdate')
@@ -364,7 +364,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
describe('replaceFileWithNew', function () {
beforeEach(async function () {
const newFile = {
_id: ObjectId(),
_id: new ObjectId(),
name: 'some-other-file.png',
linkedFileData: { some: 'data' },
hash: 'some-hash',
@@ -460,7 +460,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
describe('when the path is a new folder at the top level', function () {
beforeEach(async function () {
this.newFolder = { _id: ObjectId(), name: 'new-folder' }
this.newFolder = { _id: new ObjectId(), name: 'new-folder' }
this.FolderModel.returns(this.newFolder)
this.exactCaseMatch = false
this.ProjectMock.expects('findOneAndUpdate')
@@ -504,7 +504,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
describe('adding a subfolder', function () {
beforeEach(async function () {
this.newFolder = { _id: ObjectId(), name: 'new-folder' }
this.newFolder = { _id: new ObjectId(), name: 'new-folder' }
this.FolderModel.returns(this.newFolder)
this.ProjectMock.expects('findOneAndUpdate')
.withArgs(
@@ -548,12 +548,12 @@ describe('ProjectEntityMongoUpdateHandler', function () {
describe('when mutliple folders are missing', async function () {
beforeEach(function () {
this.folder1 = { _id: ObjectId(), name: 'folder1' }
this.folder1 = { _id: new ObjectId(), name: 'folder1' }
this.folder1Path = {
fileSystem: '/test-folder/folder1',
mongo: 'rootFolder.0.folders.0.folders.0',
}
this.folder2 = { _id: ObjectId(), name: 'folder2' }
this.folder2 = { _id: new ObjectId(), name: 'folder2' }
this.folder2Path = {
fileSystem: '/test-folder/folder1/folder2',
mongo: 'rootFolder.0.folders.0.folders.0.folders.0',
@@ -865,7 +865,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
describe('updating the project', function () {
describe('when the parent folder is given', function () {
beforeEach(function () {
this.newFile = { _id: ObjectId(), name: 'new file.png' }
this.newFile = { _id: new ObjectId(), name: 'new file.png' }
this.ProjectMock.expects('findOneAndUpdate')
.withArgs(
{
@@ -927,7 +927,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
})
it('should error if element name contains invalid characters', async function () {
const file = { _id: ObjectId(), name: 'something*bad' }
const file = { _id: new ObjectId(), name: 'something*bad' }
await expect(
this.subject.promises._putElement(
this.project,
@@ -940,7 +940,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
it('should error if element name is too long', async function () {
const file = {
_id: ObjectId(),
_id: new ObjectId(),
name: 'long-'.repeat(1000) + 'something',
}
await expect(
@@ -955,7 +955,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
it('should error if the folder name is too long', async function () {
const file = {
_id: ObjectId(),
_id: new ObjectId(),
name: 'something',
}
this.ProjectLocator.promises.findElement
@@ -980,7 +980,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
;['file', 'doc', 'folder'].forEach(entityType => {
it(`should error if a ${entityType} already exists with the same name`, async function () {
const file = {
_id: ObjectId(),
_id: new ObjectId(),
name: this[entityType].name,
}
await expect(
@@ -998,7 +998,7 @@ describe('ProjectEntityMongoUpdateHandler', function () {
describe('when the parent folder is not given', function () {
it('should default to root folder insert', async function () {
this.newFile = { _id: ObjectId(), name: 'new file.png' }
this.newFile = { _id: new ObjectId(), name: 'new file.png' }
this.ProjectMock.expects('findOneAndUpdate')
.withArgs(
{ _id: this.project._id, 'rootFolder.0': { $exists: true } },
@@ -182,7 +182,7 @@ describe('ProjectEntityUpdateHandler', function () {
this.ranges = { mock: 'ranges' }
this.lastUpdatedAt = new Date().getTime()
this.lastUpdatedBy = 'fake-last-updater-id'
this.parentFolder = { _id: ObjectId() }
this.parentFolder = { _id: new ObjectId() }
this.DocstoreManager.isDocDeleted.yields(null, false)
this.ProjectGetter.getProject.yields(null, this.project)
this.ProjectLocator.findElement.yields(
@@ -1011,7 +1011,7 @@ describe('ProjectEntityUpdateHandler', function () {
describe('updating an existing file', function () {
beforeEach(function () {
this.existingFile = { _id: fileId, name: this.fileName, rev: 1 }
this.newFile = { _id: ObjectId(), name: this.fileName, rev: 3 }
this.newFile = { _id: new ObjectId(), name: this.fileName, rev: 3 }
this.folder = { _id: folderId, fileRefs: [this.existingFile], docs: [] }
this.ProjectLocator.findElement.yields(null, this.folder)
this.newProject = 'new-project-stub'
@@ -2634,7 +2634,7 @@ describe('ProjectEntityUpdateHandler', function () {
describe('_cleanUpDoc', function () {
beforeEach(function () {
this.doc = {
_id: ObjectId(),
_id: new ObjectId(),
name: 'test.tex',
}
this.path = '/path/to/doc'
@@ -2679,7 +2679,7 @@ describe('ProjectEntityUpdateHandler', function () {
describe('when the doc is not the root doc', function () {
beforeEach(function () {
this.project.rootDoc_id = ObjectId()
this.project.rootDoc_id = new ObjectId()
this.ProjectEntityUpdateHandler._cleanUpDoc(
this.project,
this.doc,
@@ -47,8 +47,8 @@ describe('ProjectHelper', function () {
describe('project.archived being an array', function () {
it('returns true if user id is found', function () {
this.project.archived = [
ObjectId('588f3ddae8ebc1bac07c9fa4'),
ObjectId('5c41deb2b4ca500153340809'),
new ObjectId('588f3ddae8ebc1bac07c9fa4'),
new ObjectId('5c41deb2b4ca500153340809'),
]
expect(
this.ProjectHelper.isArchived(this.project, this.user._id)
@@ -76,8 +76,8 @@ describe('ProjectHelper', function () {
describe('isTrashed', function () {
it('returns true if user id is found', function () {
this.project.trashed = [
ObjectId('588f3ddae8ebc1bac07c9fa4'),
ObjectId('5c41deb2b4ca500153340809'),
new ObjectId('588f3ddae8ebc1bac07c9fa4'),
new ObjectId('5c41deb2b4ca500153340809'),
]
expect(
this.ProjectHelper.isTrashed(this.project, this.user._id)
@@ -107,17 +107,17 @@ describe('ProjectHelper', function () {
const project = { archived: [] }
const result = this.ProjectHelper.calculateArchivedArray(
project,
ObjectId('5c922599cdb09e014aa7d499'),
new ObjectId('5c922599cdb09e014aa7d499'),
'ARCHIVE'
)
expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
expect(result).to.deep.equal([new ObjectId('5c922599cdb09e014aa7d499')])
})
it('returns an array without the current user id when unarchiving', function () {
const project = { archived: [ObjectId('5c922599cdb09e014aa7d499')] }
const project = { archived: [new ObjectId('5c922599cdb09e014aa7d499')] }
const result = this.ProjectHelper.calculateArchivedArray(
project,
ObjectId('5c922599cdb09e014aa7d499'),
new ObjectId('5c922599cdb09e014aa7d499'),
'UNARCHIVE'
)
expect(result).to.deep.equal([])
@@ -130,11 +130,13 @@ describe('ProjectHelper', function () {
archived: true,
owner_ref: this.user._id,
collaberator_refs: [
ObjectId('4f2cfb341eb5855a5b000f8b'),
ObjectId('5c45f3bd425ead01488675aa'),
new ObjectId('4f2cfb341eb5855a5b000f8b'),
new ObjectId('5c45f3bd425ead01488675aa'),
],
readOnly_refs: [new ObjectId('5c92243fcdb09e014aa7d487')],
tokenAccessReadAndWrite_refs: [
new ObjectId('5c922599cdb09e014aa7d499'),
],
readOnly_refs: [ObjectId('5c92243fcdb09e014aa7d487')],
tokenAccessReadAndWrite_refs: [ObjectId('5c922599cdb09e014aa7d499')],
tokenAccessReadOnly_refs: [],
}
@@ -145,10 +147,10 @@ describe('ProjectHelper', function () {
)
expect(result).to.deep.equal([
this.user._id,
ObjectId('4f2cfb341eb5855a5b000f8b'),
ObjectId('5c45f3bd425ead01488675aa'),
ObjectId('5c92243fcdb09e014aa7d487'),
ObjectId('5c922599cdb09e014aa7d499'),
new ObjectId('4f2cfb341eb5855a5b000f8b'),
new ObjectId('5c45f3bd425ead01488675aa'),
new ObjectId('5c92243fcdb09e014aa7d487'),
new ObjectId('5c922599cdb09e014aa7d499'),
])
})
@@ -157,12 +159,14 @@ describe('ProjectHelper', function () {
archived: true,
owner_ref: this.user._id,
collaberator_refs: [
ObjectId('4f2cfb341eb5855a5b000f8b'),
ObjectId('5c45f3bd425ead01488675aa'),
ObjectId('5c922599cdb09e014aa7d499'),
new ObjectId('4f2cfb341eb5855a5b000f8b'),
new ObjectId('5c45f3bd425ead01488675aa'),
new ObjectId('5c922599cdb09e014aa7d499'),
],
readOnly_refs: [new ObjectId('5c92243fcdb09e014aa7d487')],
tokenAccessReadAndWrite_refs: [
new ObjectId('5c922599cdb09e014aa7d499'),
],
readOnly_refs: [ObjectId('5c92243fcdb09e014aa7d487')],
tokenAccessReadAndWrite_refs: [ObjectId('5c922599cdb09e014aa7d499')],
tokenAccessReadOnly_refs: [],
}
@@ -172,10 +176,10 @@ describe('ProjectHelper', function () {
'UNARCHIVE'
)
expect(result).to.deep.equal([
ObjectId('4f2cfb341eb5855a5b000f8b'),
ObjectId('5c45f3bd425ead01488675aa'),
ObjectId('5c922599cdb09e014aa7d499'),
ObjectId('5c92243fcdb09e014aa7d487'),
new ObjectId('4f2cfb341eb5855a5b000f8b'),
new ObjectId('5c45f3bd425ead01488675aa'),
new ObjectId('5c922599cdb09e014aa7d499'),
new ObjectId('5c92243fcdb09e014aa7d487'),
])
})
})
@@ -185,17 +189,17 @@ describe('ProjectHelper', function () {
const project = { archived: false }
const result = this.ProjectHelper.calculateArchivedArray(
project,
ObjectId('5c922599cdb09e014aa7d499'),
new ObjectId('5c922599cdb09e014aa7d499'),
'ARCHIVE'
)
expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
expect(result).to.deep.equal([new ObjectId('5c922599cdb09e014aa7d499')])
})
it('returns an empty array when unarchiving', function () {
const project = { archived: false }
const result = this.ProjectHelper.calculateArchivedArray(
project,
ObjectId('5c922599cdb09e014aa7d499'),
new ObjectId('5c922599cdb09e014aa7d499'),
'UNARCHIVE'
)
expect(result).to.deep.equal([])
@@ -207,17 +211,17 @@ describe('ProjectHelper', function () {
const project = { archived: undefined }
const result = this.ProjectHelper.calculateArchivedArray(
project,
ObjectId('5c922599cdb09e014aa7d499'),
new ObjectId('5c922599cdb09e014aa7d499'),
'ARCHIVE'
)
expect(result).to.deep.equal([ObjectId('5c922599cdb09e014aa7d499')])
expect(result).to.deep.equal([new ObjectId('5c922599cdb09e014aa7d499')])
})
it('returns an empty array when unarchiving', function () {
const project = { archived: undefined }
const result = this.ProjectHelper.calculateArchivedArray(
project,
ObjectId('5c922599cdb09e014aa7d499'),
new ObjectId('5c922599cdb09e014aa7d499'),
'UNARCHIVE'
)
expect(result).to.deep.equal([])
@@ -12,10 +12,10 @@ const MODULE_PATH = path.join(
describe('ProjectListController', function () {
beforeEach(function () {
this.project_id = ObjectId('abcdefabcdefabcdefabcdef')
this.project_id = new ObjectId('abcdefabcdefabcdefabcdef')
this.user = {
_id: ObjectId('123456123456123456123456'),
_id: new ObjectId('123456123456123456123456'),
email: 'test@overleaf.com',
first_name: 'bjkdsjfk',
features: {},