Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions src/EventProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const ContextFilter = require('./ContextFilter');
const errors = require('./errors');
const messages = require('./messages');
const utils = require('./utils');
const { getContextKeys } = require('./context');

function EventProcessor(
platform,
Expand Down Expand Up @@ -47,27 +46,17 @@ function EventProcessor(
// Transform an event from its internal format to the format we use when sending a payload.
function makeOutputEvent(e) {
const ret = utils.extend({}, e);
if (e.kind === 'identify') {
// identify events always have an inline context
ret.context = contextFilter.filter(e.context);
} else if (e.kind === 'feature') {
// feature events always have an inline context
ret.context = contextFilter.filter(e.context, true);
} else {
ret.contextKeys = getContextKeysFromEvent(e);
delete ret['context'];
}

// This method is used for identify, feature, and custom events, which always have an inline context.
ret.context = contextFilter.filter(e.context);

if (e.kind === 'feature') {
delete ret['trackEvents'];
delete ret['debugEventsUntilDate'];
}
return ret;
}

function getContextKeysFromEvent(event) {
return getContextKeys(event.context, logger);
}

function addToOutbox(event) {
if (queue.length < eventCapacity) {
queue.push(event);
Expand Down
58 changes: 47 additions & 11 deletions src/__tests__/EventProcessor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,6 @@ describe.each([
}
}

function checkUserInline(e, source, inlineUser) {
if (inlineUser) {
expect(e.context).toEqual(inlineUser);
expect(e.contextKeys).toBeUndefined();
} else {
expect(e.contextKeys).toEqual({ user: source.context.key || source.context.user.key });
expect(e.context).toBeUndefined();
}
}

function checkFeatureEvent(e, source, debug, inlineUser) {
expect(e.kind).toEqual(debug ? 'debug' : 'feature');
expect(e.creationDate).toEqual(source.creationDate);
Expand All @@ -87,7 +77,7 @@ describe.each([
expect(e.key).toEqual(source.key);
expect(e.data).toEqual(source.data);
expect(e.metricValue).toEqual(source.metricValue);
checkUserInline(e, source);
expect(e.context).toEqual(source.context);
}

function checkSummaryEvent(e) {
Expand Down Expand Up @@ -219,6 +209,31 @@ describe.each([
});
});

it('filters context in feature event', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't directly related to changes, but this test didn't exist before.

const config = { ...defaultConfig, allAttributesPrivate: true };
await withProcessorAndSender(config, async (ep, mockEventSender) => {
const e = {
kind: 'feature',
creationDate: 1000,
context: eventContext,
key: 'flagkey',
version: 11,
variation: 1,
value: 'value',
default: 'default',
trackEvents: true,
};
ep.enqueue(e);
await ep.flush();

expect(mockEventSender.calls.length()).toEqual(1);
const output = (await mockEventSender.calls.take()).events;
expect(output.length).toEqual(2);
checkFeatureEvent(output[0], e, false, filteredContext);
checkSummaryEvent(output[1]);
});
});

it('can both track and debug an event', async () => {
await withProcessorAndSender(defaultConfig, async (ep, mockEventSender) => {
const futureTime = new Date().getTime() + 1000000;
Expand Down Expand Up @@ -380,6 +395,27 @@ describe.each([
});
});

it('filters context in custom event', async () => {
const config = { ...defaultConfig, allAttributesPrivate: true };
await withProcessorAndSender(config, async (ep, mockEventSender) => {
const e = {
kind: 'custom',
creationDate: 1000,
context: eventContext,
key: 'eventkey',
data: { thing: 'stuff' },
metricValue: 1.5,
};
ep.enqueue(e);
await ep.flush();

expect(mockEventSender.calls.length()).toEqual(1);
const output = (await mockEventSender.calls.take()).events;
expect(output.length).toEqual(1);
checkCustomEvent(output[0], { ...e, context: filteredContext });
});
});

it('enforces event capacity', async () => {
const config = { ...defaultConfig, eventCapacity: 1, logger: stubPlatform.logger() };
const e0 = { kind: 'custom', creationDate: 1000, context: eventContext, key: 'key0' };
Expand Down