CompileControllerTests.coffee 5.6 KB

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