CompileControllerTests.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 an error', function() {
  125. beforeEach(function() {
  126. this.CompileManager.doCompileWithLock = sinon
  127. .stub()
  128. .callsArgWith(1, new Error((this.message = 'error message')), null)
  129. return this.CompileController.compile(this.req, this.res)
  130. })
  131. return it('should return the JSON response with the error', function() {
  132. this.res.status.calledWith(500).should.equal(true)
  133. return this.res.send
  134. .calledWith({
  135. compile: {
  136. status: 'error',
  137. error: this.message,
  138. outputFiles: []
  139. }
  140. })
  141. .should.equal(true)
  142. })
  143. })
  144. describe('when the request times out', function() {
  145. beforeEach(function() {
  146. this.error = new Error((this.message = 'container timed out'))
  147. this.error.timedout = true
  148. this.CompileManager.doCompileWithLock = sinon
  149. .stub()
  150. .callsArgWith(1, this.error, null)
  151. return this.CompileController.compile(this.req, this.res)
  152. })
  153. return it('should return the JSON response with the timeout status', function() {
  154. this.res.status.calledWith(200).should.equal(true)
  155. return this.res.send
  156. .calledWith({
  157. compile: {
  158. status: 'timedout',
  159. error: this.message,
  160. outputFiles: []
  161. }
  162. })
  163. .should.equal(true)
  164. })
  165. })
  166. return describe('when the request returns no output files', function() {
  167. beforeEach(function() {
  168. this.CompileManager.doCompileWithLock = sinon
  169. .stub()
  170. .callsArgWith(1, null, [])
  171. return this.CompileController.compile(this.req, this.res)
  172. })
  173. return it('should return the JSON response with the failure status', function() {
  174. this.res.status.calledWith(200).should.equal(true)
  175. return this.res.send
  176. .calledWith({
  177. compile: {
  178. error: null,
  179. status: 'failure',
  180. outputFiles: []
  181. }
  182. })
  183. .should.equal(true)
  184. })
  185. })
  186. })
  187. describe('syncFromCode', function() {
  188. beforeEach(function() {
  189. this.file = 'main.tex'
  190. this.line = 42
  191. this.column = 5
  192. this.project_id = 'mock-project-id'
  193. this.req.params = { project_id: this.project_id }
  194. this.req.query = {
  195. file: this.file,
  196. line: this.line.toString(),
  197. column: this.column.toString()
  198. }
  199. this.res.json = sinon.stub()
  200. this.CompileManager.syncFromCode = sinon
  201. .stub()
  202. .callsArgWith(5, null, (this.pdfPositions = ['mock-positions']))
  203. return this.CompileController.syncFromCode(this.req, this.res, this.next)
  204. })
  205. it('should find the corresponding location in the PDF', function() {
  206. return this.CompileManager.syncFromCode
  207. .calledWith(
  208. this.project_id,
  209. undefined,
  210. this.file,
  211. this.line,
  212. this.column
  213. )
  214. .should.equal(true)
  215. })
  216. return it('should return the positions', function() {
  217. return this.res.json
  218. .calledWith({
  219. pdf: this.pdfPositions
  220. })
  221. .should.equal(true)
  222. })
  223. })
  224. describe('syncFromPdf', function() {
  225. beforeEach(function() {
  226. this.page = 5
  227. this.h = 100.23
  228. this.v = 45.67
  229. this.project_id = 'mock-project-id'
  230. this.req.params = { project_id: this.project_id }
  231. this.req.query = {
  232. page: this.page.toString(),
  233. h: this.h.toString(),
  234. v: this.v.toString()
  235. }
  236. this.res.json = sinon.stub()
  237. this.CompileManager.syncFromPdf = sinon
  238. .stub()
  239. .callsArgWith(5, null, (this.codePositions = ['mock-positions']))
  240. return this.CompileController.syncFromPdf(this.req, this.res, this.next)
  241. })
  242. it('should find the corresponding location in the code', function() {
  243. return this.CompileManager.syncFromPdf
  244. .calledWith(this.project_id, undefined, this.page, this.h, this.v)
  245. .should.equal(true)
  246. })
  247. return it('should return the positions', function() {
  248. return this.res.json
  249. .calledWith({
  250. code: this.codePositions
  251. })
  252. .should.equal(true)
  253. })
  254. })
  255. return describe('wordcount', function() {
  256. beforeEach(function() {
  257. this.file = 'main.tex'
  258. this.project_id = 'mock-project-id'
  259. this.req.params = { project_id: this.project_id }
  260. this.req.query = {
  261. file: this.file,
  262. image: (this.image = 'example.com/image')
  263. }
  264. this.res.json = sinon.stub()
  265. this.CompileManager.wordcount = sinon
  266. .stub()
  267. .callsArgWith(4, null, (this.texcount = ['mock-texcount']))
  268. })
  269. it('should return the word count of a file', function() {
  270. this.CompileController.wordcount(this.req, this.res, this.next)
  271. return this.CompileManager.wordcount
  272. .calledWith(this.project_id, undefined, this.file, this.image)
  273. .should.equal(true)
  274. })
  275. it('should return the texcount info', function() {
  276. this.CompileController.wordcount(this.req, this.res, this.next)
  277. return this.res.json
  278. .calledWith({
  279. texcount: this.texcount
  280. })
  281. .should.equal(true)
  282. })
  283. describe('when allowedImageNamesFlat is set', function() {
  284. beforeEach(function() {
  285. this.Settings.allowedImageNamesFlat = [
  286. 'repo/image:tag1',
  287. 'repo/image:tag2'
  288. ]
  289. this.res.send = sinon.stub()
  290. this.res.status = sinon.stub().returns({ send: this.res.send })
  291. })
  292. describe('with an invalid image', function() {
  293. beforeEach(function() {
  294. this.req.query.image = 'something/evil:1337'
  295. this.CompileController.wordcount(this.req, this.res, this.next)
  296. })
  297. it('should return a 400', function() {
  298. expect(this.res.status.calledWith(400)).to.equal(true)
  299. })
  300. it('should not run the query', function() {
  301. expect(this.CompileManager.wordcount.called).to.equal(false)
  302. })
  303. })
  304. describe('with a valid image', function() {
  305. beforeEach(function() {
  306. this.req.query.image = 'repo/image:tag1'
  307. this.CompileController.wordcount(this.req, this.res, this.next)
  308. })
  309. it('should not return a 400', function() {
  310. expect(this.res.status.calledWith(400)).to.equal(false)
  311. })
  312. it('should run the query', function() {
  313. expect(this.CompileManager.wordcount.called).to.equal(true)
  314. })
  315. })
  316. })
  317. })
  318. })