CompileControllerTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. require('chai').should()
  15. const { expect } = require('chai')
  16. const modulePath = require('path').join(
  17. __dirname,
  18. '../../../app/js/CompileController'
  19. )
  20. const tk = require('timekeeper')
  21. describe('CompileController', function () {
  22. beforeEach(function () {
  23. this.CompileController = SandboxedModule.require(modulePath, {
  24. requires: {
  25. './CompileManager': (this.CompileManager = {}),
  26. './RequestParser': (this.RequestParser = {}),
  27. 'settings-sharelatex': (this.Settings = {
  28. apis: {
  29. clsi: {
  30. url: 'http://clsi.example.com'
  31. }
  32. }
  33. }),
  34. './ProjectPersistenceManager': (this.ProjectPersistenceManager = {}),
  35. 'logger-sharelatex': (this.logger = {
  36. log: sinon.stub(),
  37. error: sinon.stub(),
  38. err: sinon.stub(),
  39. warn: sinon.stub()
  40. })
  41. }
  42. })
  43. this.Settings.externalUrl = 'http://www.example.com'
  44. this.req = {}
  45. this.res = {}
  46. return (this.next = sinon.stub())
  47. })
  48. describe('compile', function () {
  49. beforeEach(function () {
  50. this.req.body = {
  51. compile: 'mock-body'
  52. }
  53. this.req.params = { project_id: (this.project_id = 'project-id-123') }
  54. this.request = {
  55. compile: 'mock-parsed-request'
  56. }
  57. this.request_with_project_id = {
  58. compile: this.request.compile,
  59. project_id: this.project_id
  60. }
  61. this.output_files = [
  62. {
  63. path: 'output.pdf',
  64. type: 'pdf',
  65. build: 1234
  66. },
  67. {
  68. path: 'output.log',
  69. type: 'log',
  70. build: 1234
  71. }
  72. ]
  73. this.RequestParser.parse = sinon
  74. .stub()
  75. .callsArgWith(1, null, this.request)
  76. this.ProjectPersistenceManager.markProjectAsJustAccessed = sinon
  77. .stub()
  78. .callsArg(1)
  79. this.res.status = sinon.stub().returnsThis()
  80. return (this.res.send = sinon.stub())
  81. })
  82. describe('successfully', function () {
  83. beforeEach(function () {
  84. this.CompileManager.doCompileWithLock = sinon
  85. .stub()
  86. .callsArgWith(1, null, this.output_files)
  87. return this.CompileController.compile(this.req, this.res)
  88. })
  89. it('should parse the request', function () {
  90. return this.RequestParser.parse
  91. .calledWith(this.req.body)
  92. .should.equal(true)
  93. })
  94. it('should run the compile for the specified project', function () {
  95. return this.CompileManager.doCompileWithLock
  96. .calledWith(this.request_with_project_id)
  97. .should.equal(true)
  98. })
  99. it('should mark the project as accessed', function () {
  100. return this.ProjectPersistenceManager.markProjectAsJustAccessed
  101. .calledWith(this.project_id)
  102. .should.equal(true)
  103. })
  104. return it('should return the JSON response', function () {
  105. this.res.status.calledWith(200).should.equal(true)
  106. return this.res.send
  107. .calledWith({
  108. compile: {
  109. status: 'success',
  110. error: null,
  111. outputFiles: this.output_files.map((file) => {
  112. return {
  113. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  114. path: file.path,
  115. type: file.type,
  116. build: file.build
  117. }
  118. })
  119. }
  120. })
  121. .should.equal(true)
  122. })
  123. })
  124. describe('with user provided fake_output.pdf', function () {
  125. beforeEach(function () {
  126. this.output_files = [
  127. {
  128. path: 'fake_output.pdf',
  129. type: 'pdf',
  130. build: 1234
  131. },
  132. {
  133. path: 'output.log',
  134. type: 'log',
  135. build: 1234
  136. }
  137. ]
  138. this.CompileManager.doCompileWithLock = sinon
  139. .stub()
  140. .callsArgWith(1, null, this.output_files)
  141. this.CompileController.compile(this.req, this.res)
  142. })
  143. it('should return the JSON response with status failure', function () {
  144. this.res.status.calledWith(200).should.equal(true)
  145. this.res.send
  146. .calledWith({
  147. compile: {
  148. status: 'failure',
  149. error: null,
  150. outputFiles: this.output_files.map((file) => {
  151. return {
  152. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  153. path: file.path,
  154. type: file.type,
  155. build: file.build
  156. }
  157. })
  158. }
  159. })
  160. .should.equal(true)
  161. })
  162. })
  163. describe('with an error', function () {
  164. beforeEach(function () {
  165. this.CompileManager.doCompileWithLock = sinon
  166. .stub()
  167. .callsArgWith(1, new Error((this.message = 'error message')), null)
  168. return this.CompileController.compile(this.req, this.res)
  169. })
  170. return it('should return the JSON response with the error', function () {
  171. this.res.status.calledWith(500).should.equal(true)
  172. return this.res.send
  173. .calledWith({
  174. compile: {
  175. status: 'error',
  176. error: this.message,
  177. outputFiles: []
  178. }
  179. })
  180. .should.equal(true)
  181. })
  182. })
  183. describe('when the request times out', function () {
  184. beforeEach(function () {
  185. this.error = new Error((this.message = 'container timed out'))
  186. this.error.timedout = true
  187. this.CompileManager.doCompileWithLock = sinon
  188. .stub()
  189. .callsArgWith(1, this.error, null)
  190. return this.CompileController.compile(this.req, this.res)
  191. })
  192. return it('should return the JSON response with the timeout status', function () {
  193. this.res.status.calledWith(200).should.equal(true)
  194. return this.res.send
  195. .calledWith({
  196. compile: {
  197. status: 'timedout',
  198. error: this.message,
  199. outputFiles: []
  200. }
  201. })
  202. .should.equal(true)
  203. })
  204. })
  205. return describe('when the request returns no output files', function () {
  206. beforeEach(function () {
  207. this.CompileManager.doCompileWithLock = sinon
  208. .stub()
  209. .callsArgWith(1, null, [])
  210. return this.CompileController.compile(this.req, this.res)
  211. })
  212. return it('should return the JSON response with the failure status', function () {
  213. this.res.status.calledWith(200).should.equal(true)
  214. return this.res.send
  215. .calledWith({
  216. compile: {
  217. error: null,
  218. status: 'failure',
  219. outputFiles: []
  220. }
  221. })
  222. .should.equal(true)
  223. })
  224. })
  225. })
  226. describe('syncFromCode', function () {
  227. beforeEach(function () {
  228. this.file = 'main.tex'
  229. this.line = 42
  230. this.column = 5
  231. this.project_id = 'mock-project-id'
  232. this.req.params = { project_id: this.project_id }
  233. this.req.query = {
  234. file: this.file,
  235. line: this.line.toString(),
  236. column: this.column.toString()
  237. }
  238. this.res.json = sinon.stub()
  239. this.CompileManager.syncFromCode = sinon
  240. .stub()
  241. .callsArgWith(5, null, (this.pdfPositions = ['mock-positions']))
  242. return this.CompileController.syncFromCode(this.req, this.res, this.next)
  243. })
  244. it('should find the corresponding location in the PDF', function () {
  245. return this.CompileManager.syncFromCode
  246. .calledWith(
  247. this.project_id,
  248. undefined,
  249. this.file,
  250. this.line,
  251. this.column
  252. )
  253. .should.equal(true)
  254. })
  255. return it('should return the positions', function () {
  256. return this.res.json
  257. .calledWith({
  258. pdf: this.pdfPositions
  259. })
  260. .should.equal(true)
  261. })
  262. })
  263. describe('syncFromPdf', function () {
  264. beforeEach(function () {
  265. this.page = 5
  266. this.h = 100.23
  267. this.v = 45.67
  268. this.project_id = 'mock-project-id'
  269. this.req.params = { project_id: this.project_id }
  270. this.req.query = {
  271. page: this.page.toString(),
  272. h: this.h.toString(),
  273. v: this.v.toString()
  274. }
  275. this.res.json = sinon.stub()
  276. this.CompileManager.syncFromPdf = sinon
  277. .stub()
  278. .callsArgWith(5, null, (this.codePositions = ['mock-positions']))
  279. return this.CompileController.syncFromPdf(this.req, this.res, this.next)
  280. })
  281. it('should find the corresponding location in the code', function () {
  282. return this.CompileManager.syncFromPdf
  283. .calledWith(this.project_id, undefined, this.page, this.h, this.v)
  284. .should.equal(true)
  285. })
  286. return it('should return the positions', function () {
  287. return this.res.json
  288. .calledWith({
  289. code: this.codePositions
  290. })
  291. .should.equal(true)
  292. })
  293. })
  294. return describe('wordcount', function () {
  295. beforeEach(function () {
  296. this.file = 'main.tex'
  297. this.project_id = 'mock-project-id'
  298. this.req.params = { project_id: this.project_id }
  299. this.req.query = {
  300. file: this.file,
  301. image: (this.image = 'example.com/image')
  302. }
  303. this.res.json = sinon.stub()
  304. this.CompileManager.wordcount = sinon
  305. .stub()
  306. .callsArgWith(4, null, (this.texcount = ['mock-texcount']))
  307. })
  308. it('should return the word count of a file', function () {
  309. this.CompileController.wordcount(this.req, this.res, this.next)
  310. return this.CompileManager.wordcount
  311. .calledWith(this.project_id, undefined, this.file, this.image)
  312. .should.equal(true)
  313. })
  314. it('should return the texcount info', function () {
  315. this.CompileController.wordcount(this.req, this.res, this.next)
  316. return this.res.json
  317. .calledWith({
  318. texcount: this.texcount
  319. })
  320. .should.equal(true)
  321. })
  322. describe('when allowedImages is set', function () {
  323. beforeEach(function () {
  324. this.Settings.clsi = { docker: {} }
  325. this.Settings.clsi.docker.allowedImages = [
  326. 'repo/image:tag1',
  327. 'repo/image:tag2'
  328. ]
  329. this.res.send = sinon.stub()
  330. this.res.status = sinon.stub().returns({ send: this.res.send })
  331. })
  332. describe('with an invalid image', function () {
  333. beforeEach(function () {
  334. this.req.query.image = 'something/evil:1337'
  335. this.CompileController.wordcount(this.req, this.res, this.next)
  336. })
  337. it('should return a 400', function () {
  338. expect(this.res.status.calledWith(400)).to.equal(true)
  339. })
  340. it('should not run the query', function () {
  341. expect(this.CompileManager.wordcount.called).to.equal(false)
  342. })
  343. })
  344. describe('with a valid image', function () {
  345. beforeEach(function () {
  346. this.req.query.image = 'repo/image:tag1'
  347. this.CompileController.wordcount(this.req, this.res, this.next)
  348. })
  349. it('should not return a 400', function () {
  350. expect(this.res.status.calledWith(400)).to.equal(false)
  351. })
  352. it('should run the query', function () {
  353. expect(this.CompileManager.wordcount.called).to.equal(true)
  354. })
  355. })
  356. })
  357. })
  358. })