ProjectDuplicatorTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. path: this.file0Path,
  103. file: this.newFile0,
  104. url: this.filestoreUrl,
  105. },
  106. {
  107. path: this.file1Path,
  108. file: this.newFile1,
  109. url: this.filestoreUrl,
  110. },
  111. {
  112. path: this.file2Path,
  113. file: this.newFile2,
  114. url: this.filestoreUrl,
  115. },
  116. ]
  117. this.Doc = sinon
  118. .stub()
  119. .callsFake(props => ({ _id: this.newDocId, ...props }))
  120. this.File = sinon
  121. .stub()
  122. .callsFake(props => ({ _id: this.newFileId, ...props }))
  123. this.DocstoreManager = {
  124. promises: {
  125. updateDoc: sinon.stub().resolves(),
  126. getAllDocs: sinon.stub().resolves(this.docContents),
  127. },
  128. }
  129. this.DocumentUpdaterHandler = {
  130. promises: {
  131. flushProjectToMongo: sinon.stub().resolves(),
  132. updateProjectStructure: sinon.stub().resolves(),
  133. },
  134. }
  135. this.FileStoreHandler = {
  136. promises: {
  137. copyFile: sinon.stub().resolves(this.filestoreUrl),
  138. },
  139. }
  140. this.HistoryManager = {
  141. promises: {
  142. copyBlob: sinon.stub().callsFake((historyId, newHistoryId, hash) => {
  143. if (hash === 'abcde') {
  144. return Promise.reject(new Error('copy blob error'))
  145. }
  146. return Promise.resolve()
  147. }),
  148. },
  149. }
  150. this.TagsHandler = {
  151. promises: {
  152. addProjectToTags: sinon.stub().resolves({
  153. _id: 'project-1',
  154. }),
  155. countTagsForProject: sinon.stub().resolves(1),
  156. },
  157. }
  158. this.ProjectCreationHandler = {
  159. promises: {
  160. createBlankProject: sinon.stub().resolves(this.newBlankProject),
  161. },
  162. }
  163. this.ProjectDeleter = {
  164. promises: {
  165. deleteProject: sinon.stub().resolves(),
  166. },
  167. }
  168. this.ProjectEntityMongoUpdateHandler = {
  169. promises: {
  170. createNewFolderStructure: sinon.stub().resolves(this.newProjectVersion),
  171. },
  172. }
  173. this.ProjectEntityUpdateHandler = {
  174. isPathValidForRootDoc: sinon.stub().returns(true),
  175. promises: {
  176. setRootDoc: sinon.stub().resolves(),
  177. },
  178. }
  179. this.ProjectGetter = {
  180. promises: {
  181. getProject: sinon
  182. .stub()
  183. .withArgs(this.project._id)
  184. .resolves(this.project),
  185. },
  186. }
  187. this.ProjectLocator = {
  188. promises: {
  189. findRootDoc: sinon.stub().resolves({
  190. element: this.rootDoc,
  191. path: { fileSystem: this.rootDocPath },
  192. }),
  193. findElementByPath: sinon
  194. .stub()
  195. .withArgs({
  196. project_id: this.newBlankProject._id,
  197. path: this.rootDocPath,
  198. exactCaseMatch: true,
  199. })
  200. .resolves({ element: this.doc0 }),
  201. },
  202. }
  203. this.ProjectOptionsHandler = {
  204. promises: {
  205. setCompiler: sinon.stub().resolves(),
  206. },
  207. }
  208. this.TpdsProjectFlusher = {
  209. promises: {
  210. flushProjectToTpds: sinon.stub().resolves(),
  211. },
  212. }
  213. this.ProjectDuplicator = SandboxedModule.require(MODULE_PATH, {
  214. requires: {
  215. '../../models/Doc': { Doc: this.Doc },
  216. '../../models/File': { File: this.File },
  217. '../Docstore/DocstoreManager': this.DocstoreManager,
  218. '../DocumentUpdater/DocumentUpdaterHandler':
  219. this.DocumentUpdaterHandler,
  220. '../FileStore/FileStoreHandler': this.FileStoreHandler,
  221. './ProjectCreationHandler': this.ProjectCreationHandler,
  222. './ProjectDeleter': this.ProjectDeleter,
  223. './ProjectEntityMongoUpdateHandler':
  224. this.ProjectEntityMongoUpdateHandler,
  225. './ProjectEntityUpdateHandler': this.ProjectEntityUpdateHandler,
  226. './ProjectGetter': this.ProjectGetter,
  227. './ProjectLocator': this.ProjectLocator,
  228. './ProjectOptionsHandler': this.ProjectOptionsHandler,
  229. '../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher,
  230. '../Tags/TagsHandler': this.TagsHandler,
  231. '../History/HistoryManager': this.HistoryManager,
  232. },
  233. })
  234. })
  235. describe('when the copy succeeds', function () {
  236. beforeEach(async function () {
  237. this.newProjectName = 'New project name'
  238. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  239. this.owner,
  240. this.project._id,
  241. this.newProjectName
  242. )
  243. })
  244. it('should flush the original project to mongo', function () {
  245. this.DocumentUpdaterHandler.promises.flushProjectToMongo.should.have.been.calledWith(
  246. this.project._id
  247. )
  248. })
  249. it('should copy docs to docstore', function () {
  250. for (const docLines of [this.doc0Lines, this.doc1Lines, this.doc2Lines]) {
  251. this.DocstoreManager.promises.updateDoc.should.have.been.calledWith(
  252. this.newProject._id.toString(),
  253. this.newDocId.toString(),
  254. docLines,
  255. 0,
  256. {}
  257. )
  258. }
  259. })
  260. it('should duplicate the files with hashes by copying the blobs in history v1', function () {
  261. for (const file of [this.file0, this.file2]) {
  262. this.HistoryManager.promises.copyBlob.should.have.been.calledWith(
  263. this.project.overleaf.history.id,
  264. this.newProject.overleaf.history.id,
  265. file.hash
  266. )
  267. }
  268. })
  269. it('should ignore any errors when copying the blobs in history v1', async function () {
  270. await expect(
  271. this.HistoryManager.promises.copyBlob(
  272. this.project.overleaf.history.id,
  273. this.newProject.overleaf.history.id,
  274. this.file0.hash
  275. )
  276. ).to.be.rejectedWith('copy blob error')
  277. })
  278. it('should not try to copy the blobs for any files without hashes', function () {
  279. for (const file of [this.file1]) {
  280. this.HistoryManager.promises.copyBlob.should.not.have.been.calledWith(
  281. this.project.overleaf.history.id,
  282. this.newProject.overleaf.history.id,
  283. file.hash
  284. )
  285. }
  286. })
  287. it('should copy files to the filestore', function () {
  288. for (const file of [this.file0, this.file1, this.file2]) {
  289. this.FileStoreHandler.promises.copyFile.should.have.been.calledWith(
  290. this.project._id,
  291. file._id,
  292. this.newProject._id,
  293. this.newFileId
  294. )
  295. }
  296. })
  297. it('should create a blank project', function () {
  298. this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
  299. this.owner._id,
  300. this.newProjectName
  301. )
  302. this.newProject._id.should.equal(this.newBlankProject._id)
  303. })
  304. it('should use the same compiler', function () {
  305. this.ProjectOptionsHandler.promises.setCompiler.should.have.been.calledWith(
  306. this.newProject._id,
  307. this.project.compiler
  308. )
  309. })
  310. it('should use the same root doc', function () {
  311. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.have.been.calledWith(
  312. this.newProject._id,
  313. this.rootFolder.docs[0]._id
  314. )
  315. })
  316. it('should not copy the collaborators or read only refs', function () {
  317. this.newProject.collaberator_refs.length.should.equal(0)
  318. this.newProject.readOnly_refs.length.should.equal(0)
  319. })
  320. it('should copy all documents and files', function () {
  321. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
  322. this.newProject._id,
  323. this.docEntries,
  324. this.fileEntries
  325. )
  326. })
  327. it('should notify document updater of changes', function () {
  328. this.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
  329. this.newProject._id,
  330. this.newProject.overleaf.history.id,
  331. this.owner._id,
  332. {
  333. newDocs: this.docEntries,
  334. newFiles: this.fileEntries,
  335. newProject: { version: this.newProjectVersion },
  336. },
  337. null
  338. )
  339. })
  340. it('should flush the project to TPDS', function () {
  341. this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
  342. this.newProject._id
  343. )
  344. })
  345. })
  346. describe('without a root doc', function () {
  347. beforeEach(async function () {
  348. this.ProjectLocator.promises.findRootDoc.resolves({
  349. element: null,
  350. path: null,
  351. })
  352. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  353. this.owner,
  354. this.project._id,
  355. 'Copy of project'
  356. )
  357. })
  358. it('should not set the root doc on the copy', function () {
  359. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
  360. .called
  361. })
  362. })
  363. describe('with an invalid root doc', function () {
  364. beforeEach(async function () {
  365. this.ProjectEntityUpdateHandler.isPathValidForRootDoc.returns(false)
  366. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  367. this.owner,
  368. this.project._id,
  369. 'Copy of project'
  370. )
  371. })
  372. it('should not set the root doc on the copy', function () {
  373. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
  374. .called
  375. })
  376. })
  377. describe('when there is an error', function () {
  378. beforeEach(async function () {
  379. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.rejects()
  380. await expect(
  381. this.ProjectDuplicator.promises.duplicate(
  382. this.owner,
  383. this.project._id,
  384. ''
  385. )
  386. ).to.be.rejected
  387. })
  388. it('should delete the broken cloned project', function () {
  389. this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
  390. this.newBlankProject._id
  391. )
  392. })
  393. it('should not delete the original project', function () {
  394. this.ProjectDeleter.promises.deleteProject.should.not.have.been.calledWith(
  395. this.project._id
  396. )
  397. })
  398. })
  399. })