Skip to content

Commit 3551d57

Browse files
aaronshimjkrems
authored andcommitted
test: e2e tests for autoCsp
(cherry picked from commit f6b8c5e)
1 parent aa627f7 commit 3551d57

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

tests/legacy-cli/e2e.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ WEBPACK_IGNORE_TESTS = [
5050
"tests/build/prerender/error-with-sourcemaps.js",
5151
"tests/build/server-rendering/server-routes-*",
5252
"tests/build/wasm-esm.js",
53+
"tests/build/auto-csp*",
5354
]
5455

5556
def _to_glob(patterns):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import assert from 'node:assert';
2+
import { getGlobalVariable } from '../../utils/env';
3+
import { expectFileToMatch, writeMultipleFiles } from '../../utils/fs';
4+
import { findFreePort } from '../../utils/network';
5+
import { execAndWaitForOutputToMatch, ng } from '../../utils/process';
6+
import { updateJsonFile } from '../../utils/project';
7+
8+
const CSP_META_TAG = /<meta http-equiv="Content-Security-Policy"/;
9+
10+
export default async function () {
11+
assert(
12+
getGlobalVariable('argv')['esbuild'],
13+
'This test should not be called in the Webpack suite.',
14+
);
15+
16+
// Turn on auto-CSP
17+
await updateJsonFile('angular.json', (json) => {
18+
const build = json['projects']['test-project']['architect']['build'];
19+
build.options = {
20+
...build.options,
21+
security: { autoCsp: true },
22+
};
23+
});
24+
25+
await writeMultipleFiles({
26+
'serve.js': `
27+
const express = require('express');
28+
const path = require('path');
29+
30+
const app = express();
31+
const PORT = process.env.PORT || 3000;
32+
33+
app.use(express.static(path.join(__dirname, 'dist/test-project/browser')));
34+
35+
app.listen(PORT, () => {
36+
console.log('Node Express server listening on ' + PORT);
37+
});
38+
`,
39+
'public/script1.js': `
40+
const externalScriptCreated = 1337;
41+
console.warn('First External Script: ' + inlineScriptBodyCreated);
42+
`,
43+
'public/script2.js': `console.warn('Second External Script: ' + externalScriptCreated);`,
44+
'src/index.html': `
45+
<!doctype html>
46+
<html lang="en">
47+
<head>
48+
<meta charset="utf-8">
49+
<base href="/">
50+
<script>
51+
const inlineScriptHeadCreated = 1339;
52+
console.warn("Inline Script Head");
53+
</script>
54+
</head>
55+
<body>
56+
<app-root></app-root>
57+
58+
<script>
59+
const inlineScriptBodyCreated = 1338;
60+
console.warn("Inline Script Body: " + inlineScriptHeadCreated);
61+
</script>
62+
<script src='script1.js'></script>
63+
<script src='script2.js'></script>
64+
</body>
65+
</html>
66+
`,
67+
'e2e/src/app.e2e-spec.ts': `
68+
import { browser, by, element } from 'protractor';
69+
import * as webdriver from 'selenium-webdriver';
70+
71+
function allConsoleWarnMessagesAndErrors() {
72+
return browser
73+
.manage()
74+
.logs()
75+
.get('browser')
76+
.then(function (browserLog: any[]) {
77+
const warnMessages: any[] = [];
78+
browserLog.filter((logEntry) => {
79+
const msg = logEntry.message;
80+
console.log('>> ' + msg);
81+
if (logEntry.level.value >= webdriver.logging.Level.INFO.value) {
82+
warnMessages.push(msg);
83+
}
84+
});
85+
return warnMessages;
86+
});
87+
}
88+
89+
describe('Hello world E2E Tests', () => {
90+
beforeAll(async () => {
91+
await browser.waitForAngularEnabled(true);
92+
});
93+
94+
it('should display: Welcome and run all scripts in order', async () => {
95+
// Load the page without waiting for Angular since it is not bootstrapped automatically.
96+
await browser.driver.get(browser.baseUrl);
97+
98+
// Test the contents.
99+
expect(await element(by.css('h1')).getText()).toMatch('Hello');
100+
101+
// Make sure all scripts ran and there were no client side errors.
102+
const consoleMessages = await allConsoleWarnMessagesAndErrors();
103+
expect(consoleMessages.length).toEqual(4); // No additional errors
104+
// Extract just the printed messages from the console data.
105+
const printedMessages = consoleMessages.map(m => m.match(/"(.*?)"/)[1]);
106+
expect(printedMessages).toEqual([
107+
// All messages printed in order because execution order is preserved.
108+
"Inline Script Head",
109+
"Inline Script Body: 1339",
110+
"First External Script: 1338",
111+
"Second External Script: 1337",
112+
]);
113+
});
114+
});
115+
`,
116+
});
117+
118+
async function spawnServer(): Promise<number> {
119+
const port = await findFreePort();
120+
121+
await execAndWaitForOutputToMatch('node', ['serve.js'], /Node Express server listening on/, {
122+
'PORT': String(port),
123+
});
124+
125+
return port;
126+
}
127+
128+
await ng('build');
129+
130+
// Make sure the output files have auto-CSP as a result of `ng build`
131+
await expectFileToMatch('dist/test-project/browser/index.html', CSP_META_TAG);
132+
133+
// Make sure that our e2e protractor tests run to confirm that our angular project runs.
134+
const port = await spawnServer();
135+
await ng('e2e', `--base-url=http://localhost:${port}`, '--dev-server-target=');
136+
}

0 commit comments

Comments
 (0)