CompileController.test.mjs 35 KB

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