RequestParserTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  10. */
  11. const SandboxedModule = require('sandboxed-module')
  12. const sinon = require('sinon')
  13. const { expect } = require('chai')
  14. const modulePath = require('path').join(
  15. __dirname,
  16. '../../../app/js/RequestParser'
  17. )
  18. const tk = require('timekeeper')
  19. describe('RequestParser', function () {
  20. beforeEach(function () {
  21. tk.freeze()
  22. this.callback = sinon.stub()
  23. this.validResource = {
  24. path: 'main.tex',
  25. date: '12:00 01/02/03',
  26. content: 'Hello world',
  27. }
  28. this.validRequest = {
  29. compile: {
  30. token: 'token-123',
  31. options: {
  32. imageName: 'basicImageName/here:2017-1',
  33. compiler: 'pdflatex',
  34. timeout: 42,
  35. },
  36. resources: [],
  37. },
  38. }
  39. return (this.RequestParser = SandboxedModule.require(modulePath, {
  40. requires: {
  41. '@overleaf/settings': (this.settings = {}),
  42. },
  43. }))
  44. })
  45. afterEach(function () {
  46. return tk.reset()
  47. })
  48. describe('without a top level object', function () {
  49. beforeEach(function () {
  50. return this.RequestParser.parse([], this.callback)
  51. })
  52. return it('should return an error', function () {
  53. return this.callback
  54. .calledWith('top level object should have a compile attribute')
  55. .should.equal(true)
  56. })
  57. })
  58. describe('without a compile attribute', function () {
  59. beforeEach(function () {
  60. return this.RequestParser.parse({}, this.callback)
  61. })
  62. return it('should return an error', function () {
  63. return this.callback
  64. .calledWith('top level object should have a compile attribute')
  65. .should.equal(true)
  66. })
  67. })
  68. describe('without a valid compiler', function () {
  69. beforeEach(function () {
  70. this.validRequest.compile.options.compiler = 'not-a-compiler'
  71. return this.RequestParser.parse(this.validRequest, this.callback)
  72. })
  73. return it('should return an error', function () {
  74. return this.callback
  75. .calledWith(
  76. 'compiler attribute should be one of: pdflatex, latex, xelatex, lualatex'
  77. )
  78. .should.equal(true)
  79. })
  80. })
  81. describe('without a compiler specified', function () {
  82. beforeEach(function (done) {
  83. delete this.validRequest.compile.options.compiler
  84. return this.RequestParser.parse(this.validRequest, (error, data) => {
  85. if (error) return done(error)
  86. this.data = data
  87. done()
  88. })
  89. })
  90. return it('should set the compiler to pdflatex by default', function () {
  91. return this.data.compiler.should.equal('pdflatex')
  92. })
  93. })
  94. describe('with imageName set', function () {
  95. beforeEach(function (done) {
  96. return this.RequestParser.parse(this.validRequest, (error, data) => {
  97. if (error) return done(error)
  98. this.data = data
  99. done()
  100. })
  101. })
  102. return it('should set the imageName', function () {
  103. return this.data.imageName.should.equal('basicImageName/here:2017-1')
  104. })
  105. })
  106. describe('when image restrictions are present', function () {
  107. beforeEach(function () {
  108. this.settings.clsi = { docker: {} }
  109. this.settings.clsi.docker.allowedImages = [
  110. 'repo/name:tag1',
  111. 'repo/name:tag2',
  112. ]
  113. })
  114. describe('with imageName set to something invalid', function () {
  115. beforeEach(function () {
  116. const request = this.validRequest
  117. request.compile.options.imageName = 'something/different:latest'
  118. this.RequestParser.parse(request, (error, data) => {
  119. this.error = error
  120. this.data = data
  121. })
  122. })
  123. it('should throw an error for imageName', function () {
  124. expect(String(this.error)).to.include(
  125. 'imageName attribute should be one of'
  126. )
  127. })
  128. })
  129. describe('with imageName set to something valid', function () {
  130. beforeEach(function () {
  131. const request = this.validRequest
  132. request.compile.options.imageName = 'repo/name:tag1'
  133. this.RequestParser.parse(request, (error, data) => {
  134. this.error = error
  135. this.data = data
  136. })
  137. })
  138. it('should set the imageName', function () {
  139. this.data.imageName.should.equal('repo/name:tag1')
  140. })
  141. })
  142. })
  143. describe('with flags set', function () {
  144. beforeEach(function (done) {
  145. this.validRequest.compile.options.flags = ['-file-line-error']
  146. return this.RequestParser.parse(this.validRequest, (error, data) => {
  147. if (error) return done(error)
  148. this.data = data
  149. done()
  150. })
  151. })
  152. return it('should set the flags attribute', function () {
  153. return expect(this.data.flags).to.deep.equal(['-file-line-error'])
  154. })
  155. })
  156. describe('with flags not specified', function () {
  157. beforeEach(function (done) {
  158. return this.RequestParser.parse(this.validRequest, (error, data) => {
  159. if (error) return done(error)
  160. this.data = data
  161. done()
  162. })
  163. })
  164. return it('it should have an empty flags list', function () {
  165. return expect(this.data.flags).to.deep.equal([])
  166. })
  167. })
  168. describe('without a timeout specified', function () {
  169. beforeEach(function (done) {
  170. delete this.validRequest.compile.options.timeout
  171. return this.RequestParser.parse(this.validRequest, (error, data) => {
  172. if (error) return done(error)
  173. this.data = data
  174. done()
  175. })
  176. })
  177. return it('should set the timeout to MAX_TIMEOUT', function () {
  178. return this.data.timeout.should.equal(
  179. this.RequestParser.MAX_TIMEOUT * 1000
  180. )
  181. })
  182. })
  183. describe('with a timeout larger than the maximum', function () {
  184. beforeEach(function (done) {
  185. this.validRequest.compile.options.timeout =
  186. this.RequestParser.MAX_TIMEOUT + 1
  187. return this.RequestParser.parse(this.validRequest, (error, data) => {
  188. if (error) return done(error)
  189. this.data = data
  190. done()
  191. })
  192. })
  193. return it('should set the timeout to MAX_TIMEOUT', function () {
  194. return this.data.timeout.should.equal(
  195. this.RequestParser.MAX_TIMEOUT * 1000
  196. )
  197. })
  198. })
  199. describe('with a timeout', function () {
  200. beforeEach(function (done) {
  201. return this.RequestParser.parse(this.validRequest, (error, data) => {
  202. if (error) return done(error)
  203. this.data = data
  204. done()
  205. })
  206. })
  207. return it('should set the timeout (in milliseconds)', function () {
  208. return this.data.timeout.should.equal(
  209. this.validRequest.compile.options.timeout * 1000
  210. )
  211. })
  212. })
  213. describe('with a resource without a path', function () {
  214. beforeEach(function () {
  215. delete this.validResource.path
  216. this.validRequest.compile.resources.push(this.validResource)
  217. return this.RequestParser.parse(this.validRequest, this.callback)
  218. })
  219. return it('should return an error', function () {
  220. return this.callback
  221. .calledWith('all resources should have a path attribute')
  222. .should.equal(true)
  223. })
  224. })
  225. describe('with a resource with a path', function () {
  226. beforeEach(function () {
  227. this.validResource.path = this.path = 'test.tex'
  228. this.validRequest.compile.resources.push(this.validResource)
  229. this.RequestParser.parse(this.validRequest, this.callback)
  230. return (this.data = this.callback.args[0][1])
  231. })
  232. return it('should return the path in the parsed response', function () {
  233. return this.data.resources[0].path.should.equal(this.path)
  234. })
  235. })
  236. describe('with a resource with a malformed modified date', function () {
  237. beforeEach(function () {
  238. this.validResource.modified = 'not-a-date'
  239. this.validRequest.compile.resources.push(this.validResource)
  240. return this.RequestParser.parse(this.validRequest, this.callback)
  241. })
  242. return it('should return an error', function () {
  243. return this.callback
  244. .calledWith(
  245. 'resource modified date could not be understood: ' +
  246. this.validResource.modified
  247. )
  248. .should.equal(true)
  249. })
  250. })
  251. describe('with a resource with a valid date', function () {
  252. beforeEach(function () {
  253. this.date = '12:00 01/02/03'
  254. this.validResource.modified = this.date
  255. this.validRequest.compile.resources.push(this.validResource)
  256. this.RequestParser.parse(this.validRequest, this.callback)
  257. return (this.data = this.callback.args[0][1])
  258. })
  259. return it('should return the date as a Javascript Date object', function () {
  260. ;(this.data.resources[0].modified instanceof Date).should.equal(true)
  261. return this.data.resources[0].modified
  262. .getTime()
  263. .should.equal(Date.parse(this.date))
  264. })
  265. })
  266. describe('with a resource without either a content or URL attribute', function () {
  267. beforeEach(function () {
  268. delete this.validResource.url
  269. delete this.validResource.content
  270. this.validRequest.compile.resources.push(this.validResource)
  271. return this.RequestParser.parse(this.validRequest, this.callback)
  272. })
  273. return it('should return an error', function () {
  274. return this.callback
  275. .calledWith(
  276. 'all resources should have either a url or content attribute'
  277. )
  278. .should.equal(true)
  279. })
  280. })
  281. describe('with a resource where the content is not a string', function () {
  282. beforeEach(function () {
  283. this.validResource.content = []
  284. this.validRequest.compile.resources.push(this.validResource)
  285. return this.RequestParser.parse(this.validRequest, this.callback)
  286. })
  287. return it('should return an error', function () {
  288. return this.callback
  289. .calledWith('content attribute should be a string')
  290. .should.equal(true)
  291. })
  292. })
  293. describe('with a resource where the url is not a string', function () {
  294. beforeEach(function () {
  295. this.validResource.url = []
  296. this.validRequest.compile.resources.push(this.validResource)
  297. return this.RequestParser.parse(this.validRequest, this.callback)
  298. })
  299. return it('should return an error', function () {
  300. return this.callback
  301. .calledWith('url attribute should be a string')
  302. .should.equal(true)
  303. })
  304. })
  305. describe('with a resource with a url', function () {
  306. beforeEach(function () {
  307. this.validResource.url = this.url = 'www.example.com'
  308. this.validRequest.compile.resources.push(this.validResource)
  309. this.RequestParser.parse(this.validRequest, this.callback)
  310. return (this.data = this.callback.args[0][1])
  311. })
  312. return it('should return the url in the parsed response', function () {
  313. return this.data.resources[0].url.should.equal(this.url)
  314. })
  315. })
  316. describe('with a resource with a content attribute', function () {
  317. beforeEach(function () {
  318. this.validResource.content = this.content = 'Hello world'
  319. this.validRequest.compile.resources.push(this.validResource)
  320. this.RequestParser.parse(this.validRequest, this.callback)
  321. return (this.data = this.callback.args[0][1])
  322. })
  323. return it('should return the content in the parsed response', function () {
  324. return this.data.resources[0].content.should.equal(this.content)
  325. })
  326. })
  327. describe('without a root resource path', function () {
  328. beforeEach(function () {
  329. delete this.validRequest.compile.rootResourcePath
  330. this.RequestParser.parse(this.validRequest, this.callback)
  331. return (this.data = this.callback.args[0][1])
  332. })
  333. return it("should set the root resource path to 'main.tex' by default", function () {
  334. return this.data.rootResourcePath.should.equal('main.tex')
  335. })
  336. })
  337. describe('with a root resource path', function () {
  338. beforeEach(function () {
  339. this.validRequest.compile.rootResourcePath = this.path = 'test.tex'
  340. this.RequestParser.parse(this.validRequest, this.callback)
  341. return (this.data = this.callback.args[0][1])
  342. })
  343. return it('should return the root resource path in the parsed response', function () {
  344. return this.data.rootResourcePath.should.equal(this.path)
  345. })
  346. })
  347. describe('with a root resource path that is not a string', function () {
  348. beforeEach(function () {
  349. this.validRequest.compile.rootResourcePath = []
  350. return this.RequestParser.parse(this.validRequest, this.callback)
  351. })
  352. return it('should return an error', function () {
  353. return this.callback
  354. .calledWith('rootResourcePath attribute should be a string')
  355. .should.equal(true)
  356. })
  357. })
  358. describe('with a root resource path that needs escaping', function () {
  359. beforeEach(function () {
  360. this.badPath = '`rm -rf foo`.tex'
  361. this.goodPath = 'rm -rf foo.tex'
  362. this.validRequest.compile.rootResourcePath = this.badPath
  363. this.validRequest.compile.resources.push({
  364. path: this.badPath,
  365. date: '12:00 01/02/03',
  366. content: 'Hello world',
  367. })
  368. this.RequestParser.parse(this.validRequest, this.callback)
  369. return (this.data = this.callback.args[0][1])
  370. })
  371. it('should return the escaped resource', function () {
  372. return this.data.rootResourcePath.should.equal(this.goodPath)
  373. })
  374. return it('should also escape the resource path', function () {
  375. return this.data.resources[0].path.should.equal(this.goodPath)
  376. })
  377. })
  378. describe('with a root resource path that has a relative path', function () {
  379. beforeEach(function () {
  380. this.validRequest.compile.rootResourcePath = 'foo/../../bar.tex'
  381. this.RequestParser.parse(this.validRequest, this.callback)
  382. return (this.data = this.callback.args[0][1])
  383. })
  384. return it('should return an error', function () {
  385. return this.callback
  386. .calledWith('relative path in root resource')
  387. .should.equal(true)
  388. })
  389. })
  390. describe('with a root resource path that has unescaped + relative path', function () {
  391. beforeEach(function () {
  392. this.validRequest.compile.rootResourcePath = 'foo/#../bar.tex'
  393. this.RequestParser.parse(this.validRequest, this.callback)
  394. return (this.data = this.callback.args[0][1])
  395. })
  396. return it('should return an error', function () {
  397. return this.callback
  398. .calledWith('relative path in root resource')
  399. .should.equal(true)
  400. })
  401. })
  402. return describe('with an unknown syncType', function () {
  403. beforeEach(function () {
  404. this.validRequest.compile.options.syncType = 'unexpected'
  405. this.RequestParser.parse(this.validRequest, this.callback)
  406. return (this.data = this.callback.args[0][1])
  407. })
  408. return it('should return an error', function () {
  409. return this.callback
  410. .calledWith('syncType attribute should be one of: full, incremental')
  411. .should.equal(true)
  412. })
  413. })
  414. })