ProjectDeleterTests.js 24 KB

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