RequestParserTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-return-assign,
  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/RequestParser'
  19. )
  20. const tk = require('timekeeper')
  21. describe('RequestParser', function() {
  22. beforeEach(function() {
  23. tk.freeze()
  24. this.callback = sinon.stub()
  25. this.validResource = {
  26. path: 'main.tex',
  27. date: '12:00 01/02/03',
  28. content: 'Hello world'
  29. }
  30. this.validRequest = {
  31. compile: {
  32. token: 'token-123',
  33. options: {
  34. imageName: 'basicImageName/here:2017-1',
  35. compiler: 'pdflatex',
  36. timeout: 42
  37. },
  38. resources: []
  39. }
  40. }
  41. return (this.RequestParser = SandboxedModule.require(modulePath, {
  42. requires: {
  43. 'settings-sharelatex': (this.settings = {})
  44. }
  45. }))
  46. })
  47. afterEach(function() {
  48. return tk.reset()
  49. })
  50. describe('without a top level object', function() {
  51. beforeEach(function() {
  52. return this.RequestParser.parse([], this.callback)
  53. })
  54. return it('should return an error', function() {
  55. return this.callback
  56. .calledWith('top level object should have a compile attribute')
  57. .should.equal(true)
  58. })
  59. })
  60. describe('without a compile attribute', function() {
  61. beforeEach(function() {
  62. return this.RequestParser.parse({}, this.callback)
  63. })
  64. return it('should return an error', function() {
  65. return this.callback
  66. .calledWith('top level object should have a compile attribute')
  67. .should.equal(true)
  68. })
  69. })
  70. describe('without a valid compiler', function() {
  71. beforeEach(function() {
  72. this.validRequest.compile.options.compiler = 'not-a-compiler'
  73. return this.RequestParser.parse(this.validRequest, this.callback)
  74. })
  75. return it('should return an error', function() {
  76. return this.callback
  77. .calledWith(
  78. 'compiler attribute should be one of: pdflatex, latex, xelatex, lualatex'
  79. )
  80. .should.equal(true)
  81. })
  82. })
  83. describe('without a compiler specified', function() {
  84. beforeEach(function() {
  85. delete this.validRequest.compile.options.compiler
  86. return this.RequestParser.parse(this.validRequest, (error, data) => {
  87. this.data = data
  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() {
  96. return this.RequestParser.parse(this.validRequest, (error, data) => {
  97. this.data = data
  98. })
  99. })
  100. return it('should set the imageName', function() {
  101. return this.data.imageName.should.equal('basicImageName/here:2017-1')
  102. })
  103. })
  104. describe('with flags set', function() {
  105. beforeEach(function() {
  106. this.validRequest.compile.options.flags = ['-file-line-error']
  107. return this.RequestParser.parse(this.validRequest, (error, data) => {
  108. this.data = data
  109. })
  110. })
  111. return it('should set the flags attribute', function() {
  112. return expect(this.data.flags).to.deep.equal(['-file-line-error'])
  113. })
  114. })
  115. describe('with flags not specified', function() {
  116. beforeEach(function() {
  117. return this.RequestParser.parse(this.validRequest, (error, data) => {
  118. this.data = data
  119. })
  120. })
  121. return it('it should have an empty flags list', function() {
  122. return expect(this.data.flags).to.deep.equal([])
  123. })
  124. })
  125. describe('without a timeout specified', function() {
  126. beforeEach(function() {
  127. delete this.validRequest.compile.options.timeout
  128. return this.RequestParser.parse(this.validRequest, (error, data) => {
  129. this.data = data
  130. })
  131. })
  132. return it('should set the timeout to MAX_TIMEOUT', function() {
  133. return this.data.timeout.should.equal(
  134. this.RequestParser.MAX_TIMEOUT * 1000
  135. )
  136. })
  137. })
  138. describe('with a timeout larger than the maximum', function() {
  139. beforeEach(function() {
  140. this.validRequest.compile.options.timeout =
  141. this.RequestParser.MAX_TIMEOUT + 1
  142. return this.RequestParser.parse(this.validRequest, (error, data) => {
  143. this.data = data
  144. })
  145. })
  146. return it('should set the timeout to MAX_TIMEOUT', function() {
  147. return this.data.timeout.should.equal(
  148. this.RequestParser.MAX_TIMEOUT * 1000
  149. )
  150. })
  151. })
  152. describe('with a timeout', function() {
  153. beforeEach(function() {
  154. return this.RequestParser.parse(this.validRequest, (error, data) => {
  155. this.data = data
  156. })
  157. })
  158. return it('should set the timeout (in milliseconds)', function() {
  159. return this.data.timeout.should.equal(
  160. this.validRequest.compile.options.timeout * 1000
  161. )
  162. })
  163. })
  164. describe('with a resource without a path', function() {
  165. beforeEach(function() {
  166. delete this.validResource.path
  167. this.validRequest.compile.resources.push(this.validResource)
  168. return this.RequestParser.parse(this.validRequest, this.callback)
  169. })
  170. return it('should return an error', function() {
  171. return this.callback
  172. .calledWith('all resources should have a path attribute')
  173. .should.equal(true)
  174. })
  175. })
  176. describe('with a resource with a path', function() {
  177. beforeEach(function() {
  178. this.validResource.path = this.path = 'test.tex'
  179. this.validRequest.compile.resources.push(this.validResource)
  180. this.RequestParser.parse(this.validRequest, this.callback)
  181. return (this.data = this.callback.args[0][1])
  182. })
  183. return it('should return the path in the parsed response', function() {
  184. return this.data.resources[0].path.should.equal(this.path)
  185. })
  186. })
  187. describe('with a resource with a malformed modified date', function() {
  188. beforeEach(function() {
  189. this.validResource.modified = 'not-a-date'
  190. this.validRequest.compile.resources.push(this.validResource)
  191. return this.RequestParser.parse(this.validRequest, this.callback)
  192. })
  193. return it('should return an error', function() {
  194. return this.callback
  195. .calledWith(
  196. 'resource modified date could not be understood: ' +
  197. this.validResource.modified
  198. )
  199. .should.equal(true)
  200. })
  201. })
  202. describe('with a resource with a valid date', function() {
  203. beforeEach(function() {
  204. this.date = '12:00 01/02/03'
  205. this.validResource.modified = this.date
  206. this.validRequest.compile.resources.push(this.validResource)
  207. this.RequestParser.parse(this.validRequest, this.callback)
  208. return (this.data = this.callback.args[0][1])
  209. })
  210. return it('should return the date as a Javascript Date object', function() {
  211. ;(this.data.resources[0].modified instanceof Date).should.equal(true)
  212. return this.data.resources[0].modified
  213. .getTime()
  214. .should.equal(Date.parse(this.date))
  215. })
  216. })
  217. describe('with a resource without either a content or URL attribute', function() {
  218. beforeEach(function() {
  219. delete this.validResource.url
  220. delete this.validResource.content
  221. this.validRequest.compile.resources.push(this.validResource)
  222. return this.RequestParser.parse(this.validRequest, this.callback)
  223. })
  224. return it('should return an error', function() {
  225. return this.callback
  226. .calledWith(
  227. 'all resources should have either a url or content attribute'
  228. )
  229. .should.equal(true)
  230. })
  231. })
  232. describe('with a resource where the content is not a string', function() {
  233. beforeEach(function() {
  234. this.validResource.content = []
  235. this.validRequest.compile.resources.push(this.validResource)
  236. return this.RequestParser.parse(this.validRequest, this.callback)
  237. })
  238. return it('should return an error', function() {
  239. return this.callback
  240. .calledWith('content attribute should be a string')
  241. .should.equal(true)
  242. })
  243. })
  244. describe('with a resource where the url is not a string', function() {
  245. beforeEach(function() {
  246. this.validResource.url = []
  247. this.validRequest.compile.resources.push(this.validResource)
  248. return this.RequestParser.parse(this.validRequest, this.callback)
  249. })
  250. return it('should return an error', function() {
  251. return this.callback
  252. .calledWith('url attribute should be a string')
  253. .should.equal(true)
  254. })
  255. })
  256. describe('with a resource with a url', function() {
  257. beforeEach(function() {
  258. this.validResource.url = this.url = 'www.example.com'
  259. this.validRequest.compile.resources.push(this.validResource)
  260. this.RequestParser.parse(this.validRequest, this.callback)
  261. return (this.data = this.callback.args[0][1])
  262. })
  263. return it('should return the url in the parsed response', function() {
  264. return this.data.resources[0].url.should.equal(this.url)
  265. })
  266. })
  267. describe('with a resource with a content attribute', function() {
  268. beforeEach(function() {
  269. this.validResource.content = this.content = 'Hello world'
  270. this.validRequest.compile.resources.push(this.validResource)
  271. this.RequestParser.parse(this.validRequest, this.callback)
  272. return (this.data = this.callback.args[0][1])
  273. })
  274. return it('should return the content in the parsed response', function() {
  275. return this.data.resources[0].content.should.equal(this.content)
  276. })
  277. })
  278. describe('without a root resource path', function() {
  279. beforeEach(function() {
  280. delete this.validRequest.compile.rootResourcePath
  281. this.RequestParser.parse(this.validRequest, this.callback)
  282. return (this.data = this.callback.args[0][1])
  283. })
  284. return it("should set the root resource path to 'main.tex' by default", function() {
  285. return this.data.rootResourcePath.should.equal('main.tex')
  286. })
  287. })
  288. describe('with a root resource path', function() {
  289. beforeEach(function() {
  290. this.validRequest.compile.rootResourcePath = this.path = 'test.tex'
  291. this.RequestParser.parse(this.validRequest, this.callback)
  292. return (this.data = this.callback.args[0][1])
  293. })
  294. return it('should return the root resource path in the parsed response', function() {
  295. return this.data.rootResourcePath.should.equal(this.path)
  296. })
  297. })
  298. describe('with a root resource path that is not a string', function() {
  299. beforeEach(function() {
  300. this.validRequest.compile.rootResourcePath = []
  301. return this.RequestParser.parse(this.validRequest, this.callback)
  302. })
  303. return it('should return an error', function() {
  304. return this.callback
  305. .calledWith('rootResourcePath attribute should be a string')
  306. .should.equal(true)
  307. })
  308. })
  309. describe('with a root resource path that needs escaping', function() {
  310. beforeEach(function() {
  311. this.badPath = '`rm -rf foo`.tex'
  312. this.goodPath = 'rm -rf foo.tex'
  313. this.validRequest.compile.rootResourcePath = this.badPath
  314. this.validRequest.compile.resources.push({
  315. path: this.badPath,
  316. date: '12:00 01/02/03',
  317. content: 'Hello world'
  318. })
  319. this.RequestParser.parse(this.validRequest, this.callback)
  320. return (this.data = this.callback.args[0][1])
  321. })
  322. it('should return the escaped resource', function() {
  323. return this.data.rootResourcePath.should.equal(this.goodPath)
  324. })
  325. return it('should also escape the resource path', function() {
  326. return this.data.resources[0].path.should.equal(this.goodPath)
  327. })
  328. })
  329. describe('with a root resource path that has a relative path', function() {
  330. beforeEach(function() {
  331. this.validRequest.compile.rootResourcePath = 'foo/../../bar.tex'
  332. this.RequestParser.parse(this.validRequest, this.callback)
  333. return (this.data = this.callback.args[0][1])
  334. })
  335. return it('should return an error', function() {
  336. return this.callback
  337. .calledWith('relative path in root resource')
  338. .should.equal(true)
  339. })
  340. })
  341. describe('with a root resource path that has unescaped + relative path', function() {
  342. beforeEach(function() {
  343. this.validRequest.compile.rootResourcePath = 'foo/#../bar.tex'
  344. this.RequestParser.parse(this.validRequest, this.callback)
  345. return (this.data = this.callback.args[0][1])
  346. })
  347. return it('should return an error', function() {
  348. return this.callback
  349. .calledWith('relative path in root resource')
  350. .should.equal(true)
  351. })
  352. })
  353. return describe('with an unknown syncType', function() {
  354. beforeEach(function() {
  355. this.validRequest.compile.options.syncType = 'unexpected'
  356. this.RequestParser.parse(this.validRequest, this.callback)
  357. return (this.data = this.callback.args[0][1])
  358. })
  359. return it('should return an error', function() {
  360. return this.callback
  361. .calledWith('syncType attribute should be one of: full, incremental')
  362. .should.equal(true)
  363. })
  364. })
  365. })