EditorHttpControllerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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)
  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 "restoreDoc", ->
  145. beforeEach ->
  146. @req.params =
  147. Project_id: @project_id
  148. doc_id: @doc_id
  149. @req.body =
  150. name: @name = "doc-name"
  151. @ProjectEntityUpdateHandler.restoreDoc = sinon.stub().callsArgWith(3, null,
  152. @doc = { "mock": "doc", _id: @new_doc_id = "new-doc-id" }
  153. @folder_id = "mock-folder-id"
  154. )
  155. @EditorRealTimeController.emitToRoom = sinon.stub()
  156. @EditorHttpController.restoreDoc @req, @res
  157. it "should restore the doc", ->
  158. @ProjectEntityUpdateHandler.restoreDoc
  159. .calledWith(@project_id, @doc_id, @name)
  160. .should.equal true
  161. it "should the real-time clients about the new doc", ->
  162. @EditorRealTimeController.emitToRoom
  163. .calledWith(@project_id, 'reciveNewDoc', @folder_id, @doc)
  164. .should.equal true
  165. it "should return the new doc id", ->
  166. @res.json
  167. .calledWith(doc_id: @new_doc_id)
  168. .should.equal true
  169. describe "addDoc", ->
  170. beforeEach ->
  171. @doc = { "mock": "doc" }
  172. @req.params =
  173. Project_id: @project_id
  174. @req.body =
  175. name: @name = "doc-name"
  176. parent_folder_id: @parent_folder_id
  177. @EditorController.addDoc = sinon.stub().callsArgWith(6, null, @doc)
  178. describe "successfully", ->
  179. beforeEach ->
  180. @EditorHttpController.addDoc @req, @res
  181. it "should call EditorController.addDoc", ->
  182. @EditorController.addDoc
  183. .calledWith(@project_id, @parent_folder_id, @name, [], "editor", @userId)
  184. .should.equal true
  185. it "should send the doc back as JSON", ->
  186. @res.json
  187. .calledWith(@doc)
  188. .should.equal true
  189. describe "unsuccesfully", ->
  190. beforeEach ->
  191. @req.body.name = ""
  192. @EditorHttpController.addDoc @req, @res
  193. it "should send back a bad request status code", ->
  194. @res.sendStatus.calledWith(400).should.equal true
  195. describe "addFolder", ->
  196. beforeEach ->
  197. @folder = { "mock": "folder" }
  198. @req.params =
  199. Project_id: @project_id
  200. @req.body =
  201. name: @name = "folder-name"
  202. parent_folder_id: @parent_folder_id
  203. @EditorController.addFolder = sinon.stub().callsArgWith(4, null, @folder)
  204. describe "successfully", ->
  205. beforeEach ->
  206. @EditorHttpController.addFolder @req, @res
  207. it "should call EditorController.addFolder", ->
  208. @EditorController.addFolder
  209. .calledWith(@project_id, @parent_folder_id, @name, "editor")
  210. .should.equal true
  211. it "should send the folder back as JSON", ->
  212. @res.json
  213. .calledWith(@folder)
  214. .should.equal true
  215. describe "unsuccesfully", ->
  216. beforeEach ->
  217. @req.body.name = ""
  218. @EditorHttpController.addFolder @req, @res
  219. it "should send back a bad request status code", ->
  220. @res.sendStatus.calledWith(400).should.equal true
  221. describe "renameEntity", ->
  222. beforeEach ->
  223. @req.params =
  224. Project_id: @project_id
  225. entity_id: @entity_id = "entity-id-123"
  226. entity_type: @entity_type = "entity-type"
  227. @req.body =
  228. name: @name = "new-name"
  229. @EditorController.renameEntity = sinon.stub().callsArg(5)
  230. @EditorHttpController.renameEntity @req, @res
  231. it "should call EditorController.renameEntity", ->
  232. @EditorController.renameEntity
  233. .calledWith(@project_id, @entity_id, @entity_type, @name, @userId)
  234. .should.equal true
  235. it "should send back a success response", ->
  236. @res.sendStatus.calledWith(204).should.equal true
  237. describe "renameEntity with long name", ->
  238. beforeEach ->
  239. @req.params =
  240. Project_id: @project_id
  241. entity_id: @entity_id = "entity-id-123"
  242. entity_type: @entity_type = "entity-type"
  243. @req.body =
  244. name: @name = "EDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOTEDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOT"
  245. @EditorController.renameEntity = sinon.stub().callsArg(4)
  246. @EditorHttpController.renameEntity @req, @res
  247. it "should send back a bad request status code", ->
  248. @res.sendStatus.calledWith(400).should.equal true
  249. describe "rename entity with 0 length name", ->
  250. beforeEach ->
  251. @req.params =
  252. Project_id: @project_id
  253. entity_id: @entity_id = "entity-id-123"
  254. entity_type: @entity_type = "entity-type"
  255. @req.body =
  256. name: @name = ""
  257. @EditorController.renameEntity = sinon.stub().callsArg(4)
  258. @EditorHttpController.renameEntity @req, @res
  259. it "should send back a bad request status code", ->
  260. @res.sendStatus.calledWith(400).should.equal true
  261. describe "moveEntity", ->
  262. beforeEach ->
  263. @req.params =
  264. Project_id: @project_id
  265. entity_id: @entity_id = "entity-id-123"
  266. entity_type: @entity_type = "entity-type"
  267. @req.body =
  268. folder_id: @folder_id = "folder-id-123"
  269. @EditorController.moveEntity = sinon.stub().callsArg(5)
  270. @EditorHttpController.moveEntity @req, @res
  271. it "should call EditorController.moveEntity", ->
  272. @EditorController.moveEntity
  273. .calledWith(@project_id, @entity_id, @folder_id, @entity_type, @userId)
  274. .should.equal true
  275. it "should send back a success response", ->
  276. @res.sendStatus.calledWith(204).should.equal true
  277. describe "deleteEntity", ->
  278. beforeEach ->
  279. @req.params =
  280. Project_id: @project_id
  281. entity_id: @entity_id = "entity-id-123"
  282. entity_type: @entity_type = "entity-type"
  283. @EditorController.deleteEntity = sinon.stub().callsArg(5)
  284. @EditorHttpController.deleteEntity @req, @res
  285. it "should call EditorController.deleteEntity", ->
  286. @EditorController.deleteEntity
  287. .calledWith(@project_id, @entity_id, @entity_type, "editor", @userId)
  288. .should.equal true
  289. it "should send back a success response", ->
  290. @res.sendStatus.calledWith(204).should.equal true