TemplatesControllerTests.coffee 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. '../Project/ProjectGetter':@ProjectGetter = {}
  31. '../Editor/EditorController': @EditorController = {}
  32. './TemplatesPublisher':@TemplatesPublisher
  33. "logger-sharelatex":
  34. log:->
  35. err:->
  36. "settings-sharelatex":
  37. path:
  38. dumpFolder:@dumpFolder
  39. siteUrl: "http://localhost:3000"
  40. apis:
  41. templates:
  42. url: @templateApiUrl="http://templates.sharelatex.env"
  43. web:
  44. url: @webApiUrl="http://web-api.sharelatex.env"
  45. "node-uuid":v4:=>@uuid
  46. "request": @request
  47. "fs":@fs
  48. @zipUrl = "%2Ftemplates%2F52fb86a81ae1e566597a25f6%2Fv%2F4%2Fzip&templateName=Moderncv%20Banking&compiler=pdflatex"
  49. @templateName = "project name here"
  50. @user_id = "1234"
  51. @req =
  52. session:
  53. user: _id:@user_id
  54. templateData:
  55. zipUrl: @zipUrl
  56. templateName: @templateName
  57. @redirect = {}
  58. describe 'reciving a request to create project from templates.sharelatex.com', ->
  59. it 'should take the zip url and write it to disk', (done)->
  60. redirect = =>
  61. @ProjectUploadManager.createProjectFromZipArchive.calledWith(@user_id, @templateName, "#{@dumpFolder}/#{@uuid}").should.equal true
  62. @request.calledWith("#{@templateApiUrl}#{@zipUrl}").should.equal true
  63. @fs.unlink.calledWith("#{@dumpFolder}/#{@uuid}").should.equal true
  64. done()
  65. res = redirect:redirect
  66. @controller.createProjectFromZipTemplate @req, res
  67. it "should go to the web api if the url does not contain templates", (done)->
  68. @req.session.templateData.zipUrl = @zipUrl = "/project/52fd24abf080d80a22000fbd/download/zip&templateName=Example_Project&compiler=xelatex"
  69. redirect = =>
  70. @request.calledWith("#{@webApiUrl}#{@zipUrl}").should.equal true
  71. done()
  72. res = redirect:redirect
  73. @controller.createProjectFromZipTemplate @req, res
  74. it "should go to the web api if the url has template futher down the string", (done)->
  75. @req.session.templateData.zipUrl = @zipUrl = "/project/52fd24abf080d80a22000fbd/download/zip&templateName=templates&compiler=xelatex"
  76. redirect = =>
  77. @request.calledWith("#{@webApiUrl}#{@zipUrl}").should.equal true
  78. done()
  79. res = redirect:redirect
  80. @controller.createProjectFromZipTemplate @req, res
  81. describe 'publishProject', ->
  82. beforeEach ->
  83. @user_id = "user-id-123"
  84. @project_id = "project-id-123"
  85. @res =
  86. send: sinon.stub()
  87. @req.params =
  88. Project_id: @project_id
  89. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
  90. @TemplatesPublisher.publish = sinon.stub().callsArgWith(2)
  91. @controller.publishProject @req, @res
  92. it "should look up the project owner", ->
  93. @ProjectGetter.getProject
  94. .calledWith(@project_id, { owner_ref: 1 })
  95. .should.equal true
  96. it "should publish the template", ->
  97. @TemplatesPublisher.publish
  98. .calledWith(@user_id, @project_id)
  99. .should.equal true
  100. it "should return a success status", ->
  101. @res.send.calledWith(204).should.equal true
  102. describe 'unpublishProject', ->
  103. beforeEach ->
  104. @user_id = "user-id-123"
  105. @project_id = "project-id-123"
  106. @res =
  107. send: sinon.stub()
  108. @req.params =
  109. Project_id: @project_id
  110. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
  111. @TemplatesPublisher.unpublish = sinon.stub().callsArgWith(2)
  112. @controller.unpublishProject @req, @res
  113. it "should look up the project owner", ->
  114. @ProjectGetter.getProject
  115. .calledWith(@project_id, { owner_ref: 1 })
  116. .should.equal true
  117. it "should publish the template", ->
  118. @TemplatesPublisher.unpublish
  119. .calledWith(@user_id, @project_id)
  120. .should.equal true
  121. it "should return a success status", ->
  122. @res.send.calledWith(204).should.equal true
  123. describe 'settings the compiler from the query string', ->
  124. it 'should use the said compiler', (done)->
  125. @req.session.templateData.compiler = "xelatex"
  126. redirect = =>
  127. @ProjectOptionsHandler.setCompiler.calledWith(project_id, "xelatex").should.equal true
  128. done()
  129. res = redirect:redirect
  130. @controller.createProjectFromZipTemplate @req, res
  131. it 'should not call the options handler if there is not set compiler', (done)->
  132. redirect = =>
  133. @ProjectOptionsHandler.setCompiler.called.should.equal false
  134. done()
  135. res = redirect:redirect
  136. @controller.createProjectFromZipTemplate @req, res
  137. describe "updateProjectDescription", ->
  138. beforeEach ->
  139. @EditorController.updateProjectDescription = sinon.stub().callsArg(2)
  140. @res =
  141. send: sinon.stub()
  142. @req.params =
  143. Project_id: @project_id = "project-id-123"
  144. @req.body =
  145. description: @description = "test description"
  146. @controller.updateProjectDescription @req, @res
  147. it "should update the project description", ->
  148. @EditorController.updateProjectDescription
  149. .calledWith(@project_id, @description)
  150. .should.equal true
  151. it "should return a success code", ->
  152. @res.send.calledWith(204).should.equal true
  153. describe 'getTemplateDetails', ->
  154. beforeEach ->
  155. @user_id = "user-id-123"
  156. @project_id = "project-id-123"
  157. @res =
  158. json: sinon.stub()
  159. @req.params =
  160. Project_id: @project_id
  161. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
  162. @TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, @details = {exists: true})
  163. @ProjectDetailsHandler.getProjectDescription.callsArgWith(1, null, @description = "test description")
  164. @controller.getTemplateDetails @req, @res
  165. it "should get the template details for the user_id and project_id", ->
  166. @TemplatesPublisher.getTemplateDetails
  167. .calledWith(@user_id, @project_id)
  168. .should.equal true
  169. it "should return the details and description", ->
  170. @res.json
  171. .calledWith({
  172. exists: @details.exists
  173. description: @description
  174. })
  175. .should.equal true