ProjectDuplicatorTests.js 11 KB

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