CompileControllerTests.js 15 KB

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