TemplatesControllerTests.coffee 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. web:
  39. url: @webApiUrl="http://web-api.sharelatex.env"
  40. "node-uuid":v4:=>@uuid
  41. "request": @request
  42. "fs":@fs
  43. @zipUrl = "%2Ftemplates%2F52fb86a81ae1e566597a25f6%2Fv%2F4%2Fzip&templateName=Moderncv%20Banking&compiler=pdflatex"
  44. @templateName = "project name here"
  45. @user_id = "1234"
  46. @req =
  47. session:
  48. user: _id:@user_id
  49. templateData:
  50. zipUrl: @zipUrl
  51. templateName: @templateName
  52. @redirect = {}
  53. describe 'reciving a request to create project from templates.sharelatex.com', ->
  54. it 'should take the zip url and write it to disk', (done)->
  55. redirect = =>
  56. @ProjectUploadManager.createProjectFromZipArchive.calledWith(@user_id, @templateName, "#{@dumpFolder}/#{@uuid}").should.equal true
  57. @request.calledWith("#{@templateApiUrl}#{@zipUrl}").should.equal true
  58. @fs.unlink.calledWith("#{@dumpFolder}/#{@uuid}").should.equal true
  59. done()
  60. res = redirect:redirect
  61. @controller.createProjectFromZipTemplate @req, res
  62. it "should go to the web api if the url does not contain templates", (done)->
  63. @req.session.templateData.zipUrl = @zipUrl = "/project/52fd24abf080d80a22000fbd/download/zip&templateName=Example_Project&compiler=xelatex"
  64. redirect = =>
  65. @request.calledWith("#{@webApiUrl}#{@zipUrl}").should.equal true
  66. done()
  67. res = redirect:redirect
  68. @controller.createProjectFromZipTemplate @req, res
  69. it "should go to the web api if the url has template futher down the string", (done)->
  70. @req.session.templateData.zipUrl = @zipUrl = "/project/52fd24abf080d80a22000fbd/download/zip&templateName=templates&compiler=xelatex"
  71. redirect = =>
  72. @request.calledWith("#{@webApiUrl}#{@zipUrl}").should.equal true
  73. done()
  74. res = redirect:redirect
  75. @controller.createProjectFromZipTemplate @req, res
  76. describe 'publishProject', (done)->
  77. beforeEach ->
  78. @user_id = "sdijdlsakjlkajdklaj"
  79. @project_id = "23213kl2j13lk1"
  80. it 'should pass the user id and project id to the handler', (done)->
  81. @TemplatesPublisher.publish.callsArgWith(2)
  82. @controller.publishProject @user_id, @project_id, =>
  83. @TemplatesPublisher.publish.calledWith(@user_id, @project_id).should.equal true
  84. done()
  85. it 'should pass the error back', (done)->
  86. error = "error"
  87. @TemplatesPublisher.publish.callsArgWith(2, error)
  88. @controller.publishProject @user_id, @project_id, (passedError)=>
  89. passedError.should.equal error
  90. done()
  91. describe 'unpublish Project', (done)->
  92. beforeEach ->
  93. @user_id = "sdijdlsakjlkajdklaj"
  94. @project_id = "23213kl2j13lk1"
  95. it 'should pass the user id and project id to the handler', (done)->
  96. @TemplatesPublisher.unpublish.callsArgWith(2)
  97. @controller.unPublishProject @user_id, @project_id, =>
  98. @TemplatesPublisher.unpublish.calledWith(@user_id, @project_id).should.equal true
  99. done()
  100. it 'should pass the error back', (done)->
  101. error = "error"
  102. @TemplatesPublisher.unpublish.callsArgWith(2, error)
  103. @controller.unPublishProject @user_id, @project_id, (passedError)=>
  104. passedError.should.equal error
  105. done()
  106. describe 'settings the compiler from the query string', ->
  107. it 'should use the said compiler', (done)->
  108. @req.session.templateData.compiler = "xelatex"
  109. redirect = =>
  110. @ProjectOptionsHandler.setCompiler.calledWith(project_id, "xelatex").should.equal true
  111. done()
  112. res = redirect:redirect
  113. @controller.createProjectFromZipTemplate @req, res
  114. it 'should not call the options handler if there is not set compiler', (done)->
  115. redirect = =>
  116. @ProjectOptionsHandler.setCompiler.called.should.equal false
  117. done()
  118. res = redirect:redirect
  119. @controller.createProjectFromZipTemplate @req, res
  120. describe 'getTemplateDetails', ->
  121. it "should return an error the templatePublisher", (done)->
  122. error = "error"
  123. @TemplatesPublisher.getTemplateDetails.callsArgWith(2, error)
  124. @controller.getTemplateDetails @user_id, @project_id, (passedError)=>
  125. passedError.should.equal error
  126. done()
  127. it "should return the details", (done)->
  128. details = {exists:true}
  129. @TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, details)
  130. @controller.getTemplateDetails @user_id, @project_id, (err, passedDetails)=>
  131. details.should.equal passedDetails
  132. done()