ConversionController.test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. import sinon from 'sinon'
  2. import { vi, describe, it, beforeEach, expect } from 'vitest'
  3. import Path from 'node:path'
  4. import { PassThrough } from 'node:stream'
  5. const MODULE_PATH = Path.join(
  6. import.meta.dirname,
  7. '../../../app/js/ConversionController'
  8. )
  9. describe('ConversionController', function () {
  10. beforeEach(async function (ctx) {
  11. ctx.conversionDir = '/path/to/conversion/result'
  12. ctx.zipPath = '/path/to/conversion/result/output.zip'
  13. ctx.zipStat = { size: 1234 }
  14. ctx.documentPath = '/compiles/output-uuid/output-uuid.docx'
  15. ctx.documentStat = { size: 5678 }
  16. ctx.Settings = {
  17. enablePandocConversions: true,
  18. path: { compilesDir: '/compiles', outputDir: '/output' },
  19. }
  20. ctx.OutputCacheManager = {
  21. CACHE_SUBDIR: 'generated-files',
  22. promises: {
  23. generateBuildId: sinon.stub().resolves('00000000001-0000000000000001'),
  24. },
  25. }
  26. ctx.ConversionOutputCleaner = {
  27. scheduleCleanup: sinon.stub(),
  28. }
  29. ctx.parsedRequest = { rootResourcePath: 'main.tex' }
  30. ctx.ConversionManager = {
  31. promises: {
  32. convertToLaTeXWithLock: sinon.stub().resolves(ctx.zipPath),
  33. convertLaTeXToDocumentInDirWithLock: sinon
  34. .stub()
  35. .resolves(ctx.documentPath),
  36. },
  37. }
  38. ctx.ResourceWriter = {
  39. promises: {
  40. syncResourcesToDisk: sinon.stub().resolves(),
  41. },
  42. }
  43. ctx.RequestParser = {
  44. promises: {
  45. parse: sinon.stub().resolves(ctx.parsedRequest),
  46. },
  47. }
  48. ctx.fs = {
  49. stat: sinon.stub().resolves(ctx.zipStat),
  50. unlink: sinon.stub().resolves(),
  51. rm: sinon.stub().resolves(),
  52. mkdir: sinon.stub().resolves(),
  53. copyFile: sinon.stub().resolves(),
  54. }
  55. ctx.readStream = new PassThrough()
  56. ctx.fsSync = {
  57. createReadStream: sinon.stub().returns(ctx.readStream),
  58. }
  59. ctx.pipeline = sinon.stub().resolves()
  60. vi.doMock('node:fs/promises', () => ({
  61. default: ctx.fs,
  62. }))
  63. vi.doMock('node:fs', () => ({
  64. default: ctx.fsSync,
  65. }))
  66. vi.doMock('node:stream/promises', () => ({
  67. pipeline: ctx.pipeline,
  68. }))
  69. vi.doMock('@overleaf/settings', () => ({
  70. default: ctx.Settings,
  71. }))
  72. vi.doMock('../../../app/js/ConversionManager', () => ({
  73. default: ctx.ConversionManager,
  74. }))
  75. vi.doMock('../../../app/js/ResourceWriter', () => ({
  76. default: ctx.ResourceWriter,
  77. }))
  78. vi.doMock('../../../app/js/RequestParser', () => ({
  79. default: ctx.RequestParser,
  80. }))
  81. vi.doMock('../../../app/js/OutputCacheManager', () => ({
  82. default: ctx.OutputCacheManager,
  83. }))
  84. vi.doMock('../../../app/js/ConversionOutputCleaner', () => ({
  85. default: ctx.ConversionOutputCleaner,
  86. }))
  87. ctx.res = new PassThrough()
  88. ctx.res.attachment = sinon.stub()
  89. ctx.res.setHeader = sinon.stub()
  90. ctx.res.json = sinon.stub()
  91. ctx.ConversionController = (await import(MODULE_PATH)).default
  92. })
  93. describe('convertDocumentToLaTeX', function () {
  94. describe('when conversions are disabled', function () {
  95. beforeEach(async function (ctx) {
  96. ctx.Settings.enablePandocConversions = false
  97. ctx.req = {
  98. file: { path: '/path/to/uploaded/file.docx' },
  99. query: { type: 'docx' },
  100. }
  101. ctx.res.sendStatus = sinon.stub()
  102. await ctx.ConversionController.convertDocumentToLaTeX(ctx.req, ctx.res)
  103. })
  104. it('should remove the uploaded file', function (ctx) {
  105. sinon.assert.calledWith(ctx.fs.unlink, ctx.req.file.path)
  106. })
  107. it('should return 404', function (ctx) {
  108. sinon.assert.calledWith(ctx.res.sendStatus, 404)
  109. })
  110. it('should not call the conversion manager', function (ctx) {
  111. sinon.assert.notCalled(
  112. ctx.ConversionManager.promises.convertToLaTeXWithLock
  113. )
  114. })
  115. })
  116. describe('when conversionType is missing', function () {
  117. beforeEach(async function (ctx) {
  118. ctx.req = {
  119. file: { path: '/path/to/uploaded/file.docx' },
  120. query: {},
  121. }
  122. ctx.res.sendStatus = sinon.stub()
  123. await ctx.ConversionController.convertDocumentToLaTeX(ctx.req, ctx.res)
  124. })
  125. it('should remove the uploaded file', function (ctx) {
  126. sinon.assert.calledWith(ctx.fs.unlink, ctx.req.file.path)
  127. })
  128. it('should return 400', function (ctx) {
  129. sinon.assert.calledWith(ctx.res.sendStatus, 400)
  130. })
  131. it('should not call the conversion manager', function (ctx) {
  132. sinon.assert.notCalled(
  133. ctx.ConversionManager.promises.convertToLaTeXWithLock
  134. )
  135. })
  136. })
  137. describe('when conversionType is unsupported', function () {
  138. beforeEach(async function (ctx) {
  139. ctx.req = {
  140. file: { path: '/path/to/uploaded/file.docx' },
  141. query: { type: 'invalid' },
  142. }
  143. ctx.res.sendStatus = sinon.stub()
  144. await ctx.ConversionController.convertDocumentToLaTeX(ctx.req, ctx.res)
  145. })
  146. it('should remove the uploaded file', function (ctx) {
  147. sinon.assert.calledWith(ctx.fs.unlink, ctx.req.file.path)
  148. })
  149. it('should return 400', function (ctx) {
  150. sinon.assert.calledWith(ctx.res.sendStatus, 400)
  151. })
  152. it('should not call the conversion manager', function (ctx) {
  153. sinon.assert.notCalled(
  154. ctx.ConversionManager.promises.convertToLaTeXWithLock
  155. )
  156. })
  157. })
  158. describe('successfully', function () {
  159. beforeEach(async function (ctx) {
  160. ctx.req = {
  161. file: { path: '/path/to/uploaded/file.docx' },
  162. query: { type: 'docx' },
  163. }
  164. await ctx.ConversionController.convertDocumentToLaTeX(ctx.req, ctx.res)
  165. })
  166. it('should call the conversion manager with the uploaded file path and type', function (ctx) {
  167. sinon.assert.calledWith(
  168. ctx.ConversionManager.promises.convertToLaTeXWithLock,
  169. sinon.match(
  170. /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
  171. ),
  172. ctx.req.file.path,
  173. 'docx'
  174. )
  175. })
  176. it('should look up the generated zip file size', function (ctx) {
  177. sinon.assert.calledWith(ctx.fs.stat, ctx.zipPath)
  178. })
  179. it('should set the response headers for a zip file download', function (ctx) {
  180. sinon.assert.calledWith(
  181. ctx.res.setHeader,
  182. 'Content-Length',
  183. ctx.zipStat.size
  184. )
  185. sinon.assert.calledWith(ctx.res.attachment, 'conversion.zip')
  186. sinon.assert.calledWith(
  187. ctx.res.setHeader,
  188. 'X-Content-Type-Options',
  189. 'nosniff'
  190. )
  191. })
  192. it('should stream the generated zip file to the response', function (ctx) {
  193. sinon.assert.calledWith(ctx.fsSync.createReadStream, ctx.zipPath)
  194. sinon.assert.calledWith(ctx.pipeline, ctx.readStream, ctx.res)
  195. })
  196. it('should clean up the generated zip file', function (ctx) {
  197. sinon.assert.calledWith(ctx.fs.rm, ctx.conversionDir)
  198. })
  199. })
  200. describe('with conversionType=markdown', function () {
  201. beforeEach(async function (ctx) {
  202. ctx.req = {
  203. file: { path: '/path/to/uploaded/file.md' },
  204. query: { type: 'markdown' },
  205. }
  206. await ctx.ConversionController.convertDocumentToLaTeX(ctx.req, ctx.res)
  207. })
  208. it('should call the conversion manager with the uploaded file path and markdown type', function (ctx) {
  209. sinon.assert.calledWith(
  210. ctx.ConversionManager.promises.convertToLaTeXWithLock,
  211. sinon.match(
  212. /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
  213. ),
  214. ctx.req.file.path,
  215. 'markdown'
  216. )
  217. })
  218. })
  219. describe('unsuccessfully', function () {
  220. describe('on streaming error', function () {
  221. it('should propagate the error and still clean up', async function (ctx) {
  222. ctx.pipeline.rejects(new Error('mock stream error'))
  223. const res = new PassThrough()
  224. res.attachment = sinon.stub()
  225. res.setHeader = sinon.stub()
  226. const req = {
  227. file: { path: '/path/to/uploaded/file.docx' },
  228. query: { type: 'docx' },
  229. }
  230. await expect(
  231. ctx.ConversionController.convertDocumentToLaTeX(req, res)
  232. ).to.be.rejectedWith('mock stream error')
  233. sinon.assert.calledWith(ctx.fs.rm, ctx.conversionDir)
  234. })
  235. })
  236. })
  237. })
  238. describe('convertProjectToDocument', function () {
  239. beforeEach(function (ctx) {
  240. ctx.req = {
  241. body: {},
  242. params: { project_id: 'test-project-id', user_id: 'test-user-id' },
  243. query: { type: 'docx' },
  244. }
  245. ctx.fs.stat.resolves(ctx.documentStat)
  246. })
  247. describe('when conversions are disabled', function () {
  248. beforeEach(async function (ctx) {
  249. ctx.Settings.enablePandocConversions = false
  250. ctx.res.sendStatus = sinon.stub()
  251. await ctx.ConversionController.convertProjectToDocument(
  252. ctx.req,
  253. ctx.res,
  254. sinon.stub()
  255. )
  256. })
  257. it('should return 404', function (ctx) {
  258. sinon.assert.calledWith(ctx.res.sendStatus, 404)
  259. })
  260. it('should not sync resources or call the conversion manager', function (ctx) {
  261. sinon.assert.notCalled(ctx.ResourceWriter.promises.syncResourcesToDisk)
  262. sinon.assert.notCalled(
  263. ctx.ConversionManager.promises.convertLaTeXToDocumentInDirWithLock
  264. )
  265. })
  266. })
  267. describe('when an unsupported type is requested', function () {
  268. beforeEach(async function (ctx) {
  269. ctx.req.query = { type: 'unsupported' }
  270. ctx.res.sendStatus = sinon.stub()
  271. await ctx.ConversionController.convertProjectToDocument(
  272. ctx.req,
  273. ctx.res,
  274. sinon.stub()
  275. )
  276. })
  277. it('should return 400', function (ctx) {
  278. sinon.assert.calledWith(ctx.res.sendStatus, 400)
  279. })
  280. it('should not sync resources or call the conversion manager', function (ctx) {
  281. sinon.assert.notCalled(ctx.ResourceWriter.promises.syncResourcesToDisk)
  282. sinon.assert.notCalled(
  283. ctx.ConversionManager.promises.convertLaTeXToDocumentInDirWithLock
  284. )
  285. })
  286. })
  287. const uuidDirPattern =
  288. /^\/compiles\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
  289. describe('successfully (default streaming response)', function () {
  290. beforeEach(async function (ctx) {
  291. await ctx.ConversionController.convertProjectToDocument(
  292. ctx.req,
  293. ctx.res,
  294. sinon.stub()
  295. )
  296. })
  297. it('should sync resources to a unique conversion directory', function (ctx) {
  298. sinon.assert.calledWith(
  299. ctx.ResourceWriter.promises.syncResourcesToDisk,
  300. sinon.match({ rootResourcePath: 'main.tex' }),
  301. sinon.match(uuidDirPattern)
  302. )
  303. })
  304. it('should call convertLaTeXToDocumentInDirWithLock with docx type', function (ctx) {
  305. sinon.assert.calledWith(
  306. ctx.ConversionManager.promises.convertLaTeXToDocumentInDirWithLock,
  307. sinon.match(
  308. /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
  309. ),
  310. sinon.match(uuidDirPattern),
  311. 'main.tex',
  312. 'docx'
  313. )
  314. })
  315. it('should set the Content-Length header from the document stat', function (ctx) {
  316. sinon.assert.calledWith(
  317. ctx.res.setHeader,
  318. 'Content-Length',
  319. ctx.documentStat.size
  320. )
  321. })
  322. it('should set the attachment filename', function (ctx) {
  323. sinon.assert.calledWith(ctx.res.attachment, 'output.docx')
  324. })
  325. it('should set X-Content-Type-Options header', function (ctx) {
  326. sinon.assert.calledWith(
  327. ctx.res.setHeader,
  328. 'X-Content-Type-Options',
  329. 'nosniff'
  330. )
  331. })
  332. it('should stream the document to the response', function (ctx) {
  333. sinon.assert.calledWith(ctx.fsSync.createReadStream, ctx.documentPath)
  334. sinon.assert.calledWith(ctx.pipeline, ctx.readStream, ctx.res)
  335. })
  336. it('should not move the document or schedule cleanup', function (ctx) {
  337. sinon.assert.notCalled(ctx.fs.copyFile)
  338. sinon.assert.notCalled(ctx.ConversionOutputCleaner.scheduleCleanup)
  339. })
  340. it('should clean up the conversion directory', function (ctx) {
  341. sinon.assert.calledWith(ctx.fs.rm, sinon.match(uuidDirPattern), {
  342. recursive: true,
  343. force: true,
  344. })
  345. })
  346. })
  347. describe('successfully (responseFormat=json)', function () {
  348. beforeEach(async function (ctx) {
  349. ctx.req.query.responseFormat = 'json'
  350. await ctx.ConversionController.convertProjectToDocument(
  351. ctx.req,
  352. ctx.res,
  353. sinon.stub()
  354. )
  355. })
  356. it('should move the document into the conversion output build dir', function (ctx) {
  357. const outputBuildDirPattern =
  358. /^\/output\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/generated-files\/[0-9a-f]+-[0-9a-f]+$/
  359. sinon.assert.calledWith(
  360. ctx.fs.mkdir,
  361. sinon.match(outputBuildDirPattern),
  362. { recursive: true }
  363. )
  364. sinon.assert.calledWith(
  365. ctx.fs.copyFile,
  366. ctx.documentPath,
  367. sinon.match(filePath => {
  368. return (
  369. filePath.startsWith('/output/') &&
  370. filePath.endsWith('/output.docx')
  371. )
  372. })
  373. )
  374. })
  375. it('should schedule cleanup of the conversion output dir', function (ctx) {
  376. sinon.assert.calledWith(
  377. ctx.ConversionOutputCleaner.scheduleCleanup,
  378. sinon.match(
  379. /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
  380. )
  381. )
  382. })
  383. it('should respond with the conversion id, build id, and file name', function (ctx) {
  384. sinon.assert.calledWith(
  385. ctx.res.json,
  386. sinon.match({
  387. conversionId: sinon.match(
  388. /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
  389. ),
  390. buildId: sinon.match(/^[0-9a-f]+-[0-9a-f]+$/),
  391. file: 'output.docx',
  392. })
  393. )
  394. })
  395. it('should not stream the document', function (ctx) {
  396. sinon.assert.notCalled(ctx.fsSync.createReadStream)
  397. sinon.assert.notCalled(ctx.pipeline)
  398. })
  399. it('should clean up the working conversion directory', function (ctx) {
  400. sinon.assert.calledWith(ctx.fs.rm, sinon.match(uuidDirPattern), {
  401. recursive: true,
  402. force: true,
  403. })
  404. })
  405. })
  406. describe('with conversionType=markdown', function () {
  407. beforeEach(async function (ctx) {
  408. ctx.req.query = { type: 'markdown', projectName: 'My_Project' }
  409. ctx.fs.stat.resolves(ctx.documentStat)
  410. await ctx.ConversionController.convertProjectToDocument(
  411. ctx.req,
  412. ctx.res,
  413. sinon.stub()
  414. )
  415. })
  416. it('should call convertLaTeXToDocumentInDirWithLock with type=markdown', function (ctx) {
  417. sinon.assert.calledWith(
  418. ctx.ConversionManager.promises.convertLaTeXToDocumentInDirWithLock,
  419. sinon.match(
  420. /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
  421. ),
  422. sinon.match(
  423. /^\/compiles\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
  424. ),
  425. 'main.tex',
  426. 'markdown'
  427. )
  428. })
  429. it('should set the attachment filename with .zip extension', function (ctx) {
  430. sinon.assert.calledWith(ctx.res.attachment, 'output.zip')
  431. })
  432. })
  433. describe('when conversion fails', function () {
  434. beforeEach(async function (ctx) {
  435. ctx.next = sinon.stub()
  436. ctx.ConversionManager.promises.convertLaTeXToDocumentInDirWithLock.rejects(
  437. new Error('mock conversion error')
  438. )
  439. await ctx.ConversionController.convertProjectToDocument(
  440. ctx.req,
  441. ctx.res,
  442. ctx.next
  443. )
  444. })
  445. it('should pass the error to next', function (ctx) {
  446. sinon.assert.calledOnce(ctx.next)
  447. expect(ctx.next.firstCall.args[0]).to.be.instanceOf(Error)
  448. })
  449. it('should still clean up the conversion directory', function (ctx) {
  450. sinon.assert.calledWith(ctx.fs.rm, sinon.match(uuidDirPattern), {
  451. recursive: true,
  452. force: true,
  453. })
  454. })
  455. })
  456. })
  457. })