TemplatesControllerTests.coffee 4.5 KB

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