TemplatesControllerTests.coffee 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, '../../../../app/js/Features/Templates/TemplatesController'
  7. describe 'TemplatesController', ->
  8. project_id = "213432"
  9. beforeEach ->
  10. @request = sinon.stub()
  11. @request.returns pipe:->
  12. @fs = {
  13. unlink : sinon.stub()
  14. createWriteStream : sinon.stub().returns(on:(_, cb)->cb())
  15. }
  16. @ProjectUploadManager = {createProjectFromZipArchive : sinon.stub().callsArgWith(3, null, {_id:project_id})}
  17. @dumpFolder = "dump/path"
  18. @ProjectOptionsHandler = {setCompiler:sinon.stub().callsArgWith(2)}
  19. @uuid = "1234"
  20. @TemplatesPublisher =
  21. publish: sinon.stub()
  22. unpublish:sinon.stub()
  23. getTemplateDetails: sinon.stub()
  24. @controller = SandboxedModule.require modulePath, requires:
  25. '../Uploads/ProjectUploadManager':@ProjectUploadManager
  26. '../Project/ProjectOptionsHandler':@ProjectOptionsHandler
  27. './TemplatesPublisher':@TemplatesPublisher
  28. "logger-sharelatex":
  29. log:->
  30. err:->
  31. "settings-sharelatex":
  32. path:
  33. dumpFolder:@dumpFolder
  34. siteUrl: "http://localhost:3000"
  35. apis:
  36. templates_api:
  37. url: @templateApiUrl="http://templates.sharelatex.env"
  38. "node-uuid":v4:=>@uuid
  39. "request": @request
  40. "fs":@fs
  41. @zipUrl = "www.sharelatex.com/templates/cv/best.zip"
  42. @templateName = "project name here"
  43. @user_id = "1234"
  44. @req =
  45. session:
  46. user: _id:@user_id
  47. templateData: zipUrl: @zipUrl, templateName: @templateName
  48. @redirect = {}
  49. describe 'reciving a request to create project from templates.sharelatex.com', ->
  50. it 'should take the zip url and write it to disk', (done)->
  51. redirect = =>
  52. @ProjectUploadManager.createProjectFromZipArchive.calledWith(@user_id, @templateName, "#{@dumpFolder}/#{@uuid}").should.equal true
  53. @request.calledWith("#{@templateApiUrl}#{@zipUrl}").should.equal true
  54. @fs.unlink.calledWith("#{@dumpFolder}/#{@uuid}").should.equal true
  55. done()
  56. res = redirect:redirect
  57. @controller.createProjectFromZipTemplate @req, res
  58. describe 'publishProject', (done)->
  59. beforeEach ->
  60. @user_id = "sdijdlsakjlkajdklaj"
  61. @project_id = "23213kl2j13lk1"
  62. it 'should pass the user id and project id to the handler', (done)->
  63. @TemplatesPublisher.publish.callsArgWith(2)
  64. @controller.publishProject @user_id, @project_id, =>
  65. @TemplatesPublisher.publish.calledWith(@user_id, @project_id).should.equal true
  66. done()
  67. it 'should pass the error back', (done)->
  68. error = "error"
  69. @TemplatesPublisher.publish.callsArgWith(2, error)
  70. @controller.publishProject @user_id, @project_id, (passedError)=>
  71. passedError.should.equal error
  72. done()
  73. describe 'unpublish Project', (done)->
  74. beforeEach ->
  75. @user_id = "sdijdlsakjlkajdklaj"
  76. @project_id = "23213kl2j13lk1"
  77. it 'should pass the user id and project id to the handler', (done)->
  78. @TemplatesPublisher.unpublish.callsArgWith(2)
  79. @controller.unPublishProject @user_id, @project_id, =>
  80. @TemplatesPublisher.unpublish.calledWith(@user_id, @project_id).should.equal true
  81. done()
  82. it 'should pass the error back', (done)->
  83. error = "error"
  84. @TemplatesPublisher.unpublish.callsArgWith(2, error)
  85. @controller.unPublishProject @user_id, @project_id, (passedError)=>
  86. passedError.should.equal error
  87. done()
  88. describe 'settings the compiler from the query string', ->
  89. it 'should use the said compiler', (done)->
  90. @req.session.templateData.compiler = "xelatex"
  91. redirect = =>
  92. @ProjectOptionsHandler.setCompiler.calledWith(project_id, "xelatex").should.equal true
  93. done()
  94. res = redirect:redirect
  95. @controller.createProjectFromZipTemplate @req, res
  96. it 'should not call the options handler if there is not set compiler', (done)->
  97. redirect = =>
  98. @ProjectOptionsHandler.setCompiler.called.should.equal false
  99. done()
  100. res = redirect:redirect
  101. @controller.createProjectFromZipTemplate @req, res
  102. describe 'getTemplateDetails', ->
  103. it "should return an error the templatePublisher", (done)->
  104. error = "error"
  105. @TemplatesPublisher.getTemplateDetails.callsArgWith(2, error)
  106. @controller.getTemplateDetails @user_id, @project_id, (passedError)=>
  107. passedError.should.equal error
  108. done()
  109. it "should return the details", (done)->
  110. details = {exists:true}
  111. @TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, details)
  112. @controller.getTemplateDetails @user_id, @project_id, (err, passedDetails)=>
  113. details.should.equal passedDetails
  114. done()