TemplatesControllerTests.coffee 5.1 KB

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