EditorHttpControllerTests.coffee 11 KB

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