CompileControllerTests.coffee 5.7 KB

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