CompileControllerTests.js 12 KB

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