ProjectUploadManager.test.mjs 12 KB

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