CompileControllerTests.coffee 5.8 KB

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