EditorHttpControllerTests.coffee 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 check the privilege level", ->
  124. @AuthorizationManager.getPrivilegeLevelForProject
  125. .calledWith(@user_id, @project_id, @token)
  126. .should.equal true
  127. it 'should include the invites', ->
  128. @CollaboratorsInviteHandler.getAllInvites
  129. .calledWith(@project._id)
  130. .should.equal true
  131. it "should return the project model view, privilege level and protocol version", ->
  132. @callback.calledWith(null, @projectModelView, "owner").should.equal true
  133. describe "when not authorized", ->
  134. beforeEach ->
  135. @AuthorizationManager.getPrivilegeLevelForProject =
  136. sinon.stub().callsArgWith(3, null, null)
  137. @EditorHttpController._buildJoinProjectView(@req, @project_id, @user_id, @callback)
  138. it "should return false in the callback", ->
  139. @callback.calledWith(null, null, false).should.equal true
  140. describe "addDoc", ->
  141. beforeEach ->
  142. @doc = { "mock": "doc" }
  143. @req.params =
  144. Project_id: @project_id
  145. @req.body =
  146. name: @name = "doc-name"
  147. parent_folder_id: @parent_folder_id
  148. @EditorController.addDoc = sinon.stub().callsArgWith(6, null, @doc)
  149. describe "successfully", ->
  150. beforeEach ->
  151. @EditorHttpController.addDoc @req, @res
  152. it "should call EditorController.addDoc", ->
  153. @EditorController.addDoc
  154. .calledWith(@project_id, @parent_folder_id, @name, [], "editor", @userId)
  155. .should.equal true
  156. it "should send the doc back as JSON", ->
  157. @res.json
  158. .calledWith(@doc)
  159. .should.equal true
  160. describe "unsuccesfully", ->
  161. beforeEach ->
  162. @req.body.name = ""
  163. @EditorHttpController.addDoc @req, @res
  164. it "should send back a bad request status code", ->
  165. @res.sendStatus.calledWith(400).should.equal true
  166. describe "addFolder", ->
  167. beforeEach ->
  168. @folder = { "mock": "folder" }
  169. @req.params =
  170. Project_id: @project_id
  171. @req.body =
  172. name: @name = "folder-name"
  173. parent_folder_id: @parent_folder_id
  174. @EditorController.addFolder = sinon.stub().callsArgWith(4, null, @folder)
  175. describe "successfully", ->
  176. beforeEach ->
  177. @EditorHttpController.addFolder @req, @res
  178. it "should call EditorController.addFolder", ->
  179. @EditorController.addFolder
  180. .calledWith(@project_id, @parent_folder_id, @name, "editor")
  181. .should.equal true
  182. it "should send the folder back as JSON", ->
  183. @res.json
  184. .calledWith(@folder)
  185. .should.equal true
  186. describe "unsuccesfully", ->
  187. beforeEach ->
  188. @req.body.name = ""
  189. @EditorHttpController.addFolder @req, @res
  190. it "should send back a bad request status code", ->
  191. @res.sendStatus.calledWith(400).should.equal true
  192. describe "renameEntity", ->
  193. beforeEach ->
  194. @req.params =
  195. Project_id: @project_id
  196. entity_id: @entity_id = "entity-id-123"
  197. entity_type: @entity_type = "entity-type"
  198. @req.body =
  199. name: @name = "new-name"
  200. @EditorController.renameEntity = sinon.stub().callsArg(5)
  201. @EditorHttpController.renameEntity @req, @res
  202. it "should call EditorController.renameEntity", ->
  203. @EditorController.renameEntity
  204. .calledWith(@project_id, @entity_id, @entity_type, @name, @userId)
  205. .should.equal true
  206. it "should send back a success response", ->
  207. @res.sendStatus.calledWith(204).should.equal true
  208. describe "renameEntity with long name", ->
  209. beforeEach ->
  210. @req.params =
  211. Project_id: @project_id
  212. entity_id: @entity_id = "entity-id-123"
  213. entity_type: @entity_type = "entity-type"
  214. @req.body =
  215. name: @name = "EDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOTEDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOT"
  216. @EditorController.renameEntity = sinon.stub().callsArg(4)
  217. @EditorHttpController.renameEntity @req, @res
  218. it "should send back a bad request status code", ->
  219. @res.sendStatus.calledWith(400).should.equal true
  220. describe "rename entity with 0 length name", ->
  221. beforeEach ->
  222. @req.params =
  223. Project_id: @project_id
  224. entity_id: @entity_id = "entity-id-123"
  225. entity_type: @entity_type = "entity-type"
  226. @req.body =
  227. name: @name = ""
  228. @EditorController.renameEntity = sinon.stub().callsArg(4)
  229. @EditorHttpController.renameEntity @req, @res
  230. it "should send back a bad request status code", ->
  231. @res.sendStatus.calledWith(400).should.equal true
  232. describe "moveEntity", ->
  233. beforeEach ->
  234. @req.params =
  235. Project_id: @project_id
  236. entity_id: @entity_id = "entity-id-123"
  237. entity_type: @entity_type = "entity-type"
  238. @req.body =
  239. folder_id: @folder_id = "folder-id-123"
  240. @EditorController.moveEntity = sinon.stub().callsArg(5)
  241. @EditorHttpController.moveEntity @req, @res
  242. it "should call EditorController.moveEntity", ->
  243. @EditorController.moveEntity
  244. .calledWith(@project_id, @entity_id, @folder_id, @entity_type, @userId)
  245. .should.equal true
  246. it "should send back a success response", ->
  247. @res.sendStatus.calledWith(204).should.equal true
  248. describe "deleteEntity", ->
  249. beforeEach ->
  250. @req.params =
  251. Project_id: @project_id
  252. entity_id: @entity_id = "entity-id-123"
  253. entity_type: @entity_type = "entity-type"
  254. @EditorController.deleteEntity = sinon.stub().callsArg(5)
  255. @EditorHttpController.deleteEntity @req, @res
  256. it "should call EditorController.deleteEntity", ->
  257. @EditorController.deleteEntity
  258. .calledWith(@project_id, @entity_id, @entity_type, "editor", @userId)
  259. .should.equal true
  260. it "should send back a success response", ->
  261. @res.sendStatus.calledWith(204).should.equal true