EditorHttpControllerTests.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const SandboxedModule = require('sandboxed-module')
  13. const sinon = require('sinon')
  14. require('chai').should()
  15. const modulePath = require('path').join(
  16. __dirname,
  17. '../../../../app/src/Features/Editor/EditorHttpController'
  18. )
  19. const Errors = require('../../../../app/src/Features/Errors/Errors')
  20. describe('EditorHttpController', function() {
  21. beforeEach(function() {
  22. this.EditorHttpController = SandboxedModule.require(modulePath, {
  23. requires: {
  24. '../Project/ProjectEntityUpdateHandler': (this.ProjectEntityUpdateHandler = {}),
  25. '../Project/ProjectDeleter': (this.ProjectDeleter = {}),
  26. '../Project/ProjectGetter': (this.ProjectGetter = {}),
  27. '../User/UserGetter': (this.UserGetter = {}),
  28. '../Authorization/AuthorizationManager': (this.AuthorizationManager = {}),
  29. '../Project/ProjectEditorHandler': (this.ProjectEditorHandler = {}),
  30. './EditorRealTimeController': (this.EditorRealTimeController = {}),
  31. 'logger-sharelatex': (this.logger = {
  32. log: sinon.stub(),
  33. error: sinon.stub()
  34. }),
  35. './EditorController': (this.EditorController = {}),
  36. 'metrics-sharelatex': (this.Metrics = { inc: sinon.stub() }),
  37. '../Collaborators/CollaboratorsHandler': (this.CollaboratorsHandler = {}),
  38. '../Collaborators/CollaboratorsInviteHandler': (this.CollaboratorsInviteHandler = {}),
  39. '../TokenAccess/TokenAccessHandler': (this.TokenAccessHandler = {}),
  40. '../Authentication/AuthenticationController': (this.AuthenticationController = {}),
  41. '../Errors/Errors': Errors
  42. }
  43. })
  44. this.project_id = 'mock-project-id'
  45. this.doc_id = 'mock-doc-id'
  46. this.user_id = 'mock-user-id'
  47. this.parent_folder_id = 'mock-folder-id'
  48. this.userId = 1234
  49. this.AuthenticationController.getLoggedInUserId = sinon
  50. .stub()
  51. .returns(this.userId)
  52. this.req = {}
  53. this.res = {
  54. send: sinon.stub(),
  55. sendStatus: sinon.stub(),
  56. json: sinon.stub()
  57. }
  58. this.callback = sinon.stub()
  59. this.TokenAccessHandler.getRequestToken = sinon
  60. .stub()
  61. .returns((this.token = null))
  62. return (this.TokenAccessHandler.protectTokens = sinon.stub())
  63. })
  64. describe('joinProject', function() {
  65. beforeEach(function() {
  66. this.req.params = { Project_id: this.project_id }
  67. this.req.query = { user_id: this.user_id }
  68. this.projectView = {
  69. _id: this.project_id
  70. }
  71. this.EditorHttpController._buildJoinProjectView = sinon
  72. .stub()
  73. .callsArgWith(3, null, this.projectView, 'owner')
  74. return (this.ProjectDeleter.unmarkAsDeletedByExternalSource = sinon.stub())
  75. })
  76. describe('successfully', function() {
  77. beforeEach(function() {
  78. return this.EditorHttpController.joinProject(this.req, this.res)
  79. })
  80. it('should get the project view', function() {
  81. return this.EditorHttpController._buildJoinProjectView
  82. .calledWith(this.req, this.project_id, this.user_id)
  83. .should.equal(true)
  84. })
  85. it('should return the project and privilege level', function() {
  86. return this.res.json
  87. .calledWith({
  88. project: this.projectView,
  89. privilegeLevel: 'owner'
  90. })
  91. .should.equal(true)
  92. })
  93. it('should not try to unmark the project as deleted', function() {
  94. return this.ProjectDeleter.unmarkAsDeletedByExternalSource.called.should.equal(
  95. false
  96. )
  97. })
  98. it('should send an inc metric', function() {
  99. return this.Metrics.inc
  100. .calledWith('editor.join-project')
  101. .should.equal(true)
  102. })
  103. })
  104. describe('when the project is marked as deleted', function() {
  105. beforeEach(function() {
  106. this.projectView.deletedByExternalDataSource = true
  107. return this.EditorHttpController.joinProject(this.req, this.res)
  108. })
  109. it('should unmark the project as deleted', function() {
  110. return this.ProjectDeleter.unmarkAsDeletedByExternalSource
  111. .calledWith(this.project_id)
  112. .should.equal(true)
  113. })
  114. })
  115. describe('with an anonymous user', function() {
  116. beforeEach(function() {
  117. this.req.query = { user_id: 'anonymous-user' }
  118. return this.EditorHttpController.joinProject(this.req, this.res)
  119. })
  120. it('should pass the user id as null', function() {
  121. return this.EditorHttpController._buildJoinProjectView
  122. .calledWith(this.req, this.project_id, null)
  123. .should.equal(true)
  124. })
  125. })
  126. })
  127. describe('_buildJoinProjectView', function() {
  128. beforeEach(function() {
  129. this.project = {
  130. _id: this.project_id,
  131. owner_ref: { _id: 'something' }
  132. }
  133. this.user = {
  134. _id: (this.user_id = 'user-id'),
  135. projects: {}
  136. }
  137. this.members = ['members', 'mock']
  138. this.tokenMembers = ['one', 'two']
  139. this.projectModelView = {
  140. _id: this.project_id,
  141. owner: { _id: 'something' },
  142. view: true
  143. }
  144. this.invites = [
  145. {
  146. _id: 'invite_one',
  147. email: 'user-one@example.com',
  148. privileges: 'readOnly',
  149. projectId: this.project._id
  150. },
  151. {
  152. _id: 'invite_two',
  153. email: 'user-two@example.com',
  154. privileges: 'readOnly',
  155. projectId: this.project._id
  156. }
  157. ]
  158. this.ProjectEditorHandler.buildProjectModelView = sinon
  159. .stub()
  160. .returns(this.projectModelView)
  161. this.ProjectGetter.getProjectWithoutDocLines = sinon
  162. .stub()
  163. .callsArgWith(1, null, this.project)
  164. this.CollaboratorsHandler.getInvitedMembersWithPrivilegeLevels = sinon
  165. .stub()
  166. .callsArgWith(1, null, this.members)
  167. this.CollaboratorsHandler.getTokenMembersWithPrivilegeLevels = sinon
  168. .stub()
  169. .callsArgWith(1, null, this.tokenMembers)
  170. this.CollaboratorsInviteHandler.getAllInvites = sinon
  171. .stub()
  172. .callsArgWith(1, null, this.invites)
  173. return (this.UserGetter.getUser = sinon
  174. .stub()
  175. .callsArgWith(2, null, this.user))
  176. })
  177. describe('when project is not found', function() {
  178. beforeEach(function() {
  179. this.ProjectGetter.getProjectWithoutDocLines.yields(null, null)
  180. return this.EditorHttpController._buildJoinProjectView(
  181. this.req,
  182. this.project_id,
  183. this.user_id,
  184. this.callback
  185. )
  186. })
  187. it('should handle return not found error', function() {
  188. let args = this.callback.lastCall.args
  189. args.length.should.equal(1)
  190. args[0].should.be.instanceof(Errors.NotFoundError)
  191. })
  192. })
  193. describe('when authorized', function() {
  194. beforeEach(function() {
  195. this.AuthorizationManager.getPrivilegeLevelForProject = sinon
  196. .stub()
  197. .callsArgWith(3, null, 'owner')
  198. return this.EditorHttpController._buildJoinProjectView(
  199. this.req,
  200. this.project_id,
  201. this.user_id,
  202. this.callback
  203. )
  204. })
  205. it('should find the project without doc lines', function() {
  206. return this.ProjectGetter.getProjectWithoutDocLines
  207. .calledWith(this.project_id)
  208. .should.equal(true)
  209. })
  210. it('should get the list of users in the project', function() {
  211. return this.CollaboratorsHandler.getInvitedMembersWithPrivilegeLevels
  212. .calledWith(this.project_id)
  213. .should.equal(true)
  214. })
  215. it('should check the privilege level', function() {
  216. return this.AuthorizationManager.getPrivilegeLevelForProject
  217. .calledWith(this.user_id, this.project_id, this.token)
  218. .should.equal(true)
  219. })
  220. it('should include the invites', function() {
  221. return this.CollaboratorsInviteHandler.getAllInvites
  222. .calledWith(this.project._id)
  223. .should.equal(true)
  224. })
  225. it('should return the project model view, privilege level and protocol version', function() {
  226. return this.callback
  227. .calledWith(null, this.projectModelView, 'owner')
  228. .should.equal(true)
  229. })
  230. })
  231. describe('when not authorized', function() {
  232. beforeEach(function() {
  233. this.AuthorizationManager.getPrivilegeLevelForProject = sinon
  234. .stub()
  235. .callsArgWith(3, null, null)
  236. return this.EditorHttpController._buildJoinProjectView(
  237. this.req,
  238. this.project_id,
  239. this.user_id,
  240. this.callback
  241. )
  242. })
  243. it('should return false in the callback', function() {
  244. return this.callback.calledWith(null, null, false).should.equal(true)
  245. })
  246. })
  247. })
  248. describe('addDoc', function() {
  249. beforeEach(function() {
  250. this.doc = { mock: 'doc' }
  251. this.req.params = { Project_id: this.project_id }
  252. this.req.body = {
  253. name: (this.name = 'doc-name'),
  254. parent_folder_id: this.parent_folder_id
  255. }
  256. return (this.EditorController.addDoc = sinon
  257. .stub()
  258. .callsArgWith(6, null, this.doc))
  259. })
  260. describe('successfully', function() {
  261. beforeEach(function() {
  262. return this.EditorHttpController.addDoc(this.req, this.res)
  263. })
  264. it('should call EditorController.addDoc', function() {
  265. return this.EditorController.addDoc
  266. .calledWith(
  267. this.project_id,
  268. this.parent_folder_id,
  269. this.name,
  270. [],
  271. 'editor',
  272. this.userId
  273. )
  274. .should.equal(true)
  275. })
  276. it('should send the doc back as JSON', function() {
  277. return this.res.json.calledWith(this.doc).should.equal(true)
  278. })
  279. })
  280. describe('unsuccesfully', function() {
  281. beforeEach(function() {
  282. this.req.body.name = ''
  283. return this.EditorHttpController.addDoc(this.req, this.res)
  284. })
  285. it('should send back a bad request status code', function() {
  286. return this.res.sendStatus.calledWith(400).should.equal(true)
  287. })
  288. })
  289. })
  290. describe('addFolder', function() {
  291. beforeEach(function() {
  292. this.folder = { mock: 'folder' }
  293. this.req.params = { Project_id: this.project_id }
  294. this.req.body = {
  295. name: (this.name = 'folder-name'),
  296. parent_folder_id: this.parent_folder_id
  297. }
  298. return (this.EditorController.addFolder = sinon
  299. .stub()
  300. .callsArgWith(4, null, this.folder))
  301. })
  302. describe('successfully', function() {
  303. beforeEach(function() {
  304. return this.EditorHttpController.addFolder(this.req, this.res)
  305. })
  306. it('should call EditorController.addFolder', function() {
  307. return this.EditorController.addFolder
  308. .calledWith(
  309. this.project_id,
  310. this.parent_folder_id,
  311. this.name,
  312. 'editor'
  313. )
  314. .should.equal(true)
  315. })
  316. it('should send the folder back as JSON', function() {
  317. return this.res.json.calledWith(this.folder).should.equal(true)
  318. })
  319. })
  320. describe('unsuccesfully', function() {
  321. beforeEach(function() {
  322. this.req.body.name = ''
  323. return this.EditorHttpController.addFolder(this.req, this.res)
  324. })
  325. it('should send back a bad request status code', function() {
  326. return this.res.sendStatus.calledWith(400).should.equal(true)
  327. })
  328. })
  329. })
  330. describe('renameEntity', function() {
  331. beforeEach(function() {
  332. this.req.params = {
  333. Project_id: this.project_id,
  334. entity_id: (this.entity_id = 'entity-id-123'),
  335. entity_type: (this.entity_type = 'entity-type')
  336. }
  337. this.req.body = { name: (this.name = 'new-name') }
  338. this.EditorController.renameEntity = sinon.stub().callsArg(5)
  339. return this.EditorHttpController.renameEntity(this.req, this.res)
  340. })
  341. it('should call EditorController.renameEntity', function() {
  342. return this.EditorController.renameEntity
  343. .calledWith(
  344. this.project_id,
  345. this.entity_id,
  346. this.entity_type,
  347. this.name,
  348. this.userId
  349. )
  350. .should.equal(true)
  351. })
  352. it('should send back a success response', function() {
  353. return this.res.sendStatus.calledWith(204).should.equal(true)
  354. })
  355. })
  356. describe('renameEntity with long name', function() {
  357. beforeEach(function() {
  358. this.req.params = {
  359. Project_id: this.project_id,
  360. entity_id: (this.entity_id = 'entity-id-123'),
  361. entity_type: (this.entity_type = 'entity-type')
  362. }
  363. this.req.body = {
  364. name: (this.name =
  365. 'EDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOTEDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOT')
  366. }
  367. this.EditorController.renameEntity = sinon.stub().callsArg(4)
  368. return this.EditorHttpController.renameEntity(this.req, this.res)
  369. })
  370. it('should send back a bad request status code', function() {
  371. return this.res.sendStatus.calledWith(400).should.equal(true)
  372. })
  373. })
  374. describe('rename entity with 0 length name', function() {
  375. beforeEach(function() {
  376. this.req.params = {
  377. Project_id: this.project_id,
  378. entity_id: (this.entity_id = 'entity-id-123'),
  379. entity_type: (this.entity_type = 'entity-type')
  380. }
  381. this.req.body = { name: (this.name = '') }
  382. this.EditorController.renameEntity = sinon.stub().callsArg(4)
  383. return this.EditorHttpController.renameEntity(this.req, this.res)
  384. })
  385. it('should send back a bad request status code', function() {
  386. return this.res.sendStatus.calledWith(400).should.equal(true)
  387. })
  388. })
  389. describe('moveEntity', function() {
  390. beforeEach(function() {
  391. this.req.params = {
  392. Project_id: this.project_id,
  393. entity_id: (this.entity_id = 'entity-id-123'),
  394. entity_type: (this.entity_type = 'entity-type')
  395. }
  396. this.req.body = { folder_id: (this.folder_id = 'folder-id-123') }
  397. this.EditorController.moveEntity = sinon.stub().callsArg(5)
  398. return this.EditorHttpController.moveEntity(this.req, this.res)
  399. })
  400. it('should call EditorController.moveEntity', function() {
  401. return this.EditorController.moveEntity
  402. .calledWith(
  403. this.project_id,
  404. this.entity_id,
  405. this.folder_id,
  406. this.entity_type,
  407. this.userId
  408. )
  409. .should.equal(true)
  410. })
  411. it('should send back a success response', function() {
  412. return this.res.sendStatus.calledWith(204).should.equal(true)
  413. })
  414. })
  415. describe('deleteEntity', function() {
  416. beforeEach(function() {
  417. this.req.params = {
  418. Project_id: this.project_id,
  419. entity_id: (this.entity_id = 'entity-id-123'),
  420. entity_type: (this.entity_type = 'entity-type')
  421. }
  422. this.EditorController.deleteEntity = sinon.stub().callsArg(5)
  423. return this.EditorHttpController.deleteEntity(this.req, this.res)
  424. })
  425. it('should call EditorController.deleteEntity', function() {
  426. return this.EditorController.deleteEntity
  427. .calledWith(
  428. this.project_id,
  429. this.entity_id,
  430. this.entity_type,
  431. 'editor',
  432. this.userId
  433. )
  434. .should.equal(true)
  435. })
  436. it('should send back a success response', function() {
  437. return this.res.sendStatus.calledWith(204).should.equal(true)
  438. })
  439. })
  440. })