EditorHttpControllerTests.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. getAssignmentForMongoUser: sinon
  131. .stub()
  132. .resolves({ variant: 'default' }),
  133. },
  134. }
  135. this.UserGetter = { promises: { getUser: sinon.stub().resolves(null, {}) } }
  136. this.EditorHttpController = SandboxedModule.require(MODULE_PATH, {
  137. requires: {
  138. '../Project/ProjectDeleter': this.ProjectDeleter,
  139. '../Project/ProjectGetter': this.ProjectGetter,
  140. '../Authorization/AuthorizationManager': this.AuthorizationManager,
  141. '../Project/ProjectEditorHandler': this.ProjectEditorHandler,
  142. './EditorController': this.EditorController,
  143. '@overleaf/metrics': this.Metrics,
  144. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  145. '../Collaborators/CollaboratorsHandler': this.CollaboratorsHandler,
  146. '../Collaborators/CollaboratorsInviteGetter':
  147. this.CollaboratorsInviteGetter,
  148. '../TokenAccess/TokenAccessHandler': this.TokenAccessHandler,
  149. '../Authentication/SessionManager': this.SessionManager,
  150. '../../infrastructure/FileWriter': this.FileWriter,
  151. '../Project/ProjectEntityUpdateHandler':
  152. this.ProjectEntityUpdateHandler,
  153. '../Docstore/DocstoreManager': this.DocstoreManager,
  154. '../Errors/HttpErrorHandler': this.HttpErrorHandler,
  155. '../SplitTests/SplitTestHandler': this.SplitTestHandler,
  156. '../Compile/CompileManager': {},
  157. '../User/UserGetter': this.UserGetter,
  158. },
  159. })
  160. })
  161. describe('joinProject', function () {
  162. beforeEach(function () {
  163. this.req.params = { Project_id: this.project._id }
  164. this.req.query = { user_id: this.user._id }
  165. this.req.body = { userId: this.user._id }
  166. })
  167. describe('successfully', function () {
  168. beforeEach(function (done) {
  169. this.CollaboratorsGetter.promises.isUserInvitedMemberOfProject.resolves(
  170. true
  171. )
  172. this.res.callback = done
  173. this.EditorHttpController.joinProject(this.req, this.res)
  174. })
  175. it('should return the project and privilege level', function () {
  176. expect(this.res.json).to.have.been.calledWith({
  177. project: this.projectView,
  178. privilegeLevel: 'owner',
  179. isRestrictedUser: false,
  180. isTokenMember: false,
  181. isInvitedMember: true,
  182. })
  183. })
  184. it('should not try to unmark the project as deleted', function () {
  185. expect(this.ProjectDeleter.promises.unmarkAsDeletedByExternalSource).not
  186. .to.have.been.called
  187. })
  188. it('should send an inc metric', function () {
  189. expect(this.Metrics.inc).to.have.been.calledWith('editor.join-project')
  190. })
  191. })
  192. describe('when the project is marked as deleted', function () {
  193. beforeEach(function (done) {
  194. this.projectView.deletedByExternalDataSource = true
  195. this.res.callback = done
  196. this.EditorHttpController.joinProject(this.req, this.res)
  197. })
  198. it('should unmark the project as deleted', function () {
  199. expect(
  200. this.ProjectDeleter.promises.unmarkAsDeletedByExternalSource
  201. ).to.have.been.calledWith(this.project._id)
  202. })
  203. })
  204. describe('with a restricted user', function () {
  205. beforeEach(function (done) {
  206. this.AuthorizationManager.isRestrictedUser.returns(true)
  207. this.AuthorizationManager.promises.getPrivilegeLevelForProject.resolves(
  208. 'readOnly'
  209. )
  210. this.res.callback = done
  211. this.EditorHttpController.joinProject(this.req, this.res)
  212. })
  213. it('should mark the user as restricted, and hide details of owner', function () {
  214. expect(this.res.json).to.have.been.calledWith({
  215. project: this.reducedProjectView,
  216. privilegeLevel: 'readOnly',
  217. isRestrictedUser: true,
  218. isTokenMember: false,
  219. isInvitedMember: false,
  220. })
  221. })
  222. })
  223. describe('when not authorized', function () {
  224. beforeEach(function (done) {
  225. this.AuthorizationManager.promises.getPrivilegeLevelForProject.resolves(
  226. null
  227. )
  228. this.res.callback = done
  229. this.EditorHttpController.joinProject(this.req, this.res)
  230. })
  231. it('should send a 403 response', function () {
  232. expect(this.res.statusCode).to.equal(403)
  233. })
  234. })
  235. describe('with an anonymous user', function () {
  236. beforeEach(function (done) {
  237. this.token = 'token'
  238. this.TokenAccessHandler.getRequestToken.returns(this.token)
  239. this.req.body = {
  240. userId: 'anonymous-user',
  241. anonymousAccessToken: this.token,
  242. }
  243. this.res.callback = done
  244. this.AuthorizationManager.isRestrictedUser
  245. .withArgs(null, 'readOnly', false, false)
  246. .returns(true)
  247. this.AuthorizationManager.promises.getPrivilegeLevelForProject
  248. .withArgs(null, this.project._id, this.token)
  249. .resolves('readOnly')
  250. this.EditorHttpController.joinProject(this.req, this.res)
  251. })
  252. it('should mark the user as restricted', function () {
  253. expect(this.res.json).to.have.been.calledWith({
  254. project: this.reducedProjectView,
  255. privilegeLevel: 'readOnly',
  256. isRestrictedUser: true,
  257. isTokenMember: false,
  258. isInvitedMember: false,
  259. })
  260. })
  261. })
  262. describe('with a token access user', function () {
  263. beforeEach(function (done) {
  264. this.CollaboratorsGetter.promises.isUserInvitedMemberOfProject.resolves(
  265. false
  266. )
  267. this.CollaboratorsHandler.promises.userIsTokenMember.resolves(true)
  268. this.AuthorizationManager.promises.getPrivilegeLevelForProject.resolves(
  269. 'readAndWrite'
  270. )
  271. this.res.callback = done
  272. this.EditorHttpController.joinProject(this.req, this.res)
  273. })
  274. it('should mark the user as being a token-access member', function () {
  275. expect(this.res.json).to.have.been.calledWith({
  276. project: this.projectView,
  277. privilegeLevel: 'readAndWrite',
  278. isRestrictedUser: false,
  279. isTokenMember: true,
  280. isInvitedMember: false,
  281. })
  282. })
  283. })
  284. describe('when project is not found', function () {
  285. beforeEach(function (done) {
  286. this.ProjectGetter.promises.getProjectWithoutDocLines.resolves(null)
  287. this.next.callsFake(() => done())
  288. this.EditorHttpController.joinProject(this.req, this.res, this.next)
  289. })
  290. it('should handle return not found error', function () {
  291. expect(this.next).to.have.been.calledWith(
  292. sinon.match.instanceOf(Errors.NotFoundError)
  293. )
  294. })
  295. })
  296. })
  297. describe('addDoc', function () {
  298. beforeEach(function () {
  299. this.req.params = { Project_id: this.project._id }
  300. this.req.body = {
  301. name: (this.name = 'doc-name'),
  302. parent_folder_id: this.parentFolderId,
  303. }
  304. })
  305. describe('successfully', function () {
  306. beforeEach(function (done) {
  307. this.res.callback = done
  308. this.EditorHttpController.addDoc(this.req, this.res)
  309. })
  310. it('should call EditorController.addDoc', function () {
  311. expect(this.EditorController.promises.addDoc).to.have.been.calledWith(
  312. this.project._id,
  313. this.parentFolderId,
  314. this.name,
  315. [],
  316. 'editor',
  317. this.user._id
  318. )
  319. })
  320. it('should send the doc back as JSON', function () {
  321. expect(this.res.json).to.have.been.calledWith(this.doc)
  322. })
  323. })
  324. describe('unsuccesfully', function () {
  325. it('handle name too short', function (done) {
  326. this.req.body.name = ''
  327. this.res.callback = () => {
  328. expect(this.res.statusCode).to.equal(400)
  329. done()
  330. }
  331. this.EditorHttpController.addDoc(this.req, this.res)
  332. })
  333. it('handle too many files', function (done) {
  334. this.EditorController.promises.addDoc.rejects(
  335. new Error('project_has_too_many_files')
  336. )
  337. this.res.callback = () => {
  338. expect(this.res.body).to.equal('"project_has_too_many_files"')
  339. expect(this.res.status).to.have.been.calledWith(400)
  340. done()
  341. }
  342. this.EditorHttpController.addDoc(this.req, this.res)
  343. })
  344. })
  345. })
  346. describe('addFolder', function () {
  347. beforeEach(function () {
  348. this.folderName = 'folder-name'
  349. this.req.params = { Project_id: this.project._id }
  350. this.req.body = {
  351. name: this.folderName,
  352. parent_folder_id: this.parentFolderId,
  353. }
  354. })
  355. describe('successfully', function () {
  356. beforeEach(function (done) {
  357. this.res.callback = done
  358. this.EditorHttpController.addFolder(this.req, this.res)
  359. })
  360. it('should call EditorController.addFolder', function () {
  361. expect(
  362. this.EditorController.promises.addFolder
  363. ).to.have.been.calledWith(
  364. this.project._id,
  365. this.parentFolderId,
  366. this.folderName,
  367. 'editor'
  368. )
  369. })
  370. it('should send the folder back as JSON', function () {
  371. expect(this.res.json).to.have.been.calledWith(this.folder)
  372. })
  373. })
  374. describe('unsuccesfully', function () {
  375. it('handle name too short', function (done) {
  376. this.req.body.name = ''
  377. this.res.callback = () => {
  378. expect(this.res.statusCode).to.equal(400)
  379. done()
  380. }
  381. this.EditorHttpController.addFolder(this.req, this.res)
  382. })
  383. it('handle too many files', function (done) {
  384. this.EditorController.promises.addFolder.rejects(
  385. new Error('project_has_too_many_files')
  386. )
  387. this.res.callback = () => {
  388. expect(this.res.body).to.equal('"project_has_too_many_files"')
  389. expect(this.res.statusCode).to.equal(400)
  390. done()
  391. }
  392. this.EditorHttpController.addFolder(this.req, this.res)
  393. })
  394. it('handle invalid element name', function (done) {
  395. this.EditorController.promises.addFolder.rejects(
  396. new Error('invalid element name')
  397. )
  398. this.res.callback = () => {
  399. expect(this.res.body).to.equal('"invalid_file_name"')
  400. expect(this.res.statusCode).to.equal(400)
  401. done()
  402. }
  403. this.EditorHttpController.addFolder(this.req, this.res)
  404. })
  405. })
  406. })
  407. describe('renameEntity', function () {
  408. beforeEach(function () {
  409. this.entityId = 'entity-id-123'
  410. this.entityType = 'entity-type'
  411. this.req.params = {
  412. Project_id: this.project._id,
  413. entity_id: this.entityId,
  414. entity_type: this.entityType,
  415. }
  416. })
  417. describe('successfully', function () {
  418. beforeEach(function (done) {
  419. this.newName = 'new-name'
  420. this.req.body = { name: this.newName, source: this.source }
  421. this.res.callback = done
  422. this.EditorHttpController.renameEntity(this.req, this.res)
  423. })
  424. it('should call EditorController.renameEntity', function () {
  425. expect(
  426. this.EditorController.promises.renameEntity
  427. ).to.have.been.calledWith(
  428. this.project._id,
  429. this.entityId,
  430. this.entityType,
  431. this.newName,
  432. this.user._id,
  433. this.source
  434. )
  435. })
  436. it('should send back a success response', function () {
  437. expect(this.res.sendStatus).to.have.been.calledWith(204)
  438. })
  439. })
  440. describe('with long name', function () {
  441. beforeEach(function () {
  442. this.newName = 'long'.repeat(100)
  443. this.req.body = { name: this.newName, source: this.source }
  444. this.EditorHttpController.renameEntity(this.req, this.res)
  445. })
  446. it('should send back a bad request status code', function () {
  447. expect(this.res.statusCode).to.equal(400)
  448. })
  449. })
  450. describe('with 0 length name', function () {
  451. beforeEach(function () {
  452. this.newName = ''
  453. this.req.body = { name: this.newName, source: this.source }
  454. this.EditorHttpController.renameEntity(this.req, this.res)
  455. })
  456. it('should send back a bad request status code', function () {
  457. expect(this.res.statusCode).to.equal(400)
  458. })
  459. })
  460. })
  461. describe('moveEntity', function () {
  462. beforeEach(function (done) {
  463. this.entityId = 'entity-id-123'
  464. this.entityType = 'entity-type'
  465. this.folderId = 'folder-id-123'
  466. this.req.params = {
  467. Project_id: this.project._id,
  468. entity_id: this.entityId,
  469. entity_type: this.entityType,
  470. }
  471. this.req.body = { folder_id: this.folderId, source: this.source }
  472. this.res.callback = done
  473. this.EditorHttpController.moveEntity(this.req, this.res)
  474. })
  475. it('should call EditorController.moveEntity', function () {
  476. expect(this.EditorController.promises.moveEntity).to.have.been.calledWith(
  477. this.project._id,
  478. this.entityId,
  479. this.folderId,
  480. this.entityType,
  481. this.user._id,
  482. this.source
  483. )
  484. })
  485. it('should send back a success response', function () {
  486. expect(this.res.statusCode).to.equal(204)
  487. })
  488. })
  489. describe('deleteEntity', function () {
  490. beforeEach(function (done) {
  491. this.entityId = 'entity-id-123'
  492. this.entityType = 'entity-type'
  493. this.req.params = {
  494. Project_id: this.project._id,
  495. entity_id: this.entityId,
  496. entity_type: this.entityType,
  497. }
  498. this.res.callback = done
  499. this.EditorHttpController.deleteEntity(this.req, this.res)
  500. })
  501. it('should call EditorController.deleteEntity', function () {
  502. expect(
  503. this.EditorController.promises.deleteEntity
  504. ).to.have.been.calledWith(
  505. this.project._id,
  506. this.entityId,
  507. this.entityType,
  508. 'editor',
  509. this.user._id
  510. )
  511. })
  512. it('should send back a success response', function () {
  513. expect(this.res.statusCode).to.equal(204)
  514. })
  515. })
  516. })