EditorHttpControllerTests.coffee 11 KB

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