EditorHttpControllerTests.coffee 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. '../Project/ProjectGetter' : @ProjectGetter = {}
  11. '../User/UserGetter' : @UserGetter = {}
  12. "../Security/AuthorizationManager": @AuthorizationManager = {}
  13. '../Project/ProjectEditorHandler': @ProjectEditorHandler = {}
  14. "./EditorRealTimeController": @EditorRealTimeController = {}
  15. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  16. "./EditorController": @EditorController = {}
  17. '../../infrastructure/Metrics': @Metrics = {inc: sinon.stub()}
  18. @project_id = "mock-project-id"
  19. @doc_id = "mock-doc-id"
  20. @user_id = "mock-user-id"
  21. @parent_folder_id = "mock-folder-id"
  22. @req = {}
  23. @res =
  24. send: sinon.stub()
  25. sendStatus: sinon.stub()
  26. json: sinon.stub()
  27. @callback = sinon.stub()
  28. describe "joinProject", ->
  29. beforeEach ->
  30. @req.params =
  31. Project_id: @project_id
  32. @req.query =
  33. user_id: @user_id
  34. @projectView = {
  35. _id: @project_id
  36. }
  37. @EditorHttpController._buildJoinProjectView = sinon.stub().callsArgWith(2, null, @projectView, "owner")
  38. @ProjectDeleter.unmarkAsDeletedByExternalSource = sinon.stub()
  39. describe "successfully", ->
  40. beforeEach ->
  41. @EditorHttpController.joinProject @req, @res
  42. it "should get the project view", ->
  43. @EditorHttpController._buildJoinProjectView
  44. .calledWith(@project_id, @user_id)
  45. .should.equal true
  46. it "should return the project and privilege level", ->
  47. @res.json
  48. .calledWith({
  49. project: @projectView
  50. privilegeLevel: "owner"
  51. })
  52. .should.equal true
  53. it "should not try to unmark the project as deleted", ->
  54. @ProjectDeleter.unmarkAsDeletedByExternalSource
  55. .called
  56. .should.equal false
  57. it "should send an inc metric", ->
  58. @Metrics.inc
  59. .calledWith("editor.join-project")
  60. .should.equal true
  61. describe "when the project is marked as deleted", ->
  62. beforeEach ->
  63. @projectView.deletedByExternalDataSource = true
  64. @EditorHttpController.joinProject @req, @res
  65. it "should unmark the project as deleted", ->
  66. @ProjectDeleter.unmarkAsDeletedByExternalSource
  67. .calledWith(@project_id)
  68. .should.equal true
  69. describe "_buildJoinProjectView", ->
  70. beforeEach ->
  71. @project =
  72. _id: @project_id
  73. owner_ref:{_id:"something"}
  74. @user =
  75. _id: @user_id = "user-id"
  76. projects: {}
  77. @projectModelView =
  78. _id: @project_id
  79. owner:{_id:"something"}
  80. view: true
  81. @ProjectEditorHandler.buildProjectModelView = sinon.stub().returns(@projectModelView)
  82. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  83. @ProjectGetter.populateProjectWithUsers = sinon.stub().callsArgWith(1, null, @project)
  84. @UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
  85. describe "when authorized", ->
  86. beforeEach ->
  87. @AuthorizationManager.getPrivilegeLevelForProject =
  88. sinon.stub().callsArgWith(2, null, true, "owner")
  89. @EditorHttpController._buildJoinProjectView(@project_id, @user_id, @callback)
  90. it "should find the project without doc lines", ->
  91. @ProjectGetter.getProjectWithoutDocLines
  92. .calledWith(@project_id)
  93. .should.equal true
  94. it "should populate the user references in the project", ->
  95. @ProjectGetter.populateProjectWithUsers
  96. .calledWith(@project)
  97. .should.equal true
  98. it "should look up the user", ->
  99. @UserGetter.getUser
  100. .calledWith(@user_id, { isAdmin: true })
  101. .should.equal true
  102. it "should check the privilege level", ->
  103. @AuthorizationManager.getPrivilegeLevelForProject
  104. .calledWith(@project, @user)
  105. .should.equal true
  106. it "should return the project model view, privilege level and protocol version", ->
  107. @callback.calledWith(null, @projectModelView, "owner").should.equal true
  108. describe "when not authorized", ->
  109. beforeEach ->
  110. @AuthorizationManager.getPrivilegeLevelForProject =
  111. sinon.stub().callsArgWith(2, null, false, null)
  112. @EditorHttpController._buildJoinProjectView(@project_id, @user_id, @callback)
  113. it "should return false in the callback", ->
  114. @callback.calledWith(null, null, false).should.equal true
  115. describe "restoreDoc", ->
  116. beforeEach ->
  117. @req.params =
  118. Project_id: @project_id
  119. doc_id: @doc_id
  120. @req.body =
  121. name: @name = "doc-name"
  122. @ProjectEntityHandler.restoreDoc = sinon.stub().callsArgWith(3, null,
  123. @doc = { "mock": "doc", _id: @new_doc_id = "new-doc-id" }
  124. @folder_id = "mock-folder-id"
  125. )
  126. @EditorRealTimeController.emitToRoom = sinon.stub()
  127. @EditorHttpController.restoreDoc @req, @res
  128. it "should restore the doc", ->
  129. @ProjectEntityHandler.restoreDoc
  130. .calledWith(@project_id, @doc_id, @name)
  131. .should.equal true
  132. it "should the real-time clients about the new doc", ->
  133. @EditorRealTimeController.emitToRoom
  134. .calledWith(@project_id, 'reciveNewDoc', @folder_id, @doc)
  135. .should.equal true
  136. it "should return the new doc id", ->
  137. @res.json
  138. .calledWith(doc_id: @new_doc_id)
  139. .should.equal true
  140. describe "addDoc", ->
  141. beforeEach ->
  142. @doc = { "mock": "doc" }
  143. @req.params =
  144. Project_id: @project_id
  145. @req.body =
  146. name: @name = "doc-name"
  147. parent_folder_id: @parent_folder_id
  148. @EditorController.addDoc = sinon.stub().callsArgWith(5, null, @doc)
  149. describe "successfully", ->
  150. beforeEach ->
  151. @EditorHttpController.addDoc @req, @res
  152. it "should call EditorController.addDoc", ->
  153. @EditorController.addDoc
  154. .calledWith(@project_id, @parent_folder_id, @name, [], "editor")
  155. .should.equal true
  156. it "should send the doc back as JSON", ->
  157. @res.json
  158. .calledWith(@doc)
  159. .should.equal true
  160. describe "unsuccesfully", ->
  161. beforeEach ->
  162. @req.body.name = ""
  163. @EditorHttpController.addDoc @req, @res
  164. it "should send back a bad request status code", ->
  165. @res.sendStatus.calledWith(400).should.equal true
  166. describe "addFolder", ->
  167. beforeEach ->
  168. @folder = { "mock": "folder" }
  169. @req.params =
  170. Project_id: @project_id
  171. @req.body =
  172. name: @name = "folder-name"
  173. parent_folder_id: @parent_folder_id
  174. @EditorController.addFolder = sinon.stub().callsArgWith(4, null, @folder)
  175. describe "successfully", ->
  176. beforeEach ->
  177. @EditorHttpController.addFolder @req, @res
  178. it "should call EditorController.addFolder", ->
  179. @EditorController.addFolder
  180. .calledWith(@project_id, @parent_folder_id, @name, "editor")
  181. .should.equal true
  182. it "should send the folder back as JSON", ->
  183. @res.json
  184. .calledWith(@folder)
  185. .should.equal true
  186. describe "unsuccesfully", ->
  187. beforeEach ->
  188. @req.body.name = ""
  189. @EditorHttpController.addFolder @req, @res
  190. it "should send back a bad request status code", ->
  191. @res.sendStatus.calledWith(400).should.equal true
  192. describe "renameEntity", ->
  193. beforeEach ->
  194. @req.params =
  195. Project_id: @project_id
  196. entity_id: @entity_id = "entity-id-123"
  197. entity_type: @entity_type = "entity-type"
  198. @req.body =
  199. name: @name = "new-name"
  200. @EditorController.renameEntity = sinon.stub().callsArg(4)
  201. @EditorHttpController.renameEntity @req, @res
  202. it "should call EditorController.renameEntity", ->
  203. @EditorController.renameEntity
  204. .calledWith(@project_id, @entity_id, @entity_type, @name)
  205. .should.equal true
  206. it "should send back a success response", ->
  207. @res.sendStatus.calledWith(204).should.equal true
  208. describe "renameEntity with long name", ->
  209. beforeEach ->
  210. @req.params =
  211. Project_id: @project_id
  212. entity_id: @entity_id = "entity-id-123"
  213. entity_type: @entity_type = "entity-type"
  214. @req.body =
  215. name: @name = "EDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOTEDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOT"
  216. @EditorController.renameEntity = sinon.stub().callsArg(4)
  217. @EditorHttpController.renameEntity @req, @res
  218. it "should send back a bad request status code", ->
  219. @res.sendStatus.calledWith(400).should.equal true
  220. describe "rename entity with 0 length name", ->
  221. beforeEach ->
  222. @req.params =
  223. Project_id: @project_id
  224. entity_id: @entity_id = "entity-id-123"
  225. entity_type: @entity_type = "entity-type"
  226. @req.body =
  227. name: @name = ""
  228. @EditorController.renameEntity = sinon.stub().callsArg(4)
  229. @EditorHttpController.renameEntity @req, @res
  230. it "should send back a bad request status code", ->
  231. @res.sendStatus.calledWith(400).should.equal true
  232. describe "moveEntity", ->
  233. beforeEach ->
  234. @req.params =
  235. Project_id: @project_id
  236. entity_id: @entity_id = "entity-id-123"
  237. entity_type: @entity_type = "entity-type"
  238. @req.body =
  239. folder_id: @folder_id = "folder-id-123"
  240. @EditorController.moveEntity = sinon.stub().callsArg(4)
  241. @EditorHttpController.moveEntity @req, @res
  242. it "should call EditorController.moveEntity", ->
  243. @EditorController.moveEntity
  244. .calledWith(@project_id, @entity_id, @folder_id, @entity_type)
  245. .should.equal true
  246. it "should send back a success response", ->
  247. @res.sendStatus.calledWith(204).should.equal true
  248. describe "deleteEntity", ->
  249. beforeEach ->
  250. @req.params =
  251. Project_id: @project_id
  252. entity_id: @entity_id = "entity-id-123"
  253. entity_type: @entity_type = "entity-type"
  254. @EditorController.deleteEntity = sinon.stub().callsArg(4)
  255. @EditorHttpController.deleteEntity @req, @res
  256. it "should call EditorController.deleteEntity", ->
  257. @EditorController.deleteEntity
  258. .calledWith(@project_id, @entity_id, @entity_type, "editor")
  259. .should.equal true
  260. it "should send back a success response", ->
  261. @res.sendStatus.calledWith(204).should.equal true