CompileControllerTests.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { expect } = require('chai')
  4. const modulePath = require('path').join(
  5. __dirname,
  6. '../../../app/js/CompileController'
  7. )
  8. function tryImageNameValidation(method, imageNameField) {
  9. describe('when allowedImages is set', function () {
  10. beforeEach(function () {
  11. this.Settings.clsi = { docker: {} }
  12. this.Settings.clsi.docker.allowedImages = [
  13. 'repo/image:tag1',
  14. 'repo/image:tag2',
  15. ]
  16. this.res.send = sinon.stub()
  17. this.res.status = sinon.stub().returns({ send: this.res.send })
  18. this.CompileManager[method].reset()
  19. })
  20. describe('with an invalid image', function () {
  21. beforeEach(function () {
  22. this.req.query[imageNameField] = 'something/evil:1337'
  23. this.CompileController[method](this.req, this.res, this.next)
  24. })
  25. it('should return a 400', function () {
  26. expect(this.res.status.calledWith(400)).to.equal(true)
  27. })
  28. it('should not run the query', function () {
  29. expect(this.CompileManager[method].called).to.equal(false)
  30. })
  31. })
  32. describe('with a valid image', function () {
  33. beforeEach(function () {
  34. this.req.query[imageNameField] = 'repo/image:tag1'
  35. this.CompileController[method](this.req, this.res, this.next)
  36. })
  37. it('should not return a 400', function () {
  38. expect(this.res.status.calledWith(400)).to.equal(false)
  39. })
  40. it('should run the query', function () {
  41. expect(this.CompileManager[method].called).to.equal(true)
  42. })
  43. })
  44. })
  45. }
  46. describe('CompileController', function () {
  47. beforeEach(function () {
  48. this.buildId = 'build-id-123'
  49. this.CompileController = SandboxedModule.require(modulePath, {
  50. requires: {
  51. './CompileManager': (this.CompileManager = {}),
  52. './RequestParser': (this.RequestParser = {}),
  53. '@overleaf/settings': (this.Settings = {
  54. apis: {
  55. clsi: {
  56. url: 'http://clsi.example.com',
  57. outputUrlPrefix: '/zone/b',
  58. },
  59. },
  60. }),
  61. '@overleaf/metrics': {
  62. Timer: sinon.stub().returns({ done: sinon.stub() }),
  63. },
  64. './ProjectPersistenceManager': (this.ProjectPersistenceManager = {}),
  65. },
  66. })
  67. this.Settings.externalUrl = 'http://www.example.com'
  68. this.req = {}
  69. this.res = {}
  70. this.next = sinon.stub()
  71. })
  72. describe('compile', function () {
  73. beforeEach(function () {
  74. this.req.body = {
  75. compile: 'mock-body',
  76. }
  77. this.req.params = { project_id: (this.project_id = 'project-id-123') }
  78. this.request = {
  79. compile: 'mock-parsed-request',
  80. }
  81. this.request_with_project_id = {
  82. compile: this.request.compile,
  83. project_id: this.project_id,
  84. }
  85. this.output_files = [
  86. {
  87. path: 'output.pdf',
  88. type: 'pdf',
  89. size: 1337,
  90. build: 1234,
  91. },
  92. {
  93. path: 'output.log',
  94. type: 'log',
  95. build: 1234,
  96. },
  97. ]
  98. this.RequestParser.parse = sinon
  99. .stub()
  100. .callsArgWith(1, null, this.request)
  101. this.ProjectPersistenceManager.markProjectAsJustAccessed = sinon
  102. .stub()
  103. .callsArg(1)
  104. this.stats = { foo: 1 }
  105. this.timings = { bar: 2 }
  106. this.res.status = sinon.stub().returnsThis()
  107. this.res.send = sinon.stub()
  108. })
  109. describe('successfully', function () {
  110. beforeEach(function () {
  111. this.CompileManager.doCompileWithLock = sinon.stub().yields(null, {
  112. outputFiles: this.output_files,
  113. stats: this.stats,
  114. timings: this.timings,
  115. buildId: this.buildId,
  116. })
  117. this.CompileController.compile(this.req, this.res)
  118. })
  119. it('should parse the request', function () {
  120. this.RequestParser.parse.calledWith(this.req.body).should.equal(true)
  121. })
  122. it('should run the compile for the specified project', function () {
  123. this.CompileManager.doCompileWithLock
  124. .calledWith(this.request_with_project_id)
  125. .should.equal(true)
  126. })
  127. it('should mark the project as accessed', function () {
  128. this.ProjectPersistenceManager.markProjectAsJustAccessed
  129. .calledWith(this.project_id)
  130. .should.equal(true)
  131. })
  132. it('should return the JSON response', function () {
  133. this.res.status.calledWith(200).should.equal(true)
  134. console.log(this.res.send.args[0][0].compile)
  135. sinon.assert.calledWith(
  136. this.res.send,
  137. sinon.match.has(
  138. 'compile',
  139. sinon.match({
  140. status: 'success',
  141. error: null,
  142. stats: this.stats,
  143. timings: this.timings,
  144. outputUrlPrefix: '/zone/b',
  145. outputFiles: [
  146. ...this.output_files.map(file => ({
  147. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  148. ...file,
  149. })),
  150. {
  151. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${this.buildId}/output/output.zip`,
  152. build: this.buildId,
  153. path: 'output.zip',
  154. type: 'zip',
  155. },
  156. ],
  157. })
  158. )
  159. )
  160. })
  161. })
  162. describe('without a outputUrlPrefix', function () {
  163. beforeEach(function () {
  164. this.Settings.apis.clsi.outputUrlPrefix = ''
  165. this.CompileManager.doCompileWithLock = sinon.stub().yields(null, {
  166. outputFiles: this.output_files,
  167. stats: this.stats,
  168. timings: this.timings,
  169. buildId: this.buildId,
  170. })
  171. this.CompileController.compile(this.req, this.res)
  172. })
  173. it('should return the JSON response with empty outputUrlPrefix', function () {
  174. this.res.status.calledWith(200).should.equal(true)
  175. this.res.send
  176. .calledWith({
  177. compile: {
  178. status: 'success',
  179. error: null,
  180. stats: this.stats,
  181. timings: this.timings,
  182. outputUrlPrefix: '',
  183. outputFiles: [
  184. ...this.output_files.map(file => ({
  185. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  186. ...file,
  187. })),
  188. {
  189. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${this.buildId}/output/output.zip`,
  190. build: this.buildId,
  191. path: 'output.zip',
  192. type: 'zip',
  193. },
  194. ],
  195. },
  196. })
  197. .should.equal(true)
  198. })
  199. })
  200. describe('with user provided fake_output.pdf', function () {
  201. beforeEach(function () {
  202. this.output_files = [
  203. {
  204. path: 'fake_output.pdf',
  205. type: 'pdf',
  206. build: 1234,
  207. },
  208. {
  209. path: 'output.log',
  210. type: 'log',
  211. build: 1234,
  212. },
  213. ]
  214. this.CompileManager.doCompileWithLock = sinon.stub().yields(null, {
  215. outputFiles: this.output_files,
  216. stats: this.stats,
  217. timings: this.timings,
  218. buildId: this.buildId,
  219. })
  220. this.CompileController.compile(this.req, this.res)
  221. })
  222. it('should return the JSON response with status failure', function () {
  223. this.res.status.calledWith(200).should.equal(true)
  224. this.res.send
  225. .calledWith({
  226. compile: {
  227. status: 'failure',
  228. error: null,
  229. stats: this.stats,
  230. timings: this.timings,
  231. outputUrlPrefix: '/zone/b',
  232. outputFiles: [
  233. ...this.output_files.map(file => ({
  234. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  235. ...file,
  236. })),
  237. {
  238. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${this.buildId}/output/output.zip`,
  239. build: this.buildId,
  240. path: 'output.zip',
  241. type: 'zip',
  242. },
  243. ],
  244. },
  245. })
  246. .should.equal(true)
  247. })
  248. })
  249. describe('with an empty output.pdf', function () {
  250. beforeEach(function () {
  251. this.output_files = [
  252. {
  253. path: 'output.pdf',
  254. type: 'pdf',
  255. size: 0,
  256. build: 1234,
  257. },
  258. {
  259. path: 'output.log',
  260. type: 'log',
  261. build: 1234,
  262. },
  263. ]
  264. this.CompileManager.doCompileWithLock = sinon.stub().yields(null, {
  265. outputFiles: this.output_files,
  266. stats: this.stats,
  267. timings: this.timings,
  268. buildId: this.buildId,
  269. })
  270. this.CompileController.compile(this.req, this.res)
  271. })
  272. it('should return the JSON response with status failure', function () {
  273. this.res.status.calledWith(200).should.equal(true)
  274. this.res.send
  275. .calledWith({
  276. compile: {
  277. status: 'failure',
  278. error: null,
  279. stats: this.stats,
  280. timings: this.timings,
  281. outputUrlPrefix: '/zone/b',
  282. outputFiles: [
  283. ...this.output_files.map(file => ({
  284. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  285. ...file,
  286. })),
  287. {
  288. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${this.buildId}/output/output.zip`,
  289. build: this.buildId,
  290. path: 'output.zip',
  291. type: 'zip',
  292. },
  293. ],
  294. },
  295. })
  296. .should.equal(true)
  297. })
  298. })
  299. describe('with an error', function () {
  300. beforeEach(function () {
  301. const error = new Error((this.message = 'error message'))
  302. error.buildId = this.buildId
  303. this.CompileManager.doCompileWithLock = sinon
  304. .stub()
  305. .callsArgWith(1, error, null)
  306. this.CompileController.compile(this.req, this.res)
  307. })
  308. it('should return the JSON response with the error', function () {
  309. this.res.status.calledWith(500).should.equal(true)
  310. this.res.send
  311. .calledWith({
  312. compile: {
  313. status: 'error',
  314. error: this.message,
  315. outputUrlPrefix: '/zone/b',
  316. outputFiles: [],
  317. // JSON.stringify will omit these
  318. stats: undefined,
  319. timings: undefined,
  320. },
  321. })
  322. .should.equal(true)
  323. })
  324. })
  325. describe('when the request times out', function () {
  326. beforeEach(function () {
  327. this.error = new Error((this.message = 'container timed out'))
  328. this.error.timedout = true
  329. this.CompileManager.doCompileWithLock = sinon
  330. .stub()
  331. .callsArgWith(1, this.error, null)
  332. this.CompileController.compile(this.req, this.res)
  333. })
  334. it('should return the JSON response with the timeout status', function () {
  335. this.res.status.calledWith(200).should.equal(true)
  336. this.res.send
  337. .calledWith({
  338. compile: {
  339. status: 'timedout',
  340. error: this.message,
  341. outputUrlPrefix: '/zone/b',
  342. outputFiles: [],
  343. // JSON.stringify will omit these
  344. stats: undefined,
  345. timings: undefined,
  346. },
  347. })
  348. .should.equal(true)
  349. })
  350. })
  351. describe('when the request returns no output files', function () {
  352. beforeEach(function () {
  353. this.CompileManager.doCompileWithLock = sinon
  354. .stub()
  355. .callsArgWith(1, null, [])
  356. this.CompileController.compile(this.req, this.res)
  357. })
  358. it('should return the JSON response with the failure status', function () {
  359. this.res.status.calledWith(200).should.equal(true)
  360. this.res.send
  361. .calledWith({
  362. compile: {
  363. error: null,
  364. status: 'failure',
  365. outputUrlPrefix: '/zone/b',
  366. outputFiles: [],
  367. // JSON.stringify will omit these
  368. stats: undefined,
  369. timings: undefined,
  370. },
  371. })
  372. .should.equal(true)
  373. })
  374. })
  375. })
  376. describe('syncFromCode', function () {
  377. beforeEach(function () {
  378. this.file = 'main.tex'
  379. this.line = 42
  380. this.column = 5
  381. this.project_id = 'mock-project-id'
  382. this.req.params = { project_id: this.project_id }
  383. this.req.query = {
  384. file: this.file,
  385. line: this.line.toString(),
  386. column: this.column.toString(),
  387. }
  388. this.res.json = sinon.stub()
  389. this.CompileManager.syncFromCode = sinon
  390. .stub()
  391. .yields(null, (this.pdfPositions = ['mock-positions']))
  392. this.CompileController.syncFromCode(this.req, this.res, this.next)
  393. })
  394. it('should find the corresponding location in the PDF', function () {
  395. this.CompileManager.syncFromCode
  396. .calledWith(
  397. this.project_id,
  398. undefined,
  399. this.file,
  400. this.line,
  401. this.column
  402. )
  403. .should.equal(true)
  404. })
  405. it('should return the positions', function () {
  406. this.res.json
  407. .calledWith({
  408. pdf: this.pdfPositions,
  409. })
  410. .should.equal(true)
  411. })
  412. tryImageNameValidation('syncFromCode', 'imageName')
  413. })
  414. describe('syncFromPdf', function () {
  415. beforeEach(function () {
  416. this.page = 5
  417. this.h = 100.23
  418. this.v = 45.67
  419. this.project_id = 'mock-project-id'
  420. this.req.params = { project_id: this.project_id }
  421. this.req.query = {
  422. page: this.page.toString(),
  423. h: this.h.toString(),
  424. v: this.v.toString(),
  425. }
  426. this.res.json = sinon.stub()
  427. this.CompileManager.syncFromPdf = sinon
  428. .stub()
  429. .yields(null, (this.codePositions = ['mock-positions']))
  430. this.CompileController.syncFromPdf(this.req, this.res, this.next)
  431. })
  432. it('should find the corresponding location in the code', function () {
  433. this.CompileManager.syncFromPdf
  434. .calledWith(this.project_id, undefined, this.page, this.h, this.v)
  435. .should.equal(true)
  436. })
  437. it('should return the positions', function () {
  438. this.res.json
  439. .calledWith({
  440. code: this.codePositions,
  441. })
  442. .should.equal(true)
  443. })
  444. tryImageNameValidation('syncFromPdf', 'imageName')
  445. })
  446. describe('wordcount', function () {
  447. beforeEach(function () {
  448. this.file = 'main.tex'
  449. this.project_id = 'mock-project-id'
  450. this.req.params = { project_id: this.project_id }
  451. this.req.query = {
  452. file: this.file,
  453. image: (this.image = 'example.com/image'),
  454. }
  455. this.res.json = sinon.stub()
  456. this.CompileManager.wordcount = sinon
  457. .stub()
  458. .callsArgWith(4, null, (this.texcount = ['mock-texcount']))
  459. })
  460. it('should return the word count of a file', function () {
  461. this.CompileController.wordcount(this.req, this.res, this.next)
  462. this.CompileManager.wordcount
  463. .calledWith(this.project_id, undefined, this.file, this.image)
  464. .should.equal(true)
  465. })
  466. it('should return the texcount info', function () {
  467. this.CompileController.wordcount(this.req, this.res, this.next)
  468. this.res.json
  469. .calledWith({
  470. texcount: this.texcount,
  471. })
  472. .should.equal(true)
  473. })
  474. tryImageNameValidation('wordcount', 'image')
  475. })
  476. })