CompileControllerTests.coffee 7.2 KB

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