EditorHttpControllerTests.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. },
  59. }
  60. this.CollaboratorsHandler = {
  61. promises: {
  62. userIsTokenMember: sinon.stub().resolves(false),
  63. },
  64. }
  65. this.CollaboratorsInviteHandler = {
  66. promises: {
  67. getAllInvites: sinon.stub().resolves([
  68. {
  69. _id: 'invite_one',
  70. email: 'user-one@example.com',
  71. privileges: 'readOnly',
  72. projectId: this.project._id,
  73. },
  74. {
  75. _id: 'invite_two',
  76. email: 'user-two@example.com',
  77. privileges: 'readOnly',
  78. projectId: this.project._id,
  79. },
  80. ]),
  81. },
  82. }
  83. this.EditorController = {
  84. promises: {
  85. addDoc: sinon.stub().resolves(this.doc),
  86. addFile: sinon.stub().resolves(this.file),
  87. addFolder: sinon.stub().resolves(this.folder),
  88. renameEntity: sinon.stub().resolves(),
  89. moveEntity: sinon.stub().resolves(),
  90. deleteEntity: sinon.stub().resolves(),
  91. },
  92. }
  93. this.ProjectDeleter = {
  94. promises: {
  95. unmarkAsDeletedByExternalSource: sinon.stub().resolves(),
  96. },
  97. }
  98. this.ProjectGetter = {
  99. promises: {
  100. getProjectWithoutDocLines: sinon.stub().resolves(this.project),
  101. },
  102. }
  103. this.ProjectEditorHandler = {
  104. buildProjectModelView: sinon.stub().returns(this.projectView),
  105. }
  106. this.Metrics = { inc: sinon.stub() }
  107. this.TokenAccessHandler = {
  108. getRequestToken: sinon.stub().returns(this.token),
  109. protectTokens: sinon.stub(),
  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.EditorHttpController = SandboxedModule.require(MODULE_PATH, {
  129. requires: {
  130. '../Project/ProjectDeleter': this.ProjectDeleter,
  131. '../Project/ProjectGetter': this.ProjectGetter,
  132. '../Authorization/AuthorizationManager': this.AuthorizationManager,
  133. '../Project/ProjectEditorHandler': this.ProjectEditorHandler,
  134. './EditorController': this.EditorController,
  135. '@overleaf/metrics': this.Metrics,
  136. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  137. '../Collaborators/CollaboratorsHandler': this.CollaboratorsHandler,
  138. '../Collaborators/CollaboratorsInviteHandler':
  139. this.CollaboratorsInviteHandler,
  140. '../TokenAccess/TokenAccessHandler': this.TokenAccessHandler,
  141. '../Authentication/SessionManager': this.SessionManager,
  142. '../../infrastructure/FileWriter': this.FileWriter,
  143. '../Project/ProjectEntityUpdateHandler':
  144. this.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, source: this.source }
  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. this.source
  389. )
  390. })
  391. it('should send back a success response', function () {
  392. expect(this.res.sendStatus).to.have.been.calledWith(204)
  393. })
  394. })
  395. describe('with long name', function () {
  396. beforeEach(function () {
  397. this.newName = 'long'.repeat(100)
  398. this.req.body = { name: this.newName, source: this.source }
  399. this.EditorHttpController.renameEntity(this.req, this.res)
  400. })
  401. it('should send back a bad request status code', function () {
  402. expect(this.res.sendStatus).to.have.been.calledWith(400)
  403. })
  404. })
  405. describe('with 0 length name', function () {
  406. beforeEach(function () {
  407. this.newName = ''
  408. this.req.body = { name: this.newName, source: this.source }
  409. this.EditorHttpController.renameEntity(this.req, this.res)
  410. })
  411. it('should send back a bad request status code', function () {
  412. expect(this.res.sendStatus).to.have.been.calledWith(400)
  413. })
  414. })
  415. })
  416. describe('moveEntity', function () {
  417. beforeEach(function (done) {
  418. this.entityId = 'entity-id-123'
  419. this.entityType = 'entity-type'
  420. this.folderId = 'folder-id-123'
  421. this.req.params = {
  422. Project_id: this.project._id,
  423. entity_id: this.entityId,
  424. entity_type: this.entityType,
  425. }
  426. this.req.body = { folder_id: this.folderId, source: this.source }
  427. this.res.sendStatus.callsFake(() => done())
  428. this.EditorHttpController.moveEntity(this.req, this.res)
  429. })
  430. it('should call EditorController.moveEntity', function () {
  431. expect(this.EditorController.promises.moveEntity).to.have.been.calledWith(
  432. this.project._id,
  433. this.entityId,
  434. this.folderId,
  435. this.entityType,
  436. this.user._id,
  437. this.source
  438. )
  439. })
  440. it('should send back a success response', function () {
  441. expect(this.res.sendStatus).to.have.been.calledWith(204)
  442. })
  443. })
  444. describe('deleteEntity', function () {
  445. beforeEach(function (done) {
  446. this.entityId = 'entity-id-123'
  447. this.entityType = 'entity-type'
  448. this.req.params = {
  449. Project_id: this.project._id,
  450. entity_id: this.entityId,
  451. entity_type: this.entityType,
  452. }
  453. this.res.sendStatus.callsFake(() => done())
  454. this.EditorHttpController.deleteEntity(this.req, this.res)
  455. })
  456. it('should call EditorController.deleteEntity', function () {
  457. expect(
  458. this.EditorController.promises.deleteEntity
  459. ).to.have.been.calledWith(
  460. this.project._id,
  461. this.entityId,
  462. this.entityType,
  463. 'editor',
  464. this.user._id
  465. )
  466. })
  467. it('should send back a success response', function () {
  468. expect(this.res.sendStatus).to.have.been.calledWith(204)
  469. })
  470. })
  471. })