Skip to content

Commit

Permalink
👌 [RUMF-998] add a few tests to ensure the setting is correctly applied
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Aug 20, 2021
1 parent 279e300 commit 7c0a10a
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 7 deletions.
30 changes: 28 additions & 2 deletions packages/rum-core/src/boot/rumPublicApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ONE_SECOND, RelativeTime, getTimeStamp, display, TimeStamp } from '@datadog/browser-core'
import { ONE_SECOND, RelativeTime, getTimeStamp, display, TimeStamp, InitialPrivacyLevel } from '@datadog/browser-core'
import { noopRecorderApi, setup, TestSetupBuilder } from '../../test/specHelper'
import { ActionType } from '../rawRumEvent.types'
import { makeRumPublicApi, RumPublicApi, RumInitConfiguration, StartRum } from './rumPublicApi'
import { makeRumPublicApi, RumPublicApi, RumInitConfiguration, StartRum, RecorderApi } from './rumPublicApi'

const noopStartRum = (): ReturnType<StartRum> => ({
addAction: () => undefined,
Expand Down Expand Up @@ -652,4 +652,30 @@ describe('rum public api', () => {
})
})
})

describe('recording', () => {
let recorderApiOnRumStartSpy: jasmine.Spy<RecorderApi['onRumStart']>
let setupBuilder: TestSetupBuilder
let rumPublicApi: RumPublicApi

beforeEach(() => {
recorderApiOnRumStartSpy = jasmine.createSpy('recorderApiOnRumStart')
rumPublicApi = makeRumPublicApi(noopStartRum, { ...noopRecorderApi, onRumStart: recorderApiOnRumStartSpy })
setupBuilder = setup()
})

afterEach(() => {
setupBuilder.cleanup()
})

it('recording is started with the default initialPrivacyLevel', () => {
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
expect(recorderApiOnRumStartSpy.calls.mostRecent().args[2].initialPrivacyLevel).toBe(InitialPrivacyLevel.ALLOW)
})

it('recording is started with the configured initialPrivacyLevel', () => {
rumPublicApi.init({ ...DEFAULT_INIT_CONFIGURATION, initialPrivacyLevel: InitialPrivacyLevel.MASK })
expect(recorderApiOnRumStartSpy.calls.mostRecent().args[2].initialPrivacyLevel).toBe(InitialPrivacyLevel.MASK)
})
})
})
61 changes: 59 additions & 2 deletions packages/rum/src/domain/record/mutationObserver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ describe('startMutationCollection', () => {
let sandbox: HTMLElement
let stopMutationCollection: () => void

function startMutationCollection() {
function startMutationCollection(initialPrivacyLevel: InitialPrivacyLevel = InitialPrivacyLevel.ALLOW) {
const mutationCallbackSpy = jasmine.createSpy<MutationCallBack>()
const mutationController = new MutationController()

;({ stop: stopMutationCollection } = startMutationObserver(
mutationController,
mutationCallbackSpy,
InitialPrivacyLevel.ALLOW
initialPrivacyLevel
))

return {
Expand Down Expand Up @@ -400,6 +400,27 @@ describe('startMutationCollection', () => {
],
})
})

it('respects the initial privacy level setting', () => {
const serializedDocument = serializeDocument(document, NodePrivacyLevel.ALLOW)
const { mutationController, getLatestMutationPayload } = startMutationCollection(InitialPrivacyLevel.MASK)

sandbox.innerText = 'foo bar'
mutationController.flush()

const { validate, expectNewNode, expectInitialNode } = createMutationPayloadValidator(serializedDocument)
validate(getLatestMutationPayload(), {
adds: [
{
parent: expectInitialNode({ idAttribute: 'sandbox' }),
node: expectNewNode({
type: NodeType.Text,
textContent: '᙮᙮᙮ ᙮᙮᙮',
}),
},
],
})
})
})

