2021-03-31 05:24:39 -04:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const path = require('path')
|
|
|
|
const sinon = require('sinon')
|
2021-07-01 07:42:30 -04:00
|
|
|
const { expect } = require('chai')
|
2021-03-31 05:24:39 -04:00
|
|
|
|
|
|
|
const MODULE_PATH = path.join(
|
|
|
|
__dirname,
|
|
|
|
'../../../../app/src/Features/User/UserOnboardingEmailManager'
|
|
|
|
)
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('UserOnboardingEmailManager', function () {
|
|
|
|
beforeEach(function () {
|
2021-03-31 05:24:39 -04:00
|
|
|
this.fakeUserId = '123abc'
|
|
|
|
this.fakeUserEmail = 'frog@overleaf.com'
|
|
|
|
this.onboardingEmailsQueue = {
|
|
|
|
add: sinon.stub().resolves(),
|
|
|
|
process: callback => {
|
|
|
|
this.queueProcessFunction = callback
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2021-03-31 05:24:39 -04:00
|
|
|
}
|
|
|
|
this.Queues = {
|
2021-10-25 08:07:45 -04:00
|
|
|
createScheduledJob: sinon.stub().resolves(),
|
2021-03-31 05:24:39 -04:00
|
|
|
}
|
|
|
|
this.UserGetter = {
|
|
|
|
promises: {
|
2021-08-17 09:08:54 -04:00
|
|
|
getUser: sinon.stub().resolves(null),
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2021-03-31 05:24:39 -04:00
|
|
|
}
|
2021-08-17 09:08:54 -04:00
|
|
|
this.UserGetter.promises.getUser
|
|
|
|
.withArgs({ _id: this.fakeUserId })
|
|
|
|
.resolves({
|
|
|
|
_id: this.fakeUserId,
|
|
|
|
email: this.fakeUserEmail,
|
|
|
|
})
|
2021-03-31 05:24:39 -04:00
|
|
|
this.EmailHandler = {
|
|
|
|
promises: {
|
2021-04-27 03:52:58 -04:00
|
|
|
sendEmail: sinon.stub().resolves(),
|
|
|
|
},
|
2021-03-31 05:24:39 -04:00
|
|
|
}
|
|
|
|
this.UserUpdater = {
|
|
|
|
promises: {
|
2021-04-27 03:52:58 -04:00
|
|
|
updateUser: sinon.stub().resolves(),
|
|
|
|
},
|
2021-03-31 05:24:39 -04:00
|
|
|
}
|
2021-07-01 07:42:30 -04:00
|
|
|
|
2021-08-17 09:08:54 -04:00
|
|
|
this.UserOnboardingEmailManager = SandboxedModule.require(MODULE_PATH, {
|
|
|
|
requires: {
|
|
|
|
'../../infrastructure/Queues': this.Queues,
|
|
|
|
'../Email/EmailHandler': this.EmailHandler,
|
|
|
|
'./UserGetter': this.UserGetter,
|
|
|
|
'./UserUpdater': this.UserUpdater,
|
|
|
|
},
|
2021-03-31 05:24:39 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-08-17 09:08:54 -04:00
|
|
|
describe('scheduleOnboardingEmail', function () {
|
|
|
|
it('should schedule delayed job on queue', async function () {
|
|
|
|
await this.UserOnboardingEmailManager.scheduleOnboardingEmail({
|
2021-04-27 03:52:58 -04:00
|
|
|
_id: this.fakeUserId,
|
2021-03-31 05:24:39 -04:00
|
|
|
})
|
2021-10-25 08:07:45 -04:00
|
|
|
sinon.assert.calledWith(
|
|
|
|
this.Queues.createScheduledJob,
|
|
|
|
'emails-onboarding',
|
|
|
|
{ data: { userId: this.fakeUserId } },
|
|
|
|
24 * 60 * 60 * 1000
|
2021-03-31 05:24:39 -04:00
|
|
|
)
|
|
|
|
})
|
2021-08-17 09:08:54 -04:00
|
|
|
})
|
2021-03-31 05:24:39 -04:00
|
|
|
|
2021-08-17 09:08:54 -04:00
|
|
|
describe('sendOnboardingEmail', function () {
|
|
|
|
it('should send onboarding email and update user', async function () {
|
|
|
|
await this.UserOnboardingEmailManager.sendOnboardingEmail(this.fakeUserId)
|
|
|
|
expect(this.EmailHandler.promises.sendEmail).to.have.been.calledWith(
|
2021-03-31 05:24:39 -04:00
|
|
|
'userOnboardingEmail',
|
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
to: this.fakeUserEmail,
|
2021-03-31 05:24:39 -04:00
|
|
|
}
|
|
|
|
)
|
2021-08-17 09:08:54 -04:00
|
|
|
expect(this.UserUpdater.promises.updateUser).to.have.been.calledWith(
|
2021-03-31 05:24:39 -04:00
|
|
|
this.fakeUserId,
|
2021-08-17 09:08:54 -04:00
|
|
|
{ $set: { onboardingEmailSentAt: sinon.match.date } }
|
2021-03-31 05:24:39 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-08-17 09:08:54 -04:00
|
|
|
it('should stop if user is not found', async function () {
|
|
|
|
await this.UserOnboardingEmailManager.sendOnboardingEmail({
|
|
|
|
data: { userId: 'deleted-user' },
|
|
|
|
})
|
|
|
|
expect(this.EmailHandler.promises.sendEmail).not.to.have.been.called
|
|
|
|
expect(this.UserUpdater.promises.updateUser).not.to.have.been.called
|
2021-03-31 05:24:39 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|