ProjectUploadManagerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. globals: {
  144. console: console
  145. },
  146. requires: {
  147. 'fs-extra': this.fs,
  148. './ArchiveManager': this.ArchiveManager,
  149. '../../models/Doc': { Doc: this.Doc },
  150. '../Docstore/DocstoreManager': this.DocstoreManager,
  151. '../Documents/DocumentHelper': this.DocumentHelper,
  152. '../DocumentUpdater/DocumentUpdaterHandler': this
  153. .DocumentUpdaterHandler,
  154. '../FileStore/FileStoreHandler': this.FileStoreHandler,
  155. './FileSystemImportManager': this.FileSystemImportManager,
  156. '../Project/ProjectCreationHandler': this.ProjectCreationHandler,
  157. '../Project/ProjectEntityMongoUpdateHandler': this
  158. .ProjectEntityMongoUpdateHandler,
  159. '../Project/ProjectRootDocManager': this.ProjectRootDocManager,
  160. '../Project/ProjectDetailsHandler': this.ProjectDetailsHandler,
  161. '../Project/ProjectDeleter': this.ProjectDeleter,
  162. '../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher
  163. }
  164. })
  165. })
  166. afterEach(function() {
  167. timekeeper.reset()
  168. })
  169. describe('createProjectFromZipArchive', function() {
  170. describe('when the title can be read from the root document', function() {
  171. beforeEach(async function() {
  172. await this.ProjectUploadManager.promises.createProjectFromZipArchive(
  173. this.ownerId,
  174. this.projectName,
  175. this.zipPath
  176. )
  177. })
  178. it('should extract the archive', function() {
  179. this.ArchiveManager.promises.extractZipArchive.should.have.been.calledWith(
  180. this.zipPath,
  181. this.extractedZipPath
  182. )
  183. })
  184. it('should create a project', function() {
  185. this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
  186. this.ownerId,
  187. this.uniqueProjectName
  188. )
  189. })
  190. it('should initialize the file tree', function() {
  191. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
  192. this.project._id,
  193. this.docEntries,
  194. this.fileEntries
  195. )
  196. })
  197. it('should notify document updater', function() {
  198. this.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
  199. this.project._id,
  200. this.project.overleaf.history.id,
  201. this.ownerId,
  202. {
  203. newDocs: this.docEntries,
  204. newFiles: this.fileEntries,
  205. newProject: { version: this.newProjectVersion }
  206. }
  207. )
  208. })
  209. it('should flush the project to TPDS', function() {
  210. this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
  211. this.project._id
  212. )
  213. })
  214. it('should set the root document', function() {
  215. this.ProjectRootDocManager.promises.setRootDocFromName.should.have.been.calledWith(
  216. this.project._id,
  217. 'main.tex'
  218. )
  219. })
  220. it('should remove the destination directory afterwards', function() {
  221. this.fs.remove.should.have.been.calledWith(this.extractedZipPath)
  222. })
  223. })
  224. describe("when the root document can't be determined", function() {
  225. beforeEach(async function() {
  226. this.ProjectRootDocManager.promises.findRootDocFileFromDirectory.resolves(
  227. {}
  228. )
  229. await this.ProjectUploadManager.promises.createProjectFromZipArchive(
  230. this.ownerId,
  231. this.projectName,
  232. this.zipPath
  233. )
  234. })
  235. it('should not try to set the root doc', function() {
  236. this.ProjectRootDocManager.promises.setRootDocFromName.should.not.have
  237. .been.called
  238. })
  239. })
  240. })
  241. describe('createProjectFromZipArchiveWithName', function() {
  242. beforeEach(async function() {
  243. await this.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
  244. this.ownerId,
  245. this.projectName,
  246. this.zipPath
  247. )
  248. })
  249. it('should extract the archive', function() {
  250. this.ArchiveManager.promises.extractZipArchive.should.have.been.calledWith(
  251. this.zipPath,
  252. this.extractedZipPath
  253. )
  254. })
  255. it('should create a project owned by the owner_id', function() {
  256. this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
  257. this.ownerId,
  258. this.uniqueProjectName
  259. )
  260. })
  261. it('should automatically set the root doc', function() {
  262. this.ProjectRootDocManager.promises.setRootDocAutomatically.should.have.been.calledWith(
  263. this.project._id
  264. )
  265. })
  266. it('should initialize the file tree', function() {
  267. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
  268. this.project._id,
  269. this.docEntries,
  270. this.fileEntries
  271. )
  272. })
  273. it('should notify document updater', function() {
  274. this.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
  275. this.project._id,
  276. this.project.overleaf.history.id,
  277. this.ownerId,
  278. {
  279. newDocs: this.docEntries,
  280. newFiles: this.fileEntries,
  281. newProject: { version: this.newProjectVersion }
  282. }
  283. )
  284. })
  285. it('should flush the project to TPDS', function() {
  286. this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
  287. this.project._id
  288. )
  289. })
  290. it('should remove the destination directory afterwards', function() {
  291. this.fs.remove.should.have.been.calledWith(this.extractedZipPath)
  292. })
  293. describe('when initializing the folder structure fails', function() {
  294. beforeEach(async function() {
  295. this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.rejects()
  296. await expect(
  297. this.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
  298. this.ownerId,
  299. this.projectName,
  300. this.zipPath
  301. )
  302. ).to.be.rejected
  303. })
  304. it('should cleanup the blank project created', async function() {
  305. this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
  306. this.project._id
  307. )
  308. })
  309. })
  310. describe('when setting automatically the root doc fails', function() {
  311. beforeEach(async function() {
  312. this.ProjectRootDocManager.promises.setRootDocAutomatically.rejects()
  313. await expect(
  314. this.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
  315. this.ownerId,
  316. this.projectName,
  317. this.zipPath
  318. )
  319. ).to.be.rejected
  320. })
  321. it('should cleanup the blank project created', function() {
  322. this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
  323. this.project._id
  324. )
  325. })
  326. })
  327. })
  328. })