EditorHttpControllerTests.js 19 KB

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