describe('characterData mutations', () => {
Expand Down Expand Up @@ -440,6 +461,24 @@ describe('startMutationCollection', () => {

expect(mutationCallbackSpy).not.toHaveBeenCalled()
})

it('respects the initial privacy level setting', () => {
const serializedDocument = serializeDocument(document, NodePrivacyLevel.ALLOW)
const { mutationController, getLatestMutationPayload } = startMutationCollection(InitialPrivacyLevel.MASK)

textNode.data = 'foo bar'
mutationController.flush()

const { validate, expectInitialNode } = createMutationPayloadValidator(serializedDocument)
validate(getLatestMutationPayload(), {
texts: [
{
node: expectInitialNode({ text: 'foo' }),
value: '᙮᙮᙮ ᙮᙮᙮',
},
],
})
})
})

describe('attributes mutations', () => {
Expand Down Expand Up @@ -493,6 +532,24 @@ describe('startMutationCollection', () => {
],
})
})

it('respects the initial privacy level setting', () => {
const serializedDocument = serializeDocument(document, NodePrivacyLevel.ALLOW)
const { mutationController, getLatestMutationPayload } = startMutationCollection(InitialPrivacyLevel.MASK)

sandbox.setAttribute('data-foo', 'biz')
mutationController.flush()

const { validate, expectInitialNode } = createMutationPayloadValidator(serializedDocument)
validate(getLatestMutationPayload(), {
attributes: [
{
node: expectInitialNode({ idAttribute: 'sandbox' }),
attributes: { 'data-foo': '***' },
},
],
})
})
})

describe('ignored nodes', () => {
Expand Down
16 changes: 13 additions & 3 deletions packages/rum/src/domain/record/observer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('initInputObserver', () => {
pending('IE not supported')
}
inputCallbackSpy = jasmine.createSpy()
stopInputObserver = initInputObserver(inputCallbackSpy, InitialPrivacyLevel.ALLOW)

sandbox = document.createElement('div')
input = document.createElement('input')
Expand All @@ -32,6 +31,7 @@ describe('initInputObserver', () => {
})

it('collects input values when an "input" event is dispatched', () => {
stopInputObserver = initInputObserver(inputCallbackSpy, InitialPrivacyLevel.ALLOW)
dispatchInputEvent('foo')

expect(inputCallbackSpy).toHaveBeenCalledOnceWith({
Expand All @@ -40,22 +40,32 @@ describe('initInputObserver', () => {
})
})

it('masks input values according to the element privacy mode', () => {
it('masks input values according to the element privacy level', () => {
stopInputObserver = initInputObserver(inputCallbackSpy, InitialPrivacyLevel.ALLOW)
sandbox.setAttribute(PRIVACY_ATTR_NAME, PRIVACY_ATTR_VALUE_INPUT_MASKED)

dispatchInputEvent('foo')

expect((inputCallbackSpy.calls.first().args[0] as { text?: string }).text).toBe('***')
})

it('masks input values according to a parent element privacy mode', () => {
it('masks input values according to a parent element privacy level', () => {
stopInputObserver = initInputObserver(inputCallbackSpy, InitialPrivacyLevel.ALLOW)
input.setAttribute(PRIVACY_ATTR_NAME, PRIVACY_ATTR_VALUE_INPUT_MASKED)

dispatchInputEvent('foo')

expect((inputCallbackSpy.calls.first().args[0] as { text?: string }).text).toBe('***')
})

it('masks input values according to a the initial privacy level', () => {
stopInputObserver = initInputObserver(inputCallbackSpy, InitialPrivacyLevel.MASK)

dispatchInputEvent('foo')

expect((inputCallbackSpy.calls.first().args[0] as { text?: string }).text).toBe('***')
})

function dispatchInputEvent(newValue: string) {
input.value = newValue
input.dispatchEvent(createNewEvent('input', { target: input }))
Expand Down

0 comments on commit 7c0a10a

Please sign in to comment.