CompileControllerTests.coffee 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. describe "when an auto compile", ->
  69. beforeEach ->
  70. @req.params =
  71. Project_id: @project_id
  72. @req.query =
  73. auto_compile: "true"
  74. @AuthenticationController.getLoggedInUserId = sinon.stub().callsArgWith(1, null, @user_id = "mock-user-id")
  75. @CompileManager.compile = sinon.stub().callsArgWith(3, null, @status = "success", @outputFiles = ["mock-output-files"])
  76. @UserGetter.getUser.callsArgWith(2, null, @user)
  77. @CompileController.compile @req, @res, @next
  78. it "should do the compile with the auto compile flag", ->
  79. @CompileManager.compile.calledWith(@project_id, @user_id, { isAutoCompile: true, settingsOverride:{timeout:@user.features.compileTimeout, compiler:@user.features.compileGroup} }).should.equal true
  80. describe "downloadPdf", ->
  81. beforeEach ->
  82. @req.params =
  83. Project_id: @project_id
  84. @project =
  85. getSafeProjectName: () => @safe_name = "safe-name"
  86. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  87. describe "when downloading for embedding", ->
  88. beforeEach ->
  89. @project.useClsi2 = true
  90. @CompileController.proxyToClsi = sinon.stub()
  91. @CompileController.downloadPdf(@req, @res, @next)
  92. it "should look up the project", ->
  93. @Project.findById
  94. .calledWith(@project_id, {name: 1})
  95. .should.equal true
  96. it "should set the content-type of the response to application/pdf", ->
  97. @res.contentType
  98. .calledWith("application/pdf")
  99. .should.equal true
  100. it "should set the content-disposition header with the project name", ->
  101. @res.header
  102. .calledWith("Content-Disposition", "filename=#{@safe_name}.pdf")
  103. .should.equal true
  104. it "should increment the pdf-downloads metric", ->
  105. @Metrics.inc
  106. .calledWith("pdf-downloads")
  107. .should.equal true
  108. it "should proxy the PDF from the CLSI", ->
  109. @CompileController.proxyToClsi
  110. .calledWith("/project/#{@project_id}/output/output.pdf", @req, @res, @next)
  111. .should.equal true
  112. describe "proxyToClsi", ->
  113. beforeEach ->
  114. @request.returns(@proxy = {
  115. pipe: sinon.stub()
  116. on: sinon.stub()
  117. })
  118. @upstream =
  119. statusCode: 204
  120. headers: { "mock": "header" }
  121. @req.method = "mock-method"
  122. @CompileController.proxyToClsi(@url = "/test", @req, @res, @next)
  123. it "should open a request to the CLSI", ->
  124. @request
  125. .calledWith(
  126. method: @req.method
  127. url: "#{@settings.apis.clsi.url}#{@url}",
  128. timeout: 60 * 1000
  129. )
  130. .should.equal true
  131. it "should pass the request on to the client", ->
  132. @proxy.pipe
  133. .calledWith(@res)
  134. .should.equal true
  135. it "should bind an error handle to the request proxy", ->
  136. @proxy.on.calledWith("error").should.equal true
  137. describe "deleteAuxFiles", ->
  138. beforeEach ->
  139. @ClsiManager.deleteAuxFiles = sinon.stub().callsArg(1)
  140. @req.params =
  141. Project_id: @project_id
  142. @res.send = sinon.stub()
  143. @CompileController.deleteAuxFiles @req, @res, @next
  144. it "should proxy to the CLSI", ->
  145. @ClsiManager.deleteAuxFiles
  146. .calledWith(@project_id)
  147. .should.equal true
  148. it "should return a 200", ->
  149. @res.send
  150. .calledWith(200)
  151. .should.equal true
  152. describe "compileAndDownloadPdf", ->
  153. beforeEach ->
  154. @req =
  155. params:
  156. project_id:@project_id
  157. @CompileManager.compile.callsArgWith(3)
  158. @CompileController.proxyToClsi = sinon.stub()
  159. @res =
  160. send:=>
  161. it "should call compile in the compile manager", (done)->
  162. @CompileController.compileAndDownloadPdf @req, @res
  163. @CompileManager.compile.calledWith(@project_id).should.equal true
  164. done()
  165. it "should proxy the res to the clsi with correct url", (done)->
  166. @CompileController.compileAndDownloadPdf @req, @res
  167. @CompileController.proxyToClsi.calledWith("/project/#{@project_id}/output/output.pdf", @req, @res).should.equal true
  168. done()