CompileControllerTests.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. @project_id = "project-id"
  24. @next = sinon.stub()
  25. @req = new MockRequest()
  26. @res = new MockResponse()
  27. describe "downloadPdf", ->
  28. beforeEach ->
  29. @req.params =
  30. Project_id: @project_id
  31. @project =
  32. getSafeProjectName: () => @safe_name = "safe-name"
  33. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  34. describe "when downloading for embedding", ->
  35. beforeEach ->
  36. @project.useClsi2 = true
  37. @CompileController.proxyToClsi = sinon.stub()
  38. @CompileController.downloadPdf(@req, @res, @next)
  39. it "should look up the project", ->
  40. @Project.findById
  41. .calledWith(@project_id, {name: 1})
  42. .should.equal true
  43. it "should set the content-type of the response to application/pdf", ->
  44. @res.contentType
  45. .calledWith("application/pdf")
  46. .should.equal true
  47. it "should set the content-disposition header with the project name", ->
  48. @res.header
  49. .calledWith("Content-Disposition", "filename=#{@safe_name}.pdf")
  50. .should.equal true
  51. it "should increment the pdf-downloads metric", ->
  52. @Metrics.inc
  53. .calledWith("pdf-downloads")
  54. .should.equal true
  55. it "should proxy the PDF from the CLSI", ->
  56. @CompileController.proxyToClsi
  57. .calledWith("/project/#{@project_id}/output/output.pdf", @req, @res, @next)
  58. .should.equal true
  59. describe "proxyToClsi", ->
  60. beforeEach ->
  61. @request.returns(@proxy = {
  62. pipe: sinon.stub()
  63. on: sinon.stub()
  64. })
  65. @upstream =
  66. statusCode: 204
  67. headers: { "mock": "header" }
  68. @req.method = "mock-method"
  69. @CompileController.proxyToClsi(@url = "/test", @req, @res, @next)
  70. it "should open a request to the CLSI", ->
  71. @request
  72. .calledWith(
  73. method: @req.method
  74. url: "#{@settings.apis.clsi.url}#{@url}",
  75. timeout: 60 * 1000
  76. )
  77. .should.equal true
  78. it "should pass the request on to the client", ->
  79. @proxy.pipe
  80. .calledWith(@res)
  81. .should.equal true
  82. it "should bind an error handle to the request proxy", ->
  83. @proxy.on.calledWith("error").should.equal true
  84. describe "deleteAuxFiles", ->
  85. beforeEach ->
  86. @CompileController.proxyToClsi = sinon.stub()
  87. @req.params =
  88. Project_id: @project_id
  89. @CompileController.deleteAuxFiles @req, @res, @next
  90. it "should proxy to the CLSI", ->
  91. @CompileController.proxyToClsi
  92. .calledWith("/project/#{@project_id}", @req, @res, @next)
  93. .should.equal true
  94. describe "compileAndDownloadPdf", ->
  95. beforeEach ->
  96. @req =
  97. params:
  98. project_id:@project_id
  99. @CompileManager.compile.callsArgWith(3)
  100. @CompileController.proxyToClsi = sinon.stub()
  101. @res =
  102. send:=>
  103. it "should call compile in the compile manager", (done)->
  104. @CompileController.compileAndDownloadPdf @req, @res
  105. @CompileManager.compile.calledWith(@project_id).should.equal true
  106. done()
  107. it "should proxy the res to the clsi with correct url", (done)->
  108. @CompileController.compileAndDownloadPdf @req, @res
  109. @CompileController.proxyToClsi.calledWith("/project/#{@project_id}/output/output.pdf", @req, @res).should.equal true
  110. done()