EditorHttpControllerTests.coffee 11 KB

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