ProjectDeleterTests.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. const modulePath = '../../../../app/src/Features/Project/ProjectDeleter'
  2. const SandboxedModule = require('sandboxed-module')
  3. const sinon = require('sinon')
  4. const chai = require('chai')
  5. const { expect } = chai
  6. const tk = require('timekeeper')
  7. const moment = require('moment')
  8. const { Project } = require('../helpers/models/Project')
  9. const { DeletedProject } = require('../helpers/models/DeletedProject')
  10. const { ObjectId } = require('mongoose').Types
  11. const Errors = require('../../../../app/src/Features/Errors/Errors')
  12. describe('ProjectDeleter', function() {
  13. beforeEach(function() {
  14. tk.freeze(Date.now())
  15. this.ip = '192.170.18.1'
  16. this.project = dummyProject()
  17. this.user = {
  18. _id: '588f3ddae8ebc1bac07c9fa4',
  19. first_name: 'bjkdsjfk',
  20. features: {}
  21. }
  22. this.doc = {
  23. _id: '5bd975f54f62e803cb8a8fec',
  24. lines: ['a bunch of lines', 'for a sunny day', 'in London town'],
  25. ranges: {},
  26. project_id: '5cf9270b4eff6e186cf8b05e'
  27. }
  28. this.deletedProjects = [
  29. {
  30. _id: '5cf7f145c1401f0ca0eb1aaa',
  31. deleterData: {
  32. _id: '5cf7f145c1401f0ca0eb1aac',
  33. deletedAt: moment()
  34. .subtract(95, 'days')
  35. .toDate(),
  36. deleterId: '588f3ddae8ebc1bac07c9fa4',
  37. deleterIpAddress: '172.19.0.1',
  38. deletedProjectId: '5cf9270b4eff6e186cf8b05e'
  39. },
  40. project: {
  41. _id: '5cf9270b4eff6e186cf8b05e'
  42. }
  43. },
  44. {
  45. _id: '5cf8eb11c1401f0ca0eb1ad7',
  46. deleterData: {
  47. _id: '5b74360c0fbe57011ae9938f',
  48. deletedAt: moment()
  49. .subtract(95, 'days')
  50. .toDate(),
  51. deleterId: '588f3ddae8ebc1bac07c9fa4',
  52. deleterIpAddress: '172.20.0.1',
  53. deletedProjectId: '5cf8f95a0c87371362c23919'
  54. },
  55. project: {
  56. _id: '5cf8f95a0c87371362c23919'
  57. }
  58. }
  59. ]
  60. this.DocumentUpdaterHandler = {
  61. promises: {
  62. flushProjectToMongoAndDelete: sinon.stub().resolves()
  63. }
  64. }
  65. this.EditorRealTimeController = {
  66. emitToRoom: sinon.stub()
  67. }
  68. this.TagsHandler = {
  69. promises: {
  70. removeProjectFromAllTags: sinon.stub().resolves()
  71. }
  72. }
  73. this.CollaboratorsHandler = {
  74. promises: {
  75. removeUserFromAllProjects: sinon.stub().resolves()
  76. }
  77. }
  78. this.CollaboratorsGetter = {
  79. promises: {
  80. getMemberIds: sinon
  81. .stub()
  82. .withArgs(this.project._id)
  83. .resolves(['member-id-1', 'member-id-2'])
  84. }
  85. }
  86. this.logger = {
  87. err: sinon.stub(),
  88. log: sinon.stub(),
  89. warn: sinon.stub()
  90. }
  91. this.ProjectDetailsHandler = {
  92. promises: {
  93. generateUniqueName: sinon.stub().resolves(this.project.name)
  94. }
  95. }
  96. this.ProjectHelper = {
  97. calculateArchivedArray: sinon.stub()
  98. }
  99. this.db = {
  100. projects: {
  101. insert: sinon.stub().yields()
  102. }
  103. }
  104. this.DocstoreManager = {
  105. promises: {
  106. destroyProject: sinon.stub().resolves()
  107. }
  108. }
  109. this.ProjectMock = sinon.mock(Project)
  110. this.DeletedProjectMock = sinon.mock(DeletedProject)
  111. this.ProjectDeleter = SandboxedModule.require(modulePath, {
  112. requires: {
  113. '../Editor/EditorRealTimeController': this.EditorRealTimeController,
  114. '../../models/Project': { Project: Project },
  115. './ProjectHelper': this.ProjectHelper,
  116. '../../models/DeletedProject': { DeletedProject: DeletedProject },
  117. '../DocumentUpdater/DocumentUpdaterHandler': this
  118. .DocumentUpdaterHandler,
  119. '../Tags/TagsHandler': this.TagsHandler,
  120. '../FileStore/FileStoreHandler': (this.FileStoreHandler = {}),
  121. '../Collaborators/CollaboratorsHandler': this.CollaboratorsHandler,
  122. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  123. '../Docstore/DocstoreManager': this.DocstoreManager,
  124. './ProjectDetailsHandler': this.ProjectDetailsHandler,
  125. '../../infrastructure/mongojs': { db: this.db, ObjectId },
  126. 'logger-sharelatex': this.logger,
  127. '../Errors/Errors': Errors
  128. },
  129. globals: {
  130. console: console
  131. }
  132. })
  133. })
  134. afterEach(function() {
  135. tk.reset()
  136. this.DeletedProjectMock.restore()
  137. this.ProjectMock.restore()
  138. })
  139. describe('mark as deleted by external source', function() {
  140. beforeEach(function() {
  141. this.ProjectMock.expects('update')
  142. .withArgs(
  143. { _id: this.project._id },
  144. { deletedByExternalDataSource: true }
  145. )
  146. .chain('exec')
  147. .resolves()
  148. })
  149. it('should update the project with the flag set to true', async function() {
  150. await this.ProjectDeleter.promises.markAsDeletedByExternalSource(
  151. this.project._id
  152. )
  153. this.ProjectMock.verify()
  154. })
  155. it('should tell the editor controler so users are notified', async function() {
  156. await this.ProjectDeleter.promises.markAsDeletedByExternalSource(
  157. this.project._id
  158. )
  159. expect(this.EditorRealTimeController.emitToRoom).to.have.been.calledWith(
  160. this.project._id,
  161. 'projectRenamedOrDeletedByExternalSource'
  162. )
  163. })
  164. })
  165. describe('unmarkAsDeletedByExternalSource', function() {
  166. beforeEach(async function() {
  167. this.ProjectMock.expects('update')
  168. .withArgs(
  169. { _id: this.project._id },
  170. { deletedByExternalDataSource: false }
  171. )
  172. .chain('exec')
  173. .resolves()
  174. await this.ProjectDeleter.promises.unmarkAsDeletedByExternalSource(
  175. this.project._id
  176. )
  177. })
  178. it('should remove the flag from the project', function() {
  179. this.ProjectMock.verify()
  180. })
  181. })
  182. describe('deleteUsersProjects', function() {
  183. beforeEach(function() {
  184. this.projects = [dummyProject(), dummyProject()]
  185. this.ProjectMock.expects('find')
  186. .withArgs({ owner_ref: this.user._id })
  187. .chain('exec')
  188. .resolves(this.projects)
  189. for (const project of this.projects) {
  190. this.ProjectMock.expects('findOne')
  191. .withArgs({ _id: project._id })
  192. .chain('exec')
  193. .resolves(project)
  194. this.ProjectMock.expects('remove')
  195. .withArgs({ _id: project._id })
  196. .chain('exec')
  197. .resolves()
  198. this.DeletedProjectMock.expects('update')
  199. .withArgs(
  200. { 'deleterData.deletedProjectId': project._id },
  201. {
  202. project,
  203. deleterData: sinon.match.object
  204. },
  205. { upsert: true }
  206. )
  207. .resolves()
  208. }
  209. })
  210. it('should delete all projects owned by the user', async function() {
  211. await this.ProjectDeleter.promises.deleteUsersProjects(this.user._id)
  212. this.ProjectMock.verify()
  213. this.DeletedProjectMock.verify()
  214. })
  215. it('should remove any collaboration from this user', async function() {
  216. await this.ProjectDeleter.promises.deleteUsersProjects(this.user._id)
  217. sinon.assert.calledWith(
  218. this.CollaboratorsHandler.promises.removeUserFromAllProjects,
  219. this.user._id
  220. )
  221. sinon.assert.calledOnce(
  222. this.CollaboratorsHandler.promises.removeUserFromAllProjects
  223. )
  224. })
  225. })
  226. describe('deleteProject', function() {
  227. beforeEach(function() {
  228. this.deleterData = {
  229. deletedAt: new Date(),
  230. deletedProjectId: this.project._id,
  231. deletedProjectOwnerId: this.project.owner_ref,
  232. deletedProjectCollaboratorIds: this.project.collaberator_refs,
  233. deletedProjectReadOnlyIds: this.project.readOnly_refs,
  234. deletedProjectReadWriteTokenAccessIds: this.project
  235. .tokenAccessReadAndWrite_refs,
  236. deletedProjectReadOnlyTokenAccessIds: this.project
  237. .tokenAccessReadOnly_refs,
  238. deletedProjectReadWriteToken: this.project.tokens.readAndWrite,
  239. deletedProjectReadOnlyToken: this.project.tokens.readOnly,
  240. deletedProjectOverleafId: this.project.overleaf.id,
  241. deletedProjectOverleafHistoryId: this.project.overleaf.history.id,
  242. deletedProjectLastUpdatedAt: this.project.lastUpdated
  243. }
  244. this.ProjectMock.expects('findOne')
  245. .withArgs({ _id: this.project._id })
  246. .chain('exec')
  247. .resolves(this.project)
  248. })
  249. it('should save a DeletedProject with additional deleterData', async function() {
  250. this.deleterData.deleterIpAddress = this.ip
  251. this.deleterData.deleterId = this.user._id
  252. this.ProjectMock.expects('remove')
  253. .chain('exec')
  254. .resolves()
  255. this.DeletedProjectMock.expects('update')
  256. .withArgs(
  257. { 'deleterData.deletedProjectId': this.project._id },
  258. {
  259. project: this.project,
  260. deleterData: this.deleterData
  261. },
  262. { upsert: true }
  263. )
  264. .resolves()
  265. await this.ProjectDeleter.promises.deleteProject(this.project._id, {
  266. deleterUser: this.user,
  267. ipAddress: this.ip
  268. })
  269. this.DeletedProjectMock.verify()
  270. })
  271. it('should flushProjectToMongoAndDelete in doc updater', async function() {
  272. this.ProjectMock.expects('remove')
  273. .chain('exec')
  274. .resolves()
  275. this.DeletedProjectMock.expects('update').resolves()
  276. await this.ProjectDeleter.promises.deleteProject(this.project._id, {
  277. deleterUser: this.user,
  278. ipAddress: this.ip
  279. })
  280. this.DocumentUpdaterHandler.promises.flushProjectToMongoAndDelete
  281. .calledWith(this.project._id)
  282. .should.equal(true)
  283. })
  284. it('should removeProjectFromAllTags', async function() {
  285. this.ProjectMock.expects('remove')
  286. .chain('exec')
  287. .resolves()
  288. this.DeletedProjectMock.expects('update').resolves()
  289. await this.ProjectDeleter.promises.deleteProject(this.project._id)
  290. sinon.assert.calledWith(
  291. this.TagsHandler.promises.removeProjectFromAllTags,
  292. 'member-id-1',
  293. this.project._id
  294. )
  295. sinon.assert.calledWith(
  296. this.TagsHandler.promises.removeProjectFromAllTags,
  297. 'member-id-2',
  298. this.project._id
  299. )
  300. })
  301. it('should remove the project from Mongo', async function() {
  302. this.ProjectMock.expects('remove')
  303. .withArgs({ _id: this.project._id })
  304. .chain('exec')
  305. .resolves()
  306. this.DeletedProjectMock.expects('update').resolves()
  307. await this.ProjectDeleter.promises.deleteProject(this.project._id)
  308. this.ProjectMock.verify()
  309. })
  310. })
  311. describe('expireDeletedProjectsAfterDuration', function() {
  312. beforeEach(async function() {
  313. this.DeletedProjectMock.expects('find')
  314. .withArgs({
  315. 'deleterData.deletedAt': {
  316. $lt: new Date(moment().subtract(90, 'days'))
  317. },
  318. project: {
  319. $ne: null
  320. }
  321. })
  322. .chain('exec')
  323. .resolves(this.deletedProjects)
  324. for (const deletedProject of this.deletedProjects) {
  325. this.DeletedProjectMock.expects('findOne')
  326. .withArgs({
  327. 'deleterData.deletedProjectId': deletedProject.project._id
  328. })
  329. .chain('exec')
  330. .resolves(deletedProject)
  331. this.DeletedProjectMock.expects('update')
  332. .withArgs(
  333. {
  334. _id: deletedProject._id
  335. },
  336. {
  337. $set: {
  338. 'deleterData.deleterIpAddress': null,
  339. project: null
  340. }
  341. }
  342. )
  343. .chain('exec')
  344. .resolves()
  345. }
  346. await this.ProjectDeleter.promises.expireDeletedProjectsAfterDuration()
  347. })
  348. it('should expire projects older than 90 days', function() {
  349. this.DeletedProjectMock.verify()
  350. })
  351. })
  352. describe('expireDeletedProject', function() {
  353. beforeEach(async function() {
  354. this.DeletedProjectMock.expects('update')
  355. .withArgs(
  356. {
  357. _id: this.deletedProjects[0]._id
  358. },
  359. {
  360. $set: {
  361. 'deleterData.deleterIpAddress': null,
  362. project: null
  363. }
  364. }
  365. )
  366. .chain('exec')
  367. .resolves()
  368. this.DeletedProjectMock.expects('findOne')
  369. .withArgs({
  370. 'deleterData.deletedProjectId': this.deletedProjects[0].project._id
  371. })
  372. .chain('exec')
  373. .resolves(this.deletedProjects[0])
  374. await this.ProjectDeleter.promises.expireDeletedProject(
  375. this.deletedProjects[0].project._id
  376. )
  377. })
  378. it('should find the specified deletedProject and remove its project and ip address', function() {
  379. this.DeletedProjectMock.verify()
  380. })
  381. it('should destroy the docs in docstore', function() {
  382. expect(
  383. this.DocstoreManager.promises.destroyProject
  384. ).to.have.been.calledWith(this.deletedProjects[0].project._id)
  385. })
  386. })
  387. describe('archiveProject', function() {
  388. beforeEach(function() {
  389. let archived = [ObjectId(this.user._id)]
  390. this.ProjectHelper.calculateArchivedArray.returns(archived)
  391. this.ProjectMock.expects('findOne')
  392. .withArgs({ _id: this.project._id })
  393. .chain('exec')
  394. .resolves(this.project)
  395. this.ProjectMock.expects('update')
  396. .withArgs(
  397. { _id: this.project._id },
  398. {
  399. $set: { archived: archived },
  400. $pull: { trashed: ObjectId(this.user._id) }
  401. }
  402. )
  403. .resolves()
  404. })
  405. it('should update the project', async function() {
  406. await this.ProjectDeleter.promises.archiveProject(
  407. this.project._id,
  408. this.user._id
  409. )
  410. this.ProjectMock.verify()
  411. })
  412. })
  413. describe('unarchiveProject', function() {
  414. beforeEach(function() {
  415. let archived = [ObjectId(this.user._id)]
  416. this.ProjectHelper.calculateArchivedArray.returns(archived)
  417. this.ProjectMock.expects('findOne')
  418. .withArgs({ _id: this.project._id })
  419. .chain('exec')
  420. .resolves(this.project)
  421. this.ProjectMock.expects('update')
  422. .withArgs({ _id: this.project._id }, { $set: { archived: archived } })
  423. .resolves()
  424. })
  425. it('should update the project', async function() {
  426. await this.ProjectDeleter.promises.unarchiveProject(
  427. this.project._id,
  428. this.user._id
  429. )
  430. this.ProjectMock.verify()
  431. })
  432. })
  433. describe('trashProject', function() {
  434. beforeEach(function() {
  435. this.ProjectMock.expects('findOne')
  436. .withArgs({ _id: this.project._id })
  437. .chain('exec')
  438. .resolves(this.project)
  439. this.ProjectMock.expects('update')
  440. .withArgs(
  441. { _id: this.project._id },
  442. {
  443. $addToSet: { trashed: ObjectId(this.user._id) },
  444. $pull: { archived: ObjectId(this.user._id) }
  445. }
  446. )
  447. .resolves()
  448. })
  449. it('should update the project', async function() {
  450. await this.ProjectDeleter.promises.trashProject(
  451. this.project._id,
  452. this.user._id
  453. )
  454. this.ProjectMock.verify()
  455. })
  456. })
  457. describe('untrashProject', function() {
  458. beforeEach(function() {
  459. this.ProjectMock.expects('findOne')
  460. .withArgs({ _id: this.project._id })
  461. .chain('exec')
  462. .resolves(this.project)
  463. this.ProjectMock.expects('update')
  464. .withArgs(
  465. { _id: this.project._id },
  466. { $pull: { trashed: ObjectId(this.user._id) } }
  467. )
  468. .resolves()
  469. })
  470. it('should update the project', async function() {
  471. await this.ProjectDeleter.promises.untrashProject(
  472. this.project._id,
  473. this.user._id
  474. )
  475. this.ProjectMock.verify()
  476. })
  477. })
  478. describe('restoreProject', function() {
  479. beforeEach(function() {
  480. this.ProjectMock.expects('update')
  481. .withArgs(
  482. {
  483. _id: this.project._id
  484. },
  485. {
  486. $unset: { archived: true }
  487. }
  488. )
  489. .chain('exec')
  490. .resolves()
  491. })
  492. it('should unset the archive attribute', async function() {
  493. await this.ProjectDeleter.promises.restoreProject(this.project._id)
  494. })
  495. })
  496. describe('undeleteProject', function() {
  497. beforeEach(function() {
  498. this.deletedProject = {
  499. _id: 'deleted',
  500. project: this.project,
  501. deleterData: {
  502. deletedProjectId: this.project._id,
  503. deletedProjectOwnerId: this.project.owner_ref
  504. }
  505. }
  506. this.purgedProject = {
  507. _id: 'purged',
  508. deleterData: {
  509. deletedProjectId: 'purgedProject',
  510. deletedProjectOwnerId: 'potato'
  511. }
  512. }
  513. this.DeletedProjectMock.expects('findOne')
  514. .withArgs({ 'deleterData.deletedProjectId': this.project._id })
  515. .chain('exec')
  516. .resolves(this.deletedProject)
  517. this.DeletedProjectMock.expects('findOne')
  518. .withArgs({ 'deleterData.deletedProjectId': 'purgedProject' })
  519. .chain('exec')
  520. .resolves(this.purgedProject)
  521. this.DeletedProjectMock.expects('findOne')
  522. .withArgs({ 'deleterData.deletedProjectId': 'wombat' })
  523. .chain('exec')
  524. .resolves(null)
  525. this.DeletedProjectMock.expects('deleteOne')
  526. .chain('exec')
  527. .resolves()
  528. })
  529. it('should return not found if the project does not exist', async function() {
  530. await expect(
  531. this.ProjectDeleter.promises.undeleteProject('wombat')
  532. ).to.be.rejectedWith(Errors.NotFoundError, 'project_not_found')
  533. })
  534. it('should return not found if the project has been expired', async function() {
  535. await expect(
  536. this.ProjectDeleter.promises.undeleteProject('purgedProject')
  537. ).to.be.rejectedWith(Errors.NotFoundError, 'project_too_old_to_restore')
  538. })
  539. it('should insert the project into the collection', async function() {
  540. await this.ProjectDeleter.promises.undeleteProject(this.project._id)
  541. sinon.assert.calledWith(
  542. this.db.projects.insert,
  543. sinon.match({
  544. _id: this.project._id,
  545. name: this.project.name
  546. })
  547. )
  548. })
  549. it('should clear the archive bit', async function() {
  550. this.project.archived = true
  551. await this.ProjectDeleter.promises.undeleteProject(this.project._id)
  552. sinon.assert.calledWith(
  553. this.db.projects.insert,
  554. sinon.match({ archived: undefined })
  555. )
  556. })
  557. it('should generate a unique name for the project', async function() {
  558. await this.ProjectDeleter.promises.undeleteProject(this.project._id)
  559. sinon.assert.calledWith(
  560. this.ProjectDetailsHandler.promises.generateUniqueName,
  561. this.project.owner_ref
  562. )
  563. })
  564. it('should add a suffix to the project name', async function() {
  565. await this.ProjectDeleter.promises.undeleteProject(this.project._id)
  566. sinon.assert.calledWith(
  567. this.ProjectDetailsHandler.promises.generateUniqueName,
  568. this.project.owner_ref,
  569. this.project.name + ' (Restored)'
  570. )
  571. })
  572. it('should remove the DeletedProject', async function() {
  573. // need to change the mock just to include the methods we want
  574. this.DeletedProjectMock.restore()
  575. this.DeletedProjectMock = sinon.mock(DeletedProject)
  576. this.DeletedProjectMock.expects('findOne')
  577. .withArgs({ 'deleterData.deletedProjectId': this.project._id })
  578. .chain('exec')
  579. .resolves(this.deletedProject)
  580. this.DeletedProjectMock.expects('deleteOne')
  581. .withArgs({ _id: 'deleted' })
  582. .chain('exec')
  583. .resolves()
  584. await this.ProjectDeleter.promises.undeleteProject(this.project._id)
  585. this.DeletedProjectMock.verify()
  586. })
  587. })
  588. })
  589. function dummyProject() {
  590. return {
  591. _id: new ObjectId(),
  592. lastUpdated: new Date(),
  593. rootFolder: [],
  594. collaberator_refs: [new ObjectId(), new ObjectId()],
  595. readOnly_refs: [new ObjectId(), new ObjectId()],
  596. tokenAccessReadAndWrite_refs: [new ObjectId(), new ObjectId()],
  597. tokenAccessReadOnly_refs: [new ObjectId(), new ObjectId()],
  598. owner_ref: new ObjectId(),
  599. tokens: {
  600. readOnly: 'wombat',
  601. readAndWrite: 'potato'
  602. },
  603. overleaf: {
  604. id: 1234,
  605. history: {
  606. id: 5678
  607. }
  608. },
  609. name: 'a very scientific analysis of spooky ghosts'
  610. }
  611. }