2023-05-02 23:43:47 +03:00
|
|
|
const EmailEventStorage = require('../lib/EmailEventStorage');
|
2022-12-02 12:21:06 +03:00
|
|
|
const {EmailDeliveredEvent, EmailOpenedEvent, EmailBouncedEvent, EmailTemporaryBouncedEvent, EmailUnsubscribedEvent, SpamComplaintEvent} = require('@tryghost/email-events');
|
|
|
|
const sinon = require('sinon');
|
2023-06-21 11:56:59 +03:00
|
|
|
const assert = require('assert/strict');
|
2023-01-10 18:36:41 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
|
|
|
const {createDb} = require('./utils');
|
|
|
|
|
|
|
|
describe('Email Event Storage', function () {
|
|
|
|
let logError;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
logError = sinon.stub(logging, 'error');
|
2023-02-09 11:36:39 +03:00
|
|
|
sinon.stub(logging, 'info');
|
2023-01-10 18:36:41 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
2022-12-02 12:21:06 +03:00
|
|
|
|
|
|
|
describe('Constructor', function () {
|
|
|
|
it('doesn\'t throw', function () {
|
|
|
|
new EmailEventStorage({});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Handles email delivered events', async function () {
|
2023-02-09 11:36:39 +03:00
|
|
|
const event = EmailDeliveredEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
emailRecipientId: '789',
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
2022-12-02 12:21:06 +03:00
|
|
|
|
2023-01-10 18:36:41 +03:00
|
|
|
const db = createDb();
|
2022-12-02 12:21:06 +03:00
|
|
|
const eventHandler = new EmailEventStorage({db});
|
2023-02-09 11:36:39 +03:00
|
|
|
await eventHandler.handleDelivered(event);
|
2022-12-02 12:21:06 +03:00
|
|
|
sinon.assert.calledOnce(db.update);
|
|
|
|
assert(!!db.update.firstCall.args[0].delivered_at);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Handles email opened events', async function () {
|
2023-02-09 11:36:39 +03:00
|
|
|
const event = EmailOpenedEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
emailRecipientId: '789',
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
2022-12-02 12:21:06 +03:00
|
|
|
|
2023-01-10 18:36:41 +03:00
|
|
|
const db = createDb();
|
2022-12-02 12:21:06 +03:00
|
|
|
const eventHandler = new EmailEventStorage({db});
|
2023-02-09 11:36:39 +03:00
|
|
|
await eventHandler.handleOpened(event);
|
2022-12-02 12:21:06 +03:00
|
|
|
sinon.assert.calledOnce(db.update);
|
|
|
|
assert(!!db.update.firstCall.args[0].opened_at);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Handles email permanent bounce events with update', async function () {
|
2023-02-09 11:36:39 +03:00
|
|
|
const event = EmailBouncedEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
emailRecipientId: '789',
|
|
|
|
error: {
|
|
|
|
message: 'test',
|
|
|
|
code: 500,
|
|
|
|
enhancedCode: '5.5.5'
|
|
|
|
},
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
2022-12-02 12:21:06 +03:00
|
|
|
|
2023-01-10 18:36:41 +03:00
|
|
|
const db = createDb();
|
2022-12-02 12:21:06 +03:00
|
|
|
const existing = {
|
|
|
|
id: 1,
|
|
|
|
get: (key) => {
|
|
|
|
if (key === 'severity') {
|
|
|
|
return 'temporary';
|
|
|
|
}
|
|
|
|
if (key === 'failed_at') {
|
|
|
|
return new Date(-5);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
save: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
const EmailRecipientFailure = {
|
|
|
|
transaction: async function (callback) {
|
|
|
|
return await callback(1);
|
|
|
|
},
|
|
|
|
findOne: sinon.stub().resolves(existing)
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
db,
|
|
|
|
models: {
|
|
|
|
EmailRecipientFailure
|
|
|
|
}
|
|
|
|
});
|
2023-02-09 11:36:39 +03:00
|
|
|
await eventHandler.handlePermanentFailed(event);
|
2022-12-02 12:21:06 +03:00
|
|
|
sinon.assert.calledOnce(db.update);
|
|
|
|
assert(!!db.update.firstCall.args[0].failed_at);
|
|
|
|
assert(existing.save.calledOnce);
|
|
|
|
});
|
|
|
|
|
2023-02-13 17:25:36 +03:00
|
|
|
it('Handles email permanent bounce events with update and empty message', async function () {
|
|
|
|
const event = EmailBouncedEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
emailRecipientId: '789',
|
|
|
|
error: {
|
|
|
|
message: '',
|
|
|
|
code: 500,
|
|
|
|
enhancedCode: '5.5.5'
|
|
|
|
},
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
|
|
|
|
|
|
|
const db = createDb();
|
|
|
|
const existing = {
|
|
|
|
id: 1,
|
|
|
|
get: (key) => {
|
|
|
|
if (key === 'severity') {
|
|
|
|
return 'temporary';
|
|
|
|
}
|
|
|
|
if (key === 'failed_at') {
|
|
|
|
return new Date(-5);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
save: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
const EmailRecipientFailure = {
|
|
|
|
transaction: async function (callback) {
|
|
|
|
return await callback(1);
|
|
|
|
},
|
|
|
|
findOne: sinon.stub().resolves(existing)
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
db,
|
|
|
|
models: {
|
|
|
|
EmailRecipientFailure
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await eventHandler.handlePermanentFailed(event);
|
|
|
|
sinon.assert.calledOnce(db.update);
|
|
|
|
assert(!!db.update.firstCall.args[0].failed_at);
|
|
|
|
assert(existing.save.calledOnce);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Handles email permanent bounce events with update and empty message and without enhanced code', async function () {
|
|
|
|
const event = EmailBouncedEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
emailRecipientId: '789',
|
|
|
|
error: {
|
|
|
|
message: '',
|
|
|
|
code: 500
|
|
|
|
},
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
|
|
|
|
|
|
|
const db = createDb();
|
|
|
|
const existing = {
|
|
|
|
id: 1,
|
|
|
|
get: (key) => {
|
|
|
|
if (key === 'severity') {
|
|
|
|
return 'temporary';
|
|
|
|
}
|
|
|
|
if (key === 'failed_at') {
|
|
|
|
return new Date(-5);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
save: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
const EmailRecipientFailure = {
|
|
|
|
transaction: async function (callback) {
|
|
|
|
return await callback(1);
|
|
|
|
},
|
|
|
|
findOne: sinon.stub().resolves(existing)
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
db,
|
|
|
|
models: {
|
|
|
|
EmailRecipientFailure
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await eventHandler.handlePermanentFailed(event);
|
|
|
|
sinon.assert.calledOnce(db.update);
|
|
|
|
assert(!!db.update.firstCall.args[0].failed_at);
|
|
|
|
assert(existing.save.calledOnce);
|
|
|
|
});
|
|
|
|
|
2022-12-02 12:21:06 +03:00
|
|
|
it('Handles email permanent bounce events with insert', async function () {
|
2023-02-09 11:36:39 +03:00
|
|
|
const event = EmailBouncedEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
emailRecipientId: '789',
|
|
|
|
error: {
|
|
|
|
message: 'test',
|
|
|
|
code: 500,
|
|
|
|
enhancedCode: '5.5.5'
|
|
|
|
},
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
2022-12-02 12:21:06 +03:00
|
|
|
|
2023-01-10 18:36:41 +03:00
|
|
|
const db = createDb();
|
2022-12-02 12:21:06 +03:00
|
|
|
const EmailRecipientFailure = {
|
|
|
|
transaction: async function (callback) {
|
|
|
|
return await callback(1);
|
|
|
|
},
|
|
|
|
findOne: sinon.stub().resolves(undefined),
|
|
|
|
add: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
|
2023-02-13 17:25:36 +03:00
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
db,
|
|
|
|
models: {
|
|
|
|
EmailRecipientFailure
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await eventHandler.handlePermanentFailed(event);
|
|
|
|
sinon.assert.calledOnce(db.update);
|
|
|
|
assert(!!db.update.firstCall.args[0].failed_at);
|
|
|
|
assert(EmailRecipientFailure.add.calledOnce);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Handles email permanent bounce events with insert and empty message', async function () {
|
|
|
|
const event = EmailBouncedEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
emailRecipientId: '789',
|
|
|
|
error: {
|
|
|
|
message: '',
|
|
|
|
code: 500,
|
|
|
|
enhancedCode: '5.5.5'
|
|
|
|
},
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
|
|
|
|
|
|
|
const db = createDb();
|
|
|
|
const EmailRecipientFailure = {
|
|
|
|
transaction: async function (callback) {
|
|
|
|
return await callback(1);
|
|
|
|
},
|
|
|
|
findOne: sinon.stub().resolves(undefined),
|
|
|
|
add: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
db,
|
|
|
|
models: {
|
|
|
|
EmailRecipientFailure
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await eventHandler.handlePermanentFailed(event);
|
|
|
|
sinon.assert.calledOnce(db.update);
|
|
|
|
assert(!!db.update.firstCall.args[0].failed_at);
|
|
|
|
assert(EmailRecipientFailure.add.calledOnce);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Handles email permanent bounce events with insert and empty message and without enhanced code', async function () {
|
|
|
|
const event = EmailBouncedEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
emailRecipientId: '789',
|
|
|
|
error: {
|
|
|
|
message: '',
|
|
|
|
code: 500
|
|
|
|
},
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
|
|
|
|
|
|
|
const db = createDb();
|
|
|
|
const EmailRecipientFailure = {
|
|
|
|
transaction: async function (callback) {
|
|
|
|
return await callback(1);
|
|
|
|
},
|
|
|
|
findOne: sinon.stub().resolves(undefined),
|
|
|
|
add: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
|
2022-12-02 12:21:06 +03:00
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
db,
|
|
|
|
models: {
|
|
|
|
EmailRecipientFailure
|
|
|
|
}
|
|
|
|
});
|
2023-02-09 11:36:39 +03:00
|
|
|
await eventHandler.handlePermanentFailed(event);
|
2022-12-02 12:21:06 +03:00
|
|
|
sinon.assert.calledOnce(db.update);
|
|
|
|
assert(!!db.update.firstCall.args[0].failed_at);
|
|
|
|
assert(EmailRecipientFailure.add.calledOnce);
|
|
|
|
});
|
|
|
|
|
2023-01-10 18:36:41 +03:00
|
|
|
it('Handles email permanent bounce event without error data', async function () {
|
2023-02-09 11:36:39 +03:00
|
|
|
const event = EmailBouncedEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
emailRecipientId: '789',
|
|
|
|
error: null,
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
2023-01-10 18:36:41 +03:00
|
|
|
|
|
|
|
const db = createDb();
|
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
db,
|
|
|
|
models: {}
|
|
|
|
});
|
2023-02-09 11:36:39 +03:00
|
|
|
await eventHandler.handlePermanentFailed(event);
|
2023-01-10 18:36:41 +03:00
|
|
|
sinon.assert.calledOnce(db.update);
|
|
|
|
});
|
|
|
|
|
2022-12-02 12:21:06 +03:00
|
|
|
it('Handles email permanent bounce events with skipped update', async function () {
|
2023-02-09 11:36:39 +03:00
|
|
|
const event = EmailBouncedEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
emailRecipientId: '789',
|
|
|
|
error: {
|
|
|
|
message: 'test',
|
|
|
|
code: 500,
|
|
|
|
enhancedCode: '5.5.5'
|
|
|
|
},
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
2022-12-02 12:21:06 +03:00
|
|
|
|
2023-01-10 18:36:41 +03:00
|
|
|
const db = createDb();
|
2022-12-02 12:21:06 +03:00
|
|
|
const existing = {
|
|
|
|
id: 1,
|
|
|
|
get: (key) => {
|
|
|
|
if (key === 'severity') {
|
|
|
|
return 'permanent';
|
|
|
|
}
|
|
|
|
if (key === 'failed_at') {
|
|
|
|
return new Date(-5);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
save: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
const EmailRecipientFailure = {
|
|
|
|
transaction: async function (callback) {
|
|
|
|
return await callback(1);
|
|
|
|
},
|
|
|
|
findOne: sinon.stub().resolves(existing)
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
db,
|
|
|
|
models: {
|
|
|
|
EmailRecipientFailure
|
|
|
|
}
|
|
|
|
});
|
2023-02-09 11:36:39 +03:00
|
|
|
await eventHandler.handlePermanentFailed(event);
|
2022-12-02 12:21:06 +03:00
|
|
|
sinon.assert.calledOnce(db.update);
|
|
|
|
assert(!!db.update.firstCall.args[0].failed_at);
|
|
|
|
assert(EmailRecipientFailure.findOne.called);
|
|
|
|
assert(!existing.save.called);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Handles email temporary bounce events with update', async function () {
|
2023-02-09 11:36:39 +03:00
|
|
|
const event = EmailTemporaryBouncedEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
emailRecipientId: '789',
|
|
|
|
error: {
|
|
|
|
message: 'test',
|
|
|
|
code: 500,
|
|
|
|
enhancedCode: null
|
|
|
|
},
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
2022-12-02 12:21:06 +03:00
|
|
|
|
|
|
|
const existing = {
|
|
|
|
id: 1,
|
|
|
|
get: (key) => {
|
|
|
|
if (key === 'severity') {
|
|
|
|
return 'temporary';
|
|
|
|
}
|
|
|
|
if (key === 'failed_at') {
|
|
|
|
return new Date(-5);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
save: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
const EmailRecipientFailure = {
|
|
|
|
transaction: async function (callback) {
|
|
|
|
return await callback(1);
|
|
|
|
},
|
|
|
|
findOne: sinon.stub().resolves(existing)
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
models: {
|
|
|
|
EmailRecipientFailure
|
|
|
|
}
|
|
|
|
});
|
2023-02-09 11:36:39 +03:00
|
|
|
await eventHandler.handleTemporaryFailed(event);
|
2022-12-02 12:21:06 +03:00
|
|
|
assert(existing.save.calledOnce);
|
|
|
|
});
|
|
|
|
|
2023-01-10 18:36:41 +03:00
|
|
|
it('Handles email temporary bounce events with skipped update', async function () {
|
2023-02-09 11:36:39 +03:00
|
|
|
const event = EmailTemporaryBouncedEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
emailRecipientId: '789',
|
|
|
|
error: {
|
|
|
|
message: 'test',
|
|
|
|
code: 500,
|
|
|
|
enhancedCode: '5.5.5'
|
|
|
|
},
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
2023-01-10 18:36:41 +03:00
|
|
|
|
|
|
|
const existing = {
|
|
|
|
id: 1,
|
|
|
|
get: (key) => {
|
|
|
|
if (key === 'severity') {
|
|
|
|
return 'temporary';
|
|
|
|
}
|
|
|
|
if (key === 'failed_at') {
|
|
|
|
return new Date(5);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
save: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
const EmailRecipientFailure = {
|
|
|
|
transaction: async function (callback) {
|
|
|
|
return await callback(1);
|
|
|
|
},
|
|
|
|
findOne: sinon.stub().resolves(existing)
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
models: {
|
|
|
|
EmailRecipientFailure
|
|
|
|
}
|
|
|
|
});
|
2023-02-09 11:36:39 +03:00
|
|
|
await eventHandler.handleTemporaryFailed(event);
|
2023-01-10 18:36:41 +03:00
|
|
|
assert(existing.save.notCalled);
|
|
|
|
});
|
|
|
|
|
2022-12-02 12:21:06 +03:00
|
|
|
it('Handles unsubscribe', async function () {
|
2023-02-09 11:36:39 +03:00
|
|
|
const event = EmailUnsubscribedEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
2022-12-02 12:21:06 +03:00
|
|
|
|
|
|
|
const update = sinon.stub().resolves();
|
|
|
|
|
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
membersRepository: {
|
|
|
|
update
|
|
|
|
}
|
|
|
|
});
|
2023-02-09 11:36:39 +03:00
|
|
|
await eventHandler.handleUnsubscribed(event);
|
2022-12-02 12:21:06 +03:00
|
|
|
assert(update.calledOnce);
|
2023-04-11 23:13:34 +03:00
|
|
|
assert(update.firstCall.args[0].newsletters.length === 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Handles unsubscribe with a non-existent member', async function () {
|
|
|
|
const event = EmailUnsubscribedEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
|
|
|
|
|
|
|
const error = new Error('Member not found');
|
|
|
|
const update = sinon.stub().throws(error);
|
|
|
|
|
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
membersRepository: {
|
|
|
|
update
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await eventHandler.handleUnsubscribed(event);
|
|
|
|
assert(update.calledOnce);
|
2022-12-02 12:21:06 +03:00
|
|
|
assert(update.firstCall.args[0].newsletters.length === 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Handles complaints', async function () {
|
2023-02-09 11:36:39 +03:00
|
|
|
const event = SpamComplaintEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
2022-12-02 12:21:06 +03:00
|
|
|
|
|
|
|
const EmailSpamComplaintEvent = {
|
|
|
|
add: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
models: {
|
|
|
|
EmailSpamComplaintEvent
|
|
|
|
}
|
|
|
|
});
|
2023-02-09 11:36:39 +03:00
|
|
|
await eventHandler.handleComplained(event);
|
2022-12-02 12:21:06 +03:00
|
|
|
assert(EmailSpamComplaintEvent.add.calledOnce);
|
|
|
|
});
|
2023-01-10 18:36:41 +03:00
|
|
|
|
|
|
|
it('Handles duplicate complaints', async function () {
|
2023-02-09 11:36:39 +03:00
|
|
|
const event = SpamComplaintEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
2023-01-10 18:36:41 +03:00
|
|
|
|
|
|
|
const EmailSpamComplaintEvent = {
|
|
|
|
add: sinon.stub().rejects({code: 'ER_DUP_ENTRY'})
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
models: {
|
|
|
|
EmailSpamComplaintEvent
|
|
|
|
}
|
|
|
|
});
|
2023-02-09 11:36:39 +03:00
|
|
|
await eventHandler.handleComplained(event);
|
2023-01-10 18:36:41 +03:00
|
|
|
assert(EmailSpamComplaintEvent.add.calledOnce);
|
|
|
|
assert(!logError.calledOnce);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Handles logging failed complaint storage', async function () {
|
2023-02-09 11:36:39 +03:00
|
|
|
const event = SpamComplaintEvent.create({
|
|
|
|
email: 'example@example.com',
|
|
|
|
memberId: '123',
|
|
|
|
emailId: '456',
|
|
|
|
timestamp: new Date(0)
|
|
|
|
});
|
2023-01-10 18:36:41 +03:00
|
|
|
|
|
|
|
const EmailSpamComplaintEvent = {
|
|
|
|
add: sinon.stub().rejects(new Error('Some database error'))
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventHandler = new EmailEventStorage({
|
|
|
|
models: {
|
|
|
|
EmailSpamComplaintEvent
|
|
|
|
}
|
|
|
|
});
|
2023-02-09 11:36:39 +03:00
|
|
|
await eventHandler.handleComplained(event);
|
2023-01-10 18:36:41 +03:00
|
|
|
assert(EmailSpamComplaintEvent.add.calledOnce);
|
|
|
|
assert(logError.calledOnce);
|
|
|
|
});
|
2022-12-02 12:21:06 +03:00
|
|
|
});
|