ProjectDuplicatorTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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.ProjectDuplicator = SandboxedModule.require(MODULE_PATH, {
  217. requires: {
  218. '../../models/Doc': { Doc: this.Doc },
  219. '../../models/File': { File: this.File },
  220. '../Docstore/DocstoreManager': this.DocstoreManager,
  221. '../DocumentUpdater/DocumentUpdaterHandler':
  222. this.DocumentUpdaterHandler,
  223. '../FileStore/FileStoreHandler': this.FileStoreHandler,
  224. './ProjectCreationHandler': this.ProjectCreationHandler,
  225. './ProjectDeleter': this.ProjectDeleter,
  226. './ProjectEntityMongoUpdateHandler':
  227. this.ProjectEntityMongoUpdateHandler,
  228. './ProjectEntityUpdateHandler': this.ProjectEntityUpdateHandler,
  229. './ProjectGetter': this.ProjectGetter,
  230. './ProjectLocator': this.ProjectLocator,
  231. './ProjectOptionsHandler': this.ProjectOptionsHandler,
  232. '../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher,
  233. '../Tags/TagsHandler': this.TagsHandler,
  234. '../History/HistoryManager': this.HistoryManager,
  235. },
  236. })
  237. })
  238. describe('when the copy succeeds', function () {
  239. beforeEach(async function () {
  240. this.newProjectName = 'New project name'
  241. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  242. this.owner,
  243. this.project._id,
  244. this.newProjectName
  245. )
  246. })
  247. it('should flush the original project to mongo', function () {
  248. this.DocumentUpdaterHandler.promises.flushProjectToMongo.should.have.been.calledWith(
  249. this.project._id
  250. )
  251. })
  252. it('should copy docs to docstore', function () {
  253. for (const docLines of [this.doc0Lines, this.doc1Lines, this.doc2Lines]) {
  254. this.DocstoreManager.promises.updateDoc.should.have.been.calledWith(
  255. this.newProject._id.toString(),
  256. this.newDocId.toString(),
  257. docLines,
  258. 0,
  259. {}
  260. )
  261. }
  262. })
  263. it('should duplicate the files with hashes by copying the blobs in history v1', function () {
  264. for (const file of [this.file0, this.file2]) {
  265. this.HistoryManager.promises.copyBlob.should.have.been.calledWith(
  266. this.project.overleaf.history.id,
  267. this.newProject.overleaf.history.id,
  268. file.hash
  269. )
  270. }
  271. })
  272. it('should ignore any errors when copying the blobs in history v1', async function () {
  273. await expect(
  274. this.HistoryManager.promises.copyBlob(
  275. this.project.overleaf.history.id,
  276. this.newProject.overleaf.history.id,
  277. this.file0.hash
  278. )
  279. ).to.be.rejectedWith('copy blob error')
  280. })
  281. it('should not try to copy the blobs for any files without hashes', function () {
  282. for (const file of [this.file1]) {
  283. this.HistoryManager.promises.copyBlob.should.not.have.been.calledWith(
  284. this.project.overleaf.history.id,
  285. this.newProject.overleaf.history.id,
  286. file.hash
  287. )
  288. }
  289. })
  290. it('should copy files to the filestore', function () {
  291. for (const file of [this.file0, this.file1]) {
  292. this.FileStoreHandler.promises.copyFile.should.have.been.calledWith(
  293. this.project._id,
  294. file._id,
  295. this.newProject._id,
  296. this.newFileId
  297. )
  298. }
  299. })
  300. it('should not copy files that have been sent to history-v1 to the filestore', function () {
  301. this.FileStoreHandler.promises.copyFile.should.not.have.been.calledWith(
  302. this.project._id,
  303. this.file2._id,
  304. this.newProject._id,
  305. this.newFileId
  306. )
  307. })
  308. it('should create a blank project', function () {
  309. this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
  310. this.owner._id,
  311. this.newProjectName
  312. )
  313. this.newProject._id.should.equal(this.newBlankProject._id)
  314. })
  315. it('should use the same compiler', function () {
  316. this.ProjectOptionsHandler.promises.setCompiler.should.have.been.calledWith(
  317. this.newProject._id,
  318. this.project.compiler
  319. )
  320. })
  321. it('should use the same root doc', function () {
  322. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.have.been.calledWith(
  323. this.newProject._id,
  324. this.rootFolder.docs[0]._id
  325. )
  326. })
  327. it('should not copy the collaborators or read only refs', function () {
  328. this.newProject.collaberator_refs.length.should.equal(0)
  329. this.newProject.readOnly_refs.length.should.equal(0)
  330. })
  331. it('should copy all documents and files', function () {
  332. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
  333. this.newProject._id,
  334. this.docEntries,
  335. this.fileEntries
  336. )
  337. })
  338. it('should notify document updater of changes', function () {
  339. this.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
  340. this.newProject._id,
  341. this.newProject.overleaf.history.id,
  342. this.owner._id,
  343. {
  344. newDocs: this.docEntries,
  345. newFiles: this.fileEntries,
  346. newProject: { version: this.newProjectVersion },
  347. },
  348. null
  349. )
  350. })
  351. it('should flush the project to TPDS', function () {
  352. this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
  353. this.newProject._id
  354. )
  355. })
  356. })
  357. describe('without a root doc', function () {
  358. beforeEach(async function () {
  359. this.ProjectLocator.promises.findRootDoc.resolves({
  360. element: null,
  361. path: null,
  362. })
  363. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  364. this.owner,
  365. this.project._id,
  366. 'Copy of project'
  367. )
  368. })
  369. it('should not set the root doc on the copy', function () {
  370. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
  371. .called
  372. })
  373. })
  374. describe('with an invalid root doc', function () {
  375. beforeEach(async function () {
  376. this.ProjectEntityUpdateHandler.isPathValidForRootDoc.returns(false)
  377. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  378. this.owner,
  379. this.project._id,
  380. 'Copy of project'
  381. )
  382. })
  383. it('should not set the root doc on the copy', function () {
  384. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
  385. .called
  386. })
  387. })
  388. describe('when there is an error', function () {
  389. beforeEach(async function () {
  390. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.rejects()
  391. await expect(
  392. this.ProjectDuplicator.promises.duplicate(
  393. this.owner,
  394. this.project._id,
  395. ''
  396. )
  397. ).to.be.rejected
  398. })
  399. it('should delete the broken cloned project', function () {
  400. this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
  401. this.newBlankProject._id
  402. )
  403. })
  404. it('should not delete the original project', function () {
  405. this.ProjectDeleter.promises.deleteProject.should.not.have.been.calledWith(
  406. this.project._id
  407. )
  408. })
  409. })
  410. })