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-07-01 07:42:30 -04:00
|
|
|
getOnboardingEmailsQueue: sinon
|
|
|
|
.stub()
|
|
|
|
.returns(this.onboardingEmailsQueue),
|
2021-03-31 05:24:39 -04:00
|
|
|
}
|
|
|
|
this.UserGetter = {
|
|
|
|
promises: {
|
|
|
|
getUser: sinon.stub().resolves({
|
|
|
|
_id: this.fakeUserId,
|
2021-04-27 03:52:58 -04:00
|
|
|
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
|
|
|
this.Features = {
|
|
|
|
hasFeature: sinon.stub(),
|
|
|
|
}
|
2021-03-31 05:24:39 -04:00
|
|
|
this.request = sinon.stub().yields()
|
2021-07-01 07:42:30 -04:00
|
|
|
|
|
|
|
this.init = isSAAS => {
|
|
|
|
this.Features.hasFeature.withArgs('saas').returns(isSAAS)
|
|
|
|
this.UserOnboardingEmailManager = SandboxedModule.require(MODULE_PATH, {
|
|
|
|
globals: {
|
|
|
|
console: console,
|
|
|
|
},
|
|
|
|
requires: {
|
|
|
|
'../../infrastructure/Features': this.Features,
|
|
|
|
'../../infrastructure/Queues': this.Queues,
|
|
|
|
'../Email/EmailHandler': this.EmailHandler,
|
|
|
|
'./UserGetter': this.UserGetter,
|
|
|
|
'./UserUpdater': this.UserUpdater,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('in Server CE/Pro', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.init(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not create any queue', function () {
|
|
|
|
expect(this.Queues.getOnboardingEmailsQueue).to.not.have.been.called
|
|
|
|
})
|
|
|
|
it('should not schedule any email', function () {
|
|
|
|
this.UserOnboardingEmailManager.scheduleOnboardingEmail({
|
|
|
|
_id: this.fakeUserId,
|
|
|
|
})
|
|
|
|
expect(this.onboardingEmailsQueue.add).to.not.have.been.called
|
2021-03-31 05:24:39 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-07-01 07:42:30 -04:00
|
|
|
describe('schedule email in SAAS', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.init(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should schedule delayed job on queue', function () {
|
2021-03-31 05:24:39 -04:00
|
|
|
this.UserOnboardingEmailManager.scheduleOnboardingEmail({
|
2021-04-27 03:52:58 -04:00
|
|
|
_id: this.fakeUserId,
|
2021-03-31 05:24:39 -04:00
|
|
|
})
|
|
|
|
sinon.assert.calledWith(
|
|
|
|
this.onboardingEmailsQueue.add,
|
|
|
|
{ userId: this.fakeUserId },
|
|
|
|
{ delay: 24 * 60 * 60 * 1000 }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('queue process callback should send onboarding email and update user', async function () {
|
2021-03-31 05:24:39 -04:00
|
|
|
await this.queueProcessFunction({ data: { userId: this.fakeUserId } })
|
|
|
|
sinon.assert.calledWith(
|
|
|
|
this.UserGetter.promises.getUser,
|
|
|
|
{ _id: this.fakeUserId },
|
|
|
|
{ email: 1 }
|
|
|
|
)
|
|
|
|
sinon.assert.calledWith(
|
|
|
|
this.EmailHandler.promises.sendEmail,
|
|
|
|
'userOnboardingEmail',
|
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
to: this.fakeUserEmail,
|
2021-03-31 05:24:39 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
sinon.assert.calledWith(
|
|
|
|
this.UserUpdater.promises.updateUser,
|
|
|
|
this.fakeUserId,
|
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
$set: { onboardingEmailSentAt: sinon.match.date },
|
2021-03-31 05:24:39 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('queue process callback should stop if user is not found', async function () {
|
2021-03-31 05:24:39 -04:00
|
|
|
this.UserGetter.promises.getUser = sinon.stub().resolves()
|
|
|
|
await this.queueProcessFunction({ data: { userId: 'deleted-user' } })
|
|
|
|
sinon.assert.calledWith(
|
|
|
|
this.UserGetter.promises.getUser,
|
|
|
|
{ _id: 'deleted-user' },
|
|
|
|
{ email: 1 }
|
|
|
|
)
|
|
|
|
sinon.assert.notCalled(this.EmailHandler.promises.sendEmail)
|
|
|
|
sinon.assert.notCalled(this.UserUpdater.promises.updateUser)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|