EditorHttpControllerTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 = { i18n: { translate: string => string } }
  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. it('handle name too short', function() {
  282. this.req.body.name = ''
  283. this.EditorHttpController.addDoc(this.req, this.res)
  284. this.res.sendStatus.calledWith(400).should.equal(true)
  285. })
  286. it('handle too many files', function() {
  287. this.EditorController.addDoc.yields(
  288. new Error('project_has_to_many_files')
  289. )
  290. let res = {
  291. status: status => {
  292. status.should.equal(400)
  293. return {
  294. json: json => {
  295. json.should.equal('project_has_to_many_files')
  296. }
  297. }
  298. }
  299. }
  300. this.EditorHttpController.addDoc(this.req, res)
  301. })
  302. })
  303. })
  304. describe('addFolder', function() {
  305. beforeEach(function() {
  306. this.folder = { mock: 'folder' }
  307. this.req.params = { Project_id: this.project_id }
  308. this.req.body = {
  309. name: (this.name = 'folder-name'),
  310. parent_folder_id: this.parent_folder_id
  311. }
  312. return (this.EditorController.addFolder = sinon
  313. .stub()
  314. .callsArgWith(4, null, this.folder))
  315. })
  316. describe('successfully', function() {
  317. beforeEach(function() {
  318. return this.EditorHttpController.addFolder(this.req, this.res)
  319. })
  320. it('should call EditorController.addFolder', function() {
  321. return this.EditorController.addFolder
  322. .calledWith(
  323. this.project_id,
  324. this.parent_folder_id,
  325. this.name,
  326. 'editor'
  327. )
  328. .should.equal(true)
  329. })
  330. it('should send the folder back as JSON', function() {
  331. return this.res.json.calledWith(this.folder).should.equal(true)
  332. })
  333. })
  334. describe('unsuccesfully', function() {
  335. it('handle name too short', function() {
  336. this.req.body.name = ''
  337. this.EditorHttpController.addFolder(this.req, this.res)
  338. this.res.sendStatus.calledWith(400).should.equal(true)
  339. })
  340. it('handle too many files', function() {
  341. this.EditorController.addFolder.yields(
  342. new Error('project_has_to_many_files')
  343. )
  344. let res = {
  345. status: status => {
  346. status.should.equal(400)
  347. return {
  348. json: json => {
  349. json.should.equal('project_has_to_many_files')
  350. }
  351. }
  352. }
  353. }
  354. this.EditorHttpController.addFolder(this.req, res)
  355. })
  356. it('handle invalid element name', function() {
  357. this.EditorController.addFolder.yields(
  358. new Error('invalid element name')
  359. )
  360. let res = {
  361. status: status => {
  362. status.should.equal(400)
  363. return {
  364. json: json => {
  365. json.should.equal('invalid_file_name')
  366. }
  367. }
  368. }
  369. }
  370. this.EditorHttpController.addFolder(this.req, res)
  371. })
  372. })
  373. })
  374. describe('renameEntity', 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 = 'new-name') }
  382. this.EditorController.renameEntity = sinon.stub().callsArg(5)
  383. return this.EditorHttpController.renameEntity(this.req, this.res)
  384. })
  385. it('should call EditorController.renameEntity', function() {
  386. return this.EditorController.renameEntity
  387. .calledWith(
  388. this.project_id,
  389. this.entity_id,
  390. this.entity_type,
  391. this.name,
  392. this.userId
  393. )
  394. .should.equal(true)
  395. })
  396. it('should send back a success response', function() {
  397. return this.res.sendStatus.calledWith(204).should.equal(true)
  398. })
  399. })
  400. describe('renameEntity with long name', function() {
  401. beforeEach(function() {
  402. this.req.params = {
  403. Project_id: this.project_id,
  404. entity_id: (this.entity_id = 'entity-id-123'),
  405. entity_type: (this.entity_type = 'entity-type')
  406. }
  407. this.req.body = {
  408. name: (this.name =
  409. 'EDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOTEDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOT')
  410. }
  411. this.EditorController.renameEntity = sinon.stub().callsArg(4)
  412. return this.EditorHttpController.renameEntity(this.req, this.res)
  413. })
  414. it('should send back a bad request status code', function() {
  415. return this.res.sendStatus.calledWith(400).should.equal(true)
  416. })
  417. })
  418. describe('rename entity with 0 length name', function() {
  419. beforeEach(function() {
  420. this.req.params = {
  421. Project_id: this.project_id,
  422. entity_id: (this.entity_id = 'entity-id-123'),
  423. entity_type: (this.entity_type = 'entity-type')
  424. }
  425. this.req.body = { name: (this.name = '') }
  426. this.EditorController.renameEntity = sinon.stub().callsArg(4)
  427. return this.EditorHttpController.renameEntity(this.req, this.res)
  428. })
  429. it('should send back a bad request status code', function() {
  430. return this.res.sendStatus.calledWith(400).should.equal(true)
  431. })
  432. })
  433. describe('moveEntity', function() {
  434. beforeEach(function() {
  435. this.req.params = {
  436. Project_id: this.project_id,
  437. entity_id: (this.entity_id = 'entity-id-123'),
  438. entity_type: (this.entity_type = 'entity-type')
  439. }
  440. this.req.body = { folder_id: (this.folder_id = 'folder-id-123') }
  441. this.EditorController.moveEntity = sinon.stub().callsArg(5)
  442. return this.EditorHttpController.moveEntity(this.req, this.res)
  443. })
  444. it('should call EditorController.moveEntity', function() {
  445. return this.EditorController.moveEntity
  446. .calledWith(
  447. this.project_id,
  448. this.entity_id,
  449. this.folder_id,
  450. this.entity_type,
  451. this.userId
  452. )
  453. .should.equal(true)
  454. })
  455. it('should send back a success response', function() {
  456. return this.res.sendStatus.calledWith(204).should.equal(true)
  457. })
  458. })
  459. describe('deleteEntity', function() {
  460. beforeEach(function() {
  461. this.req.params = {
  462. Project_id: this.project_id,
  463. entity_id: (this.entity_id = 'entity-id-123'),
  464. entity_type: (this.entity_type = 'entity-type')
  465. }
  466. this.EditorController.deleteEntity = sinon.stub().callsArg(5)
  467. return this.EditorHttpController.deleteEntity(this.req, this.res)
  468. })
  469. it('should call EditorController.deleteEntity', function() {
  470. return this.EditorController.deleteEntity
  471. .calledWith(
  472. this.project_id,
  473. this.entity_id,
  474. this.entity_type,
  475. 'editor',
  476. this.userId
  477. )
  478. .should.equal(true)
  479. })
  480. it('should send back a success response', function() {
  481. return this.res.sendStatus.calledWith(204).should.equal(true)
  482. })
  483. })
  484. })