CompileControllerTests.coffee 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.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"])
  48. @UserGetter.getUser.callsArgWith(2, null, @user)
  49. @CompileController.compile @req, @res, @next
  50. it "should look up the user id", ->
  51. @AuthenticationController.getLoggedInUserId
  52. .calledWith(@req)
  53. .should.equal true
  54. it "should do the compile without the auto compile flag", ->
  55. @CompileManager.compile
  56. .calledWith(@project_id, @user_id, { isAutoCompile: false, settingsOverride:{timeout:@user.features.compileTimeout, compiler:@user.features.compileGroup} })
  57. .should.equal true
  58. it "should set the content-type of the response to application/json", ->
  59. @res.contentType
  60. .calledWith("application/json")
  61. .should.equal true
  62. it "should send a successful response reporting the status and files", ->
  63. @res.statusCode.should.equal 200
  64. @res.body.should.equal JSON.stringify({
  65. status: @status
  66. outputFiles: @outputFiles
  67. })
  68. it "should get the compile timeout from the users features",->
  69. @UserGetter.getUser.args[0][0].should.equal @user_id
  70. assert.deepEqual @UserGetter.getUser.args[0][1], {"features.compileGroup":1, "features.compileTimeout":1}
  71. it "should put the compile group on the req", ->
  72. @req.session.compileGroup.should.equal @user.features.compileGroup
  73. it "should set the timeout", ->
  74. assert @res.timout > 1000 * 60 * 3
  75. describe "when an auto compile", ->
  76. beforeEach ->
  77. @req.params =
  78. Project_id: @project_id
  79. @req.query =
  80. auto_compile: "true"
  81. @AuthenticationController.getLoggedInUserId = sinon.stub().callsArgWith(1, null, @user_id = "mock-user-id")
  82. @CompileManager.compile = sinon.stub().callsArgWith(3, null, @status = "success", @outputFiles = ["mock-output-files"])
  83. @UserGetter.getUser.callsArgWith(2, null, @user)
  84. @CompileController.compile @req, @res, @next
  85. it "should do the compile with the auto compile flag", ->
  86. @CompileManager.compile.calledWith(@project_id, @user_id, { isAutoCompile: true, settingsOverride:{timeout:@user.features.compileTimeout, compiler:@user.features.compileGroup} }).should.equal true
  87. describe "downloadPdf", ->
  88. beforeEach ->
  89. @req.params =
  90. Project_id: @project_id
  91. @project =
  92. getSafeProjectName: () => @safe_name = "safe-name"
  93. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  94. describe "when downloading for embedding", ->
  95. beforeEach ->
  96. @project.useClsi2 = true
  97. @CompileController.proxyToClsi = sinon.stub()
  98. @CompileController.downloadPdf(@req, @res, @next)
  99. it "should look up the project", ->
  100. @Project.findById
  101. .calledWith(@project_id, {name: 1})
  102. .should.equal true
  103. it "should set the content-type of the response to application/pdf", ->
  104. @res.contentType
  105. .calledWith("application/pdf")
  106. .should.equal true
  107. it "should set the content-disposition header with the project name", ->
  108. @res.header
  109. .calledWith("Content-Disposition", "filename=#{@safe_name}.pdf")
  110. .should.equal true
  111. it "should increment the pdf-downloads metric", ->
  112. @Metrics.inc
  113. .calledWith("pdf-downloads")
  114. .should.equal true
  115. it "should proxy the PDF from the CLSI", ->
  116. @CompileController.proxyToClsi
  117. .calledWith("/project/#{@project_id}/output/output.pdf", @req, @res, @next)
  118. .should.equal true
  119. describe "proxyToClsi", ->
  120. beforeEach ->
  121. @request.returns(@proxy = {
  122. pipe: sinon.stub()
  123. on: sinon.stub()
  124. })
  125. @upstream =
  126. statusCode: 204
  127. headers: { "mock": "header" }
  128. @req.method = "mock-method"
  129. describe "user with standard priority", ->
  130. beforeEach ->
  131. @UserGetter.getUser.callsArgWith(2, null, @user)
  132. @CompileController.proxyToClsi(@url = "/test", @req, @res, @next)
  133. it "should open a request to the CLSI", ->
  134. @request
  135. .calledWith(
  136. method: @req.method
  137. url: "#{@settings.apis.clsi.url}#{@url}",
  138. timeout: 60 * 1000
  139. )
  140. .should.equal true
  141. it "should pass the request on to the client", ->
  142. @proxy.pipe
  143. .calledWith(@res)
  144. .should.equal true
  145. it "should bind an error handle to the request proxy", ->
  146. @proxy.on.calledWith("error").should.equal true
  147. describe "user with priority compile", ->
  148. beforeEach ->
  149. @req.session.compileGroup = "priority"
  150. @UserGetter.getUser.callsArgWith(2, null, @user)
  151. @CompileController.proxyToClsi(@url = "/test", @req, @res, @next)
  152. it "should proxy to the priorty url if the user has the feature", ()->
  153. @request
  154. .calledWith(
  155. method: @req.method
  156. url: "#{@settings.apis.clsi_priority.url}#{@url}",
  157. timeout: 60 * 1000
  158. )
  159. .should.equal true
  160. describe "deleteAuxFiles", ->
  161. beforeEach ->
  162. @ClsiManager.deleteAuxFiles = sinon.stub().callsArg(1)
  163. @req.params =
  164. Project_id: @project_id
  165. @res.send = sinon.stub()
  166. @CompileController.deleteAuxFiles @req, @res, @next
  167. it "should proxy to the CLSI", ->
  168. @ClsiManager.deleteAuxFiles
  169. .calledWith(@project_id)
  170. .should.equal true
  171. it "should return a 200", ->
  172. @res.send
  173. .calledWith(200)
  174. .should.equal true
  175. describe "compileAndDownloadPdf", ->
  176. beforeEach ->
  177. @req =
  178. params:
  179. project_id:@project_id
  180. @CompileManager.compile.callsArgWith(3)
  181. @CompileController.proxyToClsi = sinon.stub()
  182. @res =
  183. send:=>
  184. it "should call compile in the compile manager", (done)->
  185. @CompileController.compileAndDownloadPdf @req, @res
  186. @CompileManager.compile.calledWith(@project_id).should.equal true
  187. done()
  188. it "should proxy the res to the clsi with correct url", (done)->
  189. @CompileController.compileAndDownloadPdf @req, @res
  190. @CompileController.proxyToClsi.calledWith("/project/#{@project_id}/output/output.pdf", @req, @res).should.equal true
  191. done()