UserDeleter.test.mjs 25 KB

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