ProjectUploadManagerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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':
  150. this.DocumentUpdaterHandler,
  151. '../FileStore/FileStoreHandler': this.FileStoreHandler,
  152. './FileSystemImportManager': this.FileSystemImportManager,
  153. '../Project/ProjectCreationHandler': this.ProjectCreationHandler,
  154. '../Project/ProjectEntityMongoUpdateHandler':
  155. this.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. null
  205. )
  206. })
  207. it('should flush the project to TPDS', function () {
  208. this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
  209. this.project._id
  210. )
  211. })
  212. it('should set the root document', function () {
  213. this.ProjectRootDocManager.promises.setRootDocFromName.should.have.been.calledWith(
  214. this.project._id,
  215. 'main.tex'
  216. )
  217. })
  218. it('should remove the destination directory afterwards', function () {
  219. this.fs.remove.should.have.been.calledWith(this.extractedZipPath)
  220. })
  221. })
  222. describe("when the root document can't be determined", function () {
  223. beforeEach(async function () {
  224. this.ProjectRootDocManager.promises.findRootDocFileFromDirectory.resolves(
  225. {}
  226. )
  227. await this.ProjectUploadManager.promises.createProjectFromZipArchive(
  228. this.ownerId,
  229. this.projectName,
  230. this.zipPath
  231. )
  232. })
  233. it('should not try to set the root doc', function () {
  234. this.ProjectRootDocManager.promises.setRootDocFromName.should.not.have
  235. .been.called
  236. })
  237. })
  238. })
  239. describe('createProjectFromZipArchiveWithName', function () {
  240. beforeEach(async function () {
  241. await this.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
  242. this.ownerId,
  243. this.projectName,
  244. this.zipPath
  245. )
  246. })
  247. it('should extract the archive', function () {
  248. this.ArchiveManager.promises.extractZipArchive.should.have.been.calledWith(
  249. this.zipPath,
  250. this.extractedZipPath
  251. )
  252. })
  253. it('should create a project owned by the owner_id', function () {
  254. this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
  255. this.ownerId,
  256. this.uniqueProjectName
  257. )
  258. })
  259. it('should automatically set the root doc', function () {
  260. this.ProjectRootDocManager.promises.setRootDocAutomatically.should.have.been.calledWith(
  261. this.project._id
  262. )
  263. })
  264. it('should initialize the file tree', function () {
  265. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
  266. this.project._id,
  267. this.docEntries,
  268. this.fileEntries
  269. )
  270. })
  271. it('should notify document updater', function () {
  272. this.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
  273. this.project._id,
  274. this.project.overleaf.history.id,
  275. this.ownerId,
  276. {
  277. newDocs: this.docEntries,
  278. newFiles: this.fileEntries,
  279. newProject: { version: this.newProjectVersion },
  280. },
  281. null
  282. )
  283. })
  284. it('should flush the project to TPDS', function () {
  285. this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
  286. this.project._id
  287. )
  288. })
  289. it('should remove the destination directory afterwards', function () {
  290. this.fs.remove.should.have.been.calledWith(this.extractedZipPath)
  291. })
  292. describe('when initializing the folder structure fails', function () {
  293. beforeEach(async function () {
  294. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.rejects()
  295. await expect(
  296. this.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
  297. this.ownerId,
  298. this.projectName,
  299. this.zipPath
  300. )
  301. ).to.be.rejected
  302. })
  303. it('should cleanup the blank project created', async function () {
  304. this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
  305. this.project._id
  306. )
  307. })
  308. })
  309. describe('when setting automatically the root doc fails', function () {
  310. beforeEach(async function () {
  311. this.ProjectRootDocManager.promises.setRootDocAutomatically.rejects()
  312. await expect(
  313. this.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
  314. this.ownerId,
  315. this.projectName,
  316. this.zipPath
  317. )
  318. ).to.be.rejected
  319. })
  320. it('should cleanup the blank project created', function () {
  321. this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
  322. this.project._id
  323. )
  324. })
  325. })
  326. })
  327. })