CompileController.test.mjs 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. import MockRequest from '../helpers/MockRequest.js'
  4. import MockResponse from '../helpers/MockResponse.js'
  5. import { Headers } from 'node-fetch'
  6. import { ReadableString } from '@overleaf/stream-utils'
  7. import { RequestFailedError } from '@overleaf/fetch-utils'
  8. const modulePath = '../../../../app/src/Features/Compile/CompileController.mjs'
  9. describe('CompileController', function () {
  10. beforeEach(async function (ctx) {
  11. ctx.user_id = 'wat'
  12. ctx.user = {
  13. _id: ctx.user_id,
  14. email: 'user@example.com',
  15. features: {
  16. compileGroup: 'premium',
  17. compileTimeout: 100,
  18. },
  19. }
  20. ctx.CompileManager = {
  21. promises: {
  22. compile: sinon.stub(),
  23. getProjectCompileLimits: sinon.stub(),
  24. syncTeX: sinon.stub(),
  25. },
  26. }
  27. ctx.ClsiManager = {
  28. promises: {},
  29. }
  30. ctx.UserGetter = { getUser: sinon.stub() }
  31. ctx.rateLimiter = {
  32. consume: sinon.stub().resolves(),
  33. }
  34. ctx.RateLimiter = {
  35. RateLimiter: sinon.stub().returns(ctx.rateLimiter),
  36. }
  37. ctx.settings = {
  38. apis: {
  39. clsi: {
  40. url: 'http://clsi.example.com',
  41. submissionBackendClass: 'n2d',
  42. },
  43. clsi_priority: {
  44. url: 'http://clsi-priority.example.com',
  45. },
  46. },
  47. defaultFeatures: {
  48. compileGroup: 'standard',
  49. compileTimeout: 60,
  50. },
  51. clsiCookie: {
  52. key: 'cookie-key',
  53. },
  54. }
  55. ctx.ClsiCookieManager = {
  56. promises: {
  57. getServerId: sinon.stub().resolves('clsi-server-id-from-redis'),
  58. },
  59. }
  60. ctx.SessionManager = {
  61. getLoggedInUserId: sinon.stub().returns(ctx.user_id),
  62. getSessionUser: sinon.stub().returns(ctx.user),
  63. isUserLoggedIn: sinon.stub().returns(true),
  64. }
  65. ctx.pipeline = sinon.stub().callsFake(async (stream, res) => {
  66. if (res.callback) res.callback()
  67. })
  68. ctx.clsiStream = new ReadableString('{}')
  69. ctx.clsiResponse = {
  70. headers: new Headers({
  71. 'Content-Length': '2',
  72. 'Content-Type': 'application/json',
  73. }),
  74. }
  75. ctx.fetchUtils = {
  76. fetchStreamWithResponse: sinon.stub().resolves({
  77. stream: ctx.clsiStream,
  78. response: ctx.clsiResponse,
  79. }),
  80. RequestFailedError,
  81. }
  82. vi.doMock('stream/promises', () => ({
  83. pipeline: ctx.pipeline,
  84. }))
  85. vi.doMock('@overleaf/settings', () => ({
  86. default: ctx.settings,
  87. }))
  88. vi.doMock('@overleaf/fetch-utils', () => ctx.fetchUtils)
  89. vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
  90. default: (ctx.ProjectGetter = {
  91. promises: {},
  92. }),
  93. }))
  94. vi.doMock('@overleaf/metrics', () => ({
  95. default: (ctx.Metrics = {
  96. inc: sinon.stub(),
  97. Timer: class {
  98. constructor() {
  99. this.labels = {}
  100. }
  101. done() {}
  102. },
  103. }),
  104. }))
  105. vi.doMock('../../../../app/src/Features/Compile/CompileManager', () => ({
  106. default: ctx.CompileManager,
  107. }))
  108. vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
  109. default: ctx.UserGetter,
  110. }))
  111. vi.doMock('../../../../app/src/Features/Compile/ClsiManager', () => ({
  112. default: ctx.ClsiManager,
  113. }))
  114. vi.doMock(
  115. '../../../../app/src/Features/Authentication/SessionManager',
  116. () => ({
  117. default: ctx.SessionManager,
  118. })
  119. )
  120. vi.doMock(
  121. '../../../../app/src/infrastructure/RateLimiter.js',
  122. () => ctx.RateLimiter
  123. )
  124. vi.doMock('../../../../app/src/Features/Compile/ClsiCookieManager', () => ({
  125. default: () => ctx.ClsiCookieManager,
  126. }))
  127. vi.doMock(
  128. '../../../../app/src/Features/SplitTests/SplitTestHandler',
  129. () => ({
  130. default: {
  131. getAssignment: (ctx.getAssignment = sinon.stub().yields(null, {
  132. variant: 'default',
  133. })),
  134. promises: {
  135. getAssignment: sinon.stub().resolves({
  136. variant: 'default',
  137. }),
  138. },
  139. },
  140. })
  141. )
  142. vi.doMock(
  143. '../../../../app/src/Features/Analytics/AnalyticsManager',
  144. () => ({
  145. default: {
  146. recordEventForSession: sinon.stub(),
  147. },
  148. })
  149. )
  150. ctx.CompileController = (await import(modulePath)).default
  151. ctx.projectId = 'abc123def456abc123def456'
  152. ctx.build_id = '18fbe9e7564-30dcb2f71250c690'
  153. ctx.next = sinon.stub()
  154. ctx.req = new MockRequest()
  155. ctx.res = new MockResponse()
  156. ctx.res = new MockResponse()
  157. })
  158. describe('compile', function () {
  159. beforeEach(function (ctx) {
  160. ctx.req.params = { Project_id: ctx.projectId }
  161. ctx.req.session = {}
  162. ctx.CompileManager.promises.compile = sinon.stub().resolves({
  163. status: (ctx.status = 'success'),
  164. outputFiles: (ctx.outputFiles = [
  165. {
  166. path: 'output.pdf',
  167. url: `/project/${ctx.projectId}/user/${ctx.user_id}/build/id/output.pdf`,
  168. type: 'pdf',
  169. },
  170. ]),
  171. clsiServerId: undefined,
  172. limits: undefined,
  173. validationProblems: undefined,
  174. stats: undefined,
  175. timings: undefined,
  176. outputUrlPrefix: undefined,
  177. buildId: ctx.build_id,
  178. })
  179. })
  180. describe('pdfDownloadDomain', function () {
  181. beforeEach(function (ctx) {
  182. ctx.settings.pdfDownloadDomain = 'https://compiles.overleaf.test'
  183. })
  184. describe('when clsi does not emit zone prefix', function () {
  185. beforeEach(async function (ctx) {
  186. await ctx.CompileController.compile(ctx.req, ctx.res, ctx.next)
  187. })
  188. it('should add domain verbatim', function (ctx) {
  189. ctx.res.statusCode.should.equal(200)
  190. ctx.res.body.should.equal(
  191. JSON.stringify({
  192. status: ctx.status,
  193. outputFiles: [
  194. {
  195. path: 'output.pdf',
  196. url: `/project/${ctx.projectId}/user/${ctx.user_id}/build/id/output.pdf`,
  197. type: 'pdf',
  198. },
  199. ],
  200. outputFilesArchive: {
  201. path: 'output.zip',
  202. url: `/project/${ctx.projectId}/user/wat/build/${ctx.build_id}/output/output.zip`,
  203. type: 'zip',
  204. },
  205. pdfDownloadDomain: 'https://compiles.overleaf.test',
  206. })
  207. )
  208. })
  209. })
  210. describe('when clsi emits a zone prefix', function () {
  211. beforeEach(async function (ctx) {
  212. ctx.CompileManager.promises.compile = sinon.stub().resolves({
  213. status: (ctx.status = 'success'),
  214. outputFiles: (ctx.outputFiles = [
  215. {
  216. path: 'output.pdf',
  217. url: `/project/${ctx.projectId}/user/${ctx.user_id}/build/id/output.pdf`,
  218. type: 'pdf',
  219. },
  220. ]),
  221. clsiServerId: undefined,
  222. limits: undefined,
  223. validationProblems: undefined,
  224. stats: undefined,
  225. timings: undefined,
  226. outputUrlPrefix: '/zone/b',
  227. buildId: ctx.build_id,
  228. })
  229. await ctx.CompileController.compile(ctx.req, ctx.res, ctx.next)
  230. })
  231. it('should add the zone prefix', function (ctx) {
  232. ctx.res.statusCode.should.equal(200)
  233. ctx.res.body.should.equal(
  234. JSON.stringify({
  235. status: ctx.status,
  236. outputFiles: [
  237. {
  238. path: 'output.pdf',
  239. url: `/project/${ctx.projectId}/user/${ctx.user_id}/build/id/output.pdf`,
  240. type: 'pdf',
  241. },
  242. ],
  243. outputFilesArchive: {
  244. path: 'output.zip',
  245. url: `/project/${ctx.projectId}/user/wat/build/${ctx.build_id}/output/output.zip`,
  246. type: 'zip',
  247. },
  248. outputUrlPrefix: '/zone/b',
  249. pdfDownloadDomain: 'https://compiles.overleaf.test/zone/b',
  250. })
  251. )
  252. })
  253. })
  254. })
  255. describe('when not an auto compile', function () {
  256. beforeEach(async function (ctx) {
  257. await ctx.CompileController.compile(ctx.req, ctx.res, ctx.next)
  258. })
  259. it('should look up the user id', function (ctx) {
  260. ctx.SessionManager.getLoggedInUserId
  261. .calledWith(ctx.req.session)
  262. .should.equal(true)
  263. })
  264. it('should do the compile without the auto compile flag', function (ctx) {
  265. ctx.CompileManager.promises.compile.should.have.been.calledWith(
  266. ctx.projectId,
  267. ctx.user_id,
  268. {
  269. isAutoCompile: false,
  270. compileFromClsiCache: false,
  271. populateClsiCache: false,
  272. enablePdfCaching: false,
  273. fileLineErrors: false,
  274. stopOnFirstError: false,
  275. editorId: undefined,
  276. }
  277. )
  278. })
  279. it('should set the content-type of the response to application/json', function (ctx) {
  280. ctx.res.type.should.equal('application/json')
  281. })
  282. it('should send a successful response reporting the status and files', function (ctx) {
  283. ctx.res.statusCode.should.equal(200)
  284. ctx.res.body.should.equal(
  285. JSON.stringify({
  286. status: ctx.status,
  287. outputFiles: ctx.outputFiles,
  288. outputFilesArchive: {
  289. path: 'output.zip',
  290. url: `/project/${ctx.projectId}/user/wat/build/${ctx.build_id}/output/output.zip`,
  291. type: 'zip',
  292. },
  293. })
  294. )
  295. })
  296. })
  297. describe('when an auto compile', function () {
  298. beforeEach(async function (ctx) {
  299. ctx.req.query = { auto_compile: 'true' }
  300. await ctx.CompileController.compile(ctx.req, ctx.res, ctx.next)
  301. })
  302. it('should do the compile with the auto compile flag', function (ctx) {
  303. ctx.CompileManager.promises.compile.should.have.been.calledWith(
  304. ctx.projectId,
  305. ctx.user_id,
  306. {
  307. isAutoCompile: true,
  308. compileFromClsiCache: false,
  309. populateClsiCache: false,
  310. enablePdfCaching: false,
  311. fileLineErrors: false,
  312. stopOnFirstError: false,
  313. editorId: undefined,
  314. }
  315. )
  316. })
  317. })
  318. describe('with the draft attribute', function () {
  319. beforeEach(async function (ctx) {
  320. ctx.req.body = { draft: true }
  321. await ctx.CompileController.compile(ctx.req, ctx.res, ctx.next)
  322. })
  323. it('should do the compile without the draft compile flag', function (ctx) {
  324. ctx.CompileManager.promises.compile.should.have.been.calledWith(
  325. ctx.projectId,
  326. ctx.user_id,
  327. {
  328. isAutoCompile: false,
  329. compileFromClsiCache: false,
  330. populateClsiCache: false,
  331. enablePdfCaching: false,
  332. draft: true,
  333. fileLineErrors: false,
  334. stopOnFirstError: false,
  335. editorId: undefined,
  336. }
  337. )
  338. })
  339. })
  340. describe('with an editor id', function () {
  341. beforeEach(async function (ctx) {
  342. ctx.req.body = { editorId: 'the-editor-id' }
  343. await ctx.CompileController.compile(ctx.req, ctx.res, ctx.next)
  344. })
  345. it('should pass the editor id to the compiler', function (ctx) {
  346. ctx.CompileManager.promises.compile.should.have.been.calledWith(
  347. ctx.projectId,
  348. ctx.user_id,
  349. {
  350. isAutoCompile: false,
  351. compileFromClsiCache: false,
  352. populateClsiCache: false,
  353. enablePdfCaching: false,
  354. fileLineErrors: false,
  355. stopOnFirstError: false,
  356. editorId: 'the-editor-id',
  357. }
  358. )
  359. })
  360. })
  361. })
  362. describe('compileSubmission', function () {
  363. beforeEach(function (ctx) {
  364. ctx.submission_id = 'sub-1234'
  365. ctx.req.params = { submission_id: ctx.submission_id }
  366. ctx.req.body = {}
  367. ctx.ClsiManager.promises.sendExternalRequest = sinon.stub().resolves({
  368. status: (ctx.status = 'success'),
  369. outputFiles: (ctx.outputFiles = ['mock-output-files']),
  370. clsiServerId: 'mock-server-id',
  371. validationProblems: null,
  372. })
  373. })
  374. it('should set the content-type of the response to application/json', async function (ctx) {
  375. await ctx.CompileController.compileSubmission(ctx.req, ctx.res, ctx.next)
  376. ctx.res.contentType.calledWith('application/json').should.equal(true)
  377. })
  378. it('should send a successful response reporting the status and files', async function (ctx) {
  379. await ctx.CompileController.compileSubmission(ctx.req, ctx.res, ctx.next)
  380. ctx.res.statusCode.should.equal(200)
  381. ctx.res.body.should.equal(
  382. JSON.stringify({
  383. status: ctx.status,
  384. outputFiles: ctx.outputFiles,
  385. clsiServerId: 'mock-server-id',
  386. validationProblems: null,
  387. })
  388. )
  389. })
  390. describe('with compileGroup and timeout', function () {
  391. beforeEach(function (ctx) {
  392. ctx.req.body = {
  393. compileGroup: 'special',
  394. timeout: 600,
  395. }
  396. ctx.CompileController.compileSubmission(ctx.req, ctx.res, ctx.next)
  397. })
  398. it('should use the supplied values', function (ctx) {
  399. ctx.ClsiManager.promises.sendExternalRequest.should.have.been.calledWith(
  400. ctx.submission_id,
  401. { compileGroup: 'special', timeout: 600 },
  402. { compileGroup: 'special', compileBackendClass: 'n2d', timeout: 600 }
  403. )
  404. })
  405. })
  406. describe('with other supported options but not compileGroup and timeout', function () {
  407. beforeEach(function (ctx) {
  408. ctx.req.body = {
  409. rootResourcePath: 'main.tex',
  410. compiler: 'lualatex',
  411. draft: true,
  412. check: 'validate',
  413. }
  414. ctx.CompileController.compileSubmission(ctx.req, ctx.res, ctx.next)
  415. })
  416. it('should use the other options but default values for compileGroup and timeout', function (ctx) {
  417. ctx.ClsiManager.promises.sendExternalRequest.should.have.been.calledWith(
  418. ctx.submission_id,
  419. {
  420. rootResourcePath: 'main.tex',
  421. compiler: 'lualatex',
  422. draft: true,
  423. check: 'validate',
  424. },
  425. {
  426. rootResourcePath: 'main.tex',
  427. compiler: 'lualatex',
  428. draft: true,
  429. check: 'validate',
  430. compileGroup: 'standard',
  431. compileBackendClass: 'n2d',
  432. timeout: 60,
  433. }
  434. )
  435. })
  436. })
  437. })
  438. describe('downloadPdf', function () {
  439. beforeEach(function (ctx) {
  440. ctx.CompileController._proxyToClsi = sinon.stub().resolves()
  441. ctx.req.params = { Project_id: ctx.projectId }
  442. ctx.project = { name: 'test namè; 1' }
  443. ctx.ProjectGetter.promises.getProject = sinon.stub().resolves(ctx.project)
  444. })
  445. describe('when downloading for embedding', function () {
  446. beforeEach(async function (ctx) {
  447. await ctx.CompileController.downloadPdf(ctx.req, ctx.res, ctx.next)
  448. })
  449. it('should look up the project', function (ctx) {
  450. ctx.ProjectGetter.promises.getProject
  451. .calledWith(ctx.projectId, { name: 1 })
  452. .should.equal(true)
  453. })
  454. it('should set the content-type of the response to application/pdf', function (ctx) {
  455. ctx.res.contentType.calledWith('application/pdf').should.equal(true)
  456. })
  457. it('should set the content-disposition header with a safe version of the project name', function (ctx) {
  458. ctx.res.setContentDisposition.should.be.calledWith('inline', {
  459. filename: 'test_namè__1.pdf',
  460. })
  461. })
  462. it('should increment the pdf-downloads metric', function (ctx) {
  463. ctx.Metrics.inc.calledWith('pdf-downloads').should.equal(true)
  464. })
  465. it('should proxy the PDF from the CLSI', function (ctx) {
  466. ctx.CompileController._proxyToClsi
  467. .calledWith(
  468. ctx.projectId,
  469. 'output-file',
  470. `/project/${ctx.projectId}/user/${ctx.user_id}/output/output.pdf`,
  471. {},
  472. ctx.req,
  473. ctx.res
  474. )
  475. .should.equal(true)
  476. })
  477. })
  478. describe('when a build-id is provided', function () {
  479. beforeEach(async function (ctx) {
  480. ctx.req.params.build_id = ctx.build_id
  481. await ctx.CompileController.downloadPdf(ctx.req, ctx.res, ctx.next)
  482. })
  483. it('should proxy the PDF from the CLSI, with a build-id', function (ctx) {
  484. ctx.CompileController._proxyToClsi
  485. .calledWith(
  486. ctx.projectId,
  487. 'output-file',
  488. `/project/${ctx.projectId}/user/${ctx.user_id}/build/${ctx.build_id}/output/output.pdf`,
  489. {},
  490. ctx.req,
  491. ctx.res
  492. )
  493. .should.equal(true)
  494. })
  495. })
  496. describe('when rate-limited', function () {
  497. beforeEach(async function (ctx) {
  498. ctx.rateLimiter.consume.rejects({
  499. msBeforeNext: 250,
  500. remainingPoints: 0,
  501. consumedPoints: 5,
  502. isFirstInDuration: false,
  503. })
  504. })
  505. it('should return 500', async function (ctx) {
  506. await ctx.CompileController.downloadPdf(ctx.req, ctx.res, ctx.next)
  507. // should it be 429 instead?
  508. ctx.res.sendStatus.calledWith(500).should.equal(true)
  509. ctx.CompileController._proxyToClsi.should.not.have.been.called
  510. })
  511. })
  512. describe('when rate-limit errors', function () {
  513. beforeEach(async function (ctx) {
  514. ctx.rateLimiter.consume.rejects(new Error('uh oh'))
  515. })
  516. it('should return 500', async function (ctx) {
  517. await ctx.CompileController.downloadPdf(ctx.req, ctx.res, ctx.next)
  518. ctx.res.sendStatus.calledWith(500).should.equal(true)
  519. ctx.CompileController._proxyToClsi.should.not.have.been.called
  520. })
  521. })
  522. })
  523. describe('getFileFromClsiWithoutUser', function () {
  524. beforeEach(function (ctx) {
  525. ctx.submission_id = 'sub-1234'
  526. ctx.file = 'output.pdf'
  527. ctx.req.params = {
  528. submission_id: ctx.submission_id,
  529. build_id: ctx.build_id,
  530. file: ctx.file,
  531. }
  532. ctx.req.body = {}
  533. ctx.expected_url = `/project/${ctx.submission_id}/build/${ctx.build_id}/output/${ctx.file}`
  534. ctx.CompileController._proxyToClsiWithLimits = sinon.stub()
  535. })
  536. describe('without limits specified', function () {
  537. beforeEach(async function (ctx) {
  538. await ctx.CompileController.getFileFromClsiWithoutUser(
  539. ctx.req,
  540. ctx.res,
  541. ctx.next
  542. )
  543. })
  544. it('should proxy to CLSI with correct URL and default limits', function (ctx) {
  545. ctx.CompileController._proxyToClsiWithLimits.should.have.been.calledWith(
  546. ctx.submission_id,
  547. 'output-file',
  548. ctx.expected_url,
  549. {},
  550. { compileGroup: 'standard', compileBackendClass: 'n2d' }
  551. )
  552. })
  553. })
  554. describe('with limits specified', function () {
  555. beforeEach(function (ctx) {
  556. ctx.req.body = { compileTimeout: 600, compileGroup: 'special' }
  557. ctx.CompileController.getFileFromClsiWithoutUser(
  558. ctx.req,
  559. ctx.res,
  560. ctx.next
  561. )
  562. })
  563. it('should proxy to CLSI with correct URL and specified limits', function (ctx) {
  564. ctx.CompileController._proxyToClsiWithLimits.should.have.been.calledWith(
  565. ctx.submission_id,
  566. 'output-file',
  567. ctx.expected_url,
  568. {},
  569. {
  570. compileGroup: 'special',
  571. compileBackendClass: 'n2d',
  572. }
  573. )
  574. })
  575. })
  576. })
  577. describe('proxySyncCode', function () {
  578. let file, line, column, imageName, editorId, buildId, clsiServerId
  579. beforeEach(async function (ctx) {
  580. ctx.req.params = { Project_id: ctx.projectId }
  581. clsiServerId = 'clsi-1'
  582. file = 'main.tex'
  583. line = String(Date.now())
  584. column = String(Date.now() + 1)
  585. editorId = '172977cb-361e-4854-a4dc-a71cf11512e5'
  586. buildId = '195b4a3f9e7-03e5be430a9e7796'
  587. ctx.req.query = {
  588. file,
  589. line,
  590. column,
  591. editorId,
  592. buildId,
  593. clsiserverid: clsiServerId,
  594. }
  595. imageName = 'foo/bar:tag-0'
  596. ctx.ProjectGetter.promises.getProject = sinon
  597. .stub()
  598. .resolves({ imageName })
  599. ctx.CompileController._proxyToClsi = sinon.stub().resolves()
  600. await ctx.CompileController.proxySyncCode(ctx.req, ctx.res, ctx.next)
  601. })
  602. it('should parse the parameters', function (ctx) {
  603. expect(ctx.CompileManager.promises.syncTeX).to.have.been.calledWith(
  604. ctx.projectId,
  605. ctx.user_id,
  606. {
  607. direction: 'code',
  608. compileFromClsiCache: false,
  609. validatedOptions: {
  610. file,
  611. line,
  612. column,
  613. editorId,
  614. buildId,
  615. },
  616. clsiServerId,
  617. }
  618. )
  619. })
  620. })
  621. describe('proxySyncPdf', function () {
  622. let page, h, v, imageName, editorId, buildId, clsiServerId
  623. beforeEach(async function (ctx) {
  624. ctx.req.params = { Project_id: ctx.projectId }
  625. clsiServerId = 'clsi-1'
  626. page = String(Date.now())
  627. h = String(Math.random())
  628. v = String(Math.random())
  629. editorId = '172977cb-361e-4854-a4dc-a71cf11512e5'
  630. buildId = '195b4a3f9e7-03e5be430a9e7796'
  631. ctx.req.query = {
  632. page,
  633. h,
  634. v,
  635. editorId,
  636. buildId,
  637. clsiserverid: clsiServerId,
  638. }
  639. imageName = 'foo/bar:tag-1'
  640. ctx.ProjectGetter.promises.getProject = sinon
  641. .stub()
  642. .resolves({ imageName })
  643. ctx.CompileController._proxyToClsi = sinon.stub()
  644. await ctx.CompileController.proxySyncPdf(ctx.req, ctx.res, ctx.next)
  645. })
  646. it('should parse the parameters', function (ctx) {
  647. expect(ctx.CompileManager.promises.syncTeX).to.have.been.calledWith(
  648. ctx.projectId,
  649. ctx.user_id,
  650. {
  651. direction: 'pdf',
  652. compileFromClsiCache: false,
  653. validatedOptions: {
  654. page,
  655. h,
  656. v,
  657. editorId,
  658. buildId,
  659. },
  660. clsiServerId,
  661. }
  662. )
  663. })
  664. })
  665. describe('_proxyToClsi', function () {
  666. beforeEach(function (ctx) {
  667. ctx.req.method = 'mock-method'
  668. ctx.req.headers = {
  669. Mock: 'Headers',
  670. Range: '123-456',
  671. 'If-Range': 'abcdef',
  672. 'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT',
  673. }
  674. })
  675. describe('old pdf viewer', function () {
  676. describe('user with standard priority', function () {
  677. beforeEach(async function (ctx) {
  678. ctx.CompileManager.promises.getProjectCompileLimits = sinon
  679. .stub()
  680. .resolves({
  681. compileGroup: 'standard',
  682. compileBackendClass: 'n2d',
  683. })
  684. await ctx.CompileController._proxyToClsi(
  685. ctx.projectId,
  686. 'output-file',
  687. (ctx.url = '/test'),
  688. { query: 'foo' },
  689. ctx.req,
  690. ctx.res,
  691. ctx.next
  692. )
  693. })
  694. it('should open a request to the CLSI', function (ctx) {
  695. ctx.fetchUtils.fetchStreamWithResponse.should.have.been.calledWith(
  696. `${ctx.settings.apis.clsi.url}${ctx.url}?compileGroup=standard&compileBackendClass=n2d&query=foo`
  697. )
  698. })
  699. it('should pass the request on to the client', function (ctx) {
  700. ctx.pipeline.should.have.been.calledWith(ctx.clsiStream, ctx.res)
  701. })
  702. })
  703. describe('user with priority compile', function () {
  704. beforeEach(async function (ctx) {
  705. ctx.CompileManager.promises.getProjectCompileLimits = sinon
  706. .stub()
  707. .resolves({
  708. compileGroup: 'priority',
  709. compileBackendClass: 'c2d',
  710. })
  711. await ctx.CompileController._proxyToClsi(
  712. ctx.projectId,
  713. 'output-file',
  714. (ctx.url = '/test'),
  715. {},
  716. ctx.req,
  717. ctx.res,
  718. ctx.next
  719. )
  720. })
  721. it('should open a request to the CLSI', function (ctx) {
  722. ctx.fetchUtils.fetchStreamWithResponse.should.have.been.calledWith(
  723. `${ctx.settings.apis.clsi.url}${ctx.url}?compileGroup=priority&compileBackendClass=c2d`
  724. )
  725. })
  726. })
  727. describe('user with standard priority via query string', function () {
  728. beforeEach(async function (ctx) {
  729. ctx.req.query = { compileGroup: 'standard' }
  730. ctx.CompileManager.promises.getProjectCompileLimits = sinon
  731. .stub()
  732. .resolves({
  733. compileGroup: 'standard',
  734. compileBackendClass: 'n2d',
  735. })
  736. await ctx.CompileController._proxyToClsi(
  737. ctx.projectId,
  738. 'output-file',
  739. (ctx.url = '/test'),
  740. {},
  741. ctx.req,
  742. ctx.res,
  743. ctx.next
  744. )
  745. })
  746. it('should open a request to the CLSI', function (ctx) {
  747. ctx.fetchUtils.fetchStreamWithResponse.should.have.been.calledWith(
  748. `${ctx.settings.apis.clsi.url}${ctx.url}?compileGroup=standard&compileBackendClass=n2d`
  749. )
  750. })
  751. it('should pass the request on to the client', function (ctx) {
  752. ctx.pipeline.should.have.been.calledWith(ctx.clsiStream, ctx.res)
  753. })
  754. })
  755. describe('user with non-existent priority via query string', function () {
  756. beforeEach(async function (ctx) {
  757. ctx.req.query = { compileGroup: 'foobar' }
  758. ctx.CompileManager.promises.getProjectCompileLimits = sinon
  759. .stub()
  760. .resolves({
  761. compileGroup: 'standard',
  762. compileBackendClass: 'n2d',
  763. })
  764. await ctx.CompileController._proxyToClsi(
  765. ctx.projectId,
  766. 'output-file',
  767. (ctx.url = '/test'),
  768. {},
  769. ctx.req,
  770. ctx.res,
  771. ctx.next
  772. )
  773. })
  774. it('should proxy to the standard url', function (ctx) {
  775. ctx.fetchUtils.fetchStreamWithResponse.should.have.been.calledWith(
  776. `${ctx.settings.apis.clsi.url}${ctx.url}?compileGroup=standard&compileBackendClass=n2d`
  777. )
  778. })
  779. })
  780. describe('user with build parameter via query string', function () {
  781. beforeEach(async function (ctx) {
  782. ctx.CompileManager.promises.getProjectCompileLimits = sinon
  783. .stub()
  784. .resolves({
  785. compileGroup: 'standard',
  786. compileBackendClass: 'n2d',
  787. })
  788. ctx.req.query = { build: 1234 }
  789. await ctx.CompileController._proxyToClsi(
  790. ctx.projectId,
  791. 'output-file',
  792. (ctx.url = '/test'),
  793. {},
  794. ctx.req,
  795. ctx.res,
  796. ctx.next
  797. )
  798. })
  799. it('should proxy to the standard url without the build parameter', function (ctx) {
  800. ctx.fetchUtils.fetchStreamWithResponse.should.have.been.calledWith(
  801. `${ctx.settings.apis.clsi.url}${ctx.url}?compileGroup=standard&compileBackendClass=n2d`
  802. )
  803. })
  804. })
  805. })
  806. })
  807. describe('deleteAuxFiles', function () {
  808. beforeEach(async function (ctx) {
  809. ctx.CompileManager.promises.deleteAuxFiles = sinon.stub().resolves()
  810. ctx.req.params = { Project_id: ctx.projectId }
  811. ctx.req.query = { clsiserverid: 'node-1' }
  812. ctx.res.sendStatus = sinon.stub()
  813. await ctx.CompileController.deleteAuxFiles(ctx.req, ctx.res, ctx.next)
  814. })
  815. it('should proxy to the CLSI', function (ctx) {
  816. ctx.CompileManager.promises.deleteAuxFiles
  817. .calledWith(ctx.projectId, ctx.user_id, 'node-1')
  818. .should.equal(true)
  819. })
  820. it('should return a 200', function (ctx) {
  821. ctx.res.sendStatus.calledWith(200).should.equal(true)
  822. })
  823. })
  824. describe('compileAndDownloadPdf', function () {
  825. beforeEach(function (ctx) {
  826. ctx.req = {
  827. params: {
  828. project_id: ctx.projectId,
  829. },
  830. }
  831. ctx.downloadPath = `/project/${ctx.projectId}/build/123/output/output.pdf`
  832. ctx.CompileManager.promises.compile.resolves({
  833. status: 'success',
  834. outputFiles: [{ path: 'output.pdf', url: ctx.downloadPath }],
  835. })
  836. ctx.CompileController._proxyToClsi = sinon.stub()
  837. ctx.res = {
  838. send: () => {},
  839. sendStatus: sinon.stub(),
  840. }
  841. })
  842. it('should call compile in the compile manager', async function (ctx) {
  843. await ctx.CompileController.compileAndDownloadPdf(ctx.req, ctx.res)
  844. ctx.CompileManager.promises.compile
  845. .calledWith(ctx.projectId)
  846. .should.equal(true)
  847. })
  848. it('should proxy the res to the clsi with correct url', async function (ctx) {
  849. await ctx.CompileController.compileAndDownloadPdf(ctx.req, ctx.res)
  850. sinon.assert.calledWith(
  851. ctx.CompileController._proxyToClsi,
  852. ctx.projectId,
  853. 'output-file',
  854. ctx.downloadPath,
  855. {},
  856. ctx.req,
  857. ctx.res
  858. )
  859. ctx.CompileController._proxyToClsi
  860. .calledWith(
  861. ctx.projectId,
  862. 'output-file',
  863. ctx.downloadPath,
  864. {},
  865. ctx.req,
  866. ctx.res
  867. )
  868. .should.equal(true)
  869. })
  870. it('should not download anything on compilation failures', async function (ctx) {
  871. ctx.CompileManager.promises.compile.rejects(new Error('failed'))
  872. await ctx.CompileController.compileAndDownloadPdf(
  873. ctx.req,
  874. ctx.res,
  875. ctx.next
  876. )
  877. ctx.res.sendStatus.should.have.been.calledWith(500)
  878. ctx.CompileController._proxyToClsi.should.not.have.been.called
  879. })
  880. it('should not download anything on missing pdf', async function (ctx) {
  881. ctx.CompileManager.promises.compile.resolves({
  882. status: 'success',
  883. outputFiles: [],
  884. })
  885. await ctx.CompileController.compileAndDownloadPdf(ctx.req, ctx.res)
  886. ctx.res.sendStatus.should.have.been.calledWith(500)
  887. ctx.CompileController._proxyToClsi.should.not.have.been.called
  888. })
  889. })
  890. describe('wordCount', function () {
  891. beforeEach(async function (ctx) {
  892. ctx.CompileManager.promises.wordCount = sinon
  893. .stub()
  894. .resolves({ content: 'body' })
  895. ctx.req.params = { Project_id: ctx.projectId }
  896. ctx.req.query = { clsiserverid: 'node-42' }
  897. ctx.res.json = sinon.stub()
  898. ctx.res.contentType = sinon.stub()
  899. await ctx.CompileController.wordCount(ctx.req, ctx.res, ctx.next)
  900. })
  901. it('should proxy to the CLSI', function (ctx) {
  902. ctx.CompileManager.promises.wordCount
  903. .calledWith(ctx.projectId, ctx.user_id, false, 'node-42')
  904. .should.equal(true)
  905. })
  906. it('should return a 200 and body', function (ctx) {
  907. ctx.res.json.calledWith({ content: 'body' }).should.equal(true)
  908. })
  909. })
  910. })