Skip to content

Commit e2afc18

Browse files
committed
🧪 add e2e test for false positive allowed extension
1 parent 4f4006e commit e2afc18

File tree

12 files changed

+189
-84
lines changed

12 files changed

+189
-84
lines changed

‎scripts/build/build-test-apps.ts‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { printLog, runMain } from '../lib/executionUtils.ts'
55
import { command } from '../lib/command.ts'
66
import { modifyFile } from '../lib/filesUtils.ts'
77

8+
const OTHER_EXTENSIONS: Array<{ name: string; options?: { runAt?: string } }> = [
9+
{ name: 'cdn' },
10+
{ name: 'appendChild', options: { runAt: 'document_start' } },
11+
]
12+
813
runMain(async () => {
914
printLog('Packing packages...')
1015
command`yarn lerna run pack`.run()
@@ -56,14 +61,20 @@ async function buildExtensions(): Promise<void> {
5661

5762
buildApp(baseExtDir)
5863

59-
const cdnExtDir = 'test/apps/cdn-extension'
60-
fs.rmSync(cdnExtDir, { recursive: true, force: true })
61-
fs.cpSync(baseExtDir, cdnExtDir, { recursive: true })
64+
for (const { name, options } of OTHER_EXTENSIONS) {
65+
const targetDir = path.join('test/apps', `${name}-extension`)
6266

63-
const manifestPath = path.join(cdnExtDir, 'manifest.json')
64-
await modifyFile(manifestPath, (content: string) =>
65-
content.replace('dist/npm-content-script.js', 'dist/cdn-content-script.js')
66-
)
67+
fs.rmSync(targetDir, { recursive: true, force: true })
68+
fs.cpSync(baseExtDir, targetDir, { recursive: true })
69+
70+
const manifestPath = path.join(targetDir, 'manifest.json')
71+
await modifyFile(manifestPath, (originalContent: string) => {
72+
let content = originalContent.replace('dist/base.js', `dist/${name}.js`)
6773

68-
buildApp(cdnExtDir)
74+
if (options?.runAt) {
75+
content = content.replace('document_end', options.runAt)
76+
}
77+
return content
78+
})
79+
}
6980
}

‎test/apps/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ allowed-tracking-origin/
22
invalid-tracking-origin/
33
react-router-v7-app/
44
cdn-extension/
5+
appendChild-extension/

‎test/apps/base-extension/manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"content_scripts": [
99
{
1010
"matches": ["<all_urls>"],
11-
"js": ["dist/npm-content-script.js"],
11+
"js": ["dist/base.js"],
1212
"run_at": "document_end",
1313
"world": "MAIN"
1414
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const originalAppendChild = Node.prototype.appendChild // eslint-disable-line @typescript-eslint/unbound-method
2+
3+
Node.prototype.appendChild = function <T extends Node>(node: T): T {
4+
return originalAppendChild.call(this, node) as T
5+
}

test/apps/base-extension/src/npmContentScript.ts renamed to test/apps/base-extension/src/base.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ import type { Context } from '@datadog/browser-core'
66

77
declare global {
88
interface Window {
9-
RUM_CONFIGURATION?: RumInitConfiguration
9+
EXT_RUM_CONFIGURATION?: RumInitConfiguration
1010
RUM_CONTEXT?: Context
11-
LOGS_CONFIGURATION?: LogsInitConfiguration
11+
EXT_LOGS_CONFIGURATION?: LogsInitConfiguration
1212
LOGS_CONTEXT?: Context
1313
}
1414
}
1515

16-
if (window.RUM_CONFIGURATION) {
17-
datadogRum.init(window.RUM_CONFIGURATION)
16+
if (window.EXT_RUM_CONFIGURATION) {
17+
datadogRum.init(window.EXT_RUM_CONFIGURATION)
1818

1919
if (window.RUM_CONTEXT) {
2020
datadogRum.setGlobalContext(window.RUM_CONTEXT)
2121
}
2222
}
2323

24-
if (window.LOGS_CONFIGURATION) {
25-
datadogLogs.init(window.LOGS_CONFIGURATION)
24+
if (window.EXT_LOGS_CONFIGURATION) {
25+
datadogLogs.init(window.EXT_LOGS_CONFIGURATION)
2626

2727
if (window.LOGS_CONTEXT) {
2828
datadogLogs.setGlobalContext(window.LOGS_CONTEXT)

test/apps/base-extension/src/cdnContentScript.ts renamed to test/apps/base-extension/src/cdn.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ declare global {
66
interface Window {
77
RUM_BUNDLE_URL?: string
88
LOGS_BUNDLE_URL?: string
9-
RUM_CONFIGURATION?: RumInitConfiguration
9+
EXT_RUM_CONFIGURATION?: RumInitConfiguration
1010
RUM_CONTEXT?: Context
11-
LOGS_CONFIGURATION?: LogsInitConfiguration
11+
EXT_LOGS_CONFIGURATION?: LogsInitConfiguration
1212
LOGS_CONTEXT?: Context
1313
DD_RUM?: RumPublicApi
1414
DD_LOGS?: LogsGlobal
@@ -38,10 +38,10 @@ function load<T extends 'DD_RUM' | 'DD_LOGS'>(
3838
document.documentElement.appendChild(script)
3939
}
4040

41-
if (window.RUM_BUNDLE_URL && window.RUM_CONFIGURATION) {
42-
load('DD_RUM', window.RUM_BUNDLE_URL, window.RUM_CONFIGURATION, window.RUM_CONTEXT)
41+
if (window.RUM_BUNDLE_URL && window.EXT_RUM_CONFIGURATION) {
42+
load('DD_RUM', window.RUM_BUNDLE_URL, window.EXT_RUM_CONFIGURATION, window.RUM_CONTEXT)
4343
}
4444

45-
if (window.LOGS_BUNDLE_URL && window.LOGS_CONFIGURATION) {
46-
load('DD_LOGS', window.LOGS_BUNDLE_URL, window.LOGS_CONFIGURATION, window.LOGS_CONTEXT)
45+
if (window.LOGS_BUNDLE_URL && window.EXT_LOGS_CONFIGURATION) {
46+
load('DD_LOGS', window.LOGS_BUNDLE_URL, window.EXT_LOGS_CONFIGURATION, window.LOGS_CONTEXT)
4747
}

‎test/apps/base-extension/webpack.config.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ module.exports = {
44
mode: 'production',
55
target: ['web', 'es2018'],
66
entry: {
7-
'npm-content-script': './src/npmContentScript.ts',
8-
'cdn-content-script': './src/cdnContentScript.ts',
7+
base: './src/base.ts',
8+
cdn: './src/cdn.ts',
9+
appendChild: './src/appendChild.ts',
910
},
1011
module: {
1112
rules: [
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import path from 'path'
2+
import type { RumInitConfiguration } from '@datadog/browser-rum-core'
3+
import type test from '@playwright/test'
4+
import type { LogsInitConfiguration } from '@datadog/browser-logs'
5+
import { createExtensionTest } from '../helpers/extensionFixture'
6+
import { DEFAULT_LOGS_CONFIGURATION, DEFAULT_RUM_CONFIGURATION } from './createTest'
7+
8+
export function createExtension(name: string) {
9+
return new Extension(name)
10+
}
11+
12+
// TODO: the recorder is lazy loaded and does not works in an browser extension content script
13+
const DISABLE_SESSION_REPLAY_CONFIGURATION = { sessionReplaySampleRate: 0 }
14+
15+
export class Extension {
16+
public fixture: typeof test
17+
public rumConfiguration: RumInitConfiguration | undefined
18+
public logsConfiguration: LogsInitConfiguration | undefined
19+
20+
constructor(name: string) {
21+
this.fixture = createExtensionTest(path.join(__dirname, '../../../../test/apps/', `${name}-extension`))
22+
23+
return this
24+
}
25+
26+
withRum(rumInitConfiguration: Partial<RumInitConfiguration> = {}) {
27+
this.rumConfiguration = {
28+
...DEFAULT_RUM_CONFIGURATION,
29+
...DISABLE_SESSION_REPLAY_CONFIGURATION,
30+
...rumInitConfiguration,
31+
}
32+
33+
return this
34+
}
35+
36+
withLogs(logsInitConfiguration: Partial<LogsInitConfiguration> = {}) {
37+
this.logsConfiguration = {
38+
...DEFAULT_LOGS_CONFIGURATION,
39+
...DISABLE_SESSION_REPLAY_CONFIGURATION,
40+
...logsInitConfiguration,
41+
}
42+
43+
return this
44+
}
45+
}

‎test/e2e/lib/framework/createTest.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { RumInitConfiguration, RemoteConfiguration } from '@datadog/browser
33
import { DefaultPrivacyLevel } from '@datadog/browser-rum'
44
import type { BrowserContext, Page } from '@playwright/test'
55
import { test, expect } from '@playwright/test'
6-
import { createExtensionTest } from '../helpers/extensionFixture'
76
import { addTag, addTestOptimizationTags } from '../helpers/tags'
87
import { getRunId } from '../../../envUtils'
98
import type { BrowserLog } from '../helpers/browser'
@@ -16,9 +15,10 @@ import { flushEvents } from './flushEvents'
1615
import type { Servers } from './httpServers'
1716
import { getTestServers, waitForServersIdle } from './httpServers'
1817
import type { SetupFactory, SetupOptions } from './pageSetups'
19-
import { DEFAULT_SETUPS, extensionSetup, npmSetup, reactSetup } from './pageSetups'
18+
import { asyncSetup, DEFAULT_SETUPS, npmSetup, reactSetup } from './pageSetups'
2019
import { createIntakeServerApp } from './serverApps/intake'
2120
import { createMockServerApp } from './serverApps/mock'
21+
import type { Extension } from './createExtension'
2222

2323
export const DEFAULT_RUM_CONFIGURATION = {
2424
applicationId: APPLICATION_ID,
@@ -76,6 +76,10 @@ class TestBuilder {
7676
private eventBridge = false
7777
private setups: Array<{ factory: SetupFactory; name?: string }> = DEFAULT_SETUPS
7878
private testFixture: typeof test = test
79+
private extension: {
80+
rumConfiguration?: RumInitConfiguration
81+
logsConfiguration?: LogsInitConfiguration
82+
} = {}
7983

8084
constructor(private title: string) {}
8185

@@ -129,9 +133,17 @@ class TestBuilder {
129133
return this
130134
}
131135

132-
withExtension(extensionPath: string) {
133-
this.testFixture = createExtensionTest(extensionPath)
134-
this.setups = [{ factory: (options, servers) => extensionSetup(options, servers) }]
136+
withSetup(setup: SetupFactory) {
137+
this.setups = [{ factory: setup }]
138+
return this
139+
}
140+
141+
withExtension(extension: Extension) {
142+
this.testFixture = extension.fixture
143+
this.setups = [{ factory: asyncSetup, name: 'async' }]
144+
this.extension.rumConfiguration = extension.rumConfiguration
145+
this.extension.logsConfiguration = extension.logsConfiguration
146+
135147
return this
136148
}
137149

@@ -157,6 +169,7 @@ class TestBuilder {
157169
test_name: '<PLACEHOLDER>',
158170
},
159171
testFixture: this.testFixture,
172+
extension: this.extension,
160173
}
161174

162175
if (this.alsoRunWithRumSlim) {
@@ -203,7 +216,7 @@ function declareTestsForSetups(
203216
}
204217

205218
function declareTest(title: string, setupOptions: SetupOptions, factory: SetupFactory, runner: TestRunner) {
206-
const testFixture = setupOptions.testFixture
219+
const testFixture = setupOptions.testFixture ?? test
207220
testFixture(title, async ({ page, context }) => {
208221
const browserName = getBrowserName(test.info().project.name)
209222
addTag('test.browserName', browserName)

‎test/e2e/lib/framework/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { createTest, DEFAULT_RUM_CONFIGURATION, DEFAULT_LOGS_CONFIGURATION } from './createTest'
2-
export { bundleSetup, html, npmSetup, reactSetup } from './pageSetups'
2+
export { createExtension } from './createExtension'
3+
export { bundleSetup, html, npmSetup, reactSetup, formatConfiguration, createCrossOriginScriptUrls } from './pageSetups'
34
export { IntakeRegistry } from './intakeRegistry'
45
export { getTestServers, waitForServersIdle } from './httpServers'
56
export { flushEvents } from './flushEvents'

0 commit comments

Comments
 (0)