EditorHttpControllerTests.js 16 KB

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