|
1 | 1 | import { clientContext, ContextDeduplicator } from '@launchdarkly/private-js-mocks'; |
2 | 2 |
|
| 3 | +import { LDContextCommon, LDMultiKindContext } from '../../../dist'; |
3 | 4 | import { Context } from '../../../src'; |
4 | 5 | import { LDContextDeduplicator, LDDeliveryStatus, LDEventType } from '../../../src/api/subsystem'; |
5 | 6 | import { EventProcessor, InputIdentifyEvent } from '../../../src/internal'; |
@@ -344,6 +345,92 @@ describe('given an event processor', () => { |
344 | 345 | ]); |
345 | 346 | }); |
346 | 347 |
|
| 348 | + it('redacts all attributes from anonymous single-kind context for feature events', async () => { |
| 349 | + const userObj = { key: 'user-key', kind: 'user', name: 'Example user', anonymous: true }; |
| 350 | + const context = Context.fromLDContext(userObj); |
| 351 | + |
| 352 | + Date.now = jest.fn(() => 1000); |
| 353 | + eventProcessor.sendEvent({ |
| 354 | + kind: 'feature', |
| 355 | + creationDate: 1000, |
| 356 | + context, |
| 357 | + key: 'flagkey', |
| 358 | + version: 11, |
| 359 | + variation: 1, |
| 360 | + value: 'value', |
| 361 | + trackEvents: true, |
| 362 | + default: 'default', |
| 363 | + samplingRatio: 1, |
| 364 | + withReasons: true, |
| 365 | + }); |
| 366 | + |
| 367 | + await eventProcessor.flush(); |
| 368 | + |
| 369 | + const redactedContext = { |
| 370 | + kind: 'user', |
| 371 | + key: 'user-key', |
| 372 | + anonymous: true, |
| 373 | + _meta: { |
| 374 | + redactedAttributes: ['name'], |
| 375 | + }, |
| 376 | + }; |
| 377 | + |
| 378 | + const expectedIndexEvent = { ...testIndexEvent, context: userObj }; |
| 379 | + const expectedFeatureEvent = { ...makeFeatureEvent(1000, 11, false), context: redactedContext }; |
| 380 | + |
| 381 | + expect(mockSendEventData).toBeCalledWith(LDEventType.AnalyticsEvents, [ |
| 382 | + expectedIndexEvent, |
| 383 | + expectedFeatureEvent, |
| 384 | + makeSummary(1000, 1000, 1, 11), |
| 385 | + ]); |
| 386 | + }); |
| 387 | + |
| 388 | + it('redacts all attributes from anonymous multi-kind context for feature events', async () => { |
| 389 | + const userObj: LDContextCommon = { key: 'user-key', name: 'Example user', anonymous: true }; |
| 390 | + const org: LDContextCommon = { key: 'org-key', name: 'Example org' }; |
| 391 | + const multi: LDMultiKindContext = { kind: 'multi', user: userObj, org }; |
| 392 | + const context = Context.fromLDContext(multi); |
| 393 | + |
| 394 | + Date.now = jest.fn(() => 1000); |
| 395 | + eventProcessor.sendEvent({ |
| 396 | + kind: 'feature', |
| 397 | + creationDate: 1000, |
| 398 | + context, |
| 399 | + key: 'flagkey', |
| 400 | + version: 11, |
| 401 | + variation: 1, |
| 402 | + value: 'value', |
| 403 | + trackEvents: true, |
| 404 | + default: 'default', |
| 405 | + samplingRatio: 1, |
| 406 | + withReasons: true, |
| 407 | + }); |
| 408 | + |
| 409 | + await eventProcessor.flush(); |
| 410 | + |
| 411 | + const redactedUserContext = { |
| 412 | + key: 'user-key', |
| 413 | + anonymous: true, |
| 414 | + _meta: { |
| 415 | + redactedAttributes: ['name'], |
| 416 | + }, |
| 417 | + }; |
| 418 | + |
| 419 | + const expectedIndexEvent = { ...testIndexEvent, context: multi }; |
| 420 | + const expectedFeatureEvent = { |
| 421 | + ...makeFeatureEvent(1000, 11, false), |
| 422 | + context: { ...multi, user: redactedUserContext }, |
| 423 | + }; |
| 424 | + const expectedSummaryEvent = makeSummary(1000, 1000, 1, 11); |
| 425 | + expectedSummaryEvent.features.flagkey.contextKinds = ['user', 'org']; |
| 426 | + |
| 427 | + expect(mockSendEventData).toBeCalledWith(LDEventType.AnalyticsEvents, [ |
| 428 | + expectedIndexEvent, |
| 429 | + expectedFeatureEvent, |
| 430 | + expectedSummaryEvent, |
| 431 | + ]); |
| 432 | + }); |
| 433 | + |
347 | 434 | it('expires debug mode based on client time if client time is later than server time', async () => { |
348 | 435 | Date.now = jest.fn(() => 2000); |
349 | 436 |
|
|
0 commit comments