CompileControllerTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. },
  69. },
  70. }),
  71. './ProjectPersistenceManager': (this.ProjectPersistenceManager = {}),
  72. },
  73. })
  74. this.Settings.externalUrl = 'http://www.example.com'
  75. this.req = {}
  76. this.res = {}
  77. return (this.next = sinon.stub())
  78. })
  79. describe('compile', function () {
  80. beforeEach(function () {
  81. this.req.body = {
  82. compile: 'mock-body',
  83. }
  84. this.req.params = { project_id: (this.project_id = 'project-id-123') }
  85. this.request = {
  86. compile: 'mock-parsed-request',
  87. }
  88. this.request_with_project_id = {
  89. compile: this.request.compile,
  90. project_id: this.project_id,
  91. }
  92. this.output_files = [
  93. {
  94. path: 'output.pdf',
  95. type: 'pdf',
  96. size: 1337,
  97. build: 1234,
  98. },
  99. {
  100. path: 'output.log',
  101. type: 'log',
  102. build: 1234,
  103. },
  104. ]
  105. this.RequestParser.parse = sinon
  106. .stub()
  107. .callsArgWith(1, null, this.request)
  108. this.ProjectPersistenceManager.markProjectAsJustAccessed = sinon
  109. .stub()
  110. .callsArg(1)
  111. this.stats = { foo: 1 }
  112. this.timings = { bar: 2 }
  113. this.res.status = sinon.stub().returnsThis()
  114. return (this.res.send = sinon.stub())
  115. })
  116. describe('successfully', function () {
  117. beforeEach(function () {
  118. this.CompileManager.doCompileWithLock = sinon
  119. .stub()
  120. .yields(null, this.output_files, this.stats, this.timings)
  121. return this.CompileController.compile(this.req, this.res)
  122. })
  123. it('should parse the request', function () {
  124. return this.RequestParser.parse
  125. .calledWith(this.req.body)
  126. .should.equal(true)
  127. })
  128. it('should run the compile for the specified project', function () {
  129. return this.CompileManager.doCompileWithLock
  130. .calledWith(this.request_with_project_id)
  131. .should.equal(true)
  132. })
  133. it('should mark the project as accessed', function () {
  134. return this.ProjectPersistenceManager.markProjectAsJustAccessed
  135. .calledWith(this.project_id)
  136. .should.equal(true)
  137. })
  138. return it('should return the JSON response', function () {
  139. this.res.status.calledWith(200).should.equal(true)
  140. return this.res.send
  141. .calledWith({
  142. compile: {
  143. status: 'success',
  144. error: null,
  145. stats: this.stats,
  146. timings: this.timings,
  147. outputFiles: this.output_files.map(file => {
  148. return {
  149. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  150. ...file,
  151. }
  152. }),
  153. },
  154. })
  155. .should.equal(true)
  156. })
  157. })
  158. describe('with user provided fake_output.pdf', function () {
  159. beforeEach(function () {
  160. this.output_files = [
  161. {
  162. path: 'fake_output.pdf',
  163. type: 'pdf',
  164. build: 1234,
  165. },
  166. {
  167. path: 'output.log',
  168. type: 'log',
  169. build: 1234,
  170. },
  171. ]
  172. this.CompileManager.doCompileWithLock = sinon
  173. .stub()
  174. .yields(null, this.output_files, this.stats, this.timings)
  175. this.CompileController.compile(this.req, this.res)
  176. })
  177. it('should return the JSON response with status failure', function () {
  178. this.res.status.calledWith(200).should.equal(true)
  179. this.res.send
  180. .calledWith({
  181. compile: {
  182. status: 'failure',
  183. error: null,
  184. stats: this.stats,
  185. timings: this.timings,
  186. outputFiles: this.output_files.map(file => {
  187. return {
  188. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  189. ...file,
  190. }
  191. }),
  192. },
  193. })
  194. .should.equal(true)
  195. })
  196. })
  197. describe('with an empty output.pdf', function () {
  198. beforeEach(function () {
  199. this.output_files = [
  200. {
  201. path: 'output.pdf',
  202. type: 'pdf',
  203. size: 0,
  204. build: 1234,
  205. },
  206. {
  207. path: 'output.log',
  208. type: 'log',
  209. build: 1234,
  210. },
  211. ]
  212. this.CompileManager.doCompileWithLock = sinon
  213. .stub()
  214. .yields(null, this.output_files, this.stats, this.timings)
  215. this.CompileController.compile(this.req, this.res)
  216. })
  217. it('should return the JSON response with status failure', function () {
  218. this.res.status.calledWith(200).should.equal(true)
  219. this.res.send
  220. .calledWith({
  221. compile: {
  222. status: 'failure',
  223. error: null,
  224. stats: this.stats,
  225. timings: this.timings,
  226. outputFiles: this.output_files.map(file => {
  227. return {
  228. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  229. ...file,
  230. }
  231. }),
  232. },
  233. })
  234. .should.equal(true)
  235. })
  236. })
  237. describe('with an error', function () {
  238. beforeEach(function () {
  239. this.CompileManager.doCompileWithLock = sinon
  240. .stub()
  241. .callsArgWith(1, new Error((this.message = 'error message')), null)
  242. return this.CompileController.compile(this.req, this.res)
  243. })
  244. return it('should return the JSON response with the error', function () {
  245. this.res.status.calledWith(500).should.equal(true)
  246. return this.res.send
  247. .calledWith({
  248. compile: {
  249. status: 'error',
  250. error: this.message,
  251. outputFiles: [],
  252. // JSON.stringify will omit these
  253. stats: undefined,
  254. timings: undefined,
  255. },
  256. })
  257. .should.equal(true)
  258. })
  259. })
  260. describe('when the request times out', function () {
  261. beforeEach(function () {
  262. this.error = new Error((this.message = 'container timed out'))
  263. this.error.timedout = true
  264. this.CompileManager.doCompileWithLock = sinon
  265. .stub()
  266. .callsArgWith(1, this.error, null)
  267. return this.CompileController.compile(this.req, this.res)
  268. })
  269. return it('should return the JSON response with the timeout status', function () {
  270. this.res.status.calledWith(200).should.equal(true)
  271. return this.res.send
  272. .calledWith({
  273. compile: {
  274. status: 'timedout',
  275. error: this.message,
  276. outputFiles: [],
  277. // JSON.stringify will omit these
  278. stats: undefined,
  279. timings: undefined,
  280. },
  281. })
  282. .should.equal(true)
  283. })
  284. })
  285. return describe('when the request returns no output files', function () {
  286. beforeEach(function () {
  287. this.CompileManager.doCompileWithLock = sinon
  288. .stub()
  289. .callsArgWith(1, null, [])
  290. return this.CompileController.compile(this.req, this.res)
  291. })
  292. return it('should return the JSON response with the failure status', function () {
  293. this.res.status.calledWith(200).should.equal(true)
  294. return this.res.send
  295. .calledWith({
  296. compile: {
  297. error: null,
  298. status: 'failure',
  299. outputFiles: [],
  300. // JSON.stringify will omit these
  301. stats: undefined,
  302. timings: undefined,
  303. },
  304. })
  305. .should.equal(true)
  306. })
  307. })
  308. })
  309. describe('syncFromCode', function () {
  310. beforeEach(function () {
  311. this.file = 'main.tex'
  312. this.line = 42
  313. this.column = 5
  314. this.project_id = 'mock-project-id'
  315. this.req.params = { project_id: this.project_id }
  316. this.req.query = {
  317. file: this.file,
  318. line: this.line.toString(),
  319. column: this.column.toString(),
  320. }
  321. this.res.json = sinon.stub()
  322. this.CompileManager.syncFromCode = sinon
  323. .stub()
  324. .yields(null, (this.pdfPositions = ['mock-positions']))
  325. return this.CompileController.syncFromCode(this.req, this.res, this.next)
  326. })
  327. it('should find the corresponding location in the PDF', function () {
  328. return this.CompileManager.syncFromCode
  329. .calledWith(
  330. this.project_id,
  331. undefined,
  332. this.file,
  333. this.line,
  334. this.column
  335. )
  336. .should.equal(true)
  337. })
  338. it('should return the positions', function () {
  339. return this.res.json
  340. .calledWith({
  341. pdf: this.pdfPositions,
  342. })
  343. .should.equal(true)
  344. })
  345. tryImageNameValidation('syncFromCode', 'imageName')
  346. })
  347. describe('syncFromPdf', function () {
  348. beforeEach(function () {
  349. this.page = 5
  350. this.h = 100.23
  351. this.v = 45.67
  352. this.project_id = 'mock-project-id'
  353. this.req.params = { project_id: this.project_id }
  354. this.req.query = {
  355. page: this.page.toString(),
  356. h: this.h.toString(),
  357. v: this.v.toString(),
  358. }
  359. this.res.json = sinon.stub()
  360. this.CompileManager.syncFromPdf = sinon
  361. .stub()
  362. .yields(null, (this.codePositions = ['mock-positions']))
  363. return this.CompileController.syncFromPdf(this.req, this.res, this.next)
  364. })
  365. it('should find the corresponding location in the code', function () {
  366. return this.CompileManager.syncFromPdf
  367. .calledWith(this.project_id, undefined, this.page, this.h, this.v)
  368. .should.equal(true)
  369. })
  370. it('should return the positions', function () {
  371. return this.res.json
  372. .calledWith({
  373. code: this.codePositions,
  374. })
  375. .should.equal(true)
  376. })
  377. tryImageNameValidation('syncFromPdf', 'imageName')
  378. })
  379. return describe('wordcount', function () {
  380. beforeEach(function () {
  381. this.file = 'main.tex'
  382. this.project_id = 'mock-project-id'
  383. this.req.params = { project_id: this.project_id }
  384. this.req.query = {
  385. file: this.file,
  386. image: (this.image = 'example.com/image'),
  387. }
  388. this.res.json = sinon.stub()
  389. this.CompileManager.wordcount = sinon
  390. .stub()
  391. .callsArgWith(4, null, (this.texcount = ['mock-texcount']))
  392. })
  393. it('should return the word count of a file', function () {
  394. this.CompileController.wordcount(this.req, this.res, this.next)
  395. return this.CompileManager.wordcount
  396. .calledWith(this.project_id, undefined, this.file, this.image)
  397. .should.equal(true)
  398. })
  399. it('should return the texcount info', function () {
  400. this.CompileController.wordcount(this.req, this.res, this.next)
  401. return this.res.json
  402. .calledWith({
  403. texcount: this.texcount,
  404. })
  405. .should.equal(true)
  406. })
  407. tryImageNameValidation('wordcount', 'image')
  408. })
  409. })