CompileControllerTests.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 = {}
  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.get = sinon.stub().returns(@proxy = {
  62. pipe: sinon.stub()
  63. on: sinon.stub()
  64. })
  65. @CompileController.proxyToClsi(@url = "/test", @req, @res, @next)
  66. it "should open a request to the CLSI", ->
  67. @request.get
  68. .calledWith(url: "#{@settings.apis.clsi.url}#{@url}", timeout: 60 * 1000)
  69. .should.equal true
  70. it "should pass the request on to the client", ->
  71. @proxy.pipe
  72. .calledWith(@res)
  73. .should.equal true
  74. it "should bind an error handle to the request proxy", ->
  75. @proxy.on.calledWith("error").should.equal true
  76. describe "compileAndDownloadPdf", ->
  77. beforeEach ->
  78. @req =
  79. params:
  80. project_id:@project_id
  81. @CompileManager.compile.callsArgWith(3)
  82. @CompileController.proxyToClsi = sinon.stub()
  83. @res =
  84. send:=>
  85. it "should call compile in the compile manager", (done)->
  86. @CompileController.compileAndDownloadPdf @req, @res
  87. @CompileManager.compile.calledWith(@project_id).should.equal true
  88. done()
  89. it "should proxy the res to the clsi with correct url", (done)->
  90. @CompileController.compileAndDownloadPdf @req, @res
  91. @CompileController.proxyToClsi.calledWith("/project/#{@project_id}/output/output.pdf", @req, @res).should.equal true
  92. done()