EditorHttpControllerTests.coffee 11 KB

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