ProjectDeleterTests.js 24 KB

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