EditorHttpControllerTests.js 19 KB

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