TemplatesControllerTests.coffee 5.9 KB

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