EditorHttpControllerTests.js 18 KB

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