EditorHttpControllerTests.coffee 9.8 KB

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