ProjectDuplicatorTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. const { expect } = require('chai')
  2. const sinon = require('sinon')
  3. const SandboxedModule = require('sandboxed-module')
  4. const { ObjectId } = require('mongodb-legacy')
  5. const MODULE_PATH = '../../../../app/src/Features/Project/ProjectDuplicator.js'
  6. describe('ProjectDuplicator', function () {
  7. beforeEach(function () {
  8. this.doc0 = { _id: 'doc0_id', name: 'rootDocHere' }
  9. this.doc1 = { _id: 'doc1_id', name: 'level1folderDocName' }
  10. this.doc2 = { _id: 'doc2_id', name: 'level2folderDocName' }
  11. this.doc0Lines = ['zero']
  12. this.doc1Lines = ['one']
  13. this.doc2Lines = ['two']
  14. this.file0 = { name: 'file0', _id: 'file0', hash: 'abcde' }
  15. this.file1 = { name: 'file1', _id: 'file1' }
  16. this.file2 = {
  17. name: 'file2',
  18. _id: 'file2',
  19. created: '2024-07-05T14:18:31.401+00:00',
  20. linkedFileData: { provider: 'url' },
  21. hash: '123456',
  22. }
  23. this.level2folder = {
  24. name: 'level2folderName',
  25. _id: 'level2folderId',
  26. docs: [this.doc2, undefined],
  27. folders: [],
  28. fileRefs: [this.file2],
  29. }
  30. this.level1folder = {
  31. name: 'level1folder',
  32. _id: 'level1folderId',
  33. docs: [this.doc1],
  34. folders: [this.level2folder],
  35. fileRefs: [this.file1, null], // the null is intentional to test null docs/files
  36. }
  37. this.rootFolder = {
  38. name: 'rootFolder',
  39. _id: 'rootFolderId',
  40. docs: [this.doc0],
  41. folders: [this.level1folder, {}],
  42. fileRefs: [this.file0],
  43. }
  44. this.project = {
  45. _id: 'this_is_the_old_project_id',
  46. rootDoc_id: this.doc0._id,
  47. rootFolder: [this.rootFolder],
  48. compiler: 'this_is_a_Compiler',
  49. overleaf: { history: { id: 123456 } },
  50. }
  51. this.doc0Path = '/rootDocHere'
  52. this.doc1Path = '/level1folder/level1folderDocName'
  53. this.doc2Path = '/level1folder/level2folderName/level2folderDocName'
  54. this.file0Path = '/file0'
  55. this.file1Path = '/level1folder/file1'
  56. this.file2Path = '/level1folder/level2folderName/file2'
  57. this.docContents = [
  58. { _id: this.doc0._id, lines: this.doc0Lines },
  59. { _id: this.doc1._id, lines: this.doc1Lines },
  60. { _id: this.doc2._id, lines: this.doc2Lines },
  61. ]
  62. this.rootDoc = this.doc0
  63. this.rootDocPath = '/rootDocHere'
  64. this.owner = { _id: 'this_is_the_owner' }
  65. this.newBlankProject = {
  66. _id: 'new_project_id',
  67. overleaf: { history: { id: 339123 } },
  68. readOnly_refs: [],
  69. collaberator_refs: [],
  70. rootFolder: [{ _id: 'new_root_folder_id' }],
  71. }
  72. this.newFolder = { _id: 'newFolderId' }
  73. this.filestoreUrl = 'filestore-url'
  74. this.newProjectVersion = 2
  75. this.newDocId = new ObjectId()
  76. this.newFileId = new ObjectId()
  77. this.newDoc0 = { ...this.doc0, _id: this.newDocId }
  78. this.newDoc1 = { ...this.doc1, _id: this.newDocId }
  79. this.newDoc2 = { ...this.doc2, _id: this.newDocId }
  80. this.newFile0 = { ...this.file0, _id: this.newFileId }
  81. this.newFile1 = { ...this.file1, _id: this.newFileId }
  82. this.newFile2 = { ...this.file2, _id: this.newFileId }
  83. this.docEntries = [
  84. {
  85. path: this.doc0Path,
  86. doc: this.newDoc0,
  87. docLines: this.doc0Lines.join('\n'),
  88. },
  89. {
  90. path: this.doc1Path,
  91. doc: this.newDoc1,
  92. docLines: this.doc1Lines.join('\n'),
  93. },
  94. {
  95. path: this.doc2Path,
  96. doc: this.newDoc2,
  97. docLines: this.doc2Lines.join('\n'),
  98. },
  99. ]
  100. this.fileEntries = [
  101. {
  102. createdBlob: false,
  103. path: this.file0Path,
  104. file: this.newFile0,
  105. url: this.filestoreUrl,
  106. },
  107. {
  108. createdBlob: false,
  109. path: this.file1Path,
  110. file: this.newFile1,
  111. url: this.filestoreUrl,
  112. },
  113. {
  114. createdBlob: true,
  115. path: this.file2Path,
  116. file: this.newFile2,
  117. url: null,
  118. },
  119. ]
  120. this.Doc = sinon
  121. .stub()
  122. .callsFake(props => ({ _id: this.newDocId, ...props }))
  123. this.File = sinon
  124. .stub()
  125. .callsFake(props => ({ _id: this.newFileId, ...props }))
  126. this.DocstoreManager = {
  127. promises: {
  128. updateDoc: sinon.stub().resolves(),
  129. getAllDocs: sinon.stub().resolves(this.docContents),
  130. },
  131. }
  132. this.DocumentUpdaterHandler = {
  133. promises: {
  134. flushProjectToMongo: sinon.stub().resolves(),
  135. updateProjectStructure: sinon.stub().resolves(),
  136. },
  137. }
  138. this.FileStoreHandler = {
  139. promises: {
  140. copyFile: sinon.stub().resolves(this.filestoreUrl),
  141. },
  142. }
  143. this.HistoryManager = {
  144. promises: {
  145. copyBlob: sinon.stub().callsFake((historyId, newHistoryId, hash) => {
  146. if (hash === 'abcde') {
  147. return Promise.reject(new Error('copy blob error'))
  148. }
  149. return Promise.resolve()
  150. }),
  151. },
  152. }
  153. this.TagsHandler = {
  154. promises: {
  155. addProjectToTags: sinon.stub().resolves({
  156. _id: 'project-1',
  157. }),
  158. countTagsForProject: sinon.stub().resolves(1),
  159. },
  160. }
  161. this.ProjectCreationHandler = {
  162. promises: {
  163. createBlankProject: sinon.stub().resolves(this.newBlankProject),
  164. },
  165. }
  166. this.ProjectDeleter = {
  167. promises: {
  168. deleteProject: sinon.stub().resolves(),
  169. },
  170. }
  171. this.ProjectEntityMongoUpdateHandler = {
  172. promises: {
  173. createNewFolderStructure: sinon.stub().resolves(this.newProjectVersion),
  174. },
  175. }
  176. this.ProjectEntityUpdateHandler = {
  177. isPathValidForRootDoc: sinon.stub().returns(true),
  178. promises: {
  179. setRootDoc: sinon.stub().resolves(),
  180. },
  181. }
  182. this.ProjectGetter = {
  183. promises: {
  184. getProject: sinon
  185. .stub()
  186. .withArgs(this.project._id)
  187. .resolves(this.project),
  188. },
  189. }
  190. this.ProjectLocator = {
  191. promises: {
  192. findRootDoc: sinon.stub().resolves({
  193. element: this.rootDoc,
  194. path: { fileSystem: this.rootDocPath },
  195. }),
  196. findElementByPath: sinon
  197. .stub()
  198. .withArgs({
  199. project_id: this.newBlankProject._id,
  200. path: this.rootDocPath,
  201. exactCaseMatch: true,
  202. })
  203. .resolves({ element: this.doc0 }),
  204. },
  205. }
  206. this.ProjectOptionsHandler = {
  207. promises: {
  208. setCompiler: sinon.stub().resolves(),
  209. },
  210. }
  211. this.TpdsProjectFlusher = {
  212. promises: {
  213. flushProjectToTpds: sinon.stub().resolves(),
  214. },
  215. }
  216. this.Features = {
  217. hasFeature: sinon.stub().withArgs('project-history-blobs').returns(true),
  218. }
  219. this.ProjectDuplicator = SandboxedModule.require(MODULE_PATH, {
  220. requires: {
  221. '../../models/Doc': { Doc: this.Doc },
  222. '../../models/File': { File: this.File },
  223. '../Docstore/DocstoreManager': this.DocstoreManager,
  224. '../DocumentUpdater/DocumentUpdaterHandler':
  225. this.DocumentUpdaterHandler,
  226. '../FileStore/FileStoreHandler': this.FileStoreHandler,
  227. './ProjectCreationHandler': this.ProjectCreationHandler,
  228. './ProjectDeleter': this.ProjectDeleter,
  229. './ProjectEntityMongoUpdateHandler':
  230. this.ProjectEntityMongoUpdateHandler,
  231. './ProjectEntityUpdateHandler': this.ProjectEntityUpdateHandler,
  232. './ProjectGetter': this.ProjectGetter,
  233. './ProjectLocator': this.ProjectLocator,
  234. './ProjectOptionsHandler': this.ProjectOptionsHandler,
  235. '../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher,
  236. '../Tags/TagsHandler': this.TagsHandler,
  237. '../History/HistoryManager': this.HistoryManager,
  238. '../../infrastructure/Features': this.Features,
  239. },
  240. })
  241. })
  242. describe('when the copy succeeds', function () {
  243. beforeEach(async function () {
  244. this.newProjectName = 'New project name'
  245. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  246. this.owner,
  247. this.project._id,
  248. this.newProjectName
  249. )
  250. })
  251. it('should flush the original project to mongo', function () {
  252. this.DocumentUpdaterHandler.promises.flushProjectToMongo.should.have.been.calledWith(
  253. this.project._id
  254. )
  255. })
  256. it('should copy docs to docstore', function () {
  257. for (const docLines of [this.doc0Lines, this.doc1Lines, this.doc2Lines]) {
  258. this.DocstoreManager.promises.updateDoc.should.have.been.calledWith(
  259. this.newProject._id.toString(),
  260. this.newDocId.toString(),
  261. docLines,
  262. 0,
  263. {}
  264. )
  265. }
  266. })
  267. it('should duplicate the files with hashes by copying the blobs in history v1', function () {
  268. for (const file of [this.file0, this.file2]) {
  269. this.HistoryManager.promises.copyBlob.should.have.been.calledWith(
  270. this.project.overleaf.history.id,
  271. this.newProject.overleaf.history.id,
  272. file.hash
  273. )
  274. }
  275. })
  276. it('should ignore any errors when copying the blobs in history v1', async function () {
  277. await expect(
  278. this.HistoryManager.promises.copyBlob(
  279. this.project.overleaf.history.id,
  280. this.newProject.overleaf.history.id,
  281. this.file0.hash
  282. )
  283. ).to.be.rejectedWith('copy blob error')
  284. })
  285. it('should not try to copy the blobs for any files without hashes', function () {
  286. for (const file of [this.file1]) {
  287. this.HistoryManager.promises.copyBlob.should.not.have.been.calledWith(
  288. this.project.overleaf.history.id,
  289. this.newProject.overleaf.history.id,
  290. file.hash
  291. )
  292. }
  293. })
  294. it('should copy files to the filestore', function () {
  295. for (const file of [this.file0, this.file1]) {
  296. this.FileStoreHandler.promises.copyFile.should.have.been.calledWith(
  297. this.project._id,
  298. file._id,
  299. this.newProject._id,
  300. this.newFileId
  301. )
  302. }
  303. })
  304. it('should not copy files that have been sent to history-v1 to the filestore', function () {
  305. this.FileStoreHandler.promises.copyFile.should.not.have.been.calledWith(
  306. this.project._id,
  307. this.file2._id,
  308. this.newProject._id,
  309. this.newFileId
  310. )
  311. })
  312. it('should create a blank project', function () {
  313. this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
  314. this.owner._id,
  315. this.newProjectName
  316. )
  317. this.newProject._id.should.equal(this.newBlankProject._id)
  318. })
  319. it('should use the same compiler', function () {
  320. this.ProjectOptionsHandler.promises.setCompiler.should.have.been.calledWith(
  321. this.newProject._id,
  322. this.project.compiler
  323. )
  324. })
  325. it('should use the same root doc', function () {
  326. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.have.been.calledWith(
  327. this.newProject._id,
  328. this.rootFolder.docs[0]._id
  329. )
  330. })
  331. it('should not copy the collaborators or read only refs', function () {
  332. this.newProject.collaberator_refs.length.should.equal(0)
  333. this.newProject.readOnly_refs.length.should.equal(0)
  334. })
  335. it('should copy all documents and files', function () {
  336. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
  337. this.newProject._id,
  338. this.docEntries,
  339. this.fileEntries
  340. )
  341. })
  342. it('should notify document updater of changes', function () {
  343. this.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
  344. this.newProject._id,
  345. this.newProject.overleaf.history.id,
  346. this.owner._id,
  347. {
  348. newDocs: this.docEntries,
  349. newFiles: this.fileEntries,
  350. newProject: { version: this.newProjectVersion },
  351. },
  352. null
  353. )
  354. })
  355. it('should flush the project to TPDS', function () {
  356. this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
  357. this.newProject._id
  358. )
  359. })
  360. })
  361. describe('without a root doc', function () {
  362. beforeEach(async function () {
  363. this.ProjectLocator.promises.findRootDoc.resolves({
  364. element: null,
  365. path: null,
  366. })
  367. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  368. this.owner,
  369. this.project._id,
  370. 'Copy of project'
  371. )
  372. })
  373. it('should not set the root doc on the copy', function () {
  374. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
  375. .called
  376. })
  377. })
  378. describe('with an invalid root doc', function () {
  379. beforeEach(async function () {
  380. this.ProjectEntityUpdateHandler.isPathValidForRootDoc.returns(false)
  381. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  382. this.owner,
  383. this.project._id,
  384. 'Copy of project'
  385. )
  386. })
  387. it('should not set the root doc on the copy', function () {
  388. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
  389. .called
  390. })
  391. })
  392. describe('when there is an error', function () {
  393. beforeEach(async function () {
  394. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.rejects()
  395. await expect(
  396. this.ProjectDuplicator.promises.duplicate(
  397. this.owner,
  398. this.project._id,
  399. ''
  400. )
  401. ).to.be.rejected
  402. })
  403. it('should delete the broken cloned project', function () {
  404. this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
  405. this.newBlankProject._id
  406. )
  407. })
  408. it('should not delete the original project', function () {
  409. this.ProjectDeleter.promises.deleteProject.should.not.have.been.calledWith(
  410. this.project._id
  411. )
  412. })
  413. })
  414. })