EditorHttpControllerTests.coffee 10 KB

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