CompileControllerTests.coffee 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. assert = require("chai").assert
  5. expect = chai.expect
  6. modulePath = "../../../../app/js/Features/Compile/CompileController.js"
  7. SandboxedModule = require('sandboxed-module')
  8. MockRequest = require "../helpers/MockRequest"
  9. MockResponse = require "../helpers/MockResponse"
  10. describe "CompileController", ->
  11. beforeEach ->
  12. @CompileManager =
  13. compile: sinon.stub()
  14. @ClsiManager = {}
  15. @UserGetter =
  16. getUser:sinon.stub()
  17. @RateLimiter = {addCount:sinon.stub()}
  18. @settings =
  19. apis:
  20. clsi:
  21. url: "clsi.example.com"
  22. clsi_priority:
  23. url: "clsi-priority.example.com"
  24. @jar = {cookie:"stuff"}
  25. @ClsiCookieManager =
  26. getCookieJar:sinon.stub().callsArgWith(1, null, @jar)
  27. @CompileController = SandboxedModule.require modulePath, requires:
  28. "settings-sharelatex": @settings
  29. "request": @request = sinon.stub()
  30. "../../models/Project": Project: @Project = {}
  31. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  32. "../../infrastructure/Metrics": @Metrics = { inc: sinon.stub() }
  33. "./CompileManager":@CompileManager
  34. "../User/UserGetter":@UserGetter
  35. "./ClsiManager": @ClsiManager
  36. "../Authentication/AuthenticationController": @AuthenticationController = {}
  37. "../../infrastructure/RateLimiter":@RateLimiter
  38. "./ClsiCookieManager":@ClsiCookieManager
  39. @project_id = "project-id"
  40. @user =
  41. features:
  42. compileGroup: "premium"
  43. compileTimeout: 100
  44. @next = sinon.stub()
  45. @req = new MockRequest()
  46. @res = new MockResponse()
  47. @AuthenticationController.getLoggedInUserId = sinon.stub().callsArgWith(1, null, @user_id = "mock-user-id")
  48. describe "compile", ->
  49. beforeEach ->
  50. @req.params =
  51. Project_id: @project_id
  52. @req.session = {}
  53. @CompileManager.compile = sinon.stub().callsArgWith(3, null, @status = "success", @outputFiles = ["mock-output-files"])
  54. describe "when not an auto compile", ->
  55. beforeEach ->
  56. @CompileController.compile @req, @res, @next
  57. it "should look up the user id", ->
  58. @AuthenticationController.getLoggedInUserId
  59. .calledWith(@req)
  60. .should.equal true
  61. it "should do the compile without the auto compile flag", ->
  62. @CompileManager.compile
  63. .calledWith(@project_id, @user_id, { isAutoCompile: false })
  64. .should.equal true
  65. it "should set the content-type of the response to application/json", ->
  66. @res.contentType
  67. .calledWith("application/json")
  68. .should.equal true
  69. it "should send a successful response reporting the status and files", ->
  70. @res.statusCode.should.equal 200
  71. @res.body.should.equal JSON.stringify({
  72. status: @status
  73. outputFiles: @outputFiles
  74. })
  75. describe "when an auto compile", ->
  76. beforeEach ->
  77. @req.query =
  78. auto_compile: "true"
  79. @CompileController.compile @req, @res, @next
  80. it "should do the compile with the auto compile flag", ->
  81. @CompileManager.compile
  82. .calledWith(@project_id, @user_id, { isAutoCompile: true })
  83. .should.equal true
  84. describe "with the draft attribute", ->
  85. beforeEach ->
  86. @req.body =
  87. draft: true
  88. @CompileController.compile @req, @res, @next
  89. it "should do the compile without the draft compile flag", ->
  90. @CompileManager.compile
  91. .calledWith(@project_id, @user_id, { isAutoCompile: false, draft: true })
  92. .should.equal true
  93. describe "downloadPdf", ->
  94. beforeEach ->
  95. @req.params =
  96. Project_id: @project_id
  97. @project =
  98. getSafeProjectName: () => @safe_name = "safe-name"
  99. @req.query = {pdfng:true}
  100. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  101. describe "when downloading for embedding", ->
  102. beforeEach ->
  103. @CompileController.proxyToClsi = sinon.stub()
  104. @RateLimiter.addCount.callsArgWith(1, null, true)
  105. @CompileController.downloadPdf(@req, @res, @next)
  106. it "should look up the project", ->
  107. @Project.findById
  108. .calledWith(@project_id, {name: 1})
  109. .should.equal true
  110. it "should set the content-type of the response to application/pdf", ->
  111. @res.contentType
  112. .calledWith("application/pdf")
  113. .should.equal true
  114. it "should set the content-disposition header with the project name", ->
  115. @res.header
  116. .calledWith("Content-Disposition", "filename=#{@safe_name}.pdf")
  117. .should.equal true
  118. it "should increment the pdf-downloads metric", ->
  119. @Metrics.inc
  120. .calledWith("pdf-downloads")
  121. .should.equal true
  122. it "should proxy the PDF from the CLSI", ->
  123. @CompileController.proxyToClsi.calledWith(@project_id, "/project/#{@project_id}/user/#{@user_id}/output/output.pdf", @req, @res, @next).should.equal true
  124. describe "when the pdf is not going to be used in pdfjs viewer", ->
  125. it "should check the rate limiter when pdfng is not set", (done)->
  126. @req.query = {}
  127. @RateLimiter.addCount.callsArgWith(1, null, true)
  128. @CompileController.proxyToClsi = (project_id, url)=>
  129. @RateLimiter.addCount.args[0][0].throttle.should.equal 1000
  130. done()
  131. @CompileController.downloadPdf @req, @res
  132. it "should check the rate limiter when pdfng is false", (done)->
  133. @req.query = {pdfng:false}
  134. @RateLimiter.addCount.callsArgWith(1, null, true)
  135. @CompileController.proxyToClsi = (project_id, url)=>
  136. @RateLimiter.addCount.args[0][0].throttle.should.equal 1000
  137. done()
  138. @CompileController.downloadPdf @req, @res
  139. describe "proxyToClsi", ->
  140. beforeEach ->
  141. @request.returns(@proxy = {
  142. pipe: sinon.stub()
  143. on: sinon.stub()
  144. })
  145. @upstream =
  146. statusCode: 204
  147. headers: { "mock": "header" }
  148. @req.method = "mock-method"
  149. @req.headers = {
  150. 'Mock': 'Headers',
  151. 'Range': '123-456'
  152. 'If-Range': 'abcdef'
  153. 'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
  154. }
  155. describe "old pdf viewer", ->
  156. describe "user with standard priority", ->
  157. beforeEach ->
  158. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  159. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  160. it "should open a request to the CLSI", ->
  161. @request
  162. .calledWith(
  163. jar:@jar
  164. method: @req.method
  165. url: "#{@settings.apis.clsi.url}#{@url}",
  166. timeout: 60 * 1000
  167. )
  168. .should.equal true
  169. it "should pass the request on to the client", ->
  170. @proxy.pipe
  171. .calledWith(@res)
  172. .should.equal true
  173. it "should bind an error handle to the request proxy", ->
  174. @proxy.on.calledWith("error").should.equal true
  175. describe "user with priority compile", ->
  176. beforeEach ->
  177. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "priority"})
  178. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  179. describe "user with standard priority via query string", ->
  180. beforeEach ->
  181. @req.query = {compileGroup: 'standard'}
  182. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  183. it "should open a request to the CLSI", ->
  184. @request
  185. .calledWith(
  186. jar:@jar
  187. method: @req.method
  188. url: "#{@settings.apis.clsi.url}#{@url}",
  189. timeout: 60 * 1000
  190. )
  191. .should.equal true
  192. it "should pass the request on to the client", ->
  193. @proxy.pipe
  194. .calledWith(@res)
  195. .should.equal true
  196. it "should bind an error handle to the request proxy", ->
  197. @proxy.on.calledWith("error").should.equal true
  198. describe "user with non-existent priority via query string", ->
  199. beforeEach ->
  200. @req.query = {compileGroup: 'foobar'}
  201. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  202. it "should proxy to the standard url", ()->
  203. @request
  204. .calledWith(
  205. jar:@jar
  206. method: @req.method
  207. url: "#{@settings.apis.clsi.url}#{@url}",
  208. timeout: 60 * 1000
  209. )
  210. .should.equal true
  211. describe "user with build parameter via query string", ->
  212. beforeEach ->
  213. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  214. @req.query = {build: 1234}
  215. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  216. it "should proxy to the standard url without the build parameter", ()->
  217. @request
  218. .calledWith(
  219. jar:@jar
  220. method: @req.method
  221. url: "#{@settings.apis.clsi.url}#{@url}",
  222. timeout: 60 * 1000
  223. )
  224. .should.equal true
  225. describe "new pdf viewer", ->
  226. beforeEach ->
  227. @req.query = {pdfng: true}
  228. describe "user with standard priority", ->
  229. beforeEach ->
  230. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  231. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  232. it "should open a request to the CLSI", ->
  233. @request
  234. .calledWith(
  235. jar:@jar
  236. method: @req.method
  237. url: "#{@settings.apis.clsi.url}#{@url}",
  238. timeout: 60 * 1000
  239. headers: {
  240. 'Range': '123-456'
  241. 'If-Range': 'abcdef'
  242. 'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
  243. }
  244. )
  245. .should.equal true
  246. it "should pass the request on to the client", ->
  247. @proxy.pipe
  248. .calledWith(@res)
  249. .should.equal true
  250. it "should bind an error handle to the request proxy", ->
  251. @proxy.on.calledWith("error").should.equal true
  252. describe "user with build parameter via query string", ->
  253. beforeEach ->
  254. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  255. @req.query = {build: 1234, pdfng: true}
  256. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  257. it "should proxy to the standard url with the build parameter", ()->
  258. @request.calledWith(
  259. jar:@jar
  260. method: @req.method
  261. qs: {build: 1234}
  262. url: "#{@settings.apis.clsi.url}#{@url}",
  263. timeout: 60 * 1000
  264. headers: {
  265. 'Range': '123-456'
  266. 'If-Range': 'abcdef'
  267. 'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
  268. }
  269. )
  270. .should.equal true
  271. describe "deleteAuxFiles", ->
  272. beforeEach ->
  273. @CompileManager.deleteAuxFiles = sinon.stub().callsArg(2)
  274. @req.params =
  275. Project_id: @project_id
  276. @res.sendStatus = sinon.stub()
  277. @CompileController.deleteAuxFiles @req, @res, @next
  278. it "should proxy to the CLSI", ->
  279. @CompileManager.deleteAuxFiles
  280. .calledWith(@project_id)
  281. .should.equal true
  282. it "should return a 200", ->
  283. @res.sendStatus
  284. .calledWith(200)
  285. .should.equal true
  286. describe "compileAndDownloadPdf", ->
  287. beforeEach ->
  288. @req =
  289. params:
  290. project_id:@project_id
  291. @CompileManager.compile.callsArgWith(3)
  292. @CompileController.proxyToClsi = sinon.stub()
  293. @res =
  294. send:=>
  295. it "should call compile in the compile manager", (done)->
  296. @CompileController.compileAndDownloadPdf @req, @res
  297. @CompileManager.compile.calledWith(@project_id).should.equal true
  298. done()
  299. it "should proxy the res to the clsi with correct url", (done)->
  300. @CompileController.compileAndDownloadPdf @req, @res
  301. sinon.assert.calledWith @CompileController.proxyToClsi, @project_id, "/project/#{@project_id}/output/output.pdf", @req, @res
  302. @CompileController.proxyToClsi.calledWith(@project_id, "/project/#{@project_id}/output/output.pdf", @req, @res).should.equal true
  303. done()
  304. describe "wordCount", ->
  305. beforeEach ->
  306. @CompileManager.wordCount = sinon.stub().callsArgWith(3, null, {content:"body"})
  307. @req.params =
  308. Project_id: @project_id
  309. @res.send = sinon.stub()
  310. @res.contentType = sinon.stub()
  311. @CompileController.wordCount @req, @res, @next
  312. it "should proxy to the CLSI", ->
  313. @CompileManager.wordCount
  314. .calledWith(@project_id, @user_id, false)
  315. .should.equal true
  316. it "should return a 200 and body", ->
  317. @res.send
  318. .calledWith({content:"body"})
  319. .should.equal true