TemplatesControllerTests.coffee 5.1 KB

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