CompileControllerTests.coffee 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. clsi_priority:
  23. url: "clsi-priority.example.com"
  24. "request": @request = sinon.stub()
  25. "../../models/Project": Project: @Project = {}
  26. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  27. "../../infrastructure/Metrics": @Metrics = { inc: sinon.stub() }
  28. "./CompileManager":@CompileManager
  29. "../User/UserGetter":@UserGetter
  30. "./ClsiManager": @ClsiManager
  31. "../Authentication/AuthenticationController": @AuthenticationController = {}
  32. @project_id = "project-id"
  33. @user =
  34. features:
  35. compileGroup: "premium"
  36. compileTimeout: 100
  37. @next = sinon.stub()
  38. @req = new MockRequest()
  39. @res = new MockResponse()
  40. describe "compile", ->
  41. describe "when not an auto compile", ->
  42. beforeEach ->
  43. @req.params =
  44. Project_id: @project_id
  45. @req.session = {}
  46. @AuthenticationController.getLoggedInUserId = sinon.stub().callsArgWith(1, null, @user_id = "mock-user-id")
  47. @CompileManager.compile = sinon.stub().callsArgWith(3, null, @status = "success", @outputFiles = ["mock-output-files"], @output = "mock-output")
  48. @CompileController.compile @req, @res, @next
  49. it "should look up the user id", ->
  50. @AuthenticationController.getLoggedInUserId
  51. .calledWith(@req)
  52. .should.equal true
  53. it "should do the compile without the auto compile flag", ->
  54. @CompileManager.compile
  55. .calledWith(@project_id, @user_id, { isAutoCompile: false })
  56. .should.equal true
  57. it "should set the content-type of the response to application/json", ->
  58. @res.contentType
  59. .calledWith("application/json")
  60. .should.equal true
  61. it "should send a successful response reporting the status and files", ->
  62. @res.statusCode.should.equal 200
  63. @res.body.should.equal JSON.stringify({
  64. status: @status
  65. outputFiles: @outputFiles
  66. })
  67. describe "when an auto compile", ->
  68. beforeEach ->
  69. @req.params =
  70. Project_id: @project_id
  71. @req.query =
  72. auto_compile: "true"
  73. @AuthenticationController.getLoggedInUserId = sinon.stub().callsArgWith(1, null, @user_id = "mock-user-id")
  74. @CompileManager.compile = sinon.stub().callsArgWith(3, null, @status = "success", @outputFiles = ["mock-output-files"])
  75. @CompileController.compile @req, @res, @next
  76. it "should do the compile with the auto compile flag", ->
  77. @CompileManager.compile
  78. .calledWith(@project_id, @user_id, { isAutoCompile: true })
  79. .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_id, "/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. describe "user with standard priority", ->
  123. beforeEach ->
  124. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  125. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  126. it "should open a request to the CLSI", ->
  127. @request
  128. .calledWith(
  129. method: @req.method
  130. url: "#{@settings.apis.clsi.url}#{@url}",
  131. timeout: 60 * 1000
  132. )
  133. .should.equal true
  134. it "should pass the request on to the client", ->
  135. @proxy.pipe
  136. .calledWith(@res)
  137. .should.equal true
  138. it "should bind an error handle to the request proxy", ->
  139. @proxy.on.calledWith("error").should.equal true
  140. describe "user with priority compile", ->
  141. beforeEach ->
  142. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "priority"})
  143. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  144. it "should proxy to the priorty url if the user has the feature", ()->
  145. @request
  146. .calledWith(
  147. method: @req.method
  148. url: "#{@settings.apis.clsi_priority.url}#{@url}",
  149. timeout: 60 * 1000
  150. )
  151. .should.equal true
  152. describe "deleteAuxFiles", ->
  153. beforeEach ->
  154. @CompileManager.deleteAuxFiles = sinon.stub().callsArg(1)
  155. @req.params =
  156. Project_id: @project_id
  157. @res.send = sinon.stub()
  158. @CompileController.deleteAuxFiles @req, @res, @next
  159. it "should proxy to the CLSI", ->
  160. @CompileManager.deleteAuxFiles
  161. .calledWith(@project_id)
  162. .should.equal true
  163. it "should return a 200", ->
  164. @res.send
  165. .calledWith(200)
  166. .should.equal true
  167. describe "compileAndDownloadPdf", ->
  168. beforeEach ->
  169. @req =
  170. params:
  171. project_id:@project_id
  172. @CompileManager.compile.callsArgWith(3)
  173. @CompileController.proxyToClsi = sinon.stub()
  174. @res =
  175. send:=>
  176. it "should call compile in the compile manager", (done)->
  177. @CompileController.compileAndDownloadPdf @req, @res
  178. @CompileManager.compile.calledWith(@project_id).should.equal true
  179. done()
  180. it "should proxy the res to the clsi with correct url", (done)->
  181. @CompileController.compileAndDownloadPdf @req, @res
  182. @CompileController.proxyToClsi.calledWith(@project_id, "/project/#{@project_id}/output/output.pdf", @req, @res).should.equal true
  183. done()