CompileControllerTests.js 14 KB

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