CompileControllerTests.coffee 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/CompileController'
  5. tk = require("timekeeper")
  6. describe "CompileController", ->
  7. beforeEach ->
  8. @CompileController = SandboxedModule.require modulePath, requires:
  9. "./CompileManager": @CompileManager = {}
  10. "./RequestParser": @RequestParser = {}
  11. "settings-sharelatex": @Settings =
  12. apis:
  13. clsi:
  14. url: "http://clsi.example.com"
  15. "./ProjectPersistenceManager": @ProjectPersistenceManager = {}
  16. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  17. @Settings.externalUrl = "http://www.example.com"
  18. @req = {}
  19. @res = {}
  20. describe "compile", ->
  21. beforeEach ->
  22. @req.body = {
  23. compile: "mock-body"
  24. }
  25. @req.params =
  26. project_id: @project_id = "project-id-123"
  27. @request = {
  28. compile: "mock-parsed-request"
  29. }
  30. @request_with_project_id =
  31. compile: @request.compile
  32. project_id: @project_id
  33. @output_files = [{
  34. path: "output.pdf"
  35. type: "pdf"
  36. }, {
  37. path: "output.log"
  38. type: "log"
  39. }]
  40. @RequestParser.parse = sinon.stub().callsArgWith(1, null, @request)
  41. @ProjectPersistenceManager.markProjectAsJustAccessed = sinon.stub().callsArg(1)
  42. @res.send = sinon.stub()
  43. describe "successfully", ->
  44. beforeEach ->
  45. @CompileManager.doCompile = sinon.stub().callsArgWith(1, null, @output_files)
  46. @CompileController.compile @req, @res
  47. it "should parse the request", ->
  48. @RequestParser.parse
  49. .calledWith(@req.body)
  50. .should.equal true
  51. it "should run the compile for the specified project", ->
  52. @CompileManager.doCompile
  53. .calledWith(@request_with_project_id)
  54. .should.equal true
  55. it "should mark the project as accessed", ->
  56. @ProjectPersistenceManager.markProjectAsJustAccessed
  57. .calledWith(@project_id)
  58. .should.equal true
  59. it "should return the JSON response", ->
  60. @res.send
  61. .calledWith(JSON.stringify
  62. compile:
  63. status: "success"
  64. error: null
  65. outputFiles: @output_files.map (file) =>
  66. url: "#{@Settings.apis.clsi.url}/project/#{@project_id}/output/#{file.path}"
  67. type: file.type
  68. )
  69. .should.equal true
  70. describe "with an error", ->
  71. beforeEach ->
  72. @CompileManager.doCompile = sinon.stub().callsArgWith(1, new Error(@message = "error message"), null)
  73. @CompileController.compile @req, @res
  74. it "should return the JSON response with the error", ->
  75. @res.send
  76. .calledWith(JSON.stringify
  77. compile:
  78. status: "failure"
  79. error: @message
  80. outputFiles: []
  81. )
  82. .should.equal true