TemplatesControllerTests.coffee 6.8 KB

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