ProjectDuplicatorTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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.TagsHandler = {
  139. promises: {
  140. addProjectToTags: sinon.stub().resolves({
  141. _id: 'project-1',
  142. }),
  143. countTagsForProject: sinon.stub().resolves(1),
  144. },
  145. }
  146. this.ProjectCreationHandler = {
  147. promises: {
  148. createBlankProject: sinon.stub().resolves(this.newBlankProject),
  149. },
  150. }
  151. this.ProjectDeleter = {
  152. promises: {
  153. deleteProject: sinon.stub().resolves(),
  154. },
  155. }
  156. this.ProjectEntityMongoUpdateHandler = {
  157. promises: {
  158. createNewFolderStructure: sinon.stub().resolves(this.newProjectVersion),
  159. },
  160. }
  161. this.ProjectEntityUpdateHandler = {
  162. isPathValidForRootDoc: sinon.stub().returns(true),
  163. promises: {
  164. setRootDoc: sinon.stub().resolves(),
  165. },
  166. }
  167. this.ProjectGetter = {
  168. promises: {
  169. getProject: sinon
  170. .stub()
  171. .withArgs(this.project._id)
  172. .resolves(this.project),
  173. },
  174. }
  175. this.ProjectLocator = {
  176. promises: {
  177. findRootDoc: sinon.stub().resolves({
  178. element: this.rootDoc,
  179. path: { fileSystem: this.rootDocPath },
  180. }),
  181. findElementByPath: sinon
  182. .stub()
  183. .withArgs({
  184. project_id: this.newBlankProject._id,
  185. path: this.rootDocPath,
  186. exactCaseMatch: true,
  187. })
  188. .resolves({ element: this.doc0 }),
  189. },
  190. }
  191. this.ProjectOptionsHandler = {
  192. promises: {
  193. setCompiler: sinon.stub().resolves(),
  194. },
  195. }
  196. this.TpdsProjectFlusher = {
  197. promises: {
  198. flushProjectToTpds: sinon.stub().resolves(),
  199. },
  200. }
  201. this.ProjectDuplicator = SandboxedModule.require(MODULE_PATH, {
  202. requires: {
  203. '../../models/Doc': { Doc: this.Doc },
  204. '../../models/File': { File: this.File },
  205. '../Docstore/DocstoreManager': this.DocstoreManager,
  206. '../DocumentUpdater/DocumentUpdaterHandler':
  207. this.DocumentUpdaterHandler,
  208. '../FileStore/FileStoreHandler': this.FileStoreHandler,
  209. './ProjectCreationHandler': this.ProjectCreationHandler,
  210. './ProjectDeleter': this.ProjectDeleter,
  211. './ProjectEntityMongoUpdateHandler':
  212. this.ProjectEntityMongoUpdateHandler,
  213. './ProjectEntityUpdateHandler': this.ProjectEntityUpdateHandler,
  214. './ProjectGetter': this.ProjectGetter,
  215. './ProjectLocator': this.ProjectLocator,
  216. './ProjectOptionsHandler': this.ProjectOptionsHandler,
  217. '../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher,
  218. '../Tags/TagsHandler': this.TagsHandler,
  219. },
  220. })
  221. })
  222. describe('when the copy succeeds', function () {
  223. beforeEach(async function () {
  224. this.newProjectName = 'New project name'
  225. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  226. this.owner,
  227. this.project._id,
  228. this.newProjectName
  229. )
  230. })
  231. it('should flush the original project to mongo', function () {
  232. this.DocumentUpdaterHandler.promises.flushProjectToMongo.should.have.been.calledWith(
  233. this.project._id
  234. )
  235. })
  236. it('should copy docs to docstore', function () {
  237. for (const docLines of [this.doc0Lines, this.doc1Lines, this.doc2Lines]) {
  238. this.DocstoreManager.promises.updateDoc.should.have.been.calledWith(
  239. this.newProject._id.toString(),
  240. this.newDocId.toString(),
  241. docLines,
  242. 0,
  243. {}
  244. )
  245. }
  246. })
  247. it('should copy files to the filestore', function () {
  248. for (const file of [this.file0, this.file1, this.file2]) {
  249. this.FileStoreHandler.promises.copyFile.should.have.been.calledWith(
  250. this.project._id,
  251. file._id,
  252. this.newProject._id,
  253. this.newFileId
  254. )
  255. }
  256. })
  257. it('should create a blank project', function () {
  258. this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
  259. this.owner._id,
  260. this.newProjectName
  261. )
  262. this.newProject._id.should.equal(this.newBlankProject._id)
  263. })
  264. it('should use the same compiler', function () {
  265. this.ProjectOptionsHandler.promises.setCompiler.should.have.been.calledWith(
  266. this.newProject._id,
  267. this.project.compiler
  268. )
  269. })
  270. it('should use the same root doc', function () {
  271. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.have.been.calledWith(
  272. this.newProject._id,
  273. this.rootFolder.docs[0]._id
  274. )
  275. })
  276. it('should not copy the collaborators or read only refs', function () {
  277. this.newProject.collaberator_refs.length.should.equal(0)
  278. this.newProject.readOnly_refs.length.should.equal(0)
  279. })
  280. it('should copy all documents and files', function () {
  281. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
  282. this.newProject._id,
  283. this.docEntries,
  284. this.fileEntries
  285. )
  286. })
  287. it('should notify document updater of changes', function () {
  288. this.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
  289. this.newProject._id,
  290. this.newProject.overleaf.history.id,
  291. this.owner._id,
  292. {
  293. newDocs: this.docEntries,
  294. newFiles: this.fileEntries,
  295. newProject: { version: this.newProjectVersion },
  296. },
  297. null
  298. )
  299. })
  300. it('should flush the project to TPDS', function () {
  301. this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
  302. this.newProject._id
  303. )
  304. })
  305. })
  306. describe('without a root doc', function () {
  307. beforeEach(async function () {
  308. this.ProjectLocator.promises.findRootDoc.resolves({
  309. element: null,
  310. path: null,
  311. })
  312. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  313. this.owner,
  314. this.project._id,
  315. 'Copy of project'
  316. )
  317. })
  318. it('should not set the root doc on the copy', function () {
  319. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
  320. .called
  321. })
  322. })
  323. describe('with an invalid root doc', function () {
  324. beforeEach(async function () {
  325. this.ProjectEntityUpdateHandler.isPathValidForRootDoc.returns(false)
  326. this.newProject = await this.ProjectDuplicator.promises.duplicate(
  327. this.owner,
  328. this.project._id,
  329. 'Copy of project'
  330. )
  331. })
  332. it('should not set the root doc on the copy', function () {
  333. this.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
  334. .called
  335. })
  336. })
  337. describe('when there is an error', function () {
  338. beforeEach(async function () {
  339. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.rejects()
  340. await expect(
  341. this.ProjectDuplicator.promises.duplicate(
  342. this.owner,
  343. this.project._id,
  344. ''
  345. )
  346. ).to.be.rejected
  347. })
  348. it('should delete the broken cloned project', function () {
  349. this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
  350. this.newBlankProject._id
  351. )
  352. })
  353. it('should not delete the original project', function () {
  354. this.ProjectDeleter.promises.deleteProject.should.not.have.been.calledWith(
  355. this.project._id
  356. )
  357. })
  358. })
  359. })