mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-20 09:22:49 +03:00
b911208b41
fixes https://github.com/TryGhost/Team/issues/2129 - This changes how the activity feed API parses the filter. - We now parse the filter early to a MongoDB filter, and split it in two. One of the filters is applied to the pageActions, and the other one is used individually for every event type. We now allow to use grouping and OR's inside the filters because of this change. As long as we don't combine filters on 'type' with other filters inside grouped filters or OR, then it is allowed. - We make use of mongoTransformer to manually inject a mongo filter without needing to parse it from a string value again (that would make it a lot harder because we would have to convert the splitted filter back to a string and we currently don't have methods for that). - Added sorting by id for events with the same timestamp (required for reliable pagination) - Added id to each event (required for pagination) - Added more tests for filters - Added test for pagination - Removed unsued getSubscriptions and getVolume methods Used new mongo utility methods introduced here: https://github.com/TryGhost/NQL/pull/49
245 lines
8.4 KiB
JavaScript
245 lines
8.4 KiB
JavaScript
const should = require('should');
|
|
const EventRepository = require('../../../../lib/repositories/event');
|
|
const sinon = require('sinon');
|
|
const errors = require('@tryghost/errors');
|
|
|
|
describe('EventRepository', function () {
|
|
describe('getNQLSubset', function () {
|
|
let eventRepository;
|
|
|
|
before(function () {
|
|
eventRepository = new EventRepository({
|
|
EmailRecipient: null,
|
|
MemberSubscribeEvent: null,
|
|
MemberPaymentEvent: null,
|
|
MemberStatusEvent: null,
|
|
MemberLoginEvent: null,
|
|
MemberPaidSubscriptionEvent: null,
|
|
labsService: null
|
|
});
|
|
});
|
|
|
|
it('throws when using properties that aren\'t in the allowlist', function () {
|
|
should.throws(() => {
|
|
eventRepository.getNQLSubset('(types:1)');
|
|
}, errors.IncorrectUsageError);
|
|
});
|
|
|
|
it('throws when using an OR', function () {
|
|
should.throws(() => {
|
|
eventRepository.getNQLSubset('type:1,data.created_at:1');
|
|
}, errors.IncorrectUsageError);
|
|
|
|
should.throws(() => {
|
|
eventRepository.getNQLSubset('type:1+data.created_at:1,data.member_id:1');
|
|
}, errors.IncorrectUsageError);
|
|
|
|
should.throws(() => {
|
|
eventRepository.getNQLSubset('type:1,data.created_at:1+data.member_id:1');
|
|
}, errors.IncorrectUsageError);
|
|
});
|
|
|
|
it('passes when using it correctly with one filter', function () {
|
|
const res = eventRepository.getNQLSubset('type:email_delivered_event');
|
|
res.should.be.an.Array();
|
|
res.should.have.lengthOf(2);
|
|
|
|
res[0].should.eql({
|
|
type: 'email_delivered_event'
|
|
});
|
|
should(res[1]).be.undefined();
|
|
});
|
|
|
|
it('passes when using it correctly with multiple filters', function () {
|
|
const res = eventRepository.getNQLSubset('type:-[email_delivered_event,email_opened_event,email_failed_event]+data.created_at:<0+data.member_id:123');
|
|
res.should.be.an.Array();
|
|
res.should.have.lengthOf(2);
|
|
|
|
res[0].should.eql({
|
|
type: {
|
|
$nin: [
|
|
'email_delivered_event',
|
|
'email_opened_event',
|
|
'email_failed_event'
|
|
]
|
|
}
|
|
});
|
|
res[1].should.eql({
|
|
$and: [{
|
|
'data.created_at': {
|
|
$lt: 0
|
|
}
|
|
}, {
|
|
'data.member_id': 123
|
|
}]
|
|
});
|
|
});
|
|
|
|
it('passes when using it correctly with multiple filters used several times', function () {
|
|
const res = eventRepository.getNQLSubset('type:-email_delivered_event+data.created_at:<0+data.member_id:123+type:-[email_opened_event,email_failed_event]+data.created_at:>10');
|
|
res.should.be.an.Array();
|
|
res.should.have.lengthOf(2);
|
|
res[0].should.eql({
|
|
$and: [{
|
|
type: {
|
|
$ne: 'email_delivered_event'
|
|
}
|
|
}, {
|
|
type: {
|
|
$nin: [
|
|
'email_opened_event',
|
|
'email_failed_event'
|
|
]
|
|
}
|
|
}]
|
|
});
|
|
res[1].should.eql({
|
|
$and: [{
|
|
'data.created_at': {
|
|
$lt: 0
|
|
}
|
|
}, {
|
|
'data.member_id': 123
|
|
}, {
|
|
'data.created_at': {
|
|
$gt: 10
|
|
}
|
|
}]
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('getNewsletterSubscriptionEvents', function () {
|
|
let eventRepository;
|
|
let fake;
|
|
|
|
before(function () {
|
|
fake = sinon.fake.returns({data: [{toJSON: () => {}}]});
|
|
eventRepository = new EventRepository({
|
|
EmailRecipient: null,
|
|
MemberSubscribeEvent: {
|
|
findPage: fake
|
|
},
|
|
MemberPaymentEvent: null,
|
|
MemberStatusEvent: null,
|
|
MemberLoginEvent: null,
|
|
MemberPaidSubscriptionEvent: null,
|
|
labsService: null
|
|
});
|
|
});
|
|
|
|
afterEach(function () {
|
|
fake.resetHistory();
|
|
});
|
|
|
|
it('works when setting no filters', async function () {
|
|
await eventRepository.getNewsletterSubscriptionEvents({
|
|
filter: 'no used'
|
|
}, {
|
|
type: 'unused'
|
|
});
|
|
sinon.assert.calledOnce(fake);
|
|
fake.getCall(0).firstArg.should.match({
|
|
withRelated: ['member', 'newsletter'],
|
|
filter: 'custom:true'
|
|
});
|
|
});
|
|
|
|
it('works when setting a created_at filter', async function () {
|
|
await eventRepository.getNewsletterSubscriptionEvents({}, {
|
|
'data.created_at': 'data.created_at:123'
|
|
});
|
|
|
|
sinon.assert.calledOnce(fake);
|
|
fake.getCall(0).firstArg.should.match({
|
|
withRelated: ['member', 'newsletter'],
|
|
filter: 'custom:true'
|
|
});
|
|
});
|
|
|
|
it('works when setting a combination of filters', async function () {
|
|
await eventRepository.getNewsletterSubscriptionEvents({}, {
|
|
'data.created_at': 'data.created_at:123+data.created_at:<99999',
|
|
'data.member_id': 'data.member_id:-[3,4,5]+data.member_id:-[1,2,3]'
|
|
});
|
|
|
|
sinon.assert.calledOnce(fake);
|
|
fake.getCall(0).firstArg.should.match({
|
|
withRelated: ['member', 'newsletter'],
|
|
filter: 'custom:true'
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('getEmailFailedEvents', function () {
|
|
let eventRepository;
|
|
let fake;
|
|
|
|
before(function () {
|
|
fake = sinon.fake.returns({data: [{get: () => {}, related: () => ({toJSON: () => {}})}]});
|
|
eventRepository = new EventRepository({
|
|
EmailRecipient: {
|
|
findPage: fake
|
|
},
|
|
MemberSubscribeEvent: null,
|
|
MemberPaymentEvent: null,
|
|
MemberStatusEvent: null,
|
|
MemberLoginEvent: null,
|
|
MemberPaidSubscriptionEvent: null,
|
|
labsService: null
|
|
});
|
|
});
|
|
|
|
afterEach(function () {
|
|
fake.resetHistory();
|
|
});
|
|
|
|
it('works when setting no filters', async function () {
|
|
await eventRepository.getEmailFailedEvents({
|
|
filter: 'no used',
|
|
order: 'created_at desc, id desc'
|
|
}, {
|
|
type: 'unused'
|
|
});
|
|
|
|
fake.calledOnce.should.be.eql(true);
|
|
fake.getCall(0).firstArg.should.match({
|
|
withRelated: ['member', 'email'],
|
|
filter: 'failed_at:-null+custom:true',
|
|
order: 'failed_at desc, id desc'
|
|
});
|
|
});
|
|
|
|
it('works when setting a created_at filter', async function () {
|
|
await eventRepository.getEmailDeliveredEvents({
|
|
order: 'created_at desc, id desc'
|
|
}, {
|
|
'data.created_at': 'data.created_at:123'
|
|
});
|
|
|
|
fake.calledOnce.should.be.eql(true);
|
|
fake.getCall(0).firstArg.should.match({
|
|
withRelated: ['member', 'email'],
|
|
filter: 'delivered_at:-null+custom:true',
|
|
order: 'delivered_at desc, id desc'
|
|
});
|
|
});
|
|
|
|
it('works when setting a combination of filters', async function () {
|
|
await eventRepository.getEmailOpenedEvents({
|
|
order: 'created_at desc, id desc'
|
|
}, {
|
|
'data.created_at': 'data.created_at:123+data.created_at:<99999',
|
|
'data.member_id': 'data.member_id:-[3,4,5]+data.member_id:-[1,2,3]'
|
|
});
|
|
|
|
fake.calledOnce.should.be.eql(true);
|
|
fake.getCall(0).firstArg.should.match({
|
|
withRelated: ['member', 'email'],
|
|
filter: 'opened_at:-null+custom:true',
|
|
order: 'opened_at desc, id desc'
|
|
});
|
|
});
|
|
});
|
|
});
|