2019-08-28 08:59:41 -04:00
|
|
|
const { expect } = require('chai')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
|
const MODULE_PATH = '../../../../app/src/Features/Newsletter/NewsletterManager'
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('NewsletterManager', function () {
|
|
|
|
beforeEach('setup mocks', function () {
|
2019-08-28 08:59:41 -04:00
|
|
|
this.Settings = {
|
|
|
|
mailchimp: {
|
|
|
|
api_key: 'api_key',
|
2021-04-27 03:52:58 -04:00
|
|
|
list_id: 'list_id',
|
|
|
|
},
|
2019-08-28 08:59:41 -04:00
|
|
|
}
|
|
|
|
this.mailchimp = {
|
2022-04-27 04:42:39 -04:00
|
|
|
get: sinon.stub(),
|
2019-09-25 10:27:38 -04:00
|
|
|
put: sinon.stub(),
|
|
|
|
patch: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
delete: sinon.stub(),
|
2019-08-28 08:59:41 -04:00
|
|
|
}
|
|
|
|
this.Mailchimp = sinon.stub().returns(this.mailchimp)
|
|
|
|
|
2019-09-25 10:27:38 -04:00
|
|
|
this.mergeFields = {
|
|
|
|
FNAME: 'Overleaf',
|
|
|
|
LNAME: 'Duck',
|
2021-04-27 03:52:58 -04:00
|
|
|
MONGO_ID: 'user_id',
|
2019-09-25 10:27:38 -04:00
|
|
|
}
|
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
this.NewsletterManager = SandboxedModule.require(MODULE_PATH, {
|
|
|
|
requires: {
|
|
|
|
'mailchimp-api-v3': this.Mailchimp,
|
2021-07-07 05:38:56 -04:00
|
|
|
'@overleaf/settings': this.Settings,
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-08-28 08:59:41 -04:00
|
|
|
}).promises
|
|
|
|
|
|
|
|
this.user = {
|
|
|
|
_id: 'user_id',
|
|
|
|
email: 'overleaf.duck@example.com',
|
|
|
|
first_name: 'Overleaf',
|
2021-04-27 03:52:58 -04:00
|
|
|
last_name: 'Duck',
|
2019-08-28 08:59:41 -04:00
|
|
|
}
|
|
|
|
// MD5 sum of the user email
|
|
|
|
this.emailHash = 'c02f60ed0ef51818186274e406c9a48f'
|
|
|
|
})
|
|
|
|
|
2022-04-27 04:42:39 -04:00
|
|
|
describe('subscribed', function () {
|
|
|
|
it('calls Mailchimp to get the user status', async function () {
|
|
|
|
await this.NewsletterManager.subscribed(this.user)
|
|
|
|
expect(this.mailchimp.get).to.have.been.calledWith(
|
|
|
|
`/lists/list_id/members/${this.emailHash}`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns true when subscribed', async function () {
|
|
|
|
this.mailchimp.get.resolves({ status: 'subscribed' })
|
|
|
|
|
|
|
|
const subscribed = await this.NewsletterManager.subscribed(this.user)
|
|
|
|
expect(subscribed).to.be.true
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns false on 404', async function () {
|
|
|
|
const err = new Error()
|
|
|
|
err.status = 404
|
|
|
|
this.mailchimp.get.rejects(err)
|
|
|
|
const subscribed = await this.NewsletterManager.subscribed(this.user)
|
|
|
|
expect(subscribed).to.be.false
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('subscribe', function () {
|
|
|
|
it('calls Mailchimp to subscribe the user', async function () {
|
2019-08-28 08:59:41 -04:00
|
|
|
await this.NewsletterManager.subscribe(this.user)
|
|
|
|
expect(this.mailchimp.put).to.have.been.calledWith(
|
|
|
|
`/lists/list_id/members/${this.emailHash}`,
|
|
|
|
{
|
|
|
|
email_address: this.user.email,
|
|
|
|
status: 'subscribed',
|
|
|
|
status_if_new: 'subscribed',
|
2021-04-27 03:52:58 -04:00
|
|
|
merge_fields: this.mergeFields,
|
2019-08-28 08:59:41 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('unsubscribe', function () {
|
|
|
|
describe('when unsubscribing normally', function () {
|
|
|
|
it('calls Mailchimp to unsubscribe the user', async function () {
|
2019-09-25 10:27:38 -04:00
|
|
|
await this.NewsletterManager.unsubscribe(this.user)
|
|
|
|
expect(this.mailchimp.patch).to.have.been.calledWith(
|
|
|
|
`/lists/list_id/members/${this.emailHash}`,
|
|
|
|
{
|
|
|
|
status: 'unsubscribed',
|
2021-04-27 03:52:58 -04:00
|
|
|
merge_fields: this.mergeFields,
|
2019-08-28 08:59:41 -04:00
|
|
|
}
|
2019-09-25 10:27:38 -04:00
|
|
|
)
|
|
|
|
})
|
2019-08-28 08:59:41 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('ignores a Mailchimp error about fake emails', async function () {
|
2019-09-25 10:27:38 -04:00
|
|
|
this.mailchimp.patch.rejects(
|
|
|
|
new Error(
|
|
|
|
'overleaf.duck@example.com looks fake or invalid, please enter a real email address'
|
|
|
|
)
|
2019-08-28 08:59:41 -04:00
|
|
|
)
|
2019-09-25 10:27:38 -04:00
|
|
|
await expect(this.NewsletterManager.unsubscribe(this.user)).to.be
|
|
|
|
.fulfilled
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('rejects on other errors', async function () {
|
2019-09-25 10:27:38 -04:00
|
|
|
this.mailchimp.patch.rejects(
|
|
|
|
new Error('something really wrong is happening')
|
|
|
|
)
|
|
|
|
await expect(this.NewsletterManager.unsubscribe(this.user)).to.be
|
|
|
|
.rejected
|
|
|
|
})
|
2019-08-28 08:59:41 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when deleting', function () {
|
|
|
|
it('calls Mailchimp to delete the user', async function () {
|
2019-09-25 10:27:38 -04:00
|
|
|
await this.NewsletterManager.unsubscribe(this.user, { delete: true })
|
|
|
|
expect(this.mailchimp.delete).to.have.been.calledWith(
|
|
|
|
`/lists/list_id/members/${this.emailHash}`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('ignores a Mailchimp error about fake emails', async function () {
|
2019-09-25 10:27:38 -04:00
|
|
|
this.mailchimp.delete.rejects(
|
|
|
|
new Error(
|
|
|
|
'overleaf.duck@example.com looks fake or invalid, please enter a real email address'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
await expect(
|
|
|
|
this.NewsletterManager.unsubscribe(this.user, { delete: true })
|
|
|
|
).to.be.fulfilled
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('rejects on other errors', async function () {
|
2019-09-25 10:27:38 -04:00
|
|
|
this.mailchimp.delete.rejects(
|
|
|
|
new Error('something really wrong is happening')
|
|
|
|
)
|
|
|
|
await expect(
|
|
|
|
this.NewsletterManager.unsubscribe(this.user, { delete: true })
|
|
|
|
).to.be.rejected
|
|
|
|
})
|
2019-08-28 08:59:41 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('changeEmail', function () {
|
|
|
|
it('calls Mailchimp to change the subscriber email', async function () {
|
2019-08-28 08:59:41 -04:00
|
|
|
await this.NewsletterManager.changeEmail(
|
2019-09-25 10:27:38 -04:00
|
|
|
this.user,
|
2019-08-28 08:59:41 -04:00
|
|
|
'overleaf.squirrel@example.com'
|
|
|
|
)
|
2019-09-25 10:27:38 -04:00
|
|
|
expect(this.mailchimp.patch).to.have.been.calledWith(
|
2019-08-28 08:59:41 -04:00
|
|
|
`/lists/list_id/members/${this.emailHash}`,
|
|
|
|
{
|
|
|
|
email_address: 'overleaf.squirrel@example.com',
|
2021-04-27 03:52:58 -04:00
|
|
|
merge_fields: this.mergeFields,
|
2019-08-28 08:59:41 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2019-09-25 10:27:38 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('deletes the old email if changing the address fails', async function () {
|
2019-09-25 10:27:38 -04:00
|
|
|
this.mailchimp.patch
|
|
|
|
.withArgs(`/lists/list_id/members/${this.emailHash}`, {
|
|
|
|
email_address: 'overleaf.squirrel@example.com',
|
2021-04-27 03:52:58 -04:00
|
|
|
merge_fields: this.mergeFields,
|
2019-09-25 10:27:38 -04:00
|
|
|
})
|
|
|
|
.rejects(new Error('that did not work'))
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
this.NewsletterManager.changeEmail(
|
|
|
|
this.user,
|
|
|
|
'overleaf.squirrel@example.com'
|
|
|
|
)
|
|
|
|
).to.be.rejected
|
|
|
|
|
|
|
|
expect(this.mailchimp.delete).to.have.been.calledWith(
|
|
|
|
`/lists/list_id/members/${this.emailHash}`
|
|
|
|
)
|
|
|
|
})
|
2020-08-11 05:28:29 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('does not reject on non-fatal error ', async function () {
|
2020-08-11 05:28:29 -04:00
|
|
|
const nonFatalError = new Error('merge fields were invalid')
|
|
|
|
this.mailchimp.patch.rejects(nonFatalError)
|
|
|
|
await expect(
|
|
|
|
this.NewsletterManager.changeEmail(
|
|
|
|
this.user,
|
|
|
|
'overleaf.squirrel@example.com'
|
|
|
|
)
|
|
|
|
).to.be.fulfilled
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('rejects on any other error', async function () {
|
2020-08-11 05:28:29 -04:00
|
|
|
const fatalError = new Error('fatal error')
|
|
|
|
this.mailchimp.patch.rejects(fatalError)
|
|
|
|
await expect(
|
|
|
|
this.NewsletterManager.changeEmail(
|
|
|
|
this.user,
|
|
|
|
'overleaf.squirrel@example.com'
|
|
|
|
)
|
|
|
|
).to.be.rejected
|
|
|
|
})
|
2019-08-28 08:59:41 -04:00
|
|
|
})
|
|
|
|
})
|