CompileControllerTests.js 14 KB

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