CompileControllerTests.coffee 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. @CompileController = SandboxedModule.require modulePath, requires:
  18. "settings-sharelatex": @settings =
  19. apis:
  20. clsi:
  21. url: "clsi.example.com"
  22. "request": @request = sinon.stub()
  23. "../../models/Project": Project: @Project = {}
  24. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  25. "../../infrastructure/Metrics": @Metrics = { inc: sinon.stub() }
  26. "./CompileManager":@CompileManager
  27. "../User/UserGetter":@UserGetter
  28. "./ClsiManager": @ClsiManager
  29. "../Authentication/AuthenticationController": @AuthenticationController = {}
  30. @project_id = "project-id"
  31. @user =
  32. features:
  33. compileGroup: "premium"
  34. compileTimeout: 100
  35. @next = sinon.stub()
  36. @req = new MockRequest()
  37. @res = new MockResponse()
  38. describe "compile", ->
  39. describe "when not an auto compile", ->
  40. beforeEach ->
  41. @req.params =
  42. Project_id: @project_id
  43. @AuthenticationController.getLoggedInUserId = sinon.stub().callsArgWith(1, null, @user_id = "mock-user-id")
  44. @CompileManager.compile = sinon.stub().callsArgWith(3, null, @status = "success", @outputFiles = ["mock-output-files"])
  45. @UserGetter.getUser.callsArgWith(2, null, @user)
  46. @CompileController.compile @req, @res, @next
  47. it "should look up the user id", ->
  48. @AuthenticationController.getLoggedInUserId
  49. .calledWith(@req)
  50. .should.equal true
  51. it "should do the compile without the auto compile flag", ->
  52. @CompileManager.compile
  53. .calledWith(@project_id, @user_id, { isAutoCompile: false, settingsOverride:{timeout:@user.features.compileTimeout, compiler:@user.features.compileGroup} })
  54. .should.equal true
  55. it "should set the content-type of the response to application/json", ->
  56. @res.contentType
  57. .calledWith("application/json")
  58. .should.equal true
  59. it "should send a successful response reporting the status and files", ->
  60. @res.statusCode.should.equal 200
  61. @res.body.should.equal JSON.stringify({
  62. status: @status
  63. outputFiles: @outputFiles
  64. })
  65. it "should get the compile timeout from the users features",->
  66. @UserGetter.getUser.args[0][0].should.equal @user_id
  67. assert.deepEqual @UserGetter.getUser.args[0][1], {"features.compileGroup":1, "features.compileTimeout":1}
  68. it "should set the timeout", ->
  69. assert @res.timout > 1000 * 60 * 3
  70. describe "when an auto compile", ->
  71. beforeEach ->
  72. @req.params =
  73. Project_id: @project_id
  74. @req.query =
  75. auto_compile: "true"
  76. @AuthenticationController.getLoggedInUserId = sinon.stub().callsArgWith(1, null, @user_id = "mock-user-id")
  77. @CompileManager.compile = sinon.stub().callsArgWith(3, null, @status = "success", @outputFiles = ["mock-output-files"])
  78. @UserGetter.getUser.callsArgWith(2, null, @user)
  79. @CompileController.compile @req, @res, @next
  80. it "should do the compile with the auto compile flag", ->
  81. @CompileManager.compile.calledWith(@project_id, @user_id, { isAutoCompile: true, settingsOverride:{timeout:@user.features.compileTimeout, compiler:@user.features.compileGroup} }).should.equal true
  82. describe "downloadPdf", ->
  83. beforeEach ->
  84. @req.params =
  85. Project_id: @project_id
  86. @project =
  87. getSafeProjectName: () => @safe_name = "safe-name"
  88. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  89. describe "when downloading for embedding", ->
  90. beforeEach ->
  91. @project.useClsi2 = true
  92. @CompileController.proxyToClsi = sinon.stub()
  93. @CompileController.downloadPdf(@req, @res, @next)
  94. it "should look up the project", ->
  95. @Project.findById
  96. .calledWith(@project_id, {name: 1})
  97. .should.equal true
  98. it "should set the content-type of the response to application/pdf", ->
  99. @res.contentType
  100. .calledWith("application/pdf")
  101. .should.equal true
  102. it "should set the content-disposition header with the project name", ->
  103. @res.header
  104. .calledWith("Content-Disposition", "filename=#{@safe_name}.pdf")
  105. .should.equal true
  106. it "should increment the pdf-downloads metric", ->
  107. @Metrics.inc
  108. .calledWith("pdf-downloads")
  109. .should.equal true
  110. it "should proxy the PDF from the CLSI", ->
  111. @CompileController.proxyToClsi
  112. .calledWith("/project/#{@project_id}/output/output.pdf", @req, @res, @next)
  113. .should.equal true
  114. describe "proxyToClsi", ->
  115. beforeEach ->
  116. @request.returns(@proxy = {
  117. pipe: sinon.stub()
  118. on: sinon.stub()
  119. })
  120. @upstream =
  121. statusCode: 204
  122. headers: { "mock": "header" }
  123. @req.method = "mock-method"
  124. @CompileController.proxyToClsi(@url = "/test", @req, @res, @next)
  125. it "should open a request to the CLSI", ->
  126. @request
  127. .calledWith(
  128. method: @req.method
  129. url: "#{@settings.apis.clsi.url}#{@url}",
  130. timeout: 60 * 1000
  131. )
  132. .should.equal true
  133. it "should pass the request on to the client", ->
  134. @proxy.pipe
  135. .calledWith(@res)
  136. .should.equal true
  137. it "should bind an error handle to the request proxy", ->
  138. @proxy.on.calledWith("error").should.equal true
  139. describe "deleteAuxFiles", ->
  140. beforeEach ->
  141. @ClsiManager.deleteAuxFiles = sinon.stub().callsArg(1)
  142. @req.params =
  143. Project_id: @project_id
  144. @res.send = sinon.stub()
  145. @CompileController.deleteAuxFiles @req, @res, @next
  146. it "should proxy to the CLSI", ->
  147. @ClsiManager.deleteAuxFiles
  148. .calledWith(@project_id)
  149. .should.equal true
  150. it "should return a 200", ->
  151. @res.send
  152. .calledWith(200)
  153. .should.equal true
  154. describe "compileAndDownloadPdf", ->
  155. beforeEach ->
  156. @req =
  157. params:
  158. project_id:@project_id
  159. @CompileManager.compile.callsArgWith(3)
  160. @CompileController.proxyToClsi = sinon.stub()
  161. @res =
  162. send:=>
  163. it "should call compile in the compile manager", (done)->
  164. @CompileController.compileAndDownloadPdf @req, @res
  165. @CompileManager.compile.calledWith(@project_id).should.equal true
  166. done()
  167. it "should proxy the res to the clsi with correct url", (done)->
  168. @CompileController.compileAndDownloadPdf @req, @res
  169. @CompileController.proxyToClsi.calledWith("/project/#{@project_id}/output/output.pdf", @req, @res).should.equal true
  170. done()