EditorHttpControllerTests.js 17 KB

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