EditorHttpControllerTests.coffee 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/Features/Editor/EditorHttpController'
  5. describe "EditorHttpController", ->
  6. beforeEach ->
  7. @EditorHttpController = SandboxedModule.require modulePath, requires:
  8. '../Project/ProjectEntityHandler' : @ProjectEntityHandler = {}
  9. '../Project/ProjectDeleter' : @ProjectDeleter = {}
  10. "./EditorRealTimeController": @EditorRealTimeController = {}
  11. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  12. "./EditorController": @EditorController = {}
  13. '../../infrastructure/Metrics': @Metrics = {inc: sinon.stub()}
  14. @project_id = "mock-project-id"
  15. @doc_id = "mock-doc-id"
  16. @user_id = "mock-user-id"
  17. @parent_folder_id = "mock-folder-id"
  18. @req = {}
  19. @res =
  20. send: sinon.stub()
  21. json: sinon.stub()
  22. describe "joinProject", ->
  23. beforeEach ->
  24. @req.params =
  25. Project_id: @project_id
  26. @req.query =
  27. user_id: @user_id
  28. @projectView = {
  29. _id: @project_id
  30. }
  31. @EditorController.buildJoinProjectView = sinon.stub().callsArgWith(2, null, @projectView, "owner")
  32. @ProjectDeleter.unmarkAsDeletedByExternalSource = sinon.stub()
  33. describe "successfully", ->
  34. beforeEach ->
  35. @EditorHttpController.joinProject @req, @res
  36. it "should get the project view", ->
  37. @EditorController.buildJoinProjectView
  38. .calledWith(@project_id, @user_id)
  39. .should.equal true
  40. it "should return the project and privilege level", ->
  41. @res.json
  42. .calledWith({
  43. project: @projectView
  44. privilegeLevel: "owner"
  45. })
  46. .should.equal true
  47. it "should not try to unmark the project as deleted", ->
  48. @ProjectDeleter.unmarkAsDeletedByExternalSource
  49. .called
  50. .should.equal false
  51. it "should send an inc metric", ->
  52. @Metrics.inc
  53. .calledWith("editor.join-project")
  54. .should.equal true
  55. describe "when the project is marked as deleted", ->
  56. beforeEach ->
  57. @projectView.deletedByExternalDataSource = true
  58. @EditorHttpController.joinProject @req, @res
  59. it "should unmark the project as deleted", ->
  60. @ProjectDeleter.unmarkAsDeletedByExternalSource
  61. .calledWith(@project_id)
  62. .should.equal true
  63. describe "restoreDoc", ->
  64. beforeEach ->
  65. @req.params =
  66. Project_id: @project_id
  67. doc_id: @doc_id
  68. @req.body =
  69. name: @name = "doc-name"
  70. @ProjectEntityHandler.restoreDoc = sinon.stub().callsArgWith(3, null,
  71. @doc = { "mock": "doc", _id: @new_doc_id = "new-doc-id" }
  72. @folder_id = "mock-folder-id"
  73. )
  74. @EditorRealTimeController.emitToRoom = sinon.stub()
  75. @EditorHttpController.restoreDoc @req, @res
  76. it "should restore the doc", ->
  77. @ProjectEntityHandler.restoreDoc
  78. .calledWith(@project_id, @doc_id, @name)
  79. .should.equal true
  80. it "should the real-time clients about the new doc", ->
  81. @EditorRealTimeController.emitToRoom
  82. .calledWith(@project_id, 'reciveNewDoc', @folder_id, @doc)
  83. .should.equal true
  84. it "should return the new doc id", ->
  85. @res.json
  86. .calledWith(doc_id: @new_doc_id)
  87. .should.equal true
  88. describe "addDoc", ->
  89. beforeEach ->
  90. @doc = { "mock": "doc" }
  91. @req.params =
  92. Project_id: @project_id
  93. @req.body =
  94. name: @name = "doc-name"
  95. parent_folder_id: @parent_folder_id
  96. @EditorController.addDoc = sinon.stub().callsArgWith(5, null, @doc)
  97. @EditorHttpController.addDoc @req, @res
  98. it "should call EditorController.addDoc", ->
  99. @EditorController.addDoc
  100. .calledWith(@project_id, @parent_folder_id, @name, [], "editor")
  101. .should.equal true
  102. it "should send the doc back as JSON", ->
  103. @res.json
  104. .calledWith(@doc)
  105. .should.equal true
  106. describe "addFolder", ->
  107. beforeEach ->
  108. @folder = { "mock": "folder" }
  109. @req.params =
  110. Project_id: @project_id
  111. @req.body =
  112. name: @name = "folder-name"
  113. parent_folder_id: @parent_folder_id
  114. @EditorController.addFolder = sinon.stub().callsArgWith(4, null, @folder)
  115. @EditorHttpController.addFolder @req, @res
  116. it "should call EditorController.addFolder", ->
  117. @EditorController.addFolder
  118. .calledWith(@project_id, @parent_folder_id, @name, "editor")
  119. .should.equal true
  120. it "should send the folder back as JSON", ->
  121. @res.json
  122. .calledWith(@folder)
  123. .should.equal true
  124. describe "renameEntity", ->
  125. beforeEach ->
  126. @req.params =
  127. Project_id: @project_id
  128. entity_id: @entity_id = "entity-id-123"
  129. entity_type: @entity_type = "entity-type"
  130. @req.body =
  131. name: @name = "new-name"
  132. @EditorController.renameEntity = sinon.stub().callsArg(4)
  133. @EditorHttpController.renameEntity @req, @res
  134. it "should call EditorController.renameEntity", ->
  135. @EditorController.renameEntity
  136. .calledWith(@project_id, @entity_id, @entity_type, @name)
  137. .should.equal true
  138. it "should send back a success response", ->
  139. @res.send.calledWith(204).should.equal true
  140. describe "renameEntity with long name", ->
  141. beforeEach ->
  142. @req.params =
  143. Project_id: @project_id
  144. entity_id: @entity_id = "entity-id-123"
  145. entity_type: @entity_type = "entity-type"
  146. @req.body =
  147. name: @name = "EDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOTEDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOT"
  148. @EditorController.renameEntity = sinon.stub().callsArg(4)
  149. @EditorHttpController.renameEntity @req, @res
  150. it "should send back a bad request status code", ->
  151. @res.send.calledWith(400).should.equal true
  152. describe "moveEntity", ->
  153. beforeEach ->
  154. @req.params =
  155. Project_id: @project_id
  156. entity_id: @entity_id = "entity-id-123"
  157. entity_type: @entity_type = "entity-type"
  158. @req.body =
  159. folder_id: @folder_id = "folder-id-123"
  160. @EditorController.moveEntity = sinon.stub().callsArg(4)
  161. @EditorHttpController.moveEntity @req, @res
  162. it "should call EditorController.moveEntity", ->
  163. @EditorController.moveEntity
  164. .calledWith(@project_id, @entity_id, @folder_id, @entity_type)
  165. .should.equal true
  166. it "should send back a success response", ->
  167. @res.send.calledWith(204).should.equal true
  168. describe "deleteEntity", ->
  169. beforeEach ->
  170. @req.params =
  171. Project_id: @project_id
  172. entity_id: @entity_id = "entity-id-123"
  173. entity_type: @entity_type = "entity-type"
  174. @EditorController.deleteEntity = sinon.stub().callsArg(4)
  175. @EditorHttpController.deleteEntity @req, @res
  176. it "should call EditorController.deleteEntity", ->
  177. @EditorController.deleteEntity
  178. .calledWith(@project_id, @entity_id, @entity_type, "editor")
  179. .should.equal true
  180. it "should send back a success response", ->
  181. @res.send.calledWith(204).should.equal true