2019-05-29 05:21:06 -04:00
|
|
|
const chai = require('chai')
|
2019-07-18 10:18:56 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const tk = require('timekeeper')
|
|
|
|
const moment = require('moment')
|
2019-05-29 05:21:06 -04:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const Errors = require('../../../../app/src/Features/Errors/Errors')
|
2019-07-18 10:18:56 -04:00
|
|
|
const ObjectId = require('mongoose').Types.ObjectId
|
|
|
|
const { User } = require('../helpers/models/User')
|
|
|
|
const { DeletedUser } = require('../helpers/models/DeletedUser')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-07-18 10:18:56 -04:00
|
|
|
const expect = chai.expect
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-07-18 10:18:56 -04:00
|
|
|
const modulePath = '../../../../app/src/Features/User/UserDeleter.js'
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('UserDeleter', function() {
|
|
|
|
beforeEach(function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
tk.freeze(Date.now())
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-07-18 10:18:56 -04:00
|
|
|
this.userId = ObjectId()
|
|
|
|
|
|
|
|
this.UserMock = sinon.mock(User)
|
|
|
|
this.DeletedUserMock = sinon.mock(DeletedUser)
|
|
|
|
|
|
|
|
this.mockedUser = sinon.mock(
|
|
|
|
new User({
|
|
|
|
_id: this.userId,
|
|
|
|
email: 'bob@bob.com',
|
|
|
|
lastLoggedIn: Date.now() + 1000,
|
|
|
|
signUpDate: Date.now() + 2000,
|
|
|
|
loginCount: 10,
|
|
|
|
overleaf: {
|
|
|
|
id: 1234
|
|
|
|
},
|
|
|
|
refered_users: ['wombat', 'potato'],
|
|
|
|
refered_user_count: 2,
|
|
|
|
referal_id: ['giraffe']
|
|
|
|
})
|
|
|
|
)
|
|
|
|
this.user = this.mockedUser.object
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
this.NewsletterManager = {
|
|
|
|
promises: {
|
|
|
|
unsubscribe: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 10:18:56 -04:00
|
|
|
|
|
|
|
this.ProjectDeleter = {
|
|
|
|
promises: {
|
|
|
|
deleteUsersProjects: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
this.SubscriptionHandler = {
|
2019-08-28 08:59:41 -04:00
|
|
|
promises: {
|
|
|
|
cancelSubscription: sinon.stub().resolves()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.SubscriptionUpdater = {
|
2019-08-28 08:59:41 -04:00
|
|
|
promises: {
|
|
|
|
removeUserFromAllGroups: sinon.stub().resolves()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.SubscriptionLocator = {
|
2019-08-28 08:59:41 -04:00
|
|
|
promises: {
|
|
|
|
getUsersSubscription: sinon.stub().resolves()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.UserMembershipsHandler = {
|
2019-08-28 08:59:41 -04:00
|
|
|
promises: {
|
|
|
|
removeUserFromAllEntities: sinon.stub().resolves()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 03:10:05 -05:00
|
|
|
this.UserSessionsManager = {
|
|
|
|
promises: {
|
|
|
|
revokeAllUserSessions: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 10:18:56 -04:00
|
|
|
this.InstitutionsApi = {
|
2019-08-28 08:59:41 -04:00
|
|
|
promises: {
|
|
|
|
deleteAffiliations: sinon.stub().resolves()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-07-18 10:18:56 -04:00
|
|
|
this.UserDeleter = SandboxedModule.require(modulePath, {
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
2019-07-18 10:18:56 -04:00
|
|
|
'../../models/User': { User: User },
|
|
|
|
'../../models/DeletedUser': { DeletedUser: DeletedUser },
|
2019-05-29 05:21:06 -04:00
|
|
|
'../Newsletter/NewsletterManager': this.NewsletterManager,
|
2019-12-10 03:10:05 -05:00
|
|
|
'./UserSessionsManager': this.UserSessionsManager,
|
2019-05-29 05:21:06 -04:00
|
|
|
'../Subscription/SubscriptionHandler': this.SubscriptionHandler,
|
|
|
|
'../Subscription/SubscriptionUpdater': this.SubscriptionUpdater,
|
|
|
|
'../Subscription/SubscriptionLocator': this.SubscriptionLocator,
|
|
|
|
'../UserMembership/UserMembershipsHandler': this.UserMembershipsHandler,
|
|
|
|
'../Project/ProjectDeleter': this.ProjectDeleter,
|
2019-07-18 10:18:56 -04:00
|
|
|
'../Institutions/InstitutionsAPI': this.InstitutionsApi,
|
2019-05-29 05:21:06 -04:00
|
|
|
'logger-sharelatex': (this.logger = {
|
|
|
|
log: sinon.stub(),
|
2019-07-01 09:48:09 -04:00
|
|
|
warn: sinon.stub(),
|
2019-05-29 05:21:06 -04:00
|
|
|
err: sinon.stub()
|
|
|
|
}),
|
|
|
|
'../Errors/Errors': Errors
|
2019-07-18 10:18:56 -04:00
|
|
|
},
|
|
|
|
globals: {
|
|
|
|
console: console
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
2019-07-18 10:18:56 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
afterEach(function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
this.DeletedUserMock.restore()
|
|
|
|
this.UserMock.restore()
|
|
|
|
this.mockedUser.restore()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('deleteUser', function() {
|
|
|
|
beforeEach(function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
this.UserMock.expects('findById')
|
|
|
|
.withArgs(this.userId)
|
|
|
|
.chain('exec')
|
|
|
|
.resolves(this.user)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when the user can be deleted', function() {
|
|
|
|
beforeEach(function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
this.deletedUser = {
|
|
|
|
user: this.user,
|
|
|
|
deleterData: {
|
|
|
|
deletedAt: new Date(),
|
|
|
|
deletedUserId: this.userId,
|
|
|
|
deleterIpAddress: undefined,
|
|
|
|
deleterId: undefined,
|
|
|
|
deletedUserLastLoggedIn: this.user.lastLoggedIn,
|
|
|
|
deletedUserSignUpDate: this.user.signUpDate,
|
|
|
|
deletedUserLoginCount: this.user.loginCount,
|
|
|
|
deletedUserReferralId: this.user.referal_id,
|
|
|
|
deletedUserReferredUsers: this.user.refered_users,
|
|
|
|
deletedUserReferredUserCount: this.user.refered_user_count,
|
|
|
|
deletedUserOverleafId: this.user.overleaf.id
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when no options are passed', function() {
|
|
|
|
beforeEach(function() {
|
2020-10-12 07:40:01 -04:00
|
|
|
this.DeletedUserMock.expects('updateOne')
|
2020-02-12 10:14:57 -05:00
|
|
|
.withArgs(
|
|
|
|
{ 'deleterData.deletedUserId': this.userId },
|
|
|
|
this.deletedUser,
|
|
|
|
{ upsert: true }
|
|
|
|
)
|
2019-07-18 10:18:56 -04:00
|
|
|
.chain('exec')
|
|
|
|
.resolves()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
describe('when unsubscribing in Mailchimp succeeds', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.UserMock.expects('deleteOne')
|
|
|
|
.withArgs({ _id: this.userId })
|
|
|
|
.chain('exec')
|
|
|
|
.resolves()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
it('should find and the user in mongo by its id', async function() {
|
|
|
|
await this.UserDeleter.promises.deleteUser(this.userId)
|
|
|
|
this.UserMock.verify()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-25 10:27:38 -04:00
|
|
|
it('should delete the user from mailchimp', async function() {
|
2019-08-28 08:59:41 -04:00
|
|
|
await this.UserDeleter.promises.deleteUser(this.userId)
|
|
|
|
expect(
|
|
|
|
this.NewsletterManager.promises.unsubscribe
|
2019-09-25 10:27:38 -04:00
|
|
|
).to.have.been.calledWith(this.user, { delete: true })
|
2019-08-28 08:59:41 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
it('should delete all the projects of a user', async function() {
|
|
|
|
await this.UserDeleter.promises.deleteUser(this.userId)
|
|
|
|
expect(
|
|
|
|
this.ProjectDeleter.promises.deleteUsersProjects
|
|
|
|
).to.have.been.calledWith(this.userId)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
it("should cancel the user's subscription", async function() {
|
|
|
|
await this.UserDeleter.promises.deleteUser(this.userId)
|
|
|
|
expect(
|
|
|
|
this.SubscriptionHandler.promises.cancelSubscription
|
|
|
|
).to.have.been.calledWith(this.user)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
it('should delete user affiliations', async function() {
|
|
|
|
await this.UserDeleter.promises.deleteUser(this.userId)
|
|
|
|
expect(
|
|
|
|
this.InstitutionsApi.promises.deleteAffiliations
|
|
|
|
).to.have.been.calledWith(this.userId)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-12-10 03:10:05 -05:00
|
|
|
it('should stop the user sessions', async function() {
|
|
|
|
await this.UserDeleter.promises.deleteUser(this.userId)
|
|
|
|
expect(
|
|
|
|
this.UserSessionsManager.promises.revokeAllUserSessions
|
|
|
|
).to.have.been.calledWith(this.userId, [])
|
|
|
|
})
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
it('should remove user from group subscriptions', async function() {
|
|
|
|
await this.UserDeleter.promises.deleteUser(this.userId)
|
|
|
|
expect(
|
|
|
|
this.SubscriptionUpdater.promises.removeUserFromAllGroups
|
|
|
|
).to.have.been.calledWith(this.userId)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
it('should remove user memberships', async function() {
|
|
|
|
await this.UserDeleter.promises.deleteUser(this.userId)
|
|
|
|
expect(
|
|
|
|
this.UserMembershipsHandler.promises.removeUserFromAllEntities
|
|
|
|
).to.have.been.calledWith(this.userId)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
it('rejects if the user is a subscription admin', async function() {
|
|
|
|
this.SubscriptionLocator.promises.getUsersSubscription.rejects({
|
|
|
|
_id: 'some-subscription'
|
|
|
|
})
|
|
|
|
await expect(this.UserDeleter.promises.deleteUser(this.userId)).to
|
|
|
|
.be.rejected
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should create a deletedUser', async function() {
|
|
|
|
await this.UserDeleter.promises.deleteUser(this.userId)
|
|
|
|
this.DeletedUserMock.verify()
|
|
|
|
})
|
2019-07-18 10:18:56 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when unsubscribing from mailchimp fails', function() {
|
|
|
|
beforeEach(function() {
|
2019-08-28 08:59:41 -04:00
|
|
|
this.NewsletterManager.promises.unsubscribe.rejects(
|
|
|
|
new Error('something went wrong')
|
2019-07-18 10:18:56 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
it('should return an error and not delete the user', async function() {
|
|
|
|
await expect(this.UserDeleter.promises.deleteUser(this.userId)).to
|
|
|
|
.be.rejected
|
2019-07-18 10:18:56 -04:00
|
|
|
this.UserMock.verify()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when called as a callback', function() {
|
2019-08-28 08:59:41 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
this.UserMock.expects('deleteOne')
|
|
|
|
.withArgs({ _id: this.userId })
|
|
|
|
.chain('exec')
|
|
|
|
.resolves()
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
it('should delete the user', function(done) {
|
2019-07-18 10:18:56 -04:00
|
|
|
this.UserDeleter.deleteUser(this.userId, err => {
|
|
|
|
expect(err).not.to.exist
|
|
|
|
this.UserMock.verify()
|
|
|
|
this.DeletedUserMock.verify()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when a user and IP address are specified', function() {
|
|
|
|
beforeEach(function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
this.ipAddress = '1.2.3.4'
|
|
|
|
this.deleterId = ObjectId()
|
|
|
|
|
|
|
|
this.deletedUser.deleterData.deleterIpAddress = this.ipAddress
|
|
|
|
this.deletedUser.deleterData.deleterId = this.deleterId
|
|
|
|
|
2020-10-12 07:40:01 -04:00
|
|
|
this.DeletedUserMock.expects('updateOne')
|
2020-02-12 10:14:57 -05:00
|
|
|
.withArgs(
|
|
|
|
{ 'deleterData.deletedUserId': this.userId },
|
|
|
|
this.deletedUser,
|
|
|
|
{ upsert: true }
|
|
|
|
)
|
2019-07-18 10:18:56 -04:00
|
|
|
.chain('exec')
|
|
|
|
.resolves()
|
2019-08-28 08:59:41 -04:00
|
|
|
this.UserMock.expects('deleteOne')
|
|
|
|
.withArgs({ _id: this.userId })
|
|
|
|
.chain('exec')
|
|
|
|
.resolves()
|
2019-07-18 10:18:56 -04:00
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
it('should add the deleted user id and ip address to the deletedUser', async function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
await this.UserDeleter.promises.deleteUser(this.userId, {
|
|
|
|
deleterUser: { _id: this.deleterId },
|
|
|
|
ipAddress: this.ipAddress
|
|
|
|
})
|
|
|
|
this.DeletedUserMock.verify()
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when called as a callback', function() {
|
|
|
|
it('should delete the user', function(done) {
|
2019-07-18 10:18:56 -04:00
|
|
|
this.UserDeleter.deleteUser(
|
|
|
|
this.userId,
|
|
|
|
{
|
|
|
|
deleterUser: { _id: this.deleterId },
|
|
|
|
ipAddress: this.ipAddress
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
expect(err).not.to.exist
|
|
|
|
this.UserMock.verify()
|
|
|
|
this.DeletedUserMock.verify()
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when the user cannot be deleted because they are a subscription admin', function() {
|
|
|
|
beforeEach(function() {
|
2019-08-28 08:59:41 -04:00
|
|
|
this.SubscriptionLocator.promises.getUsersSubscription.resolves({
|
|
|
|
_id: 'some-subscription'
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
it('fails with a SubscriptionAdminDeletionError', async function() {
|
2019-08-28 08:59:41 -04:00
|
|
|
await expect(
|
|
|
|
this.UserDeleter.promises.deleteUser(this.userId)
|
|
|
|
).to.be.rejectedWith(Errors.SubscriptionAdminDeletionError)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
it('should not create a deletedUser', async function() {
|
2019-08-28 08:59:41 -04:00
|
|
|
await expect(this.UserDeleter.promises.deleteUser(this.userId)).to.be
|
|
|
|
.rejected
|
|
|
|
this.DeletedUserMock.verify()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
it('should not remove the user from mongo', async function() {
|
2019-08-28 08:59:41 -04:00
|
|
|
await expect(this.UserDeleter.promises.deleteUser(this.userId)).to.be
|
|
|
|
.rejected
|
|
|
|
this.UserMock.verify()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('ensureCanDeleteUser', function() {
|
|
|
|
it('should not return error when user can be deleted', async function() {
|
2019-08-28 08:59:41 -04:00
|
|
|
this.SubscriptionLocator.promises.getUsersSubscription.resolves(null)
|
2019-07-18 10:18:56 -04:00
|
|
|
let error
|
|
|
|
try {
|
|
|
|
await this.UserDeleter.promises.ensureCanDeleteUser(this.user)
|
|
|
|
} catch (e) {
|
|
|
|
error = e
|
|
|
|
} finally {
|
|
|
|
expect(error).not.to.exist
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
it('should return custom error when user is group admin', async function() {
|
2019-08-28 08:59:41 -04:00
|
|
|
this.SubscriptionLocator.promises.getUsersSubscription.resolves({
|
2019-05-29 05:21:06 -04:00
|
|
|
_id: '123abc'
|
|
|
|
})
|
2019-07-18 10:18:56 -04:00
|
|
|
let error
|
|
|
|
try {
|
|
|
|
await this.UserDeleter.promises.ensureCanDeleteUser(this.user)
|
|
|
|
} catch (e) {
|
|
|
|
error = e
|
|
|
|
} finally {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(error).to.be.instanceof(Errors.SubscriptionAdminDeletionError)
|
2019-07-18 10:18:56 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
it('propagates errors', async function() {
|
2019-08-28 08:59:41 -04:00
|
|
|
this.SubscriptionLocator.promises.getUsersSubscription.rejects(
|
2019-05-29 05:21:06 -04:00
|
|
|
new Error('Some error')
|
|
|
|
)
|
2019-07-18 10:18:56 -04:00
|
|
|
let error
|
|
|
|
try {
|
|
|
|
await this.UserDeleter.promises.ensureCanDeleteUser(this.user)
|
|
|
|
} catch (e) {
|
|
|
|
error = e
|
|
|
|
} finally {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(error).to.be.instanceof(Error)
|
2019-07-18 10:18:56 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('expireDeletedUsersAfterDuration', function() {
|
2019-08-28 08:59:41 -04:00
|
|
|
const userId1 = new ObjectId()
|
|
|
|
const userId2 = new ObjectId()
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
beforeEach(function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
this.deletedUsers = [
|
|
|
|
{
|
2019-08-28 08:59:41 -04:00
|
|
|
user: { _id: userId1 },
|
|
|
|
deleterData: { deletedUserId: userId1 },
|
|
|
|
save: sinon.stub().resolves()
|
2019-07-18 10:18:56 -04:00
|
|
|
},
|
|
|
|
{
|
2019-08-28 08:59:41 -04:00
|
|
|
user: { _id: userId2 },
|
|
|
|
deleterData: { deletedUserId: userId2 },
|
|
|
|
save: sinon.stub().resolves()
|
2019-07-18 10:18:56 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
this.DeletedUserMock.expects('find')
|
|
|
|
.withArgs({
|
|
|
|
'deleterData.deletedAt': {
|
|
|
|
$lt: new Date(moment().subtract(90, 'days'))
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
$ne: null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.chain('exec')
|
|
|
|
.resolves(this.deletedUsers)
|
2019-08-28 08:59:41 -04:00
|
|
|
for (const deletedUser of this.deletedUsers) {
|
|
|
|
this.DeletedUserMock.expects('findOne')
|
|
|
|
.withArgs({
|
|
|
|
'deleterData.deletedUserId': deletedUser.deleterData.deletedUserId
|
|
|
|
})
|
|
|
|
.chain('exec')
|
|
|
|
.resolves(deletedUser)
|
|
|
|
}
|
2019-07-18 10:18:56 -04:00
|
|
|
})
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
it('clears data from all deleted users', async function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
await this.UserDeleter.promises.expireDeletedUsersAfterDuration()
|
2019-08-28 08:59:41 -04:00
|
|
|
for (const deletedUser of this.deletedUsers) {
|
|
|
|
expect(deletedUser.user).to.be.undefined
|
|
|
|
expect(deletedUser.save.called).to.be.true
|
|
|
|
}
|
2019-07-18 10:18:56 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('expireDeletedUser', function() {
|
|
|
|
beforeEach(function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
this.mockedDeletedUser = sinon.mock(
|
|
|
|
new DeletedUser({
|
|
|
|
user: this.user,
|
|
|
|
deleterData: {
|
|
|
|
deleterIpAddress: '1.1.1.1',
|
|
|
|
deletedUserId: this.userId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
this.deletedUser = this.mockedDeletedUser.object
|
|
|
|
|
|
|
|
this.mockedDeletedUser.expects('save').resolves()
|
|
|
|
|
|
|
|
this.DeletedUserMock.expects('findOne')
|
|
|
|
.withArgs({ 'deleterData.deletedUserId': 'giraffe' })
|
|
|
|
.chain('exec')
|
|
|
|
.resolves(this.deletedUser)
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
afterEach(function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
this.mockedDeletedUser.restore()
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
it('should find the user by user ID', async function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
await this.UserDeleter.promises.expireDeletedUser('giraffe')
|
|
|
|
this.DeletedUserMock.verify()
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
it('should remove the user data from mongo', async function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
await this.UserDeleter.promises.expireDeletedUser('giraffe')
|
|
|
|
expect(this.deletedUser.user).not.to.exist
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
it('should remove the IP address from mongo', async function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
await this.UserDeleter.promises.expireDeletedUser('giraffe')
|
|
|
|
expect(this.deletedUser.deleterData.ipAddress).not.to.exist
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
it('should not delete other deleterData fields', async function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
await this.UserDeleter.promises.expireDeletedUser('giraffe')
|
|
|
|
expect(this.deletedUser.deleterData.deletedUserId).to.equal(this.userId)
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
it('should save the record to mongo', async function() {
|
2019-07-18 10:18:56 -04:00
|
|
|
await this.UserDeleter.promises.expireDeletedUser('giraffe')
|
|
|
|
this.mockedDeletedUser.verify()
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when called as a callback', function() {
|
|
|
|
it('should expire the user', function(done) {
|
2019-07-18 10:18:56 -04:00
|
|
|
this.UserDeleter.expireDeletedUser('giraffe', err => {
|
2019-11-18 09:37:05 -05:00
|
|
|
expect(err).not.to.exist
|
2019-07-18 10:18:56 -04:00
|
|
|
this.DeletedUserMock.verify()
|
|
|
|
this.mockedDeletedUser.verify()
|
|
|
|
expect(this.deletedUser.user).not.to.exist
|
|
|
|
expect(this.deletedUser.deleterData.ipAddress).not.to.exist
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|