ProjectUploadManagerTests.js 11 KB

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