UserDeleter.test.mjs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. import { beforeEach, describe, expect, vi, it } from 'vitest'
  2. import sinon from 'sinon'
  3. import tk from 'timekeeper'
  4. import moment from 'moment'
  5. import Errors from '../../../../app/src/Features/Errors/Errors.js'
  6. import mongoose from 'mongoose'
  7. import { DeletedUser } from '../../../../app/src/models/DeletedUser.mjs'
  8. import { User } from '../../../../app/src/models/User.mjs'
  9. const ObjectId = mongoose.Types.ObjectId
  10. vi.mock('../../../../app/src/Features/Errors/Errors.js', () =>
  11. vi.importActual('../../../../app/src/Features/Errors/Errors.js')
  12. )
  13. const modulePath = '../../../../app/src/Features/User/UserDeleter.mjs'
  14. describe('UserDeleter', function () {
  15. beforeEach(async function (ctx) {
  16. ctx.userId = new ObjectId()
  17. ctx.ipAddress = '1.2.3.4'
  18. ctx.UserMock = sinon.mock(User)
  19. ctx.DeletedUserMock = sinon.mock(DeletedUser)
  20. tk.freeze(Date.now())
  21. ctx.mockedUser = sinon.mock(
  22. new User({
  23. _id: ctx.userId,
  24. email: 'bob@bob.com',
  25. lastLoggedIn: Date.now() + 1000,
  26. signUpDate: Date.now() + 2000,
  27. loginCount: 10,
  28. overleaf: {
  29. id: 1234,
  30. },
  31. refered_users: ['wombat', 'potato'],
  32. refered_user_count: 2,
  33. referal_id: ['giraffe'],
  34. })
  35. )
  36. ctx.user = ctx.mockedUser.object
  37. ctx.NewsletterManager = {
  38. promises: {
  39. unsubscribe: sinon.stub().resolves(),
  40. },
  41. }
  42. ctx.ProjectDeleter = {
  43. promises: {
  44. deleteUsersProjects: sinon.stub().resolves(),
  45. },
  46. }
  47. ctx.SubscriptionHandler = {
  48. promises: {
  49. cancelSubscription: sinon.stub().resolves(),
  50. },
  51. }
  52. ctx.SubscriptionUpdater = {
  53. promises: {
  54. removeUserFromAllGroups: sinon.stub().resolves(),
  55. },
  56. }
  57. ctx.SubscriptionLocator = {
  58. promises: {
  59. getUsersSubscription: sinon.stub().resolves(),
  60. getUniqueManagedSubscriptionMemberOf: sinon.stub().resolves(),
  61. },
  62. }
  63. ctx.UserMembershipsHandler = {
  64. promises: {
  65. removeUserFromAllEntities: sinon.stub().resolves(),
  66. },
  67. }
  68. ctx.UserSessionsManager = {
  69. promises: {
  70. removeSessionsFromRedis: sinon.stub().resolves(),
  71. },
  72. }
  73. ctx.InstitutionsApi = {
  74. promises: {
  75. deleteAffiliations: sinon.stub().resolves(),
  76. },
  77. }
  78. ctx.UserAuditLogEntry = {
  79. deleteMany: sinon.stub().returns({ exec: sinon.stub().resolves() }),
  80. }
  81. ctx.Modules = {
  82. promises: { hooks: { fire: sinon.stub().resolves() } },
  83. }
  84. ctx.OnboardingDataCollectionManager = {
  85. deleteOnboardingDataCollection: sinon.stub().resolves(),
  86. }
  87. ctx.EmailHandler = {
  88. promises: {
  89. sendEmail: sinon.stub().resolves(),
  90. },
  91. }
  92. ctx.UserAuditLogHandler = {
  93. promises: {
  94. addEntry: sinon.stub().resolves(),
  95. },
  96. }
  97. vi.doMock('../../../../app/src/models/User', () => ({
  98. User,
  99. }))
  100. vi.doMock('../../../../app/src/models/DeletedUser', () => ({
  101. DeletedUser,
  102. }))
  103. vi.doMock(
  104. '../../../../app/src/Features/Newsletter/NewsletterManager',
  105. () => ({
  106. default: ctx.NewsletterManager,
  107. })
  108. )
  109. vi.doMock('../../../../app/src/Features/User/UserSessionsManager', () => ({
  110. default: ctx.UserSessionsManager,
  111. }))
  112. vi.doMock(
  113. '../../../../app/src/Features/Subscription/SubscriptionHandler',
  114. () => ({
  115. default: ctx.SubscriptionHandler,
  116. })
  117. )
  118. vi.doMock(
  119. '../../../../app/src/Features/Subscription/SubscriptionUpdater',
  120. () => ({
  121. default: ctx.SubscriptionUpdater,
  122. })
  123. )
  124. vi.doMock(
  125. '../../../../app/src/Features/Subscription/SubscriptionLocator',
  126. () => ({
  127. default: ctx.SubscriptionLocator,
  128. })
  129. )
  130. vi.doMock(
  131. '../../../../app/src/Features/UserMembership/UserMembershipsHandler',
  132. () => ({
  133. default: ctx.UserMembershipsHandler,
  134. })
  135. )
  136. vi.doMock('../../../../app/src/Features/Project/ProjectDeleter', () => ({
  137. default: ctx.ProjectDeleter,
  138. }))
  139. vi.doMock(
  140. '../../../../app/src/Features/Institutions/InstitutionsAPI',
  141. () => ({
  142. default: ctx.InstitutionsApi,
  143. })
  144. )
  145. vi.doMock('../../../../app/src/models/UserAuditLogEntry', () => ({
  146. UserAuditLogEntry: ctx.UserAuditLogEntry,
  147. }))
  148. vi.doMock('../../../../app/src/Features/User/UserAuditLogHandler', () => ({
  149. default: ctx.UserAuditLogHandler,
  150. }))
  151. vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
  152. default: ctx.Modules,
  153. }))
  154. vi.doMock(
  155. '../../../../app/src/Features/OnboardingDataCollection/OnboardingDataCollectionManager',
  156. () => ({
  157. default: ctx.OnboardingDataCollectionManager,
  158. })
  159. )
  160. vi.doMock('../../../../app/src/Features/Email/EmailHandler', () => ({
  161. default: ctx.EmailHandler,
  162. }))
  163. ctx.UserDeleter = (await import(modulePath)).default
  164. })
  165. afterEach(function (ctx) {
  166. ctx.DeletedUserMock.restore()
  167. ctx.UserMock.restore()
  168. ctx.mockedUser.restore()
  169. tk.reset()
  170. })
  171. describe('deleteUser', function () {
  172. beforeEach(function (ctx) {
  173. ctx.UserMock.expects('findById')
  174. .withArgs(ctx.userId)
  175. .chain('exec')
  176. .resolves(ctx.user)
  177. })
  178. describe('when the user can be deleted', function () {
  179. beforeEach(function (ctx) {
  180. ctx.deletedUser = {
  181. user: ctx.user,
  182. deleterData: {
  183. deletedAt: new Date(),
  184. deletedUserId: ctx.userId,
  185. deleterIpAddress: ctx.ipAddress,
  186. deleterId: undefined,
  187. deletedUserLastLoggedIn: ctx.user.lastLoggedIn,
  188. deletedUserSignUpDate: ctx.user.signUpDate,
  189. deletedUserLoginCount: ctx.user.loginCount,
  190. deletedUserReferralId: ctx.user.referal_id,
  191. deletedUserReferredUsers: ctx.user.refered_users,
  192. deletedUserReferredUserCount: ctx.user.refered_user_count,
  193. deletedUserOverleafId: ctx.user.overleaf.id,
  194. },
  195. }
  196. })
  197. describe('when only the ip address is passed', function () {
  198. beforeEach(function (ctx) {
  199. ctx.DeletedUserMock.expects('updateOne')
  200. .withArgs(
  201. { 'deleterData.deletedUserId': ctx.userId },
  202. ctx.deletedUser,
  203. { upsert: true }
  204. )
  205. .chain('exec')
  206. .resolves()
  207. })
  208. describe('when unsubscribing in Mailchimp succeeds', function () {
  209. beforeEach(function (ctx) {
  210. ctx.UserMock.expects('deleteOne')
  211. .withArgs({ _id: ctx.userId })
  212. .chain('exec')
  213. .resolves()
  214. })
  215. it('should find and the user in mongo by its id', async function (ctx) {
  216. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  217. ipAddress: ctx.ipAddress,
  218. })
  219. ctx.UserMock.verify()
  220. })
  221. it('should delete the user from mailchimp', async function (ctx) {
  222. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  223. ipAddress: ctx.ipAddress,
  224. })
  225. expect(
  226. ctx.NewsletterManager.promises.unsubscribe
  227. ).to.have.been.calledWith(ctx.user, { delete: true })
  228. })
  229. it('should delete all the projects of a user', async function (ctx) {
  230. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  231. ipAddress: ctx.ipAddress,
  232. })
  233. expect(
  234. ctx.ProjectDeleter.promises.deleteUsersProjects
  235. ).to.have.been.calledWith(ctx.userId)
  236. })
  237. it("should cancel the user's subscription", async function (ctx) {
  238. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  239. ipAddress: ctx.ipAddress,
  240. })
  241. expect(
  242. ctx.SubscriptionHandler.promises.cancelSubscription
  243. ).to.have.been.calledWith(ctx.user)
  244. })
  245. it('should delete user affiliations', async function (ctx) {
  246. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  247. ipAddress: ctx.ipAddress,
  248. })
  249. expect(
  250. ctx.InstitutionsApi.promises.deleteAffiliations
  251. ).to.have.been.calledWith(ctx.userId)
  252. })
  253. it('should cleanup collabratec access tokens', async function (ctx) {
  254. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  255. ipAddress: ctx.ipAddress,
  256. })
  257. expect(ctx.Modules.promises.hooks.fire).to.have.been.calledWith(
  258. 'cleanupPersonalAccessTokens',
  259. ctx.userId,
  260. ['collabratec', 'git_bridge']
  261. )
  262. })
  263. it('should fire the deleteUser hook for modules', async function (ctx) {
  264. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  265. ipAddress: ctx.ipAddress,
  266. })
  267. expect(ctx.Modules.promises.hooks.fire).to.have.been.calledWith(
  268. 'deleteUser',
  269. ctx.userId
  270. )
  271. })
  272. it('should stop the user sessions', async function (ctx) {
  273. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  274. ipAddress: ctx.ipAddress,
  275. })
  276. expect(
  277. ctx.UserSessionsManager.promises.removeSessionsFromRedis
  278. ).to.have.been.calledWith(ctx.user)
  279. })
  280. it('should remove user from group subscriptions', async function (ctx) {
  281. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  282. ipAddress: ctx.ipAddress,
  283. })
  284. expect(
  285. ctx.SubscriptionUpdater.promises.removeUserFromAllGroups
  286. ).to.have.been.calledWith(ctx.userId)
  287. })
  288. it('should remove user memberships', async function (ctx) {
  289. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  290. ipAddress: ctx.ipAddress,
  291. })
  292. expect(
  293. ctx.UserMembershipsHandler.promises.removeUserFromAllEntities
  294. ).to.have.been.calledWith(ctx.userId)
  295. })
  296. it('rejects if the user is a subscription admin', async function (ctx) {
  297. ctx.SubscriptionLocator.promises.getUsersSubscription.rejects({
  298. _id: 'some-subscription',
  299. })
  300. await expect(ctx.UserDeleter.promises.deleteUser(ctx.userId, {})).to
  301. .be.rejected
  302. })
  303. it('should create a deletedUser', async function (ctx) {
  304. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  305. ipAddress: ctx.ipAddress,
  306. })
  307. ctx.DeletedUserMock.verify()
  308. })
  309. describe('email notifications', function () {
  310. it('should email the user', async function (ctx) {
  311. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  312. ipAddress: ctx.ipAddress,
  313. })
  314. const emailOptions = {
  315. to: 'bob@bob.com',
  316. action: 'account deleted',
  317. actionDescribed: 'your Overleaf account was deleted',
  318. }
  319. expect(
  320. ctx.EmailHandler.promises.sendEmail
  321. ).to.have.been.calledWith('securityAlert', emailOptions)
  322. })
  323. it('should not email the user with skipEmail === true', async function (ctx) {
  324. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  325. ipAddress: ctx.ipAddress,
  326. skipEmail: true,
  327. })
  328. expect(ctx.EmailHandler.promises.sendEmail).not.to.have.been
  329. .called
  330. })
  331. it('should fail when the email service fails', async function (ctx) {
  332. ctx.EmailHandler.promises.sendEmail = sinon
  333. .stub()
  334. .rejects(new Error('email failed'))
  335. await expect(
  336. ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  337. ipAddress: ctx.ipAddress,
  338. })
  339. ).to.be.rejectedWith('email failed')
  340. })
  341. describe('with "force: true" option', function () {
  342. it('should succeed when the email service fails', async function (ctx) {
  343. ctx.EmailHandler.promises.sendEmail = sinon
  344. .stub()
  345. .rejects(new Error('email failed'))
  346. await expect(
  347. ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  348. ipAddress: ctx.ipAddress,
  349. force: true,
  350. })
  351. ).to.be.fulfilled
  352. })
  353. })
  354. })
  355. it('should add an audit log entry', async function (ctx) {
  356. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  357. ipAddress: ctx.ipAddress,
  358. })
  359. expect(
  360. ctx.UserAuditLogHandler.promises.addEntry
  361. ).to.have.been.calledWith(
  362. ctx.userId,
  363. 'delete-account',
  364. ctx.userId,
  365. ctx.ipAddress,
  366. {}
  367. )
  368. })
  369. })
  370. describe('when unsubscribing from mailchimp fails', function () {
  371. beforeEach(function (ctx) {
  372. ctx.NewsletterManager.promises.unsubscribe.rejects(
  373. new Error('something went wrong')
  374. )
  375. })
  376. it('should return an error and not delete the user', async function (ctx) {
  377. await expect(
  378. ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  379. ipAddress: ctx.ipAddress,
  380. })
  381. ).to.be.rejected
  382. ctx.UserMock.verify()
  383. })
  384. })
  385. describe('when called as a callback', function () {
  386. beforeEach(function (ctx) {
  387. ctx.UserMock.expects('deleteOne')
  388. .withArgs({ _id: ctx.userId })
  389. .chain('exec')
  390. .resolves()
  391. })
  392. it('should delete the user', async function (ctx) {
  393. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  394. ipAddress: ctx.ipAddress,
  395. })
  396. ctx.UserMock.verify()
  397. ctx.DeletedUserMock.verify()
  398. })
  399. })
  400. })
  401. describe('when a user and IP address are specified', function () {
  402. beforeEach(function (ctx) {
  403. ctx.ipAddress = '1.2.3.4'
  404. ctx.deleterId = new ObjectId()
  405. ctx.deletedUser.deleterData.deleterIpAddress = ctx.ipAddress
  406. ctx.deletedUser.deleterData.deleterId = ctx.deleterId
  407. ctx.DeletedUserMock.expects('updateOne')
  408. .withArgs(
  409. { 'deleterData.deletedUserId': ctx.userId },
  410. ctx.deletedUser,
  411. { upsert: true }
  412. )
  413. .chain('exec')
  414. .resolves()
  415. ctx.UserMock.expects('deleteOne')
  416. .withArgs({ _id: ctx.userId })
  417. .chain('exec')
  418. .resolves()
  419. })
  420. it('should add the deleted user id and ip address to the deletedUser', async function (ctx) {
  421. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  422. deleterUser: { _id: ctx.deleterId },
  423. ipAddress: ctx.ipAddress,
  424. })
  425. ctx.DeletedUserMock.verify()
  426. })
  427. it('should add an audit log entry', async function (ctx) {
  428. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  429. deleterUser: { _id: ctx.deleterId },
  430. ipAddress: ctx.ipAddress,
  431. })
  432. expect(
  433. ctx.UserAuditLogHandler.promises.addEntry
  434. ).to.have.been.calledWith(
  435. ctx.userId,
  436. 'delete-account',
  437. ctx.deleterId,
  438. ctx.ipAddress,
  439. {}
  440. )
  441. })
  442. describe('when called as a callback', function () {
  443. it('should delete the user', async function (ctx) {
  444. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  445. deleterUser: { _id: ctx.deleterId },
  446. ipAddress: ctx.ipAddress,
  447. })
  448. ctx.UserMock.verify()
  449. ctx.DeletedUserMock.verify()
  450. })
  451. })
  452. })
  453. describe('when the user is part of a managed subscription', function () {
  454. beforeEach(function (ctx) {
  455. ctx.managedSubscriptionId = new ObjectId()
  456. ctx.SubscriptionLocator.promises.getUniqueManagedSubscriptionMemberOf.resolves(
  457. {
  458. _id: ctx.managedSubscriptionId,
  459. }
  460. )
  461. ctx.DeletedUserMock.expects('updateOne')
  462. .withArgs(
  463. { 'deleterData.deletedUserId': ctx.userId },
  464. ctx.deletedUser,
  465. { upsert: true }
  466. )
  467. .chain('exec')
  468. .resolves()
  469. ctx.UserMock.expects('deleteOne')
  470. .withArgs({ _id: ctx.userId })
  471. .chain('exec')
  472. .resolves()
  473. })
  474. it('should include managedSubscriptionId in audit log', async function (ctx) {
  475. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  476. ipAddress: ctx.ipAddress,
  477. })
  478. expect(
  479. ctx.UserAuditLogHandler.promises.addEntry
  480. ).to.have.been.calledWith(
  481. ctx.userId,
  482. 'delete-account',
  483. ctx.userId,
  484. ctx.ipAddress,
  485. {}
  486. )
  487. })
  488. })
  489. describe('when checking managed subscription fails', function () {
  490. beforeEach(function (ctx) {
  491. ctx.SubscriptionLocator.promises.getUniqueManagedSubscriptionMemberOf.rejects(
  492. new Error('subscription lookup failed')
  493. )
  494. ctx.DeletedUserMock.expects('updateOne')
  495. .withArgs(
  496. { 'deleterData.deletedUserId': ctx.userId },
  497. ctx.deletedUser,
  498. { upsert: true }
  499. )
  500. .chain('exec')
  501. .resolves()
  502. ctx.UserMock.expects('deleteOne')
  503. .withArgs({ _id: ctx.userId })
  504. .chain('exec')
  505. .resolves()
  506. })
  507. it('should continue with deletion and not include managedSubscriptionId', async function (ctx) {
  508. await ctx.UserDeleter.promises.deleteUser(ctx.userId, {
  509. ipAddress: ctx.ipAddress,
  510. })
  511. expect(
  512. ctx.UserAuditLogHandler.promises.addEntry
  513. ).to.have.been.calledWith(
  514. ctx.userId,
  515. 'delete-account',
  516. ctx.userId,
  517. ctx.ipAddress,
  518. {}
  519. )
  520. })
  521. })
  522. })
  523. describe('when the user cannot be deleted because they are a subscription admin', function () {
  524. beforeEach(function (ctx) {
  525. ctx.SubscriptionLocator.promises.getUsersSubscription.resolves({
  526. _id: 'some-subscription',
  527. })
  528. })
  529. it('fails with a SubscriptionAdminDeletionError', async function (ctx) {
  530. await expect(
  531. ctx.UserDeleter.promises.deleteUser(ctx.userId)
  532. ).to.be.rejectedWith(Errors.SubscriptionAdminDeletionError)
  533. })
  534. it('should not create a deletedUser', async function (ctx) {
  535. await expect(ctx.UserDeleter.promises.deleteUser(ctx.userId)).to.be
  536. .rejected
  537. ctx.DeletedUserMock.verify()
  538. })
  539. it('should not remove the user from mongo', async function (ctx) {
  540. await expect(ctx.UserDeleter.promises.deleteUser(ctx.userId)).to.be
  541. .rejected
  542. ctx.UserMock.verify()
  543. })
  544. })
  545. })
  546. describe('ensureCanDeleteUser', function () {
  547. it('should not return error when user can be deleted', async function (ctx) {
  548. ctx.SubscriptionLocator.promises.getUsersSubscription.resolves(null)
  549. let error
  550. try {
  551. await ctx.UserDeleter.promises.ensureCanDeleteUser(ctx.user)
  552. } catch (e) {
  553. error = e
  554. } finally {
  555. expect(error).not.to.exist
  556. }
  557. })
  558. it('should return custom error when user is group admin', async function (ctx) {
  559. ctx.SubscriptionLocator.promises.getUsersSubscription.resolves({
  560. _id: '123abc',
  561. })
  562. let error
  563. try {
  564. await ctx.UserDeleter.promises.ensureCanDeleteUser(ctx.user)
  565. } catch (e) {
  566. error = e
  567. } finally {
  568. expect(error).to.be.instanceof(Errors.SubscriptionAdminDeletionError)
  569. }
  570. })
  571. it('propagates errors', async function (ctx) {
  572. ctx.SubscriptionLocator.promises.getUsersSubscription.rejects(
  573. new Error('Some error')
  574. )
  575. let error
  576. try {
  577. await ctx.UserDeleter.promises.ensureCanDeleteUser(ctx.user)
  578. } catch (e) {
  579. error = e
  580. } finally {
  581. expect(error).to.be.instanceof(Error)
  582. }
  583. })
  584. })
  585. describe('expireDeletedUsersAfterDuration', function () {
  586. const userId1 = new ObjectId()
  587. const userId2 = new ObjectId()
  588. beforeEach(function (ctx) {
  589. ctx.deletedUsers = [
  590. {
  591. user: { _id: userId1 },
  592. deleterData: { deletedUserId: userId1 },
  593. save: sinon.stub().resolves(),
  594. },
  595. {
  596. user: { _id: userId2 },
  597. deleterData: { deletedUserId: userId2 },
  598. save: sinon.stub().resolves(),
  599. },
  600. ]
  601. ctx.DeletedUserMock.expects('find')
  602. .withArgs({
  603. 'deleterData.deletedAt': {
  604. $lt: new Date(moment().subtract(90, 'days')),
  605. },
  606. user: {
  607. $type: 'object',
  608. },
  609. })
  610. .chain('exec')
  611. .resolves(ctx.deletedUsers)
  612. for (const deletedUser of ctx.deletedUsers) {
  613. ctx.DeletedUserMock.expects('findOne')
  614. .withArgs({
  615. 'deleterData.deletedUserId': deletedUser.deleterData.deletedUserId,
  616. })
  617. .chain('exec')
  618. .resolves(deletedUser)
  619. }
  620. })
  621. it('clears data from all deleted users', async function (ctx) {
  622. await ctx.UserDeleter.promises.expireDeletedUsersAfterDuration()
  623. for (const deletedUser of ctx.deletedUsers) {
  624. expect(deletedUser.user).to.be.undefined
  625. expect(deletedUser.save.called).to.be.true
  626. }
  627. })
  628. it('deletes audit logs for all deleted users', async function (ctx) {
  629. await ctx.UserDeleter.promises.expireDeletedUsersAfterDuration()
  630. for (const deletedUser of ctx.deletedUsers) {
  631. expect(ctx.UserAuditLogEntry.deleteMany).to.have.been.calledWith({
  632. userId: deletedUser.deleterData.deletedUserId,
  633. })
  634. }
  635. })
  636. })
  637. describe('expireDeletedUser', function () {
  638. beforeEach(function (ctx) {
  639. ctx.mockedDeletedUser = sinon.mock(
  640. new DeletedUser({
  641. user: ctx.user,
  642. deleterData: {
  643. deleterIpAddress: '1.1.1.1',
  644. deletedUserId: ctx.userId,
  645. },
  646. })
  647. )
  648. ctx.deletedUser = ctx.mockedDeletedUser.object
  649. ctx.mockedDeletedUser.expects('save').resolves()
  650. ctx.DeletedUserMock.expects('findOne')
  651. .withArgs({ 'deleterData.deletedUserId': ctx.userId })
  652. .chain('exec')
  653. .resolves(ctx.deletedUser)
  654. })
  655. afterEach(function (ctx) {
  656. ctx.mockedDeletedUser.restore()
  657. })
  658. it('should find the user by user ID', async function (ctx) {
  659. await ctx.UserDeleter.promises.expireDeletedUser(ctx.userId)
  660. ctx.DeletedUserMock.verify()
  661. })
  662. it('should remove the user data from mongo', async function (ctx) {
  663. await ctx.UserDeleter.promises.expireDeletedUser(ctx.userId)
  664. expect(ctx.deletedUser.user).not.to.exist
  665. })
  666. it('should remove the IP address from mongo', async function (ctx) {
  667. await ctx.UserDeleter.promises.expireDeletedUser(ctx.userId)
  668. expect(ctx.deletedUser.deleterData.ipAddress).not.to.exist
  669. })
  670. it('should not delete other deleterData fields', async function (ctx) {
  671. await ctx.UserDeleter.promises.expireDeletedUser(ctx.userId)
  672. expect(ctx.deletedUser.deleterData.deletedUserId).to.equal(ctx.userId)
  673. })
  674. it('should save the record to mongo', async function (ctx) {
  675. await ctx.UserDeleter.promises.expireDeletedUser(ctx.userId)
  676. ctx.mockedDeletedUser.verify()
  677. })
  678. it('should fire the expireDeletedUser hook for modules', async function (ctx) {
  679. await ctx.UserDeleter.promises.expireDeletedUser(ctx.userId)
  680. expect(ctx.Modules.promises.hooks.fire).to.have.been.calledWith(
  681. 'expireDeletedUser',
  682. ctx.userId
  683. )
  684. })
  685. describe('when called as a callback', function () {
  686. it('should expire the user', async function (ctx) {
  687. await new Promise(resolve => {
  688. ctx.UserDeleter.expireDeletedUser(ctx.userId, err => {
  689. expect(err).not.to.exist
  690. ctx.DeletedUserMock.verify()
  691. ctx.mockedDeletedUser.verify()
  692. expect(ctx.deletedUser.user).not.to.exist
  693. expect(ctx.deletedUser.deleterData.ipAddress).not.to.exist
  694. resolve()
  695. })
  696. })
  697. })
  698. })
  699. })
  700. })