ProjectDeleterTests.js 23 KB

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