ProjectUploadManagerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const timekeeper = require('timekeeper')
  4. const SandboxedModule = require('sandboxed-module')
  5. const { ObjectId } = require('mongodb')
  6. const MODULE_PATH =
  7. '../../../../app/src/Features/Uploads/ProjectUploadManager.js'
  8. describe('ProjectUploadManager', function () {
  9. beforeEach(function () {
  10. this.now = Date.now()
  11. timekeeper.freeze(this.now)
  12. this.rootFolderId = new ObjectId()
  13. this.ownerId = new ObjectId()
  14. this.zipPath = '/path/to/zip/file-name.zip'
  15. this.extractedZipPath = `/path/to/zip/file-name-${this.now}`
  16. this.mainContent = 'Contents of main.tex'
  17. this.projectName = 'My project*'
  18. this.fixedProjectName = 'My project'
  19. this.uniqueProjectName = 'My project (1)'
  20. this.project = {
  21. _id: new ObjectId(),
  22. rootFolder: [{ _id: this.rootFolderId }],
  23. overleaf: { history: { id: 12345 } },
  24. }
  25. this.doc = {
  26. _id: new ObjectId(),
  27. name: 'main.tex',
  28. }
  29. this.docFsPath = '/path/to/doc'
  30. this.docLines = ['My thesis', 'by A. U. Thor']
  31. this.file = {
  32. _id: new ObjectId(),
  33. name: 'image.png',
  34. }
  35. this.fileFsPath = '/path/to/file'
  36. this.topLevelDestination = '/path/to/zip/file-extracted/nested'
  37. this.newProjectVersion = 123
  38. this.importEntries = [
  39. {
  40. type: 'doc',
  41. projectPath: '/main.tex',
  42. lines: this.docLines,
  43. },
  44. {
  45. type: 'file',
  46. projectPath: `/${this.file.name}`,
  47. fsPath: this.fileFsPath,
  48. },
  49. ]
  50. this.docEntries = [
  51. {
  52. doc: this.doc,
  53. path: `/${this.doc.name}`,
  54. docLines: this.docLines.join('\n'),
  55. },
  56. ]
  57. this.fileEntries = [
  58. { file: this.file, path: `/${this.file.name}`, url: this.fileStoreUrl },
  59. ]
  60. this.fs = {
  61. remove: sinon.stub().resolves(),
  62. }
  63. this.ArchiveManager = {
  64. promises: {
  65. extractZipArchive: sinon.stub().resolves(),
  66. findTopLevelDirectory: sinon
  67. .stub()
  68. .withArgs(this.extractedZipPath)
  69. .resolves(this.topLevelDestination),
  70. },
  71. }
  72. this.Doc = sinon.stub().returns(this.doc)
  73. this.DocstoreManager = {
  74. promises: {
  75. updateDoc: sinon.stub().resolves(),
  76. },
  77. }
  78. this.DocumentHelper = {
  79. getTitleFromTexContent: sinon
  80. .stub()
  81. .withArgs(this.mainContent)
  82. .returns(this.projectName),
  83. }
  84. this.DocumentUpdaterHandler = {
  85. promises: {
  86. updateProjectStructure: sinon.stub().resolves(),
  87. },
  88. }
  89. this.FileStoreHandler = {
  90. promises: {
  91. uploadFileFromDisk: sinon
  92. .stub()
  93. .resolves({ fileRef: this.file, url: this.fileStoreUrl }),
  94. },
  95. }
  96. this.FileSystemImportManager = {
  97. promises: {
  98. importDir: sinon
  99. .stub()
  100. .withArgs(this.topLevelDestination)
  101. .resolves(this.importEntries),
  102. },
  103. }
  104. this.ProjectCreationHandler = {
  105. promises: {
  106. createBlankProject: sinon.stub().resolves(this.project),
  107. },
  108. }
  109. this.ProjectEntityMongoUpdateHandler = {
  110. promises: {
  111. createNewFolderStructure: sinon.stub().resolves(this.newProjectVersion),
  112. },
  113. }
  114. this.ProjectRootDocManager = {
  115. promises: {
  116. setRootDocAutomatically: sinon.stub().resolves(),
  117. findRootDocFileFromDirectory: sinon
  118. .stub()
  119. .resolves({ path: 'main.tex', content: this.mainContent }),
  120. setRootDocFromName: sinon.stub().resolves(),
  121. },
  122. }
  123. this.ProjectDetailsHandler = {
  124. fixProjectName: sinon
  125. .stub()
  126. .withArgs(this.projectName)
  127. .returns(this.fixedProjectName),
  128. promises: {
  129. generateUniqueName: sinon.stub().resolves(this.uniqueProjectName),
  130. },
  131. }
  132. this.ProjectDeleter = {
  133. promises: {
  134. deleteProject: sinon.stub().resolves(),
  135. },
  136. }
  137. this.TpdsProjectFlusher = {
  138. promises: {
  139. flushProjectToTpds: sinon.stub().resolves(),
  140. },
  141. }
  142. this.ProjectUploadManager = SandboxedModule.require(MODULE_PATH, {
  143. requires: {
  144. 'fs-extra': this.fs,
  145. './ArchiveManager': this.ArchiveManager,
  146. '../../models/Doc': { Doc: this.Doc },
  147. '../Docstore/DocstoreManager': this.DocstoreManager,
  148. '../Documents/DocumentHelper': this.DocumentHelper,
  149. '../DocumentUpdater/DocumentUpdaterHandler': this
  150. .DocumentUpdaterHandler,
  151. '../FileStore/FileStoreHandler': this.FileStoreHandler,
  152. './FileSystemImportManager': this.FileSystemImportManager,
  153. '../Project/ProjectCreationHandler': this.ProjectCreationHandler,
  154. '../Project/ProjectEntityMongoUpdateHandler': this
  155. .ProjectEntityMongoUpdateHandler,
  156. '../Project/ProjectRootDocManager': this.ProjectRootDocManager,
  157. '../Project/ProjectDetailsHandler': this.ProjectDetailsHandler,
  158. '../Project/ProjectDeleter': this.ProjectDeleter,
  159. '../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher,
  160. },
  161. })
  162. })
  163. afterEach(function () {
  164. timekeeper.reset()
  165. })
  166. describe('createProjectFromZipArchive', function () {
  167. describe('when the title can be read from the root document', function () {
  168. beforeEach(async function () {
  169. await this.ProjectUploadManager.promises.createProjectFromZipArchive(
  170. this.ownerId,
  171. this.projectName,
  172. this.zipPath
  173. )
  174. })
  175. it('should extract the archive', function () {
  176. this.ArchiveManager.promises.extractZipArchive.should.have.been.calledWith(
  177. this.zipPath,
  178. this.extractedZipPath
  179. )
  180. })
  181. it('should create a project', function () {
  182. this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
  183. this.ownerId,
  184. this.uniqueProjectName
  185. )
  186. })
  187. it('should initialize the file tree', function () {
  188. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
  189. this.project._id,
  190. this.docEntries,
  191. this.fileEntries
  192. )
  193. })
  194. it('should notify document updater', function () {
  195. this.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
  196. this.project._id,
  197. this.project.overleaf.history.id,
  198. this.ownerId,
  199. {
  200. newDocs: this.docEntries,
  201. newFiles: this.fileEntries,
  202. newProject: { version: this.newProjectVersion },
  203. }
  204. )
  205. })
  206. it('should flush the project to TPDS', function () {
  207. this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
  208. this.project._id
  209. )
  210. })
  211. it('should set the root document', function () {
  212. this.ProjectRootDocManager.promises.setRootDocFromName.should.have.been.calledWith(
  213. this.project._id,
  214. 'main.tex'
  215. )
  216. })
  217. it('should remove the destination directory afterwards', function () {
  218. this.fs.remove.should.have.been.calledWith(this.extractedZipPath)
  219. })
  220. })
  221. describe("when the root document can't be determined", function () {
  222. beforeEach(async function () {
  223. this.ProjectRootDocManager.promises.findRootDocFileFromDirectory.resolves(
  224. {}
  225. )
  226. await this.ProjectUploadManager.promises.createProjectFromZipArchive(
  227. this.ownerId,
  228. this.projectName,
  229. this.zipPath
  230. )
  231. })
  232. it('should not try to set the root doc', function () {
  233. this.ProjectRootDocManager.promises.setRootDocFromName.should.not.have
  234. .been.called
  235. })
  236. })
  237. })
  238. describe('createProjectFromZipArchiveWithName', function () {
  239. beforeEach(async function () {
  240. await this.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
  241. this.ownerId,
  242. this.projectName,
  243. this.zipPath
  244. )
  245. })
  246. it('should extract the archive', function () {
  247. this.ArchiveManager.promises.extractZipArchive.should.have.been.calledWith(
  248. this.zipPath,
  249. this.extractedZipPath
  250. )
  251. })
  252. it('should create a project owned by the owner_id', function () {
  253. this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
  254. this.ownerId,
  255. this.uniqueProjectName
  256. )
  257. })
  258. it('should automatically set the root doc', function () {
  259. this.ProjectRootDocManager.promises.setRootDocAutomatically.should.have.been.calledWith(
  260. this.project._id
  261. )
  262. })
  263. it('should initialize the file tree', function () {
  264. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
  265. this.project._id,
  266. this.docEntries,
  267. this.fileEntries
  268. )
  269. })
  270. it('should notify document updater', function () {
  271. this.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
  272. this.project._id,
  273. this.project.overleaf.history.id,
  274. this.ownerId,
  275. {
  276. newDocs: this.docEntries,
  277. newFiles: this.fileEntries,
  278. newProject: { version: this.newProjectVersion },
  279. }
  280. )
  281. })
  282. it('should flush the project to TPDS', function () {
  283. this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
  284. this.project._id
  285. )
  286. })
  287. it('should remove the destination directory afterwards', function () {
  288. this.fs.remove.should.have.been.calledWith(this.extractedZipPath)
  289. })
  290. describe('when initializing the folder structure fails', function () {
  291. beforeEach(async function () {
  292. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.rejects()
  293. await expect(
  294. this.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
  295. this.ownerId,
  296. this.projectName,
  297. this.zipPath
  298. )
  299. ).to.be.rejected
  300. })
  301. it('should cleanup the blank project created', async function () {
  302. this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
  303. this.project._id
  304. )
  305. })
  306. })
  307. describe('when setting automatically the root doc fails', function () {
  308. beforeEach(async function () {
  309. this.ProjectRootDocManager.promises.setRootDocAutomatically.rejects()
  310. await expect(
  311. this.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
  312. this.ownerId,
  313. this.projectName,
  314. this.zipPath
  315. )
  316. ).to.be.rejected
  317. })
  318. it('should cleanup the blank project created', function () {
  319. this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
  320. this.project._id
  321. )
  322. })
  323. })
  324. })
  325. })