2022-08-18 18:38:42 +03:00
|
|
|
// Switch these lines once there are useful utils
|
|
|
|
// const testUtils = require('./utils');
|
|
|
|
require('./utils');
|
|
|
|
const {MemberCreatedEvent, SubscriptionCreatedEvent} = require('@tryghost/member-events');
|
2022-09-07 17:41:59 +03:00
|
|
|
const EventStorage = require('../lib/event-storage');
|
2022-08-18 18:38:42 +03:00
|
|
|
|
2022-09-07 17:41:59 +03:00
|
|
|
describe('EventStorage', function () {
|
2022-08-18 18:38:42 +03:00
|
|
|
describe('Constructor', function () {
|
|
|
|
it('doesn\'t throw', function () {
|
2022-09-07 17:41:59 +03:00
|
|
|
new EventStorage({});
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('MemberCreatedEvent handling', function () {
|
|
|
|
it('defaults to external for missing attributions', function () {
|
|
|
|
const DomainEvents = {
|
|
|
|
subscribe: (type, handler) => {
|
|
|
|
if (type === MemberCreatedEvent) {
|
|
|
|
handler(MemberCreatedEvent.create({
|
|
|
|
memberId: '123',
|
|
|
|
source: 'test'
|
|
|
|
}, new Date(0)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MemberCreatedEventModel = {add: sinon.stub()};
|
|
|
|
const labsService = {isSet: sinon.stub().returns(true)};
|
|
|
|
const subscribeSpy = sinon.spy(DomainEvents, 'subscribe');
|
2022-09-07 17:41:59 +03:00
|
|
|
const eventHandler = new EventStorage({
|
2022-08-22 12:36:24 +03:00
|
|
|
models: {MemberCreatedEvent: MemberCreatedEventModel},
|
2022-08-18 18:38:42 +03:00
|
|
|
labsService
|
|
|
|
});
|
2022-09-07 17:41:59 +03:00
|
|
|
eventHandler.subscribe(DomainEvents);
|
2022-08-18 18:38:42 +03:00
|
|
|
sinon.assert.calledOnceWithMatch(MemberCreatedEventModel.add, {
|
|
|
|
member_id: '123',
|
|
|
|
created_at: new Date(0),
|
|
|
|
source: 'test'
|
|
|
|
});
|
|
|
|
sinon.assert.calledTwice(subscribeSpy);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes custom attributions', function () {
|
|
|
|
const DomainEvents = {
|
|
|
|
subscribe: (type, handler) => {
|
|
|
|
if (type === MemberCreatedEvent) {
|
|
|
|
handler(MemberCreatedEvent.create({
|
|
|
|
memberId: '123',
|
|
|
|
source: 'test',
|
|
|
|
attribution: {
|
|
|
|
id: '123',
|
|
|
|
type: 'post',
|
|
|
|
url: 'url'
|
|
|
|
}
|
|
|
|
}, new Date(0)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MemberCreatedEventModel = {add: sinon.stub()};
|
|
|
|
const labsService = {isSet: sinon.stub().returns(true)};
|
|
|
|
const subscribeSpy = sinon.spy(DomainEvents, 'subscribe');
|
2022-09-07 17:41:59 +03:00
|
|
|
const eventHandler = new EventStorage({
|
2022-08-22 12:36:24 +03:00
|
|
|
models: {MemberCreatedEvent: MemberCreatedEventModel},
|
2022-08-18 18:38:42 +03:00
|
|
|
labsService
|
|
|
|
});
|
2022-09-07 17:41:59 +03:00
|
|
|
eventHandler.subscribe(DomainEvents);
|
2022-08-18 18:38:42 +03:00
|
|
|
sinon.assert.calledOnceWithMatch(MemberCreatedEventModel.add, {
|
|
|
|
member_id: '123',
|
|
|
|
created_at: new Date(0),
|
|
|
|
attribution_id: '123',
|
|
|
|
attribution_type: 'post',
|
|
|
|
attribution_url: 'url',
|
|
|
|
source: 'test'
|
|
|
|
});
|
|
|
|
sinon.assert.calledTwice(subscribeSpy);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('filters if disabled', function () {
|
|
|
|
const DomainEvents = {
|
|
|
|
subscribe: (type, handler) => {
|
|
|
|
if (type === MemberCreatedEvent) {
|
|
|
|
handler(MemberCreatedEvent.create({
|
|
|
|
memberId: '123',
|
|
|
|
source: 'test',
|
|
|
|
attribution: {
|
|
|
|
id: '123',
|
|
|
|
type: 'post',
|
|
|
|
url: 'url'
|
|
|
|
}
|
|
|
|
}, new Date(0)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MemberCreatedEventModel = {add: sinon.stub()};
|
|
|
|
const labsService = {isSet: sinon.stub().returns(false)};
|
|
|
|
const subscribeSpy = sinon.spy(DomainEvents, 'subscribe');
|
2022-09-07 17:41:59 +03:00
|
|
|
const eventHandler = new EventStorage({
|
2022-08-22 12:36:24 +03:00
|
|
|
models: {MemberCreatedEvent: MemberCreatedEventModel},
|
2022-08-18 18:38:42 +03:00
|
|
|
labsService
|
|
|
|
});
|
2022-09-07 17:41:59 +03:00
|
|
|
eventHandler.subscribe(DomainEvents);
|
2022-08-18 18:38:42 +03:00
|
|
|
sinon.assert.calledOnceWithMatch(MemberCreatedEventModel.add, {
|
|
|
|
member_id: '123',
|
|
|
|
created_at: new Date(0),
|
|
|
|
attribution_id: null,
|
|
|
|
attribution_type: null,
|
|
|
|
attribution_url: null,
|
|
|
|
source: 'test'
|
|
|
|
});
|
|
|
|
sinon.assert.calledTwice(subscribeSpy);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SubscriptionCreatedEvent handling', function () {
|
|
|
|
it('defaults to external for missing attributions', function () {
|
|
|
|
const DomainEvents = {
|
|
|
|
subscribe: (type, handler) => {
|
|
|
|
if (type === SubscriptionCreatedEvent) {
|
|
|
|
handler(SubscriptionCreatedEvent.create({
|
|
|
|
memberId: '123',
|
|
|
|
subscriptionId: '456'
|
|
|
|
}, new Date(0)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const SubscriptionCreatedEventModel = {add: sinon.stub()};
|
|
|
|
const labsService = {isSet: sinon.stub().returns(true)};
|
|
|
|
const subscribeSpy = sinon.spy(DomainEvents, 'subscribe');
|
2022-09-07 17:41:59 +03:00
|
|
|
const eventHandler = new EventStorage({
|
2022-08-22 12:36:24 +03:00
|
|
|
models: {SubscriptionCreatedEvent: SubscriptionCreatedEventModel},
|
2022-08-18 18:38:42 +03:00
|
|
|
labsService
|
|
|
|
});
|
2022-09-07 17:41:59 +03:00
|
|
|
eventHandler.subscribe(DomainEvents);
|
2022-08-18 18:38:42 +03:00
|
|
|
sinon.assert.calledOnceWithMatch(SubscriptionCreatedEventModel.add, {
|
|
|
|
member_id: '123',
|
|
|
|
subscription_id: '456',
|
|
|
|
created_at: new Date(0)
|
|
|
|
});
|
|
|
|
sinon.assert.calledTwice(subscribeSpy);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes custom attributions', function () {
|
|
|
|
const DomainEvents = {
|
|
|
|
subscribe: (type, handler) => {
|
|
|
|
if (type === SubscriptionCreatedEvent) {
|
|
|
|
handler(SubscriptionCreatedEvent.create({
|
|
|
|
memberId: '123',
|
|
|
|
subscriptionId: '456',
|
|
|
|
attribution: {
|
|
|
|
id: '123',
|
|
|
|
type: 'post',
|
|
|
|
url: 'url'
|
|
|
|
}
|
|
|
|
}, new Date(0)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const SubscriptionCreatedEventModel = {add: sinon.stub()};
|
|
|
|
const labsService = {isSet: sinon.stub().returns(true)};
|
|
|
|
const subscribeSpy = sinon.spy(DomainEvents, 'subscribe');
|
2022-09-07 17:41:59 +03:00
|
|
|
const eventHandler = new EventStorage({
|
2022-08-22 12:36:24 +03:00
|
|
|
models: {SubscriptionCreatedEvent: SubscriptionCreatedEventModel},
|
2022-08-18 18:38:42 +03:00
|
|
|
labsService
|
|
|
|
});
|
2022-09-07 17:41:59 +03:00
|
|
|
eventHandler.subscribe(DomainEvents);
|
2022-08-18 18:38:42 +03:00
|
|
|
sinon.assert.calledOnceWithMatch(SubscriptionCreatedEventModel.add, {
|
|
|
|
member_id: '123',
|
|
|
|
subscription_id: '456',
|
|
|
|
created_at: new Date(0),
|
|
|
|
attribution_id: '123',
|
|
|
|
attribution_type: 'post',
|
|
|
|
attribution_url: 'url'
|
|
|
|
});
|
|
|
|
sinon.assert.calledTwice(subscribeSpy);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('filters if disabled', function () {
|
|
|
|
const DomainEvents = {
|
|
|
|
subscribe: (type, handler) => {
|
|
|
|
if (type === SubscriptionCreatedEvent) {
|
|
|
|
handler(SubscriptionCreatedEvent.create({
|
|
|
|
memberId: '123',
|
|
|
|
subscriptionId: '456',
|
|
|
|
attribution: {
|
|
|
|
id: '123',
|
|
|
|
type: 'post',
|
|
|
|
url: 'url'
|
|
|
|
}
|
|
|
|
}, new Date(0)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const SubscriptionCreatedEventModel = {add: sinon.stub()};
|
|
|
|
const labsService = {isSet: sinon.stub().returns(false)};
|
|
|
|
const subscribeSpy = sinon.spy(DomainEvents, 'subscribe');
|
2022-09-07 17:41:59 +03:00
|
|
|
const eventHandler = new EventStorage({
|
2022-08-22 12:36:24 +03:00
|
|
|
models: {SubscriptionCreatedEvent: SubscriptionCreatedEventModel},
|
2022-08-18 18:38:42 +03:00
|
|
|
labsService
|
|
|
|
});
|
2022-09-07 17:41:59 +03:00
|
|
|
eventHandler.subscribe(DomainEvents);
|
2022-08-18 18:38:42 +03:00
|
|
|
sinon.assert.calledOnceWithMatch(SubscriptionCreatedEventModel.add, {
|
|
|
|
member_id: '123',
|
|
|
|
subscription_id: '456',
|
|
|
|
created_at: new Date(0),
|
|
|
|
attribution_id: null,
|
|
|
|
attribution_type: null,
|
|
|
|
attribution_url: null
|
|
|
|
});
|
|
|
|
sinon.assert.calledTwice(subscribeSpy);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|