ProjectDuplicatorTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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' }
  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. }
  50. this.doc0Path = '/rootDocHere'
  51. this.doc1Path = '/level1folder/level1folderDocName'
  52. this.doc2Path = '/level1folder/level2folderName/level2folderDocName'
  53. this.file0Path = '/file0'
  54. this.file1Path = '/level1folder/file1'
  55. this.file2Path = '/level1folder/level2folderName/file2'
  56. this.docContents = [
  57. { _id: this.doc0._id, lines: this.doc0Lines },
  58. { _id: this.doc1._id, lines: this.doc1Lines },
  59. { _id: this.doc2._id, lines: this.doc2Lines },
  60. ]
  61. this.rootDoc = this.doc0
  62. this.rootDocPath = '/rootDocHere'
  63. this.owner = { _id: 'this_is_the_owner' }
  64. this.newBlankProject = {
  65. _id: 'new_project_id',
  66. overleaf: { history: { id: 339123 } },
  67. readOnly_refs: [],
  68. collaberator_refs: [],
  69. rootFolder: [{ _id: 'new_root_folder_id' }],
  70. }
  71. this.newFolder = { _id: 'newFolderId' }
  72. this.filestoreUrl = 'filestore-url'
  73. this.newProjectVersion = 2
  74. this.newDocId = new ObjectId()
  75. this.newFileId = new ObjectId()
  76. this.newDoc0 = { ...this.doc0, _id: this.newDocId }
  77. this.newDoc1 = { ...this.doc1, _id: this.newDocId }
  78. this.newDoc2 = { ...this.doc2, _id: this.newDocId }
  79. this.newFile0 = { ...this.file0, _id: this.newFileId }
  80. this.newFile1 = { ...this.file1, _id: this.newFileId }
  81. this.newFile2 = { ...this.file2, _id: this.newFileId }
  82. this.docEntries = [
  83. {
  84. path: this.doc0Path,
  85. doc: this.newDoc0,
  86. docLines: this.doc0Lines.join('\n'),
  87. },
  88. {
  89. path: this.doc1Path,
  90. doc: this.newDoc1,
  91. docLines: this.doc1Lines.join('\n'),
  92. },
  93. {
  94. path: this.doc2Path,
  95. doc: this.newDoc2,
  96. docLines: this.doc2Lines.join('\n'),
  97. },
  98. ]
  99. this.fileEntries = [
  100. {
  101. path: this.file0Path,
  102. file: this.newFile0,
  103. url: this.filestoreUrl,
  104. },
  105. {
  106. path: this.file1Path,
  107. file: this.newFile1,
  108. url: this.filestoreUrl,
  109. },
  110. {
  111. path: this.file2Path,
  112. file: this.newFile2,
  113. url: this.filestoreUrl,
  114. },
  115. ]
  116. this.Doc = sinon
  117. .stub()
  118. .callsFake(props => ({ _id: this.newDocId, ...props }))
  119. this.File = sinon
  120. .stub()
  121. .callsFake(props => ({ _id: this.newFileId, ...props }))
  122. this.DocstoreManager = {
  123. promises: {
  124. updateDoc: sinon.stub().resolves(),
  125. getAllDocs: sinon.stub().resolves(this.docContents),
  126. },
  127. }
  128. this.DocumentUpdaterHandler = {
  129. promises: {
  130. flushProjectToMongo: sinon.stub().resolves(),
  131. updateProjectStructure: sinon.stub().resolves(),
  132. },
  133. }
  134. this.FileStoreHandler = {
  135. promises: {
  136. copyFile: sinon.stub().resolves(this.filestoreUrl),
  137. },
  138. }
  139. this.TagsHandler = {
  140. promises: {
  141. addProjectToTags: sinon.stub().resolves({
  142. _id: 'project-1',
  143. }),
  144. countTagsForProject: sinon.stub().resolves(1),
  145. },
  146. }
  147. this.ProjectCreationHandler = {
  148. promises: {
  149. createBlankProject: sinon.stub().resolves(this.newBlankProject),
  150. },
  151. }
  152. this.ProjectDeleter = {
  153. promises: {
  154. deleteProject: sinon.stub().resolves(),
  155. },
  156. }
  157. this.ProjectEntityMongoUpdateHandler = {
  158. promises: {
  159. createNewFolderStructure: sinon.stub().resolves(this.newProjectVersion),
  160. },
  161. }
  162. this.ProjectEntityUpdateHandler = {
  163. isPathValidForRootDoc: sinon.stub().returns(true),
  164. promises: {
  165. setRootDoc: sinon.stub().resolves(),
  166. },
  167. }
  168. this.ProjectGetter = {
  169. promises: {
  170. getProject: sinon
  171. .stub()
  172. .withArgs(this.project._id)
  173. .resolves(this.project),
  174. },
  175. }
  176. this.ProjectLocator = {
  177. promises: {
  178. findRootDoc: sinon.stub().resolves({
  179. element: this.rootDoc,
  180. path: { fileSystem: this.rootDocPath },
  181. }),
  182. findElementByPath: sinon
  183. .stub()
  184. .withArgs({
  185. project_id: this.newBlankProject._id,
  186. path: this.rootDocPath,
  187. exactCaseMatch: true,
  188. })
  189. .resolves({ element: this.doc0 }),
  190. },
  191. }
  192. this.ProjectOptionsHandler = {
  193. promises: {
  194. setCompiler: sinon.stub().resolves(),
  195. },
  196. }
  197. this.TpdsProjectFlusher = {
  198. promises: {
  199. flushProjectToTpds: sinon.stub().resolves(),
  200. },
  201. }
  202. this.ProjectDuplicator = SandboxedModule.require(MODULE_PATH, {
  203. requires: {
  204. '../../models/Doc': { Doc: this.Doc },
  205. '../../models/File': { File: this.File },
  206. '../Docstore/DocstoreManager': this.DocstoreManager,
  207. '../DocumentUpdater/DocumentUpdaterHandler':
  208. this.DocumentUpdaterHandler,
  209. '../FileStore/FileStoreHandler': this.FileStoreHandler,
  210. './ProjectCreationHandler': this.ProjectCreationHandler,
  211. './ProjectDeleter': this.ProjectDeleter,
  212. './ProjectEntityMongoUpdateHandler':
  213. this.ProjectEntityMongoUpdateHandler,
  214. './ProjectEntityUpdateHandler': this.ProjectEntityUpdateHandler,
  215. './ProjectGetter': this.ProjectGetter,
  216. './ProjectLocator': this.ProjectLocator,
  217. './ProjectOptionsHandler': this.ProjectOptionsHandler,
  218. '../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher,
  219. '../Tags/TagsHandler': this.TagsHandler,
  220. },
  221. })
  222. })
  223. describe('when the copy succeeds', function () {
  224. beforeEach(async function () {
  225. this.newProjectName = 'New project name'
  226. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  227. this.owner,
  228. this.project._id,
  229. this.newProjectName
  230. )
  231. })
  232. it('should flush the original project to mongo', function () {
  233. this.DocumentUpdaterHandler.promises.flushProjectToMongo.should.have.been.calledWith(
  234. this.project._id
  235. )
  236. })
  237. it('should copy docs to docstore', function () {
  238. for (const docLines of [this.doc0Lines, this.doc1Lines, this.doc2Lines]) {
  239. this.DocstoreManager.promises.updateDoc.should.have.been.calledWith(
  240. this.newProject._id.toString(),
  241. this.newDocId.toString(),
  242. docLines,
  243. 0,
  244. {}
  245. )
  246. }
  247. })
  248. it('should copy files to the filestore', function () {
  249. for (const file of [this.file0, this.file1, this.file2]) {
  250. this.FileStoreHandler.promises.copyFile.should.have.been.calledWith(
  251. this.project._id,
  252. file._id,
  253. this.newProject._id,
  254. this.newFileId
  255. )
  256. }
  257. })
  258. it('should create a blank project', function () {
  259. this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
  260. this.owner._id,
  261. this.newProjectName
  262. )
  263. this.newProject._id.should.equal(this.newBlankProject._id)
  264. })
  265. it('should use the same compiler', function () {
  266. this.ProjectOptionsHandler.promises.setCompiler.should.have.been.calledWith(
  267. this.newProject._id,
  268. this.project.compiler
  269. )
  270. })
  271. it('should use the same root doc', function () {
  272. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.have.been.calledWith(
  273. this.newProject._id,
  274. this.rootFolder.docs[0]._id
  275. )
  276. })
  277. it('should not copy the collaborators or read only refs', function () {
  278. this.newProject.collaberator_refs.length.should.equal(0)
  279. this.newProject.readOnly_refs.length.should.equal(0)
  280. })
  281. it('should copy all documents and files', function () {
  282. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
  283. this.newProject._id,
  284. this.docEntries,
  285. this.fileEntries
  286. )
  287. })
  288. it('should notify document updater of changes', function () {
  289. this.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
  290. this.newProject._id,
  291. this.newProject.overleaf.history.id,
  292. this.owner._id,
  293. {
  294. newDocs: this.docEntries,
  295. newFiles: this.fileEntries,
  296. newProject: { version: this.newProjectVersion },
  297. },
  298. null
  299. )
  300. })
  301. it('should flush the project to TPDS', function () {
  302. this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
  303. this.newProject._id
  304. )
  305. })
  306. })
  307. describe('without a root doc', function () {
  308. beforeEach(async function () {
  309. this.ProjectLocator.promises.findRootDoc.resolves({
  310. element: null,
  311. path: null,
  312. })
  313. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  314. this.owner,
  315. this.project._id,
  316. 'Copy of project'
  317. )
  318. })
  319. it('should not set the root doc on the copy', function () {
  320. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
  321. .called
  322. })
  323. })
  324. describe('with an invalid root doc', function () {
  325. beforeEach(async function () {
  326. this.ProjectEntityUpdateHandler.isPathValidForRootDoc.returns(false)
  327. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  328. this.owner,
  329. this.project._id,
  330. 'Copy of project'
  331. )
  332. })
  333. it('should not set the root doc on the copy', function () {
  334. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
  335. .called
  336. })
  337. })
  338. describe('when there is an error', function () {
  339. beforeEach(async function () {
  340. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.rejects()
  341. await expect(
  342. this.ProjectDuplicator.promises.duplicate(
  343. this.owner,
  344. this.project._id,
  345. ''
  346. )
  347. ).to.be.rejected
  348. })
  349. it('should delete the broken cloned project', function () {
  350. this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
  351. this.newBlankProject._id
  352. )
  353. })
  354. it('should not delete the original project', function () {
  355. this.ProjectDeleter.promises.deleteProject.should.not.have.been.calledWith(
  356. this.project._id
  357. )
  358. })
  359. })
  360. })