EditorHttpControllerTests.js 17 KB

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