EditorHttpControllerTests.js 18 KB

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