RequestParserTests.coffee 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/RequestParser'
  5. tk = require("timekeeper")
  6. describe "RequestParser", ->
  7. beforeEach ->
  8. tk.freeze()
  9. @callback = sinon.stub()
  10. @validResource =
  11. path: "main.tex"
  12. date: "12:00 01/02/03"
  13. content: "Hello world"
  14. @validRequest =
  15. compile:
  16. token: "token-123"
  17. options:
  18. compiler: "pdflatex"
  19. timeout: 42
  20. resources: []
  21. @RequestParser = SandboxedModule.require modulePath
  22. afterEach ->
  23. tk.reset()
  24. describe "without a top level object", ->
  25. beforeEach ->
  26. @RequestParser.parse [], @callback
  27. it "should return an error", ->
  28. @callback.calledWith("top level object should have a compile attribute")
  29. .should.equal true
  30. describe "without a compile attribute", ->
  31. beforeEach ->
  32. @RequestParser.parse {}, @callback
  33. it "should return an error", ->
  34. @callback.calledWith("top level object should have a compile attribute")
  35. .should.equal true
  36. describe "without a valid compiler", ->
  37. beforeEach ->
  38. @validRequest.compile.options.compiler = "not-a-compiler"
  39. @RequestParser.parse @validRequest, @callback
  40. it "should return an error", ->
  41. @callback.calledWith("compiler attribute should be one of: pdflatex, latex, xelatex, lualatex")
  42. .should.equal true
  43. describe "without a compiler specified", ->
  44. beforeEach ->
  45. delete @validRequest.compile.options.compiler
  46. @RequestParser.parse @validRequest, (error, @data) =>
  47. it "should set the compiler to pdflatex by default", ->
  48. @data.compiler.should.equal "pdflatex"
  49. describe "without a timeout specified", ->
  50. beforeEach ->
  51. delete @validRequest.compile.options.timeout
  52. @RequestParser.parse @validRequest, (error, @data) =>
  53. it "should set the timeout to MAX_TIMEOUT", ->
  54. @data.timeout.should.equal @RequestParser.MAX_TIMEOUT * 1000
  55. describe "with a timeout larger than the maximum", ->
  56. beforeEach ->
  57. @validRequest.compile.options.timeout = @RequestParser.MAX_TIMEOUT + 1
  58. @RequestParser.parse @validRequest, (error, @data) =>
  59. it "should set the timeout to MAX_TIMEOUT", ->
  60. @data.timeout.should.equal @RequestParser.MAX_TIMEOUT * 1000
  61. describe "with a timeout", ->
  62. beforeEach ->
  63. @RequestParser.parse @validRequest, (error, @data) =>
  64. it "should set the timeout (in milliseconds)", ->
  65. @data.timeout.should.equal @validRequest.compile.options.timeout * 1000
  66. describe "with a resource without a path", ->
  67. beforeEach ->
  68. delete @validResource.path
  69. @validRequest.compile.resources.push @validResource
  70. @RequestParser.parse @validRequest, @callback
  71. it "should return an error", ->
  72. @callback.calledWith("all resources should have a path attribute")
  73. .should.equal true
  74. describe "with a resource with a path", ->
  75. beforeEach ->
  76. @validResource.path = @path = "test.tex"
  77. @validRequest.compile.resources.push @validResource
  78. @RequestParser.parse @validRequest, @callback
  79. @data = @callback.args[0][1]
  80. it "should return the path in the parsed response", ->
  81. @data.resources[0].path.should.equal @path
  82. describe "with a resource with a malformed modified date", ->
  83. beforeEach ->
  84. @validResource.modified = "not-a-date"
  85. @validRequest.compile.resources.push @validResource
  86. @RequestParser.parse @validRequest, @callback
  87. it "should return an error", ->
  88. @callback
  89. .calledWith(
  90. "resource modified date could not be understood: "+
  91. @validResource.modified
  92. )
  93. .should.equal true
  94. describe "with a resource with a valid date", ->
  95. beforeEach ->
  96. @date = "12:00 01/02/03"
  97. @validResource.modified = @date
  98. @validRequest.compile.resources.push @validResource
  99. @RequestParser.parse @validRequest, @callback
  100. @data = @callback.args[0][1]
  101. it "should return the date as a Javascript Date object", ->
  102. (@data.resources[0].modified instanceof Date).should.equal true
  103. @data.resources[0].modified.getTime().should.equal Date.parse(@date)
  104. describe "with a resource without either a content or URL attribute", ->
  105. beforeEach ->
  106. delete @validResource.url
  107. delete @validResource.content
  108. @validRequest.compile.resources.push @validResource
  109. @RequestParser.parse @validRequest, @callback
  110. it "should return an error", ->
  111. @callback.calledWith("all resources should have either a url or content attribute")
  112. .should.equal true
  113. describe "with a resource where the content is not a string", ->
  114. beforeEach ->
  115. @validResource.content = []
  116. @validRequest.compile.resources.push @validResource
  117. @RequestParser.parse (@validRequest), @callback
  118. it "should return an error", ->
  119. @callback.calledWith("content attribute should be a string")
  120. .should.equal true
  121. describe "with a resource where the url is not a string", ->
  122. beforeEach ->
  123. @validResource.url = []
  124. @validRequest.compile.resources.push @validResource
  125. @RequestParser.parse (@validRequest), @callback
  126. it "should return an error", ->
  127. @callback.calledWith("url attribute should be a string")
  128. .should.equal true
  129. describe "with a resource with a url", ->
  130. beforeEach ->
  131. @validResource.url = @url = "www.example.com"
  132. @validRequest.compile.resources.push @validResource
  133. @RequestParser.parse (@validRequest), @callback
  134. @data = @callback.args[0][1]
  135. it "should return the url in the parsed response", ->
  136. @data.resources[0].url.should.equal @url
  137. describe "with a resource with a content attribute", ->
  138. beforeEach ->
  139. @validResource.content = @content = "Hello world"
  140. @validRequest.compile.resources.push @validResource
  141. @RequestParser.parse (@validRequest), @callback
  142. @data = @callback.args[0][1]
  143. it "should return the content in the parsed response", ->
  144. @data.resources[0].content.should.equal @content
  145. describe "without a root resource path", ->
  146. beforeEach ->
  147. delete @validRequest.compile.rootResourcePath
  148. @RequestParser.parse (@validRequest), @callback
  149. @data = @callback.args[0][1]
  150. it "should set the root resource path to 'main.tex' by default", ->
  151. @data.rootResourcePath.should.equal "main.tex"
  152. describe "with a root resource path", ->
  153. beforeEach ->
  154. @validRequest.compile.rootResourcePath = @path = "test.tex"
  155. @RequestParser.parse (@validRequest), @callback
  156. @data = @callback.args[0][1]
  157. it "should return the root resource path in the parsed response", ->
  158. @data.rootResourcePath.should.equal @path
  159. describe "with a root resource path that is not a string", ->
  160. beforeEach ->
  161. @validRequest.compile.rootResourcePath = []
  162. @RequestParser.parse (@validRequest), @callback
  163. it "should return an error", ->
  164. @callback.calledWith("rootResourcePath attribute should be a string")
  165. .should.equal true
  166. describe "with a root resource path that needs escaping", ->
  167. beforeEach ->
  168. @validRequest.compile.rootResourcePath = "`rm -rf foo`.tex"
  169. @RequestParser.parse @validRequest, @callback
  170. @data = @callback.args[0][1]
  171. it "should return the escaped resource", ->
  172. @data.rootResourcePath.should.equal "rm -rf foo.tex"