ProjectDeleterTests.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. const modulePath = '../../../../app/src/Features/Project/ProjectDeleter'
  2. const SandboxedModule = require('sandboxed-module')
  3. const sinon = require('sinon')
  4. const { expect } = require('chai')
  5. const tk = require('timekeeper')
  6. const moment = require('moment')
  7. const { Project } = require('../helpers/models/Project')
  8. const { DeletedProject } = require('../helpers/models/DeletedProject')
  9. const { ObjectId, ReadPreference } = require('mongodb')
  10. const Errors = require('../../../../app/src/Features/Errors/Errors')
  11. describe('ProjectDeleter', function () {
  12. beforeEach(function () {
  13. tk.freeze(Date.now())
  14. this.ip = '192.170.18.1'
  15. this.project = dummyProject()
  16. this.user = {
  17. _id: '588f3ddae8ebc1bac07c9fa4',
  18. first_name: 'bjkdsjfk',
  19. features: {},
  20. }
  21. this.doc = {
  22. _id: '5bd975f54f62e803cb8a8fec',
  23. lines: ['a bunch of lines', 'for a sunny day', 'in London town'],
  24. ranges: {},
  25. project_id: '5cf9270b4eff6e186cf8b05e',
  26. }
  27. this.deletedProjects = [
  28. {
  29. _id: '5cf7f145c1401f0ca0eb1aaa',
  30. deleterData: {
  31. _id: '5cf7f145c1401f0ca0eb1aac',
  32. deletedAt: moment().subtract(95, 'days').toDate(),
  33. deleterId: '588f3ddae8ebc1bac07c9fa4',
  34. deleterIpAddress: '172.19.0.1',
  35. deletedProjectId: '5cf9270b4eff6e186cf8b05e',
  36. },
  37. project: {
  38. _id: '5cf9270b4eff6e186cf8b05e',
  39. overleaf: {
  40. history: {
  41. id: new ObjectId(),
  42. },
  43. },
  44. },
  45. },
  46. {
  47. _id: '5cf8eb11c1401f0ca0eb1ad7',
  48. deleterData: {
  49. _id: '5b74360c0fbe57011ae9938f',
  50. deletedAt: moment().subtract(95, 'days').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.ProjectDetailsHandler = {
  87. promises: {
  88. generateUniqueName: sinon.stub().resolves(this.project.name),
  89. },
  90. }
  91. this.ProjectHelper = {
  92. calculateArchivedArray: sinon.stub(),
  93. }
  94. this.db = {
  95. deletedFiles: {
  96. indexExists: sinon.stub().resolves(false),
  97. deleteMany: sinon.stub(),
  98. },
  99. projects: {
  100. insertOne: sinon.stub().resolves(),
  101. },
  102. }
  103. this.DocstoreManager = {
  104. promises: {
  105. archiveProject: sinon.stub().resolves(),
  106. destroyProject: sinon.stub().resolves(),
  107. },
  108. }
  109. this.HistoryManager = {
  110. promises: {
  111. deleteProject: sinon.stub().resolves(),
  112. },
  113. }
  114. this.ProjectMock = sinon.mock(Project)
  115. this.DeletedProjectMock = sinon.mock(DeletedProject)
  116. this.FileStoreHandler = {
  117. promises: {
  118. deleteProject: sinon.stub().resolves(),
  119. },
  120. }
  121. this.Features = {
  122. hasFeature: sinon.stub().returns(true),
  123. }
  124. this.ChatApiHandler = {
  125. promises: {
  126. destroyProject: sinon.stub().resolves(),
  127. },
  128. }
  129. this.ProjectAuditLogEntry = {
  130. deleteMany: sinon.stub().returns({ exec: sinon.stub().resolves() }),
  131. }
  132. this.ProjectDeleter = SandboxedModule.require(modulePath, {
  133. requires: {
  134. '../../infrastructure/Features': this.Features,
  135. '../Editor/EditorRealTimeController': this.EditorRealTimeController,
  136. '../../models/Project': { Project },
  137. './ProjectHelper': this.ProjectHelper,
  138. '../../models/DeletedProject': { DeletedProject },
  139. '../DocumentUpdater/DocumentUpdaterHandler':
  140. this.DocumentUpdaterHandler,
  141. '../Tags/TagsHandler': this.TagsHandler,
  142. '../FileStore/FileStoreHandler': this.FileStoreHandler,
  143. '../Chat/ChatApiHandler': this.ChatApiHandler,
  144. '../Collaborators/CollaboratorsHandler': this.CollaboratorsHandler,
  145. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  146. '../Docstore/DocstoreManager': this.DocstoreManager,
  147. './ProjectDetailsHandler': this.ProjectDetailsHandler,
  148. '../../infrastructure/mongodb': {
  149. db: this.db,
  150. ObjectId,
  151. READ_PREFERENCE_SECONDARY: ReadPreference.secondaryPreferred.mode,
  152. },
  153. '../History/HistoryManager': this.HistoryManager,
  154. '../../models/ProjectAuditLogEntry': {
  155. ProjectAuditLogEntry: this.ProjectAuditLogEntry,
  156. },
  157. },
  158. })
  159. })
  160. afterEach(function () {
  161. tk.reset()
  162. this.DeletedProjectMock.restore()
  163. this.ProjectMock.restore()
  164. })
  165. describe('mark as deleted by external source', function () {
  166. beforeEach(function () {
  167. this.ProjectMock.expects('updateOne')
  168. .withArgs(
  169. { _id: this.project._id },
  170. { deletedByExternalDataSource: true }
  171. )
  172. .chain('exec')
  173. .resolves()
  174. })
  175. it('should update the project with the flag set to true', async function () {
  176. await this.ProjectDeleter.promises.markAsDeletedByExternalSource(
  177. this.project._id
  178. )
  179. this.ProjectMock.verify()
  180. })
  181. it('should tell the editor controler so users are notified', async function () {
  182. await this.ProjectDeleter.promises.markAsDeletedByExternalSource(
  183. this.project._id
  184. )
  185. expect(this.EditorRealTimeController.emitToRoom).to.have.been.calledWith(
  186. this.project._id,
  187. 'projectRenamedOrDeletedByExternalSource'
  188. )
  189. })
  190. })
  191. describe('unmarkAsDeletedByExternalSource', function () {
  192. beforeEach(async function () {
  193. this.ProjectMock.expects('updateOne')
  194. .withArgs(
  195. { _id: this.project._id },
  196. { deletedByExternalDataSource: false }
  197. )
  198. .chain('exec')
  199. .resolves()
  200. await this.ProjectDeleter.promises.unmarkAsDeletedByExternalSource(
  201. this.project._id
  202. )
  203. })
  204. it('should remove the flag from the project', function () {
  205. this.ProjectMock.verify()
  206. })
  207. })
  208. describe('deleteUsersProjects', function () {
  209. beforeEach(function () {
  210. this.projects = [dummyProject(), dummyProject()]
  211. this.ProjectMock.expects('find')
  212. .withArgs({ owner_ref: this.user._id })
  213. .chain('exec')
  214. .resolves(this.projects)
  215. for (const project of this.projects) {
  216. this.ProjectMock.expects('findOne')
  217. .withArgs({ _id: project._id })
  218. .chain('exec')
  219. .resolves(project)
  220. this.ProjectMock.expects('deleteOne')
  221. .withArgs({ _id: project._id })
  222. .chain('exec')
  223. .resolves()
  224. this.DeletedProjectMock.expects('updateOne')
  225. .withArgs(
  226. { 'deleterData.deletedProjectId': project._id },
  227. {
  228. project,
  229. deleterData: sinon.match.object,
  230. },
  231. { upsert: true }
  232. )
  233. .resolves()
  234. }
  235. })
  236. it('should delete all projects owned by the user', async function () {
  237. await this.ProjectDeleter.promises.deleteUsersProjects(this.user._id)
  238. this.ProjectMock.verify()
  239. this.DeletedProjectMock.verify()
  240. })
  241. it('should remove any collaboration from this user', async function () {
  242. await this.ProjectDeleter.promises.deleteUsersProjects(this.user._id)
  243. sinon.assert.calledWith(
  244. this.CollaboratorsHandler.promises.removeUserFromAllProjects,
  245. this.user._id
  246. )
  247. sinon.assert.calledOnce(
  248. this.CollaboratorsHandler.promises.removeUserFromAllProjects
  249. )
  250. })
  251. })
  252. describe('deleteProject', function () {
  253. beforeEach(function () {
  254. this.deleterData = {
  255. deletedAt: new Date(),
  256. deletedProjectId: this.project._id,
  257. deletedProjectOwnerId: this.project.owner_ref,
  258. deletedProjectCollaboratorIds: this.project.collaberator_refs,
  259. deletedProjectReadOnlyIds: this.project.readOnly_refs,
  260. deletedProjectReadWriteTokenAccessIds:
  261. this.project.tokenAccessReadAndWrite_refs,
  262. deletedProjectReadOnlyTokenAccessIds:
  263. this.project.tokenAccessReadOnly_refs,
  264. deletedProjectReadWriteToken: this.project.tokens.readAndWrite,
  265. deletedProjectReadOnlyToken: this.project.tokens.readOnly,
  266. deletedProjectOverleafId: this.project.overleaf.id,
  267. deletedProjectOverleafHistoryId: this.project.overleaf.history.id,
  268. deletedProjectLastUpdatedAt: this.project.lastUpdated,
  269. }
  270. this.ProjectMock.expects('findOne')
  271. .withArgs({ _id: this.project._id })
  272. .chain('exec')
  273. .resolves(this.project)
  274. })
  275. it('should save a DeletedProject with additional deleterData', async function () {
  276. this.deleterData.deleterIpAddress = this.ip
  277. this.deleterData.deleterId = this.user._id
  278. this.ProjectMock.expects('deleteOne').chain('exec').resolves()
  279. this.DeletedProjectMock.expects('updateOne')
  280. .withArgs(
  281. { 'deleterData.deletedProjectId': this.project._id },
  282. {
  283. project: this.project,
  284. deleterData: this.deleterData,
  285. },
  286. { upsert: true }
  287. )
  288. .resolves()
  289. await this.ProjectDeleter.promises.deleteProject(this.project._id, {
  290. deleterUser: this.user,
  291. ipAddress: this.ip,
  292. })
  293. this.DeletedProjectMock.verify()
  294. })
  295. it('should flushProjectToMongoAndDelete in doc updater', async function () {
  296. this.ProjectMock.expects('deleteOne').chain('exec').resolves()
  297. this.DeletedProjectMock.expects('updateOne').resolves()
  298. await this.ProjectDeleter.promises.deleteProject(this.project._id, {
  299. deleterUser: this.user,
  300. ipAddress: this.ip,
  301. })
  302. this.DocumentUpdaterHandler.promises.flushProjectToMongoAndDelete
  303. .calledWith(this.project._id)
  304. .should.equal(true)
  305. })
  306. it('should flush docs out of mongo', async function () {
  307. this.ProjectMock.expects('deleteOne').chain('exec').resolves()
  308. this.DeletedProjectMock.expects('updateOne').resolves()
  309. await this.ProjectDeleter.promises.deleteProject(this.project._id, {
  310. deleterUser: this.user,
  311. ipAddress: this.ip,
  312. })
  313. expect(
  314. this.DocstoreManager.promises.archiveProject
  315. ).to.have.been.calledWith(this.project._id)
  316. })
  317. it('should flush docs out of mongo and ignore errors', async function () {
  318. this.ProjectMock.expects('deleteOne').chain('exec').resolves()
  319. this.DeletedProjectMock.expects('updateOne').resolves()
  320. this.DocstoreManager.promises.archiveProject.rejects(new Error('foo'))
  321. await this.ProjectDeleter.promises.deleteProject(this.project._id, {
  322. deleterUser: this.user,
  323. ipAddress: this.ip,
  324. })
  325. })
  326. it('should removeProjectFromAllTags', async function () {
  327. this.ProjectMock.expects('deleteOne').chain('exec').resolves()
  328. this.DeletedProjectMock.expects('updateOne').resolves()
  329. await this.ProjectDeleter.promises.deleteProject(this.project._id)
  330. sinon.assert.calledWith(
  331. this.TagsHandler.promises.removeProjectFromAllTags,
  332. 'member-id-1',
  333. this.project._id
  334. )
  335. sinon.assert.calledWith(
  336. this.TagsHandler.promises.removeProjectFromAllTags,
  337. 'member-id-2',
  338. this.project._id
  339. )
  340. })
  341. it('should remove the project from Mongo', async function () {
  342. this.ProjectMock.expects('deleteOne')
  343. .withArgs({ _id: this.project._id })
  344. .chain('exec')
  345. .resolves()
  346. this.DeletedProjectMock.expects('updateOne').resolves()
  347. await this.ProjectDeleter.promises.deleteProject(this.project._id)
  348. this.ProjectMock.verify()
  349. })
  350. })
  351. describe('expireDeletedProjectsAfterDuration', function () {
  352. beforeEach(async function () {
  353. for (const deletedProject of this.deletedProjects) {
  354. this.ProjectMock.expects('findById')
  355. .withArgs(deletedProject.deleterData.deletedProjectId)
  356. .chain('exec')
  357. .resolves(null)
  358. }
  359. this.DeletedProjectMock.expects('find')
  360. .withArgs({
  361. 'deleterData.deletedAt': {
  362. $lt: new Date(moment().subtract(90, 'days')),
  363. },
  364. project: {
  365. $ne: null,
  366. },
  367. })
  368. .chain('exec')
  369. .resolves(this.deletedProjects)
  370. for (const deletedProject of this.deletedProjects) {
  371. this.DeletedProjectMock.expects('findOne')
  372. .withArgs({
  373. 'deleterData.deletedProjectId': deletedProject.project._id,
  374. })
  375. .chain('exec')
  376. .resolves(deletedProject)
  377. this.DeletedProjectMock.expects('updateOne')
  378. .withArgs(
  379. {
  380. _id: deletedProject._id,
  381. },
  382. {
  383. $set: {
  384. 'deleterData.deleterIpAddress': null,
  385. project: null,
  386. },
  387. }
  388. )
  389. .chain('exec')
  390. .resolves()
  391. }
  392. await this.ProjectDeleter.promises.expireDeletedProjectsAfterDuration()
  393. })
  394. it('should expire projects older than 90 days', function () {
  395. this.DeletedProjectMock.verify()
  396. })
  397. })
  398. describe('expireDeletedProject', function () {
  399. describe('on an inactive project', function () {
  400. beforeEach(async function () {
  401. this.ProjectMock.expects('findById')
  402. .withArgs(this.deletedProjects[0].deleterData.deletedProjectId)
  403. .chain('exec')
  404. .resolves(null)
  405. this.DeletedProjectMock.expects('updateOne')
  406. .withArgs(
  407. {
  408. _id: this.deletedProjects[0]._id,
  409. },
  410. {
  411. $set: {
  412. 'deleterData.deleterIpAddress': null,
  413. project: null,
  414. },
  415. }
  416. )
  417. .chain('exec')
  418. .resolves()
  419. this.DeletedProjectMock.expects('findOne')
  420. .withArgs({
  421. 'deleterData.deletedProjectId': this.deletedProjects[0].project._id,
  422. })
  423. .chain('exec')
  424. .resolves(this.deletedProjects[0])
  425. await this.ProjectDeleter.promises.expireDeletedProject(
  426. this.deletedProjects[0].project._id
  427. )
  428. })
  429. it('should find the specified deletedProject and remove its project and ip address', function () {
  430. this.DeletedProjectMock.verify()
  431. })
  432. it('should destroy the docs in docstore', function () {
  433. expect(
  434. this.DocstoreManager.promises.destroyProject
  435. ).to.have.been.calledWith(this.deletedProjects[0].project._id)
  436. })
  437. it('should delete the project in history', function () {
  438. expect(
  439. this.HistoryManager.promises.deleteProject
  440. ).to.have.been.calledWith(
  441. this.deletedProjects[0].project._id,
  442. this.deletedProjects[0].project.overleaf.history.id
  443. )
  444. })
  445. it('should destroy the files in filestore', function () {
  446. expect(
  447. this.FileStoreHandler.promises.deleteProject
  448. ).to.have.been.calledWith(this.deletedProjects[0].project._id)
  449. })
  450. it('should destroy the chat threads and messages', function () {
  451. expect(
  452. this.ChatApiHandler.promises.destroyProject
  453. ).to.have.been.calledWith(this.deletedProjects[0].project._id)
  454. })
  455. it('should delete audit logs', async function () {
  456. expect(this.ProjectAuditLogEntry.deleteMany).to.have.been.calledWith({
  457. projectId: this.deletedProjects[0].project._id,
  458. })
  459. })
  460. })
  461. describe('on an active project (from an incomplete delete)', function () {
  462. beforeEach(async function () {
  463. this.ProjectMock.expects('findById')
  464. .withArgs(this.deletedProjects[0].deleterData.deletedProjectId)
  465. .chain('exec')
  466. .resolves(this.deletedProjects[0].project)
  467. this.DeletedProjectMock.expects('deleteOne')
  468. .withArgs({
  469. 'deleterData.deletedProjectId': this.deletedProjects[0].project._id,
  470. })
  471. .chain('exec')
  472. .resolves()
  473. await this.ProjectDeleter.promises.expireDeletedProject(
  474. this.deletedProjects[0].project._id
  475. )
  476. })
  477. it('should delete the spurious deleted project record', function () {
  478. this.DeletedProjectMock.verify()
  479. })
  480. it('should not destroy the docs in docstore', function () {
  481. expect(this.DocstoreManager.promises.destroyProject).to.not.have.been
  482. .called
  483. })
  484. it('should not delete the project in history', function () {
  485. expect(this.HistoryManager.promises.deleteProject).to.not.have.been
  486. .called
  487. })
  488. it('should not destroy the files in filestore', function () {
  489. expect(this.FileStoreHandler.promises.deleteProject).to.not.have.been
  490. .called
  491. })
  492. it('should not destroy the chat threads and messages', function () {
  493. expect(this.ChatApiHandler.promises.destroyProject).to.not.have.been
  494. .called
  495. })
  496. })
  497. })
  498. describe('archiveProject', function () {
  499. beforeEach(function () {
  500. const archived = [new ObjectId(this.user._id)]
  501. this.ProjectHelper.calculateArchivedArray.returns(archived)
  502. this.ProjectMock.expects('findOne')
  503. .withArgs({ _id: this.project._id })
  504. .chain('exec')
  505. .resolves(this.project)
  506. this.ProjectMock.expects('updateOne')
  507. .withArgs(
  508. { _id: this.project._id },
  509. {
  510. $set: { archived },
  511. $pull: { trashed: new ObjectId(this.user._id) },
  512. }
  513. )
  514. .resolves()
  515. })
  516. it('should update the project', async function () {
  517. await this.ProjectDeleter.promises.archiveProject(
  518. this.project._id,
  519. this.user._id
  520. )
  521. this.ProjectMock.verify()
  522. })
  523. it('calculates the archived array', async function () {
  524. await this.ProjectDeleter.promises.archiveProject(
  525. this.project._id,
  526. this.user._id
  527. )
  528. expect(this.ProjectHelper.calculateArchivedArray).to.have.been.calledWith(
  529. this.project,
  530. this.user._id,
  531. 'ARCHIVE'
  532. )
  533. })
  534. })
  535. describe('unarchiveProject', function () {
  536. beforeEach(function () {
  537. const archived = [new ObjectId(this.user._id)]
  538. this.ProjectHelper.calculateArchivedArray.returns(archived)
  539. this.ProjectMock.expects('findOne')
  540. .withArgs({ _id: this.project._id })
  541. .chain('exec')
  542. .resolves(this.project)
  543. this.ProjectMock.expects('updateOne')
  544. .withArgs({ _id: this.project._id }, { $set: { archived } })
  545. .resolves()
  546. })
  547. it('should update the project', async function () {
  548. await this.ProjectDeleter.promises.unarchiveProject(
  549. this.project._id,
  550. this.user._id
  551. )
  552. this.ProjectMock.verify()
  553. })
  554. it('calculates the archived array', async function () {
  555. await this.ProjectDeleter.promises.unarchiveProject(
  556. this.project._id,
  557. this.user._id
  558. )
  559. expect(this.ProjectHelper.calculateArchivedArray).to.have.been.calledWith(
  560. this.project,
  561. this.user._id,
  562. 'UNARCHIVE'
  563. )
  564. })
  565. })
  566. describe('trashProject', function () {
  567. beforeEach(function () {
  568. const archived = [new ObjectId(this.user._id)]
  569. this.ProjectHelper.calculateArchivedArray.returns(archived)
  570. this.ProjectMock.expects('findOne')
  571. .withArgs({ _id: this.project._id })
  572. .chain('exec')
  573. .resolves(this.project)
  574. this.ProjectMock.expects('updateOne')
  575. .withArgs(
  576. { _id: this.project._id },
  577. {
  578. $addToSet: { trashed: new ObjectId(this.user._id) },
  579. $set: { archived },
  580. }
  581. )
  582. .resolves()
  583. })
  584. it('should update the project', async function () {
  585. await this.ProjectDeleter.promises.trashProject(
  586. this.project._id,
  587. this.user._id
  588. )
  589. this.ProjectMock.verify()
  590. })
  591. it('unarchives the project', async function () {
  592. await this.ProjectDeleter.promises.trashProject(
  593. this.project._id,
  594. this.user._id
  595. )
  596. expect(this.ProjectHelper.calculateArchivedArray).to.have.been.calledWith(
  597. this.project,
  598. this.user._id,
  599. 'UNARCHIVE'
  600. )
  601. })
  602. })
  603. describe('untrashProject', function () {
  604. beforeEach(function () {
  605. this.ProjectMock.expects('findOne')
  606. .withArgs({ _id: this.project._id })
  607. .chain('exec')
  608. .resolves(this.project)
  609. this.ProjectMock.expects('updateOne')
  610. .withArgs(
  611. { _id: this.project._id },
  612. { $pull: { trashed: new ObjectId(this.user._id) } }
  613. )
  614. .resolves()
  615. })
  616. it('should update the project', async function () {
  617. await this.ProjectDeleter.promises.untrashProject(
  618. this.project._id,
  619. this.user._id
  620. )
  621. this.ProjectMock.verify()
  622. })
  623. })
  624. describe('restoreProject', function () {
  625. beforeEach(function () {
  626. this.ProjectMock.expects('updateOne')
  627. .withArgs(
  628. {
  629. _id: this.project._id,
  630. },
  631. {
  632. $unset: { archived: true },
  633. }
  634. )
  635. .chain('exec')
  636. .resolves()
  637. })
  638. it('should unset the archive attribute', async function () {
  639. await this.ProjectDeleter.promises.restoreProject(this.project._id)
  640. })
  641. })
  642. describe('undeleteProject', function () {
  643. beforeEach(function () {
  644. this.unknownProjectId = new ObjectId()
  645. this.purgedProjectId = new ObjectId()
  646. this.deletedProject = {
  647. _id: 'deleted',
  648. project: this.project,
  649. deleterData: {
  650. deletedProjectId: this.project._id,
  651. deletedProjectOwnerId: this.project.owner_ref,
  652. },
  653. }
  654. this.purgedProject = {
  655. _id: 'purged',
  656. deleterData: {
  657. deletedProjectId: this.purgedProjectId,
  658. deletedProjectOwnerId: 'potato',
  659. },
  660. }
  661. this.DeletedProjectMock.expects('findOne')
  662. .withArgs({ 'deleterData.deletedProjectId': this.project._id })
  663. .chain('exec')
  664. .resolves(this.deletedProject)
  665. this.DeletedProjectMock.expects('findOne')
  666. .withArgs({ 'deleterData.deletedProjectId': this.purgedProjectId })
  667. .chain('exec')
  668. .resolves(this.purgedProject)
  669. this.DeletedProjectMock.expects('findOne')
  670. .withArgs({ 'deleterData.deletedProjectId': this.unknownProjectId })
  671. .chain('exec')
  672. .resolves(null)
  673. this.DeletedProjectMock.expects('deleteOne').chain('exec').resolves()
  674. })
  675. it('should return not found if the project does not exist', async function () {
  676. await expect(
  677. this.ProjectDeleter.promises.undeleteProject(
  678. this.unknownProjectId.toString()
  679. )
  680. ).to.be.rejectedWith(Errors.NotFoundError, 'project_not_found')
  681. })
  682. it('should return not found if the project has been expired', async function () {
  683. await expect(
  684. this.ProjectDeleter.promises.undeleteProject(
  685. this.purgedProjectId.toString()
  686. )
  687. ).to.be.rejectedWith(Errors.NotFoundError, 'project_too_old_to_restore')
  688. })
  689. it('should insert the project into the collection', async function () {
  690. await this.ProjectDeleter.promises.undeleteProject(this.project._id)
  691. sinon.assert.calledWith(
  692. this.db.projects.insertOne,
  693. sinon.match({
  694. _id: this.project._id,
  695. name: this.project.name,
  696. })
  697. )
  698. })
  699. it('should clear the archive bit', async function () {
  700. this.project.archived = true
  701. await this.ProjectDeleter.promises.undeleteProject(this.project._id)
  702. sinon.assert.calledWith(
  703. this.db.projects.insertOne,
  704. sinon.match({ archived: undefined })
  705. )
  706. })
  707. it('should generate a unique name for the project', async function () {
  708. await this.ProjectDeleter.promises.undeleteProject(this.project._id)
  709. sinon.assert.calledWith(
  710. this.ProjectDetailsHandler.promises.generateUniqueName,
  711. this.project.owner_ref
  712. )
  713. })
  714. it('should add a suffix to the project name', async function () {
  715. await this.ProjectDeleter.promises.undeleteProject(this.project._id)
  716. sinon.assert.calledWith(
  717. this.ProjectDetailsHandler.promises.generateUniqueName,
  718. this.project.owner_ref,
  719. this.project.name + ' (Restored)'
  720. )
  721. })
  722. it('should remove the DeletedProject', async function () {
  723. // need to change the mock just to include the methods we want
  724. this.DeletedProjectMock.restore()
  725. this.DeletedProjectMock = sinon.mock(DeletedProject)
  726. this.DeletedProjectMock.expects('findOne')
  727. .withArgs({ 'deleterData.deletedProjectId': this.project._id })
  728. .chain('exec')
  729. .resolves(this.deletedProject)
  730. this.DeletedProjectMock.expects('deleteOne')
  731. .withArgs({ _id: 'deleted' })
  732. .chain('exec')
  733. .resolves()
  734. await this.ProjectDeleter.promises.undeleteProject(this.project._id)
  735. this.DeletedProjectMock.verify()
  736. })
  737. })
  738. })
  739. function dummyProject() {
  740. return {
  741. _id: new ObjectId(),
  742. lastUpdated: new Date(),
  743. rootFolder: [],
  744. collaberator_refs: [new ObjectId(), new ObjectId()],
  745. readOnly_refs: [new ObjectId(), new ObjectId()],
  746. tokenAccessReadAndWrite_refs: [new ObjectId(), new ObjectId()],
  747. tokenAccessReadOnly_refs: [new ObjectId(), new ObjectId()],
  748. owner_ref: new ObjectId(),
  749. tokens: {
  750. readOnly: 'wombat',
  751. readAndWrite: 'potato',
  752. },
  753. overleaf: {
  754. id: 1234,
  755. history: {
  756. id: 5678,
  757. },
  758. },
  759. name: 'a very scientific analysis of spooky ghosts',
  760. }
  761. }