ProjectDuplicatorTests.js 14 KB

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