CompileControllerTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. this.res.send
  135. .calledWith({
  136. compile: {
  137. status: 'success',
  138. error: null,
  139. stats: this.stats,
  140. timings: this.timings,
  141. buildId: this.buildId,
  142. outputUrlPrefix: '/zone/b',
  143. outputFiles: this.output_files.map(file => ({
  144. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  145. ...file,
  146. })),
  147. },
  148. })
  149. .should.equal(true)
  150. })
  151. })
  152. describe('without a outputUrlPrefix', function () {
  153. beforeEach(function () {
  154. this.Settings.apis.clsi.outputUrlPrefix = ''
  155. this.CompileManager.doCompileWithLock = sinon.stub().yields(null, {
  156. outputFiles: this.output_files,
  157. stats: this.stats,
  158. timings: this.timings,
  159. buildId: this.buildId,
  160. })
  161. this.CompileController.compile(this.req, this.res)
  162. })
  163. it('should return the JSON response with empty outputUrlPrefix', function () {
  164. this.res.status.calledWith(200).should.equal(true)
  165. this.res.send
  166. .calledWith({
  167. compile: {
  168. status: 'success',
  169. error: null,
  170. stats: this.stats,
  171. timings: this.timings,
  172. buildId: this.buildId,
  173. outputUrlPrefix: '',
  174. outputFiles: this.output_files.map(file => ({
  175. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  176. ...file,
  177. })),
  178. },
  179. })
  180. .should.equal(true)
  181. })
  182. })
  183. describe('with user provided fake_output.pdf', function () {
  184. beforeEach(function () {
  185. this.output_files = [
  186. {
  187. path: 'fake_output.pdf',
  188. type: 'pdf',
  189. build: 1234,
  190. },
  191. {
  192. path: 'output.log',
  193. type: 'log',
  194. build: 1234,
  195. },
  196. ]
  197. this.CompileManager.doCompileWithLock = sinon.stub().yields(null, {
  198. outputFiles: this.output_files,
  199. stats: this.stats,
  200. timings: this.timings,
  201. buildId: this.buildId,
  202. })
  203. this.CompileController.compile(this.req, this.res)
  204. })
  205. it('should return the JSON response with status failure', function () {
  206. this.res.status.calledWith(200).should.equal(true)
  207. this.res.send
  208. .calledWith({
  209. compile: {
  210. status: 'failure',
  211. error: null,
  212. stats: this.stats,
  213. timings: this.timings,
  214. outputUrlPrefix: '/zone/b',
  215. buildId: this.buildId,
  216. outputFiles: this.output_files.map(file => ({
  217. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  218. ...file,
  219. })),
  220. },
  221. })
  222. .should.equal(true)
  223. })
  224. })
  225. describe('with an empty output.pdf', function () {
  226. beforeEach(function () {
  227. this.output_files = [
  228. {
  229. path: 'output.pdf',
  230. type: 'pdf',
  231. size: 0,
  232. build: 1234,
  233. },
  234. {
  235. path: 'output.log',
  236. type: 'log',
  237. build: 1234,
  238. },
  239. ]
  240. this.CompileManager.doCompileWithLock = sinon.stub().yields(null, {
  241. outputFiles: this.output_files,
  242. stats: this.stats,
  243. timings: this.timings,
  244. buildId: this.buildId,
  245. })
  246. this.CompileController.compile(this.req, this.res)
  247. })
  248. it('should return the JSON response with status failure', function () {
  249. this.res.status.calledWith(200).should.equal(true)
  250. this.res.send
  251. .calledWith({
  252. compile: {
  253. status: 'failure',
  254. error: null,
  255. stats: this.stats,
  256. buildId: this.buildId,
  257. timings: this.timings,
  258. outputUrlPrefix: '/zone/b',
  259. outputFiles: this.output_files.map(file => ({
  260. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  261. ...file,
  262. })),
  263. },
  264. })
  265. .should.equal(true)
  266. })
  267. })
  268. describe('with an error', function () {
  269. beforeEach(function () {
  270. const error = new Error((this.message = 'error message'))
  271. error.buildId = this.buildId
  272. this.CompileManager.doCompileWithLock = sinon
  273. .stub()
  274. .callsArgWith(1, error, null)
  275. this.CompileController.compile(this.req, this.res)
  276. })
  277. it('should return the JSON response with the error', function () {
  278. this.res.status.calledWith(500).should.equal(true)
  279. this.res.send
  280. .calledWith({
  281. compile: {
  282. status: 'error',
  283. error: this.message,
  284. outputUrlPrefix: '/zone/b',
  285. outputFiles: [],
  286. buildId: this.buildId,
  287. // JSON.stringify will omit these
  288. stats: undefined,
  289. timings: undefined,
  290. },
  291. })
  292. .should.equal(true)
  293. })
  294. })
  295. describe('when the request times out', function () {
  296. beforeEach(function () {
  297. this.error = new Error((this.message = 'container timed out'))
  298. this.error.timedout = true
  299. this.CompileManager.doCompileWithLock = sinon
  300. .stub()
  301. .callsArgWith(1, this.error, null)
  302. this.CompileController.compile(this.req, this.res)
  303. })
  304. it('should return the JSON response with the timeout status', function () {
  305. this.res.status.calledWith(200).should.equal(true)
  306. this.res.send
  307. .calledWith({
  308. compile: {
  309. status: 'timedout',
  310. error: this.message,
  311. outputUrlPrefix: '/zone/b',
  312. outputFiles: [],
  313. // JSON.stringify will omit these
  314. buildId: undefined,
  315. stats: undefined,
  316. timings: undefined,
  317. },
  318. })
  319. .should.equal(true)
  320. })
  321. })
  322. describe('when the request returns no output files', function () {
  323. beforeEach(function () {
  324. this.CompileManager.doCompileWithLock = sinon
  325. .stub()
  326. .callsArgWith(1, null, [])
  327. this.CompileController.compile(this.req, this.res)
  328. })
  329. it('should return the JSON response with the failure status', function () {
  330. this.res.status.calledWith(200).should.equal(true)
  331. this.res.send
  332. .calledWith({
  333. compile: {
  334. error: null,
  335. status: 'failure',
  336. outputUrlPrefix: '/zone/b',
  337. outputFiles: [],
  338. // JSON.stringify will omit these
  339. buildId: undefined,
  340. stats: undefined,
  341. timings: undefined,
  342. },
  343. })
  344. .should.equal(true)
  345. })
  346. })
  347. })
  348. describe('syncFromCode', function () {
  349. beforeEach(function () {
  350. this.file = 'main.tex'
  351. this.line = 42
  352. this.column = 5
  353. this.project_id = 'mock-project-id'
  354. this.req.params = { project_id: this.project_id }
  355. this.req.query = {
  356. file: this.file,
  357. line: this.line.toString(),
  358. column: this.column.toString(),
  359. }
  360. this.res.json = sinon.stub()
  361. this.CompileManager.syncFromCode = sinon
  362. .stub()
  363. .yields(null, (this.pdfPositions = ['mock-positions']))
  364. this.CompileController.syncFromCode(this.req, this.res, this.next)
  365. })
  366. it('should find the corresponding location in the PDF', function () {
  367. this.CompileManager.syncFromCode
  368. .calledWith(
  369. this.project_id,
  370. undefined,
  371. this.file,
  372. this.line,
  373. this.column
  374. )
  375. .should.equal(true)
  376. })
  377. it('should return the positions', function () {
  378. this.res.json
  379. .calledWith({
  380. pdf: this.pdfPositions,
  381. })
  382. .should.equal(true)
  383. })
  384. tryImageNameValidation('syncFromCode', 'imageName')
  385. })
  386. describe('syncFromPdf', function () {
  387. beforeEach(function () {
  388. this.page = 5
  389. this.h = 100.23
  390. this.v = 45.67
  391. this.project_id = 'mock-project-id'
  392. this.req.params = { project_id: this.project_id }
  393. this.req.query = {
  394. page: this.page.toString(),
  395. h: this.h.toString(),
  396. v: this.v.toString(),
  397. }
  398. this.res.json = sinon.stub()
  399. this.CompileManager.syncFromPdf = sinon
  400. .stub()
  401. .yields(null, (this.codePositions = ['mock-positions']))
  402. this.CompileController.syncFromPdf(this.req, this.res, this.next)
  403. })
  404. it('should find the corresponding location in the code', function () {
  405. this.CompileManager.syncFromPdf
  406. .calledWith(this.project_id, undefined, this.page, this.h, this.v)
  407. .should.equal(true)
  408. })
  409. it('should return the positions', function () {
  410. this.res.json
  411. .calledWith({
  412. code: this.codePositions,
  413. })
  414. .should.equal(true)
  415. })
  416. tryImageNameValidation('syncFromPdf', 'imageName')
  417. })
  418. describe('wordcount', function () {
  419. beforeEach(function () {
  420. this.file = 'main.tex'
  421. this.project_id = 'mock-project-id'
  422. this.req.params = { project_id: this.project_id }
  423. this.req.query = {
  424. file: this.file,
  425. image: (this.image = 'example.com/image'),
  426. }
  427. this.res.json = sinon.stub()
  428. this.CompileManager.wordcount = sinon
  429. .stub()
  430. .callsArgWith(4, null, (this.texcount = ['mock-texcount']))
  431. })
  432. it('should return the word count of a file', function () {
  433. this.CompileController.wordcount(this.req, this.res, this.next)
  434. this.CompileManager.wordcount
  435. .calledWith(this.project_id, undefined, this.file, this.image)
  436. .should.equal(true)
  437. })
  438. it('should return the texcount info', function () {
  439. this.CompileController.wordcount(this.req, this.res, this.next)
  440. this.res.json
  441. .calledWith({
  442. texcount: this.texcount,
  443. })
  444. .should.equal(true)
  445. })
  446. tryImageNameValidation('wordcount', 'image')
  447. })
  448. })