EditorHttpControllerTests.js 18 KB

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