EditorHttpControllerTests.coffee 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. "../Authorization/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 "with an anonymous user", ->
  71. beforeEach ->
  72. @req.query =
  73. user_id: "anonymous-user"
  74. @EditorHttpController.joinProject @req, @res
  75. it "should pass the user id as null", ->
  76. @EditorHttpController._buildJoinProjectView
  77. .calledWith(@project_id, null)
  78. .should.equal true
  79. describe "_buildJoinProjectView", ->
  80. beforeEach ->
  81. @project =
  82. _id: @project_id
  83. owner_ref:{_id:"something"}
  84. @user =
  85. _id: @user_id = "user-id"
  86. projects: {}
  87. @members = ["members", "mock"]
  88. @projectModelView =
  89. _id: @project_id
  90. owner:{_id:"something"}
  91. view: true
  92. @ProjectEditorHandler.buildProjectModelView = sinon.stub().returns(@projectModelView)
  93. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  94. @CollaboratorsHandler.getMembersWithPrivilegeLevels = sinon.stub().callsArgWith(1, null, @members)
  95. @UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
  96. describe "when authorized", ->
  97. beforeEach ->
  98. @AuthorizationManager.getPrivilegeLevelForProject =
  99. sinon.stub().callsArgWith(2, null, "owner")
  100. @EditorHttpController._buildJoinProjectView(@project_id, @user_id, @callback)
  101. it "should find the project without doc lines", ->
  102. @ProjectGetter.getProjectWithoutDocLines
  103. .calledWith(@project_id)
  104. .should.equal true
  105. it "should get the list of users in the project", ->
  106. @CollaboratorsHandler.getMembersWithPrivilegeLevels
  107. .calledWith(@project)
  108. .should.equal true
  109. it "should look up the user", ->
  110. @UserGetter.getUser
  111. .calledWith(@user_id, { isAdmin: true })
  112. .should.equal true
  113. it "should check the privilege level", ->
  114. @AuthorizationManager.getPrivilegeLevelForProject
  115. .calledWith(@user_id, @project_id)
  116. .should.equal true
  117. it "should return the project model view, privilege level and protocol version", ->
  118. @callback.calledWith(null, @projectModelView, "owner").should.equal true
  119. describe "when not authorized", ->
  120. beforeEach ->
  121. @AuthorizationManager.getPrivilegeLevelForProject =
  122. sinon.stub().callsArgWith(2, null, null)
  123. @EditorHttpController._buildJoinProjectView(@project_id, @user_id, @callback)
  124. it "should return false in the callback", ->
  125. @callback.calledWith(null, null, false).should.equal true
  126. describe "restoreDoc", ->
  127. beforeEach ->
  128. @req.params =
  129. Project_id: @project_id
  130. doc_id: @doc_id
  131. @req.body =
  132. name: @name = "doc-name"
  133. @ProjectEntityHandler.restoreDoc = sinon.stub().callsArgWith(3, null,
  134. @doc = { "mock": "doc", _id: @new_doc_id = "new-doc-id" }
  135. @folder_id = "mock-folder-id"
  136. )
  137. @EditorRealTimeController.emitToRoom = sinon.stub()
  138. @EditorHttpController.restoreDoc @req, @res
  139. it "should restore the doc", ->
  140. @ProjectEntityHandler.restoreDoc
  141. .calledWith(@project_id, @doc_id, @name)
  142. .should.equal true
  143. it "should the real-time clients about the new doc", ->
  144. @EditorRealTimeController.emitToRoom
  145. .calledWith(@project_id, 'reciveNewDoc', @folder_id, @doc)
  146. .should.equal true
  147. it "should return the new doc id", ->
  148. @res.json
  149. .calledWith(doc_id: @new_doc_id)
  150. .should.equal true
  151. describe "addDoc", ->
  152. beforeEach ->
  153. @doc = { "mock": "doc" }
  154. @req.params =
  155. Project_id: @project_id
  156. @req.body =
  157. name: @name = "doc-name"
  158. parent_folder_id: @parent_folder_id
  159. @EditorController.addDoc = sinon.stub().callsArgWith(5, null, @doc)
  160. describe "successfully", ->
  161. beforeEach ->
  162. @EditorHttpController.addDoc @req, @res
  163. it "should call EditorController.addDoc", ->
  164. @EditorController.addDoc
  165. .calledWith(@project_id, @parent_folder_id, @name, [], "editor")
  166. .should.equal true
  167. it "should send the doc back as JSON", ->
  168. @res.json
  169. .calledWith(@doc)
  170. .should.equal true
  171. describe "unsuccesfully", ->
  172. beforeEach ->
  173. @req.body.name = ""
  174. @EditorHttpController.addDoc @req, @res
  175. it "should send back a bad request status code", ->
  176. @res.sendStatus.calledWith(400).should.equal true
  177. describe "addFolder", ->
  178. beforeEach ->
  179. @folder = { "mock": "folder" }
  180. @req.params =
  181. Project_id: @project_id
  182. @req.body =
  183. name: @name = "folder-name"
  184. parent_folder_id: @parent_folder_id
  185. @EditorController.addFolder = sinon.stub().callsArgWith(4, null, @folder)
  186. describe "successfully", ->
  187. beforeEach ->
  188. @EditorHttpController.addFolder @req, @res
  189. it "should call EditorController.addFolder", ->
  190. @EditorController.addFolder
  191. .calledWith(@project_id, @parent_folder_id, @name, "editor")
  192. .should.equal true
  193. it "should send the folder back as JSON", ->
  194. @res.json
  195. .calledWith(@folder)
  196. .should.equal true
  197. describe "unsuccesfully", ->
  198. beforeEach ->
  199. @req.body.name = ""
  200. @EditorHttpController.addFolder @req, @res
  201. it "should send back a bad request status code", ->
  202. @res.sendStatus.calledWith(400).should.equal true
  203. describe "renameEntity", ->
  204. beforeEach ->
  205. @req.params =
  206. Project_id: @project_id
  207. entity_id: @entity_id = "entity-id-123"
  208. entity_type: @entity_type = "entity-type"
  209. @req.body =
  210. name: @name = "new-name"
  211. @EditorController.renameEntity = sinon.stub().callsArg(4)
  212. @EditorHttpController.renameEntity @req, @res
  213. it "should call EditorController.renameEntity", ->
  214. @EditorController.renameEntity
  215. .calledWith(@project_id, @entity_id, @entity_type, @name)
  216. .should.equal true
  217. it "should send back a success response", ->
  218. @res.sendStatus.calledWith(204).should.equal true
  219. describe "renameEntity with long 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 = "EDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOTEDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOT"
  227. @EditorController.renameEntity = sinon.stub().callsArg(4)
  228. @EditorHttpController.renameEntity @req, @res
  229. it "should send back a bad request status code", ->
  230. @res.sendStatus.calledWith(400).should.equal true
  231. describe "rename entity with 0 length name", ->
  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. name: @name = ""
  239. @EditorController.renameEntity = sinon.stub().callsArg(4)
  240. @EditorHttpController.renameEntity @req, @res
  241. it "should send back a bad request status code", ->
  242. @res.sendStatus.calledWith(400).should.equal true
  243. describe "moveEntity", ->
  244. beforeEach ->
  245. @req.params =
  246. Project_id: @project_id
  247. entity_id: @entity_id = "entity-id-123"
  248. entity_type: @entity_type = "entity-type"
  249. @req.body =
  250. folder_id: @folder_id = "folder-id-123"
  251. @EditorController.moveEntity = sinon.stub().callsArg(4)
  252. @EditorHttpController.moveEntity @req, @res
  253. it "should call EditorController.moveEntity", ->
  254. @EditorController.moveEntity
  255. .calledWith(@project_id, @entity_id, @folder_id, @entity_type)
  256. .should.equal true
  257. it "should send back a success response", ->
  258. @res.sendStatus.calledWith(204).should.equal true
  259. describe "deleteEntity", ->
  260. beforeEach ->
  261. @req.params =
  262. Project_id: @project_id
  263. entity_id: @entity_id = "entity-id-123"
  264. entity_type: @entity_type = "entity-type"
  265. @EditorController.deleteEntity = sinon.stub().callsArg(4)
  266. @EditorHttpController.deleteEntity @req, @res
  267. it "should call EditorController.deleteEntity", ->
  268. @EditorController.deleteEntity
  269. .calledWith(@project_id, @entity_id, @entity_type, "editor")
  270. .should.equal true
  271. it "should send back a success response", ->
  272. @res.sendStatus.calledWith(204).should.equal true