CompileControllerTests.js 14 KB

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