ProjectDeleterTests.js 26 KB

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