CompileControllerTests.js 12 KB

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